internal void RegisterAsComposite( PDF pdf, Font font, Stream inputStream, bool embed) { otf = new OTF(inputStream); if (embed) { EmbedFontFile(pdf, font, otf, false); } AddFontDescriptorObject(pdf, font, otf, embed); AddCIDFontDictionaryObject(pdf, font, otf); AddToUnicodeCMapObject(pdf, font, otf); // Type0 Font Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); pdf.Append(otf.fontName); pdf.Append('\n'); pdf.Append("/Encoding /Identity-H\n"); pdf.Append("/DescendantFonts ["); pdf.Append(font.GetCidFontDictObjNumber()); pdf.Append(" 0 R]\n"); pdf.Append("/ToUnicode "); pdf.Append(font.GetToUnicodeCMapObjNumber()); pdf.Append(" 0 R\n"); pdf.Append(">>\n"); pdf.Endobj(); font.objNumber = pdf.objNumber; }
internal static void Register( PDF pdf, Font font, Stream inputStream) { font.name = DejaVuLGCSerif.name; font.unitsPerEm = DejaVuLGCSerif.unitsPerEm; font.bBoxLLx = DejaVuLGCSerif.bBoxLLx; font.bBoxLLy = DejaVuLGCSerif.bBoxLLy; font.bBoxURx = DejaVuLGCSerif.bBoxURx; font.bBoxURy = DejaVuLGCSerif.bBoxURy; font.ascent = DejaVuLGCSerif.ascent; font.descent = DejaVuLGCSerif.descent; font.capHeight = DejaVuLGCSerif.capHeight; font.firstChar = DejaVuLGCSerif.firstChar; font.lastChar = DejaVuLGCSerif.lastChar; font.underlinePosition = DejaVuLGCSerif.underlinePosition; font.underlineThickness = DejaVuLGCSerif.underlineThickness; font.compressed_size = DejaVuLGCSerif.compressed_size; font.uncompressed_size = DejaVuLGCSerif.uncompressed_size; font.advanceWidth = DecodeRLE(DejaVuLGCSerif.advanceWidth); font.glyphWidth = DecodeRLE(DejaVuLGCSerif.glyphWidth); font.unicodeToGID = DecodeRLE(DejaVuLGCSerif.unicodeToGID); EmbedFontFile(pdf, font, inputStream); AddFontDescriptorObject(pdf, font); AddCIDFontDictionaryObject(pdf, font); AddToUnicodeCMapObject(pdf, font); // Type0 Font Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); pdf.Append(font.name); pdf.Append('\n'); pdf.Append("/Encoding /Identity-H\n"); pdf.Append("/DescendantFonts ["); pdf.Append(font.GetCidFontDictObjNumber()); pdf.Append(" 0 R]\n"); pdf.Append("/ToUnicode "); pdf.Append(font.GetToUnicodeCMapObjNumber()); pdf.Append(" 0 R\n"); pdf.Append(">>\n"); pdf.Endobj(); font.objNumber = pdf.objNumber; }
private static void AddFontDescriptorObject(PDF pdf, Font font) { float factor = 1000f / font.unitsPerEm; for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(font.name) && f.GetFontDescriptorObjNumber() != -1) { font.SetFontDescriptorObjNumber(f.GetFontDescriptorObjNumber()); return; } } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /FontDescriptor\n"); pdf.Append("/FontName /"); pdf.Append(font.name); pdf.Append('\n'); pdf.Append("/FontFile2 "); pdf.Append(font.fileObjNumber); pdf.Append(" 0 R\n"); pdf.Append("/Flags 32\n"); pdf.Append("/FontBBox ["); pdf.Append(font.bBoxLLx * factor); pdf.Append(' '); pdf.Append(font.bBoxLLy * factor); pdf.Append(' '); pdf.Append(font.bBoxURx * factor); pdf.Append(' '); pdf.Append(font.bBoxURy * factor); pdf.Append("]\n"); pdf.Append("/Ascent "); pdf.Append(font.ascent * factor); pdf.Append('\n'); pdf.Append("/Descent "); pdf.Append(font.descent * factor); pdf.Append('\n'); pdf.Append("/ItalicAngle 0\n"); pdf.Append("/CapHeight "); pdf.Append(font.capHeight * factor); pdf.Append('\n'); pdf.Append("/StemV 79\n"); pdf.Append(">>\n"); pdf.Endobj(); font.SetFontDescriptorObjNumber(pdf.objNumber); }
private static void EmbedFontFile(PDF pdf, Font font, Stream inputStream) { // Check if the font file is already embedded for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(font.name) && f.fileObjNumber != -1) { font.fileObjNumber = f.fileObjNumber; return; } } /* * int metadataObjNumber = pdf.AddMetadataObject(DejaVu.FONT_LICENSE, true); */ pdf.Newobj(); pdf.Append("<<\n"); /* * pdf.Append("/Metadata "); * pdf.Append(metadataObjNumber); * pdf.Append(" 0 R\n"); */ pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Length "); pdf.Append(font.compressed_size); pdf.Append("\n"); pdf.Append("/Length1 "); pdf.Append(font.uncompressed_size); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf = new byte[2048]; int len; while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) { pdf.Append(buf, 0, len); } inputStream.Dispose(); pdf.Append("\nendstream\n"); pdf.Endobj(); font.fileObjNumber = pdf.objNumber; }
private static void AddCIDFontDictionaryObject(PDF pdf, Font font) { for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(font.name) && f.GetCidFontDictObjNumber() != -1) { font.SetCidFontDictObjNumber(f.GetCidFontDictObjNumber()); return; } } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /CIDFontType2\n"); pdf.Append("/BaseFont /"); pdf.Append(font.name); pdf.Append('\n'); pdf.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>>\n"); pdf.Append("/FontDescriptor "); pdf.Append(font.GetFontDescriptorObjNumber()); pdf.Append(" 0 R\n"); pdf.Append("/DW "); pdf.Append((int) ((1000f / font.unitsPerEm) * font.advanceWidth[0])); pdf.Append('\n'); pdf.Append("/W [0[\n"); for (int i = 0; i < font.advanceWidth.Length; i++) { pdf.Append((int) ((1000f / font.unitsPerEm) * font.advanceWidth[i])); if ((i + 1) % 10 == 0) { pdf.Append('\n'); } else { pdf.Append(' '); } } pdf.Append("]]\n"); pdf.Append("/CIDToGIDMap /Identity\n"); pdf.Append(">>\n"); pdf.Endobj(); font.SetCidFontDictObjNumber(pdf.objNumber); }
private static void AddCIDFontDictionaryObject(PDF pdf, Font font) { foreach (Font f in pdf.fonts) { if (f.cidFontDictObjNumber != 0 && f.name.Equals(font.name)) { font.cidFontDictObjNumber = f.cidFontDictObjNumber; return; } } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); if (font.cff) { pdf.Append("/Subtype /CIDFontType0\n"); } else { pdf.Append("/Subtype /CIDFontType2\n"); } pdf.Append("/BaseFont /"); pdf.Append(font.name); pdf.Append('\n'); pdf.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>>\n"); pdf.Append("/FontDescriptor "); pdf.Append(font.fontDescriptorObjNumber); pdf.Append(" 0 R\n"); pdf.Append("/DW "); pdf.Append((int) ((1000f / font.unitsPerEm) * font.advanceWidth[0])); pdf.Append('\n'); pdf.Append("/W [0[\n"); for (int i = 0; i < font.advanceWidth.Length; i++) { pdf.Append((int) ((1000f / font.unitsPerEm) * font.advanceWidth[i])); pdf.Append(' '); } pdf.Append("]]\n"); pdf.Append("/CIDToGIDMap /Identity\n"); pdf.Append(">>\n"); pdf.Endobj(); font.cidFontDictObjNumber = pdf.GetObjNumber(); }
private void AddEncodingObject( PDF pdf, Font font, int codePage) { pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Encoding\n"); pdf.Append("/BaseEncoding /WinAnsiEncoding\n"); pdf.Append("/Differences [127\n"); for (int i = 0; i < 129; i++) { if (codePage == 0) { pdf.Append(CP1250.names[i]); } else if (codePage == 1) { pdf.Append(CP1251.names[i]); } else if (codePage == 2) { pdf.Append(CP1252.names[i]); } else if (codePage == 3) { pdf.Append(CP1253.names[i]); } else if (codePage == 4) { pdf.Append(CP1254.names[i]); } else if (codePage == 7) { pdf.Append(CP1257.names[i]); } pdf.Append(' '); } pdf.Append("]\n"); pdf.Append(">>\n"); pdf.Endobj(); font.SetEncodingObjNumber(pdf.objNumber); }
private void AddCoreFont(PDF pdf, CoreFont coreFont) { StandardFont font = StandardFont.GetInstance(coreFont); this.name = font.name; this.bBoxLLx = font.bBoxLLx; this.bBoxLLy = font.bBoxLLy; this.bBoxURx = font.bBoxURx; this.bBoxURy = font.bBoxURy; this.fontUnderlinePosition = font.underlinePosition; this.fontUnderlineThickness = font.underlineThickness; this.metrics = font.metrics; pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type1\n"); pdf.Append("/BaseFont /"); pdf.Append(this.name); pdf.Append('\n'); if (!this.name.Equals("Symbol") && !this.name.Equals("ZapfDingbats")) { pdf.Append("/Encoding /WinAnsiEncoding\n"); } pdf.Append(">>\n"); pdf.Endobj(); objNumber = pdf.objNumber; ascent = bBoxURy * size / unitsPerEm; descent = bBoxLLy * size / unitsPerEm; body_height = ascent - descent; underlineThickness = fontUnderlineThickness * size / unitsPerEm; underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0f; pdf.fonts.Add(this); }
internal void RegisterAsSimple( PDF pdf, Font font, Stream inputStream, int codePage, bool embed) { otf = new OTF(inputStream); if (embed) { EmbedFontFile(pdf, font, otf, true); } AddFontDescriptorObject(pdf, font, otf, embed); AddWidthsArrayObject(pdf, font, otf, codePage); AddEncodingObject(pdf, font, codePage); // Simple font object pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); if (otf.cff) { pdf.Append("/Subtype /Type1\n"); } else { pdf.Append("/Subtype /TrueType\n"); } pdf.Append("/BaseFont /"); pdf.Append(otf.fontName); pdf.Append('\n'); pdf.Append("/FirstChar "); pdf.Append(otf.firstChar); pdf.Append('\n'); pdf.Append("/LastChar "); pdf.Append(255); pdf.Append('\n'); pdf.Append("/Encoding "); pdf.Append(font.GetEncodingObjNumber()); pdf.Append(" 0 R\n"); pdf.Append("/Widths "); pdf.Append(font.GetWidthsArrayObjNumber()); pdf.Append(" 0 R\n"); pdf.Append("/FontDescriptor "); pdf.Append(font.GetFontDescriptorObjNumber()); pdf.Append(" 0 R\n"); pdf.Append(">>\n"); pdf.Endobj(); font.objNumber = pdf.objNumber; }
private void EmbedFontFile( PDF pdf, Font font, OTF otf, bool simpleFont) { // Check if the font file is already embedded for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(otf.fontName) && f.fileObjNumber != -1) { font.fileObjNumber = f.fileObjNumber; return; } } int metadataObjNumber = -1; if (otf.fontName.ToLower().IndexOf("droid") != -1 || otf.fontName.ToLower().IndexOf("roboto") != -1) { metadataObjNumber = pdf.AddMetadataObject(AndroidFontsCopyright.NOTICE, true); } pdf.Newobj(); pdf.Append("<<\n"); if (otf.cff) { if (simpleFont) { pdf.Append("/Subtype /Type1C\n"); } else { pdf.Append("/Subtype /CIDFontType0C\n"); } } pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Length "); pdf.Append(otf.baos.Length); pdf.Append("\n"); if (!otf.cff) { pdf.Append("/Length1 "); pdf.Append(otf.buf.Length); pdf.Append('\n'); } if (metadataObjNumber != -1) { pdf.Append("/Metadata "); pdf.Append(metadataObjNumber); pdf.Append(" 0 R\n"); } pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(otf.baos); pdf.Append("\nendstream\n"); pdf.Endobj(); font.fileObjNumber = pdf.objNumber; }
private void AddWidthsArrayObject( PDF pdf, Font font, OTF otf, int codePage) { pdf.Newobj(); pdf.Append("[ "); int n = 1; for (int c = otf.firstChar; c < 256; c++) { if (c < 127) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[c])); } else { if (codePage == 0) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1250.codes[c - 127]])); } else if (codePage == 1) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1251.codes[c - 127]])); } else if (codePage == 2) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1252.codes[c - 127]])); } else if (codePage == 3) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1253.codes[c - 127]])); } else if (codePage == 4) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1254.codes[c - 127]])); } else if (codePage == 7) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1257.codes[c - 127]])); } } if (n < 10) { pdf.Append(' '); ++n; } else { pdf.Append('\n'); n = 1; } } pdf.Append("]\n"); pdf.Endobj(); font.SetWidthsArrayObjNumber(pdf.objNumber); }
private static void AddToUnicodeCMapObject(PDF pdf, Font font) { for (int i = 0; i < pdf.fonts.Count; i++) { Font font2 = pdf.fonts[i]; if (font2.name.Equals(font.name) && font2.GetToUnicodeCMapObjNumber() != -1) { font.SetToUnicodeCMapObjNumber(font2.GetToUnicodeCMapObjNumber()); return; } } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("/CIDInit /ProcSet findresource begin\n"); stringBuilder.Append("12 dict begin\n"); stringBuilder.Append("begincmap\n"); stringBuilder.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n"); stringBuilder.Append("/CMapName /Adobe-Identity def\n"); stringBuilder.Append("/CMapType 2 def\n"); stringBuilder.Append("1 begincodespacerange\n"); stringBuilder.Append("<0000> <FFFF>\n"); stringBuilder.Append("endcodespacerange\n"); List <string> list = new List <string>(); StringBuilder stringBuilder2 = new StringBuilder(); for (int j = 0; j <= 65535; j++) { int num = font.unicodeToGID[j]; if (num > 0) { stringBuilder2.Append('<'); stringBuilder2.Append(FastFont.ToHexString(num)); stringBuilder2.Append("> <"); stringBuilder2.Append(FastFont.ToHexString(j)); stringBuilder2.Append(">\n"); list.Add(stringBuilder2.ToString()); stringBuilder2.Length = 0; if (list.Count == 100) { FastFont.WriteListToBuffer(list, stringBuilder); } } } if (list.Count > 0) { FastFont.WriteListToBuffer(list, stringBuilder); } stringBuilder.Append("endcmap\n"); stringBuilder.Append("CMapName currentdict /CMap defineresource pop\n"); stringBuilder.Append("end\nend"); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Length "); pdf.Append(stringBuilder.Length); pdf.Append("\n"); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(stringBuilder.ToString()); pdf.Append("\nendstream\n"); pdf.Endobj(); font.SetToUnicodeCMapObjNumber(pdf.objNumber); }
private void AddImage( PDF pdf, byte[] data, byte[] alpha, int imageType, String colorSpace, int bitsPerComponent) { if (alpha != null) { AddSoftMask(pdf, alpha, "DeviceGray", 8); } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); if (imageType == ImageType.JPG) { pdf.Append("/Filter /DCTDecode\n"); } else if (imageType == ImageType.PNG || imageType == ImageType.BMP) { pdf.Append("/Filter /FlateDecode\n"); if (alpha != null) { pdf.Append("/SMask "); pdf.Append(objNumber); pdf.Append(" 0 R\n"); } } pdf.Append("/Width "); pdf.Append(( int ) w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(( int ) h); pdf.Append('\n'); pdf.Append("/ColorSpace /"); pdf.Append(colorSpace); pdf.Append('\n'); pdf.Append("/BitsPerComponent "); pdf.Append(bitsPerComponent); pdf.Append('\n'); if (colorSpace.Equals("DeviceCMYK")) { // If the image was created with Photoshop - invert the colors: pdf.Append("/Decode [1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0]\n"); } pdf.Append("/Length "); pdf.Append(data.Length); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(data, 0, data.Length); pdf.Append("\nendstream\n"); pdf.Endobj(); pdf.images.Add(this); objNumber = pdf.objNumber; }
private void AddWidthsArrayObject( PDF pdf, Font font, OTF otf, int codePage) { pdf.Newobj(); pdf.Append("[ "); int n = 1; for (int c = otf.firstChar; c < 256; c++) { if (c < 127) { pdf.Append((int) ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[c])); } else { if (codePage == 0) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1250.codes[c - 127]])); } else if (codePage == 1) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1251.codes[c - 127]])); } else if (codePage == 2) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1252.codes[c - 127]])); } else if (codePage == 3) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1253.codes[c - 127]])); } else if (codePage == 4) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1254.codes[c - 127]])); } else if (codePage == 7) { pdf.Append((int)((1000.0f / otf.unitsPerEm) * otf.glyphWidth[CP1257.codes[c - 127]])); } } if (n < 10) { pdf.Append(' '); ++n; } else { pdf.Append('\n'); n = 1; } } pdf.Append("]\n"); pdf.Endobj(); font.SetWidthsArrayObjNumber(pdf.objNumber); }
private static void AddToUnicodeCMapObject(PDF pdf, Font font) { for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(font.name) && f.GetToUnicodeCMapObjNumber() != -1) { font.SetToUnicodeCMapObjNumber(f.GetToUnicodeCMapObjNumber()); return; } } StringBuilder sb = new StringBuilder(); sb.Append("/CIDInit /ProcSet findresource begin\n"); sb.Append("12 dict begin\n"); sb.Append("begincmap\n"); sb.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n"); sb.Append("/CMapName /Adobe-Identity def\n"); sb.Append("/CMapType 2 def\n"); sb.Append("1 begincodespacerange\n"); sb.Append("<0000> <FFFF>\n"); sb.Append("endcodespacerange\n"); List <String> list = new List <String>(); StringBuilder buf = new StringBuilder(); for (int cid = 0; cid <= 0xffff; cid++) { int gid = font.unicodeToGID[cid]; if (gid > 0) { buf.Append('<'); buf.Append(ToHexString(gid)); buf.Append("> <"); buf.Append(ToHexString(cid)); buf.Append(">\n"); list.Add(buf.ToString()); buf.Length = 0; if (list.Count == 100) { WriteListToBuffer(list, sb); } } } if (list.Count > 0) { WriteListToBuffer(list, sb); } sb.Append("endcmap\n"); sb.Append("CMapName currentdict /CMap defineresource pop\n"); sb.Append("end\nend"); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Length "); pdf.Append(sb.Length); pdf.Append("\n"); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(sb.ToString()); pdf.Append("\nendstream\n"); pdf.Endobj(); font.SetToUnicodeCMapObjNumber(pdf.objNumber); }
// Constructor for CJK fonts public Font(PDF pdf, String fontName) { this.name = fontName; this.isCJK = true; this.firstChar = 0x0020; this.lastChar = 0xFFEE; this.ascent = this.size; this.descent = this.size / 4; this.bodyHeight = this.ascent + this.descent; // Font Descriptor pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /FontDescriptor\n"); pdf.Append("/FontName /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/Flags 4\n"); pdf.Append("/FontBBox [0 0 0 0]\n"); pdf.Append(">>\n"); pdf.Endobj(); // CIDFont Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /CIDFontType0\n"); pdf.Append("/BaseFont /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/FontDescriptor "); pdf.Append(pdf.GetObjNumber() - 1); pdf.Append(" 0 R\n"); pdf.Append("/CIDSystemInfo <<\n"); pdf.Append("/Registry (Adobe)\n"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append("/Ordering (CNS1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append("/Ordering (GB1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("KozMinPro")) { pdf.Append("/Ordering (Japan1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("AdobeMyungjoStd")) { pdf.Append("/Ordering (Korea1)\n"); pdf.Append("/Supplement 1\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.Append(">>\n"); pdf.Append(">>\n"); pdf.Endobj(); // Type0 Font Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append(fontName + "-UniCNS-UTF16-H\n"); pdf.Append("/Encoding /UniCNS-UTF16-H\n"); } else if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append(fontName + "-UniGB-UTF16-H\n"); pdf.Append("/Encoding /UniGB-UTF16-H\n"); } else if (fontName.StartsWith("KozMinPro")) { pdf.Append(fontName + "-UniJIS-UCS2-H\n"); pdf.Append("/Encoding /UniJIS-UCS2-H\n"); } else if (fontName.StartsWith("AdobeMyungjoStd")) { pdf.Append(fontName + "-UniKS-UCS2-H\n"); pdf.Append("/Encoding /UniKS-UCS2-H\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.Append("/DescendantFonts ["); pdf.Append(pdf.GetObjNumber() - 1); pdf.Append(" 0 R]\n"); pdf.Append(">>\n"); pdf.Endobj(); objNumber = pdf.GetObjNumber(); pdf.fonts.Add(this); }
/** * Constructor for the 14 standard fonts. * Creates a font object and adds it to the PDF. * * <pre> * Examples: * Font font1 = new Font(pdf, CoreFont.HELVETICA); * Font font2 = new Font(pdf, CoreFont.TIMES_ITALIC); * Font font3 = new Font(pdf, CoreFont.ZAPF_DINGBATS); * ... * </pre> * * @param pdf the PDF to add this font to. * @param coreFont the core font. Must be one the names defined in the CoreFont class. */ public Font(PDF pdf, CoreFont coreFont) { StandardFont font = StandardFont.GetInstance(coreFont); this.name = font.name; this.bBoxLLx = font.bBoxLLx; this.bBoxLLy = font.bBoxLLy; this.bBoxURx = font.bBoxURx; this.bBoxURy = font.bBoxURy; this.fontUnderlinePosition = font.underlinePosition; this.fontUnderlineThickness = font.underlineThickness; this.metrics = font.metrics; this.ascent = bBoxURy * size / unitsPerEm; this.descent = bBoxLLy * size / unitsPerEm; this.body_height = ascent - descent; this.underlineThickness = fontUnderlineThickness * size / unitsPerEm; this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0f; pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type1\n"); pdf.Append("/BaseFont /"); pdf.Append(this.name); pdf.Append('\n'); if (!this.name.Equals("Symbol") && !this.name.Equals("ZapfDingbats")) { pdf.Append("/Encoding /WinAnsiEncoding\n"); } pdf.Append(">>\n"); pdf.Endobj(); objNumber = pdf.objNumber; pdf.fonts.Add(this); }
private void AddSoftMask( PDF pdf, byte[] data, String colorSpace, int bitsPerComponent) { pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Width "); pdf.Append(( int ) w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(( int ) h); pdf.Append('\n'); pdf.Append("/ColorSpace /"); pdf.Append(colorSpace); pdf.Append('\n'); pdf.Append("/BitsPerComponent "); pdf.Append(bitsPerComponent); pdf.Append('\n'); pdf.Append("/Length "); pdf.Append(data.Length); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(data, 0, data.Length); pdf.Append("\nendstream\n"); pdf.Endobj(); objNumber = pdf.objNumber; }
private void AddImage(PDF pdf, Stream inputStream) { w = GetInt(inputStream); // Width h = GetInt(inputStream); // Height byte c = (byte) inputStream.ReadByte(); // Color Space byte a = (byte) inputStream.ReadByte(); // Alpha if (a != 0) { pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Width "); pdf.Append(w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(h); pdf.Append('\n'); pdf.Append("/ColorSpace /DeviceGray\n"); pdf.Append("/BitsPerComponent 8\n"); int length = GetInt(inputStream); pdf.Append("/Length "); pdf.Append(length); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf1 = new byte[length]; inputStream.Read(buf1, 0, length); pdf.Append(buf1, 0, length); pdf.Append("\nendstream\n"); pdf.Endobj(); objNumber = pdf.objNumber; } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); pdf.Append("/Filter /FlateDecode\n"); if (a != 0) { pdf.Append("/SMask "); pdf.Append(objNumber); pdf.Append(" 0 R\n"); } pdf.Append("/Width "); pdf.Append(w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(h); pdf.Append('\n'); pdf.Append("/ColorSpace /"); if (c == 1) { pdf.Append("DeviceGray"); } else if (c == 3 || c == 6) { pdf.Append("DeviceRGB"); } pdf.Append('\n'); pdf.Append("/BitsPerComponent 8\n"); pdf.Append("/Length "); pdf.Append(GetInt(inputStream)); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf2 = new byte[2048]; int count; while ((count = inputStream.Read(buf2, 0, buf2.Length)) > 0) { pdf.Append(buf2, 0, count); } pdf.Append("\nendstream\n"); pdf.Endobj(); pdf.images.Add(this); objNumber = pdf.objNumber; }
private void AddImage(PDF pdf, Stream inputStream) { w = GetInt(inputStream); // Width h = GetInt(inputStream); // Height byte c = (byte)inputStream.ReadByte(); // Color Space byte a = (byte)inputStream.ReadByte(); // Alpha if (a != 0) { pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Width "); pdf.Append(w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(h); pdf.Append('\n'); pdf.Append("/ColorSpace /DeviceGray\n"); pdf.Append("/BitsPerComponent 8\n"); int length = GetInt(inputStream); pdf.Append("/Length "); pdf.Append(length); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf1 = new byte[length]; inputStream.Read(buf1, 0, length); pdf.Append(buf1, 0, length); pdf.Append("\nendstream\n"); pdf.Endobj(); objNumber = pdf.objNumber; } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /XObject\n"); pdf.Append("/Subtype /Image\n"); pdf.Append("/Filter /FlateDecode\n"); if (a != 0) { pdf.Append("/SMask "); pdf.Append(objNumber); pdf.Append(" 0 R\n"); } pdf.Append("/Width "); pdf.Append(w); pdf.Append('\n'); pdf.Append("/Height "); pdf.Append(h); pdf.Append('\n'); pdf.Append("/ColorSpace /"); if (c == 1) { pdf.Append("DeviceGray"); } else if (c == 3 || c == 6) { pdf.Append("DeviceRGB"); } pdf.Append('\n'); pdf.Append("/BitsPerComponent 8\n"); pdf.Append("/Length "); pdf.Append(GetInt(inputStream)); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf2 = new byte[2048]; int count; while ((count = inputStream.Read(buf2, 0, buf2.Length)) > 0) { pdf.Append(buf2, 0, count); } pdf.Append("\nendstream\n"); pdf.Endobj(); pdf.images.Add(this); objNumber = pdf.objNumber; }
internal static void Register( PDF pdf, Font font, Stream inputStream) { int len = inputStream.ReadByte(); byte[] fontName = new byte[len]; inputStream.Read(fontName, 0, len); font.name = System.Text.Encoding.UTF8.GetString(fontName); // Console.WriteLine(font.name); font.unitsPerEm = GetInt32(inputStream); font.bBoxLLx = GetInt32(inputStream); font.bBoxLLy = GetInt32(inputStream); font.bBoxURx = GetInt32(inputStream); font.bBoxURy = GetInt32(inputStream); font.ascent = GetInt32(inputStream); font.descent = GetInt32(inputStream); font.firstChar = GetInt32(inputStream); font.lastChar = GetInt32(inputStream); font.capHeight = GetInt32(inputStream); font.underlinePosition = GetInt32(inputStream); font.underlineThickness = GetInt32(inputStream); len = GetInt32(inputStream); font.advanceWidth = new int[len]; for (int i = 0; i < len; i++) { font.advanceWidth[i] = GetInt16(inputStream); } len = GetInt32(inputStream); font.glyphWidth = new int[len]; for (int i = 0; i < len; i++) { font.glyphWidth[i] = GetInt16(inputStream); } len = GetInt32(inputStream); font.unicodeToGID = new int[len]; for (int i = 0; i < len; i++) { font.unicodeToGID[i] = GetInt16(inputStream); } font.uncompressed_size = GetInt32(inputStream); font.compressed_size = GetInt32(inputStream); EmbedFontFile(pdf, font, inputStream); AddFontDescriptorObject(pdf, font); AddCIDFontDictionaryObject(pdf, font); AddToUnicodeCMapObject(pdf, font); // Type0 Font Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); pdf.Append(font.name); pdf.Append('\n'); pdf.Append("/Encoding /Identity-H\n"); pdf.Append("/DescendantFonts ["); pdf.Append(font.GetCidFontDictObjNumber()); pdf.Append(" 0 R]\n"); pdf.Append("/ToUnicode "); pdf.Append(font.GetToUnicodeCMapObjNumber()); pdf.Append(" 0 R\n"); pdf.Append(">>\n"); pdf.Endobj(); font.objNumber = pdf.objNumber; }
private void AddToUnicodeCMapObject( PDF pdf, Font font, OTF otf) { for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(otf.fontName) && f.GetToUnicodeCMapObjNumber() != -1) { font.SetToUnicodeCMapObjNumber(f.GetToUnicodeCMapObjNumber()); return; } } StringBuilder sb = new StringBuilder(); sb.Append("/CIDInit /ProcSet findresource begin\n"); sb.Append("12 dict begin\n"); sb.Append("begincmap\n"); sb.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n"); sb.Append("/CMapName /Adobe-Identity def\n"); sb.Append("/CMapType 2 def\n"); sb.Append("1 begincodespacerange\n"); sb.Append("<0000> <FFFF>\n"); sb.Append("endcodespacerange\n"); List<String> list = new List<String>(); StringBuilder buf = new StringBuilder(); for (int cid = 0; cid <= 0xffff; cid++) { int gid = otf.unicodeToGID[cid]; if (gid > 0) { buf.Append('<'); buf.Append(ToHexString(gid)); buf.Append("> <"); buf.Append(ToHexString(cid)); buf.Append(">\n"); list.Add(buf.ToString()); buf.Length = 0; if (list.Count == 100) { WriteListToBuffer(list, sb); } } } if (list.Count > 0) { WriteListToBuffer(list, sb); } sb.Append("endcmap\n"); sb.Append("CMapName currentdict /CMap defineresource pop\n"); sb.Append("end\nend"); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Length "); pdf.Append(sb.Length); pdf.Append("\n"); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(sb.ToString()); pdf.Append("\nendstream\n"); pdf.Endobj(); font.SetToUnicodeCMapObjNumber(pdf.objNumber); }
private void AddCIDFontDictionaryObject( PDF pdf, Font font, OTF otf) { for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(otf.fontName) && f.GetCidFontDictObjNumber() != -1) { font.SetCidFontDictObjNumber(f.GetCidFontDictObjNumber()); return; } } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); if (otf.cff) { pdf.Append("/Subtype /CIDFontType0\n"); } else { pdf.Append("/Subtype /CIDFontType2\n"); } pdf.Append("/BaseFont /"); pdf.Append(otf.fontName); pdf.Append('\n'); pdf.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>>\n"); pdf.Append("/FontDescriptor "); pdf.Append(font.GetFontDescriptorObjNumber()); pdf.Append(" 0 R\n"); pdf.Append("/DW "); pdf.Append((int) ((1000f / otf.unitsPerEm) * otf.advanceWidth[0])); pdf.Append('\n'); pdf.Append("/W [0[\n"); for (int i = 0; i < otf.advanceWidth.Length; i++) { pdf.Append((int) ((1000f / otf.unitsPerEm) * otf.advanceWidth[i])); if ((i + 1) % 10 == 0) { pdf.Append('\n'); } else { pdf.Append(' '); } } pdf.Append("]]\n"); pdf.Append("/CIDToGIDMap /Identity\n"); pdf.Append(">>\n"); pdf.Endobj(); font.SetCidFontDictObjNumber(pdf.objNumber); }
// Constructor for CJK fonts public Font(PDF pdf, String fontName, int codePage) { this.name = fontName; this.codePage = codePage; isCJK = true; isStandard = false; isComposite = true; firstChar = 0x0020; lastChar = 0xFFEE; // Font Descriptor pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /FontDescriptor\n"); pdf.Append("/FontName /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/Flags 4\n"); pdf.Append("/FontBBox [0 0 0 0]\n"); pdf.Append(">>\n"); pdf.Endobj(); // CIDFont Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /CIDFontType0\n"); pdf.Append("/BaseFont /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/FontDescriptor "); pdf.Append(pdf.objNumber - 1); pdf.Append(" 0 R\n"); pdf.Append("/CIDSystemInfo <<\n"); pdf.Append("/Registry (Adobe)\n"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append("/Ordering (CNS1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append("/Ordering (GB1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("KozMinPro")) { pdf.Append("/Ordering (Japan1)\n"); pdf.Append("/Supplement 4\n"); } else if (fontName.StartsWith("AdobeMyungjoStd")) { pdf.Append("/Ordering (Korea1)\n"); pdf.Append("/Supplement 1\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.Append(">>\n"); pdf.Append(">>\n"); pdf.Endobj(); // Type0 Font Dictionary pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append(fontName + "-UniCNS-UTF16-H\n"); pdf.Append("/Encoding /UniCNS-UTF16-H\n"); } else if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append(fontName + "-UniGB-UTF16-H\n"); pdf.Append("/Encoding /UniGB-UTF16-H\n"); } else if (fontName.StartsWith("KozMinPro")) { pdf.Append(fontName + "-UniJIS-UCS2-H\n"); pdf.Append("/Encoding /UniJIS-UCS2-H\n"); } else if (fontName.StartsWith("AdobeMyungjoStd")) { pdf.Append(fontName + "-UniKS-UCS2-H\n"); pdf.Append("/Encoding /UniKS-UCS2-H\n"); } else { throw new Exception("Unsupported font: " + fontName); } pdf.Append("/DescendantFonts ["); pdf.Append(pdf.objNumber - 1); pdf.Append(" 0 R]\n"); pdf.Append(">>\n"); pdf.Endobj(); objNumber = pdf.objNumber; ascent = size; descent = -ascent/4; body_height = ascent - descent; pdf.fonts.Add(this); }
private void AddFontDescriptorObject( PDF pdf, Font font, OTF otf, bool embed) { float factor = 1000f / otf.unitsPerEm; for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(otf.fontName) && f.GetFontDescriptorObjNumber() != -1) { font.SetFontDescriptorObjNumber(f.GetFontDescriptorObjNumber()); return; } } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /FontDescriptor\n"); pdf.Append("/FontName /"); pdf.Append(otf.fontName); pdf.Append('\n'); if (embed) { if (otf.cff) { pdf.Append("/FontFile3 "); } else { pdf.Append("/FontFile2 "); } pdf.Append(font.fileObjNumber); pdf.Append(" 0 R\n"); } pdf.Append("/Flags 32\n"); pdf.Append("/FontBBox ["); pdf.Append(otf.bBoxLLx * factor); pdf.Append(' '); pdf.Append(otf.bBoxLLy * factor); pdf.Append(' '); pdf.Append(otf.bBoxURx * factor); pdf.Append(' '); pdf.Append(otf.bBoxURy * factor); pdf.Append("]\n"); pdf.Append("/Ascent "); pdf.Append(otf.ascent * factor); pdf.Append('\n'); pdf.Append("/Descent "); pdf.Append(otf.descent * factor); pdf.Append('\n'); pdf.Append("/ItalicAngle 0\n"); pdf.Append("/CapHeight "); pdf.Append(otf.capHeight * factor); pdf.Append('\n'); pdf.Append("/StemV 79\n"); pdf.Append(">>\n"); pdf.Endobj(); font.SetFontDescriptorObjNumber(pdf.objNumber); }
public Font(PDF pdf, string fontName, int codePage) { this.name = fontName; this.codePage = codePage; this.isCJK = true; this.isStandard = false; this.isComposite = true; this.firstChar = 32; this.lastChar = 65518; pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /FontDescriptor\n"); pdf.Append("/FontName /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/Flags 4\n"); pdf.Append("/FontBBox [0 0 0 0]\n"); pdf.Append(">>\n"); pdf.Endobj(); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /CIDFontType0\n"); pdf.Append("/BaseFont /"); pdf.Append(fontName); pdf.Append('\n'); pdf.Append("/FontDescriptor "); pdf.Append(pdf.objNumber - 1); pdf.Append(" 0 R\n"); pdf.Append("/CIDSystemInfo <<\n"); pdf.Append("/Registry (Adobe)\n"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append("/Ordering (CNS1)\n"); pdf.Append("/Supplement 4\n"); } else { if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append("/Ordering (GB1)\n"); pdf.Append("/Supplement 4\n"); } else { if (fontName.StartsWith("KozMinPro")) { pdf.Append("/Ordering (Japan1)\n"); pdf.Append("/Supplement 4\n"); } else { if (!fontName.StartsWith("AdobeMyungjoStd")) { throw new Exception("Unsupported font: " + fontName); } pdf.Append("/Ordering (Korea1)\n"); pdf.Append("/Supplement 1\n"); } } } pdf.Append(">>\n"); pdf.Append(">>\n"); pdf.Endobj(); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Font\n"); pdf.Append("/Subtype /Type0\n"); pdf.Append("/BaseFont /"); if (fontName.StartsWith("AdobeMingStd")) { pdf.Append(fontName + "-UniCNS-UTF16-H\n"); pdf.Append("/Encoding /UniCNS-UTF16-H\n"); } else { if (fontName.StartsWith("AdobeSongStd") || fontName.StartsWith("STHeitiSC")) { pdf.Append(fontName + "-UniGB-UTF16-H\n"); pdf.Append("/Encoding /UniGB-UTF16-H\n"); } else { if (fontName.StartsWith("KozMinPro")) { pdf.Append(fontName + "-UniJIS-UCS2-H\n"); pdf.Append("/Encoding /UniJIS-UCS2-H\n"); } else { if (!fontName.StartsWith("AdobeMyungjoStd")) { throw new Exception("Unsupported font: " + fontName); } pdf.Append(fontName + "-UniKS-UCS2-H\n"); pdf.Append("/Encoding /UniKS-UCS2-H\n"); } } } pdf.Append("/DescendantFonts ["); pdf.Append(pdf.objNumber - 1); pdf.Append(" 0 R]\n"); pdf.Append(">>\n"); pdf.Endobj(); this.objNumber = pdf.objNumber; this.ascent = this.size; this.descent = -this.ascent / 4f; this.body_height = this.ascent - this.descent; pdf.fonts.Add(this); }
private static void EmbedFontFile(PDF pdf, Font font, Stream inputStream) { // Check if the font file is already embedded for (int i = 0; i < pdf.fonts.Count; i++) { Font f = pdf.fonts[i]; if (f.name.Equals(font.name) && f.fileObjNumber != -1) { font.fileObjNumber = f.fileObjNumber; return; } } /* int metadataObjNumber = pdf.AddMetadataObject(DejaVu.FONT_LICENSE, true); */ pdf.Newobj(); pdf.Append("<<\n"); /* pdf.Append("/Metadata "); pdf.Append(metadataObjNumber); pdf.Append(" 0 R\n"); */ pdf.Append("/Filter /FlateDecode\n"); pdf.Append("/Length "); pdf.Append(font.compressed_size); pdf.Append("\n"); pdf.Append("/Length1 "); pdf.Append(font.uncompressed_size); pdf.Append('\n'); pdf.Append(">>\n"); pdf.Append("stream\n"); byte[] buf = new byte[2048]; int len; while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) { pdf.Append(buf, 0, len); } inputStream.Dispose(); pdf.Append("\nendstream\n"); pdf.Endobj(); font.fileObjNumber = pdf.objNumber; }