...
1
2
3
4
5 package plan9font_test
6
7 import (
8 "image"
9 "image/draw"
10 "io/ioutil"
11 "log"
12 "os"
13 "path"
14 "path/filepath"
15
16 "golang.org/x/image/font"
17 "golang.org/x/image/font/plan9font"
18 "golang.org/x/image/math/fixed"
19 )
20
21 func ExampleParseFont() {
22 readFile := func(name string) ([]byte, error) {
23 return ioutil.ReadFile(filepath.FromSlash(path.Join("../testdata/fixed", name)))
24 }
25 fontData, err := readFile("unicode.7x13.font")
26 if err != nil {
27 log.Fatal(err)
28 }
29 face, err := plan9font.ParseFont(fontData, readFile)
30 if err != nil {
31 log.Fatal(err)
32 }
33 ascent := face.Metrics().Ascent.Ceil()
34
35 dst := image.NewRGBA(image.Rect(0, 0, 4*7, 13))
36 draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
37 d := &font.Drawer{
38 Dst: dst,
39 Src: image.White,
40 Face: face,
41 Dot: fixed.P(0, ascent),
42 }
43
44
45
46
47
48
49
50
51
52
53
54 d.DrawString("SΣ∫さ")
55
56
57 var out []byte
58 b := dst.Bounds()
59 for y := b.Min.Y; y < b.Max.Y; y++ {
60 out = append(out, '0'+byte(y%10), ' ')
61 for x := b.Min.X; x < b.Max.X; x++ {
62 if dst.RGBAAt(x, y).R > 0 {
63 out = append(out, 'X')
64 } else {
65 out = append(out, '.')
66 }
67 }
68
69
70
71 if y == ascent-1 {
72 out = append(out, '_')
73 }
74 out = append(out, '\n')
75 }
76 os.Stdout.Write(out)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 }
93
View as plain text