From 9ce4eec92a4b5e260b4622c873355647ecf68304 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 21 Feb 2016 22:25:27 +1100 Subject: [PATCH] Add an example/capjoin program. --- example/capjoin/main.go | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 example/capjoin/main.go diff --git a/example/capjoin/main.go b/example/capjoin/main.go new file mode 100644 index 0000000..13675c9 --- /dev/null +++ b/example/capjoin/main.go @@ -0,0 +1,84 @@ +// Copyright 2016 The Freetype-Go Authors. All rights reserved. +// Use of this source code is governed by your choice of either the +// FreeType License or the GNU General Public License version 2 (or +// any later version), both of which can be found in the LICENSE file. + +// +build ignore +// +// This build tag means that "go install github.com/golang/freetype/..." +// doesn't install this example program. Use "go run main.go" to run it. + +package main + +import ( + "bufio" + "fmt" + "image" + "image/color" + "image/draw" + "image/png" + "log" + "os" + + "github.com/golang/freetype/raster" + "golang.org/x/image/math/fixed" +) + +func main() { + const ( + w = 400 + h = 400 + ) + r := raster.NewRasterizer(w, h) + r.UseNonZeroWinding = true + + cjs := []struct { + c raster.Capper + j raster.Joiner + }{ + {raster.RoundCapper, raster.RoundJoiner}, + {raster.ButtCapper, raster.BevelJoiner}, + {raster.SquareCapper, raster.BevelJoiner}, + } + + for i, cj := range cjs { + var path raster.Path + path.Start(fixed.P(30+100*i, 30+120*i)) + path.Add1(fixed.P(180+100*i, 80+120*i)) + path.Add1(fixed.P(50+100*i, 130+120*i)) + raster.Stroke(r, path, fixed.I(20), cj.c, cj.j) + } + + rgba := image.NewRGBA(image.Rect(0, 0, w, h)) + draw.Draw(rgba, rgba.Bounds(), image.Black, image.Point{}, draw.Src) + p := raster.NewRGBAPainter(rgba) + p.SetColor(color.RGBA{0x7f, 0x7f, 0x7f, 0xff}) + r.Rasterize(p) + + white := color.RGBA{0xff, 0xff, 0xff, 0xff} + for i := range cjs { + rgba.SetRGBA(30+100*i, 30+120*i, white) + rgba.SetRGBA(180+100*i, 80+120*i, white) + rgba.SetRGBA(50+100*i, 130+120*i, white) + } + + // Save that RGBA image to disk. + outFile, err := os.Create("out.png") + if err != nil { + log.Println(err) + os.Exit(1) + } + defer outFile.Close() + b := bufio.NewWriter(outFile) + err = png.Encode(b, rgba) + if err != nil { + log.Println(err) + os.Exit(1) + } + err = b.Flush() + if err != nil { + log.Println(err) + os.Exit(1) + } + fmt.Println("Wrote out.png OK.") +}