diff options
author | Kurt Jung <kurt.w.jung@gmail.com> | 2015-07-04 14:29:05 -0400 |
---|---|---|
committer | Kurt Jung <kurt.w.jung@gmail.com> | 2015-07-04 14:29:05 -0400 |
commit | 530dba8469726219691bbb3d72c283d3bb233922 (patch) | |
tree | 0a910b223f8bc1e82e88ab0fd182c610aea3d78f | |
parent | fc0728dfdeebace8a2ea23d62fa05fa2cbadbe98 (diff) | |
parent | ae8e6c604c3b6fdd21328cf7c58c5f28093f9cd3 (diff) |
Multi-segment path drawing routines by stanim
-rw-r--r-- | fpdf.go | 71 | ||||
-rw-r--r-- | fpdf_test.go | 20 |
2 files changed, 90 insertions, 1 deletions
@@ -3462,3 +3462,74 @@ func (f *Fpdf) enddoc() { f.state = 3 return } + +// Path Drawing +// +// Create a "path" by moving a virtual stylus around the page, then draw it or +// fill it in. The main advantage of using the path drawing routines rather +// than multiple Fpdf.Line is that PDF creates nice line joins at the angles, +// rather than just overlaying the lines. + +// MoveTo moves the stylus to (x, y) without drawing the path from the previous +// point. Paths must start with a MoveTo to set the original stylus location or +// the result is undefined. +// +// See tutorial 30 for an example of this function. +func (f *Fpdf) MoveTo(x, y float64) { + f.point(x, y) // rename? +} + +// LineTo creates a line from the current stylus location to (x, y), which +// becomes the new stylus location. Note that this only creates the line in +// the path; it does not actually draw the line on the page. +// +// See tutorial 30 for an example of this function. +func (f *Fpdf) LineTo(x, y float64) { + f.outf("%.2f %.2f l", x*f.k, (f.h-y)*f.k) +} + +// CurveTo creates a single-segment quadratic Bézier curve. The curve starts at +// the current stylus location and ends at the point (x, y). The control point +// (cx, cy) specifies the curvature. At the start point, the curve is tangent +// to the straight line between the current stylus location and the control +// point. At the end point, the curve is tangent to the straight line between +// the end point and the control point. +// +// See tutorial 30 for an example of this function. +func (f *Fpdf) CurveTo(cx, cy, x, y float64) { + f.outf("%.5f %.5f %.5f %.5f v", cx*f.k, (f.h-cy)*f.k, x*f.k, (f.h-y)*f.k) +} + +// CurveBezierCubicTo creates a single-segment cubic Bézier curve. The curve +// starts at the current stylus location and ends at the point (x, y). The +// control points (cx0, cy0) and (cx1, cy1) specify the curvature. At the +// current stylus, the curve is tangent to the straight line between the +// current stylus location and the control point (cx0, cy0). At the end point, +// the curve is tangent to the straight line between the end point and the +// control point (cx1, cy1). +// +// See tutorial 30 for examples of this function. +func (f *Fpdf) CurveBezierCubicTo(cx0, cy0, cx1, cy1, x, y float64) { + f.curve(cx0, cy0, cx1, cy1, x, y) // rename? +} + +// ClosePath creates a line from the current location to the last MoveTo point +// (if not the same) and mark the path as closed so the first and last lines +// join nicely. +// +// See tutorial 30 for an example of this function. +func (f *Fpdf) ClosePath() { + f.outf("h") +} + +// DrawPath actually draws the path on the page. +// +// styleStr can be "F" for filled, "D" for outlined only, or "DF" or "FD" for +// outlined and filled. An empty string will be replaced with "D". Drawing uses +// the current draw color, line width, and cap style centered on the +// path. Filling uses the current fill color. +// +// See tutorial 30 for an example of this function. +func (f *Fpdf) DrawPath(styleStr string) { + f.outf(fillDrawOp(styleStr)) +} diff --git a/fpdf_test.go b/fpdf_test.go index 3439f91..8959ece 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -1451,10 +1451,28 @@ func ExampleFpdf_tutorial29() { pdf.AddFont("Calligrapher", "", "calligra.json") pdf.AddPage() pdf.SetFont("Calligrapher", "", 35) - pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") + pdf.Cell(0, 10, "Load fonts from any source") pdf.OutputAndClose(docWriter(pdf, 29)) // Output: // Generalized font loader reading calligra.json // Generalized font loader reading calligra.z // Successfully generated pdf/tutorial29.pdf } + +// This example demonstrates the Path Drawing functions, such as: MoveTo, +// LineTo, CurveTo, ..., ClosePath and DrawPath. +func ExampleFpdf_tutorial30() { + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.MoveTo(20, 20) + pdf.LineTo(190, 20) + pdf.CurveTo(190, 100, 105, 100) + pdf.CurveBezierCubicTo(20, 100, 105, 200, 20, 200) + pdf.ClosePath() + pdf.SetFillColor(200, 200, 200) + pdf.SetLineWidth(3) + pdf.DrawPath("DF") + pdf.OutputAndClose(docWriter(pdf, 30)) + // Output: + // Successfully generated pdf/tutorial30.pdf +} |