-
Notifications
You must be signed in to change notification settings - Fork 0
/
subset.go
353 lines (318 loc) · 8.99 KB
/
subset.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
// seehuhn.de/go/sfnt - a library for reading and writing font files
// Copyright (C) 2023 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 (
"errors"
"fmt"
"seehuhn.de/go/postscript/cid"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/sfnt/cff"
"seehuhn.de/go/sfnt/cmap"
"seehuhn.de/go/sfnt/glyf"
"seehuhn.de/go/sfnt/glyph"
"seehuhn.de/go/sfnt/opentype/gdef"
"seehuhn.de/go/sfnt/opentype/gtab"
)
// Subset returns a subset of the font containing containing the given
// glyphs at the first positions. More glyphs may be included in the
// subset, if they occur as ligatures between the given glyphs.
func (f *Font) Subset(glyphs []glyph.ID) (*Font, error) {
if glyphs[0] != 0 {
return nil, errors.New("sfnt: subset must start with .notdef glyph")
}
res := f.Clone()
s := subsetter{
glyphs: glyphs,
newGid: map[glyph.ID]glyph.ID{},
}
for newgid, oldGid := range glyphs {
s.newGid[oldGid] = glyph.ID(newgid)
}
if f.CMapTable != nil {
res.CMapTable = make(cmap.Table, len(f.CMapTable))
for key := range f.CMapTable {
c, err := res.CMapTable.Get(key)
if err != nil {
continue
}
c = s.SubsetCMap(c)
res.CMapTable[key] = c.Encode(key.Language)
}
}
res.Gsub = s.SubsetGsub(f.Gsub)
// At this point we have the final list of glyphs.
res.Gpos = s.SubsetGpos(f.Gpos)
res.Gdef = s.SubsetGdef(f.Gdef)
switch outlines := f.Outlines.(type) {
case *cff.Outlines:
res.Outlines = s.SubsetCFF(outlines)
case *glyf.Outlines:
res.Outlines = s.SubsetGlyf(outlines)
}
return res, nil
}
type subsetter struct {
glyphs []glyph.ID
newGid map[glyph.ID]glyph.ID
}
func (s *subsetter) getNewGid(oldGid glyph.ID) glyph.ID {
newGid, ok := s.newGid[oldGid]
if !ok {
newGid = glyph.ID(len(s.glyphs))
s.glyphs = append(s.glyphs, oldGid)
s.newGid[oldGid] = newGid
}
return newGid
}
func (s *subsetter) SubsetCMap(c cmap.Subtable) cmap.Subtable {
if c == nil {
return nil
}
switch c := c.(type) {
case cmap.Format4:
res := cmap.Format4{}
for key, oldGid := range c {
newGid, ok := s.newGid[oldGid]
if !ok {
continue
}
res[key] = newGid
}
return res
case cmap.Format12:
res := cmap.Format12{}
for key, oldGid := range c {
newGid, ok := s.newGid[oldGid]
if !ok {
continue
}
res[key] = newGid
}
return res
default:
panic(fmt.Sprintf("sfnt: unsupported cmap format %T", c))
}
}
func (s *subsetter) SubsetGsub(old *gtab.Info) *gtab.Info {
if old == nil {
return nil
}
res := *old
res.LookupList = make(gtab.LookupList, len(old.LookupList))
// TODO(voss): if glyphs were added, we need to re-run this loop
for i, tOld := range old.LookupList {
tNew := >ab.LookupTable{
Meta: tOld.Meta,
Subtables: make([]gtab.Subtable, len(tOld.Subtables)),
}
for j, sOld := range tOld.Subtables {
switch sOld := sOld.(type) {
case *gtab.Gsub1_1:
// It is difficult to produce a format 1 subtable for the subset
// (constant offset between original and subtitute), so we
// convert this to format 2 instead.
sNew := >ab.Gsub1_2{
Cov: make(map[glyph.ID]int),
}
for oldOrig := range sOld.Cov {
newOrig, ok := s.newGid[oldOrig]
if !ok {
continue
}
newSubst := oldOrig + sOld.Delta
// TODO(voss): keep the coverage table sorted
sNew.Cov[newOrig] = len(sNew.SubstituteGlyphIDs)
sNew.SubstituteGlyphIDs = append(sNew.SubstituteGlyphIDs, s.getNewGid(newSubst))
}
tNew.Subtables[j] = sNew
case *gtab.Gsub1_2:
panic("not implemented")
case *gtab.Gsub2_1:
panic("not implemented")
case *gtab.Gsub3_1:
panic("not implemented")
case *gtab.Gsub4_1:
panic("not implemented")
case *gtab.Gsub8_1:
panic("not implemented")
case *gtab.SeqContext1:
panic("not implemented")
case *gtab.SeqContext2:
panic("not implemented")
case *gtab.SeqContext3:
panic("not implemented")
case *gtab.ChainedSeqContext1:
panic("not implemented")
case *gtab.ChainedSeqContext2:
panic("not implemented")
case *gtab.ChainedSeqContext3:
panic("not implemented")
default:
panic(fmt.Sprintf("sfnt: unsupported GSUB format %T", sOld))
}
}
res.LookupList[i] = tNew
}
return &res
}
func (s *subsetter) SubsetGpos(old *gtab.Info) *gtab.Info {
if old == nil {
return nil
}
res := *old
res.LookupList = make(gtab.LookupList, len(old.LookupList))
for i, tOld := range old.LookupList {
tNew := >ab.LookupTable{
Meta: tOld.Meta,
Subtables: make([]gtab.Subtable, len(tOld.Subtables)),
}
for j, sOld := range tOld.Subtables {
switch sOld := sOld.(type) {
case *gtab.Gpos1_1:
case *gtab.Gpos1_2:
case gtab.Gpos2_1:
sNew := gtab.Gpos2_1{}
for pair, adj := range sOld {
if _, ok := s.newGid[pair.Left]; !ok {
continue
}
if _, ok := s.newGid[pair.Right]; !ok {
continue
}
sNew[pair] = adj
}
tNew.Subtables[j] = sNew
case *gtab.Gpos2_2:
panic("not implemented")
case *gtab.Gpos3_1:
panic("not implemented")
case *gtab.Gpos4_1:
panic("not implemented")
case *gtab.Gpos6_1:
panic("not implemented")
case *gtab.SeqContext1:
panic("not implemented")
case *gtab.SeqContext2:
panic("not implemented")
case *gtab.SeqContext3:
panic("not implemented")
case *gtab.ChainedSeqContext1:
panic("not implemented")
case *gtab.ChainedSeqContext2:
panic("not implemented")
case *gtab.ChainedSeqContext3:
panic("not implemented")
default:
panic(fmt.Sprintf("sfnt: unsupported GPOS format %T", tOld))
}
}
res.LookupList[i] = tNew
}
return &res
}
func (s *subsetter) SubsetGdef(old *gdef.Table) *gdef.Table {
if old == nil {
return nil
}
panic("not implemented")
}
func (s *subsetter) SubsetCFF(oldOutlines *cff.Outlines) *cff.Outlines {
newOutlines := &cff.Outlines{}
newOutlines.Glyphs = make([]*cff.Glyph, len(s.glyphs))
for i, oldGid := range s.glyphs {
newOutlines.Glyphs[i] = oldOutlines.Glyphs[oldGid]
}
pIdxMap := make(map[int]int)
for _, oldGid := range s.glyphs {
oldPIdx := oldOutlines.FDSelect(oldGid)
if _, ok := pIdxMap[oldPIdx]; !ok {
newPIdx := len(newOutlines.Private)
newOutlines.Private = append(newOutlines.Private, oldOutlines.Private[oldPIdx])
pIdxMap[oldPIdx] = newPIdx
}
}
if len(newOutlines.Private) == 1 {
newOutlines.FDSelect = func(glyph.ID) int { return 0 }
} else {
fdSel := make([]int, len(s.glyphs))
for newgid, oldGid := range s.glyphs {
fdSel[newgid] = pIdxMap[oldOutlines.FDSelect(oldGid)]
}
newOutlines.FDSelect = func(gid glyph.ID) int { return fdSel[gid] }
}
if oldOutlines.Encoding != nil {
newOutlines.Encoding = make([]glyph.ID, len(oldOutlines.Encoding))
for i, oldGid := range oldOutlines.Encoding {
if newGid, ok := s.newGid[oldGid]; ok {
newOutlines.Encoding[i] = newGid
}
}
}
newOutlines.ROS = oldOutlines.ROS
if oldOutlines.GIDToCID != nil {
newOutlines.GIDToCID = make([]cid.CID, len(s.glyphs))
for newGid, oldGid := range s.glyphs {
newOutlines.GIDToCID[newGid] = oldOutlines.GIDToCID[oldGid]
}
}
return newOutlines
}
func (s *subsetter) SubsetGlyf(oldOutlines *glyf.Outlines) *glyf.Outlines {
newOutlines := &glyf.Outlines{
Tables: oldOutlines.Tables,
Maxp: oldOutlines.Maxp,
}
todo := make(map[glyph.ID]bool, len(s.glyphs))
for _, oldGid := range s.glyphs {
todo[oldGid] = true
}
for len(todo) > 0 {
oldGid := pop(todo)
cc := oldOutlines.Glyphs[oldGid].Components()
for _, componentGidOld := range cc {
if _, ok := s.newGid[componentGidOld]; ok {
continue
}
componendGidNew := glyph.ID(len(s.glyphs))
s.glyphs = append(s.glyphs, componentGidOld)
s.newGid[oldGid] = componendGidNew
todo[componentGidOld] = true
}
}
newOutlines.Glyphs = make([]*glyf.Glyph, len(s.glyphs))
for newGid, oldGid := range s.glyphs {
newOutlines.Glyphs[newGid] = oldOutlines.Glyphs[oldGid].FixComponents(s.newGid)
}
newOutlines.Widths = make([]funit.Int16, len(s.glyphs))
for newGid, oldGid := range s.glyphs {
newOutlines.Widths[newGid] = oldOutlines.Widths[oldGid]
}
if oldOutlines.Names != nil {
newOutlines.Names = make([]string, len(s.glyphs))
for newGid, oldGid := range s.glyphs {
newOutlines.Names[newGid] = oldOutlines.Names[oldGid]
}
}
// TODO(voss): can anything be done to make the "fpgm" table smaller?
return newOutlines
}
func pop(todo map[glyph.ID]bool) glyph.ID {
for key := range todo {
delete(todo, key)
return key
}
panic("empty map")
}