From 875b1c7a15f289f388a0187781c5f2b1f4e9dc90 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sun, 29 Jun 2014 14:14:19 -0400 Subject: Polygon function and demonstration --- doc.go | 3 ++- fpdf.go | 25 +++++++++++++++++++++++++ fpdf_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/doc.go b/doc.go index 5399b02..d62a20d 100644 --- a/doc.go +++ b/doc.go @@ -66,7 +66,8 @@ FPDF product. Lawrence Kesteloot provided code to allow an image's extent to be determined prior to placement. Support for vertical alignment within a cell was provided by Stefan Schroeder. Ivan Daniluk generalized the font and image loading code to use the Reader interface while maintaining backward -compatibility. Bruno Michel has provided valuable assistance with the code. +compatibility. Anthony Starks provided code for the Polygon function. Bruno +Michel has provided valuable assistance with the code. The FPDF website is http://www.fpdf.org/. diff --git a/fpdf.go b/fpdf.go index 4c6fa81..4a43fe4 100644 --- a/fpdf.go +++ b/fpdf.go @@ -798,6 +798,31 @@ func (f *Fpdf) Ellipse(x, y, rx, ry, degRotate float64, styleStr string) { f.Arc(x, y, rx, ry, degRotate, 0, 360, styleStr) } +// Polygon draws a closed figure defined by a series of vertices specified by +// points. The x and y fields of the points use the units established in New(). +// The last point in the slice will be implicitly joined to the first to close +// the polygon. +// +// 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 and line width centered on the ellipse's perimeter. +// Filling uses the current fill color. +// +// See tutorial 25 for an example of this function. +func (f *Fpdf) Polygon(points []PointType, styleStr string) { + if len(points) > 2 { + for j, pt := range points { + if j == 0 { + f.point(pt.X, pt.Y) + } else { + f.outf("%.5f %.5f l ", pt.X*f.k, (f.h-pt.Y)*f.k) + } + } + f.outf("%.5f %.5f l ", points[0].X*f.k, (f.h-points[0].Y)*f.k) + f.outf(fillDrawOp(styleStr)) + } +} + // Outputs current point func (f *Fpdf) point(x, y float64) { f.outf("%.2f %.2f m", x*f.k, (f.h-y)*f.k) diff --git a/fpdf_test.go b/fpdf_test.go index c727087..262e482 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -21,6 +21,7 @@ import ( "code.google.com/p/gofpdf" "fmt" "io/ioutil" + "math" "os" "path/filepath" "strings" @@ -1197,3 +1198,50 @@ func ExampleFpdf_tutorial24() { // Output: // Successfully generated pdf/tutorial24.pdf } + +// This example displays equilateral polygons in a demonstration of the Polygon +// function. +func ExampleFpdf_tutorial25() { + const rowCount = 5 + const colCount = 4 + const ptSize = 36 + var x, y, radius, gap, advance float64 + var rgVal int + var pts []gofpdf.PointType + vertices := func(count int) (res []gofpdf.PointType) { + var pt gofpdf.PointType + res = make([]gofpdf.PointType, 0, count) + mlt := 2.0 * math.Pi / float64(count) + for j := 0; j < count; j++ { + pt.Y, pt.X = math.Sincos(float64(j) * mlt) + res = append(res, gofpdf.PointType{x + radius*pt.X, y + radius*pt.Y}) + } + return + } + pdf := gofpdf.New("P", "mm", "A4", cnFontDir) // A4 210.0 x 297.0 + pdf.AddPage() + pdf.SetFont("Helvetica", "", ptSize) + pdf.SetDrawColor(0, 80, 180) + gap = 12.0 + pdf.SetY(gap) + pdf.CellFormat(190.0, gap, "Equilateral polygons", "", 1, "C", false, 0, "") + radius = (210.0 - float64(colCount+1)*gap) / (2.0 * float64(colCount)) + advance = gap + 2.0*radius + y = 2*gap + pdf.PointConvert(ptSize) + radius + rgVal = 230 + for row := 0; row < rowCount; row++ { + pdf.SetFillColor(rgVal, rgVal, 0) + rgVal -= 12 + x = gap + radius + for col := 0; col < colCount; col++ { + pts = vertices(row*colCount + col + 3) + // pdf.Circle(x, y, radius, "FD") + pdf.Polygon(pts, "FD") + x += advance + } + y += advance + } + pdf.OutputAndClose(docWriter(pdf, 25)) + // Output: + // Successfully generated pdf/tutorial25.pdf +} -- cgit v1.2.1-24-ge1ad