fonts.go raw
1 package p9fonts
2
3 import (
4 "fmt"
5 "sync"
6
7 "github.com/p9c/gio/font/opentype"
8 "github.com/p9c/gio/text"
9 "golang.org/x/image/font/gofont/gomono"
10 "golang.org/x/image/font/gofont/gomonobold"
11 "golang.org/x/image/font/gofont/gomonobolditalic"
12 "golang.org/x/image/font/gofont/gomonoitalic"
13
14 "github.com/p9c/gel/fonts/bariolbold"
15 "github.com/p9c/gel/fonts/bariolbolditalic"
16 "github.com/p9c/gel/fonts/bariollight"
17 "github.com/p9c/gel/fonts/bariollightitalic"
18 "github.com/p9c/gel/fonts/bariolregular"
19 "github.com/p9c/gel/fonts/bariolregularitalic"
20 "github.com/p9c/gel/fonts/plan9"
21 )
22
23 var (
24 once sync.Once
25 collection []text.FontFace
26 Fonts = map[string]text.Font{
27 "plan9": {Typeface: "plan9"},
28 "bariol regular": {Typeface: "bariol regular"},
29 "bariol italic": {Typeface: "bariol italic", Style: text.Italic},
30 "bariol bold": {Typeface: "bariol bold", Weight: text.Bold},
31 "bariol bolditalic": {Typeface: "bariol bolditalic", Style: text.Italic, Weight: text.Bold},
32 "bariol light": {Typeface: "bariol light", Weight: text.Medium},
33 "bariol lightitalic": {Typeface: "bariol lightitalic", Weight: text.Medium, Style: text.Italic},
34 "go regular": {Typeface: "go regular"},
35 "go bold": {Typeface: "go bold", Weight: text.Bold},
36 "go bolditalic": {Typeface: "go bolditalic", Weight: text.Bold, Style: text.Italic},
37 "go italic": {Typeface: "go italic", Style: text.Italic},
38 }
39 )
40
41 func Collection() []text.FontFace {
42 once.Do(func() {
43 register(Fonts["plan9"], plan9.TTF)
44 register(Fonts["bariol regular"], bariolregular.TTF)
45 register(Fonts["bariol italic"], bariolregularitalic.TTF)
46 register(Fonts["bariol bold"], bariolbold.TTF)
47 register(Fonts["bariol bold italic"], bariolbolditalic.TTF)
48 register(Fonts["bariol light"], bariollight.TTF)
49 register(Fonts["bariol light italic"], bariollightitalic.TTF)
50 register(Fonts["go regular"], gomono.TTF)
51 register(Fonts["go bold"], gomonobold.TTF)
52 register(Fonts["go bolditalic"], gomonobolditalic.TTF)
53 register(Fonts["go italic"], gomonoitalic.TTF)
54 // Ensure that any outside appends will not reuse the backing store.
55 n := len(collection)
56 collection = collection[:n:n]
57 })
58 return collection
59 }
60
61 func register(fnt text.Font, ttf []byte) {
62 face, e := opentype.Parse(ttf)
63 if e != nil {
64 panic(fmt.Errorf("failed to parse font: %v", e))
65 }
66 // fnt.Typeface = "Go"
67 collection = append(collection, text.FontFace{Font: fnt, Face: face})
68 }
69