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"
|
2015-06-28 23:29:04 +00:00
|
|
|
"math"
|
2015-06-26 23:13:20 +00:00
|
|
|
"os"
|
2015-06-30 23:06:53 +00:00
|
|
|
"path/filepath"
|
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"
|
|
|
|
)
|
|
|
|
|
2015-06-30 23:06:53 +00:00
|
|
|
const (
|
|
|
|
c255 = 255.0 / 65535.0
|
|
|
|
DPI = 72
|
|
|
|
)
|
|
|
|
|
2015-06-26 23:13:20 +00:00
|
|
|
var (
|
|
|
|
caps = map[draw2d.Cap]string{
|
|
|
|
draw2d.RoundCap: "round",
|
|
|
|
draw2d.ButtCap: "butt",
|
|
|
|
draw2d.SquareCap: "square"}
|
2015-06-27 14:21:13 +00:00
|
|
|
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 {
|
2015-06-30 23:06:53 +00:00
|
|
|
gc := &GraphicContext{draw2d.NewStackGraphicContext(), pdf, DPI}
|
|
|
|
gc.SetDPI(DPI)
|
|
|
|
return gc
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2015-06-28 23:29:04 +00:00
|
|
|
x0, y0 := float64(bounds.Min.X), float64(bounds.Min.Y)
|
|
|
|
w, h := float64(bounds.Dx()), float64(bounds.Dy())
|
|
|
|
gc.pdf.Image(name, x0, y0, w, h, 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-30 23:06:53 +00:00
|
|
|
// recalc recalculates scale and bounds values from the font size, screen
|
|
|
|
// resolution and font metrics, and invalidates the glyph cache.
|
|
|
|
func (gc *GraphicContext) recalc() {
|
|
|
|
// TODO: resolve properly the font size for pdf and bitmap
|
|
|
|
gc.Current.Scale = 3 * float64(gc.DPI) / 72
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDPI sets the DPI which influences the font size.
|
2015-06-26 23:13:20 +00:00
|
|
|
func (gc *GraphicContext) SetDPI(dpi int) {
|
|
|
|
gc.DPI = dpi
|
2015-06-30 23:06:53 +00:00
|
|
|
gc.recalc()
|
2015-06-26 23:13:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 23:06:53 +00:00
|
|
|
// GetDPI returns the DPI which influences the font size.
|
|
|
|
// (Note that gofpdf uses a fixed dpi of 72:
|
|
|
|
// https://godoc.org/code.google.com/p/gofpdf#Fpdf.PointConvert)
|
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
|
|
|
_, _, w, h := gc.GetStringBounds(text)
|
2015-06-30 23:06:53 +00:00
|
|
|
margin := gc.pdf.GetCellMargin()
|
|
|
|
gc.pdf.MoveTo(x-margin, y+margin-0.82*h)
|
|
|
|
gc.pdf.CellFormat(w, h, text, "", 0, "BL", false, 0, "")
|
|
|
|
// gc.pdf.Cell(w, h, text)
|
2015-06-27 14:21:13 +00:00
|
|
|
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 18:06:06 +00:00
|
|
|
// StrokeString draws a string at 0, 0 (stroking is unsupported,
|
|
|
|
// string will be filled)
|
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 18:06:06 +00:00
|
|
|
// StrokeStringAt draws a string at x, y (stroking is unsupported,
|
|
|
|
// string will be filled)
|
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-28 23:29:04 +00:00
|
|
|
pathConverter := NewPathConverter(gc.pdf)
|
|
|
|
// pathConverter := NewPathConverter(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 17:41:34 +00:00
|
|
|
// SetFont is unsupported by the pdf graphic context, use SetFontData
|
|
|
|
// instead.
|
|
|
|
func (gc *GraphicContext) SetFont(font *truetype.Font) {
|
|
|
|
// TODO: what to do with this api conflict between draw2d and gofpdf?!
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFontData sets the current font used to draw text. Always use
|
|
|
|
// this method, as SetFont is unsupported by the pdf graphic context.
|
2015-06-27 14:21:13 +00:00
|
|
|
// It is mandatory to call this method at least once before printing
|
|
|
|
// text or the resulting document will not be valid.
|
2015-06-27 17:41:34 +00:00
|
|
|
// It is necessary to generate a font definition file first with the
|
|
|
|
// makefont utility. It is not necessary to call this function for the
|
|
|
|
// core PDF fonts (courier, helvetica, times, zapfdingbats).
|
|
|
|
// go get github.com/jung-kurt/gofpdf/makefont
|
|
|
|
// http://godoc.org/github.com/jung-kurt/gofpdf#Fpdf.AddFont
|
|
|
|
func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) {
|
2015-06-30 23:06:53 +00:00
|
|
|
// TODO: call Makefont embed if json file does not exist yet
|
2015-06-27 17:41:34 +00:00
|
|
|
gc.StackGraphicContext.SetFontData(fontData)
|
|
|
|
var style string
|
|
|
|
if fontData.Style&draw2d.FontStyleBold != 0 {
|
|
|
|
style += "B"
|
|
|
|
}
|
|
|
|
if fontData.Style&draw2d.FontStyleItalic != 0 {
|
|
|
|
style += "I"
|
|
|
|
}
|
|
|
|
fn := draw2d.FontFileName(fontData)
|
|
|
|
fn = fn[:len(fn)-4]
|
2015-06-30 23:06:53 +00:00
|
|
|
jfn := filepath.Join(draw2d.GetFontFolder(), fn+".json")
|
|
|
|
gc.pdf.AddFont(fn, style, jfn)
|
2015-06-27 14:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFontSize sets the font size in points (as in ``a 12 point font'').
|
2015-06-30 23:06:53 +00:00
|
|
|
// TODO: resolve this with ImgGraphicContext (now done with gc.scale)
|
2015-06-27 14:21:13 +00:00
|
|
|
func (gc *GraphicContext) SetFontSize(fontSize float64) {
|
|
|
|
gc.StackGraphicContext.SetFontSize(fontSize)
|
2015-06-30 23:06:53 +00:00
|
|
|
gc.recalc()
|
|
|
|
gc.pdf.SetFontSize(fontSize * gc.scale)
|
2015-06-27 14:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
}
|
2015-06-28 23:29:04 +00:00
|
|
|
|
|
|
|
// Transformations
|
|
|
|
|
|
|
|
// Scale generally scales the following text, drawings and images.
|
|
|
|
// sx and sy are the scaling factors for width and height.
|
|
|
|
// This must be placed between gc.Save() and gc.Restore(), otherwise
|
|
|
|
// the pdf is invalid.
|
|
|
|
func (gc *GraphicContext) Scale(sx, sy float64) {
|
|
|
|
gc.StackGraphicContext.Scale(sx, sy)
|
|
|
|
gc.pdf.TransformScale(sx*100, sy*100, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate rotates the following text, drawings and images.
|
|
|
|
// Angle is specified in radians and measured clockwise from the
|
|
|
|
// 3 o'clock position.
|
|
|
|
// This must be placed between gc.Save() and gc.Restore(), otherwise
|
|
|
|
// the pdf is invalid.
|
|
|
|
func (gc *GraphicContext) Rotate(angle float64) {
|
|
|
|
gc.StackGraphicContext.Rotate(angle)
|
|
|
|
gc.pdf.TransformRotate(-angle*180/math.Pi, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate moves the following text, drawings and images
|
|
|
|
// horizontally and vertically by the amounts specified by tx and ty.
|
|
|
|
// This must be placed between gc.Save() and gc.Restore(), otherwise
|
|
|
|
// the pdf is invalid.
|
|
|
|
func (gc *GraphicContext) Translate(tx, ty float64) {
|
|
|
|
gc.StackGraphicContext.Translate(tx, ty)
|
|
|
|
gc.pdf.TransformTranslate(tx, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves the current context stack
|
|
|
|
// (transformation, font, color,...).
|
|
|
|
func (gc *GraphicContext) Save() {
|
|
|
|
gc.StackGraphicContext.Save()
|
|
|
|
gc.pdf.TransformBegin()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores the current context stack
|
|
|
|
// (transformation, color,...). Restoring the font is not supported.
|
|
|
|
func (gc *GraphicContext) Restore() {
|
|
|
|
gc.pdf.TransformEnd()
|
|
|
|
gc.StackGraphicContext.Restore()
|
|
|
|
c := gc.Current
|
|
|
|
gc.SetFontSize(c.FontSize)
|
|
|
|
// gc.SetFontData(c.FontData) unsupported, causes bug (do not enable)
|
|
|
|
gc.SetLineWidth(c.LineWidth)
|
|
|
|
gc.SetStrokeColor(c.StrokeColor)
|
|
|
|
gc.SetFillColor(c.FillColor)
|
|
|
|
gc.SetFillRule(c.FillRule)
|
|
|
|
// gc.SetLineDash(c.Dash, c.DashOffset) // TODO
|
|
|
|
gc.SetLineCap(c.Cap)
|
|
|
|
// gc.SetLineJoin(c.Join) // TODO
|
|
|
|
// c.Path unsupported
|
|
|
|
// c.Font unsupported
|
|
|
|
}
|