/
renderer.go
345 lines (297 loc) · 8.21 KB
/
renderer.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
// SPDX-License-Identifier: Unlicense OR BSD-3-Clause
package font
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"github.com/go-text/typesetting/opentype/api"
)
var (
errEmptySbixTable = errors.New("empty 'sbix' table")
errEmptyBitmapTable = errors.New("empty bitmap table")
)
// GlyphData returns the glyph content for [gid], or nil if
// not found.
func (f *Face) GlyphData(gid GID) api.GlyphData {
// since outline may be specified for SVG and bitmaps, check it at the end
outB, err := f.sbix.glyphData(gID(gid), f.XPpem, f.YPpem)
if err == nil {
outline, ok := f.outlineGlyphData(gID(gid))
if ok {
outB.Outline = &outline
}
return outB
}
outB, err = f.bitmap.glyphData(gID(gid), f.XPpem, f.YPpem)
if err == nil {
outline, ok := f.outlineGlyphData(gID(gid))
if ok {
outB.Outline = &outline
}
return outB
}
outS, ok := f.svg.glyphData(gID(gid))
if ok {
// Spec :
// For every SVG glyph description, there must be a corresponding TrueType,
// CFF or CFF2 glyph description in the font.
outS.Outline, _ = f.outlineGlyphData(gID(gid))
return outS
}
if out, ok := f.outlineGlyphData(gID(gid)); ok {
return out
}
return nil
}
func (sb sbix) glyphData(gid gID, xPpem, yPpem uint16) (api.GlyphBitmap, error) {
st := sb.chooseStrike(xPpem, yPpem)
if st == nil {
return api.GlyphBitmap{}, errEmptySbixTable
}
glyph := strikeGlyph(st, gid, 0)
if glyph.GraphicType == 0 {
return api.GlyphBitmap{}, fmt.Errorf("no glyph %d in 'sbix' table for resolution (%d, %d)", gid, xPpem, yPpem)
}
out := api.GlyphBitmap{Data: glyph.Data}
var err error
out.Width, out.Height, out.Format, err = decodeBitmapConfig(glyph)
return out, err
}
func (bt bitmap) glyphData(gid gID, xPpem, yPpem uint16) (api.GlyphBitmap, error) {
st := bt.chooseStrike(xPpem, yPpem)
if st == nil || st.ppemX == 0 || st.ppemY == 0 {
return api.GlyphBitmap{}, errEmptyBitmapTable
}
subtable := st.findTable(gid)
if subtable == nil {
return api.GlyphBitmap{}, fmt.Errorf("no glyph %d in bitmap table for resolution (%d, %d)", gid, xPpem, yPpem)
}
glyph := subtable.image(gid)
if glyph == nil {
return api.GlyphBitmap{}, fmt.Errorf("no glyph %d in bitmap table for resolution (%d, %d)", gid, xPpem, yPpem)
}
out := api.GlyphBitmap{
Data: glyph.image,
Width: int(glyph.metrics.Width),
Height: int(glyph.metrics.Height),
}
switch subtable.imageFormat {
case 17, 18, 19: // PNG
out.Format = api.PNG
case 2, 5:
out.Format = api.BlackAndWhite
// ensure data length
L := out.Width * out.Height // in bits
if len(out.Data)*8 < L {
return api.GlyphBitmap{}, fmt.Errorf("EOF in glyph bitmap: expected %d, got %d", L, len(out.Data)*8)
}
default:
return api.GlyphBitmap{}, fmt.Errorf("unsupported format %d in bitmap table", subtable.imageFormat)
}
return out, nil
}
// look for data in 'glyf', 'CFF ' and 'CFF2' tables
func (f *Face) outlineGlyphData(gid gID) (api.GlyphOutline, bool) {
out, err := f.glyphDataFromCFF1(gid)
if err == nil {
return out, true
}
out, err = f.glyphDataFromCFF2(gid)
if err == nil {
return out, true
}
out, err = f.glyphDataFromGlyf(gid)
if err == nil {
return out, true
}
return api.GlyphOutline{}, false
}
func (s svg) glyphData(gid gID) (api.GlyphSVG, bool) {
data, ok := s.rawGlyphData(gid)
if !ok {
return api.GlyphSVG{}, false
}
// un-compress if needed
if r, err := gzip.NewReader(bytes.NewReader(data)); err == nil {
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err == nil {
data = buf.Bytes()
}
}
return api.GlyphSVG{Source: data}, true
}
// this file converts from font format for glyph outlines to
// segments that rasterizer will consume
//
// adapted from snft/truetype.go
func midPoint(p, q api.SegmentPoint) api.SegmentPoint {
return api.SegmentPoint{
X: (p.X + q.X) / 2,
Y: (p.Y + q.Y) / 2,
}
}
// build the segments from the resolved contour points
func buildSegments(points []contourPoint) []api.Segment {
if len(points) == 0 {
return nil
}
var (
firstOnCurveValid, firstOffCurveValid, lastOffCurveValid bool
firstOnCurve, firstOffCurve, lastOffCurve api.SegmentPoint
)
out := make([]api.Segment, 0, len(points)+2)
for _, point := range points {
p := point.SegmentPoint
if !firstOnCurveValid {
if point.isOnCurve {
firstOnCurve = p
firstOnCurveValid = true
out = append(out, api.Segment{
Op: api.SegmentOpMoveTo,
Args: [3]api.SegmentPoint{p},
})
} else if !firstOffCurveValid {
firstOffCurve = p
firstOffCurveValid = true
if !point.isEndPoint {
continue
}
} else {
firstOnCurve = midPoint(firstOffCurve, p)
firstOnCurveValid = true
lastOffCurve = p
lastOffCurveValid = true
out = append(out, api.Segment{
Op: api.SegmentOpMoveTo,
Args: [3]api.SegmentPoint{firstOnCurve},
})
}
} else if !lastOffCurveValid {
if !point.isOnCurve {
lastOffCurve = p
lastOffCurveValid = true
if !point.isEndPoint {
continue
}
} else {
out = append(out, api.Segment{
Op: api.SegmentOpLineTo,
Args: [3]api.SegmentPoint{p},
})
}
} else {
if !point.isOnCurve {
out = append(out, api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{
lastOffCurve,
midPoint(lastOffCurve, p),
},
})
lastOffCurve = p
lastOffCurveValid = true
} else {
out = append(out, api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{lastOffCurve, p},
})
lastOffCurveValid = false
}
}
if point.isEndPoint {
// closing the contour
switch {
case !firstOffCurveValid && !lastOffCurveValid:
out = append(out, api.Segment{
Op: api.SegmentOpLineTo,
Args: [3]api.SegmentPoint{firstOnCurve},
})
case !firstOffCurveValid && lastOffCurveValid:
out = append(out, api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{lastOffCurve, firstOnCurve},
})
case firstOffCurveValid && !lastOffCurveValid:
out = append(out, api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{firstOffCurve, firstOnCurve},
})
case firstOffCurveValid && lastOffCurveValid:
out = append(out, api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{
lastOffCurve,
midPoint(lastOffCurve, firstOffCurve),
},
},
api.Segment{
Op: api.SegmentOpQuadTo,
Args: [3]api.SegmentPoint{firstOffCurve, firstOnCurve},
},
)
}
firstOnCurveValid = false
firstOffCurveValid = false
lastOffCurveValid = false
}
}
return out
}
type errGlyphOutOfRange int
func (e errGlyphOutOfRange) Error() string {
return fmt.Sprintf("out of range glyph %d", e)
}
// apply variation when needed
func (f *Face) glyphDataFromGlyf(glyph gID) (api.GlyphOutline, error) {
if int(glyph) >= len(f.glyf) {
return api.GlyphOutline{}, errGlyphOutOfRange(glyph)
}
var points []contourPoint
f.getPointsForGlyph(glyph, 0, &points)
segments := buildSegments(points[:len(points)-phantomCount])
return api.GlyphOutline{Segments: segments}, nil
}
var (
errNoCFFTable error = errors.New("no CFF table")
errNoCFF2Table error = errors.New("no CFF2 table")
)
func (f *Font) glyphDataFromCFF1(glyph gID) (api.GlyphOutline, error) {
if f.cff == nil {
return api.GlyphOutline{}, errNoCFFTable
}
segments, _, err := f.cff.LoadGlyph(glyph)
if err != nil {
return api.GlyphOutline{}, err
}
return api.GlyphOutline{Segments: segments}, nil
}
func (f *Face) glyphDataFromCFF2(glyph gID) (api.GlyphOutline, error) {
if f.cff2 == nil {
return api.GlyphOutline{}, errNoCFF2Table
}
segments, _, err := f.cff2.LoadGlyph(glyph, f.Coords)
if err != nil {
return api.GlyphOutline{}, err
}
return api.GlyphOutline{Segments: segments}, nil
}
// BitmapSizes returns the size of bitmap glyphs present in the font.
func (font *Font) BitmapSizes() []api.BitmapSize {
upem := font.head.UnitsPerEm
avgWidth := font.os2.xAvgCharWidth
// handle invalid head/os2 tables
if upem == 0 || font.os2.version == 0xFFFF {
avgWidth = 1
upem = 1
}
// adapted from freetype tt_face_load_sbit
if font.bitmap != nil {
return font.bitmap.availableSizes(avgWidth, upem)
}
if hori := font.hhea; hori != nil {
return font.sbix.availableSizes(hori, avgWidth, upem)
}
return nil
}