draw2dpdf

package
v0.0.0-...-d99e2b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2015 License: BSD-2-Clause Imports: 13 Imported by: 0

README

draw2d pdf

Package draw2dpdf provides a graphic context that can draw vector graphics and text on pdf file with the gofpdf package.

Quick Start

The following Go code generates a simple drawing and saves it to a pdf document:

// Initialize the graphic context on an RGBA image
dest := draw2dpdf.NewPdf("L", "mm", "A4")
gc := draw2dpdf.NewGraphicContext(dest)

// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)

// Draw a closed shape
gc.MoveTo(10, 10) // should always be called first for a new path
gc.LineTo(100, 50)
gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()

// Save to file
draw2dpdf.SaveToPdfFile("hello.pdf", dest)

There are more examples here: https://github.com/llgcode/draw2d/tree/master/samples

Alternative backends

  • Drawing on images is provided by the draw2d package.
  • Drawing on opengl is provided by the draw2dgl package.

Acknowledgments

The pdf backend uses https://github.com/jung-kurt/gofpdf

Documentation

Overview

Package draw2dpdf provides a graphic context that can draw vector graphics and text on pdf file with the gofpdf package.

Quick Start

The following Go code generates a simple drawing and saves it to a pdf document:

// Initialize the graphic context on an RGBA image
dest := draw2dpdf.NewPdf("L", "mm", "A4")
gc := draw2dpdf.NewGraphicContext(dest)

// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)

// Draw a closed shape
gc.MoveTo(10, 10) // should always be called first for a new path
gc.LineTo(100, 50)
gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()

// Save to file
draw2dpdf.SaveToPdfFile("hello.pdf", dest)

There are more examples here: https://github.com/ninjasphere/draw2d/tree/master/samples

Alternative backends

Drawing on images is provided by the draw2d package. Drawing on opengl is provided by the draw2dgl package.

Acknowledgments

The pdf backend uses https://github.com/jung-kurt/gofpdf

Index

Constants

View Source
const (
	// DPI of a pdf document is fixed at 72.
	DPI = 72
)

Variables

This section is empty.

Functions

func ConvertPath

func ConvertPath(path *draw2d.Path, pdf Vectorizer)

ConvertPath converts a paths to the pdf api

func NewPdf

func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf

NewPdf creates a new pdf document with the draw2d fontfolder, adds a page and set fill color to white.

func SaveToPdfFile

func SaveToPdfFile(filePath string, pdf *gofpdf.Fpdf) error

SaveToPdfFile creates and saves a pdf document to a file

Types

type GraphicContext

type GraphicContext struct {
	*draw2dbase.StackGraphicContext

	DPI int
	// contains filtered or unexported fields
}

GraphicContext implements the draw2d.GraphicContext interface It provides draw2d with a pdf backend (based on gofpdf)

func NewGraphicContext

func NewGraphicContext(pdf *gofpdf.Fpdf) *GraphicContext

NewGraphicContext creates a new pdf GraphicContext

func (*GraphicContext) Clear

func (gc *GraphicContext) Clear()

Clear draws a white rectangle over the whole page

func (*GraphicContext) ClearRect

func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int)

ClearRect draws a white rectangle over the specified area. Samples: line.

func (*GraphicContext) CreateStringPath

func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor float64)

CreateStringPath creates a path from the string s at x, y, and returns the string width.

func (*GraphicContext) DrawImage

func (gc *GraphicContext) DrawImage(image image.Image)

DrawImage draws an image as PNG TODO: add type (tp) as parameter to argument list?

func (*GraphicContext) Fill

func (gc *GraphicContext) Fill(paths ...*draw2d.Path)

Fill fills the paths with the color specified by SetFillColor

func (*GraphicContext) FillString

func (gc *GraphicContext) FillString(text string) (cursor float64)

FillString draws a string at 0, 0

func (*GraphicContext) FillStringAt

func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64)

FillStringAt draws a string at x, y

func (*GraphicContext) FillStroke

func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path)

FillStroke first fills the paths and than strokes them

func (*GraphicContext) GetDPI

func (gc *GraphicContext) GetDPI() int

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)

