-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-fonts.go
189 lines (159 loc) · 4.32 KB
/
gen-fonts.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright ©2020 The go-fonts Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// 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.
//go:build ignore
// +build ignore
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"flag"
"fmt"
"go/format"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
func main() {
log.SetPrefix("liberation-gen: ")
log.SetFlags(0)
var (
src = flag.String(
"src",
"https://github.com/liberationfonts/liberation-fonts/files/7261483/liberation-fonts-2.1.5.tar.gz",
"remote tar-gz file holding TTF files for Liberation fonts",
)
)
flag.Parse()
tmp, err := os.MkdirTemp("", "go-fonts-liberation-")
if err != nil {
log.Fatalf("could not create tmp dir: %+v", err)
}
defer os.RemoveAll(tmp)
var (
fname string
)
switch {
case strings.HasPrefix(*src, "http://"),
strings.HasPrefix(*src, "https://"):
fname, err = fetch(tmp, *src)
if err != nil {
log.Fatalf("could not fetch Liberation sources: %+v", err)
}
default:
fname = *src
}
f, err := os.Open(fname)
if err != nil {
log.Fatalf("could not open tar file: %+v", err)
}
defer f.Close()
gr, err := gzip.NewReader(f)
if err != nil {
log.Fatalf("could not open gzip file: %+v", err)
}
defer gr.Close()
log.Printf("inspecting tar file...")
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("could not read tar archive: %+v", err)
}
if !strings.HasSuffix(hdr.Name, suffix) {
continue
}
err = gen(path.Base(hdr.Name), tr)
if err != nil {
log.Fatalf("could not generate font: %+v", err)
}
}
}
func fetch(tmp, src string) (string, error) {
resp, err := http.Get(src)
if err != nil {
return "", fmt.Errorf("could not GET %q: %w", src, err)
}
defer resp.Body.Close()
f, err := os.Create(path.Join(tmp, "liberation.tar.gz"))
if err != nil {
return "", fmt.Errorf("could not create zip file: %w", err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
return "", fmt.Errorf("could not copy zip file: %w", err)
}
err = f.Close()
if err != nil {
return "", fmt.Errorf("could not save zip file: %w", err)
}
return f.Name(), nil
}
func gen(fname string, tr *tar.Reader) error {
log.Printf("generating fonts package for %q...", fname)
raw := new(bytes.Buffer)
_, err := io.Copy(raw, tr)
if err != nil {
return fmt.Errorf("could not download TTF file: %w", err)
}
err = do(fname, raw.Bytes())
if err != nil {
return fmt.Errorf("could not generate package for %q: %w", fname, err)
}
return nil
}
func do(ttfName string, src []byte) error {
fontName := fontName(ttfName)
pkgName := pkgName(ttfName)
if err := os.Mkdir(pkgName, 0777); err != nil && !os.IsExist(err) {
return fmt.Errorf("could not create package dir %q: %w", pkgName, err)
}
b := new(bytes.Buffer)
fmt.Fprintf(b, "// generated by go run gen-fonts.go; DO NOT EDIT\n\n")
fmt.Fprintf(b, "// Package %s provides the %q TrueType font\n", pkgName, fontName)
fmt.Fprintf(b, "// from the Liberation font family.\n")
fmt.Fprintf(b, "package %[1]s // import \"github.com/go-fonts/liberation/%[1]s\"\n\n", pkgName)
fmt.Fprintf(b, "import _ \"embed\"\n")
fmt.Fprintf(b, "// TTF is the data for the %q TrueType font.\n", fontName)
fmt.Fprintf(b, "//\n//go:embed %s\n", ttfName)
fmt.Fprintf(b, "var TTF []byte\n")
dst, err := format.Source(b.Bytes())
if err != nil {
return fmt.Errorf("could not format source: %w", err)
}
err = os.WriteFile(filepath.Join(pkgName, "data.go"), dst, 0666)
if err != nil {
return fmt.Errorf("could not write package source file: %w", err)
}
err = os.WriteFile(filepath.Join(pkgName, ttfName), src, 0666)
if err != nil {
return fmt.Errorf("could not write package TTF file: %w", err)
}
return nil
}
const suffix = ".ttf"
// 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
}