From 815705494acb4c4530d4be2d73b47aadef564e0d Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Tue, 24 Dec 2019 23:24:52 +0000 Subject: [PATCH] cache last loaded font from the resource store during content parsing --- src/UglyToad.PdfPig/Content/ResourceStore.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/UglyToad.PdfPig/Content/ResourceStore.cs b/src/UglyToad.PdfPig/Content/ResourceStore.cs index 73236a9c..a0008ae5 100644 --- a/src/UglyToad.PdfPig/Content/ResourceStore.cs +++ b/src/UglyToad.PdfPig/Content/ResourceStore.cs @@ -22,6 +22,8 @@ private readonly Dictionary namedColorSpaces = new Dictionary(); + private (NameToken name, IFont font) lastLoadedFont; + public ResourceStore(IPdfTokenScanner scanner, IFontFactory fontFactory) { this.scanner = scanner; @@ -30,6 +32,8 @@ public void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing) { + lastLoadedFont = (null, null); + currentResourceState.Push(); if (resourceDictionary.TryGet(NameToken.Font, out var fontBase)) @@ -101,11 +105,14 @@ public void UnloadResourceDictionary() { + lastLoadedFont = (null, null); currentResourceState.Pop(); } private void LoadFontDictionary(DictionaryToken fontDictionary, bool isLenientParsing) { + lastLoadedFont = (null, null); + foreach (var pair in fontDictionary.Data) { if (!(pair.Value is IndirectReferenceToken objectKey)) @@ -140,15 +147,24 @@ public IFont GetFont(NameToken name) { + if (lastLoadedFont.name == name) + { + return lastLoadedFont.font; + } + var reference = currentResourceState[name]; loadedFonts.TryGetValue(reference, out var font); + lastLoadedFont = (name, font); + return font; } public IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken, bool isLenientParsing) { + lastLoadedFont = (null, null); + if (!DirectObjectFinder.TryGet(fontReferenceToken, scanner, out DictionaryToken fontDictionaryToken)) { throw new PdfDocumentFormatException($"The requested font reference token {fontReferenceToken} wasn't a font.");