diff options
Diffstat (limited to 'Godeps/_workspace/src/code.google.com/p/freetype-go/example')
5 files changed, 569 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/code.google.com/p/freetype-go/example/freetype/main.go b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/freetype/main.go new file mode 100644 index 000000000..83b8c3e5e --- /dev/null +++ b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/freetype/main.go @@ -0,0 +1,143 @@ +// 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. + +package main + +import ( + "bufio" + "flag" + "fmt" + "image" + "image/color" + "image/draw" + "image/png" + "io/ioutil" + "log" + "os" + + "code.google.com/p/freetype-go/freetype" +) + +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 + } + font, 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(font) + c.SetFontSize(*size) + c.SetClip(rgba.Bounds()) + c.SetDst(rgba) + c.SetSrc(fg) + switch *hinting { + default: + c.SetHinting(freetype.NoHinting) + case "full": + c.SetHinting(freetype.FullHinting) + } + + // 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.PointToFix32(*size)>>8)) + for _, s := range text { + _, err = c.DrawString(s, pt) + if err != nil { + log.Println(err) + return + } + pt.Y += c.PointToFix32(*size * *spacing) + } + + // Save that RGBA image to disk. + f, err := os.Create("out.png") + if err != nil { + log.Println(err) + os.Exit(1) + } + defer f.Close() + b := bufio.NewWriter(f) + 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/Godeps/_workspace/src/code.google.com/p/freetype-go/example/gamma/main.go b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/gamma/main.go new file mode 100644 index 000000000..4bf7e3309 --- /dev/null +++ b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/gamma/main.go @@ -0,0 +1,79 @@ +// 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. + +package main + +import ( + "bufio" + "fmt" + "image" + "image/draw" + "image/png" + "log" + "os" + + "code.google.com/p/freetype-go/freetype/raster" +) + +func p(x, y int) raster.Point { + return raster.Point{ + X: raster.Fix32(x * 256), + Y: raster.Fix32(y * 256), + } +} + +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. + f, err := os.Create("out.png") + if err != nil { + log.Println(err) + os.Exit(1) + } + defer f.Close() + b := bufio.NewWriter(f) + 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/Godeps/_workspace/src/code.google.com/p/freetype-go/example/raster/main.go b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/raster/main.go new file mode 100644 index 000000000..5e6c9815d --- /dev/null +++ b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/raster/main.go @@ -0,0 +1,178 @@ +// 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. + +package main + +import ( + "bufio" + "fmt" + "image" + "image/color" + "image/draw" + "image/png" + "log" + "os" + + "code.google.com/p/freetype-go/freetype/raster" +) + +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) raster.Point { + x, y := 20+n.x/4, 380-n.y/4 + return raster.Point{ + X: raster.Fix32(x * 256), + Y: raster.Fix32(y * 256), + } +} + +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)/256, int(p.Y)/256 + 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. + f, err := os.Create("out.png") + if err != nil { + log.Println(err) + os.Exit(1) + } + defer f.Close() + b := bufio.NewWriter(f) + 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/Godeps/_workspace/src/code.google.com/p/freetype-go/example/round/main.go b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/round/main.go new file mode 100644 index 000000000..e2435f9a2 --- /dev/null +++ b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/round/main.go @@ -0,0 +1,96 @@ +// 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. + +// 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" + + "code.google.com/p/freetype-go/freetype/raster" +) + +func main() { + const ( + n = 17 + r = 256 * 80 + ) + s := raster.Fix32(r * math.Sqrt(2) / 2) + t := raster.Fix32(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 := raster.Fix32(25600 + 51200*(i%4)) + cy := raster.Fix32(2560 + 32000*(i/4)) + c := raster.Point{X: cx, Y: cy} + theta := math.Pi * (0.5 + 0.5*float64(i)/(n-1)) + dx := raster.Fix32(r * math.Cos(theta)) + dy := raster.Fix32(r * math.Sin(theta)) + d := raster.Point{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(raster.Point{X: r, Y: 0})) + z.Add2(c.Add(raster.Point{X: r, Y: t}), c.Add(raster.Point{X: s, Y: s})) + z.Add2(c.Add(raster.Point{X: t, Y: r}), c.Add(raster.Point{X: 0, Y: r})) + // Add another quadratic segment whose angle ranges between 0 and 90 degrees. + // For an explanation of the magic constants 22, 150, 181 and 256, read the + // comments in the freetype/raster package. + dot := 256 * d.Dot(raster.Point{X: 0, Y: r}) / (r * r) + multiple := raster.Fix32(150 - 22*(dot-181)/(256-181)) + z.Add2(c.Add(raster.Point{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 := raster.Fix32(25600 + 51200*(i%4)) + cy := raster.Fix32(2560 + 32000*(i/4)) + for j := 0; j < n; j++ { + theta := math.Pi * float64(j) / (n - 1) + dx := raster.Fix32(r * math.Cos(theta)) + dy := raster.Fix32(r * math.Sin(theta)) + m.Set(int((cx+dx)/256), int((cy+dy)/256), color.RGBA{255, 255, 0, 255}) + } + } + + // Save that RGBA image to disk. + f, err := os.Create("out.png") + if err != nil { + log.Println(err) + os.Exit(1) + } + defer f.Close() + b := bufio.NewWriter(f) + 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/Godeps/_workspace/src/code.google.com/p/freetype-go/example/truetype/main.go b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/truetype/main.go new file mode 100644 index 000000000..747694b61 --- /dev/null +++ b/Godeps/_workspace/src/code.google.com/p/freetype-go/example/truetype/main.go @@ -0,0 +1,73 @@ +// 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. + +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + + "code.google.com/p/freetype-go/freetype/truetype" +) + +var fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font") + +func printBounds(b truetype.Bounds) { + fmt.Printf("XMin:%d YMin:%d XMax:%d YMax:%d\n", b.XMin, b.YMin, b.XMax, b.YMax) +} + +func printGlyph(g *truetype.GlyphBuf) { + printBounds(g.B) + fmt.Print("Points:\n---\n") + e := 0 + for i, p := range g.Point { + 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.End[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 + } + font, err := truetype.Parse(b) + if err != nil { + log.Println(err) + return + } + fupe := font.FUnitsPerEm() + printBounds(font.Bounds(fupe)) + fmt.Printf("FUnitsPerEm:%d\n\n", fupe) + + c0, c1 := 'A', 'V' + + i0 := font.Index(c0) + hm := font.HMetric(fupe, i0) + g := truetype.NewGlyphBuf() + err = g.Load(font, fupe, i0, truetype.NoHinting) + 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 := font.Index(c1) + fmt.Printf("\n'%c', '%c' Kerning:%d\n", c0, c1, font.Kerning(fupe, i0, i1)) +} |