From 2905171a222373f6b4087bb2d74b97f0a1959046 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 27 Jun 2015 18:24:16 +0200 Subject: [PATCH] added a NewPdf constructor for convenience --- pdf2d/graphiccontext.go | 15 +++++++++----- pdf2d/samples/android/android.go | 26 ++++++++++-------------- pdf2d/samples/frame-image/frame-image.go | 6 ++---- pdf2d/samples/gopher/gopher.go | 4 +--- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pdf2d/graphiccontext.go b/pdf2d/graphiccontext.go index 2250015..cbd6b3b 100644 --- a/pdf2d/graphiccontext.go +++ b/pdf2d/graphiccontext.go @@ -6,7 +6,6 @@ package pdf2d import ( "bytes" - "fmt" "image" "image/color" "image/png" @@ -27,10 +26,6 @@ var ( draw2d.SquareCap: "square"} ) -func notImplemented(method string) { - fmt.Printf("%s: not implemented\n", method) -} - const c255 = 255.0 / 65535.0 var ( @@ -38,11 +33,21 @@ var ( white color.Color = color.RGBA{255, 255, 255, 255} ) +// NewPdf creates a new pdf document with the draw2d fontfolder and +// already a page added. +func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf { + pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder()) + pdf.AddPage() + return pdf +} + +// rgb converts a color (used by draw2d) into 3 int (used by gofpdf) func rgb(c color.Color) (int, int, int) { r, g, b, _ := c.RGBA() return int(float64(r) * c255), int(float64(g) * c255), int(float64(b) * c255) } +// clearRect draws a white rectangle func clearRect(gc *GraphicContext, x1, y1, x2, y2 float64) { // save state f := gc.Current.FillColor diff --git a/pdf2d/samples/android/android.go b/pdf2d/samples/android/android.go index 1af1769..600e080 100644 --- a/pdf2d/samples/android/android.go +++ b/pdf2d/samples/android/android.go @@ -5,20 +5,17 @@ package main import ( - "fmt" "image/color" "math" "github.com/stanim/draw2d" "github.com/stanim/draw2d/pdf2d" - "github.com/stanim/gofpdf" ) func main() { // Initialize the graphic context on a pdf document - pdf := gofpdf.New("P", "mm", "A4", "../font") - pdf.AddPage() - gc := pdf2d.NewGraphicContext(pdf) + dest := pdf2d.NewPdf("P", "mm", "A4") + gc := pdf2d.NewGraphicContext(dest) // set the fill and stroke color of the droid gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff}) @@ -28,14 +25,14 @@ func main() { DrawDroid(gc, 10, 10) // Save to pdf - pdf2d.SaveToPdfFile("android.pdf", pdf) + pdf2d.SaveToPdfFile("android.pdf", dest) } func DrawDroid(gc draw2d.GraphicContext, x, y float64) { gc.SetLineCap(draw2d.RoundCap) gc.SetLineWidth(5) - fmt.Println("\nhead") + // head gc.MoveTo(x+30, y+70) gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 180*(math.Pi/180)) gc.Close() @@ -46,34 +43,33 @@ func DrawDroid(gc draw2d.GraphicContext, x, y float64) { gc.LineTo(x+110, y+10) gc.Stroke() - fmt.Println("\nleft eye") + // left eye draw2d.Circle(gc, x+60, y+45, 5) gc.FillStroke() - fmt.Println("\nright eye") + // right eye draw2d.Circle(gc, x+100, y+45, 5) gc.FillStroke() - fmt.Println("\nbody") + // body draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) gc.FillStroke() draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80) gc.FillStroke() - fmt.Println("\nleft arm") + // left arm draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) gc.FillStroke() - fmt.Println("\nright arm") + // right arm draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) gc.FillStroke() - fmt.Println("\nleft leg") + // left leg draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) gc.FillStroke() - fmt.Println("\nright leg") + // right leg draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) gc.FillStroke() - } diff --git a/pdf2d/samples/frame-image/frame-image.go b/pdf2d/samples/frame-image/frame-image.go index a582de7..5956b6f 100644 --- a/pdf2d/samples/frame-image/frame-image.go +++ b/pdf2d/samples/frame-image/frame-image.go @@ -10,7 +10,6 @@ import ( "github.com/stanim/draw2d" "github.com/stanim/draw2d/pdf2d" - "github.com/stanim/gofpdf" ) func main() { @@ -19,9 +18,8 @@ func main() { // Line width od the frame const lineWidth = 3 - // Initialize the graphic context on an RGBA image - dest := gofpdf.New("P", "mm", "A4", "../font") - dest.AddPage() + // Initialize the graphic context on a pdf document + dest := pdf2d.NewPdf("P", "mm", "A3") gc := pdf2d.NewGraphicContext(dest) // Size of destination image dw, dh := dest.GetPageSize() diff --git a/pdf2d/samples/gopher/gopher.go b/pdf2d/samples/gopher/gopher.go index 39bff1b..cb6696c 100644 --- a/pdf2d/samples/gopher/gopher.go +++ b/pdf2d/samples/gopher/gopher.go @@ -9,13 +9,11 @@ import ( "github.com/stanim/draw2d" "github.com/stanim/draw2d/pdf2d" - "github.com/stanim/gofpdf" ) func main() { // Initialize the graphic context on an RGBA image - dest := gofpdf.New("P", "mm", "A3", "../font") - dest.AddPage() + dest := pdf2d.NewPdf("P", "mm", "A4") gc := pdf2d.NewGraphicContext(dest) // Draw a gopher