diff --git a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs index 9dec83ec..f25433dd 100644 --- a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs +++ b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs @@ -105,7 +105,7 @@ private class TestFontFactory : IFontFactory { - public IFont Get(DictionaryToken dictionary, bool isLenientParsing) + public IFont Get(DictionaryToken dictionary) { return null; } diff --git a/src/UglyToad.PdfPig/Content/IResourceStore.cs b/src/UglyToad.PdfPig/Content/IResourceStore.cs index c59a8d0e..26984a6c 100644 --- a/src/UglyToad.PdfPig/Content/IResourceStore.cs +++ b/src/UglyToad.PdfPig/Content/IResourceStore.cs @@ -6,7 +6,7 @@ internal interface IResourceStore { - void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing); + void LoadResourceDictionary(DictionaryToken resourceDictionary); /// /// Remove any named resources and associated state for the last resource dictionary loaded. @@ -20,7 +20,7 @@ DictionaryToken GetExtendedGraphicsStateDictionary(NameToken name); - IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken, bool isLenientParsing); + IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken); bool TryGetNamedColorSpace(NameToken name, out ResourceColorSpace namedColorSpace); diff --git a/src/UglyToad.PdfPig/Content/ResourceStore.cs b/src/UglyToad.PdfPig/Content/ResourceStore.cs index 8cd55eb7..ec605730 100644 --- a/src/UglyToad.PdfPig/Content/ResourceStore.cs +++ b/src/UglyToad.PdfPig/Content/ResourceStore.cs @@ -33,7 +33,7 @@ this.fontFactory = fontFactory; } - public void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing) + public void LoadResourceDictionary(DictionaryToken resourceDictionary) { lastLoadedFont = (null, null); @@ -43,7 +43,7 @@ { var fontDictionary = DirectObjectFinder.Get(fontBase, scanner); - LoadFontDictionary(fontDictionary, isLenientParsing); + LoadFontDictionary(fontDictionary); } if (resourceDictionary.TryGet(NameToken.Xobject, out var xobjectBase)) @@ -127,7 +127,7 @@ currentResourceState.Pop(); } - private void LoadFontDictionary(DictionaryToken fontDictionary, bool isLenientParsing) + private void LoadFontDictionary(DictionaryToken fontDictionary) { lastLoadedFont = (null, null); @@ -151,20 +151,15 @@ throw new InvalidOperationException($"Could not retrieve the font with name: {pair.Key} which should have been object {objectKey}"); } - loadedFonts[reference] = fontFactory.Get(fontObject, isLenientParsing); + loadedFonts[reference] = fontFactory.Get(fontObject); } else if (pair.Value is DictionaryToken fd) { - loadedDirectFonts[NameToken.Create(pair.Key)] = fontFactory.Get(fd, isLenientParsing); + loadedDirectFonts[NameToken.Create(pair.Key)] = fontFactory.Get(fd); } else { - if (isLenientParsing) - { - continue; - } - - throw new InvalidOperationException($"The font with name {pair.Key} did not link to an object key. Value was: {pair.Value}."); + continue; } } } @@ -191,7 +186,7 @@ return font; } - public IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken, bool isLenientParsing) + public IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken) { lastLoadedFont = (null, null); @@ -200,7 +195,7 @@ throw new PdfDocumentFormatException($"The requested font reference token {fontReferenceToken} wasn't a font."); } - var font = fontFactory.Get(fontDictionaryToken, isLenientParsing); + var font = fontFactory.Get(fontDictionaryToken); return font; } diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index c270a03b..ecb75641 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -362,7 +362,7 @@ var hasResources = formStream.StreamDictionary.TryGet(NameToken.Resources, pdfScanner, out var formResources); if (hasResources) { - resourceStore.LoadResourceDictionary(formResources, isLenientParsing); + resourceStore.LoadResourceDictionary(formResources); } // 1. Save current state. @@ -484,7 +484,7 @@ { currentGraphicsState.FontState.FromExtendedGraphicsState = true; currentGraphicsState.FontState.FontSize = (double)sizeToken.Data; - activeExtendedGraphicsStateFont = resourceStore.GetFontDirectly(fontReference, isLenientParsing); + activeExtendedGraphicsStateFont = resourceStore.GetFontDirectly(fontReference); } } diff --git a/src/UglyToad.PdfPig/Parser/PageFactory.cs b/src/UglyToad.PdfPig/Parser/PageFactory.cs index 11c050ed..36165645 100644 --- a/src/UglyToad.PdfPig/Parser/PageFactory.cs +++ b/src/UglyToad.PdfPig/Parser/PageFactory.cs @@ -43,9 +43,9 @@ var type = dictionary.GetNameOrDefault(NameToken.Type); - if (type != null && !type.Equals(NameToken.Page) && !isLenientParsing) + if (type != null && !type.Equals(NameToken.Page)) { - throw new InvalidOperationException($"Page {number} had its type specified as {type} rather than 'Page'."); + log?.Error($"Page {number} had its type specified as {type} rather than 'Page'."); } var rotation = new PageRotationDegrees(pageTreeMembers.Rotation); @@ -63,13 +63,13 @@ { var resource = pageTreeMembers.ParentResources.Dequeue(); - resourceStore.LoadResourceDictionary(resource, isLenientParsing); + resourceStore.LoadResourceDictionary(resource); stackDepth++; } if (dictionary.TryGet(NameToken.Resources, pdfScanner, out DictionaryToken resources)) { - resourceStore.LoadResourceDictionary(resources, isLenientParsing); + resourceStore.LoadResourceDictionary(resources); stackDepth++; } diff --git a/src/UglyToad.PdfPig/PdfFonts/FontFactory.cs b/src/UglyToad.PdfPig/PdfFonts/FontFactory.cs index 7a96a1f1..1332fd76 100644 --- a/src/UglyToad.PdfPig/PdfFonts/FontFactory.cs +++ b/src/UglyToad.PdfPig/PdfFonts/FontFactory.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using Fonts; using Logging; using Parser.Handlers; using Tokens; @@ -13,7 +12,7 @@ private readonly ILog log; private readonly IReadOnlyDictionary handlers; - public FontFactory(ILog log, Type0FontHandler type0FontHandler, TrueTypeFontHandler trueTypeFontHandler, + public FontFactory(ILog log, Type0FontHandler type0FontHandler, TrueTypeFontHandler trueTypeFontHandler, Type1FontHandler type1FontHandler, Type3FontHandler type3FontHandler) { this.log = log; @@ -27,7 +26,7 @@ }; } - public IFont Get(DictionaryToken dictionary, bool isLenientParsing) + public IFont Get(DictionaryToken dictionary) { var type = dictionary.GetNameOrDefault(NameToken.Type); @@ -35,21 +34,14 @@ { var message = "The font dictionary did not have type 'Font'. " + dictionary; - if (isLenientParsing) - { - log?.Error(message); - } - else - { - throw new InvalidFontFormatException(message); - } + log?.Error(message); } var subtype = dictionary.GetNameOrDefault(NameToken.Subtype); if (handlers.TryGetValue(subtype, out var handler)) { - return handler.Generate(dictionary, isLenientParsing); + return handler.Generate(dictionary); } throw new NotImplementedException($"Parsing not implemented for fonts of type: {subtype}, please submit a pull request or an issue."); diff --git a/src/UglyToad.PdfPig/PdfFonts/IFontFactory.cs b/src/UglyToad.PdfPig/PdfFonts/IFontFactory.cs index 80ca7547..ac77bf6c 100644 --- a/src/UglyToad.PdfPig/PdfFonts/IFontFactory.cs +++ b/src/UglyToad.PdfPig/PdfFonts/IFontFactory.cs @@ -4,6 +4,6 @@ internal interface IFontFactory { - IFont Get(DictionaryToken dictionary, bool isLenientParsing); + IFont Get(DictionaryToken dictionary); } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/EncodingReader.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/EncodingReader.cs index 9195e696..fd5b8cb6 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/EncodingReader.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/EncodingReader.cs @@ -18,7 +18,7 @@ this.pdfScanner = pdfScanner; } - public Encoding Read(DictionaryToken fontDictionary, bool isLenientParsing, FontDescriptor descriptor = null, + public Encoding Read(DictionaryToken fontDictionary, FontDescriptor descriptor = null, Encoding fontEncoding = null) { if (!fontDictionary.TryGet(NameToken.Encoding, out var baseEncodingObject)) diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/FontDictionaryAccessHelper.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/FontDictionaryAccessHelper.cs index 18f1f8d5..1013eff3 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/FontDictionaryAccessHelper.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/FontDictionaryAccessHelper.cs @@ -65,7 +65,7 @@ return descriptor; } - public static NameToken GetName(IPdfTokenScanner pdfScanner, DictionaryToken dictionary, FontDescriptor descriptor, bool isLenientParsing) + public static NameToken GetName(IPdfTokenScanner pdfScanner, DictionaryToken dictionary, FontDescriptor descriptor) { if (dictionary.TryGet(NameToken.BaseFont, out var nameBase)) { diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/IFontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/IFontHandler.cs index 8caf6a9f..1855ce05 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/IFontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/IFontHandler.cs @@ -4,6 +4,6 @@ internal interface IFontHandler { - IFont Generate(DictionaryToken dictionary, bool isLenientParsing); + IFont Generate(DictionaryToken dictionary); } } \ No newline at end of file diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/TrueTypeFontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/TrueTypeFontHandler.cs index 51d711b6..d030262a 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/TrueTypeFontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/TrueTypeFontHandler.cs @@ -42,7 +42,7 @@ this.pdfScanner = pdfScanner; } - public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) + public IFont Generate(DictionaryToken dictionary) { if (!dictionary.TryGetOptionalTokenDirect(NameToken.FirstChar, pdfScanner, out NumericToken firstCharacterToken) || !dictionary.TryGet(NameToken.FontDescriptor, pdfScanner, out _) @@ -63,7 +63,7 @@ var fileSystemFont = systemFontFinder.GetTrueTypeFont(baseFont.Data); - var thisEncoding = encodingReader.Read(dictionary, isLenientParsing); + var thisEncoding = encodingReader.Read(dictionary); if (thisEncoding == null) { @@ -98,10 +98,10 @@ if (font == null && actualHandler != null) { - return actualHandler.Generate(dictionary, isLenientParsing); + return actualHandler.Generate(dictionary); } - var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor, isLenientParsing); + var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) @@ -116,7 +116,7 @@ } } - Encoding encoding = encodingReader.Read(dictionary, isLenientParsing, descriptor); + Encoding encoding = encodingReader.Read(dictionary, descriptor); if (encoding == null && font?.TableRegister?.CMapTable != null && font.TableRegister.PostScriptTable?.GlyphNames != null) diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type0FontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type0FontHandler.cs index f1319675..8b31910a 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type0FontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type0FontHandler.cs @@ -27,7 +27,7 @@ this.scanner = scanner; } - public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) + public IFont Generate(DictionaryToken dictionary) { var baseFont = dictionary.GetNameOrDefault(NameToken.BaseFont); diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs index 8e9ef0b5..e7c002a7 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type1FontHandler.cs @@ -29,7 +29,7 @@ this.encodingReader = encodingReader; } - public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) + public IFont Generate(DictionaryToken dictionary) { var usingStandard14Only = !dictionary.ContainsKey(NameToken.FirstChar) || !dictionary.ContainsKey(NameToken.Widths); @@ -46,7 +46,7 @@ if (metrics != null) { - var overrideEncoding = encodingReader.Read(dictionary, isLenientParsing); + var overrideEncoding = encodingReader.Read(dictionary); return new Type1Standard14Font(metrics, overrideEncoding); } @@ -76,7 +76,7 @@ { var metrics = Standard14.GetAdobeFontMetrics(baseFontName.Data); - var overrideEncoding = encodingReader.Read(dictionary, isLenientParsing); + var overrideEncoding = encodingReader.Read(dictionary); return new Type1Standard14Font(metrics, overrideEncoding); } @@ -84,9 +84,9 @@ var descriptor = FontDictionaryAccessHelper.GetFontDescriptor(pdfScanner, dictionary); - var font = ParseFontProgram(descriptor, isLenientParsing); + var font = ParseFontProgram(descriptor); - var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor, isLenientParsing); + var name = FontDictionaryAccessHelper.GetName(pdfScanner, dictionary, descriptor); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) @@ -111,7 +111,7 @@ return default(Encoding); }); - Encoding encoding = encodingReader.Read(dictionary, isLenientParsing, descriptor, fromFont); + Encoding encoding = encodingReader.Read(dictionary, descriptor, fromFont); if (encoding == null) { @@ -121,7 +121,7 @@ return new Type1FontSimple(name, firstCharacter, lastCharacter, widths, descriptor, encoding, toUnicodeCMap, font); } - private Union ParseFontProgram(FontDescriptor descriptor, bool isLenientParsing) + private Union ParseFontProgram(FontDescriptor descriptor) { if (descriptor?.FontFile == null) { @@ -159,10 +159,7 @@ } catch { - if (!isLenientParsing) - { - throw; - } + // ignored. } return null; diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type3FontHandler.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type3FontHandler.cs index 853bf120..c9d38334 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type3FontHandler.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/Handlers/Type3FontHandler.cs @@ -25,7 +25,7 @@ this.scanner = scanner; } - public IFont Generate(DictionaryToken dictionary, bool isLenientParsing) + public IFont Generate(DictionaryToken dictionary) { var boundingBox = GetBoundingBox(dictionary); @@ -35,7 +35,7 @@ var lastCharacter = FontDictionaryAccessHelper.GetLastCharacter(dictionary); var widths = FontDictionaryAccessHelper.GetWidths(scanner, dictionary); - Encoding encoding = encodingReader.Read(dictionary, isLenientParsing); + Encoding encoding = encodingReader.Read(dictionary); CMap toUnicodeCMap = null; if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj)) diff --git a/src/UglyToad.PdfPig/PdfFonts/Parser/IEncodingReader.cs b/src/UglyToad.PdfPig/PdfFonts/Parser/IEncodingReader.cs index 114f6011..09685459 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Parser/IEncodingReader.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Parser/IEncodingReader.cs @@ -5,7 +5,7 @@ internal interface IEncodingReader { - Encoding Read(DictionaryToken fontDictionary, bool isLenientParsing, FontDescriptor descriptor = null, + Encoding Read(DictionaryToken fontDictionary, FontDescriptor descriptor = null, Encoding fontEncoding = null); } } \ No newline at end of file