Categorygithub.com/llgcode/draw2d
modulepackage
0.0.0-20240627062922-0ed1ff131195
Repository: https://github.com/llgcode/draw2d.git
Documentation: pkg.go.dev

# README

draw2d

Coverage GoDoc BuyMeaBeer

Package draw2d is a go 2D vector graphics library with support for multiple outputs such as images (draw2d), pdf documents (draw2dpdf), opengl (draw2dgl) and svg (draw2dsvg). There's also a Postscript reader that uses draw2d. draw2d is released under the BSD license. See the documentation for more details.

geometrypostscript

Click on an image above to get the pdf, generated with exactly the same draw2d code. The first image is the output of samples/geometry. The second image is the result of samples/postcript, which demonstrates that draw2d can draw postscript files into images or pdf documents with the ps package.

Features

Operations 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).

Package draw2d follows the conventions of the HTML Canvas 2D Context for coordinate system, angles, etc...

Installation

Install golang. To install or update the package draw2d on your system, run:

Stable release

go get -u gopkg.in/llgcode/draw2d.v1

or Current release

go get -u github.com/llgcode/draw2d

Quick Start

The following Go code generates a simple drawing and saves it to an image file with package draw2d:

package main

import (
	"github.com/llgcode/draw2d/draw2dimg"
	"image"
	"image/color"
)

func main() {
	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2dimg.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.BeginPath() // Initialize a new path
	gc.MoveTo(10, 10) // Move to a position to start the new path
	gc.LineTo(100, 50)
	gc.QuadCurveTo(100, 10, 10, 10)
	gc.Close()
	gc.FillStroke()

	// Save to file
	draw2dimg.SaveToPngFile("hello.png", dest)
}

The same Go code can also generate a pdf document with package draw2dpdf:

package main

import (
	"github.com/llgcode/draw2d/draw2dpdf"
	"image/color"
)

func main() {
	// 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

Drawing on opengl is provided by the draw2dgl package.

Testing

The samples are run as tests from the root package folder draw2d by:

go test ./...

Or if you want to run with test coverage:

go test -cover ./... | grep -v "no test"

This will generate output by the different backends in the output folder.

Acknowledgments

Laurent 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. Stani Michiels implemented the pdf backend with the gofpdf package.

Packages using draw2d

  • ps: Postscript interpreter written in Go
  • go.uik: a concurrent UI kit written in pure go.
  • smartcrop: content aware image cropping
  • karta: drawing Voronoi diagrams
  • chart: basic charts in Go
  • hilbert: package for drawing Hilbert curves

References

# Packages

Package draw2dgl provides a graphic context that can draw vector graphics and text on OpenGL.
Package draw2dkit provides helpers to draw common figures using a Path.
Package draw2dpdf provides a graphic context that can draw vector graphics and text on pdf file with the gofpdf package.
NOTE that this is identical copy of draw2dgl/text.go and draw2dimg/text.go.
Package samples provides examples which can be used with different backends.

# Functions

NewFolderFontCache creates FolderFontCache.
NewIdentityMatrix creates an identity transformation matrix.
NewMatrixFromRects creates a transformation matrix, combining a scale and a translation, that transform rectangle1 into rectangle2.
NewRotationMatrix creates a rotation transformation matrix.
NewScaleMatrix creates a transformation matrix with a sx, sy scale factor.
NewSyncFolderFontCache creates SyncFolderFontCache.
NewTranslationMatrix creates a transformation matrix with a translation tx and ty translation parameter.
Changes the font cache backend used by the package.

# Constants

ArcToCmp is a ArcTo component in a Path.
BevelJoin represents cut segments joint.
ButtCap defines a squared shape exactly at the end of the line.
CloseCmp is a ArcTo component in a Path.
CubicCurveToCmp is a CubicCurveTo component in a Path.
FillRuleEvenOdd determines the "insideness" of a point in the shape by drawing a ray from that point to infinity in any direction and counting the number of path segments from the given shape that the ray crosses.
FillRuleWinding determines the "insideness" of a point in the shape by drawing a ray from that point to infinity in any direction and then examining the places where a segment of the shape crosses the ray.
HalignCenter Horizontally align to center.
HalignLeft Horizontally align to left.
HalignRight Horizontally align to right.
LineToCmp is a LineTo component in a Path.
MiterJoin represents peaker segments joint.
MoveToCmp is a MoveTo component in a Path.
QuadCurveToCmp is a QuadCurveTo component in a Path.
RoundCap defines a rounded shape at the end of the line.
RoundJoin represents rounded segments joint.
ScalingFill the image is scaled to the smallest scale that allow the image to fully cover a rectangle width x height.
ScalingFit the image is scaled to the largest scale that allow the image to fit within a rectangle width x height.
ScalingHeight the image is scaled so that its height is exactly the given height.
ScalingNone no scaling applied.
ScalingSameArea the image is scaled so that its area is exactly the area of the given rectangle width x height.
ScalingStretch the image is stretched so that its width and height are exactly the given width and height.
ScalingWidth the image is scaled so that its width is exactly the given width.
SquareCap defines a squared shape at the end of the line.
ValignBaseline align text with the baseline of the font.
ValignBottom bottom aligned text.
ValignCenter centered text.
ValignTop top align text.

# Structs

FolderFontCache can Load font from folder.
ImageScaling style attributes used to display the image.
Path stores points.
SolidFillStyle define style attributes for a solid fill style.
StrokeStyle keeps stroke style attributes that is used by the Stroke method of a Drawer.
SyncFolderFontCache can Load font from folder.
TextStyle describe text property.

# Interfaces

Types implementing this interface can be passed to SetFontCache to change the way fonts are being stored and retrieved.
GraphicContext describes the interface for the various backends (images, pdf, opengl, ...).
PathBuilder describes the interface for path drawing.

# Type aliases

FillRule defines the type for fill rules.
FontStyle defines bold and italic styles for the font It is possible to combine values for mixed styles, eg.
Halign Horizontal Alignment of the text.
LineCap is the style of line extremities.
LineJoin is the style of segments joint.
Matrix represents an affine transformation.
PathCmp represents component of a path.
ScalingPolicy is a constant to define how to scale an image.
Valign Vertical Alignment of the text.