Merge pull request #63 from stanim/samples
Add extra samples, fix arc angle and implement SetLineDash for pdf
3
AUTHORS
|
@ -1 +1,2 @@
|
||||||
Laurent Le Goff
|
Laurent Le Goff
|
||||||
|
Stani Michiels, gmail:stani.be
|
|
@ -5,6 +5,9 @@ Package draw2d is a pure [go](http://golang.org) 2D vector graphics library with
|
||||||
|
|
||||||
See the [documentation](http://godoc.org/github.com/llgcode/draw2d) for more details.
|
See the [documentation](http://godoc.org/github.com/llgcode/draw2d) for more details.
|
||||||
|
|
||||||
|
![demonstration of the image backend](https://raw.githubusercontent.com/llgcode/draw2d/master/output/samples/geometry.png)
|
||||||
|
The image above is generated with the image backend. The pdf which is generated with exactly the same code is [here](https://raw.githubusercontent.com/llgcode/draw2d/master/output/samples/geometry.pdf).
|
||||||
|
|
||||||
Features
|
Features
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
|
|
@ -261,6 +261,12 @@ func (gc *GraphicContext) SetFontSize(fontSize float64) {
|
||||||
gc.pdf.SetFontSize(fontSize * gc.Current.Scale)
|
gc.pdf.SetFontSize(fontSize * gc.Current.Scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLineDash sets the line dash pattern
|
||||||
|
func (gc *GraphicContext) SetLineDash(Dash []float64, DashOffset float64) {
|
||||||
|
gc.StackGraphicContext.SetLineDash(Dash, DashOffset)
|
||||||
|
gc.pdf.SetDashPattern(Dash, DashOffset)
|
||||||
|
}
|
||||||
|
|
||||||
// SetLineWidth sets the line width
|
// SetLineWidth sets the line width
|
||||||
func (gc *GraphicContext) SetLineWidth(LineWidth float64) {
|
func (gc *GraphicContext) SetLineWidth(LineWidth float64) {
|
||||||
gc.StackGraphicContext.SetLineWidth(LineWidth)
|
gc.StackGraphicContext.SetLineWidth(LineWidth)
|
||||||
|
|
|
@ -47,10 +47,11 @@ func (c *PathConverter) ConvertCommand(cmd draw2d.PathCmd, vertices ...float64)
|
||||||
c.pdf.CurveBezierCubicTo(vertices[0], vertices[1], vertices[2], vertices[3], vertices[4], vertices[5])
|
c.pdf.CurveBezierCubicTo(vertices[0], vertices[1], vertices[2], vertices[3], vertices[4], vertices[5])
|
||||||
return 6
|
return 6
|
||||||
case draw2d.ArcTo:
|
case draw2d.ArcTo:
|
||||||
|
// draw2d: angles clockwise, fpdf angles counter clockwise
|
||||||
c.pdf.ArcTo(vertices[0], vertices[1], vertices[2], vertices[3],
|
c.pdf.ArcTo(vertices[0], vertices[1], vertices[2], vertices[3],
|
||||||
0, // degRotate
|
0, // degRotate
|
||||||
vertices[4]*deg, // degStart = startAngle
|
-vertices[4]*deg, // degStart = -startAngle
|
||||||
(vertices[4]-vertices[5])*deg) // degEnd = startAngle-angle
|
(-vertices[4]-vertices[5])*deg) // degEnd = -startAngle-angle
|
||||||
return 6
|
return 6
|
||||||
default: // case draw2d.Close:
|
default: // case draw2d.Close:
|
||||||
c.pdf.ClosePath()
|
c.pdf.ClosePath()
|
||||||
|
|
|
@ -10,7 +10,9 @@ import (
|
||||||
"github.com/llgcode/draw2d"
|
"github.com/llgcode/draw2d"
|
||||||
"github.com/llgcode/draw2d/samples/android"
|
"github.com/llgcode/draw2d/samples/android"
|
||||||
"github.com/llgcode/draw2d/samples/frameimage"
|
"github.com/llgcode/draw2d/samples/frameimage"
|
||||||
|
"github.com/llgcode/draw2d/samples/geometry"
|
||||||
"github.com/llgcode/draw2d/samples/gopher"
|
"github.com/llgcode/draw2d/samples/gopher"
|
||||||
|
"github.com/llgcode/draw2d/samples/gopher2"
|
||||||
"github.com/llgcode/draw2d/samples/helloworld"
|
"github.com/llgcode/draw2d/samples/helloworld"
|
||||||
"github.com/llgcode/draw2d/samples/line"
|
"github.com/llgcode/draw2d/samples/line"
|
||||||
"github.com/llgcode/draw2d/samples/linecapjoin"
|
"github.com/llgcode/draw2d/samples/linecapjoin"
|
||||||
|
@ -21,10 +23,23 @@ func TestSampleAndroid(t *testing.T) {
|
||||||
test(t, android.Main)
|
test(t, android.Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: FillString: w (width) is incorrect
|
||||||
|
func TestSampleGeometry(t *testing.T) {
|
||||||
|
// Set the global folder for searching fonts
|
||||||
|
// The pdf backend needs for every ttf file its corresponding
|
||||||
|
// json/.z file which is generated by gofpdf/makefont.
|
||||||
|
draw2d.SetFontFolder("../resource/font")
|
||||||
|
test(t, geometry.Main)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSampleGopher(t *testing.T) {
|
func TestSampleGopher(t *testing.T) {
|
||||||
test(t, gopher.Main)
|
test(t, gopher.Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSampleGopher2(t *testing.T) {
|
||||||
|
test(t, gopher2.Main)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSampleHelloWorld(t *testing.T) {
|
func TestSampleHelloWorld(t *testing.T) {
|
||||||
// Set the global folder for searching fonts
|
// Set the global folder for searching fonts
|
||||||
// The pdf backend needs for every ttf file its corresponding
|
// The pdf backend needs for every ttf file its corresponding
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
package draw2dpdf_test
|
package draw2dpdf_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/llgcode/draw2d"
|
"github.com/llgcode/draw2d"
|
||||||
|
@ -25,13 +26,13 @@ func test(t *testing.T, draw sample) {
|
||||||
t.Errorf("Drawing %q failed: %v", output, err)
|
t.Errorf("Drawing %q failed: %v", output, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
// Save to pdf only if it doesn't exist because of git
|
// Save to pdf only if it doesn't exist because of git
|
||||||
if _, err = os.Stat(output); err == nil {
|
if _, err = os.Stat(output); err == nil {
|
||||||
t.Skipf("Saving %q skipped, as it exists already. (Git would consider it modified.)", output)
|
t.Skipf("Saving %q skipped, as it exists already. (Git would consider it modified.)", output)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
err = draw2dpdf.SaveToPdfFile(output, dest)
|
err = draw2dpdf.SaveToPdfFile(output, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Saving %q failed: %v", output, err)
|
t.Errorf("Saving %q failed: %v", output, err)
|
||||||
|
|
11
output/README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
Demo output
|
||||||
|
===========
|
||||||
|
|
||||||
|
These folders are empty when you check out the git repository. The output is generated by the tests:
|
||||||
|
```
|
||||||
|
go test ./...
|
||||||
|
```
|
||||||
|
or with coverage:
|
||||||
|
```
|
||||||
|
go test -cover ./... | grep -v "no test"
|
||||||
|
```
|
3
output/samples/.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
# Ignore everything in this directory
|
# Ignore everything in this directory
|
||||||
*
|
*
|
||||||
# Except this file
|
# Except this file
|
||||||
!.gitignore
|
!.gitignore
|
||||||
|
!geometry.*
|
BIN
output/samples/geometry.pdf
Normal file
BIN
output/samples/geometry.png
Normal file
After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 5.2 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 9.8 KiB |
Before Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 5 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 6.1 KiB |
331
samples/geometry/geometry.go
Normal file
|
@ -0,0 +1,331 @@
|
||||||
|
// Copyright 2010 The draw2d Authors. All rights reserved.
|
||||||
|
// created: 21/11/2010 by Laurent Le Goff
|
||||||
|
|
||||||
|
// Package geometry draws some geometric tests.
|
||||||
|
package geometry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/llgcode/draw2d"
|
||||||
|
"github.com/llgcode/draw2d/samples"
|
||||||
|
"github.com/llgcode/draw2d/samples/gopher2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Main draws geometry and returns the filename. This should only be
|
||||||
|
// used during testing.
|
||||||
|
func Main(gc draw2d.GraphicContext, ext string) (string, error) {
|
||||||
|
// Draw the droid
|
||||||
|
Draw(gc, 297, 210)
|
||||||
|
|
||||||
|
// Return the output filename
|
||||||
|
return samples.Output("geometry", ext), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bubble draws a text balloon.
|
||||||
|
func Bubble(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/100, height/100
|
||||||
|
gc.MoveTo(x+sx*50, y)
|
||||||
|
gc.QuadCurveTo(x, y, x, y+sy*37.5)
|
||||||
|
gc.QuadCurveTo(x, y+sy*75, x+sx*25, y+sy*75)
|
||||||
|
gc.QuadCurveTo(x+sx*25, y+sy*95, x+sx*5, y+sy*100)
|
||||||
|
gc.QuadCurveTo(x+sx*35, y+sy*95, x+sx*40, y+sy*75)
|
||||||
|
gc.QuadCurveTo(x+sx*100, y+sy*75, x+sx*100, y+sy*37.5)
|
||||||
|
gc.QuadCurveTo(x+sx*100, y, x+sx*50, y)
|
||||||
|
gc.Stroke()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurveRectangle draws a rectangle with bezier curves (not rounded rectangle).
|
||||||
|
func CurveRectangle(gc draw2d.GraphicContext, x0, y0,
|
||||||
|
rectWidth, rectHeight float64, stroke, fill color.Color) {
|
||||||
|
radius := (rectWidth + rectHeight) / 4
|
||||||
|
|
||||||
|
x1 := x0 + rectWidth
|
||||||
|
y1 := y0 + rectHeight
|
||||||
|
if rectWidth/2 < radius {
|
||||||
|
if rectHeight/2 < radius {
|
||||||
|
gc.MoveTo(x0, (y0+y1)/2)
|
||||||
|
gc.CubicCurveTo(x0, y0, x0, y0, (x0+x1)/2, y0)
|
||||||
|
gc.CubicCurveTo(x1, y0, x1, y0, x1, (y0+y1)/2)
|
||||||
|
gc.CubicCurveTo(x1, y1, x1, y1, (x1+x0)/2, y1)
|
||||||
|
gc.CubicCurveTo(x0, y1, x0, y1, x0, (y0+y1)/2)
|
||||||
|
} else {
|
||||||
|
gc.MoveTo(x0, y0+radius)
|
||||||
|
gc.CubicCurveTo(x0, y0, x0, y0, (x0+x1)/2, y0)
|
||||||
|
gc.CubicCurveTo(x1, y0, x1, y0, x1, y0+radius)
|
||||||
|
gc.LineTo(x1, y1-radius)
|
||||||
|
gc.CubicCurveTo(x1, y1, x1, y1, (x1+x0)/2, y1)
|
||||||
|
gc.CubicCurveTo(x0, y1, x0, y1, x0, y1-radius)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if rectHeight/2 < radius {
|
||||||
|
gc.MoveTo(x0, (y0+y1)/2)
|
||||||
|
gc.CubicCurveTo(x0, y0, x0, y0, x0+radius, y0)
|
||||||
|
gc.LineTo(x1-radius, y0)
|
||||||
|
gc.CubicCurveTo(x1, y0, x1, y0, x1, (y0+y1)/2)
|
||||||
|
gc.CubicCurveTo(x1, y1, x1, y1, x1-radius, y1)
|
||||||
|
gc.LineTo(x0+radius, y1)
|
||||||
|
gc.CubicCurveTo(x0, y1, x0, y1, x0, (y0+y1)/2)
|
||||||
|
} else {
|
||||||
|
gc.MoveTo(x0, y0+radius)
|
||||||
|
gc.CubicCurveTo(x0, y0, x0, y0, x0+radius, y0)
|
||||||
|
gc.LineTo(x1-radius, y0)
|
||||||
|
gc.CubicCurveTo(x1, y0, x1, y0, x1, y0+radius)
|
||||||
|
gc.LineTo(x1, y1-radius)
|
||||||
|
gc.CubicCurveTo(x1, y1, x1, y1, x1-radius, y1)
|
||||||
|
gc.LineTo(x0+radius, y1)
|
||||||
|
gc.CubicCurveTo(x0, y1, x0, y1, x0, y1-radius)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gc.Close()
|
||||||
|
|
||||||
|
gc.SetStrokeColor(stroke)
|
||||||
|
gc.SetFillColor(fill)
|
||||||
|
gc.SetLineWidth(10.0)
|
||||||
|
gc.FillStroke()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dash draws a line with a dash pattern
|
||||||
|
func Dash(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/162, height/205
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.SetLineDash([]float64{height / 10, height / 50, height / 50, height / 50}, -50.0)
|
||||||
|
gc.SetLineCap(draw2d.ButtCap)
|
||||||
|
gc.SetLineJoin(draw2d.RoundJoin)
|
||||||
|
gc.SetLineWidth(height / 50)
|
||||||
|
|
||||||
|
gc.MoveTo(x+sx*60.0, y)
|
||||||
|
gc.LineTo(x+sx*60.0, y)
|
||||||
|
gc.LineTo(x+sx*162, y+sy*205)
|
||||||
|
gc.RLineTo(sx*-102.4, 0.0)
|
||||||
|
gc.CubicCurveTo(x+sx*-17, y+sy*205, x+sx*-17, y+sy*103, x+sx*60.0, y+sy*103.0)
|
||||||
|
gc.Stroke()
|
||||||
|
gc.SetLineDash(nil, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arc draws an arc with a positive angle (clockwise)
|
||||||
|
func Arc(gc draw2d.GraphicContext, xc, yc, width, height float64) {
|
||||||
|
// draw an arc
|
||||||
|
xc += width / 2
|
||||||
|
yc += height / 2
|
||||||
|
radiusX, radiusY := width/2, height/2
|
||||||
|
startAngle := 45 * (math.Pi / 180.0) /* angles are specified */
|
||||||
|
angle := 135 * (math.Pi / 180.0) /* clockwise in radians */
|
||||||
|
gc.SetLineWidth(width / 10)
|
||||||
|
gc.SetLineCap(draw2d.ButtCap)
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
|
||||||
|
gc.ArcTo(xc, yc, radiusX, radiusY, startAngle, angle)
|
||||||
|
gc.Stroke()
|
||||||
|
|
||||||
|
// fill a circle
|
||||||
|
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
gc.SetLineWidth(width / 20)
|
||||||
|
|
||||||
|
gc.MoveTo(xc, yc)
|
||||||
|
gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
|
||||||
|
gc.MoveTo(xc, yc)
|
||||||
|
gc.LineTo(xc-radiusX, yc)
|
||||||
|
gc.Stroke()
|
||||||
|
|
||||||
|
gc.MoveTo(xc, yc)
|
||||||
|
gc.ArcTo(xc, yc, width/10.0, height/10.0, 0, 2*math.Pi)
|
||||||
|
gc.Fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArcNegative draws an arc with a negative angle (anti clockwise).
|
||||||
|
func ArcNegative(gc draw2d.GraphicContext, xc, yc, width, height float64) {
|
||||||
|
xc += width / 2
|
||||||
|
yc += height / 2
|
||||||
|
radiusX, radiusY := width/2, height/2
|
||||||
|
startAngle := 45.0 * (math.Pi / 180.0) /* angles are specified */
|
||||||
|
angle := -225 * (math.Pi / 180.0) /* clockwise in radians */
|
||||||
|
gc.SetLineWidth(width / 10)
|
||||||
|
gc.SetLineCap(draw2d.ButtCap)
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
|
||||||
|
gc.ArcTo(xc, yc, radiusX, radiusY, startAngle, angle)
|
||||||
|
gc.Stroke()
|
||||||
|
// fill a circle
|
||||||
|
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
gc.SetLineWidth(width / 20)
|
||||||
|
|
||||||
|
gc.MoveTo(xc, yc)
|
||||||
|
gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
|
||||||
|
gc.MoveTo(xc, yc)
|
||||||
|
gc.LineTo(xc-radiusX, yc)
|
||||||
|
gc.Stroke()
|
||||||
|
|
||||||
|
gc.ArcTo(xc, yc, width/10.0, height/10.0, 0, 2*math.Pi)
|
||||||
|
gc.Fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CubicCurve draws a cubic curve with its control points.
|
||||||
|
func CubicCurve(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/162, height/205
|
||||||
|
x0, y0 := x, y+sy*100.0
|
||||||
|
x1, y1 := x+sx*75, y+sy*205
|
||||||
|
x2, y2 := x+sx*125, y
|
||||||
|
x3, y3 := x+sx*205, y+sy*100
|
||||||
|
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.SetFillColor(color.NRGBA{0xAA, 0xAA, 0xAA, 0xFF})
|
||||||
|
gc.SetLineWidth(width / 10)
|
||||||
|
gc.MoveTo(x0, y0)
|
||||||
|
gc.CubicCurveTo(x1, y1, x2, y2, x3, y3)
|
||||||
|
gc.Stroke()
|
||||||
|
|
||||||
|
gc.SetStrokeColor(color.NRGBA{0xFF, 0x33, 0x33, 0x88})
|
||||||
|
|
||||||
|
gc.SetLineWidth(width / 20)
|
||||||
|
// draw segment of curve
|
||||||
|
gc.MoveTo(x0, y0)
|
||||||
|
gc.LineTo(x1, y1)
|
||||||
|
gc.LineTo(x2, y2)
|
||||||
|
gc.LineTo(x3, y3)
|
||||||
|
gc.Stroke()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FillString draws a filled and stroked string.
|
||||||
|
func FillString(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/100, height/100
|
||||||
|
gc.Save()
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.SetLineWidth(1)
|
||||||
|
draw2d.RoundRect(gc, x+sx*5, y+sy*5, x+sx*95, y+sy*95, sx*10, sy*10)
|
||||||
|
gc.FillStroke()
|
||||||
|
gc.SetFillColor(image.Black)
|
||||||
|
gc.SetFontSize(height / 6)
|
||||||
|
gc.Translate(x+sx*6, y+sy*52)
|
||||||
|
gc.SetFontData(draw2d.FontData{
|
||||||
|
Name: "luxi",
|
||||||
|
Family: draw2d.FontFamilyMono,
|
||||||
|
Style: draw2d.FontStyleBold | draw2d.FontStyleItalic})
|
||||||
|
w := gc.FillString("cou")
|
||||||
|
gc.Translate(w+sx, 0)
|
||||||
|
left, top, right, bottom := gc.GetStringBounds("cou")
|
||||||
|
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
draw2d.Rect(gc, left, top, right, bottom)
|
||||||
|
gc.SetLineWidth(height / 20)
|
||||||
|
gc.Stroke()
|
||||||
|
gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
|
||||||
|
gc.SetLineWidth(height / 100)
|
||||||
|
gc.StrokeString("cou")
|
||||||
|
gc.Restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FillStroke first fills and afterwards strokes a path.
|
||||||
|
func FillStroke(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/210, height/215
|
||||||
|
gc.MoveTo(x+sx*113.0, y)
|
||||||
|
gc.LineTo(x+sx*215.0, y+sy*215)
|
||||||
|
gc.RLineTo(sx*-100, 0)
|
||||||
|
gc.CubicCurveTo(x+sx*35, y+sy*215, x+sx*35, y+sy*113, x+sx*113.0, y+sy*113)
|
||||||
|
gc.Close()
|
||||||
|
|
||||||
|
gc.MoveTo(x+sx*50.0, y)
|
||||||
|
gc.RLineTo(sx*51.2, sy*51.2)
|
||||||
|
gc.RLineTo(sx*-51.2, sy*51.2)
|
||||||
|
gc.RLineTo(sx*-51.2, sy*-51.2)
|
||||||
|
gc.Close()
|
||||||
|
|
||||||
|
gc.SetLineWidth(width / 20.0)
|
||||||
|
gc.SetFillColor(color.NRGBA{0, 0, 0xFF, 0xFF})
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.FillStroke()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FillStyle demonstrates the difference between even odd and non zero winding rule.
|
||||||
|
func FillStyle(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
sx, sy := width/232, height/220
|
||||||
|
gc.SetLineWidth(width / 40)
|
||||||
|
|
||||||
|
draw2d.Rect(gc, x+sx*0, y+sy*12, x+sx*232, y+sy*70)
|
||||||
|
|
||||||
|
wheel1 := new(draw2d.PathStorage)
|
||||||
|
wheel1.ArcTo(x+sx*52, y+sy*70, sx*40, sy*40, 0, 2*math.Pi)
|
||||||
|
wheel2 := new(draw2d.PathStorage)
|
||||||
|
wheel2.ArcTo(x+sx*180, y+sy*70, sx*40, sy*40, 0, -2*math.Pi)
|
||||||
|
|
||||||
|
gc.SetFillRule(draw2d.FillRuleEvenOdd)
|
||||||
|
gc.SetFillColor(color.NRGBA{0, 0xB2, 0, 0xFF})
|
||||||
|
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.FillStroke(wheel1, wheel2)
|
||||||
|
|
||||||
|
draw2d.Rect(gc, x, y+sy*140, x+sx*232, y+sy*198)
|
||||||
|
wheel1 = new(draw2d.PathStorage)
|
||||||
|
wheel1.ArcTo(x+sx*52, y+sy*198, sx*40, sy*40, 0, 2*math.Pi)
|
||||||
|
wheel2 = new(draw2d.PathStorage)
|
||||||
|
wheel2.ArcTo(x+sx*180, y+sy*198, sx*40, sy*40, 0, -2*math.Pi)
|
||||||
|
|
||||||
|
gc.SetFillRule(draw2d.FillRuleWinding)
|
||||||
|
gc.SetFillColor(color.NRGBA{0, 0, 0xE5, 0xFF})
|
||||||
|
gc.FillStroke(wheel1, wheel2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathTransform scales a path differently in horizontal and vertical direction.
|
||||||
|
func PathTransform(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
gc.Save()
|
||||||
|
gc.SetLineWidth(width / 10)
|
||||||
|
gc.Translate(x+width/2, y+height/2)
|
||||||
|
gc.Scale(1, 4)
|
||||||
|
gc.ArcTo(0, 0, width/8, height/8, 0, math.Pi*2)
|
||||||
|
gc.Close()
|
||||||
|
gc.Stroke()
|
||||||
|
gc.Restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Star draws many lines from a center.
|
||||||
|
func Star(gc draw2d.GraphicContext, x, y, width, height float64) {
|
||||||
|
gc.Save()
|
||||||
|
gc.Translate(x+width/2, y+height/2)
|
||||||
|
gc.SetLineWidth(width / 40)
|
||||||
|
for i := 0.0; i < 360; i = i + 10 { // Go from 0 to 360 degrees in 10 degree steps
|
||||||
|
gc.Save() // Keep rotations temporary
|
||||||
|
gc.Rotate(i * (math.Pi / 180.0)) // Rotate by degrees on stack from 'for'
|
||||||
|
gc.MoveTo(0, 0)
|
||||||
|
gc.LineTo(width/2, 0)
|
||||||
|
gc.Stroke()
|
||||||
|
gc.Restore()
|
||||||
|
}
|
||||||
|
gc.Restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw all figures in a nice 4x3 grid.
|
||||||
|
func Draw(gc draw2d.GraphicContext, width, height float64) {
|
||||||
|
mx, my := width*0.025, height*0.025 // margin
|
||||||
|
dx, dy := (width-2*mx)/4, (height-2*my)/3
|
||||||
|
w, h := dx-2*mx, dy-2*my
|
||||||
|
x0, y := 2*mx, 2*my
|
||||||
|
x := x0
|
||||||
|
Bubble(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
CurveRectangle(gc, x, y, w, h, color.NRGBA{0x80, 0, 0, 0x80}, color.NRGBA{0x80, 0x80, 0xFF, 0xFF})
|
||||||
|
x += dx
|
||||||
|
Dash(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
Arc(gc, x, y, w, h)
|
||||||
|
x = x0
|
||||||
|
y += dy
|
||||||
|
ArcNegative(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
CubicCurve(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
FillString(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
FillStroke(gc, x, y, w, h)
|
||||||
|
x = x0
|
||||||
|
y += dy
|
||||||
|
FillStyle(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
PathTransform(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
Star(gc, x, y, w, h)
|
||||||
|
x += dx
|
||||||
|
gopher2.Draw(gc, x, y, w, h/2)
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Main draws a left hand and ear of a gopher. Afterwards it returns
|
// Main draws a left hand and ear of a gopher. Afterwards it returns
|
||||||
// the filename. This should only be during testing.
|
// the filename. This should only be used during testing.
|
||||||
func Main(gc draw2d.GraphicContext, ext string) (string, error) {
|
func Main(gc draw2d.GraphicContext, ext string) (string, error) {
|
||||||
gc.Save()
|
gc.Save()
|
||||||
gc.Scale(0.5, 0.5)
|
gc.Scale(0.5, 0.5)
|
||||||
|
|
106
samples/gopher2/gopher2.go
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
// Copyright 2010 The draw2d Authors. All rights reserved.
|
||||||
|
// created: 21/11/2010 by Laurent Le Goff
|
||||||
|
|
||||||
|
// Package gopher2 draws a gopher avatar based on a svg of:
|
||||||
|
// https://github.com/golang-samples/gopher-vector/
|
||||||
|
package gopher2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/llgcode/draw2d"
|
||||||
|
"github.com/llgcode/draw2d/samples"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Main draws a rotated face of the gopher. Afterwards it returns
|
||||||
|
// the filename. This should only be used during testing.
|
||||||
|
func Main(gc draw2d.GraphicContext, ext string) (string, error) {
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.SetFillColor(image.White)
|
||||||
|
gc.Save()
|
||||||
|
// Draw a (partial) gopher
|
||||||
|
gc.Translate(-65, 65)
|
||||||
|
gc.Rotate(-30 * (math.Pi / 180.0))
|
||||||
|
Draw(gc, 48, 48, 240, 72)
|
||||||
|
gc.Restore()
|
||||||
|
|
||||||
|
// Return the output filename
|
||||||
|
return samples.Output("gopher2", ext), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw a gopher head (not rotated)
|
||||||
|
func Draw(gc draw2d.GraphicContext, x, y, w, h float64) {
|
||||||
|
h23 := (h * 2) / 3
|
||||||
|
|
||||||
|
blf := color.RGBA{0, 0, 0, 0xff} // black
|
||||||
|
wf := color.RGBA{0xff, 0xff, 0xff, 0xff} // white
|
||||||
|
nf := color.RGBA{0x8B, 0x45, 0x13, 0xff} // brown opaque
|
||||||
|
brf := color.RGBA{0x8B, 0x45, 0x13, 0x99} // brown transparant
|
||||||
|
brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant
|
||||||
|
|
||||||
|
// round head top
|
||||||
|
gc.MoveTo(x, y+h)
|
||||||
|
gc.CubicCurveTo(x, y+h*1.05, x+w/2, y-h, x+w, y+h*1.05)
|
||||||
|
gc.Close()
|
||||||
|
gc.SetFillColor(brb)
|
||||||
|
gc.Fill()
|
||||||
|
// rectangle head bottom
|
||||||
|
draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, w/5, h/5)
|
||||||
|
gc.Fill()
|
||||||
|
// left ear outside
|
||||||
|
draw2d.Circle(gc, x, y+h, w/12)
|
||||||
|
gc.SetFillColor(brf)
|
||||||
|
gc.Fill()
|
||||||
|
// left ear inside
|
||||||
|
draw2d.Circle(gc, x, y+h, 0.5*w/12)
|
||||||
|
gc.SetFillColor(nf)
|
||||||
|
gc.Fill()
|
||||||
|
// right ear outside
|
||||||
|
draw2d.Circle(gc, x+w, y+h, w/12)
|
||||||
|
gc.SetFillColor(brf)
|
||||||
|
gc.Fill()
|
||||||
|
// right ear inside
|
||||||
|
draw2d.Circle(gc, x+w, y+h, 0.5*w/12)
|
||||||
|
gc.SetFillColor(nf)
|
||||||
|
gc.Fill()
|
||||||
|
// left eye outside white
|
||||||
|
draw2d.Circle(gc, x+w/3, y+h23, w/9)
|
||||||
|
gc.SetFillColor(wf)
|
||||||
|
gc.Fill()
|
||||||
|
// left eye black
|
||||||
|
draw2d.Circle(gc, x+w/3+w/24, y+h23, 0.5*w/9)
|
||||||
|
gc.SetFillColor(blf)
|
||||||
|
gc.Fill()
|
||||||
|
// left eye inside white
|
||||||
|
draw2d.Circle(gc, x+w/3+w/24+w/48, y+h23, 0.2*w/9)
|
||||||
|
gc.SetFillColor(wf)
|
||||||
|
gc.Fill()
|
||||||
|
// right eye outside white
|
||||||
|
draw2d.Circle(gc, x+w-w/3, y+h23, w/9)
|
||||||
|
gc.Fill()
|
||||||
|
// right eye black
|
||||||
|
draw2d.Circle(gc, x+w-w/3+w/24, y+h23, 0.5*w/9)
|
||||||
|
gc.SetFillColor(blf)
|
||||||
|
gc.Fill()
|
||||||
|
// right eye inside white
|
||||||
|
draw2d.Circle(gc, x+w-(w/3)+w/24+w/48, y+h23, 0.2*w/9)
|
||||||
|
gc.SetFillColor(wf)
|
||||||
|
gc.Fill()
|
||||||
|
// left tooth
|
||||||
|
gc.SetFillColor(wf)
|
||||||
|
draw2d.RoundRect(gc, x+w/2-w/8, y+h+h/2.5, x+w/2-w/8+w/8, y+h+h/2.5+w/6, w/10, w/10)
|
||||||
|
gc.Fill()
|
||||||
|
// right tooth
|
||||||
|
draw2d.RoundRect(gc, x+w/2, y+h+h/2.5, x+w/2+w/8, y+h+h/2.5+w/6, w/10, w/10)
|
||||||
|
gc.Fill()
|
||||||
|
// snout
|
||||||
|
draw2d.Ellipse(gc, x+(w/2), y+h+h/2.5, w/6, w/12)
|
||||||
|
gc.SetFillColor(nf)
|
||||||
|
gc.Fill()
|
||||||
|
// nose
|
||||||
|
draw2d.Ellipse(gc, x+(w/2), y+h+h/7, w/10, w/12)
|
||||||
|
gc.SetFillColor(blf)
|
||||||
|
gc.Fill()
|
||||||
|
}
|
|
@ -8,7 +8,9 @@ import (
|
||||||
"github.com/llgcode/draw2d"
|
"github.com/llgcode/draw2d"
|
||||||
"github.com/llgcode/draw2d/samples/android"
|
"github.com/llgcode/draw2d/samples/android"
|
||||||
"github.com/llgcode/draw2d/samples/frameimage"
|
"github.com/llgcode/draw2d/samples/frameimage"
|
||||||
|
"github.com/llgcode/draw2d/samples/geometry"
|
||||||
"github.com/llgcode/draw2d/samples/gopher"
|
"github.com/llgcode/draw2d/samples/gopher"
|
||||||
|
"github.com/llgcode/draw2d/samples/gopher2"
|
||||||
"github.com/llgcode/draw2d/samples/helloworld"
|
"github.com/llgcode/draw2d/samples/helloworld"
|
||||||
"github.com/llgcode/draw2d/samples/line"
|
"github.com/llgcode/draw2d/samples/line"
|
||||||
"github.com/llgcode/draw2d/samples/linecapjoin"
|
"github.com/llgcode/draw2d/samples/linecapjoin"
|
||||||
|
@ -19,10 +21,22 @@ func TestSampleAndroid(t *testing.T) {
|
||||||
test(t, android.Main)
|
test(t, android.Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSampleGeometry(t *testing.T) {
|
||||||
|
// Set the global folder for searching fonts
|
||||||
|
// The pdf backend needs for every ttf file its corresponding
|
||||||
|
// json/.z file which is generated by gofpdf/makefont.
|
||||||
|
draw2d.SetFontFolder("resource/font")
|
||||||
|
test(t, geometry.Main)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSampleGopher(t *testing.T) {
|
func TestSampleGopher(t *testing.T) {
|
||||||
test(t, gopher.Main)
|
test(t, gopher.Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSampleGopher2(t *testing.T) {
|
||||||
|
test(t, gopher2.Main)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSampleHelloWorld(t *testing.T) {
|
func TestSampleHelloWorld(t *testing.T) {
|
||||||
// Set the global folder for searching fonts
|
// Set the global folder for searching fonts
|
||||||
draw2d.SetFontFolder("resource/font")
|
draw2d.SetFontFolder("resource/font")
|
||||||
|
|