{"modulePath":"github.com/llgcode/draw2d","version":"v0.0.0-20260422081035-c4331ac66734","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package draw2d\n\nPackage draw2d is a pure go 2D vector graphics library with support for multiple output devices such as images (draw2d), pdf documents (draw2dpdf) and opengl (draw2dgl), which can also be used on the google app engine. It can be used as a pure go Cairo alternative. draw2d is released under the BSD license.\n\n### Features {#hdr-Features}\n\nOperations in draw2d include stroking and filling polygons, arcs, Bézier curves, drawing images and text rendering with truetype fonts. All drawing operations can be transformed by affine transformations (scale, rotation, translation).\n\nPackage draw2d follows the conventions of [http://www.w3.org/TR/2dcontext](http://www.w3.org/TR/2dcontext) for coordinate system, angles, etc...\n\n### Installation {#hdr-Installation}\n\nTo install or update the package draw2d on your system, run:\n\n\tgo get -u github.com/llgcode/draw2d\n\n### Quick Start {#hdr-Quick_Start}\n\nPackage draw2d itself provides a graphic context that can draw vector graphics and text on an image canvas. The following Go code generates a simple drawing and saves it to an image file:\n\n\tpackage main\n\n\timport (\n\t\t\"github.com/llgcode/draw2d/draw2dimg\"\n\t\t\"image\"\n\t\t\"image/color\"\n\t)\n\n\tfunc main() {\n\t\t// Initialize the graphic context on an RGBA image\n\t\tdest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))\n\t\tgc := draw2dimg.NewGraphicContext(dest)\n\n\t\t// Set some properties\n\t\tgc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})\n\t\tgc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})\n\t\tgc.SetLineWidth(5)\n\n\t\t// Draw a closed shape\n\t\tgc.MoveTo(10, 10) // should always be called first for a new path\n\t\tgc.LineTo(100, 50)\n\t\tgc.QuadCurveTo(100, 10, 10, 10)\n\t\tgc.Close()\n\t\tgc.FillStroke()\n\n\t\t// Save to file\n\t\tdraw2dimg.SaveToPngFile(\"hello.png\", dest)\n\t}\n\nThere are more examples here: [https://github.com/llgcode/draw2d/tree/master/samples](https://github.com/llgcode/draw2d/tree/master/samples)\n\nDrawing on pdf documents is provided by the draw2dpdf package. Drawing on opengl is provided by the draw2dgl package. See subdirectories at the bottom of this page.\n\n### Testing {#hdr-Testing}\n\nThe samples are run as tests from the root package folder \\`draw2d\\` by:\n\n\tgo test ./...\n\nOr if you want to run with test coverage:\n\n\tgo test -cover ./... | grep -v \"no test\"\n\nThis will generate output by the different backends in the output folder.\n\n### Acknowledgments {#hdr-Acknowledgments}\n\nLaurent Le Goff wrote this library, inspired by Postscript and HTML5 canvas. He implemented the image and opengl backend with the freetype-go package. Also he created a pure go Postscript interpreter, which can read postscript images and draw to a draw2d graphic context ([https://github.com/llgcode/ps](https://github.com/llgcode/ps)). Stani Michiels implemented the pdf backend with the gofpdf package.\n\n### Packages using draw2d {#hdr-Packages_using_draw2d}\n\n\\- [https://github.com/llgcode/ps](https://github.com/llgcode/ps): Postscript interpreter written in Go\n\n\\- [https://github.com/gonum/plot](https://github.com/gonum/plot): drawing plots in Go\n\n\\- [https://github.com/muesli/smartcrop](https://github.com/muesli/smartcrop): content aware image cropping\n\n\\- [https://github.com/peterhellberg/karta](https://github.com/peterhellberg/karta): drawing Voronoi diagrams\n\n\\- [https://github.com/vdobler/chart](https://github.com/vdobler/chart): basic charts in Go\n\n## Constants\n\n```go\nconst (\n\t// HalignLeft Horizontally align to left\n\tHalignLeft = iota\n\t// HalignCenter Horizontally align to center\n\tHalignCenter\n\t// HalignRight Horizontally align to right\n\tHalignRight\n)\n```\n\n## Functions\n\n```go\nfunc FontFileName(fontData FontData) string\n```\n\n```go\nfunc GetFont(fontData FontData) (font *truetype.Font)\n```\n\n```go\nfunc GetFontFolder() string\n```\n\n```go\nfunc RegisterFont(fontData FontData, font *truetype.Font)\n```\n\n```go\nfunc SetFontCache(cache FontCache)\n```\nChanges the font cache backend used by the package. After calling this functionSetFontFolder and SetFontNamer will not affect anymore how fonts are loaded. To restore the default font cache, call this function passing nil as argument.\n\n```go\nfunc SetFontFolder(folder string)\n```\n\n```go\nfunc SetFontNamer(fn FontFileNamer)\n```\n\n## Types\n\n```go\ntype FillRule int\n```\nFillRule defines the type for fill rules\n\n```go\nconst (\n\t// FillRuleEvenOdd determines the \"insideness\" of a point in the shape\n\t// by drawing a ray from that point to infinity in any direction\n\t// and counting the number of path segments from the given shape that the ray crosses.\n\t// If this number is odd, the point is inside; if even, the point is outside.\n\tFillRuleEvenOdd FillRule = iota\n\t// FillRuleWinding determines the \"insideness\" of a point in the shape\n\t// by drawing a ray from that point to infinity in any direction\n\t// and then examining the places where a segment of the shape crosses the ray.\n\t// Starting with a count of zero, add one each time a path segment crosses\n\t// the ray from left to right and subtract one each time\n\t// a path segment crosses the ray from right to left. After counting the crossings,\n\t// if the result is zero then the point is outside the path. Otherwise, it is inside.\n\tFillRuleWinding\n)\n```\n\n```go\ntype FolderFontCache struct {\n\t// contains filtered or unexported fields\n}\n```\nFolderFontCache can Load font from folder\n\n```go\nfunc NewFolderFontCache(folder string) *FolderFontCache\n```\nNewFolderFontCache creates FolderFontCache\n\n```go\nfunc (cache *FolderFontCache) Load(fontData FontData) (font *truetype.Font, err error)\n```\nLoad a font from cache if exists otherwise it will load the font from file\n\n```go\nfunc (cache *FolderFontCache) Store(fontData FontData, font *truetype.Font)\n```\nStore a font to this cache\n\n```go\ntype FontCache interface {\n\t// Loads a truetype font represented by the FontData object passed as\n\t// argument.\n\t// The method returns an error if the font could not be loaded, either\n\t// because it didn't exist or the resource it was loaded from was corrupted.\n\tLoad(FontData) (*truetype.Font, error)\n\n\t// Sets the truetype font that will be returned by Load when given the font\n\t// data passed as first argument.\n\tStore(FontData, *truetype.Font)\n}\n```\nTypes implementing this interface can be passed to SetFontCache to change the way fonts are being stored and retrieved.\n\n```go\nfunc GetGlobalFontCache() FontCache\n```\n\n```go\ntype FontData struct {\n\tName   string\n\tFamily FontFamily\n\tStyle  FontStyle\n}\n```\n\n```go\ntype FontFamily byte\n```\n\n```go\nconst (\n\tFontFamilySans FontFamily = iota\n\tFontFamilySerif\n\tFontFamilyMono\n)\n```\n\n```go\ntype FontFileNamer func(fontData FontData) string\n```\n\n```go\ntype FontStyle byte\n```\nFontStyle defines bold and italic styles for the font It is possible to combine values for mixed styles, eg.\n\n\tFontData.Style = FontStyleBold | FontStyleItalic\n\n```go\nconst (\n\tFontStyleNormal FontStyle = iota\n\tFontStyleBold\n\tFontStyleItalic\n)\n```\n\n```go\ntype GraphicContext interface {\n\t// PathBuilder describes the interface for path drawing\n\tPathBuilder\n\t// BeginPath creates a new path\n\tBeginPath()\n\t// GetPath copies the current path, then returns it\n\tGetPath() Path\n\t// GetMatrixTransform returns the current transformation matrix\n\tGetMatrixTransform() Matrix\n\t// SetMatrixTransform sets the current transformation matrix\n\tSetMatrixTransform(tr Matrix)\n\t// ComposeMatrixTransform composes the current transformation matrix with tr\n\tComposeMatrixTransform(tr Matrix)\n\t// Rotate applies a rotation to the current transformation matrix. angle is in radian.\n\tRotate(angle float64)\n\t// Translate applies a translation to the current transformation matrix.\n\tTranslate(tx, ty float64)\n\t// Scale applies a scale to the current transformation matrix.\n\tScale(sx, sy float64)\n\t// SetStrokeColor sets the current stroke color\n\tSetStrokeColor(c color.Color)\n\t// SetFillColor sets the current fill color\n\tSetFillColor(c color.Color)\n\t// SetFillRule sets the current fill rule\n\tSetFillRule(f FillRule)\n\t// SetLineWidth sets the current line width\n\tSetLineWidth(lineWidth float64)\n\t// SetLineCap sets the current line cap\n\tSetLineCap(cap LineCap)\n\t// SetLineJoin sets the current line join\n\tSetLineJoin(join LineJoin)\n\t// SetLineDash sets the current dash\n\tSetLineDash(dash []float64, dashOffset float64)\n\t// SetFontSize sets the current font size\n\tSetFontSize(fontSize float64)\n\t// GetFontSize gets the current font size\n\tGetFontSize() float64\n\t// SetFontData sets the current FontData\n\tSetFontData(fontData FontData)\n\t// GetFontData gets the current FontData\n\tGetFontData() FontData\n\t// GetFontName gets the current FontData as a string\n\tGetFontName() string\n\t// DrawImage draws the raster image in the current canvas\n\tDrawImage(image image.Image)\n\t// Save the context and push it to the context stack\n\tSave()\n\t// Restore remove the current context and restore the last one\n\tRestore()\n\t// Clear fills the current canvas with a default transparent color\n\tClear()\n\t// ClearRect fills the specified rectangle with a default transparent color\n\tClearRect(x1, y1, x2, y2 int)\n\t// SetDPI sets the current DPI\n\tSetDPI(dpi int)\n\t// GetDPI gets the current DPI\n\tGetDPI() int\n\t// GetStringBounds gets pixel bounds(dimensions) of given string\n\tGetStringBounds(s string) (left, top, right, bottom float64)\n\t// CreateStringPath creates a path from the string s at x, y\n\tCreateStringPath(text string, x, y float64) (cursor float64)\n\t// FillString draws the text at point (0, 0)\n\tFillString(text string) (cursor float64)\n\t// FillStringAt draws the text at the specified point (x, y)\n\tFillStringAt(text string, x, y float64) (cursor float64)\n\t// StrokeString draws the contour of the text at point (0, 0)\n\tStrokeString(text string) (cursor float64)\n\t// StrokeStringAt draws the contour of the text at point (x, y)\n\tStrokeStringAt(text string, x, y float64) (cursor float64)\n\t// Stroke strokes the paths with the color specified by SetStrokeColor\n\tStroke(paths ...*Path)\n\t// Fill fills the paths with the color specified by SetFillColor\n\tFill(paths ...*Path)\n\t// FillStroke first fills the paths and than strokes them\n\tFillStroke(paths ...*Path)\n}\n```\nGraphicContext describes the interface for the various backends (images, pdf, opengl, ...)\n\n```go\ntype Halign int\n```\nHalign Horizontal Alignment of the text\n\n```go\ntype ImageScaling struct {\n\t// Horizontal Alignment of the image\n\tHalign Halign\n\t// Vertical Alignment of the image\n\tValign Valign\n\t// Width Height used by scaling policy\n\tWidth, Height float64\n\t// ScalingPolicy defines the scaling policy to applied to the image\n\tScalingPolicy ScalingPolicy\n}\n```\nImageScaling style attributes used to display the image\n\n```go\ntype LineCap int\n```\nLineCap is the style of line extremities\n\n```go\nconst (\n\t// RoundCap defines a rounded shape at the end of the line\n\tRoundCap LineCap = iota\n\t// ButtCap defines a squared shape exactly at the end of the line\n\tButtCap\n\t// SquareCap defines a squared shape at the end of the line\n\tSquareCap\n)\n```\n\n```go\nfunc (cap LineCap) String() string\n```\n\n```go\ntype LineJoin int\n```\nLineJoin is the style of segments joint\n\n```go\nconst (\n\t// BevelJoin represents cut segments joint\n\tBevelJoin LineJoin = iota\n\t// RoundJoin represents rounded segments joint\n\tRoundJoin\n\t// MiterJoin represents peaker segments joint\n\tMiterJoin\n)\n```\n\n```go\nfunc (join LineJoin) String() string\n```\n\n```go\ntype Matrix [6]float64\n```\nMatrix represents an affine transformation\n\n```go\nfunc NewIdentityMatrix() Matrix\n```\nNewIdentityMatrix creates an identity transformation matrix.\n\n```go\nfunc NewMatrixFromRects(rectangle1, rectangle2 [4]float64) Matrix\n```\nNewMatrixFromRects creates a transformation matrix, combining a scale and a translation, that transform rectangle1 into rectangle2.\n\n```go\nfunc NewRotationMatrix(angle float64) Matrix\n```\nNewRotationMatrix creates a rotation transformation matrix. angle is in radian\n\n```go\nfunc NewScaleMatrix(sx, sy float64) Matrix\n```\nNewScaleMatrix creates a transformation matrix with a sx, sy scale factor\n\n```go\nfunc NewTranslationMatrix(tx, ty float64) Matrix\n```\nNewTranslationMatrix creates a transformation matrix with a translation tx and ty translation parameter\n\n```go\nfunc (tr *Matrix) Compose(trToCompose Matrix)\n```\nCompose multiplies trToConcat x tr\n\n```go\nfunc (tr Matrix) Copy() Matrix\n```\n\n```go\nfunc (tr Matrix) Determinant() float64\n```\nDeterminant compute the determinant of the matrix\n\n```go\nfunc (tr1 Matrix) Equals(tr2 Matrix) bool\n```\nEquals tests if a two transformation are equal. A tolerance is applied when comparing matrix elements.\n\n```go\nfunc (tr Matrix) GetScale() float64\n```\nGetScale computes a scale for the matrix\n\n```go\nfunc (tr Matrix) GetScaling() (x, y float64)\n```\nGetScaling\n\n```go\nfunc (tr Matrix) GetTranslation() (x, y float64)\n```\nGetTranslation\n\n```go\nfunc (tr *Matrix) Inverse()\n```\nInverse computes the inverse matrix\n\n```go\nfunc (tr Matrix) InverseTransform(points []float64)\n```\nInverseTransform applies the transformation inverse matrix to the rectangle represented by the min and the max point of the rectangle\n\n```go\nfunc (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64)\n```\nInverseTransformPoint applies the transformation inverse matrix to point. It returns the point the transformed point.\n\n```go\nfunc (tr Matrix) IsIdentity() bool\n```\nIsIdentity tests if a transformation is the identity transformation. A tolerance is applied when comparing matrix elements.\n\n```go\nfunc (tr Matrix) IsTranslation() bool\n```\nIsTranslation tests if a transformation is is a pure translation. A tolerance is applied when comparing matrix elements.\n\n```go\nfunc (tr *Matrix) Rotate(angle float64)\n```\nRotate adds a rotation to the matrix. angle is in radian\n\n```go\nfunc (tr *Matrix) Scale(sx, sy float64)\n```\nScale adds a scale to the matrix\n\n```go\nfunc (tr Matrix) Transform(points []float64)\n```\nTransform applies the transformation matrix to points. It modify the points passed in parameter.\n\n```go\nfunc (tr Matrix) TransformPoint(x, y float64) (xres, yres float64)\n```\nTransformPoint applies the transformation matrix to point. It returns the point the transformed point.\n\n```go\nfunc (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64)\n```\nTransform applies the transformation matrix to the rectangle represented by the min and the max point of the rectangle\n\n```go\nfunc (tr *Matrix) Translate(tx, ty float64)\n```\nTranslate adds a translation to the matrix\n\n```go\nfunc (tr Matrix) VectorTransform(points []float64)\n```\nVectorTransform applies the transformation matrix to points without using the translation parameter of the affine matrix. It modify the points passed in parameter.\n\n```go\ntype Path struct {\n\t// Components is a slice of PathCmp in a Path and mark the role of each points in the Path\n\tComponents []PathCmp\n\t// Points are combined with Components to have a specific role in the path\n\tPoints []float64\n\t// contains filtered or unexported fields\n}\n```\nPath stores points\n\n```go\nfunc (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64)\n```\nArcTo adds an arc to the path\n\n```go\nfunc (p *Path) Clear()\n```\nClear reset the path\n\n```go\nfunc (p *Path) Close()\n```\nClose closes the current path\n\n```go\nfunc (p *Path) Copy() (dest *Path)\n```\nCopy make a clone of the current path and return it\n\n```go\nfunc (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64)\n```\nCubicCurveTo adds a cubic bezier curve to the current path\n\n```go\nfunc (p *Path) IsEmpty() bool\n```\nIsEmpty returns true if the path is empty\n\n```go\nfunc (p *Path) LastPoint() (x, y float64)\n```\nLastPoint returns the current point of the current path\n\n```go\nfunc (p *Path) LineTo(x, y float64)\n```\nLineTo adds a line to the current path\n\n```go\nfunc (p *Path) MoveTo(x, y float64)\n```\nMoveTo starts a new path at (x, y) position\n\n```go\nfunc (p *Path) QuadCurveTo(cx, cy, x, y float64)\n```\nQuadCurveTo adds a quadratic bezier curve to the current path\n\n```go\nfunc (p *Path) String() string\n```\nString returns a debug text view of the path\n\n```go\nfunc (path *Path) VerticalFlip() *Path\n```\nReturns new Path with flipped y axes\n\n```go\ntype PathBuilder interface {\n\t// LastPoint returns the current point of the current sub path\n\tLastPoint() (x, y float64)\n\t// MoveTo creates a new subpath that start at the specified point\n\tMoveTo(x, y float64)\n\t// LineTo adds a line to the current subpath\n\tLineTo(x, y float64)\n\t// QuadCurveTo adds a quadratic Bézier curve to the current subpath\n\tQuadCurveTo(cx, cy, x, y float64)\n\t// CubicCurveTo adds a cubic Bézier curve to the current subpath\n\tCubicCurveTo(cx1, cy1, cx2, cy2, x, y float64)\n\t// ArcTo adds an arc to the current subpath\n\tArcTo(cx, cy, rx, ry, startAngle, angle float64)\n\t// Close creates a line from the current point to the last MoveTo\n\t// point (if not the same) and mark the path as closed so the\n\t// first and last lines join nicely.\n\tClose()\n}\n```\nPathBuilder describes the interface for path drawing.\n\n```go\ntype PathCmp int\n```\nPathCmp represents component of a path\n\n```go\nconst (\n\t// MoveToCmp is a MoveTo component in a Path\n\tMoveToCmp PathCmp = iota\n\t// LineToCmp is a LineTo component in a Path\n\tLineToCmp\n\t// QuadCurveToCmp is a QuadCurveTo component in a Path\n\tQuadCurveToCmp\n\t// CubicCurveToCmp is a CubicCurveTo component in a Path\n\tCubicCurveToCmp\n\t// ArcToCmp is a ArcTo component in a Path\n\tArcToCmp\n\t// CloseCmp is a ArcTo component in a Path\n\tCloseCmp\n)\n```\n\n```go\ntype ScalingPolicy int\n```\nScalingPolicy is a constant to define how to scale an image\n\n```go\nconst (\n\t// ScalingNone no scaling applied\n\tScalingNone ScalingPolicy = iota\n\t// ScalingStretch the image is stretched so that its width and height are exactly the given width and height\n\tScalingStretch\n\t// ScalingWidth the image is scaled so that its width is exactly the given width\n\tScalingWidth\n\t// ScalingHeight the image is scaled so that its height is exactly the given height\n\tScalingHeight\n\t// ScalingFit the image is scaled to the largest scale that allow the image to fit within a rectangle width x height\n\tScalingFit\n\t// ScalingSameArea the image is scaled so that its area is exactly the area of the given rectangle width x height\n\tScalingSameArea\n\t// ScalingFill the image is scaled to the smallest scale that allow the image to fully cover a rectangle width x height\n\tScalingFill\n)\n```\n\n```go\ntype SolidFillStyle struct {\n\t// Color defines the line color\n\tColor color.Color\n\t// FillRule defines the file rule to used\n\tFillRule FillRule\n}\n```\nSolidFillStyle define style attributes for a solid fill style\n\n```go\ntype StrokeStyle struct {\n\t// Color defines the color of stroke\n\tColor color.Color\n\t// Line width\n\tWidth float64\n\t// Line cap style rounded, butt or square\n\tLineCap LineCap\n\t// Line join style bevel, round or miter\n\tLineJoin LineJoin\n\t// offset of the first dash\n\tDashOffset float64\n\t// array represented dash length pair values are plain dash and impair are space between dash\n\t// if empty display plain line\n\tDash []float64\n}\n```\nStrokeStyle keeps stroke style attributes that is used by the Stroke method of a Drawer\n\n```go\ntype SyncFolderFontCache struct {\n\tsync.RWMutex\n\t// contains filtered or unexported fields\n}\n```\nSyncFolderFontCache can Load font from folder\n\n```go\nfunc NewSyncFolderFontCache(folder string) *SyncFolderFontCache\n```\nNewSyncFolderFontCache creates SyncFolderFontCache\n\n```go\nfunc (cache *SyncFolderFontCache) Load(fontData FontData) (font *truetype.Font, err error)\n```\nLoad a font from cache if exists otherwise it will load the font from file\n\n```go\nfunc (cache *SyncFolderFontCache) Store(fontData FontData, font *truetype.Font)\n```\nStore a font to this cache\n\n```go\ntype TextStyle struct {\n\t// Color defines the color of text\n\tColor color.Color\n\t// Size font size\n\tSize float64\n\t// The font to use\n\tFont FontData\n\t// Horizontal Alignment of the text\n\tHalign Halign\n\t// Vertical Alignment of the text\n\tValign Valign\n}\n```\nTextStyle describe text property\n\n```go\ntype Valign int\n```\nValign Vertical Alignment of the text\n\n```go\nconst (\n\t// ValignTop top align text\n\tValignTop Valign = iota\n\t// ValignCenter centered text\n\tValignCenter\n\t// ValignBottom bottom aligned text\n\tValignBottom\n\t// ValignBaseline align text with the baseline of the font\n\tValignBaseline\n)\n```\n\n","path":"github.com/llgcode/draw2d","name":"draw2d","synopsis":"Package draw2d is a pure go 2D vector graphics library with support for multiple output devices such as images (draw2d), pdf documents (draw2dpdf) and opengl (draw2dgl), which can also be used on the google app engine.","isRedistributable":true}
