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); }
/// <summary>Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.</summary> /// <param name="fontDictionary">a dictionary of type <code>/Font</code>, must have an indirect reference.</param> internal PdfType3Font(PdfDictionary fontDictionary) : base(fontDictionary) { subset = true; embedded = true; fontProgram = new Type3Font(false); CMapToUnicode toUni = FontUtil.ProcessToUnicode(fontDictionary.Get(PdfName.ToUnicode)); fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUni); PdfDictionary charProcsDic = GetPdfObject().GetAsDictionary(PdfName.CharProcs); PdfArray fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix); if (GetPdfObject().ContainsKey(PdfName.FontBBox)) { PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox); fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue( ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue()); } else { fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0); } int firstChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.FirstChar), 0); int lastChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.LastChar), 255); for (int i = firstChar; i <= lastChar; i++) { shortTag[i] = 1; } int[] widths = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, 0); double[] fontMatrix = new double[6]; for (int i = 0; i < fontMatrixArray.Size(); i++) { fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue(); } SetFontMatrix(fontMatrix); foreach (PdfName glyphName in charProcsDic.KeySet()) { int unicode = AdobeGlyphList.NameToUnicode(glyphName.GetValue()); if (unicode != -1 && fontEncoding.CanEncode(unicode)) { int code = fontEncoding.ConvertToByte(unicode); ((Type3Font)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.GetAsStream (glyphName), GetDocument())); } } FillFontDescriptor(fontDictionary.GetAsDictionary(PdfName.FontDescriptor)); }
/// <summary>Creates a Type3 font based on an existing font dictionary, which must be an indirect object.</summary> /// <param name="fontDictionary">a dictionary of type <code>/Font</code>, must have an indirect reference.</param> internal PdfType3Font(PdfDictionary fontDictionary) : base(fontDictionary) { EnsureObjectIsAddedToDocument(fontDictionary); CheckFontDictionary(fontDictionary, PdfName.Type3); subset = true; embedded = true; fontProgram = new Type3FontProgram(false); fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), null, false); PdfDictionary charProcsDic = GetPdfObject().GetAsDictionary(PdfName.CharProcs); PdfArray fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix); if (GetPdfObject().ContainsKey(PdfName.FontBBox)) { PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox); fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue( ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue()); } else { fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0); } 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); double[] fontMatrix = new double[6]; for (int i = 0; i < fontMatrixArray.Size(); i++) { fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue(); } SetFontMatrix(fontMatrix); foreach (PdfName glyphName in charProcsDic.KeySet()) { int unicode = (int)AdobeGlyphList.NameToUnicode(glyphName.GetValue()); if (unicode != -1 && fontEncoding.CanEncode(unicode)) { int code = fontEncoding.ConvertToByte(unicode); ((Type3FontProgram)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic .GetAsStream(glyphName), GetDocument())); } } }
/// <summary>Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.</summary> /// <param name="fontDictionary">a dictionary of type <c>/Font</c>, must have an indirect reference.</param> internal PdfType3Font(PdfDictionary fontDictionary) : base(fontDictionary) { subset = true; embedded = true; fontProgram = new Type3Font(false); fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode); PdfDictionary charProcsDic = GetPdfObject().GetAsDictionary(PdfName.CharProcs); PdfArray fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix); if (GetPdfObject().ContainsKey(PdfName.FontBBox)) { PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox); fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue( ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue()); } else { fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0); } int firstChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.FirstChar), 0); int lastChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.LastChar), 255); for (int i = firstChar; i <= lastChar; i++) { shortTag[i] = 1; } int[] widths = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, 0); double[] fontMatrix = new double[6]; for (int i = 0; i < fontMatrixArray.Size(); i++) { fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue(); } SetFontMatrix(fontMatrix); if (toUnicode != null && toUnicode.HasByteMappings() && fontEncoding.HasDifferences()) { for (int i = 0; i < 256; i++) { int unicode = fontEncoding.GetUnicode(i); PdfName glyphName = new PdfName(fontEncoding.GetDifference(i)); if (unicode != -1 && !FontEncoding.NOTDEF.Equals(glyphName.GetValue()) && charProcsDic.ContainsKey(glyphName )) { ((Type3Font)GetFontProgram()).AddGlyph(i, unicode, widths[i], null, new Type3Glyph(charProcsDic.GetAsStream (glyphName), GetDocument())); } } } IDictionary <int, int?> unicodeToCode = null; if (toUnicode != null) { try { unicodeToCode = toUnicode.CreateReverseMapping(); } catch (Exception) { } } foreach (PdfName glyphName in charProcsDic.KeySet()) { int unicode = AdobeGlyphList.NameToUnicode(glyphName.GetValue()); int code = -1; if (fontEncoding.CanEncode(unicode)) { code = fontEncoding.ConvertToByte(unicode); } else { if (unicodeToCode != null && unicodeToCode.ContainsKey(unicode)) { code = (int)unicodeToCode.Get(unicode); } } if (code != -1 && GetFontProgram().GetGlyphByCode(code) == null) { ((Type3Font)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.GetAsStream (glyphName), GetDocument())); } } FillFontDescriptor(fontDictionary.GetAsDictionary(PdfName.FontDescriptor)); }
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); }