public ushort GetGlyphIndexByName(string glyphName) { if (_cffTable != null) { return(GetGlyphByName(glyphName)?.GlyphIndex ?? 0); } else if (PostTable != null) { if (PostTable.Version == 2) { return(PostTable.GetGlyphIndex(glyphName)); } else { //check data from adobe glyph list //from the unicode value //select glyph index //we use AdobeGlyphList //from https://github.com/adobe-type-tools/agl-aglfn/blob/master/glyphlist.txt //but user can provide their own map here... return(GetGlyphIndex(AdobeGlyphList.GetUnicodeValueByGlyphName(glyphName))); } } return(0); }
public virtual void GetFontWithDirectFontDictionaryTest() { PdfDictionary initialFontDict = new PdfDictionary(); initialFontDict.Put(PdfName.Subtype, PdfName.Type3); initialFontDict.Put(PdfName.FontMatrix, new PdfArray(new float[] { 0.001F, 0, 0, 0.001F, 0, 0 })); initialFontDict.Put(PdfName.Widths, new PdfArray()); PdfDictionary encoding = new PdfDictionary(); initialFontDict.Put(PdfName.Encoding, encoding); PdfArray differences = new PdfArray(); differences.Add(new PdfNumber(AdobeGlyphList.NameToUnicode("a"))); differences.Add(new PdfName("a")); encoding.Put(PdfName.Differences, differences); NUnit.Framework.Assert.IsNull(initialFontDict.GetIndirectReference()); using (PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { // prevent no pages exception on close doc.AddNewPage(); PdfType3Font font1 = (PdfType3Font)doc.GetFont(initialFontDict); NUnit.Framework.Assert.IsNotNull(font1); // prevent no glyphs for type3 font on close font1.AddGlyph('a', 0, 0, 0, 0, 0); } }
private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, PdfArray diffs, CMapToUnicode toUnicode) { IntHashtable byte2uni = toUnicode != null?toUnicode.CreateDirectMapping() : new IntHashtable(); if (diffs != null) { int currentNumber = 0; for (int k = 0; k < diffs.Size(); ++k) { PdfObject obj = diffs.Get(k); if (obj.IsNumber()) { currentNumber = ((PdfNumber)obj).IntValue(); } else { if (currentNumber > 255) { ILog LOGGER = LogManager.GetLogger(typeof(iText.Kernel.Font.DocFontEncoding)); LOGGER.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DOCFONT_HAS_ILLEGAL_DIFFERENCES, ((PdfName )obj).GetValue())); } else { /* don't return or break, because differences subarrays may * be in any order: * e.g. [255 /space /one 250 /two /three] * /one is invalid but all others should be parsed */ String glyphName = ((PdfName)obj).GetValue(); int unicode = AdobeGlyphList.NameToUnicode(glyphName); if (unicode != -1) { fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } else { if (byte2uni.ContainsKey(currentNumber)) { unicode = byte2uni.Get(currentNumber); fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } } currentNumber++; } } } } }
public ushort GetGlyphIndexByName(string glyphName) { if (glyphName == null) { return(0); } if (_cff1FontSet != null && _cachedGlyphDicByName == null) { //we create a dictionary //create cache data _cachedGlyphDicByName = new Dictionary <string, ushort>(); for (int i = 1; i < _glyphs.Length; ++i) { Glyph glyph = _glyphs[i]; if (glyph._cff1GlyphData.Name != null) { _cachedGlyphDicByName.Add(glyph._cff1GlyphData.Name, (ushort)i); } else { #if DEBUG System.Diagnostics.Debug.WriteLine("Cff unknown glyphname"); #endif } } return(_cachedGlyphDicByName.TryGetValue(glyphName, out ushort glyphIndex) ? glyphIndex : (ushort)0); } else if (PostTable != null) { if (PostTable.Version == 2) { return(PostTable.GetGlyphIndex(glyphName)); } else { //check data from adobe glyph list //from the unicode value //select glyph index //we use AdobeGlyphList //from https://github.com/adobe-type-tools/agl-aglfn/blob/master/glyphlist.txt //but user can provide their own map here... return(GetGlyphIndex(AdobeGlyphList.GetUnicodeValueByGlyphName(glyphName))); } } return(0); }
private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, CMapToUnicode toUnicode ) { IntHashtable byte2uni = toUnicode.CreateDirectMapping(); foreach (int?code in byte2uni.GetKeys()) { int unicode = byte2uni.Get((int)code); String glyphName = AdobeGlyphList.UnicodeToName(unicode); fontEncoding.codeToUnicode[(int)code] = unicode; fontEncoding.unicodeToCode.Put(unicode, (int)code); fontEncoding.differences[(int)code] = glyphName; fontEncoding.unicodeDifferences.Put(unicode, unicode); } }
public void Setup() { var fs = new Mock <IMediaFileSystem>(); fs.Setup <System.IO.Stream>(m => m.OpenFile(It.IsAny <string>())).Returns <string>(path => System.IO.File.OpenRead(path)); var logger = new Mock <ILogger>(); var glyphList = new AdobeGlyphList(new AdobeGlphListDataProvider()); _pdfTextService = new PdfTextService(new PdfSharpTextExtractor(glyphList, logger.Object), fs.Object, logger.Object); _testFilesDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); while (!_testFilesDir.Name.Equals("UmbracoExamine.PDF.Tests", StringComparison.InvariantCultureIgnoreCase)) { _testFilesDir = _testFilesDir.Parent; } }
/// <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())); } } }
private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, PdfArray diffs, CMapToUnicode toUnicode) { IntHashtable byte2uni = toUnicode != null?toUnicode.CreateDirectMapping() : new IntHashtable(); if (diffs != null) { int currentNumber = 0; for (int k = 0; k < diffs.Size(); ++k) { PdfObject obj = diffs.Get(k); if (obj.IsNumber()) { currentNumber = ((PdfNumber)obj).IntValue(); } else { String glyphName = ((PdfName)obj).GetValue(); int unicode = (int)AdobeGlyphList.NameToUnicode(glyphName); if (unicode != -1) { fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } else { if (byte2uni.Contains(currentNumber)) { unicode = byte2uni.Get(currentNumber); fontEncoding.codeToUnicode[currentNumber] = (int)unicode; fontEncoding.unicodeToCode.Put((int)unicode, currentNumber); fontEncoding.differences[currentNumber] = glyphName; fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode); } } currentNumber++; } } } }
public virtual void TestGlyphListCount() { NUnit.Framework.Assert.AreEqual(4200, AdobeGlyphList.GetNameToUnicodeLength()); NUnit.Framework.Assert.AreEqual(3680, AdobeGlyphList.GetUnicodeToNameLength()); }
/// <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)); }