added a NewPdf constructor for convenience

This commit is contained in:
Stani 2015-06-27 18:24:16 +02:00
parent 01150ac981
commit 2905171a22
4 changed files with 24 additions and 27 deletions

View file

@ -6,7 +6,6 @@ package pdf2d
import ( import (
"bytes" "bytes"
"fmt"
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
@ -27,10 +26,6 @@ var (
draw2d.SquareCap: "square"} draw2d.SquareCap: "square"}
) )
func notImplemented(method string) {
fmt.Printf("%s: not implemented\n", method)
}
const c255 = 255.0 / 65535.0 const c255 = 255.0 / 65535.0
var ( var (
@ -38,11 +33,21 @@ var (
white color.Color = color.RGBA{255, 255, 255, 255} 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) { func rgb(c color.Color) (int, int, int) {
r, g, b, _ := c.RGBA() r, g, b, _ := c.RGBA()
return int(float64(r) * c255), int(float64(g) * c255), int(float64(b) * c255) 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) { func clearRect(gc *GraphicContext, x1, y1, x2, y2 float64) {
// save state // save state
f := gc.Current.FillColor f := gc.Current.FillColor

View file

@ -5,20 +5,17 @@
package main package main
import ( import (
"fmt"
"image/color" "image/color"
"math" "math"
"github.com/stanim/draw2d" "github.com/stanim/draw2d"
"github.com/stanim/draw2d/pdf2d" "github.com/stanim/draw2d/pdf2d"
"github.com/stanim/gofpdf"
) )
func main() { func main() {
// Initialize the graphic context on a pdf document // Initialize the graphic context on a pdf document
pdf := gofpdf.New("P", "mm", "A4", "../font") dest := pdf2d.NewPdf("P", "mm", "A4")
pdf.AddPage() gc := pdf2d.NewGraphicContext(dest)
gc := pdf2d.NewGraphicContext(pdf)
// set the fill and stroke color of the droid // set the fill and stroke color of the droid
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff}) gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
@ -28,14 +25,14 @@ func main() {
DrawDroid(gc, 10, 10) DrawDroid(gc, 10, 10)
// Save to pdf // Save to pdf
pdf2d.SaveToPdfFile("android.pdf", pdf) pdf2d.SaveToPdfFile("android.pdf", dest)
} }
func DrawDroid(gc draw2d.GraphicContext, x, y float64) { func DrawDroid(gc draw2d.GraphicContext, x, y float64) {
gc.SetLineCap(draw2d.RoundCap) gc.SetLineCap(draw2d.RoundCap)
gc.SetLineWidth(5) gc.SetLineWidth(5)
fmt.Println("\nhead") // head
gc.MoveTo(x+30, y+70) gc.MoveTo(x+30, y+70)
gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 180*(math.Pi/180)) gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 180*(math.Pi/180))
gc.Close() gc.Close()
@ -46,34 +43,33 @@ func DrawDroid(gc draw2d.GraphicContext, x, y float64) {
gc.LineTo(x+110, y+10) gc.LineTo(x+110, y+10)
gc.Stroke() gc.Stroke()
fmt.Println("\nleft eye") // left eye
draw2d.Circle(gc, x+60, y+45, 5) draw2d.Circle(gc, x+60, y+45, 5)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nright eye") // right eye
draw2d.Circle(gc, x+100, y+45, 5) draw2d.Circle(gc, x+100, y+45, 5)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nbody") // body
draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10)
gc.FillStroke() gc.FillStroke()
draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80) draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nleft arm") // left arm
draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nright arm") // right arm
draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nleft leg") // left leg
draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10)
gc.FillStroke() gc.FillStroke()
fmt.Println("\nright leg") // right leg
draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10)
gc.FillStroke() gc.FillStroke()
} }

View file

@ -10,7 +10,6 @@ import (
"github.com/stanim/draw2d" "github.com/stanim/draw2d"
"github.com/stanim/draw2d/pdf2d" "github.com/stanim/draw2d/pdf2d"
"github.com/stanim/gofpdf"
) )
func main() { func main() {
@ -19,9 +18,8 @@ func main() {
// Line width od the frame // Line width od the frame
const lineWidth = 3 const lineWidth = 3
// Initialize the graphic context on an RGBA image // Initialize the graphic context on a pdf document
dest := gofpdf.New("P", "mm", "A4", "../font") dest := pdf2d.NewPdf("P", "mm", "A3")
dest.AddPage()
gc := pdf2d.NewGraphicContext(dest) gc := pdf2d.NewGraphicContext(dest)
// Size of destination image // Size of destination image
dw, dh := dest.GetPageSize() dw, dh := dest.GetPageSize()

View file

@ -9,13 +9,11 @@ import (
"github.com/stanim/draw2d" "github.com/stanim/draw2d"
"github.com/stanim/draw2d/pdf2d" "github.com/stanim/draw2d/pdf2d"
"github.com/stanim/gofpdf"
) )
func main() { func main() {
// Initialize the graphic context on an RGBA image // Initialize the graphic context on an RGBA image
dest := gofpdf.New("P", "mm", "A3", "../font") dest := pdf2d.NewPdf("P", "mm", "A4")
dest.AddPage()
gc := pdf2d.NewGraphicContext(dest) gc := pdf2d.NewGraphicContext(dest)
// Draw a gopher // Draw a gopher