diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index 2279ed7c..872ccb4a 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.55228474983079m; + + 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. ///