private CMap ReadEncoding(DictionaryToken dictionary, out bool isCMapPredefined) { isCMapPredefined = false; CMap result; if (dictionary.TryGet(NameToken.Encoding, scanner, out NameToken encodingName)) { var cmap = CMapCache.Get(encodingName.Data); result = cmap ?? throw new InvalidOperationException($"Missing CMap named {encodingName.Data}."); isCMapPredefined = true; } else if (dictionary.TryGet(NameToken.Encoding, scanner, out StreamToken stream)) { var decoded = stream.Decode(filterProvider); var cmap = CMapCache.Parse(new ByteArrayInputBytes(decoded)); result = cmap ?? throw new InvalidOperationException($"Could not read CMap from stream in the dictionary: {dictionary}"); } else { throw new InvalidOperationException( $"Could not read the encoding, expected a name or a stream but it was not found in the dictionary: {dictionary}"); } return(result); }
public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) { var baseFont = dictionary.GetNameOrDefault(NameToken.BaseFont); var cMap = ReadEncoding(dictionary, out var isCMapPredefined); ICidFont cidFont; if (TryGetFirstDescendant(dictionary, out var descendantObject)) { DictionaryToken descendantFontDictionary; if (descendantObject is IndirectReferenceToken obj) { var parsed = DirectObjectFinder.Get<DictionaryToken>(obj, scanner); descendantFontDictionary = parsed; } else { descendantFontDictionary = (DictionaryToken) descendantObject; } cidFont = ParseDescendant(descendantFontDictionary, isLenientParsing); } else { throw new InvalidFontFormatException("No descendant font dictionary was declared for this Type 0 font. This dictionary should contain the CIDFont for the Type 0 font. " + dictionary); } var (ucs2CMap, isChineseJapaneseOrKorean) = GetUcs2CMap(dictionary, isCMapPredefined, cidFont); CMap toUnicodeCMap = null; if (dictionary.ContainsKey(NameToken.ToUnicode)) { var toUnicodeValue = dictionary.Data[NameToken.ToUnicode]; if (DirectObjectFinder.TryGet<StreamToken>(toUnicodeValue, scanner, out var toUnicodeStream)) { var decodedUnicodeCMap = toUnicodeStream?.Decode(filterProvider); if (decodedUnicodeCMap != null) { toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap), isLenientParsing); } } else if (DirectObjectFinder.TryGet<NameToken>(toUnicodeValue, scanner, out var toUnicodeName)) { toUnicodeCMap = CMapCache.Get(toUnicodeName.Data); } else { throw new PdfDocumentFormatException($"Invalid type of toUnicode CMap encountered. Got: {toUnicodeValue}."); } } var font = new Type0Font(baseFont, cidFont, cMap, toUnicodeCMap, ucs2CMap, isChineseJapaneseOrKorean); return font; }
public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) { var boundingBox = GetBoundingBox(dictionary); var fontMatrix = GetFontMatrix(dictionary); var firstCharacter = FontDictionaryAccessHelper.GetFirstCharacter(dictionary); var lastCharacter = FontDictionaryAccessHelper.GetLastCharacter(dictionary); var widths = FontDictionaryAccessHelper.GetWidths(scanner, dictionary, isLenientParsing); Encoding encoding = encodingReader.Read(dictionary, isLenientParsing); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) { var toUnicode = DirectObjectFinder.Get <StreamToken>(toUnicodeObj, scanner); var decodedUnicodeCMap = toUnicode?.Decode(filterProvider); if (decodedUnicodeCMap != null) { toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap), isLenientParsing); } } return(new Type3Font(NameToken.Type3, boundingBox, fontMatrix, encoding, firstCharacter, lastCharacter, widths, toUnicodeCMap)); }
public Type0FontHandler(CidFontFactory cidFontFactory, CMapCache cMapCache, IFilterProvider filterProvider, IPdfObjectParser pdfObjectParser) { this.cidFontFactory = cidFontFactory; this.cMapCache = cMapCache; this.filterProvider = filterProvider; this.pdfObjectParser = pdfObjectParser; }
private CMap ReadEncoding(DictionaryToken dictionary, out bool isCMapPredefined) { isCMapPredefined = false; CMap result = default(CMap); if (dictionary.TryGet(NameToken.Encoding, out var value)) { if (value is NameToken encodingName) { var cmap = CMapCache.Get(encodingName.Data); result = cmap ?? throw new InvalidOperationException("Missing CMap for " + encodingName.Data); isCMapPredefined = true; } else if (value is StreamToken stream) { var decoded = stream.Decode(filterProvider); var cmap = CMapCache.Parse(new ByteArrayInputBytes(decoded)); result = cmap ?? throw new InvalidOperationException("Could not read CMap for " + dictionary); } else { throw new InvalidOperationException("Could not read the encoding, expected a name or a stream but got a: " + value.GetType().Name); } } return(result); }
private void LoadCMaps() { try { fontDesc = allFonts[fontName]; hMetrics = (IntHashtable)fontDesc["W"]; vMetrics = (IntHashtable)fontDesc["W2"]; String registry = (String)fontDesc["Registry"]; uniMap = ""; foreach (String name in registryNames[registry + "_Uni"].Keys) { uniMap = name; if (name.EndsWith("V") && vertical) { break; } if (!name.EndsWith("V") && !vertical) { break; } } if (cidDirect) { cidUni = CMapCache.GetCachedCMapCidUni(uniMap); } else { uniCid = CMapCache.GetCachedCMapUniCid(uniMap); cidByte = CMapCache.GetCachedCMapCidByte(CMap); } } catch (Exception ex) { throw new DocumentException(ex.Message); } }
public Type0FontHandler(CidFontFactory cidFontFactory, CMapCache cMapCache, IFilterProvider filterProvider, IPdfTokenScanner scanner) { this.cidFontFactory = cidFontFactory; this.cMapCache = cMapCache; this.filterProvider = filterProvider; this.scanner = scanner; }
public Type3FontHandler(IPdfTokenScanner scanner, CMapCache cMapCache, IFilterProvider filterProvider, IEncodingReader encodingReader) { this.cMapCache = cMapCache; this.filterProvider = filterProvider; this.encodingReader = encodingReader; this.scanner = scanner; }
public TrueTypeFontHandler(IPdfObjectParser pdfObjectParser, IFilterProvider filterProvider, CMapCache cMapCache, FontDescriptorFactory fontDescriptorFactory, TrueTypeFontParser trueTypeFontParser) { this.pdfObjectParser = pdfObjectParser; this.filterProvider = filterProvider; this.cMapCache = cMapCache; this.fontDescriptorFactory = fontDescriptorFactory; this.trueTypeFontParser = trueTypeFontParser; }
private static (CMap, bool isChineseJapaneseOrKorean) GetUcs2CMap(DictionaryToken dictionary, bool isCMapPredefined, ICidFont cidFont) { if (!isCMapPredefined) { return(null, false); } /* * If the font is a composite font that uses one of the predefined CMaps except Identity–H and Identity–V or whose descendant * CIDFont uses the Adobe-GB1, Adobe-CNS1, Adobe-Japan1, or Adobe-Korea1 character collection use a UCS2 CMap. */ var encodingName = dictionary.GetNameOrDefault(NameToken.Encoding); if (encodingName == null) { return(null, false); } var isChineseJapaneseOrKorean = false; if (cidFont != null && string.Equals(cidFont.SystemInfo.Registry, "Adobe", StringComparison.OrdinalIgnoreCase)) { isChineseJapaneseOrKorean = string.Equals(cidFont.SystemInfo.Ordering, "GB1", StringComparison.OrdinalIgnoreCase) || string.Equals(cidFont.SystemInfo.Ordering, "CNS1", StringComparison.OrdinalIgnoreCase) || string.Equals(cidFont.SystemInfo.Ordering, "Japan1", StringComparison.OrdinalIgnoreCase) || string.Equals(cidFont.SystemInfo.Ordering, "Korea1", StringComparison.OrdinalIgnoreCase); } var isPredefinedIdentityMap = encodingName.Equals(NameToken.IdentityH) || encodingName.Equals(NameToken.IdentityV); if (isPredefinedIdentityMap && !isChineseJapaneseOrKorean) { return(null, false); } if (!isChineseJapaneseOrKorean) { return(null, false); } var fullCmapName = cidFont.SystemInfo.ToString(); var nonUnicodeCMap = CMapCache.Get(fullCmapName); if (nonUnicodeCMap == null) { return(null, true); } var unicodeCMapName = $"{nonUnicodeCMap.Info.Registry}-{nonUnicodeCMap.Info.Ordering}-UCS2"; return(CMapCache.Get(unicodeCMapName), true); }
public Type1FontHandler(IPdfTokenScanner pdfScanner, CMapCache cMapCache, IFilterProvider filterProvider, FontDescriptorFactory fontDescriptorFactory, IEncodingReader encodingReader, Type1FontParser type1FontParser) { this.pdfScanner = pdfScanner; this.cMapCache = cMapCache; this.filterProvider = filterProvider; this.fontDescriptorFactory = fontDescriptorFactory; this.encodingReader = encodingReader; this.type1FontParser = type1FontParser; }
public TrueTypeFontHandler(ILog log, IPdfTokenScanner pdfScanner, IFilterProvider filterProvider, CMapCache cMapCache, FontDescriptorFactory fontDescriptorFactory, TrueTypeFontParser trueTypeFontParser, IEncodingReader encodingReader) { this.log = log; this.filterProvider = filterProvider; this.cMapCache = cMapCache; this.fontDescriptorFactory = fontDescriptorFactory; this.trueTypeFontParser = trueTypeFontParser; this.encodingReader = encodingReader; this.pdfScanner = pdfScanner; }
private void InitFont() { ProcessToUnicode(); //if (toUnicodeCmap == null) ProcessUni2Byte(); spaceWidth = base.GetWidth(' '); if (spaceWidth == 0) { spaceWidth = ComputeAverageWidth(); } if (cjkEncoding != null) { byteCid = CMapCache.GetCachedCMapByteCid(cjkEncoding); cidUni = CMapCache.GetCachedCMapCidUni(uniMap); } }
private static PdfDocument OpenDocument(IInputBytes inputBytes, ISeekableTokenScanner scanner, IContainer container, bool isLenientParsing, string password) { var log = container.Get <ILog>(); var filterProvider = container.Get <IFilterProvider>(); var catalogFactory = new CatalogFactory(); var cMapCache = new CMapCache(new CMapParser()); CrossReferenceTable crossReferenceTable = null; var bruteForceSearcher = new BruteForceSearcher(inputBytes); var xrefValidator = new XrefOffsetValidator(log); var objectChecker = new XrefCosOffsetChecker(log, bruteForceSearcher); // We're ok with this since our intent is to lazily load the cross reference table. // ReSharper disable once AccessToModifiedClosure var locationProvider = new ObjectLocationProvider(() => crossReferenceTable, bruteForceSearcher); var pdfScanner = new PdfTokenScanner(inputBytes, locationProvider, filterProvider, NoOpEncryptionHandler.Instance); var crossReferenceStreamParser = new CrossReferenceStreamParser(filterProvider); var crossReferenceParser = new CrossReferenceParser(log, xrefValidator, objectChecker, crossReferenceStreamParser, new CrossReferenceTableParser()); var version = FileHeaderParser.Parse(scanner, isLenientParsing, log); var crossReferenceOffset = container.Get <FileTrailerParser>().GetFirstCrossReferenceOffset(inputBytes, scanner, isLenientParsing); // TODO: make this use the scanner. var validator = new CrossReferenceOffsetValidator(xrefValidator); crossReferenceOffset = validator.Validate(crossReferenceOffset, scanner, inputBytes, isLenientParsing); crossReferenceTable = crossReferenceParser.Parse(inputBytes, isLenientParsing, crossReferenceOffset, pdfScanner, scanner); var trueTypeFontParser = new TrueTypeFontParser(); var fontDescriptorFactory = new FontDescriptorFactory(); var compactFontFormatIndexReader = new CompactFontFormatIndexReader(); var compactFontFormatParser = new CompactFontFormatParser(new CompactFontFormatIndividualFontParser(compactFontFormatIndexReader, new CompactFontFormatTopLevelDictionaryReader(), new CompactFontFormatPrivateDictionaryReader()), compactFontFormatIndexReader); var rootDictionary = ParseTrailer(crossReferenceTable, isLenientParsing, pdfScanner, out var encryptionDictionary); var encryptionHandler = encryptionDictionary != null ? (IEncryptionHandler) new EncryptionHandler(encryptionDictionary, crossReferenceTable.Trailer, password ?? string.Empty) : NoOpEncryptionHandler.Instance; pdfScanner.UpdateEncryptionHandler(encryptionHandler); var cidFontFactory = new CidFontFactory(pdfScanner, fontDescriptorFactory, trueTypeFontParser, compactFontFormatParser, filterProvider); var encodingReader = new EncodingReader(pdfScanner); var fontFactory = new FontFactory(log, new Type0FontHandler(cidFontFactory, cMapCache, filterProvider, pdfScanner), new TrueTypeFontHandler(log, pdfScanner, filterProvider, cMapCache, fontDescriptorFactory, trueTypeFontParser, encodingReader, new SystemFontFinder(new TrueTypeFontParser())), new Type1FontHandler(pdfScanner, cMapCache, filterProvider, fontDescriptorFactory, encodingReader, new Type1FontParser(new Type1EncryptedPortionParser()), compactFontFormatParser), new Type3FontHandler(pdfScanner, cMapCache, filterProvider, encodingReader)); var resourceContainer = new ResourceContainer(pdfScanner, fontFactory); var pageFactory = new PageFactory(pdfScanner, resourceContainer, filterProvider, new PageContentParser(new ReflectionGraphicsStateOperationFactory()), new XObjectFactory(), log); var informationFactory = new DocumentInformationFactory(); var information = informationFactory.Create(pdfScanner, crossReferenceTable.Trailer); var catalog = catalogFactory.Create(pdfScanner, rootDictionary); var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer); var acroFormFactory = new AcroFormFactory(pdfScanner, filterProvider); return(new PdfDocument(log, inputBytes, version, crossReferenceTable, isLenientParsing, caching, pageFactory, catalog, information, encryptionDictionary, pdfScanner, filterProvider, acroFormFactory)); }
public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) { if (!dictionary.TryGetOptionalTokenDirect(NameToken.FirstChar, pdfScanner, out NumericToken firstCharacterToken) || !dictionary.TryGet <IToken>(NameToken.FontDescriptor, pdfScanner, out _) || !dictionary.TryGet(NameToken.Widths, out IToken _)) { if (!dictionary.TryGetOptionalTokenDirect(NameToken.BaseFont, pdfScanner, out NameToken baseFont)) { throw new InvalidFontFormatException($"The provided TrueType font dictionary did not contain a /FirstChar or a /BaseFont entry: {dictionary}."); } // Can use the AFM descriptor despite not being Type 1! var standard14Font = Standard14.GetAdobeFontMetrics(baseFont.Data); if (standard14Font == null) { throw new InvalidFontFormatException($"The provided TrueType font dictionary did not have a /FirstChar and did not match a Standard 14 font: {dictionary}."); } var fileSystemFont = systemFontFinder.GetTrueTypeFont(baseFont.Data); var thisEncoding = encodingReader.Read(dictionary, isLenientParsing); if (thisEncoding == null) { thisEncoding = new AdobeFontMetricsEncoding(standard14Font); } int? firstChar = null; double[] widthsOverride = null; if (dictionary.TryGet(NameToken.FirstChar, pdfScanner, out firstCharacterToken)) { firstChar = firstCharacterToken.Int; } if (dictionary.TryGet(NameToken.Widths, pdfScanner, out ArrayToken widthsArray)) { widthsOverride = widthsArray.Data.OfType <NumericToken>() .Select(x => x.Double).ToArray(); } return(new TrueTypeStandard14FallbackSimpleFont(baseFont, standard14Font, thisEncoding, fileSystemFont, new TrueTypeStandard14FallbackSimpleFont.MetricOverrides(firstChar, widthsOverride))); } var firstCharacter = firstCharacterToken.Int; var widths = FontDictionaryAccessHelper.GetWidths(pdfScanner, dictionary, isLenientParsing); var descriptor = FontDictionaryAccessHelper.GetFontDescriptor(pdfScanner, fontDescriptorFactory, dictionary, isLenientParsing); var font = ParseTrueTypeFont(descriptor, out var actualHandler); if (font == null && actualHandler != null) { return(actualHandler.Generate(dictionary, isLenientParsing)); } var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor, isLenientParsing); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) { var toUnicode = DirectObjectFinder.Get <StreamToken>(toUnicodeObj, pdfScanner); var decodedUnicodeCMap = toUnicode.Decode(filterProvider); if (decodedUnicodeCMap != null) { toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap), isLenientParsing); } } Encoding encoding = encodingReader.Read(dictionary, isLenientParsing, descriptor); if (encoding == null && font?.TableRegister?.CMapTable != null && font.TableRegister.PostScriptTable?.GlyphNames != null) { var postscript = font.TableRegister.PostScriptTable; // Synthesize an encoding var fakeEncoding = new Dictionary <int, string>(); for (var i = 0; i < 256; i++) { if (font.TableRegister.CMapTable.TryGetGlyphIndex(i, out var index)) { string glyphName; if (index >= 0 && index < postscript.GlyphNames.Count) { glyphName = postscript.GlyphNames[index]; } else { glyphName = index.ToString(); } fakeEncoding[i] = glyphName; } } encoding = new BuiltInEncoding(fakeEncoding); } return(new TrueTypeSimpleFont(name, descriptor, toUnicodeCMap, encoding, font, firstCharacter, widths)); }
public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) { var usingStandard14Only = !dictionary.ContainsKey(NameToken.FirstChar) || !dictionary.ContainsKey(NameToken.Widths); if (usingStandard14Only) { // TODO: some fonts combine standard 14 font with other metrics. if (!dictionary.TryGet(NameToken.BaseFont, out var baseFontToken) || !(baseFontToken is NameToken standard14Name)) { throw new InvalidFontFormatException($"The Type 1 font did not contain a first character entry but also did not reference a standard 14 font: {dictionary}"); } var metrics = Standard14.GetAdobeFontMetrics(standard14Name.Data); var overrideEncoding = encodingReader.Read(dictionary, isLenientParsing); return(new Type1Standard14Font(metrics, overrideEncoding)); } var firstCharacter = FontDictionaryAccessHelper.GetFirstCharacter(dictionary); var lastCharacter = FontDictionaryAccessHelper.GetLastCharacter(dictionary); var widths = FontDictionaryAccessHelper.GetWidths(pdfScanner, dictionary, isLenientParsing); if (!dictionary.TryGet(NameToken.FontDescriptor, out var _)) { if (dictionary.TryGet(NameToken.BaseFont, out var baseFontToken) && DirectObjectFinder.TryGet(baseFontToken, pdfScanner, out NameToken baseFontName)) { var metrics = Standard14.GetAdobeFontMetrics(baseFontName.Data); var overrideEncoding = encodingReader.Read(dictionary, isLenientParsing); return(new Type1Standard14Font(metrics, overrideEncoding)); } } var descriptor = FontDictionaryAccessHelper.GetFontDescriptor(pdfScanner, fontDescriptorFactory, dictionary, isLenientParsing); var font = ParseFontProgram(descriptor, isLenientParsing); var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor, isLenientParsing); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) { var toUnicode = DirectObjectFinder.Get <StreamToken>(toUnicodeObj, pdfScanner); var decodedUnicodeCMap = toUnicode?.Decode(filterProvider); if (decodedUnicodeCMap != null) { toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap), isLenientParsing); } } Encoding fromFont = font?.Match(x => x.Encoding != null ? new BuiltInEncoding(x.Encoding) : default(Encoding), x => { if (x.Fonts != null && x.Fonts.Count > 0) { return(x.Fonts.First().Value.Encoding); } return(default(Encoding)); }); Encoding encoding = encodingReader.Read(dictionary, isLenientParsing, descriptor, fromFont); if (encoding == null) { font?.Match(x => encoding = new BuiltInEncoding(x.Encoding), _ => {}); } return(new Type1FontSimple(name, firstCharacter, lastCharacter, widths, descriptor, encoding, toUnicodeCMap, font)); }
private static PdfDocument OpenDocument(IRandomAccessRead reader, IInputBytes inputBytes, ISeekableTokenScanner scanner, IContainer container, bool isLenientParsing) { var log = container.Get <ILog>(); var version = container.Get <FileHeaderParser>().Parse(scanner, isLenientParsing); var crossReferenceOffset = container.Get <FileTrailerParser>().GetFirstCrossReferenceOffset(inputBytes, scanner, isLenientParsing); var pool = new CosObjectPool(); // TODO: make this use the scanner. var validator = new CrossReferenceOffsetValidator(new XrefOffsetValidator(log, reader, container.Get <CosDictionaryParser>(), container.Get <CosBaseParser>(), pool)); crossReferenceOffset = validator.Validate(crossReferenceOffset, isLenientParsing); var crossReferenceTable = container.Get <CrossReferenceParser>() .Parse(reader, isLenientParsing, crossReferenceOffset, pool); container.Get <CrossReferenceParser>().ParseNew(crossReferenceOffset, scanner, isLenientParsing); var filterProvider = container.Get <IFilterProvider>(); var bruteForceSearcher = new BruteForceSearcher(reader); var pdfObjectParser = new PdfObjectParser(container.Get <ILog>(), container.Get <CosBaseParser>(), container.Get <CosStreamParser>(), crossReferenceTable, bruteForceSearcher, pool, container.Get <ObjectStreamParser>()); var trueTypeFontParser = new TrueTypeFontParser(); var fontDescriptorFactory = new FontDescriptorFactory(); var cidFontFactory = new CidFontFactory(fontDescriptorFactory, trueTypeFontParser, pdfObjectParser, filterProvider); var cMapCache = new CMapCache(new CMapParser()); var fontFactory = new FontFactory(log, new Type0FontHandler(cidFontFactory, cMapCache, filterProvider, pdfObjectParser), new TrueTypeFontHandler(pdfObjectParser, filterProvider, cMapCache, fontDescriptorFactory, trueTypeFontParser)); var dynamicParser = container.Get <DynamicParser>(); var resourceContainer = new ResourceContainer(pdfObjectParser, fontFactory); var pageFactory = new PageFactory(resourceContainer, pdfObjectParser, filterProvider, new PageContentParser(new ReflectionGraphicsStateOperationFactory())); var informationFactory = new DocumentInformationFactory(); var catalogFactory = new CatalogFactory(pdfObjectParser); var root = ParseTrailer(reader, crossReferenceTable, dynamicParser, bruteForceSearcher, pool, isLenientParsing); if (!(root is PdfDictionary rootDictionary)) { throw new InvalidOperationException("Expected root dictionary, but got this: " + root); } // in some pdfs the type value "Catalog" is missing in the root object if (isLenientParsing && !rootDictionary.ContainsKey(CosName.TYPE)) { rootDictionary.Set(CosName.TYPE, CosName.CATALOG); } var information = informationFactory.Create(pdfObjectParser, crossReferenceTable.Dictionary, reader, isLenientParsing); var catalog = catalogFactory.Create(rootDictionary, reader, isLenientParsing); var caching = new ParsingCachingProviders(pool, bruteForceSearcher, resourceContainer); return(new PdfDocument(log, reader, version, crossReferenceTable, isLenientParsing, caching, pageFactory, pdfObjectParser, catalog, information)); }
public IFont Generate(DictionaryToken dictionary) { var usingStandard14Only = !dictionary.ContainsKey(NameToken.FirstChar) || !dictionary.ContainsKey(NameToken.Widths); if (usingStandard14Only) { // TODO: some fonts combine standard 14 font with other metrics. if (!dictionary.TryGet(NameToken.BaseFont, out var baseFontToken) || !(baseFontToken is NameToken standard14Name)) { throw new InvalidFontFormatException($"The Type 1 font did not contain a first character entry but also did not reference a standard 14 font: {dictionary}"); } var metrics = Standard14.GetAdobeFontMetrics(standard14Name.Data); if (metrics != null) { var overrideEncoding = encodingReader.Read(dictionary); return(new Type1Standard14Font(metrics, overrideEncoding)); } } int firstCharacter, lastCharacter; double[] widths; if (!usingStandard14Only) { firstCharacter = FontDictionaryAccessHelper.GetFirstCharacter(dictionary); lastCharacter = FontDictionaryAccessHelper.GetLastCharacter(dictionary); widths = FontDictionaryAccessHelper.GetWidths(pdfScanner, dictionary); } else { firstCharacter = 0; lastCharacter = 0; widths = EmptyArray <double> .Instance; } if (!dictionary.TryGet(NameToken.FontDescriptor, out var _)) { if (dictionary.TryGet(NameToken.BaseFont, out var baseFontToken) && DirectObjectFinder.TryGet(baseFontToken, pdfScanner, out NameToken baseFontName)) { var metrics = Standard14.GetAdobeFontMetrics(baseFontName.Data); var overrideEncoding = encodingReader.Read(dictionary); return(new Type1Standard14Font(metrics, overrideEncoding)); } } var descriptor = FontDictionaryAccessHelper.GetFontDescriptor(pdfScanner, dictionary); var font = ParseFontProgram(descriptor); var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) { var toUnicode = DirectObjectFinder.Get <StreamToken>(toUnicodeObj, pdfScanner); var decodedUnicodeCMap = toUnicode?.Decode(filterProvider, pdfScanner); if (decodedUnicodeCMap != null) { toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap)); } } var fromFont = default(Encoding); if (font != null) { if (font.TryGetFirst(out var t1Font)) { fromFont = t1Font.Encoding != null ? new BuiltInEncoding(t1Font.Encoding) : default(Encoding); } else if (font.TryGetSecond(out var cffFont)) { fromFont = cffFont.FirstFont?.Encoding; } } var encoding = encodingReader.Read(dictionary, descriptor, fromFont); if (encoding == null && font != null && font.TryGetFirst(out var t1FontReplacment)) { encoding = new BuiltInEncoding(t1FontReplacment.Encoding); } return(new Type1FontSimple(name, firstCharacter, lastCharacter, widths, descriptor, encoding, toUnicodeCMap, font)); }