From 21dfa3e98584340565f014fa9bdf2dfaa9cf1e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Nowak?= Date: Wed, 8 Jun 2022 13:20:06 +0200 Subject: [PATCH 1/2] Add DrawTriangle, DrawCircle and DrawEllipsis --- src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs | 105 +++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index 2279ed7c..e37a9b06 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -180,6 +180,111 @@ } } + /// + /// Draws a triangle on the current page with the specified points and line width. + /// + /// Position of the first corner of the triangle. + /// Position of the second corner of the triangle. + /// Position of the third corner of the triangle. + /// The width of the line border of the triangle. + /// Whether to fill with the color set by . + public void DrawTriangle(PdfPoint point1, PdfPoint point2, PdfPoint point3, decimal lineWidth = 1, bool fill = false) + { + if (lineWidth != 1) + { + currentStream.Add(new SetLineWidth(lineWidth)); + } + + currentStream.Add(new BeginNewSubpath((decimal)point1.X, (decimal)point1.Y)); + currentStream.Add(new AppendStraightLineSegment((decimal)point2.X, (decimal)point2.Y)); + currentStream.Add(new AppendStraightLineSegment((decimal)point3.X, (decimal)point3.Y)); + currentStream.Add(new AppendStraightLineSegment((decimal)point1.X, (decimal)point1.Y)); + + if (fill) + { + currentStream.Add(FillPathEvenOddRuleAndStroke.Value); + } + else + { + currentStream.Add(StrokePath.Value); + } + + if (lineWidth != 1) + { + currentStream.Add(new SetLineWidth(lineWidth)); + } + } + + /// + /// Draws a circle on the current page centering at the specified point with the given diameter and line width. + /// + /// The center position of the circle. + /// The diameter of the circle. + /// The width of the line border of the circle. + /// Whether to fill with the color set by . + public void DrawCircle(PdfPoint center, decimal diameter, decimal lineWidth = 1, bool fill = false) + { + DrawEllipsis(center, diameter, diameter, lineWidth, fill); + } + + /// + /// Draws an ellipsis on the current page centering at the specified point with the given width, height and line width. + /// + /// The center position of the ellipsis. + /// The width of the ellipsis. + /// The height of the ellipsis. + /// The width of the line border of the ellipsis. + /// Whether to fill with the color set by . + public void DrawEllipsis(PdfPoint center, decimal width, decimal height, decimal lineWidth = 1, bool fill = false) + { + width /= 2; + height /= 2; + + // See here: https://spencermortensen.com/articles/bezier-circle/ + decimal cc = 0.5519150244935105707435627m; + + if (lineWidth != 1) + { + currentStream.Add(new SetLineWidth(lineWidth)); + } + + currentStream.Add(new BeginNewSubpath((decimal)center.X - width, (decimal)center.Y)); + currentStream.Add(new AppendDualControlPointBezierCurve( + (decimal)center.X - width, (decimal)center.Y + height * cc, + (decimal)center.X - width * cc, (decimal)center.Y + height, + (decimal)center.X, (decimal)center.Y + height + )); + currentStream.Add(new AppendDualControlPointBezierCurve( + (decimal)center.X + width * cc, (decimal)center.Y + height, + (decimal)center.X + width, (decimal)center.Y + height * cc, + (decimal)center.X + width, (decimal)center.Y + )); + currentStream.Add(new AppendDualControlPointBezierCurve( + (decimal)center.X + width, (decimal)center.Y - height * cc, + (decimal)center.X + width * cc, (decimal)center.Y - height, + (decimal)center.X, (decimal)center.Y - height + )); + currentStream.Add(new AppendDualControlPointBezierCurve( + (decimal)center.X - width * cc, (decimal)center.Y - height, + (decimal)center.X - width, (decimal)center.Y - height * cc, + (decimal)center.X - width, (decimal)center.Y + )); + + if (fill) + { + currentStream.Add(FillPathEvenOddRuleAndStroke.Value); + } + else + { + currentStream.Add(StrokePath.Value); + } + + if (lineWidth != 1) + { + currentStream.Add(new SetLineWidth(lineWidth)); + } + } + /// /// Sets the stroke color for any following operations to the RGB value. Use to reset. /// From ae83861c5fb85fa8f70119078a3ca161d032abaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Nowak?= Date: Thu, 9 Jun 2022 07:49:50 +0200 Subject: [PATCH 2/2] Correct value of cc in DrawEllipsis --- src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index e37a9b06..872ccb4a 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -241,7 +241,7 @@ height /= 2; // See here: https://spencermortensen.com/articles/bezier-circle/ - decimal cc = 0.5519150244935105707435627m; + decimal cc = 0.55228474983079m; if (lineWidth != 1) {