/
fonts.go
378 lines (327 loc) · 11.9 KB
/
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
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
package harfbuzz
import (
"github.com/benoitkugler/go-opentype/api"
"github.com/benoitkugler/go-opentype/api/font"
"github.com/benoitkugler/go-opentype/tables"
)
// ported from src/hb-font.hh, src/hb-font.cc Copyright © 2009 Red Hat, Inc., 2012 Google, Inc. Behdad Esfahbod
type Face = *font.Face
var _ FaceOpentype = (*font.Font)(nil)
// FaceOpentype adds support for advanced layout features
// found in Opentype/Truetype font files.
// See the package fonts/truetype for more details.
type FaceOpentype interface {
// Face
// tables.FaceVariable
// // Returns true if the font has Graphite capabilities.
// // Note that tables validity will still be checked in `NewFont`,
// // using the table from the returned `tables.Font`.
// // Overide this method to disable Graphite functionalities.
// IsGraphite() (*tables.Font, bool)
// // LayoutTables fetchs the Opentype layout tables of the font.
// LayoutTables() tables.LayoutTables
// GetGlyphContourPoint retrieves the (X,Y) coordinates (in font units) for a
// specified contour point in a glyph, or false if not found.
GetGlyphContourPoint(glyph GID, pointIndex uint16) (x, y int32, ok bool)
// VariationGlyph retrieves the glyph ID for a specified Unicode code point
// followed by a specified Variation Selector code point, or false if not found
VariationGlyph(ch, varSelector rune) (GID, bool)
}
// Font is used internally as a light wrapper around the provided Face.
//
// While a font face is generally the in-memory representation of a static font file,
// `Font` handles dynamic attributes like size, width and
// other parameters (pixels-per-em, points-per-em, variation
// settings).
//
// Font are constructed with `NewFont` and adjusted by accessing the fields
// XPpem, YPpem, Ptem,XScale, YScale and with the method `SetVarCoordsDesign` for
// variable fonts.
type Font struct {
face Face
gsubAccels, gposAccels []otLayoutLookupAccelerator // accelators for lookup
faceUpem int32 // cached value of Face.Upem()
// Point size of the font. Set to zero to unset.
// This is used in AAT layout, when applying 'trak' table.
Ptem float32
// Horizontal and vertical scale of the font.
// The resulting positions are computed with: fontUnit * Scale / faceUpem,
// where faceUpem is given by the face.
//
// Given a device resolution (in dpi) and a point size, the scale to
// get result in pixels is given by : pointSize * dpi / 72
XScale, YScale int32
}
// NewFont constructs a new font object from the specified face.
//
// The scale is set to the face Upem, meaning that by default
// the output results will be expressed in font units.
//
// The `face` object should not be modified after this call.
func NewFont(face Face) *Font {
var font Font
font.face = face
font.faceUpem = Position(font.face.Upem())
font.XScale = font.faceUpem
font.YScale = font.faceUpem
// accelerators
font.gsubAccels = make([]otLayoutLookupAccelerator, len(face.GSUB.Lookups))
for i, l := range face.GSUB.Lookups {
font.gsubAccels[i].init(lookupGSUB(l))
}
font.gposAccels = make([]otLayoutLookupAccelerator, len(face.GPOS.Lookups))
for i, l := range face.GPOS.Lookups {
font.gposAccels[i].init(lookupGPOS(l))
}
return &font
}
// SetVarCoordsDesign applies a list of variation coordinates, in design-space units,
// to the font.
func (f *Font) SetVarCoordsDesign(coords []float32) {
f.face.Coords = f.face.NormalizeVariations(coords)
}
// Face returns the underlying face.
// Note that field is readonly, since some caching may happen
// in the `NewFont` constructor.
func (f *Font) Face() Face { return f.face }
func (f *Font) nominalGlyph(r rune, notFound GID) (GID, bool) {
g, ok := f.face.NominalGlyph(r)
if !ok {
g = notFound
}
return g, ok
}
// ---- Convert from font-space to user-space ----
func (f *Font) emScaleX(v int16) Position { return Position(v) * f.XScale / f.faceUpem }
func (f *Font) emScaleY(v int16) Position { return Position(v) * f.YScale / f.faceUpem }
func (f *Font) emScalefX(v float32) Position { return emScalef(v, f.XScale, f.faceUpem) }
func (f *Font) emScalefY(v float32) Position { return emScalef(v, f.YScale, f.faceUpem) }
func (f *Font) emFscaleX(v int16) float32 { return emFscale(v, f.XScale, f.faceUpem) }
func (f *Font) emFscaleY(v int16) float32 { return emFscale(v, f.YScale, f.faceUpem) }
func emScalef(v float32, scale, faceUpem int32) Position {
return roundf(v * float32(scale) / float32(faceUpem))
}
func emFscale(v int16, scale, faceUpem int32) float32 {
return float32(v) * float32(scale) / float32(faceUpem)
}
// GlyphExtents is the same as fonts.GlyphExtents but with int type
type GlyphExtents struct {
XBearing int32
YBearing int32
Width int32
Height int32
}
// GlyphExtents fetches the GlyphExtents data for a glyph ID
// in the specified font, or false if not found
func (f *Font) GlyphExtents(glyph GID) (out GlyphExtents, ok bool) {
ext, ok := f.face.GlyphExtents(glyph)
if !ok {
return out, false
}
out.XBearing = f.emScalefX(ext.XBearing)
out.Width = f.emScalefX(ext.Width)
out.YBearing = f.emScalefY(ext.YBearing)
out.Height = f.emScalefY(ext.Height)
return out, true
}
// GlyphAdvanceForDirection fetches the advance for a glyph ID from the specified font,
// in a text segment of the specified direction.
//
// Calls the appropriate direction-specific variant (horizontal
// or vertical) depending on the value of `dir`.
func (f *Font) GlyphAdvanceForDirection(glyph GID, dir Direction) (x, y Position) {
if dir.isHorizontal() {
return f.GlyphHAdvance(glyph), 0
}
return 0, f.getGlyphVAdvance(glyph)
}
// GlyphHAdvance fetches the advance for a glyph ID in the font,
// for horizontal text segments.
func (f *Font) GlyphHAdvance(glyph GID) Position {
adv := f.face.HorizontalAdvance(glyph)
return f.emScalefX(adv)
}
// Fetches the advance for a glyph ID in the font,
// for vertical text segments.
func (f *Font) getGlyphVAdvance(glyph GID) Position {
adv := f.face.VerticalAdvance(glyph)
return f.emScalefY(adv)
}
// Subtracts the origin coordinates from an (X,Y) point coordinate,
// in the specified glyph ID in the specified font.
//
// Calls the appropriate direction-specific variant (horizontal
// or vertical) depending on the value of @direction.
func (f *Font) subtractGlyphOriginForDirection(glyph GID, direction Direction,
x, y Position,
) (Position, Position) {
originX, originY := f.getGlyphOriginForDirection(glyph, direction)
return x - originX, y - originY
}
// Fetches the (X,Y) coordinates of the origin for a glyph in
// the specified font.
//
// Calls the appropriate direction-specific variant (horizontal
// or vertical) depending on the value of @direction.
func (f *Font) getGlyphOriginForDirection(glyph GID, direction Direction) (x, y Position) {
if direction.isHorizontal() {
return f.getGlyphHOriginWithFallback(glyph)
}
return f.getGlyphVOriginWithFallback(glyph)
}
func (f *Font) getGlyphHOriginWithFallback(glyph GID) (Position, Position) {
x, y, ok := f.face.GlyphHOrigin(glyph)
if !ok {
x, y, ok = f.face.GlyphVOrigin(glyph)
if ok {
dx, dy := f.guessVOriginMinusHOrigin(glyph)
return x - dx, y - dy
}
}
return x, y
}
func (f *Font) getGlyphVOriginWithFallback(glyph GID) (Position, Position) {
x, y, ok := f.face.GlyphVOrigin(glyph)
if !ok {
x, y, ok = f.face.GlyphHOrigin(glyph)
if ok {
dx, dy := f.guessVOriginMinusHOrigin(glyph)
return x + dx, y + dy
}
}
return x, y
}
func (f *Font) guessVOriginMinusHOrigin(glyph GID) (x, y Position) {
x = f.GlyphHAdvance(glyph) / 2
y = f.getHExtendsAscender()
return x, y
}
func (f *Font) getHExtendsAscender() Position {
extents, ok := f.face.FontHExtents()
if !ok {
return f.YScale * 4 / 5
}
return f.emScalefY(extents.Ascender)
}
func (f *Font) hasGlyph(ch rune) bool {
_, ok := f.face.NominalGlyph(ch)
return ok
}
func (f *Font) subtractGlyphHOrigin(glyph GID, x, y Position) (Position, Position) {
originX, originY := f.getGlyphHOriginWithFallback(glyph)
return x - originX, y - originY
}
func (f *Font) subtractGlyphVOrigin(glyph GID, x, y Position) (Position, Position) {
originX, originY := f.getGlyphVOriginWithFallback(glyph)
return x - originX, y - originY
}
func (f *Font) addGlyphHOrigin(glyph GID, x, y Position) (Position, Position) {
originX, originY := f.getGlyphHOriginWithFallback(glyph)
return x + originX, y + originY
}
func (f *Font) getGlyphContourPointForOrigin(glyph GID, pointIndex uint16, direction Direction) (x, y Position, ok bool) {
x, y, ok = f.face.GetGlyphContourPoint(glyph, pointIndex)
if ok {
x, y = f.subtractGlyphOriginForDirection(glyph, direction, x, y)
}
return x, y, ok
}
// ExtentsForDirection fetches the extents for a font in a text segment of the
// specified direction, applying the scaling.
//
// Calls the appropriate direction-specific variant (horizontal
// or vertical) depending on the value of `direction`.
func (f *Font) ExtentsForDirection(direction Direction) api.FontExtents {
var (
extents api.FontExtents
ok bool
)
if direction.isHorizontal() {
extents, ok = f.face.FontHExtents()
extents.Ascender = float32(f.emScalefY(extents.Ascender))
extents.Descender = float32(f.emScalefY(extents.Descender))
extents.LineGap = float32(f.emScalefY(extents.LineGap))
if !ok {
extents.Ascender = float32(f.YScale) * 0.8
extents.Descender = extents.Ascender - float32(f.YScale)
extents.LineGap = 0
}
} else {
extents, ok = f.face.FontVExtents()
extents.Ascender = float32(f.emScalefX(extents.Ascender))
extents.Descender = float32(f.emScalefX(extents.Descender))
extents.LineGap = float32(f.emScalefX(extents.LineGap))
if !ok {
extents.Ascender = float32(f.XScale) * 0.5
extents.Descender = extents.Ascender - float32(f.XScale)
extents.LineGap = 0
}
}
return extents
}
func (font *Font) varCoords() []float32 { return font.face.Coords }
func (font *Font) getXDelta(varStore tables.ItemVarStore, device tables.DeviceTable) Position {
switch device := device.(type) {
case tables.DeviceHinting:
return device.GetDelta(font.face.XPpem, font.XScale)
case tables.DeviceVariation:
return font.emScalefX(varStore.GetDelta(tables.VariationStoreIndex(device), font.varCoords()))
default:
return 0
}
}
func (font *Font) getYDelta(varStore tables.ItemVarStore, device tables.DeviceTable) Position {
switch device := device.(type) {
case tables.DeviceHinting:
return device.GetDelta(font.face.YPpem, font.YScale)
case tables.DeviceVariation:
return font.emScalefY(varStore.GetDelta(tables.VariationStoreIndex(device), font.varCoords()))
default:
return 0
}
}
// GetOTLigatureCarets fetches a list of the caret positions defined for a ligature glyph in the GDEF
// table of the font (or nil if not found).
func (f *Font) GetOTLigatureCarets(direction Direction, glyph GID) []Position {
varStore := f.face.GDEF.ItemVarStore
list := f.face.GDEF.LigCaretList
if list.Coverage == nil {
return nil
}
index, ok := list.Coverage.Index(gID(glyph))
if !ok {
return nil
}
glyphCarets := list.LigGlyphs[index].CaretValues
out := make([]Position, len(glyphCarets))
for i, c := range glyphCarets {
out[i] = f.getCaretValue(c, direction, glyph, varStore)
}
return out
}
// interpreted the CaretValue according to its format
func (f *Font) getCaretValue(caret tables.CaretValue, direction Direction, glyph GID, varStore tables.ItemVarStore) Position {
switch caret := caret.(type) {
case tables.CaretValue1:
if direction.isHorizontal() {
return f.emScaleX(int16(caret.Coordinate))
} else {
return f.emScaleY(int16(caret.Coordinate))
}
case tables.CaretValue2:
x, y, _ := f.getGlyphContourPointForOrigin(glyph, uint16(caret.CaretValuePointIndex), direction)
if direction.isHorizontal() {
return x
} else {
return y
}
case tables.CaretValue3:
if direction.isHorizontal() {
return f.emScaleX(caret.Coordinate) + f.getXDelta(varStore, caret.Device)
} else {
return f.emScaleY(caret.Coordinate) + f.getYDelta(varStore, caret.Device)
}
default:
return 0
}
}