1
2
3
4
5
6
7
8 package main
9
10 import (
11 "bytes"
12 "fmt"
13 "go/format"
14 "image"
15 "image/draw"
16 "io/ioutil"
17 "log"
18 "path"
19 "path/filepath"
20
21 "golang.org/x/image/font"
22 "golang.org/x/image/font/plan9font"
23 "golang.org/x/image/math/fixed"
24 )
25
26 func main() {
27
28
29 const nGlyphs = 95 + 1
30
31
32
33 const width, height, ascent = 7 - 1, 13, 11
34
35 readFile := func(name string) ([]byte, error) {
36 return ioutil.ReadFile(filepath.FromSlash(path.Join("../testdata/fixed", name)))
37 }
38 fontData, err := readFile("unicode.7x13.font")
39 if err != nil {
40 log.Fatalf("readFile: %v", err)
41 }
42 face, err := plan9font.ParseFont(fontData, readFile)
43 if err != nil {
44 log.Fatalf("plan9font.ParseFont: %v", err)
45 }
46
47 dst := image.NewRGBA(image.Rect(0, 0, width, nGlyphs*height))
48 draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
49 d := &font.Drawer{
50 Dst: dst,
51 Src: image.White,
52 Face: face,
53 }
54 for i := 0; i < nGlyphs; i++ {
55 r := '\ufffd'
56 if i < nGlyphs-1 {
57 r = 0x20 + rune(i)
58 }
59 d.Dot = fixed.P(0, height*i+ascent)
60 d.DrawString(string(r))
61 }
62
63 w := bytes.NewBuffer(nil)
64 w.WriteString(preamble)
65 fmt.Fprintf(w, "// mask7x13 contains %d %d×%d glyphs in %d Pix bytes.\n", nGlyphs, width, height, nGlyphs*width*height)
66 fmt.Fprintf(w, "var mask7x13 = &image.Alpha{\n")
67 fmt.Fprintf(w, " Stride: %d,\n", width)
68 fmt.Fprintf(w, " Rect: image.Rectangle{Max: image.Point{%d, %d*%d}},\n", width, nGlyphs, height)
69 fmt.Fprintf(w, " Pix: []byte{\n")
70 b := dst.Bounds()
71 for y := b.Min.Y; y < b.Max.Y; y++ {
72 if y%height == 0 {
73 if y != 0 {
74 w.WriteByte('\n')
75 }
76 i := y / height
77 if i < nGlyphs-1 {
78 i += 0x20
79 fmt.Fprintf(w, "// %#2x %q\n", i, rune(i))
80 } else {
81 fmt.Fprintf(w, "// U+FFFD REPLACEMENT CHARACTER\n")
82 }
83 }
84
85 for x := b.Min.X; x < b.Max.X; x++ {
86 if dst.RGBAAt(x, y).R > 0 {
87 w.WriteString("0xff,")
88 } else {
89 w.WriteString("0x00,")
90 }
91 }
92 w.WriteByte('\n')
93 }
94 w.WriteString("},\n}\n")
95
96 fmted, err := format.Source(w.Bytes())
97 if err != nil {
98 log.Fatalf("format.Source: %v", err)
99 }
100 if err := ioutil.WriteFile("data.go", fmted, 0644); err != nil {
101 log.Fatalf("ioutil.WriteFile: %v", err)
102 }
103 }
104
105 const preamble = `// generated by go generate; DO NOT EDIT.
106
107 package basicfont
108
109 // This data is derived from files in the font/fixed directory of the Plan 9
110 // Port source code (https://github.com/9fans/plan9port) which were originally
111 // based on the public domain X11 misc-fixed font files.
112
113 import "image"
114
115 `
116
View as plain text