forked from tdewolff/canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
246 lines (219 loc) · 5.67 KB
/
font.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
package canvas
import (
"fmt"
"io/ioutil"
"math"
"unicode/utf8"
findfont "github.com/flopp/go-findfont"
"golang.org/x/image/font"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed"
)
var sfntBuffer sfnt.Buffer
type FontStyle int
const (
Regular FontStyle = 0
Bold FontStyle = 1 << iota
Italic
)
type Font struct {
mimetype string
raw []byte
sfnt *sfnt.Font
name string
style FontStyle
}
// LoadLocalFont loads a font from the system fonts location.
func LoadLocalFont(name string, style FontStyle) (Font, error) {
fontPath, err := findfont.Find(name)
if err != nil {
return Font{}, err
}
return LoadFontFile(name, style, fontPath)
}
// LoadFontFile loads a font from a file.
func LoadFontFile(name string, style FontStyle, filename string) (Font, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return Font{}, err
}
return LoadFont(name, style, b)
}
// LoadFont loads a font from memory.
func LoadFont(name string, style FontStyle, b []byte) (Font, error) {
if len(b) < 4 {
return Font{}, fmt.Errorf("invalid font file")
}
tag := toTag(string(b[:4]))
mimetype := ""
if tag == toTag("wOFF") {
mimetype = "font/woff"
return Font{}, fmt.Errorf("WOFF font files not yet supported")
} else if tag == toTag("wOF2") {
mimetype = "font/woff2"
return Font{}, fmt.Errorf("WOFF2 font files not yet supported")
} else if tag == toTag("true") || tag == 0x00010000 {
mimetype = "font/truetype"
} else if tag == toTag("OTTO") {
mimetype = "font/opentype"
} else {
return Font{}, fmt.Errorf("unrecognized font file format")
}
sfnt, err := sfnt.Parse(b)
if err != nil {
return Font{}, err
}
return Font{
mimetype: mimetype,
raw: b,
sfnt: sfnt,
name: name,
style: style,
}, nil
}
// Face gets the font face associated with the give font name and font size (in mm).
func (f *Font) Face(size float64) FontFace {
// TODO: add hinting
return FontFace{
f: f,
size: size,
ppem: toI26_6(size),
hinting: font.HintingNone,
}
}
type Metrics struct {
LineHeight float64
Ascent float64
Descent float64
XHeight float64
CapHeight float64
}
type FontFace struct {
f *Font
size float64
ppem fixed.Int26_6
hinting font.Hinting
}
// Info returns the font name, style and size.
func (ff FontFace) Info() (name string, style FontStyle, size float64) {
return ff.f.name, ff.f.style, ff.size
}
// Metrics returns the font metrics. See https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png for an explaination of the different metrics.
func (ff FontFace) Metrics() Metrics {
m, _ := ff.f.sfnt.Metrics(&sfntBuffer, ff.ppem, ff.hinting)
return Metrics{
LineHeight: math.Abs(fromI26_6(m.Height)),
Ascent: math.Abs(fromI26_6(m.Ascent)),
Descent: math.Abs(fromI26_6(m.Descent)),
XHeight: math.Abs(fromI26_6(m.XHeight)),
CapHeight: math.Abs(fromI26_6(m.CapHeight)),
}
}
func splitNewlines(s string) []string {
ss := []string{}
i := 0
for j, r := range s {
if r == '\n' || r == '\r' || r == '\u2028' || r == '\u2029' {
if r == '\n' && j > 0 && s[j-1] == '\r' {
i++
continue
}
ss = append(ss, s[i:j])
i = j + utf8.RuneLen(r)
}
}
ss = append(ss, s[i:])
return ss
}
// textWidth returns the width of a given string in mm.
func (ff FontFace) textWidth(s string) float64 {
x := 0.0
var prevIndex sfnt.GlyphIndex
for i, r := range s {
index, err := ff.f.sfnt.GlyphIndex(&sfntBuffer, r)
if err != nil {
continue
}
if i != 0 {
kern, err := ff.f.sfnt.Kern(&sfntBuffer, prevIndex, index, ff.ppem, ff.hinting)
if err == nil {
x += fromI26_6(kern)
}
}
advance, err := ff.f.sfnt.GlyphAdvance(&sfntBuffer, index, ff.ppem, ff.hinting)
if err == nil {
x += fromI26_6(advance)
}
prevIndex = index
}
return x
}
// Bounds returns the bounding box (width and height) of a string.
func (ff FontFace) Bounds(s string) (w float64, h float64) {
ss := splitNewlines(s)
for _, s := range ss {
w = math.Max(w, ff.textWidth(s))
}
h = ff.Metrics().CapHeight + float64(len(ss)-1)*ff.Metrics().LineHeight
return w, h
}
// ToPath converts a string to a path.
func (ff FontFace) ToPath(s string) *Path {
p := &Path{}
x := 0.0
y := 0.0
for _, s := range splitNewlines(s) {
var prevIndex sfnt.GlyphIndex
for i, r := range s {
index, err := ff.f.sfnt.GlyphIndex(&sfntBuffer, r)
if err != nil {
continue
}
if i > 0 {
kern, err := ff.f.sfnt.Kern(&sfntBuffer, prevIndex, index, ff.ppem, ff.hinting)
if err == nil {
x += fromI26_6(kern)
}
}
segments, err := ff.f.sfnt.LoadGlyph(&sfntBuffer, index, ff.ppem, nil)
if err != nil {
continue
}
var start0, end Point
for i, segment := range segments {
switch segment.Op {
case sfnt.SegmentOpMoveTo:
if i != 0 && start0.Equals(end) {
p.Close()
}
end = fromP26_6(segment.Args[0])
p.MoveTo(x+end.X, y+end.Y)
start0 = end
case sfnt.SegmentOpLineTo:
end = fromP26_6(segment.Args[0])
p.LineTo(x+end.X, y+end.Y)
case sfnt.SegmentOpQuadTo:
c := fromP26_6(segment.Args[0])
end = fromP26_6(segment.Args[1])
p.QuadTo(x+c.X, y+c.Y, x+end.X, y+end.Y)
case sfnt.SegmentOpCubeTo:
c0 := fromP26_6(segment.Args[0])
c1 := fromP26_6(segment.Args[1])
end = fromP26_6(segment.Args[2])
p.CubeTo(x+c0.X, y+c0.Y, x+c1.X, y+c1.Y, x+end.X, y+end.Y)
}
}
if !p.Empty() && start0.Equals(end) {
p.Close()
}
advance, err := ff.f.sfnt.GlyphAdvance(&sfntBuffer, index, ff.ppem, ff.hinting)
if err == nil {
x += fromI26_6(advance)
}
prevIndex = index
}
x = 0.0
y += ff.Metrics().LineHeight
}
return p
}