/
main.go
104 lines (91 loc) · 2.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2016 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
// this will run this generator if go generate is called on this directory
//go:generate go run main.go logmain.go
package main
// This program generates the subdirectories of Go packages that contain []byte versions of the TrueType font files
// under ./ttfs.
//
// Currently, "go run gen.go" needs to be run manually. This isn't done by the usual "go generate" mechanism as there
// isn't any other Go code in this directory (excluding sub-directories) to attach a "go:generate" line to.
//
// In any case, code generation should only need to happen when the underlying TTF files change, which isn't expected to
// happen frequently.
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const suffix = ".ttf"
func main() {
ttfs, e := os.Open("ttfs")
if e != nil {
F.Ln(e)
}
defer func() {
if e = ttfs.Close(); E.Chk(e) {
}
}()
infos, e := ttfs.Readdir(-1)
if e != nil {
F.Ln(e)
}
for _, info := range infos {
ttfName := info.Name()
if !strings.HasSuffix(ttfName, suffix) {
continue
}
do(ttfName)
}
}
func do(ttfName string) {
fontName := fontName(ttfName)
pkgName := pkgName(ttfName)
if e := os.Mkdir(pkgName, 0777); e != nil && !os.IsExist(e) {
F.Ln(e)
}
src, e := ioutil.ReadFile(filepath.Join("ttfs", ttfName))
if e != nil {
F.Ln(e)
}
// desc := "a proportional-width, sans-serif"
// if strings.Contains(ttfName, "Mono") {
// desc = "a fixed-width, slab-serif"
// }
b := new(bytes.Buffer)
_, _ = fmt.Fprintf(b, "// generated by go run github.com/p9c/gel/fonts; DO NOT EDIT\n\n")
_, _ = fmt.Fprintf(b, "package %s\n\n", pkgName)
_, _ = fmt.Fprintf(b, "// TTF is the data for the %q TrueType font.\n", fontName)
_, _ = fmt.Fprintf(b, "var TTF = []byte{")
for i, x := range src {
if i&15 == 0 {
b.WriteByte('\n')
}
_, _ = fmt.Fprintf(b, "%#02x,", x)
}
_, _ = fmt.Fprintf(b, "\n}\n")
dst, e := format.Source(b.Bytes())
if e != nil {
F.Ln(e)
}
if e := ioutil.WriteFile(filepath.Join(pkgName, "data.go"), dst, 0666); E.Chk(e) {
F.Ln(e)
}
}
// fontName maps "Go-Regular.ttf" to "Go Regular".
func fontName(ttfName string) string {
s := ttfName[:len(ttfName)-len(suffix)]
s = strings.Replace(s, "-", " ", -1)
return s
}
// pkgName maps "Go-Regular.ttf" to "goregular".
func pkgName(ttfName string) string {
s := ttfName[:len(ttfName)-len(suffix)]
s = strings.Replace(s, "-", "", -1)
s = strings.ToLower(s)
return s
}