remove islenientparsing from the font handlers

we're removing islenientparsing to make the code simpler to maintain and use as well as more resilient.
This commit is contained in:
Eliot Jones
2020-02-28 11:37:18 +00:00
parent 746cbfa30c
commit 7b09999a3f
15 changed files with 42 additions and 58 deletions

View File

@@ -105,7 +105,7 @@
private class TestFontFactory : IFontFactory
{
public IFont Get(DictionaryToken dictionary, bool isLenientParsing)
public IFont Get(DictionaryToken dictionary)
{
return null;
}

View File

@@ -6,7 +6,7 @@
internal interface IResourceStore
{
void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing);
void LoadResourceDictionary(DictionaryToken resourceDictionary);
/// <summary>
/// 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);

View File

@@ -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<DictionaryToken>(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;
}

View File

@@ -362,7 +362,7 @@
var hasResources = formStream.StreamDictionary.TryGet<DictionaryToken>(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);
}
}

View File

@@ -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++;
}

View File

@@ -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<NameToken, IFontHandler> 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.");

View File

@@ -4,6 +4,6 @@
internal interface IFontFactory
{
IFont Get(DictionaryToken dictionary, bool isLenientParsing);
IFont Get(DictionaryToken dictionary);
}
}

View File

@@ -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))

View File

@@ -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))
{

View File

@@ -4,6 +4,6 @@
internal interface IFontHandler
{
IFont Generate(DictionaryToken dictionary, bool isLenientParsing);
IFont Generate(DictionaryToken dictionary);
}
}

View File

@@ -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<IToken>(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)

View File

@@ -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);

View File

@@ -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<Type1Font, CompactFontFormatFontCollection> ParseFontProgram(FontDescriptor descriptor, bool isLenientParsing)
private Union<Type1Font, CompactFontFormatFontCollection> ParseFontProgram(FontDescriptor descriptor)
{
if (descriptor?.FontFile == null)
{
@@ -159,10 +159,7 @@
}
catch
{
if (!isLenientParsing)
{
throw;
}
// ignored.
}
return null;

View File

@@ -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))

View File

@@ -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);
}
}