mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-11-28 17:47:12 +08:00
cache last loaded font from the resource store during content parsing
This commit is contained in:
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
private readonly Dictionary<NameToken, ResourceColorSpace> namedColorSpaces = new Dictionary<NameToken, ResourceColorSpace>();
|
private readonly Dictionary<NameToken, ResourceColorSpace> namedColorSpaces = new Dictionary<NameToken, ResourceColorSpace>();
|
||||||
|
|
||||||
|
private (NameToken name, IFont font) lastLoadedFont;
|
||||||
|
|
||||||
public ResourceStore(IPdfTokenScanner scanner, IFontFactory fontFactory)
|
public ResourceStore(IPdfTokenScanner scanner, IFontFactory fontFactory)
|
||||||
{
|
{
|
||||||
this.scanner = scanner;
|
this.scanner = scanner;
|
||||||
@@ -30,6 +32,8 @@
|
|||||||
|
|
||||||
public void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing)
|
public void LoadResourceDictionary(DictionaryToken resourceDictionary, bool isLenientParsing)
|
||||||
{
|
{
|
||||||
|
lastLoadedFont = (null, null);
|
||||||
|
|
||||||
currentResourceState.Push();
|
currentResourceState.Push();
|
||||||
|
|
||||||
if (resourceDictionary.TryGet(NameToken.Font, out var fontBase))
|
if (resourceDictionary.TryGet(NameToken.Font, out var fontBase))
|
||||||
@@ -101,11 +105,14 @@
|
|||||||
|
|
||||||
public void UnloadResourceDictionary()
|
public void UnloadResourceDictionary()
|
||||||
{
|
{
|
||||||
|
lastLoadedFont = (null, null);
|
||||||
currentResourceState.Pop();
|
currentResourceState.Pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadFontDictionary(DictionaryToken fontDictionary, bool isLenientParsing)
|
private void LoadFontDictionary(DictionaryToken fontDictionary, bool isLenientParsing)
|
||||||
{
|
{
|
||||||
|
lastLoadedFont = (null, null);
|
||||||
|
|
||||||
foreach (var pair in fontDictionary.Data)
|
foreach (var pair in fontDictionary.Data)
|
||||||
{
|
{
|
||||||
if (!(pair.Value is IndirectReferenceToken objectKey))
|
if (!(pair.Value is IndirectReferenceToken objectKey))
|
||||||
@@ -140,15 +147,24 @@
|
|||||||
|
|
||||||
public IFont GetFont(NameToken name)
|
public IFont GetFont(NameToken name)
|
||||||
{
|
{
|
||||||
|
if (lastLoadedFont.name == name)
|
||||||
|
{
|
||||||
|
return lastLoadedFont.font;
|
||||||
|
}
|
||||||
|
|
||||||
var reference = currentResourceState[name];
|
var reference = currentResourceState[name];
|
||||||
|
|
||||||
loadedFonts.TryGetValue(reference, out var font);
|
loadedFonts.TryGetValue(reference, out var font);
|
||||||
|
|
||||||
|
lastLoadedFont = (name, font);
|
||||||
|
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken, bool isLenientParsing)
|
public IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken, bool isLenientParsing)
|
||||||
{
|
{
|
||||||
|
lastLoadedFont = (null, null);
|
||||||
|
|
||||||
if (!DirectObjectFinder.TryGet(fontReferenceToken, scanner, out DictionaryToken fontDictionaryToken))
|
if (!DirectObjectFinder.TryGet(fontReferenceToken, scanner, out DictionaryToken fontDictionaryToken))
|
||||||
{
|
{
|
||||||
throw new PdfDocumentFormatException($"The requested font reference token {fontReferenceToken} wasn't a font.");
|
throw new PdfDocumentFormatException($"The requested font reference token {fontReferenceToken} wasn't a font.");
|
||||||
|
|||||||
Reference in New Issue
Block a user