public override byte[] ConvertToBytes(GlyphLine glyphLine) { if (glyphLine != null) { byte[] bytes = new byte[glyphLine.Size()]; int ptr = 0; if (fontEncoding.IsFontSpecific()) { for (int i = 0; i < glyphLine.Size(); i++) { bytes[ptr++] = (byte)glyphLine.Get(i).GetCode(); } } else { for (int i = 0; i < glyphLine.Size(); i++) { if (fontEncoding.CanEncode(glyphLine.Get(i).GetUnicode())) { bytes[ptr++] = (byte)fontEncoding.ConvertToByte(glyphLine.Get(i).GetUnicode()); } } } bytes = ArrayUtil.ShortenArray(bytes, ptr); foreach (byte b in bytes) { shortTag[b & 0xff] = 1; } return(bytes); } else { return(EMPTY_BYTES); } }
internal static TrueTypeFont CreateFontProgram(PdfDictionary fontDictionary, FontEncoding fontEncoding) { iText.Kernel.Font.DocTrueTypeFont fontProgram = new iText.Kernel.Font.DocTrueTypeFont(fontDictionary); FillFontDescriptor(fontProgram, fontDictionary.GetAsDictionary(PdfName.FontDescriptor)); PdfNumber firstCharNumber = fontDictionary.GetAsNumber(PdfName.FirstChar); int firstChar = firstCharNumber != null?Math.Max(firstCharNumber.IntValue(), 0) : 0; int[] widths = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, fontProgram .GetMissingWidth()); fontProgram.avgWidth = 0; int glyphsWithWidths = 0; for (int i = 0; i < 256; i++) { Glyph glyph = new Glyph(i, widths[i], fontEncoding.GetUnicode(i)); fontProgram.codeToGlyph[i] = glyph; //FontEncoding.codeToUnicode table has higher priority if (glyph.HasValidUnicode() && fontEncoding.ConvertToByte(glyph.GetUnicode()) == i) { fontProgram.unicodeToGlyph[glyph.GetUnicode()] = glyph; } if (widths[i] > 0) { glyphsWithWidths++; fontProgram.avgWidth += widths[i]; } } if (glyphsWithWidths != 0) { fontProgram.avgWidth /= glyphsWithWidths; } return(fontProgram); }
internal static Type1Font CreateFontProgram(PdfDictionary fontDictionary, FontEncoding fontEncoding, CMapToUnicode toUnicode) { PdfName baseFontName = fontDictionary.GetAsName(PdfName.BaseFont); String baseFont; if (baseFontName != null) { baseFont = baseFontName.GetValue(); } else { baseFont = FontUtil.CreateRandomFontName(); } if (!fontDictionary.ContainsKey(PdfName.FontDescriptor)) { Type1Font type1StdFont; try { //if there are no font modifiers, cached font could be used, //otherwise a new instance should be created. type1StdFont = (Type1Font)FontProgramFactory.CreateFont(baseFont, true); } catch (Exception) { type1StdFont = null; } if (type1StdFont != null) { return(type1StdFont); } } iText.Kernel.Font.DocType1Font fontProgram = new iText.Kernel.Font.DocType1Font(baseFont); PdfDictionary fontDesc = fontDictionary.GetAsDictionary(PdfName.FontDescriptor); fontProgram.subtype = fontDesc.GetAsName(PdfName.Subtype); FillFontDescriptor(fontProgram, fontDesc); PdfNumber firstCharNumber = fontDictionary.GetAsNumber(PdfName.FirstChar); int firstChar = firstCharNumber != null?Math.Max(firstCharNumber.IntValue(), 0) : 0; int[] widths = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, fontProgram .GetMissingWidth()); fontProgram.avgWidth = 0; int glyphsWithWidths = 0; for (int i = 0; i < 256; i++) { Glyph glyph = new Glyph(i, widths[i], fontEncoding.GetUnicode(i)); fontProgram.codeToGlyph[i] = glyph; if (glyph.HasValidUnicode()) { //FontEncoding.codeToUnicode table has higher priority if (fontEncoding.ConvertToByte(glyph.GetUnicode()) == i) { fontProgram.unicodeToGlyph[glyph.GetUnicode()] = glyph; } } else { if (toUnicode != null) { glyph.SetChars(toUnicode.Lookup(i)); } } if (widths[i] > 0) { glyphsWithWidths++; fontProgram.avgWidth += widths[i]; } } if (glyphsWithWidths != 0) { fontProgram.avgWidth /= glyphsWithWidths; } return(fontProgram); }