summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/golang/freetype/example
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/golang/freetype/example')
-rw-r--r--vendor/github.com/golang/freetype/example/capjoin/main.go85
-rw-r--r--vendor/github.com/golang/freetype/example/drawer/main.go158
-rw-r--r--vendor/github.com/golang/freetype/example/freetype/main.go150
-rw-r--r--vendor/github.com/golang/freetype/example/gamma/main.go86
-rw-r--r--vendor/github.com/golang/freetype/example/raster/main.go185
-rw-r--r--vendor/github.com/golang/freetype/example/round/main.go110
-rw-r--r--vendor/github.com/golang/freetype/example/truetype/main.go89
7 files changed, 863 insertions, 0 deletions
diff --git a/vendor/github.com/golang/freetype/example/capjoin/main.go b/vendor/github.com/golang/freetype/example/capjoin/main.go
new file mode 100644
index 000000000..71f3356c7
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/capjoin/main.go
@@ -0,0 +1,85 @@
+// 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 example
+//
+// 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 or "go
+// install -tags=example" to install 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/drawer/main.go b/vendor/github.com/golang/freetype/example/drawer/main.go
new file mode 100644
index 000000000..d26d066d9
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/drawer/main.go
@@ -0,0 +1,158 @@
+// Copyright 2015 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 example
+//
+// 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 or "go
+// install -tags=example" to install it.
+
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "image"
+ "image/color"
+ "image/draw"
+ "image/png"
+ "io/ioutil"
+ "log"
+ "math"
+ "os"
+
+ "github.com/golang/freetype/truetype"
+ "golang.org/x/image/font"
+ "golang.org/x/image/math/fixed"
+)
+
+var (
+ dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
+ fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
+ hinting = flag.String("hinting", "none", "none | full")
+ size = flag.Float64("size", 12, "font size in points")
+ spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
+ wonb = flag.Bool("whiteonblack", false, "white text on a black background")
+)
+
+const title = "Jabberwocky"
+
+var text = []string{
+ "’Twas brillig, and the slithy toves",
+ "Did gyre and gimble in the wabe;",
+ "All mimsy were the borogoves,",
+ "And the mome raths outgrabe.",
+ "",
+ "“Beware the Jabberwock, my son!",
+ "The jaws that bite, the claws that catch!",
+ "Beware the Jubjub bird, and shun",
+ "The frumious Bandersnatch!”",
+ "",
+ "He took his vorpal sword in hand:",
+ "Long time the manxome foe he sought—",
+ "So rested he by the Tumtum tree,",
+ "And stood awhile in thought.",
+ "",
+ "And as in uffish thought he stood,",
+ "The Jabberwock, with eyes of flame,",
+ "Came whiffling through the tulgey wood,",
+ "And burbled as it came!",
+ "",
+ "One, two! One, two! and through and through",
+ "The vorpal blade went snicker-snack!",
+ "He left it dead, and with its head",
+ "He went galumphing back.",
+ "",
+ "“And hast thou slain the Jabberwock?",
+ "Come to my arms, my beamish boy!",
+ "O frabjous day! Callooh! Callay!”",
+ "He chortled in his joy.",
+ "",
+ "’Twas brillig, and the slithy toves",
+ "Did gyre and gimble in the wabe;",
+ "All mimsy were the borogoves,",
+ "And the mome raths outgrabe.",
+}
+
+func main() {
+ flag.Parse()
+
+ // Read the font data.
+ fontBytes, err := ioutil.ReadFile(*fontfile)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ f, err := truetype.Parse(fontBytes)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+
+ // Draw the background and the guidelines.
+ fg, bg := image.Black, image.White
+ ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
+ if *wonb {
+ fg, bg = image.White, image.Black
+ ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
+ }
+ const imgW, imgH = 640, 480
+ rgba := image.NewRGBA(image.Rect(0, 0, imgW, imgH))
+ draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
+ for i := 0; i < 200; i++ {
+ rgba.Set(10, 10+i, ruler)
+ rgba.Set(10+i, 10, ruler)
+ }
+
+ // Draw the text.
+ h := font.HintingNone
+ switch *hinting {
+ case "full":
+ h = font.HintingFull
+ }
+ d := &font.Drawer{
+ Dst: rgba,
+ Src: fg,
+ Face: truetype.NewFace(f, &truetype.Options{
+ Size: *size,
+ DPI: *dpi,
+ Hinting: h,
+ }),
+ }
+ y := 10 + int(math.Ceil(*size**dpi/72))
+ dy := int(math.Ceil(*size * *spacing * *dpi / 72))
+ d.Dot = fixed.Point26_6{
+ X: (fixed.I(imgW) - d.MeasureString(title)) / 2,
+ Y: fixed.I(y),
+ }
+ d.DrawString(title)
+ y += dy
+ for _, s := range text {
+ d.Dot = fixed.P(10, y)
+ d.DrawString(s)
+ y += dy
+ }
+
+ // 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/freetype/main.go b/vendor/github.com/golang/freetype/example/freetype/main.go
new file mode 100644
index 000000000..dfbde9a2f
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/freetype/main.go
@@ -0,0 +1,150 @@
+// Copyright 2010 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 example
+//
+// 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 or "go
+// install -tags=example" to install it.
+
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "image"
+ "image/color"
+ "image/draw"
+ "image/png"
+ "io/ioutil"
+ "log"
+ "os"
+
+ "github.com/golang/freetype"
+ "golang.org/x/image/font"
+)
+
+var (
+ dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
+ fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
+ hinting = flag.String("hinting", "none", "none | full")
+ size = flag.Float64("size", 12, "font size in points")
+ spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
+ wonb = flag.Bool("whiteonblack", false, "white text on a black background")
+)
+
+var text = []string{
+ "’Twas brillig, and the slithy toves",
+ "Did gyre and gimble in the wabe;",
+ "All mimsy were the borogoves,",
+ "And the mome raths outgrabe.",
+ "",
+ "“Beware the Jabberwock, my son!",
+ "The jaws that bite, the claws that catch!",
+ "Beware the Jubjub bird, and shun",
+ "The frumious Bandersnatch!”",
+ "",
+ "He took his vorpal sword in hand:",
+ "Long time the manxome foe he sought—",
+ "So rested he by the Tumtum tree,",
+ "And stood awhile in thought.",
+ "",
+ "And as in uffish thought he stood,",
+ "The Jabberwock, with eyes of flame,",
+ "Came whiffling through the tulgey wood,",
+ "And burbled as it came!",
+ "",
+ "One, two! One, two! and through and through",
+ "The vorpal blade went snicker-snack!",
+ "He left it dead, and with its head",
+ "He went galumphing back.",
+ "",
+ "“And hast thou slain the Jabberwock?",
+ "Come to my arms, my beamish boy!",
+ "O frabjous day! Callooh! Callay!”",
+ "He chortled in his joy.",
+ "",
+ "’Twas brillig, and the slithy toves",
+ "Did gyre and gimble in the wabe;",
+ "All mimsy were the borogoves,",
+ "And the mome raths outgrabe.",
+}
+
+func main() {
+ flag.Parse()
+
+ // Read the font data.
+ fontBytes, err := ioutil.ReadFile(*fontfile)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ f, err := freetype.ParseFont(fontBytes)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+
+ // Initialize the context.
+ fg, bg := image.Black, image.White
+ ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
+ if *wonb {
+ fg, bg = image.White, image.Black
+ ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
+ }
+ rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
+ draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
+ c := freetype.NewContext()
+ c.SetDPI(*dpi)
+ c.SetFont(f)
+ c.SetFontSize(*size)
+ c.SetClip(rgba.Bounds())
+ c.SetDst(rgba)
+ c.SetSrc(fg)
+ switch *hinting {
+ default:
+ c.SetHinting(font.HintingNone)
+ case "full":
+ c.SetHinting(font.HintingFull)
+ }
+
+ // Draw the guidelines.
+ for i := 0; i < 200; i++ {
+ rgba.Set(10, 10+i, ruler)
+ rgba.Set(10+i, 10, ruler)
+ }
+
+ // Draw the text.
+ pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6))
+ for _, s := range text {
+ _, err = c.DrawString(s, pt)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ pt.Y += c.PointToFixed(*size * *spacing)
+ }
+
+ // 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/gamma/main.go b/vendor/github.com/golang/freetype/example/gamma/main.go
new file mode 100644
index 000000000..cdd50bc3b
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/gamma/main.go
@@ -0,0 +1,86 @@
+// Copyright 2010 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 example
+//
+// 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 or "go
+// install -tags=example" to install it.
+
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "image"
+ "image/draw"
+ "image/png"
+ "log"
+ "os"
+
+ "github.com/golang/freetype/raster"
+ "golang.org/x/image/math/fixed"
+)
+
+func p(x, y int) fixed.Point26_6 {
+ return fixed.Point26_6{
+ X: fixed.Int26_6(x * 64),
+ Y: fixed.Int26_6(y * 64),
+ }
+}
+
+func main() {
+ // Draw a rounded corner that is one pixel wide.
+ r := raster.NewRasterizer(50, 50)
+ r.Start(p(5, 5))
+ r.Add1(p(5, 25))
+ r.Add2(p(5, 45), p(25, 45))
+ r.Add1(p(45, 45))
+ r.Add1(p(45, 44))
+ r.Add1(p(26, 44))
+ r.Add2(p(6, 44), p(6, 24))
+ r.Add1(p(6, 5))
+ r.Add1(p(5, 5))
+
+ // Rasterize that curve multiple times at different gammas.
+ const (
+ w = 600
+ h = 200
+ )
+ rgba := image.NewRGBA(image.Rect(0, 0, w, h))
+ draw.Draw(rgba, image.Rect(0, 0, w, h/2), image.Black, image.ZP, draw.Src)
+ draw.Draw(rgba, image.Rect(0, h/2, w, h), image.White, image.ZP, draw.Src)
+ mask := image.NewAlpha(image.Rect(0, 0, 50, 50))
+ painter := raster.NewAlphaSrcPainter(mask)
+ gammas := []float64{1.0 / 10.0, 1.0 / 3.0, 1.0 / 2.0, 2.0 / 3.0, 4.0 / 5.0, 1.0, 5.0 / 4.0, 3.0 / 2.0, 2.0, 3.0, 10.0}
+ for i, g := range gammas {
+ draw.Draw(mask, mask.Bounds(), image.Transparent, image.ZP, draw.Src)
+ r.Rasterize(raster.NewGammaCorrectionPainter(painter, g))
+ x, y := 50*i+25, 25
+ draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.White, image.ZP, mask, image.ZP, draw.Over)
+ y += 100
+ draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.Black, image.ZP, mask, image.ZP, draw.Over)
+ }
+
+ // 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/raster/main.go b/vendor/github.com/golang/freetype/example/raster/main.go
new file mode 100644
index 000000000..3e572e1c5
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/raster/main.go
@@ -0,0 +1,185 @@
+// Copyright 2010 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 example
+//
+// 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 or "go
+// install -tags=example" to install 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"
+)
+
+type node struct {
+ x, y, degree int
+}
+
+// These contours "outside" and "inside" are from the 'A' glyph from the Droid
+// Serif Regular font.
+
+var outside = []node{
+ node{414, 489, 1},
+ node{336, 274, 2},
+ node{327, 250, 0},
+ node{322, 226, 2},
+ node{317, 203, 0},
+ node{317, 186, 2},
+ node{317, 134, 0},
+ node{350, 110, 2},
+ node{384, 86, 0},
+ node{453, 86, 1},
+ node{500, 86, 1},
+ node{500, 0, 1},
+ node{0, 0, 1},
+ node{0, 86, 1},
+ node{39, 86, 2},
+ node{69, 86, 0},
+ node{90, 92, 2},
+ node{111, 99, 0},
+ node{128, 117, 2},
+ node{145, 135, 0},
+ node{160, 166, 2},
+ node{176, 197, 0},
+ node{195, 246, 1},
+ node{649, 1462, 1},
+ node{809, 1462, 1},
+ node{1272, 195, 2},
+ node{1284, 163, 0},
+ node{1296, 142, 2},
+ node{1309, 121, 0},
+ node{1326, 108, 2},
+ node{1343, 96, 0},
+ node{1365, 91, 2},
+ node{1387, 86, 0},
+ node{1417, 86, 1},
+ node{1444, 86, 1},
+ node{1444, 0, 1},
+ node{881, 0, 1},
+ node{881, 86, 1},
+ node{928, 86, 2},
+ node{1051, 86, 0},
+ node{1051, 184, 2},
+ node{1051, 201, 0},
+ node{1046, 219, 2},
+ node{1042, 237, 0},
+ node{1034, 260, 1},
+ node{952, 489, 1},
+ node{414, 489, -1},
+}
+
+var inside = []node{
+ node{686, 1274, 1},
+ node{453, 592, 1},
+ node{915, 592, 1},
+ node{686, 1274, -1},
+}
+
+func p(n node) fixed.Point26_6 {
+ x, y := 20+n.x/4, 380-n.y/4
+ return fixed.Point26_6{
+ X: fixed.Int26_6(x << 6),
+ Y: fixed.Int26_6(y << 6),
+ }
+}
+
+func contour(r *raster.Rasterizer, ns []node) {
+ if len(ns) == 0 {
+ return
+ }
+ i := 0
+ r.Start(p(ns[i]))
+ for {
+ switch ns[i].degree {
+ case -1:
+ // -1 signifies end-of-contour.
+ return
+ case 1:
+ i += 1
+ r.Add1(p(ns[i]))
+ case 2:
+ i += 2
+ r.Add2(p(ns[i-1]), p(ns[i]))
+ default:
+ panic("bad degree")
+ }
+ }
+}
+
+func showNodes(m *image.RGBA, ns []node) {
+ for _, n := range ns {
+ p := p(n)
+ x, y := int(p.X)/64, int(p.Y)/64
+ if !(image.Point{x, y}).In(m.Bounds()) {
+ continue
+ }
+ var c color.Color
+ switch n.degree {
+ case 0:
+ c = color.RGBA{0, 255, 255, 255}
+ case 1:
+ c = color.RGBA{255, 0, 0, 255}
+ case 2:
+ c = color.RGBA{255, 0, 0, 255}
+ }
+ if c != nil {
+ m.Set(x, y, c)
+ }
+ }
+}
+
+func main() {
+ // Rasterize the contours to a mask image.
+ const (
+ w = 400
+ h = 400
+ )
+ r := raster.NewRasterizer(w, h)
+ contour(r, outside)
+ contour(r, inside)
+ mask := image.NewAlpha(image.Rect(0, 0, w, h))
+ p := raster.NewAlphaSrcPainter(mask)
+ r.Rasterize(p)
+
+ // Draw the mask image (in gray) onto an RGBA image.
+ rgba := image.NewRGBA(image.Rect(0, 0, w, h))
+ gray := image.NewUniform(color.Alpha{0x1f})
+ draw.Draw(rgba, rgba.Bounds(), image.Black, image.ZP, draw.Src)
+ draw.DrawMask(rgba, rgba.Bounds(), gray, image.ZP, mask, image.ZP, draw.Over)
+ showNodes(rgba, outside)
+ showNodes(rgba, inside)
+
+ // 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/round/main.go b/vendor/github.com/golang/freetype/example/round/main.go
new file mode 100644
index 000000000..2920e8335
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/round/main.go
@@ -0,0 +1,110 @@
+// Copyright 2010 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 example
+//
+// 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 or "go
+// install -tags=example" to install it.
+
+// This program visualizes the quadratic approximation to the circle, used to
+// implement round joins when stroking paths. The approximation is used in the
+// stroking code for arcs between 0 and 45 degrees, but is visualized here
+// between 0 and 90 degrees. The discrepancy between the approximation and the
+// true circle is clearly visible at angles above 65 degrees.
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "image"
+ "image/color"
+ "image/draw"
+ "image/png"
+ "log"
+ "math"
+ "os"
+
+ "github.com/golang/freetype/raster"
+ "golang.org/x/image/math/fixed"
+)
+
+// pDot returns the dot product p·q.
+func pDot(p, q fixed.Point26_6) fixed.Int52_12 {
+ px, py := int64(p.X), int64(p.Y)
+ qx, qy := int64(q.X), int64(q.Y)
+ return fixed.Int52_12(px*qx + py*qy)
+}
+
+func main() {
+ const (
+ n = 17
+ r = 64 * 80
+ )
+ s := fixed.Int26_6(r * math.Sqrt(2) / 2)
+ t := fixed.Int26_6(r * math.Tan(math.Pi/8))
+
+ m := image.NewRGBA(image.Rect(0, 0, 800, 600))
+ draw.Draw(m, m.Bounds(), image.NewUniform(color.RGBA{63, 63, 63, 255}), image.ZP, draw.Src)
+ mp := raster.NewRGBAPainter(m)
+ mp.SetColor(image.Black)
+ z := raster.NewRasterizer(800, 600)
+
+ for i := 0; i < n; i++ {
+ cx := fixed.Int26_6(6400 + 12800*(i%4))
+ cy := fixed.Int26_6(640 + 8000*(i/4))
+ c := fixed.Point26_6{X: cx, Y: cy}
+ theta := math.Pi * (0.5 + 0.5*float64(i)/(n-1))
+ dx := fixed.Int26_6(r * math.Cos(theta))
+ dy := fixed.Int26_6(r * math.Sin(theta))
+ d := fixed.Point26_6{X: dx, Y: dy}
+ // Draw a quarter-circle approximated by two quadratic segments,
+ // with each segment spanning 45 degrees.
+ z.Start(c)
+ z.Add1(c.Add(fixed.Point26_6{X: r, Y: 0}))
+ z.Add2(c.Add(fixed.Point26_6{X: r, Y: t}), c.Add(fixed.Point26_6{X: s, Y: s}))
+ z.Add2(c.Add(fixed.Point26_6{X: t, Y: r}), c.Add(fixed.Point26_6{X: 0, Y: r}))
+ // Add another quadratic segment whose angle ranges between 0 and 90
+ // degrees. For an explanation of the magic constants 128, 150, 181 and
+ // 256, read the comments in the freetype/raster package.
+ dot := 256 * pDot(d, fixed.Point26_6{X: 0, Y: r}) / (r * r)
+ multiple := fixed.Int26_6(150-(150-128)*(dot-181)/(256-181)) >> 2
+ z.Add2(c.Add(fixed.Point26_6{X: dx, Y: r + dy}.Mul(multiple)), c.Add(d))
+ // Close the curve.
+ z.Add1(c)
+ }
+ z.Rasterize(mp)
+
+ for i := 0; i < n; i++ {
+ cx := fixed.Int26_6(6400 + 12800*(i%4))
+ cy := fixed.Int26_6(640 + 8000*(i/4))
+ for j := 0; j < n; j++ {
+ theta := math.Pi * float64(j) / (n - 1)
+ dx := fixed.Int26_6(r * math.Cos(theta))
+ dy := fixed.Int26_6(r * math.Sin(theta))
+ m.Set(int((cx+dx)/64), int((cy+dy)/64), color.RGBA{255, 255, 0, 255})
+ }
+ }
+
+ // 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, m)
+ 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.")
+}
diff --git a/vendor/github.com/golang/freetype/example/truetype/main.go b/vendor/github.com/golang/freetype/example/truetype/main.go
new file mode 100644
index 000000000..e7db2d0ca
--- /dev/null
+++ b/vendor/github.com/golang/freetype/example/truetype/main.go
@@ -0,0 +1,89 @@
+// Copyright 2010 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 example
+//
+// 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 or "go
+// install -tags=example" to install it.
+
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+
+ "github.com/golang/freetype/truetype"
+ "golang.org/x/image/font"
+ "golang.org/x/image/math/fixed"
+)
+
+var fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
+
+func printBounds(b fixed.Rectangle26_6) {
+ fmt.Printf("Min.X:%d Min.Y:%d Max.X:%d Max.Y:%d\n", b.Min.X, b.Min.Y, b.Max.X, b.Max.Y)
+}
+
+func printGlyph(g *truetype.GlyphBuf) {
+ printBounds(g.Bounds)
+ fmt.Print("Points:\n---\n")
+ e := 0
+ for i, p := range g.Points {
+ fmt.Printf("%4d, %4d", p.X, p.Y)
+ if p.Flags&0x01 != 0 {
+ fmt.Print(" on\n")
+ } else {
+ fmt.Print(" off\n")
+ }
+ if i+1 == int(g.Ends[e]) {
+ fmt.Print("---\n")
+ e++
+ }
+ }
+}
+
+func main() {
+ flag.Parse()
+ fmt.Printf("Loading fontfile %q\n", *fontfile)
+ b, err := ioutil.ReadFile(*fontfile)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ f, err := truetype.Parse(b)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ fupe := fixed.Int26_6(f.FUnitsPerEm())
+ printBounds(f.Bounds(fupe))
+ fmt.Printf("FUnitsPerEm:%d\n\n", fupe)
+
+ c0, c1 := 'A', 'V'
+
+ i0 := f.Index(c0)
+ hm := f.HMetric(fupe, i0)
+ g := &truetype.GlyphBuf{}
+ err = g.Load(f, fupe, i0, font.HintingNone)
+ if err != nil {
+ log.Println(err)
+ return
+ }
+ fmt.Printf("'%c' glyph\n", c0)
+ fmt.Printf("AdvanceWidth:%d LeftSideBearing:%d\n", hm.AdvanceWidth, hm.LeftSideBearing)
+ printGlyph(g)
+ i1 := f.Index(c1)
+ fmt.Printf("\n'%c', '%c' Kern:%d\n", c0, c1, f.Kern(fupe, i0, i1))
+
+ fmt.Printf("\nThe numbers above are in FUnits.\n" +
+ "The numbers below are in 26.6 fixed point pixels, at 12pt and 72dpi.\n\n")
+ a := truetype.NewFace(f, &truetype.Options{
+ Size: 12,
+ DPI: 72,
+ })
+ fmt.Printf("%#v\n", a.Metrics())
+}