From 43b40da5d52ebff6e6aa8e2152354441273d9694 Mon Sep 17 00:00:00 2001 From: BobLd Date: Thu, 2 Apr 2020 16:39:48 +0100 Subject: [PATCH] Change Subpath to path where necessary --- .../Export/AltoXmlTextExporter.cs | 3 +- .../Export/HOcrTextExporter.cs | 5 +- .../Export/PageXmlTextExporter.cs | 3 +- .../Graphics/TestOperationContext.cs | 6 ++- src/UglyToad.PdfPig/Content/Page.cs | 5 +- src/UglyToad.PdfPig/Content/PageContent.cs | 4 +- .../Graphics/ContentStreamProcessor.cs | 52 +++---------------- .../Graphics/IOperationContext.cs | 8 ++- .../AppendDualControlPointBezierCurve.cs | 2 +- .../AppendEndControlPointBezierCurve.cs | 2 +- .../PathConstruction/AppendRectangle.cs | 2 +- .../AppendStartControlPointBezierCurve.cs | 2 +- .../AppendStraightLineSegment.cs | 2 +- .../PathConstruction/BeginNewSubpath.cs | 2 +- .../PathConstruction/CloseSubpath.cs | 2 +- 15 files changed, 37 insertions(+), 63 deletions(-) diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs index 46939fa5..5a97a463 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/AltoXmlTextExporter.cs @@ -10,6 +10,7 @@ using System.Xml; using System.Xml.Serialization; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; + using UglyToad.PdfPig.Graphics; using Util; /// @@ -159,7 +160,7 @@ return altoPage; } - private AltoDocument.AltoGraphicalElement ToAltoGraphicalElement(PdfSubpath pdfPath, double height) + private AltoDocument.AltoGraphicalElement ToAltoGraphicalElement(PdfPath pdfPath, double height) { graphicalElementCount++; diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs index fca4b720..4f77dfc2 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/HOcrTextExporter.cs @@ -6,6 +6,7 @@ using System; using System.Linq; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; + using UglyToad.PdfPig.Graphics; using Util; /// @@ -176,7 +177,7 @@ /// /// /// The indent level. - 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; @@ -190,7 +191,7 @@ areaCount++; hocr += GetIndent(level) + @"
\n"; - foreach (var subPath in path.Commands) + foreach (var subPath in path) { var subBbox = subPath.GetBoundingRectangle(); if (subBbox.HasValue) diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs index 52ed18a2..3b71c2e5 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Export/PageXmlTextExporter.cs @@ -12,6 +12,7 @@ using System.Xml.Serialization; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.DocumentLayoutAnalysis.ReadingOrderDetector; + using UglyToad.PdfPig.Graphics; using Util; /// @@ -199,7 +200,7 @@ return pageXmlPage; } - private PageXmlDocument.PageXmlLineDrawingRegion ToPageXmlLineDrawingRegion(PdfSubpath pdfPath, double height) + private PageXmlDocument.PageXmlLineDrawingRegion ToPageXmlLineDrawingRegion(PdfPath pdfPath, double height) { var bbox = pdfPath.GetBoundingRectangle(); if (bbox.HasValue) diff --git a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs index 036427bb..003e9a47 100644 --- a/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs +++ b/src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs @@ -20,7 +20,9 @@ public TransformationMatrix CurrentTransformationMatrix => GetCurrentState().CurrentTransformationMatrix; - public PdfSubpath CurrentPath { get; set; } + public PdfSubpath CurrentSubpath { get; set; } + + public PdfPath CurrentPath { get; set; } public IColorSpaceContext ColorSpaceContext { get; } @@ -29,7 +31,7 @@ public TestOperationContext() { StateStack.Push(new CurrentGraphicsState()); - CurrentPath = new PdfSubpath(); + CurrentSubpath = new PdfSubpath(); ColorSpaceContext = new ColorSpaceContext(GetCurrentState, new ResourceStore(new TestPdfTokenScanner(), new TestFontFactory())); } diff --git a/src/UglyToad.PdfPig/Content/Page.cs b/src/UglyToad.PdfPig/Content/Page.cs index 6ce2166d..0aaf17c5 100644 --- a/src/UglyToad.PdfPig/Content/Page.cs +++ b/src/UglyToad.PdfPig/Content/Page.cs @@ -10,6 +10,7 @@ using Util; using Util.JetBrains.Annotations; using Tokenization.Scanner; + using UglyToad.PdfPig.Graphics; /// /// Contains the content and provides access to methods of a single page in the . @@ -172,9 +173,9 @@ private readonly AnnotationProvider annotationProvider; /// - /// The set of s drawn by the PDF content. + /// The set of s drawn by the PDF content. /// - public IReadOnlyList Paths => page.Content?.Paths ?? new List(); + public IReadOnlyList Paths => page.Content?.Paths ?? new List(); internal Experimental(Page page, AnnotationProvider annotationProvider) { diff --git a/src/UglyToad.PdfPig/Content/PageContent.cs b/src/UglyToad.PdfPig/Content/PageContent.cs index 3888ab10..55ddad9f 100644 --- a/src/UglyToad.PdfPig/Content/PageContent.cs +++ b/src/UglyToad.PdfPig/Content/PageContent.cs @@ -28,12 +28,12 @@ public IReadOnlyList Letters { get; } - public IReadOnlyList Paths { get; } + public IReadOnlyList Paths { get; } public int NumberOfImages => images.Count; internal PageContent(IReadOnlyList graphicsStateOperations, IReadOnlyList letters, - IReadOnlyList paths, + IReadOnlyList paths, IReadOnlyList> images, IReadOnlyList markedContents, IPdfTokenScanner pdfScanner, diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index 2b095e4c..7bb3df6f 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -28,7 +28,7 @@ /// /// Stores each path as it is encountered in the content stream. /// - private readonly List paths = new List(); + private readonly List paths = new List(); /// /// Stores a link to each image (either inline or XObject) as it is encountered in the content stream. @@ -52,7 +52,6 @@ private Stack graphicsStack = new Stack(); private IFont activeExtendedGraphicsStateFont; private InlineImageBuilder inlineImageBuilder; - private bool currentPathAdded; private int pageNumber; /// @@ -66,7 +65,9 @@ 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; } @@ -398,61 +399,22 @@ 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) { - if (CurrentPath == null) - { - return; - } - if (close) - { - ClosePath(); - } - else - { - paths.Add(CurrentPath); - markedContentStack.AddPath(CurrentPath); - currentPathAdded = true; - } } public void FillPath(bool close) { - if (CurrentPath == null) - { - return; - } - if (close) - { - ClosePath(); - } - else - { - paths.Add(CurrentPath); - markedContentStack.AddPath(CurrentPath); - currentPathAdded = true; - } } public void ClosePath() { - CurrentPath.ClosePath(); - paths.Add(CurrentPath); - markedContentStack.AddPath(CurrentPath); - CurrentPath = null; - currentPathAdded = false; + } public void SetNamedGraphicsState(NameToken stateName) @@ -557,7 +519,7 @@ public void ModifyClippingIntersect(FillingRule clippingRule) { - if (CurrentPath == null) + if (CurrentSubpath == null) { return; } diff --git a/src/UglyToad.PdfPig/Graphics/IOperationContext.cs b/src/UglyToad.PdfPig/Graphics/IOperationContext.cs index 76f2d397..64eb1744 100644 --- a/src/UglyToad.PdfPig/Graphics/IOperationContext.cs +++ b/src/UglyToad.PdfPig/Graphics/IOperationContext.cs @@ -10,11 +10,17 @@ /// public interface IOperationContext { + /// + /// The current subpath being drawn if applicable. + /// + [CanBeNull] + PdfSubpath CurrentSubpath { get; } + /// /// The current path being drawn if applicable. /// [CanBeNull] - PdfSubpath CurrentPath { get; } + PdfPath CurrentPath { get; } /// /// The active colorspaces for this content stream. diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs index 6b338a92..a6c3b33d 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendDualControlPointBezierCurve.cs @@ -78,7 +78,7 @@ var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1); var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); - operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, + operationContext.CurrentSubpath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, controlPoint2Transform.X, controlPoint2Transform.Y, endTransform.X, endTransform.Y); operationContext.CurrentPosition = endTransform; diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs index ee799a36..5d46ae26 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendEndControlPointBezierCurve.cs @@ -60,7 +60,7 @@ var end = new PdfPoint(X3, Y3); var controlPoint1Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint1); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); - operationContext.CurrentPath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, + operationContext.CurrentSubpath.BezierCurveTo(controlPoint1Transform.X, controlPoint1Transform.Y, endTransform.X, endTransform.Y, endTransform.X, diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs index 5a88bcd9..2aa72307 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendRectangle.cs @@ -63,7 +63,7 @@ var lowerLeftTransform = operationContext.CurrentTransformationMatrix.Transform(lowerLeft); 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); } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs index 86f7752d..e334d167 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStartControlPointBezierCurve.cs @@ -60,7 +60,7 @@ var end = new PdfPoint(X3, Y3); var controlPoint2Transform = operationContext.CurrentTransformationMatrix.Transform(controlPoint2); var endTransform = operationContext.CurrentTransformationMatrix.Transform(end); - operationContext.CurrentPath.BezierCurveTo(operationContext.CurrentPosition.X, + operationContext.CurrentSubpath.BezierCurveTo(operationContext.CurrentPosition.X, operationContext.CurrentPosition.Y, controlPoint2Transform.X, controlPoint2Transform.Y, diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs index 24f2cf22..93a9c201 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/AppendStraightLineSegment.cs @@ -42,7 +42,7 @@ public void Run(IOperationContext operationContext) { 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; } diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs index 70db1112..0a4a373f 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/BeginNewSubpath.cs @@ -45,7 +45,7 @@ operationContext.BeginSubpath(); var pointTransform = operationContext.CurrentTransformationMatrix.Transform(point); operationContext.CurrentPosition = pointTransform; - operationContext.CurrentPath.MoveTo(pointTransform.X, pointTransform.Y); + operationContext.CurrentSubpath.MoveTo(pointTransform.X, pointTransform.Y); } /// diff --git a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/CloseSubpath.cs b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/CloseSubpath.cs index 795eefda..7c34db20 100644 --- a/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/CloseSubpath.cs +++ b/src/UglyToad.PdfPig/Graphics/Operations/PathConstruction/CloseSubpath.cs @@ -29,7 +29,7 @@ /// public void Run(IOperationContext operationContext) { - operationContext.CurrentPath?.ClosePath(); + operationContext.CurrentSubpath?.ClosePath(); } ///