2015-06-26 23:13:20 +00:00
|
|
|
// Copyright 2015 The draw2d Authors. All rights reserved.
|
|
|
|
// created: 26/06/2015 by Stani Michiels
|
2015-06-27 14:21:13 +00:00
|
|
|
// TODO: fonts, dpi
|
2015-06-26 23:13:20 +00:00
|
|
|
|
|
|
|
package pdf2d
|
|
|
|
|
|
|
|
import (
|
2015-06-27 14:21:13 +00:00
|
|
|
"bytes"
|
2015-06-26 23:13:20 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2015-06-27 15:46:03 +00:00
|
|
|
"image/png"
|
2015-06-26 23:13:20 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2015-06-27 14:21:13 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.google.com/p/freetype-go/freetype/truetype"
|
2015-06-26 23:13:20 +00:00
|
|
|
|
|
|
|
"github.com/stanim/draw2d"
|
|
|
|
"github.com/stanim/gofpdf"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
caps = map[draw2d.Cap]string{
|
|
|
|
draw2d.RoundCap: "round",
|
|
|
|
draw2d.ButtCap: "butt",
|
|
|
|
draw2d.SquareCap: "square"}
|
|
|
|
)
|
|
|
|
|
2015-06-27 11:50:43 +00:00
|
|
|
const c255 = 255.0 / 65535.0
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
var (
|
|
|
|
imageCount uint32
|
|
|
|
white color.Color = color.RGBA{255, 255, 255, 255}
|
|
|
|
)
|
|
|
|
|
2015-06-27 16:33:15 +00:00
|
|
|
// NewPdf creates a new pdf document with the draw2d fontfolder, adds
|
|
|
|
// a page and set fill color to white.
|
2015-06-27 16:24:16 +00:00
|
|
|
func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf {
|
|
|
|
pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder())
|
|
|
|
pdf.AddPage()
|
2015-06-27 16:33:15 +00:00
|
|
|
pdf.SetFillColor(255, 255, 255) // to be compatible with draw2d
|
2015-06-27 16:24:16 +00:00
|
|
|
return pdf
|
|
|
|
}
|
|
|
|
|
|
|
|
// rgb converts a color (used by draw2d) into 3 int (used by gofpdf)
|
2015-06-26 23:13:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-06-27 16:24:16 +00:00
|
|
|
// clearRect draws a white rectangle
|
2015-06-27 14:21:13 +00:00
|
|
|
func clearRect(gc *GraphicContext, x1, y1, x2, y2 float64) {
|
|
|
|
// save state
|
|
|
|
f := gc.Current.FillColor
|
|
|
|
x, y := gc.pdf.GetXY()
|
|
|
|
// cover page with white rectangle
|
|
|
|
gc.SetFillColor(white)
|
|
|
|
draw2d.Rect(gc, x1, y1, x2, y2)
|
|
|
|
gc.Fill()
|
|
|
|
// restore state
|
|
|
|
gc.SetFillColor(f)
|
|
|
|
gc.pdf.MoveTo(x, y)
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:13:20 +00:00
|
|
|
// GraphicContext implements the draw2d.GraphicContext interface
|
|
|
|
// It provides draw2d with a pdf backend (based on gofpdf)
|
|
|
|
type GraphicContext struct {
|
|
|
|
*draw2d.StackGraphicContext
|
|
|
|
pdf *gofpdf.Fpdf
|
|
|
|
DPI int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGraphicContext creates a new pdf GraphicContext
|
|
|
|
func NewGraphicContext(pdf *gofpdf.Fpdf) *GraphicContext {
|
|
|
|
dpi := 92
|
|
|
|
return &GraphicContext{draw2d.NewStackGraphicContext(), pdf, dpi}
|
|
|
|
}
|
|
|
|
|
2015-06-27 15:46:03 +00:00
|
|
|
// DrawImage draws an image as PNG
|
|
|
|
// TODO: add type (tp) as parameter to argument list?
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) DrawImage(image image.Image) {
|
2015-06-27 14:21:13 +00:00
|
|
|
name := strconv.Itoa(int(imageCount))
|
2015-06-27 15:46:03 +00:00
|
|
|
tp := "PNG" // "JPG", "JPEG", "PNG" and "GIF"
|
2015-06-27 14:21:13 +00:00
|
|
|
b := &bytes.Buffer{}
|
2015-06-27 15:46:03 +00:00
|
|
|
png.Encode(b, image)
|
2015-06-27 14:21:13 +00:00
|
|
|
gc.pdf.RegisterImageReader(name, tp, b)
|
2015-06-27 15:46:03 +00:00
|
|
|
bounds := image.Bounds()
|
|
|
|
//x0, y0, x1, y1 := float64(bounds.Min.X), float64(bounds.Min.Y), float64(bounds.Dx()), float64(bounds.Dy())
|
|
|
|
x0, y0, x1, y1 := float64(bounds.Min.X), float64(bounds.Min.Y), float64(bounds.Max.X), float64(bounds.Max.Y)
|
|
|
|
tr := gc.Current.Tr
|
|
|
|
tr.TransformRectangle(&x0, &y0, &x1, &y1)
|
|
|
|
gc.pdf.Image(name, x0, y0, x1-x0, y1-y0, false, tp, 0, "")
|
2015-06-27 14:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear draws a white rectangle over the whole page
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) Clear() {
|
2015-06-27 14:21:13 +00:00
|
|
|
width, height := gc.pdf.GetPageSize()
|
|
|
|
clearRect(gc, 0, 0, width, height)
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// ClearRect draws a white rectangle over the specified area
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int) {
|
2015-06-27 14:21:13 +00:00
|
|
|
clearRect(gc, float64(x1), float64(y1), float64(x2), float64(y2))
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// SetDPI is a dummy method to implement the GraphicContext interface
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetDPI(dpi int) {
|
|
|
|
gc.DPI = dpi
|
2015-06-27 14:21:13 +00:00
|
|
|
// gc.recalc()
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// GetDPI is a dummy method to implement the GraphicContext interface
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) GetDPI() int {
|
|
|
|
return gc.DPI
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
_, h := gc.pdf.GetFontSize()
|
|
|
|
return 0, 0, gc.pdf.GetStringWidth(s), h
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
gc.pdf.MoveTo(x, y)
|
|
|
|
_, _, w, h := gc.GetStringBounds(text)
|
|
|
|
gc.pdf.Cell(w, h, text)
|
|
|
|
return w
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// FillString draws a string at 0, 0
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) FillString(text string) (cursor float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
return gc.FillStringAt(text, 0, 0)
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// FillStringAt draws a string at x, y
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
return gc.CreateStringPath(text, x, y)
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// StrokeString draws a string at 0, 0
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) StrokeString(text string) (cursor float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
return gc.StrokeStringAt(text, 0, 0)
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// StrokeStringAt draws a string at x, y
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) {
|
2015-06-27 14:21:13 +00:00
|
|
|
return gc.CreateStringPath(text, x, y)
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// Stroke strokes the paths
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) {
|
|
|
|
gc.draw("D", paths...)
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// Fill strokes the paths
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) {
|
|
|
|
gc.draw("F", paths...)
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// FillStroke first fills the paths and than strokes them
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) {
|
|
|
|
gc.draw("FD", paths...)
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
var logger = log.New(os.Stdout, "", log.Lshortfile)
|
2015-06-26 23:13:20 +00:00
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// draw fills and/or strokes paths
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) draw(style string, paths ...*draw2d.PathStorage) {
|
|
|
|
paths = append(paths, gc.Current.Path)
|
2015-06-27 14:51:37 +00:00
|
|
|
pathConverter := NewPathConverter(NewVertexMatrixTransform(gc.Current.Tr, gc.pdf))
|
|
|
|
// pathConverter := NewPathConverter(NewVertexMatrixTransform(gc.Current.Tr,NewPathLogger(logger, gc.pdf)))
|
2015-06-26 23:13:20 +00:00
|
|
|
pathConverter.Convert(paths...)
|
|
|
|
if gc.Current.FillRule.UseNonZeroWinding() {
|
|
|
|
style += "*"
|
|
|
|
}
|
|
|
|
gc.pdf.DrawPath(style)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite StackGraphicContext methods
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// SetStrokeColor sets the stroke color
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetStrokeColor(c color.Color) {
|
|
|
|
gc.StackGraphicContext.SetStrokeColor(c)
|
|
|
|
gc.pdf.SetDrawColor(rgb(c))
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// SetFillColor sets the fill color
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetFillColor(c color.Color) {
|
|
|
|
gc.StackGraphicContext.SetFillColor(c)
|
|
|
|
gc.pdf.SetFillColor(rgb(c))
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// SetFont sets the font used to draw text.
|
|
|
|
// It is mandatory to call this method at least once before printing
|
|
|
|
// text or the resulting document will not be valid.
|
|
|
|
func (gc *GraphicContext) SetFont(font *truetype.Font) {
|
|
|
|
// TODO: this api conflict needs to be fixed
|
|
|
|
gc.pdf.SetFont("Helvetica", "", 12)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFontSize sets the font size in points (as in ``a 12 point font'').
|
|
|
|
func (gc *GraphicContext) SetFontSize(fontSize float64) {
|
|
|
|
gc.StackGraphicContext.SetFontSize(fontSize)
|
|
|
|
gc.pdf.SetFontSize(fontSize)
|
|
|
|
//gc.recalc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLineWidth sets the line width
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetLineWidth(LineWidth float64) {
|
|
|
|
gc.StackGraphicContext.SetLineWidth(LineWidth)
|
|
|
|
gc.pdf.SetLineWidth(LineWidth)
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// SetLineCap sets the line cap (round, but or square)
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetLineCap(Cap draw2d.Cap) {
|
|
|
|
gc.StackGraphicContext.SetLineCap(Cap)
|
|
|
|
gc.pdf.SetLineCapStyle(caps[Cap])
|
|
|
|
}
|