func (*GraphicContext) GetStringBounds

func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64)

GetStringBounds returns the approximate pixel bounds of the string s at x, y. The left edge of the em square of the first character of s and the baseline intersect at 0, 0 in the returned coordinates. Therefore the top and left coordinates may well be negative.

func (*GraphicContext) Restore

func (gc *GraphicContext) Restore()

Restore restores the current context stack (transformation, color,...). Restoring the font is not supported.

func (*GraphicContext) Rotate

func (gc *GraphicContext) Rotate(angle float64)

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 (*GraphicContext) Save

func (gc *GraphicContext) Save()

Save saves the current context stack (transformation, font, color,...).

func (*GraphicContext) Scale

func (gc *GraphicContext) Scale(sx, sy float64)

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 (*GraphicContext) SetDPI

func (gc *GraphicContext) SetDPI(dpi int)

SetDPI sets the DPI which influences the font size.

func (*GraphicContext) SetFillColor

func (gc *GraphicContext) SetFillColor(c color.Color)

SetFillColor sets the fill and text color

func (*GraphicContext) SetFont

func (gc *GraphicContext) SetFont(font *truetype.Font)

SetFont is unsupported by the pdf graphic context, use SetFontData instead.

func (*GraphicContext) SetFontData

func (gc *GraphicContext) SetFontData(fontData draw2d.FontData)

SetFontData sets the current font used to draw text. Always use this method, as SetFont is unsupported by the pdf graphic context. It is mandatory to call this method at least once before printing text or the resulting document will not be valid. 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 (*GraphicContext) SetFontSize

func (gc *GraphicContext) SetFontSize(fontSize float64)

SetFontSize sets the font size in points (as in “a 12 point font”). TODO: resolve this with ImgGraphicContext (now done with gc.Current.Scale)

func (*GraphicContext) SetLineCap

func (gc *GraphicContext) SetLineCap(Cap draw2d.LineCap)

SetLineCap sets the line cap (round, but or square)

func (*GraphicContext) SetLineDash

func (gc *GraphicContext) SetLineDash(Dash []float64, DashOffset float64)

SetLineDash sets the line dash pattern

func (*GraphicContext) SetLineJoin

func (gc *GraphicContext) SetLineJoin(Join draw2d.LineJoin)

SetLineJoin sets the line cap (round, bevel or miter)

func (*GraphicContext) SetLineWidth

func (gc *GraphicContext) SetLineWidth(LineWidth float64)

SetLineWidth sets the line width

func (*GraphicContext) SetStrokeColor

func (gc *GraphicContext) SetStrokeColor(c color.Color)

SetStrokeColor sets the stroke color

func (*GraphicContext) Stroke

func (gc *GraphicContext) Stroke(paths ...*draw2d.Path)

Stroke strokes the paths with the color specified by SetStrokeColor

func (*GraphicContext) StrokeString

func (gc *GraphicContext) StrokeString(text string) (cursor float64)

StrokeString draws a string at 0, 0 (stroking is unsupported, string will be filled)

func (*GraphicContext) StrokeStringAt

func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64)

StrokeStringAt draws a string at x, y (stroking is unsupported, string will be filled)

func (*GraphicContext) Translate

func (gc *GraphicContext) Translate(tx, ty float64)

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.

type Vectorizer

type Vectorizer interface {
	// MoveTo creates a new subpath that start at the specified point
	MoveTo(x, y float64)
	// LineTo adds a line to the current subpath
	LineTo(x, y float64)
	// CurveTo adds a quadratic bezier curve to the current subpath
	CurveTo(cx, cy, x, y float64)
	// CurveTo adds a cubic bezier curve to the current subpath
	CurveBezierCubicTo(cx1, cy1, cx2, cy2, x, y float64)
	// ArcTo adds an arc to the current subpath
	ArcTo(x, y, rx, ry, degRotate, degStart, degEnd float64)
	// ClosePath closes the subpath
	ClosePath()
}

Vectorizer defines the minimal interface for gofpdf.Fpdf to be passed to a PathConvertor. It is also implemented by for example VertexMatrixTransform

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL