-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
363 lines (322 loc) · 8.7 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
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
// seehuhn.de/go/sfnt - a library for reading and writing font files
// Copyright (C) 2022 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package sfnt
import (
"regexp"
"strings"
"time"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/postscript/type1"
"seehuhn.de/go/sfnt/cff"
"seehuhn.de/go/sfnt/cmap"
"seehuhn.de/go/sfnt/glyf"
"seehuhn.de/go/sfnt/glyph"
"seehuhn.de/go/sfnt/head"
"seehuhn.de/go/sfnt/opentype/gdef"
"seehuhn.de/go/sfnt/opentype/gtab"
"seehuhn.de/go/sfnt/os2"
)
// TODO(voss): read https://github.com/googlefonts/gf-docs/tree/main/VerticalMetrics
// Font contains information about a TrueType or OpenType font.
//
// TODO(voss): clarify the relation between IsOblique, IsItalic, and
// ItalicAngle != 0.
type Font struct {
FamilyName string
Width os2.Width
Weight os2.Weight
IsRegular bool // glyphs are in the standard weight/style for the font
IsBold bool // glyphs are emboldened
IsItalic bool // font contains italic or oblique glyphs
IsOblique bool // font contains oblique glyphs
IsSerif bool
IsScript bool // Glyphs resemble cursive handwriting.
CodePageRange os2.CodePageRange
Version head.Version
CreationTime time.Time
ModificationTime time.Time
Description string
SampleText string
Copyright string
Trademark string
License string
LicenseURL string
PermUse os2.Permissions
UnitsPerEm uint16
FontMatrix [6]float64
Ascent funit.Int16
Descent funit.Int16 // negative
LineGap funit.Int16 // LineGap = Leading - Ascent + Descent
CapHeight funit.Int16
XHeight funit.Int16
ItalicAngle float64 // Italic angle (degrees counterclockwise from vertical)
UnderlinePosition funit.Float64 // Underline position (negative)
UnderlineThickness funit.Float64 // Underline thickness
Outlines interface{} // either *cff.Outlines or *glyf.Outlines
CMapTable cmap.Table
Gdef *gdef.Table
Gsub *gtab.Info
Gpos *gtab.Info
}
// Clone makes a shallow copy of the font object.
func (f *Font) Clone() *Font {
f2 := *f
return &f2
}
// GetFontInfo returns an Adobe FontInfo structure for the given font.
func (f *Font) GetFontInfo() *type1.FontInfo {
fontInfo := &type1.FontInfo{
FontName: f.PostScriptName(),
FullName: f.FullName(),
FamilyName: f.FamilyName,
Weight: f.Weight.String(),
Version: f.Version.String(),
Copyright: strings.ReplaceAll(f.Copyright, "©", "(c)"),
Notice: f.Trademark,
FontMatrix: f.FontMatrix,
ItalicAngle: f.ItalicAngle,
IsFixedPitch: f.IsFixedPitch(),
UnderlinePosition: f.UnderlinePosition,
UnderlineThickness: f.UnderlineThickness,
}
return fontInfo
}
// IsGlyf returns true if the font contains TrueType glyph outlines.
func (f *Font) IsGlyf() bool {
_, ok := f.Outlines.(*glyf.Outlines)
return ok
}
// IsCFF returns true if the font contains CFF glyph outlines.
func (f *Font) IsCFF() bool {
_, ok := f.Outlines.(*cff.Outlines)
return ok
}
// AsCFF returns the CFF font data for the given font.
// Panics if the font does not contain CFF outlines.
func (f *Font) AsCFF() *cff.Font {
return &cff.Font{
FontInfo: f.GetFontInfo(),
Outlines: f.Outlines.(*cff.Outlines),
}
}
// FullName returns the full name of the font.
func (f *Font) FullName() string {
return f.FamilyName + " " + f.Subfamily()
}
// Subfamily returns the subfamily name of the font.
func (f *Font) Subfamily() string {
var words []string
if f.Width != 0 && f.Width != os2.WidthNormal {
words = append(words, f.Width.String())
}
if f.Weight != 0 && f.Weight != os2.WeightNormal {
words = append(words, f.Weight.SimpleString())
} else if f.IsBold {
words = append(words, "Bold")
}
if f.IsOblique {
words = append(words, "Oblique")
} else if f.IsItalic {
words = append(words, "Italic")
}
if len(words) == 0 {
return "Regular"
}
return strings.Join(words, " ")
}
// PostScriptName returns the PostScript name of the font.
func (f *Font) PostScriptName() string {
name := f.FamilyName + "-" + f.Subfamily()
re := regexp.MustCompile(`[^!-$&-'*-.0-;=?-Z\\^-z|~]+`)
return re.ReplaceAllString(name, "")
}
// BBox returns the bounding box of the font.
func (f *Font) BBox() (bbox funit.Rect16) {
first := true
for i := 0; i < f.NumGlyphs(); i++ {
ext := f.GlyphBBox(glyph.ID(i))
if ext.IsZero() {
continue
}
if first {
bbox = ext
} else {
bbox.Extend(ext)
}
}
return
}
// NumGlyphs returns the number of glyphs in the font.
func (f *Font) NumGlyphs() int {
switch outlines := f.Outlines.(type) {
case *cff.Outlines:
return len(outlines.Glyphs)
case *glyf.Outlines:
return len(outlines.Glyphs)
default:
panic("unexpected font type")
}
}
// GlyphWidth returns the advance width of the glyph with the given glyph ID,
// in font design units.
func (f *Font) GlyphWidth(gid glyph.ID) funit.Int16 {
switch f := f.Outlines.(type) {
case *cff.Outlines:
return f.Glyphs[gid].Width
case *glyf.Outlines:
if f.Widths == nil {
return 0
}
return f.Widths[gid]
default:
panic("unexpected font type")
}
}
// GlyphWidthPDF returns the advance width in PDF text space units.
func (f *Font) GlyphWidthPDF(gid glyph.ID) float64 {
switch o := f.Outlines.(type) {
case *cff.Outlines:
// TODO(voss): ... + f.FontMatrix[4] ???
return float64(o.Glyphs[gid].Width) * f.FontMatrix[0]
case *glyf.Outlines:
if o.Widths == nil {
return 0
}
return float64(o.Widths[gid]) / float64(f.UnitsPerEm)
default:
panic("unexpected font type")
}
}
// Widths returns the advance widths of the glyphs in the font.
func (f *Font) Widths() []funit.Int16 {
switch outlines := f.Outlines.(type) {
case *cff.Outlines:
widths := make([]funit.Int16, f.NumGlyphs())
for gid, g := range outlines.Glyphs {
widths[gid] = g.Width
}
return widths
case *glyf.Outlines:
return outlines.Widths
default:
panic("unexpected font type")
}
}
// WidthsPDF returns the advance widths of the glyphs in the font.
func (f *Font) WidthsPDF() []float64 {
widths := make([]float64, f.NumGlyphs())
switch outlines := f.Outlines.(type) {
case *cff.Outlines:
for gid, g := range outlines.Glyphs {
widths[gid] = float64(g.Width) * f.FontMatrix[0]
}
return widths
case *glyf.Outlines:
if outlines.Widths == nil {
return nil
}
for gid, w := range outlines.Widths {
widths[gid] = float64(w) / float64(f.UnitsPerEm)
}
default:
panic("unexpected font type")
}
return widths
}
// GlyphBBoxes returns the glyph bounding boxes for the font.
func (f *Font) GlyphBBoxes() []funit.Rect16 {
extents := make([]funit.Rect16, f.NumGlyphs())
switch f := f.Outlines.(type) {
case *cff.Outlines:
for i, g := range f.Glyphs {
extents[i] = g.Extent()
}
case *glyf.Outlines:
for i, g := range f.Glyphs {
if g == nil {
continue
}
extents[i] = g.Rect16
}
default:
panic("unexpected font type")
}
return extents
}
// GlyphBBox returns the glyph bounding box for one glyph in font design
// units.
func (f *Font) GlyphBBox(gid glyph.ID) funit.Rect16 {
switch f := f.Outlines.(type) {
case *cff.Outlines:
return f.Glyphs[gid].Extent()
case *glyf.Outlines:
g := f.Glyphs[gid]
if g == nil {
return funit.Rect16{}
}
return g.Rect16
default:
panic("unexpected font type")
}
}
func (f *Font) glyphHeight(gid glyph.ID) funit.Int16 {
switch f := f.Outlines.(type) {
case *cff.Outlines:
return f.Glyphs[gid].Extent().URy
case *glyf.Outlines:
g := f.Glyphs[gid]
if g == nil {
return 0
}
return g.Rect16.URy
default:
panic("unexpected font type")
}
}
// GlyphName returns the name of a glyph.
// If the name is not known, the empty string is returned.
func (f *Font) GlyphName(gid glyph.ID) string {
switch f := f.Outlines.(type) {
case *cff.Outlines:
return f.Glyphs[gid].Name
case *glyf.Outlines:
if f.Names == nil {
return ""
}
return f.Names[gid]
default:
panic("unexpected font type")
}
}
// IsFixedPitch returns true if all glyphs in the font have the same width.
func (f *Font) IsFixedPitch() bool {
ww := f.Widths()
if len(ww) == 0 {
return false
}
var width funit.Int16
for _, w := range ww {
if w == 0 {
continue
}
if width == 0 {
width = w
} else if width != w {
return false
}
}
return true
}