Make classes related to page content parsing public

This commit is contained in:
BobLd
2025-06-28 13:07:59 +01:00
parent d1d79b0b4c
commit 73ce5bbb73
7 changed files with 58 additions and 6 deletions

View File

@@ -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]

View File

@@ -125,6 +125,8 @@
"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",
@@ -265,6 +267,7 @@
"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",

View File

@@ -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);
}
}

View File

@@ -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>
{
@@ -161,6 +174,7 @@ namespace UglyToad.PdfPig.Graphics
return numeric.Data;
}
/// <inheritdoc/>
public IGraphicsStateOperation? Create(OperatorToken op, IReadOnlyList<IToken> operands)
{
switch (op.Data)

View File

@@ -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,

View File

@@ -192,7 +192,7 @@
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,

View File

@@ -74,7 +74,7 @@ namespace UglyToad.PdfPig.Writer
return false;
}
var pageContentParser = new PageContentParser(new ReflectionGraphicsStateOperationFactory());
var pageContentParser = new PageContentParser(ReflectionGraphicsStateOperationFactory.Instance);
IReadOnlyList<IGraphicsStateOperation> operations;
try
{