handle case where xobjects use same key as fonts

in document 10122 the font and xobject names are the same so the
xobject overwrote references to the font for the page content, separate
the dictionaries
This commit is contained in:
EliotJones
2025-09-13 15:52:16 +02:00
parent e886ae648f
commit 472fcea60e

View File

@@ -21,7 +21,8 @@
private readonly Dictionary<IndirectReference, IFont> loadedFonts = new Dictionary<IndirectReference, IFont>(); private readonly Dictionary<IndirectReference, IFont> loadedFonts = new Dictionary<IndirectReference, IFont>();
private readonly Dictionary<NameToken, IFont> loadedDirectFonts = new Dictionary<NameToken, IFont>(); private readonly Dictionary<NameToken, IFont> loadedDirectFonts = new Dictionary<NameToken, IFont>();
private readonly StackDictionary<NameToken, IndirectReference> currentResourceState = new StackDictionary<NameToken, IndirectReference>(); private readonly StackDictionary<NameToken, IndirectReference> currentFontState = new StackDictionary<NameToken, IndirectReference>();
private readonly StackDictionary<NameToken, IndirectReference> currentXObjectState = new StackDictionary<NameToken, IndirectReference>();
private readonly Dictionary<NameToken, DictionaryToken> extendedGraphicsStates = new Dictionary<NameToken, DictionaryToken>(); private readonly Dictionary<NameToken, DictionaryToken> extendedGraphicsStates = new Dictionary<NameToken, DictionaryToken>();
@@ -53,7 +54,8 @@
loadedNamedColorSpaceDetails.Clear(); loadedNamedColorSpaceDetails.Clear();
namedColorSpaces.Push(); namedColorSpaces.Push();
currentResourceState.Push(); currentFontState.Push();
currentXObjectState.Push();
if (resourceDictionary.TryGet(NameToken.Font, out var fontBase)) if (resourceDictionary.TryGet(NameToken.Font, out var fontBase))
{ {
@@ -78,7 +80,7 @@
throw new InvalidOperationException($"Expected the XObject dictionary value for key /{pair.Key} to be an indirect reference, instead got: {pair.Value}."); throw new InvalidOperationException($"Expected the XObject dictionary value for key /{pair.Key} to be an indirect reference, instead got: {pair.Value}.");
} }
currentResourceState[NameToken.Create(pair.Key)] = reference.Data; currentXObjectState[NameToken.Create(pair.Key)] = reference.Data;
} }
} }
@@ -185,7 +187,8 @@
{ {
lastLoadedFont = (null, null); lastLoadedFont = (null, null);
loadedNamedColorSpaceDetails.Clear(); loadedNamedColorSpaceDetails.Clear();
currentResourceState.Pop(); currentFontState.Pop();
currentXObjectState.Pop();
namedColorSpaces.Pop(); namedColorSpaces.Pop();
} }
@@ -199,7 +202,7 @@
{ {
var reference = objectKey.Data; var reference = objectKey.Data;
currentResourceState[NameToken.Create(pair.Key)] = reference; currentFontState[NameToken.Create(pair.Key)] = reference;
if (loadedFonts.ContainsKey(reference)) if (loadedFonts.ContainsKey(reference))
{ {
@@ -245,7 +248,7 @@
} }
IFont? font; IFont? font;
if (currentResourceState.TryGetValue(name, out var reference)) if (currentFontState.TryGetValue(name, out var reference))
{ {
loadedFonts.TryGetValue(reference, out font); loadedFonts.TryGetValue(reference, out font);
} }
@@ -335,7 +338,7 @@
public bool TryGetXObject(NameToken name, [NotNullWhen(true)] out StreamToken? stream) public bool TryGetXObject(NameToken name, [NotNullWhen(true)] out StreamToken? stream)
{ {
stream = null; stream = null;
if (!currentResourceState.TryGetValue(name, out var indirectReference)) if (!currentXObjectState.TryGetValue(name, out var indirectReference))
{ {
return false; return false;
} }