Change Subpath to path where necessary

This commit is contained in:
BobLd
2020-04-05 17:58:57 +01:00
committed by Eliot Jones
parent 002d8dc06d
commit 43b40da5d5
15 changed files with 37 additions and 63 deletions
@@ -10,6 +10,7 @@
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter;
using UglyToad.PdfPig.Graphics;
using Util; using Util;
/// <inheritdoc /> /// <inheritdoc />
@@ -159,7 +160,7 @@
return altoPage; return altoPage;
} }
private AltoDocument.AltoGraphicalElement ToAltoGraphicalElement(PdfSubpath pdfPath, double height) private AltoDocument.AltoGraphicalElement ToAltoGraphicalElement(PdfPath pdfPath, double height)
{ {
graphicalElementCount++; graphicalElementCount++;
@@ -6,6 +6,7 @@
using System; using System;
using System.Linq; using System.Linq;
using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter;
using UglyToad.PdfPig.Graphics;
using Util; using Util;
/// <summary> /// <summary>
@@ -176,7 +177,7 @@
/// <param name="pageHeight"></param> /// <param name="pageHeight"></param>
/// <param name="subPaths"></param> /// <param name="subPaths"></param>
/// <param name="level">The indent level.</param> /// <param name="level">The indent level.</param>
private string GetCode(PdfSubpath path, double pageHeight, bool subPaths, int level) private string GetCode(PdfPath path, double pageHeight, bool subPaths, int level)
{ {
if (path == null) return string.Empty; if (path == null) return string.Empty;
@@ -190,7 +191,7 @@
areaCount++; areaCount++;
hocr += GetIndent(level) + @"<div class='ocr_carea' id='block_" + pageCount + "_" hocr += GetIndent(level) + @"<div class='ocr_carea' id='block_" + pageCount + "_"
+ areaCount + "' title='" + GetCode(bbox.Value, pageHeight) + "'>\n"; + areaCount + "' title='" + GetCode(bbox.Value, pageHeight) + "'>\n";
foreach (var subPath in path.Commands) foreach (var subPath in path)
{ {
var subBbox = subPath.GetBoundingRectangle(); var subBbox = subPath.GetBoundingRectangle();
if (subBbox.HasValue) if (subBbox.HasValue)
@@ -12,6 +12,7 @@
using System.Xml.Serialization; using System.Xml.Serialization;
using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter;
using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector; using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector;
using UglyToad.PdfPig.Graphics;
using Util; using Util;
/// <summary> /// <summary>
@@ -199,7 +200,7 @@
return pageXmlPage; return pageXmlPage;
} }
private PageXmlDocument.PageXmlLineDrawingRegion ToPageXmlLineDrawingRegion(PdfSubpath pdfPath, double height) private PageXmlDocument.PageXmlLineDrawingRegion ToPageXmlLineDrawingRegion(PdfPath pdfPath, double height)
{ {
var bbox = pdfPath.GetBoundingRectangle(); var bbox = pdfPath.GetBoundingRectangle();
if (bbox.HasValue) if (bbox.HasValue)
@@ -20,7 +20,9 @@
public TransformationMatrix CurrentTransformationMatrix => GetCurrentState().CurrentTransformationMatrix; public TransformationMatrix CurrentTransformationMatrix => GetCurrentState().CurrentTransformationMatrix;
public PdfSubpath CurrentPath { get; set; } public PdfSubpath CurrentSubpath { get; set; }
public PdfPath CurrentPath { get; set; }
public IColorSpaceContext ColorSpaceContext { get; } public IColorSpaceContext ColorSpaceContext { get; }
@@ -29,7 +31,7 @@
public TestOperationContext() public TestOperationContext()
{ {
StateStack.Push(new CurrentGraphicsState()); StateStack.Push(new CurrentGraphicsState());
CurrentPath = new PdfSubpath(); CurrentSubpath = new PdfSubpath();
ColorSpaceContext = new ColorSpaceContext(GetCurrentState, new ResourceStore(new TestPdfTokenScanner(), new TestFontFactory())); ColorSpaceContext = new ColorSpaceContext(GetCurrentState, new ResourceStore(new TestPdfTokenScanner(), new TestFontFactory()));
} }
+3 -2
View File
@@ -10,6 +10,7 @@
using Util; using Util;
using Util.JetBrains.Annotations; using Util.JetBrains.Annotations;
using Tokenization.Scanner; using Tokenization.Scanner;
using UglyToad.PdfPig.Graphics;
/// <summary> /// <summary>
/// Contains the content and provides access to methods of a single page in the <see cref="PdfDocument"/>. /// Contains the content and provides access to methods of a single page in the <see cref="PdfDocument"/>.
@@ -172,9 +173,9 @@
private readonly AnnotationProvider annotationProvider; private readonly AnnotationProvider annotationProvider;
/// <summary> /// <summary>
/// The set of <see cref="PdfSubpath"/>s drawn by the PDF content. /// The set of <see cref="PdfPath"/>s drawn by the PDF content.
/// </summary> /// </summary>
public IReadOnlyList<PdfSubpath> Paths => page.Content?.Paths ?? new List<PdfSubpath>(); public IReadOnlyList<PdfPath> Paths => page.Content?.Paths ?? new List<PdfPath>();
internal Experimental(Page page, AnnotationProvider annotationProvider) internal Experimental(Page page, AnnotationProvider annotationProvider)
{ {
+2 -2
View File
@@ -28,12 +28,12 @@
public IReadOnlyList<Letter> Letters { get; } public IReadOnlyList<Letter> Letters { get; }
public IReadOnlyList<PdfSubpath> Paths { get; } public IReadOnlyList<PdfPath> Paths { get; }
public int NumberOfImages => images.Count; public int NumberOfImages => images.Count;
internal PageContent(IReadOnlyList<IGraphicsStateOperation> graphicsStateOperations, IReadOnlyList<Letter> letters, internal PageContent(IReadOnlyList<IGraphicsStateOperation> graphicsStateOperations, IReadOnlyList<Letter> letters,
IReadOnlyList<PdfSubpath> paths, IReadOnlyList<PdfPath> paths,
IReadOnlyList<Union<XObjectContentRecord, InlineImage>> images, IReadOnlyList<Union<XObjectContentRecord, InlineImage>> images,
IReadOnlyList<MarkedContentElement> markedContents, IReadOnlyList<MarkedContentElement> markedContents,
IPdfTokenScanner pdfScanner, IPdfTokenScanner pdfScanner,
@@ -28,7 +28,7 @@
/// <summary> /// <summary>
/// Stores each path as it is encountered in the content stream. /// Stores each path as it is encountered in the content stream.
/// </summary> /// </summary>
private readonly List<PdfSubpath> paths = new List<PdfSubpath>(); private readonly List<PdfPath> paths = new List<PdfPath>();
/// <summary> /// <summary>
/// Stores a link to each image (either inline or XObject) as it is encountered in the content stream. /// Stores a link to each image (either inline or XObject) as it is encountered in the content stream.
@@ -52,7 +52,6 @@
private Stack<CurrentGraphicsState> graphicsStack = new Stack<CurrentGraphicsState>(); private Stack<CurrentGraphicsState> graphicsStack = new Stack<CurrentGraphicsState>();
private IFont activeExtendedGraphicsStateFont; private IFont activeExtendedGraphicsStateFont;
private InlineImageBuilder inlineImageBuilder; private InlineImageBuilder inlineImageBuilder;
private bool currentPathAdded;
private int pageNumber; private int pageNumber;
/// <summary> /// <summary>
@@ -66,7 +65,9 @@
public TransformationMatrix CurrentTransformationMatrix => GetCurrentState().CurrentTransformationMatrix; public TransformationMatrix CurrentTransformationMatrix => GetCurrentState().CurrentTransformationMatrix;
public PdfSubpath CurrentPath { get; private set; } public PdfSubpath CurrentSubpath { get; private set; }
public PdfPath CurrentPath { get; private set; }
public IColorSpaceContext ColorSpaceContext { get; } public IColorSpaceContext ColorSpaceContext { get; }
@@ -398,61 +399,22 @@
public void BeginSubpath() public void BeginSubpath()
{ {
if (CurrentPath != null && CurrentPath.Commands.Count > 0 && !currentPathAdded)
{
paths.Add(CurrentPath);
markedContentStack.AddPath(CurrentPath);
}
CurrentPath = new PdfSubpath();
currentPathAdded = false;
} }
public void StrokePath(bool close) public void StrokePath(bool close)
{ {
if (CurrentPath == null)
{
return;
}
if (close)
{
ClosePath();
}
else
{
paths.Add(CurrentPath);
markedContentStack.AddPath(CurrentPath);
currentPathAdded = true;
}
} }
public void FillPath(bool close) public void FillPath(bool close)
{ {
if (CurrentPath == null)
{
return;
}
if (close)
{
ClosePath();
}
else
{
paths.Add(CurrentPath);
markedContentStack.AddPath(CurrentPath);
currentPathAdded = true;
}
} }
public void ClosePath() public void ClosePath()
{ {
CurrentPath.ClosePath();
paths.Add(CurrentPath);
markedContentStack.AddPath(CurrentPath);
CurrentPath = null;
currentPathAdded = false;
} }
public void SetNamedGraphicsState(NameToken stateName) public void SetNamedGraphicsState(NameToken stateName)
@@ -557,7 +519,7 @@
public void ModifyClippingIntersect(FillingRule clippingRule) public void ModifyClippingIntersect(FillingRule clippingRule)
{ {
if (CurrentPath == null) if (CurrentSubpath == null)
{ {
return; return;
} }
@@ -10,11 +10,17 @@
/// </summary> /// </summary>
public interface IOperationContext public interface IOperationContext
{ {
/// <summary>
/// The current subpath being drawn if applicable.
/// </summary>
[CanBeNull]
PdfSubpath CurrentSubpath { get; }
/// <summary> /// <summary>
/// The current path being drawn if applicable. /// The current path being drawn if applicable.
/// </summary> /// </summary>
[CanBeNull] [CanBeNull]
PdfSubpath CurrentPath { get; } PdfPath CurrentPath { get; }
/// <summary> /// <summary>
/// The active colorspaces for this content stream. /// The active colorspaces for this content stream.
@@ -78,7 +78,7 @@
var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1); var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1);
var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2); var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2);
var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end);
operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, operationContext.CurrentSubpath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y,
controlPoint2Transform.X, controlPoint2Transform.Y, controlPoint2Transform.X, controlPoint2Transform.Y,
endTransform.X, endTransform.Y); endTransform.X, endTransform.Y);
operationContext.CurrentPosition = endTransform; operationContext.CurrentPosition = endTransform;
@@ -60,7 +60,7 @@
var end = new PdfPoint(X3, Y3); var end = new PdfPoint(X3, Y3);
var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1); var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1);
var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end);
operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, operationContext.CurrentSubpath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y,
endTransform.X, endTransform.X,
endTransform.Y, endTransform.Y,
endTransform.X, endTransform.X,
@@ -63,7 +63,7 @@
var lowerLeftTransform = operationContext.CurrentTransformationMatrix.Transform(lowerLeft); var lowerLeftTransform = operationContext.CurrentTransformationMatrix.Transform(lowerLeft);
var upperRightTransform = operationContext.CurrentTransformationMatrix.Transform(upperRight); var upperRightTransform = operationContext.CurrentTransformationMatrix.Transform(upperRight);
operationContext.CurrentPath.Rectangle(lowerLeftTransform.X, lowerLeftTransform.Y, upperRightTransform.X - lowerLeftTransform.X, upperRightTransform.Y - lowerLeftTransform.Y); operationContext.CurrentSubpath.Rectangle(lowerLeftTransform.X, lowerLeftTransform.Y, upperRightTransform.X - lowerLeftTransform.X, upperRightTransform.Y - lowerLeftTransform.Y);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -60,7 +60,7 @@
var end = new PdfPoint(X3, Y3); var end = new PdfPoint(X3, Y3);
var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2); var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2);
var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end);
operationContext.CurrentPath.BezierCurveTo(operationContext.CurrentPosition.X, operationContext.CurrentSubpath.BezierCurveTo(operationContext.CurrentPosition.X,
operationContext.CurrentPosition.Y, operationContext.CurrentPosition.Y,
controlPoint2Transform.X, controlPoint2Transform.X,
controlPoint2Transform.Y, controlPoint2Transform.Y,
@@ -42,7 +42,7 @@
public void Run(IOperationContext operationContext) public void Run(IOperationContext operationContext)
{ {
var endPoint = operationContext.CurrentTransformationMatrix.Transform(new PdfPoint(X, Y)); var endPoint = operationContext.CurrentTransformationMatrix.Transform(new PdfPoint(X, Y));
operationContext.CurrentPath.LineTo(endPoint.X, endPoint.Y); operationContext.CurrentSubpath.LineTo(endPoint.X, endPoint.Y);
operationContext.CurrentPosition = endPoint; operationContext.CurrentPosition = endPoint;
} }
@@ -45,7 +45,7 @@
operationContext.BeginSubpath(); operationContext.BeginSubpath();
var pointTransform = operationContext.CurrentTransformationMatrix.Transform(point); var pointTransform = operationContext.CurrentTransformationMatrix.Transform(point);
operationContext.CurrentPosition = pointTransform; operationContext.CurrentPosition = pointTransform;
operationContext.CurrentPath.MoveTo(pointTransform.X, pointTransform.Y); operationContext.CurrentSubpath.MoveTo(pointTransform.X, pointTransform.Y);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -29,7 +29,7 @@
/// <inheritdoc /> /// <inheritdoc />
public void Run(IOperationContext operationContext) public void Run(IOperationContext operationContext)
{ {
operationContext.CurrentPath?.ClosePath(); operationContext.CurrentSubpath?.ClosePath();
} }
/// <inheritdoc /> /// <inheritdoc />