-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
496 lines (446 loc) · 11.9 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Command fv is a command-line font viewer using terminal graphics (Sixel,
// iTerm, Kitty).
package main
import (
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"unicode"
"github.com/kenshaw/colors"
"github.com/kenshaw/rasterm"
"github.com/spf13/pflag"
"github.com/tdewolff/canvas"
"github.com/tdewolff/canvas/renderers/rasterizer"
fontpkg "github.com/tdewolff/font"
"github.com/xo/ox"
_ "github.com/xo/ox/color"
)
func main() {
args := &Args{}
ox.RunContext(
context.Background(),
ox.Exec(run(os.Stdout, args)),
ox.Usage("fv", "a command-line font view using terminal graphics"),
ox.Defaults(),
ox.From(args),
)
}
func run(w io.Writer, args *Args) func(context.Context, []string) error {
return func(ctx context.Context, cliargs []string) error {
// style, variant := canvas.FontRegular, canvas.FontNormal
sysfonts, err := fontpkg.FindSystemFonts(fontpkg.DefaultFontDirs())
if err != nil {
return err
}
switch hasArgs := len(cliargs) != 0; {
case args.All && hasArgs:
return errors.New("--all does not take any args")
case args.List && hasArgs:
return errors.New("--list does not take any args")
case args.Match && !hasArgs:
return errors.New("--match requires one or more args")
case args.All && args.List,
args.All && args.Match,
args.Match && args.List:
return errors.New("--all, --list, and --match are exclusive")
}
f := do
switch {
case args.All:
f = doAll
case args.List:
f = doList
case args.Match:
f = doMatch
}
return f(w, sysfonts, args, cliargs)
}
}
type Args struct {
All bool `ox:"show all system fonts"`
List bool `ox:"list system fonts"`
Match bool `ox:"match system fonts"`
Fg *colors.Color `ox:"foreground color,default:black"`
Bg *colors.Color `ox:"background color,default:white"`
Size int `ox:"font size,default:48"`
Dpi int `ox:"dpi,default:100"`
Margin int `ox:"margin,default:5"`
Style canvas.FontStyle `ox:"font style"`
Variant canvas.FontVariant `ox:"font variant"`
Text string `ox:"display text"`
once sync.Once
tpl *template.Template
}
func (args *Args) Template() (*template.Template, error) {
var err error
args.once.Do(func() {
s := args.Text
if s == "" {
s = string(textTpl)
}
args.tpl, err = template.New("").Funcs(map[string]interface{}{
"size": func(size int) string {
return fmt.Sprintf("\x00%d\x00", size)
},
"inc": func(a, b int) int {
return a + b
},
}).Parse(s)
})
switch {
case err != nil:
return nil, err
case args.tpl == nil:
return nil, errors.New("invalid template state")
}
return args.tpl, nil
}
// do renders the specified font queries to w.
func do(w io.Writer, sysfonts *fontpkg.SystemFonts, args *Args, cliargs []string) error {
if !rasterm.Available() {
return rasterm.ErrTermGraphicsNotAvailable
}
var fonts []*Font
// collect fonts
for i := 0; i < len(cliargs); i++ {
v, err := Open(sysfonts, cliargs[i], args.Style)
if err != nil {
fmt.Fprintf(w, "error: unable to open arg %d: %v\n", i, err)
}
fonts = append(fonts, v...)
}
return render(w, fonts, args, cliargs)
}
// doAll renders all system fonts to w.
func doAll(w io.Writer, sysfonts *fontpkg.SystemFonts, args *Args, cliargs []string) error {
if !rasterm.Available() {
return rasterm.ErrTermGraphicsNotAvailable
}
// collect fonts
var fonts []*Font
for _, family := range slices.Sorted(maps.Keys(sysfonts.Fonts)) {
for _, style := range slices.Sorted(maps.Keys(sysfonts.Fonts[family])) {
fonts = append(fonts, NewFont(sysfonts.Fonts[family][style]))
}
}
return render(w, fonts, args, cliargs)
}
func doList(w io.Writer, sysfonts *fontpkg.SystemFonts, _ *Args, _ []string) error {
for _, family := range slices.Sorted(maps.Keys(sysfonts.Fonts)) {
fmt.Fprintln(w, "---")
fmt.Fprintf(w, "family: %q\n", family)
fmt.Fprintln(w, "styles:")
for _, style := range slices.Sorted(maps.Keys(sysfonts.Fonts[family])) {
fmt.Fprintf(w, " %s: %s\n", style, sysfonts.Fonts[family][style].Filename)
}
}
return nil
}
func doMatch(w io.Writer, sysfonts *fontpkg.SystemFonts, args *Args, cliargs []string) error {
for _, name := range cliargs {
if font := Match(sysfonts, name, args.Style); font != nil {
font.WriteYAML(w)
}
}
return nil
}
func render(w io.Writer, fonts []*Font, args *Args, cliargs []string) error {
tpl, err := args.Template()
if err != nil {
return err
}
for i := 0; i < len(fonts); i++ {
if err := fonts[i].Render(w, tpl, args); err != nil {
fmt.Fprintf(os.Stdout, "%s -- error: %v\n", fonts[i], err)
}
if i != len(fonts)-1 {
fmt.Fprintln(w)
}
}
return nil
}
type TemplateData struct {
Size int
Name string
Style string
SampleText string
}
type Font struct {
Path string
Family string
Name string
Style string
SampleText string
once sync.Once
}
func NewFont(md fontpkg.FontMetadata) *Font {
family := md.Family
if family == "" {
family = titleCase(strings.TrimSuffix(filepath.Base(md.Filename), filepath.Ext(md.Filename)))
}
return &Font{
Path: md.Filename,
Family: family,
Style: md.Style.String(),
}
}
func NewFontForPath(path string) *Font {
return &Font{
Path: path,
Family: titleCase(strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))),
}
}
func Match(sysfonts *fontpkg.SystemFonts, name string, style canvas.FontStyle) *Font {
md, ok := sysfonts.Match(name, fontpkg.ParseStyle(style.String()))
if !ok {
return nil
}
return NewFont(md)
}
// Open opens fonts.
func Open(sysfonts *fontpkg.SystemFonts, name string, style canvas.FontStyle) ([]*Font, error) {
var v []*Font
switch fi, err := os.Stat(name); {
case err == nil && fi.IsDir():
entries, err := os.ReadDir(name)
if err != nil {
return nil, fmt.Errorf("unable to open directory %q: %v", name, err)
}
for _, entry := range entries {
if s := entry.Name(); !entry.IsDir() && extRE.MatchString(s) {
v = append(v, NewFontForPath(filepath.Join(name, s)))
}
}
sort.Slice(v, func(i, j int) bool {
return strings.ToLower(v[i].Family) < strings.ToLower(v[j].Family)
})
case err == nil:
v = append(v, NewFontForPath(name))
default:
if font := Match(sysfonts, name, style); font != nil {
v = append(v, font)
}
}
if len(v) == 0 {
return nil, fmt.Errorf("unable to locate font %q", name)
}
return v, nil
}
var extRE = regexp.MustCompile(`(?i)\.(ttf|ttc|otf|woff|woff2|sfnt)$`)
func (font *Font) BestName() string {
if font.Name != "" {
return font.Name
}
return font.Family
}
func (font *Font) String() string {
name := font.BestName()
if font.Style != "" {
name += " (" + font.Style + ")"
}
return fmt.Sprintf("%q: %s", name, font.Path)
}
func (font *Font) WriteYAML(w io.Writer) {
fmt.Fprintln(w, "---")
fmt.Fprintf(w, "path: %s\n", font.Path)
fmt.Fprintf(w, "family: %q\n", font.BestName())
fmt.Fprintf(w, "style: %q\n", font.Style)
}
func (font *Font) Load(style canvas.FontStyle) (*canvas.FontFamily, error) {
ff := canvas.NewFontFamily(font.Family)
if err := ff.LoadFontFile(font.Path, style); err != nil {
return nil, err
}
font.once.Do(func() {
face := ff.Face(16)
if v := face.Font.SFNT.Name.Get(fontpkg.NameFontFamily); 0 < len(v) {
font.Name = v[0].String()
}
if v := face.Font.SFNT.Name.Get(fontpkg.NameFontSubfamily); 0 < len(v) {
font.Style = fontpkg.ParseStyle(v[0].String()).String()
}
if v := face.Font.SFNT.Name.Get(fontpkg.NameSampleText); 0 < len(v) {
font.SampleText = v[0].String()
}
})
return ff, nil
}
func (font *Font) Render(w io.Writer, tpl *template.Template, args *Args) error {
// load font family
ff, err := font.Load(args.Style)
if err != nil {
return err
}
// generate text
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, TemplateData{
Size: args.Size,
Name: font.BestName(),
Style: font.Style,
SampleText: font.SampleText,
}); err != nil {
return err
}
fmt.Fprintf(w, "%s\n", font)
// create canvas and context
c := canvas.New(100, 100)
ctx := canvas.NewContext(c)
ctx.SetZIndex(1)
ctx.SetFillColor(args.Fg)
// draw text
lines, sizes := breakLines(buf.Bytes(), args.Size)
for i, y := 0, float64(0); i < len(lines); i++ {
face := ff.Face(float64(sizes[i]), args.Fg, args.Style, args.Variant)
txt := canvas.NewTextBox(face, strings.TrimSpace(lines[i]), 0, 0, canvas.Left, canvas.Top, 0, 0)
b := txt.Bounds()
ctx.DrawText(0, y, txt)
y += b.Y0 - b.Y1
}
// fit canvas to context
c.Fit(float64(args.Margin))
// draw background
ctx.SetZIndex(-1)
ctx.SetFillColor(args.Bg)
width, height := ctx.Size()
ctx.DrawPath(0, 0, canvas.Rectangle(width, height))
ctx.Close()
// encode
return rasterm.Encode(w, rasterizer.Draw(
c,
canvas.DPI(float64(args.Dpi)),
canvas.DefaultColorSpace,
))
}
func breakLines(buf []byte, size int) ([]string, []int) {
var lines []string
var sizes []int
for _, line := range bytes.Split(buf, []byte{'\n'}) {
sz := size
if m := sizeRE.FindSubmatch(line); m != nil {
if s, err := strconv.Atoi(string(m[1])); err == nil {
sz = s
}
line = m[2]
}
lines, sizes = append(lines, string(line)), append(sizes, sz)
}
return lines, sizes
}
var sizeRE = regexp.MustCompile(`^\x00([0-9]+)\x00(.*)$`)
func titleCase(name string) string {
var prev rune
var s []rune
r := []rune(name)
for i, c := range r {
switch {
case unicode.IsLower(prev) && unicode.IsUpper(c):
s = append(s, ' ')
case !unicode.IsLetter(c):
c = ' '
}
if unicode.IsUpper(prev) && unicode.IsUpper(c) && unicode.IsLower(peek(r, i+1)) {
s = append(s, ' ')
}
s = append(s, c)
prev = c
}
return spaceRE.ReplaceAllString(strings.TrimSpace(string(s)), " ")
}
var spaceRE = regexp.MustCompile(`\s+`)
func peek(r []rune, i int) rune {
if i < len(r) {
return r[i]
}
return 0
}
type Style struct {
fontStyle *canvas.FontStyle
}
func NewStyle(fontStyle *canvas.FontStyle) pflag.Value {
return Style{
fontStyle: fontStyle,
}
}
func (style Style) String() string {
return strings.ToLower(style.fontStyle.String())
}
func (style Style) Set(s string) error {
italic, str := false, strings.ToLower(s)
if italicRE.MatchString(str) {
italic, str = true, italicRE.ReplaceAllString(str, "")
}
switch strings.TrimSpace(str) {
case "regular", "", "400", "0":
*style.fontStyle = canvas.FontRegular
case "thin", "100":
*style.fontStyle = canvas.FontThin
case "extra-light", "extralight", "200":
*style.fontStyle = canvas.FontExtraLight
case "light", "300":
*style.fontStyle = canvas.FontLight
case "medium", "500":
*style.fontStyle = canvas.FontMedium
case "semi-bold", "semibold", "600":
*style.fontStyle = canvas.FontSemiBold
case "bold", "700":
*style.fontStyle = canvas.FontBold
case "extra-bold", "extrabold", "800":
*style.fontStyle = canvas.FontExtraBold
case "black", "900":
*style.fontStyle = canvas.FontBlack
default:
return fmt.Errorf("invalid font style %q", s)
}
if italic {
*style.fontStyle |= canvas.FontItalic
}
return nil
}
var italicRE = regexp.MustCompile(`(?i)\s*italic\s*`)
func (style Style) Type() string {
return "font-style"
}
type Variant struct {
fontVariant *canvas.FontVariant
}
func NewVariant(fontVariant *canvas.FontVariant) pflag.Value {
return Variant{
fontVariant: fontVariant,
}
}
func (variant Variant) String() string {
return strings.ToLower(variant.fontVariant.String())
}
func (variant Variant) Set(s string) error {
switch strings.ToLower(s) {
case "normal":
*variant.fontVariant = canvas.FontNormal
case "subscript":
*variant.fontVariant = canvas.FontSubscript
case "superscript":
*variant.fontVariant = canvas.FontSuperscript
case "smallcaps":
*variant.fontVariant = canvas.FontSmallcaps
default:
return fmt.Errorf("invalid font variant %q", s)
}
return nil
}
func (variant Variant) Type() string {
return "font-variant"
}
//go:embed text.tpl
var textTpl []byte