From c89928d97624374ad4ee70eb8cc5995b2e442466 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Tue, 10 Dec 2019 13:20:57 +0000 Subject: [PATCH] remove inefficient approach to checking if content stream path has been added #99 --- .../Graphics/ContentStreamProcessor.cs | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs index b8c05427..c07af4ae 100644 --- a/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs +++ b/src/UglyToad.PdfPig/Graphics/ContentStreamProcessor.cs @@ -50,6 +50,7 @@ private Stack graphicsStack = new Stack(); private IFont activeExtendedGraphicsStateFont; private InlineImageBuilder inlineImageBuilder; + private bool currentPathAdded; /// /// A counter to track individual calls to operations used to determine if letters are likely to be @@ -215,20 +216,22 @@ ? currentState.CurrentNonStrokingColor : currentState.CurrentStrokingColor; - ShowGlyph(font, transformedGlyphBounds, + var letter = new Letter(unicode, transformedGlyphBounds, transformedPdfBounds.BottomLeft, transformedPdfBounds.BottomRight, transformedPdfBounds.Width, - unicode, fontSize, + font.Name.Data, color, pointSize, textSequence); + letters.Add(letter); + decimal tx, ty; if (font.IsVertical) { - var verticalFont = (IVerticalWritingSupported) font; + var verticalFont = (IVerticalWritingSupported)font; var displacement = verticalFont.GetDisplacementVector(code); tx = 0; ty = (displacement.Y * fontSize) + characterSpacing + wordSpacing; @@ -352,7 +355,7 @@ { formMatrix = TransformationMatrix.FromArray(formMatrixToken.Data.OfType().Select(x => x.Data).ToArray()); } - + // 2. Update current transformation matrix. var resultingTransformationMatrix = startState.CurrentTransformationMatrix.Multiply(formMatrix); @@ -378,12 +381,13 @@ public void BeginSubpath() { - if (CurrentPath != null && CurrentPath.Commands.Count > 0 && !paths.Contains(CurrentPath)) + if (CurrentPath != null && CurrentPath.Commands.Count > 0 && !currentPathAdded) { paths.Add(CurrentPath); } CurrentPath = new PdfPath(); + currentPathAdded = false; } public void StrokePath(bool close) @@ -395,6 +399,7 @@ else { paths.Add(CurrentPath); + currentPathAdded = true; } } @@ -407,6 +412,7 @@ else { paths.Add(CurrentPath); + currentPathAdded = true; } } @@ -415,6 +421,7 @@ CurrentPath.ClosePath(); paths.Add(CurrentPath); CurrentPath = null; + currentPathAdded = false; } public void SetNamedGraphicsState(NameToken stateName) @@ -501,28 +508,5 @@ TextMatrices.TextMatrix = newMatrix; } - - private void ShowGlyph(IFont font, PdfRectangle glyphRectangle, - PdfPoint startBaseLine, - PdfPoint endBaseLine, - decimal width, - string unicode, - decimal fontSize, - IColor color, - decimal pointSize, - int textSequence) - { - var letter = new Letter(unicode, glyphRectangle, - startBaseLine, - endBaseLine, - width, - fontSize, - font.Name.Data, - color, - pointSize, - textSequence); - - letters.Add(letter); - } } } \ No newline at end of file