Skip to content

Commit

Permalink
Make IResourceStore part of the public API and pass InternalParsingOp…
Browse files Browse the repository at this point in the history
…tions to the ResourceStore constructor
  • Loading branch information
BobLd committed Oct 22, 2023
1 parent 7ab3a6a commit ba865b3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
{
using System.Collections.Generic;
using Content;
using Logging;
using PdfFonts;
using PdfPig.Graphics;
using PdfPig.Tokens;
using PdfPig.Core;
using System;
using Tokens;
using UglyToad.PdfPig.Graphics.Core;
using UglyToad.PdfPig.Graphics.Operations.TextPositioning;
Expand All @@ -32,7 +34,11 @@ public TestOperationContext()
{
StateStack.Push(new CurrentGraphicsState()
{
ColorSpaceContext = new ColorSpaceContext(GetCurrentState, new ResourceStore(new TestPdfTokenScanner(), new TestFontFactory(), new TestFilterProvider()))
ColorSpaceContext = new ColorSpaceContext(GetCurrentState,
new ResourceStore(new TestPdfTokenScanner(),
new TestFontFactory(),
new TestFilterProvider(),
new InternalParsingOptions(Array.Empty<string>(), true, false, true, true, new NoOpLog())))
});
CurrentSubpath = new PdfSubpath();
}
Expand Down
1 change: 1 addition & 0 deletions src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Content.Hyperlink",
"UglyToad.PdfPig.Content.InlineImage",
"UglyToad.PdfPig.Content.IPdfImage",
"UglyToad.PdfPig.Content.IResourceStore",
"UglyToad.PdfPig.Content.Letter",
"UglyToad.PdfPig.Content.MarkedContentElement",
"UglyToad.PdfPig.Content.MediaBox",
Expand Down
37 changes: 35 additions & 2 deletions src/UglyToad.PdfPig/Content/IResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,65 @@
using System.Collections.Generic;
using Tokens;

internal interface IResourceStore
/// <summary>
/// Resource store.
/// </summary>
public interface IResourceStore
{
void LoadResourceDictionary(DictionaryToken resourceDictionary, InternalParsingOptions parsingOptions);
/// <summary>
/// Load the resource dictionary.
/// </summary>
void LoadResourceDictionary(DictionaryToken resourceDictionary);

/// <summary>
/// Remove any named resources and associated state for the last resource dictionary loaded.
/// Does not affect the cached resources, just the labels associated with them.
/// </summary>
void UnloadResourceDictionary();

/// <summary>
/// Get the font corresponding to the name.
/// </summary>
IFont GetFont(NameToken name);

/// <summary>
/// Try getting the XObject corresponding to the name.
/// </summary>
bool TryGetXObject(NameToken name, out StreamToken stream);

/// <summary>
/// Get the extended graphics state dictionary corresponding to the name.
/// </summary>
DictionaryToken GetExtendedGraphicsStateDictionary(NameToken name);

/// <summary>
/// Get the font from the <see cref="IndirectReferenceToken"/>.
/// </summary>
IFont GetFontDirectly(IndirectReferenceToken fontReferenceToken);

/// <summary>
/// Get the named color space by its name.
/// </summary>
bool TryGetNamedColorSpace(NameToken name, out ResourceColorSpace namedColorSpace);

/// <summary>
/// Get the color space details corresponding to the name.
/// </summary>
ColorSpaceDetails GetColorSpaceDetails(NameToken name, DictionaryToken dictionary);

/// <summary>
/// Get the marked content properties dictionary corresponding to the name.
/// </summary>
DictionaryToken GetMarkedContentPropertiesDictionary(NameToken name);

/// <summary>
/// Get all <see cref="PatternColor"/> as a dictionary. Keys are the <see cref="PatternColor"/> names.
/// </summary>
IReadOnlyDictionary<NameToken, PatternColor> GetPatterns();

/// <summary>
/// Get the shading corresponding to the name.
/// </summary>
Shading GetShading(NameToken name);
}
}
9 changes: 7 additions & 2 deletions src/UglyToad.PdfPig/Content/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class ResourceStore : IResourceStore
private readonly IPdfTokenScanner scanner;
private readonly IFontFactory fontFactory;
private readonly ILookupFilterProvider filterProvider;
private readonly InternalParsingOptions parsingOptions;

private readonly Dictionary<IndirectReference, IFont> loadedFonts = new Dictionary<IndirectReference, IFont>();
private readonly Dictionary<NameToken, IFont> loadedDirectFonts = new Dictionary<NameToken, IFont>();
Expand All @@ -34,14 +35,18 @@ internal class ResourceStore : IResourceStore

private (NameToken name, IFont font) lastLoadedFont;

public ResourceStore(IPdfTokenScanner scanner, IFontFactory fontFactory, ILookupFilterProvider filterProvider)
public ResourceStore(IPdfTokenScanner scanner,
IFontFactory fontFactory,
ILookupFilterProvider filterProvider,
InternalParsingOptions parsingOptions)
{
this.scanner = scanner;
this.fontFactory = fontFactory;
this.filterProvider = filterProvider;
this.parsingOptions = parsingOptions;
}

public void LoadResourceDictionary(DictionaryToken resourceDictionary, InternalParsingOptions parsingOptions)
public void LoadResourceDictionary(DictionaryToken resourceDictionary)
{
lastLoadedFont = (null, null);
loadedNamedColorSpaceDetails.Clear();
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private void ProcessFormXObject(StreamToken formStream, NameToken xObjectName)
var hasResources = formStream.StreamDictionary.TryGet<DictionaryToken>(NameToken.Resources, pdfScanner, out var formResources);
if (hasResources)
{
resourceStore.LoadResourceDictionary(formResources, parsingOptions);
resourceStore.LoadResourceDictionary(formResources);
}

// 1. Save current state.
Expand Down
5 changes: 2 additions & 3 deletions src/UglyToad.PdfPig/Parser/PageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Graphics;
using Graphics.Operations;
using Logging;
using Outline;
using Outline.Destinations;
using Parts;
using Tokenization.Scanner;
Expand Down Expand Up @@ -66,13 +65,13 @@ public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageT
{
var resource = pageTreeMembers.ParentResources.Dequeue();

resourceStore.LoadResourceDictionary(resource, parsingOptions);
resourceStore.LoadResourceDictionary(resource);
stackDepth++;
}

if (dictionary.TryGet(NameToken.Resources, pdfScanner, out DictionaryToken resources))
{
resourceStore.LoadResourceDictionary(resources, parsingOptions);
resourceStore.LoadResourceDictionary(resources);
stackDepth++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Parser/PdfDocumentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static PdfDocument OpenDocument(
type1Handler,
new Type3FontHandler(pdfScanner, filterProvider, encodingReader));

var resourceContainer = new ResourceStore(pdfScanner, fontFactory, filterProvider);
var resourceContainer = new ResourceStore(pdfScanner, fontFactory, filterProvider, parsingOptions);

var information = DocumentInformationFactory.Create(
pdfScanner,
Expand Down

0 comments on commit ba865b3

Please sign in to comment.