Debian Codegraph @

example/raster/main.go

raw symbols SCIP
1 // Copyright 2010 The Freetype-Go Authors. All rights reserved.
2 // Use of this source code is governed by your choice of either the
3 // FreeType License or the GNU General Public License version 2 (or
4 // any later version), both of which can be found in the LICENSE file.
5
6 // +build example
7 //
8 // This build tag means that "go install github.com/golang/freetype/..."
9 // doesn't install this example program. Use "go run main.go" to run it or "go
10 // install -tags=example" to install it.
11
12 package main
13
14 import (
15 "bufio"
16 "fmt"
17 "image"
18 "image/color"
19 "image/draw"
20 "image/png"
21 "log"
22 "os"
23
24 "github.com/golang/freetype/raster"
25 "golang.org/x/image/math/fixed"
26 )
27
28 type node struct {
29 x, y, degree int
30 }
31
32 // These contours "outside" and "inside" are from the 'A' glyph from the Droid
33 // Serif Regular font.
34
35 var outside = []node{
36 node{414, 489, 1},
37 node{336, 274, 2},
38 node{327, 250, 0},
39 node{322, 226, 2},
40 node{317, 203, 0},
41 node{317, 186, 2},
42 node{317, 134, 0},
43 node{350, 110, 2},
44 node{384, 86, 0},
45 node{453, 86, 1},
46 node{500, 86, 1},
47 node{500, 0, 1},
48 node{0, 0, 1},
49 node{0, 86, 1},
50 node{39, 86, 2},
51 node{69, 86, 0},
52 node{90, 92, 2},
53 node{111, 99, 0},
54 node{128, 117, 2},
55 node{145, 135, 0},
56 node{160, 166, 2},
57 node{176, 197, 0},
58 node{195, 246, 1},
59 node{649, 1462, 1},
60 node{809, 1462, 1},
61 node{1272, 195, 2},
62 node{1284, 163, 0},
63 node{1296, 142, 2},
64 node{1309, 121, 0},
65 node{1326, 108, 2},
66 node{1343, 96, 0},
67 node{1365, 91, 2},
68 node{1387, 86, 0},
69 node{1417, 86, 1},
70 node{1444, 86, 1},
71 node{1444, 0, 1},
72 node{881, 0, 1},
73 node{881, 86, 1},
74 node{928, 86, 2},
75 node{1051, 86, 0},
76 node{1051, 184, 2},
77 node{1051, 201, 0},
78 node{1046, 219, 2},
79 node{1042, 237, 0},
80 node{1034, 260, 1},
81 node{952, 489, 1},
82 node{414, 489, -1},
83 }
84
85 var inside = []node{
86 node{686, 1274, 1},
87 node{453, 592, 1},
88 node{915, 592, 1},
89 node{686, 1274, -1},
90 }
91
92 func p(n node) fixed.Point26_6 {
93 x, y := 20+n.x/4, 380-n.y/4
94 return fixed.Point26_6{
95 X: fixed.Int26_6(x << 6),
96 Y: fixed.Int26_6(y << 6),
97 }
98 }
99
100 func contour(r *raster.Rasterizer, ns []node) {
101 if len(ns) == 0 {
102 return
103 }
104 i := 0
105 r.Start(p(ns[i]))
106 for {
107 switch ns[i].degree {
108 case -1:
109 // -1 signifies end-of-contour.
110 return
111 case 1:
112 i += 1
113 r.Add1(p(ns[i]))
114 case 2:
115 i += 2
116 r.Add2(p(ns[i-1]), p(ns[i]))
117 default:
118 panic("bad degree")
119 }
120 }
121 }
122
123 func showNodes(m *image.RGBA, ns []node) {
124 for _, n := range ns {
125 p := p(n)
126 x, y := int(p.X)/64, int(p.Y)/64
127 if !(image.Point{x, y}).In(m.Bounds()) {
128 continue
129 }
130 var c color.Color
131 switch n.degree {
132 case 0:
133 c = color.RGBA{0, 255, 255, 255}
134 case 1:
135 c = color.RGBA{255, 0, 0, 255}
136 case 2:
137 c = color.RGBA{255, 0, 0, 255}
138 }
139 if c != nil {
140 m.Set(x, y, c)
141 }
142 }
143 }
144
145 func main() {
146 // Rasterize the contours to a mask image.
147 const (
148 w = 400
149 h = 400
150 )
151 r := raster.NewRasterizer(w, h)
152 contour(r, outside)
153 contour(r, inside)
154 mask := image.NewAlpha(image.Rect(0, 0, w, h))
155 p := raster.NewAlphaSrcPainter(mask)
156 r.Rasterize(p)
157
158 // Draw the mask image (in gray) onto an RGBA image.
159 rgba := image.NewRGBA(image.Rect(0, 0, w, h))
160 gray := image.NewUniform(color.Alpha{0x1f})
161 draw.Draw(rgba, rgba.Bounds(), image.Black, image.ZP, draw.Src)
162 draw.DrawMask(rgba, rgba.Bounds(), gray, image.ZP, mask, image.ZP, draw.Over)
163 showNodes(rgba, outside)
164 showNodes(rgba, inside)
165
166 // Save that RGBA image to disk.
167 outFile, err := os.Create("out.png")
168 if err != nil {
169 log.Println(err)
170 os.Exit(1)
171 }
172 defer outFile.Close()
173 b := bufio.NewWriter(outFile)
174 err = png.Encode(b, rgba)
175 if err != nil {
176 log.Println(err)
177 os.Exit(1)
178 }
179 err = b.Flush()
180 if err != nil {
181 log.Println(err)
182 os.Exit(1)
183 }
184 fmt.Println("Wrote out.png OK.")
185 }