From c4a077bbbb0ab00a935e8329c628779814da7cfb Mon Sep 17 00:00:00 2001 From: BobLd Date: Thu, 2 Apr 2020 16:28:49 +0100 Subject: [PATCH] Add IsFilled and IsStroked flags Add CloneEmpty() and GetBoundingRectangle()methods --- src/UglyToad.PdfPig/Graphics/PdfPath.cs | 85 +++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/UglyToad.PdfPig/Graphics/PdfPath.cs b/src/UglyToad.PdfPig/Graphics/PdfPath.cs index 376f2d00..d5336527 100644 --- a/src/UglyToad.PdfPig/Graphics/PdfPath.cs +++ b/src/UglyToad.PdfPig/Graphics/PdfPath.cs @@ -1,6 +1,7 @@ namespace UglyToad.PdfPig.Graphics { using System.Collections.Generic; + using System.Linq; using UglyToad.PdfPig.Core; using UglyToad.PdfPig.Graphics.Colors; using UglyToad.PdfPig.Graphics.Core; @@ -21,11 +22,21 @@ /// public bool IsClipping { get; private set; } + /// + /// + /// + public bool IsFilled { get; private set; } + /// /// /// public IColor FillColor { get; set; } + /// + /// + /// + public bool IsStroked { get; private set; } + /// /// /// @@ -56,8 +67,82 @@ /// public void SetClipping(FillingRule fillingRule) { + IsFilled = false; + IsStroked = false; IsClipping = true; FillingRule = fillingRule; } + + /// + /// Set the filling rule for this path. + /// + public void SetFilled(FillingRule fillingRule) + { + IsFilled = true; + FillingRule = fillingRule; + } + + /// + /// + /// + public void SetStroked() + { + IsStroked = true; + } + + /// + /// Create a clone with no Subpaths. + /// + public PdfPath CloneEmpty() + { + PdfPath newPath = new PdfPath(); + if (IsClipping) + { + newPath.SetClipping(FillingRule); + } + else + { + if (IsFilled) + { + newPath.SetFilled(FillingRule); + newPath.FillColor = FillColor; + } + + if (IsStroked) + { + newPath.SetStroked(); + newPath.LineCapStyle = LineCapStyle; + newPath.LineDashPattern = LineDashPattern; + newPath.LineJoinStyle = LineJoinStyle; + newPath.LineWidth = LineWidth; + newPath.StrokeColor = StrokeColor; + } + } + return newPath; + } + + /// + /// + /// + /// + public PdfRectangle? GetBoundingRectangle() + { + if (this.Count == 0) + { + return null; + } + + var bboxes = this.Select(x => x.GetBoundingRectangle()).Where(x => x.HasValue).Select(x => x.Value).ToList(); + if (bboxes.Count == 0) + { + return null; + } + + var minX = bboxes.Min(x => x.Left); + var minY = bboxes.Min(x => x.Bottom); + var maxX = bboxes.Max(x => x.Right); + var maxY = bboxes.Max(x => x.Top); + return new PdfRectangle(minX, minY, maxX, maxY); + } } }