1// Copyright 2010 The draw2d Authors. All rights reserved. 2// created: 13/12/2010 by Laurent Le Goff 3 4// Package draw2d is a pure go 2D vector graphics library with support 5// for multiple output devices such as images (draw2d), pdf documents 6// (draw2dpdf) and opengl (draw2dgl), which can also be used on the 7// google app engine. It can be used as a pure go Cairo alternative. 8// draw2d is released under the BSD license. 9// 10// Features 11// 12// Operations in draw2d include stroking and filling polygons, arcs, 13// Bézier curves, drawing images and text rendering with truetype fonts. 14// All drawing operations can be transformed by affine transformations 15// (scale, rotation, translation). 16// 17// Package draw2d follows the conventions of http://www.w3.org/TR/2dcontext for coordinate system, angles, etc... 18// 19// Installation 20// 21// To install or update the package draw2d on your system, run: 22// go get -u github.com/llgcode/draw2d 23// 24// Quick Start 25// 26// Package draw2d itself provides a graphic context that can draw vector 27// graphics and text on an image canvas. The following Go code 28// generates a simple drawing and saves it to an image file: 29// package main 30// 31// import ( 32// "github.com/llgcode/draw2d/draw2dimg" 33// "image" 34// "image/color" 35// ) 36// 37// func main() { 38// // Initialize the graphic context on an RGBA image 39// dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) 40// gc := draw2dimg.NewGraphicContext(dest) 41// 42// // Set some properties 43// gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff}) 44// gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff}) 45// gc.SetLineWidth(5) 46// 47// // Draw a closed shape 48// gc.MoveTo(10, 10) // should always be called first for a new path 49// gc.LineTo(100, 50) 50// gc.QuadCurveTo(100, 10, 10, 10) 51// gc.Close() 52// gc.FillStroke() 53// 54// // Save to file 55// draw2dimg.SaveToPngFile("hello.png", dest) 56// } 57// 58// 59// There are more examples here: 60// https://github.com/llgcode/draw2d/tree/master/samples 61// 62// Drawing on pdf documents is provided by the draw2dpdf package. 63// Drawing on opengl is provided by the draw2dgl package. 64// See subdirectories at the bottom of this page. 65// 66// Testing 67// 68// The samples are run as tests from the root package folder `draw2d` by: 69// go test ./... 70// 71// Or if you want to run with test coverage: 72// go test -cover ./... | grep -v "no test" 73// 74// This will generate output by the different backends in the output folder. 75// 76// Acknowledgments 77// 78// Laurent Le Goff wrote this library, inspired by Postscript and 79// HTML5 canvas. He implemented the image and opengl backend with the 80// freetype-go package. Also he created a pure go Postscript 81// interpreter, which can read postscript images and draw to a draw2d 82// graphic context (https://github.com/llgcode/ps). Stani Michiels 83// implemented the pdf backend with the gofpdf package. 84// 85// Packages using draw2d 86// 87// - https://github.com/llgcode/ps: Postscript interpreter written in Go 88// 89// - https://github.com/gonum/plot: drawing plots in Go 90// 91// - https://github.com/muesli/smartcrop: content aware image cropping 92// 93// - https://github.com/peterhellberg/karta: drawing Voronoi diagrams 94// 95// - https://github.com/vdobler/chart: basic charts in Go 96package draw2d 97 98import "image/color" 99 100// FillRule defines the type for fill rules 101type FillRule int 102 103const ( 104 // FillRuleEvenOdd determines the "insideness" of a point in the shape 105 // by drawing a ray from that point to infinity in any direction 106 // and counting the number of path segments from the given shape that the ray crosses. 107 // If this number is odd, the point is inside; if even, the point is outside. 108 FillRuleEvenOdd FillRule = iota 109 // FillRuleWinding determines the "insideness" of a point in the shape 110 // by drawing a ray from that point to infinity in any direction 111 // and then examining the places where a segment of the shape crosses the ray. 112 // Starting with a count of zero, add one each time a path segment crosses 113 // the ray from left to right and subtract one each time 114 // a path segment crosses the ray from right to left. After counting the crossings, 115 // if the result is zero then the point is outside the path. Otherwise, it is inside. 116 FillRuleWinding 117) 118 119// LineCap is the style of line extremities 120type LineCap int 121 122const ( 123 // RoundCap defines a rounded shape at the end of the line 124 RoundCap LineCap = iota 125 // ButtCap defines a squared shape exactly at the end of the line 126 ButtCap 127 // SquareCap defines a squared shape at the end of the line 128 SquareCap 129) 130 131// LineJoin is the style of segments joint 132type LineJoin int 133 134const ( 135 // BevelJoin represents cut segments joint 136 BevelJoin LineJoin = iota 137 // RoundJoin represents rounded segments joint 138 RoundJoin 139 // MiterJoin represents peaker segments joint 140 MiterJoin 141) 142 143// StrokeStyle keeps stroke style attributes 144// that is used by the Stroke method of a Drawer 145type StrokeStyle struct { 146 // Color defines the color of stroke 147 Color color.Color 148 // Line width 149 Width float64 150 // Line cap style rounded, butt or square 151 LineCap LineCap 152 // Line join style bevel, round or miter 153 LineJoin LineJoin 154 // offset of the first dash 155 DashOffset float64 156 // array represented dash length pair values are plain dash and impair are space between dash 157 // if empty display plain line 158 Dash []float64 159} 160 161// SolidFillStyle define style attributes for a solid fill style 162type SolidFillStyle struct { 163 // Color defines the line color 164 Color color.Color 165 // FillRule defines the file rule to used 166 FillRule FillRule 167} 168 169// Valign Vertical Alignment of the text 170type Valign int 171 172const ( 173 // ValignTop top align text 174 ValignTop Valign = iota 175 // ValignCenter centered text 176 ValignCenter 177 // ValignBottom bottom aligned text 178 ValignBottom 179 // ValignBaseline align text with the baseline of the font 180 ValignBaseline 181) 182 183// Halign Horizontal Alignment of the text 184type Halign int 185 186const ( 187 // HalignLeft Horizontally align to left 188 HalignLeft = iota 189 // HalignCenter Horizontally align to center 190 HalignCenter 191 // HalignRight Horizontally align to right 192 HalignRight 193) 194 195// TextStyle describe text property 196type TextStyle struct { 197 // Color defines the color of text 198 Color color.Color 199 // Size font size 200 Size float64 201 // The font to use 202 Font FontData 203 // Horizontal Alignment of the text 204 Halign Halign 205 // Vertical Alignment of the text 206 Valign Valign 207} 208 209// ScalingPolicy is a constant to define how to scale an image 210type ScalingPolicy int 211 212const ( 213 // ScalingNone no scaling applied 214 ScalingNone ScalingPolicy = iota 215 // ScalingStretch the image is stretched so that its width and height are exactly the given width and height 216 ScalingStretch 217 // ScalingWidth the image is scaled so that its width is exactly the given width 218 ScalingWidth 219 // ScalingHeight the image is scaled so that its height is exactly the given height 220 ScalingHeight 221 // ScalingFit the image is scaled to the largest scale that allow the image to fit within a rectangle width x height 222 ScalingFit 223 // ScalingSameArea the image is scaled so that its area is exactly the area of the given rectangle width x height 224 ScalingSameArea 225 // ScalingFill the image is scaled to the smallest scale that allow the image to fully cover a rectangle width x height 226 ScalingFill 227) 228 229// ImageScaling style attributes used to display the image 230type ImageScaling struct { 231 // Horizontal Alignment of the image 232 Halign Halign 233 // Vertical Alignment of the image 234 Valign Valign 235 // Width Height used by scaling policy 236 Width, Height float64 237 // ScalingPolicy defines the scaling policy to applied to the image 238 ScalingPolicy ScalingPolicy 239} 240