Skip to content
2D rendering for different output (raster, pdf, svg)
Branch: master
Clone or download
llgcode Merge pull request #146 from piotrkowalczuk/get-font-name-proper-font…
…-size-serialization

GetFontName serialize font size properly
thanks @piotrkowalczuk
Latest commit f52c8a7 Aug 25, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
draw2dbase single GetFontName implementation that serialize font size properly Aug 20, 2018
draw2dgl Implement CreateStringPath and GetStringBounds in svg context Jan 7, 2018
draw2dimg Refactor Api using an interface Dec 4, 2017
draw2dkit Create README.md Sep 11, 2015
draw2dpdf Update code.google.com/p/freetype-go to github.com/golang/freetype. F… Sep 7, 2015
draw2dsvg single GetFontName implementation that serialize font size properly Aug 20, 2018
output single GetFontName implementation that serialize font size properly Aug 20, 2018
resource Merge branch 'master' into Clean-Up Aug 14, 2015
samples Experimental embed svg font functionality Jan 7, 2018
.gitignore single GetFontName implementation that serialize font size properly Aug 20, 2018
AUTHORS draw2d: add Sebastien Binet to AUTHORS Jul 24, 2018
LICENSE Remove oldies Apr 27, 2011
README.md Good Practice: always use BeginPath Jan 8, 2018
draw2d.go Gofmt Dec 24, 2017
font.go Gofmt Dec 24, 2017
gc.go Merge pull request #118 from redstarcoder/text_cache_upstream Oct 26, 2016
go.mod draw2d: add support for Go modules Jul 24, 2018
matrix.go Rename MatrixTransform to Matrix Apr 30, 2015
path.go Experimental embed svg font functionality Jan 7, 2018
samples_test.go add geometry and gopher2 samples Jul 11, 2015
sync_test.go Gofmt Dec 24, 2017
test Change file permission Aug 14, 2015
test_test.go Merge branch 'master' into Clean-Up Aug 14, 2015

README.md

draw2d

Coverage GoDoc

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. It can be used as a pure go Cairo alternative. 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
  • gonum/plot: drawing plots 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

You can’t perform that action at this time.