Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tests/Parser/PageContentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class PageContentParserTests
{
private readonly PageContentParser parser = new PageContentParser(new ReflectionGraphicsStateOperationFactory());
private readonly PageContentParser parser = new PageContentParser(ReflectionGraphicsStateOperationFactory.Instance);
private readonly ILog log = new NoOpLog();

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Geometry.GeometryExtensions",
"UglyToad.PdfPig.Geometry.UserSpaceUnit",
"UglyToad.PdfPig.Graphics.BaseStreamProcessor`1",
"UglyToad.PdfPig.Graphics.IGraphicsStateOperationFactory",
"UglyToad.PdfPig.Graphics.ReflectionGraphicsStateOperationFactory",
"UglyToad.PdfPig.Graphics.Colors.CMYKColor",
"UglyToad.PdfPig.Graphics.Colors.ColorSpace",
"UglyToad.PdfPig.Graphics.PdfPath",
Expand Down Expand Up @@ -265,6 +267,7 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Outline.Destinations.NamedDestinations",
"UglyToad.PdfPig.ParsingOptions",
"UglyToad.PdfPig.Parser.IPageContentParser",
"UglyToad.PdfPig.Parser.PageContentParser",
"UglyToad.PdfPig.Parser.Parts.DirectObjectFinder",
"UglyToad.PdfPig.PdfDocument",
"UglyToad.PdfPig.PdfExtensions",
Expand Down
10 changes: 9 additions & 1 deletion src/UglyToad.PdfPig/Graphics/IGraphicsStateOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
using Operations;
using Tokens;

internal interface IGraphicsStateOperationFactory
/// <summary>
/// Graphics state operation factory interface.
/// </summary>
public interface IGraphicsStateOperationFactory
{
/// <summary>
/// Create a graphics state operation.
/// </summary>
/// <param name="op">The operator token to build from.</param>
/// <param name="operands"></param>
IGraphicsStateOperation? Create(OperatorToken op, IReadOnlyList<IToken> operands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ namespace UglyToad.PdfPig.Graphics
using System.Reflection;
using Tokens;

internal sealed class ReflectionGraphicsStateOperationFactory : IGraphicsStateOperationFactory
/// <summary>
/// Reflection based graphics state operation factory.
/// </summary>
public sealed class ReflectionGraphicsStateOperationFactory : IGraphicsStateOperationFactory
{
/// <summary>
/// The single instance of the <see cref="ReflectionGraphicsStateOperationFactory"/>.
/// </summary>
public static readonly ReflectionGraphicsStateOperationFactory Instance = new ReflectionGraphicsStateOperationFactory();

private ReflectionGraphicsStateOperationFactory()
{
// private
}

private static readonly IReadOnlyDictionary<string, Type> operations =
new Dictionary<string, Type>
{
Expand Down Expand Up @@ -161,6 +174,7 @@ private static double OperandToDouble(IToken token)
return numeric.Data;
}

/// <inheritdoc/>
public IGraphicsStateOperation? Create(OperatorToken op, IReadOnlyList<IToken> operands)
{
switch (op.Data)
Expand Down
29 changes: 28 additions & 1 deletion src/UglyToad.PdfPig/Parser/PageContentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,44 @@
using Tokenization.Scanner;
using Tokens;

internal sealed class PageContentParser : IPageContentParser
/// <summary>
/// Provides functionality to parse the content of a PDF page, extracting graphics state operations
/// from the input data. This class is responsible for interpreting the PDF content stream and
/// converting it into a collection of operations.
/// </summary>
public sealed class PageContentParser : IPageContentParser
{
private readonly IGraphicsStateOperationFactory operationFactory;
private readonly bool useLenientParsing;

/// <summary>
/// Initialises a new instance of the <see cref="PageContentParser"/> class.
/// </summary>
/// <param name="operationFactory">
/// The factory responsible for creating graphics state operations.
/// </param>
/// <param name="useLenientParsing">
/// A value indicating whether lenient parsing should be used. Defaults to <c>false</c>.
/// </param>
public PageContentParser(IGraphicsStateOperationFactory operationFactory, bool useLenientParsing = false)
{
this.operationFactory = operationFactory;
this.useLenientParsing = useLenientParsing;
}

/// <summary>
/// Parses the content of a PDF page and extracts a collection of graphics state operations.
/// </summary>
/// <param name="pageNumber">The number of the page being parsed.</param>
/// <param name="inputBytes">The input bytes representing the content of the page.</param>
/// <param name="log">The logger instance for recording parsing-related information.</param>
/// <returns>A read-only list of graphics state operations extracted from the page content.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="inputBytes"/> or <paramref name="log"/> is <c>null</c>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the parsing process encounters an invalid or unsupported token.
/// </exception>
public IReadOnlyList<IGraphicsStateOperation> Parse(
int pageNumber,
IInputBytes inputBytes,
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 @@ -192,7 +192,7 @@ private static PdfDocument OpenDocument(
parsingOptions.UseLenientParsing);

var pageFactory = new PageFactory(pdfScanner, resourceContainer, filterProvider,
new PageContentParser(new ReflectionGraphicsStateOperationFactory(), parsingOptions.UseLenientParsing), parsingOptions);
new PageContentParser(ReflectionGraphicsStateOperationFactory.Instance, parsingOptions.UseLenientParsing), parsingOptions);

var catalog = CatalogFactory.Create(
rootReference,
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Writer/NoTextTokenWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private bool TryGetStreamWithoutText(StreamToken streamToken, [NotNullWhen(true)
return false;
}

var pageContentParser = new PageContentParser(new ReflectionGraphicsStateOperationFactory());
var pageContentParser = new PageContentParser(ReflectionGraphicsStateOperationFactory.Instance);
IReadOnlyList<IGraphicsStateOperation> operations;
try
{
Expand Down
Loading