-
Notifications
You must be signed in to change notification settings - Fork 1
/
sfnt_subset.c
415 lines (369 loc) · 11 KB
/
sfnt_subset.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "sfnt.h"
#include "sfnt_int.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "bitset.h"
int otf_copy_sfnt(OTF_FILE *otf,struct _OTF_WRITE_WOFF *woff,OUTPUT_FN output,void *context) // {{{
{
assert(otf);
assert(output);
int iA;
struct _OTF_WRITE_INFO otw={
.version=otf->version,
.numTables=otf->numTables
};
otw.tables=malloc(sizeof(struct _OTF_WRITE_TABLE)*otf->numTables);
if (!otw.tables) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
return -1;
}
// just copy everything
for (iA=0;iA<otf->numTables;iA++) {
otw.tables[iA].tag=otf->tables[iA].tag;
otw.tables[iA].action=otf_action_copy;
otw.tables[iA].copy.otf=otf;
otw.tables[iA].copy.table_no=iA;
}
otw.order=otf_tagorder_offset_sort(otf);
const int ret=otf_write_sfnt(&otw,woff,output,context);
free(otw.order);
free(otw.tables);
return ret;
}
// }}}
// TODO: remove
int otf_ttc_extract(OTF_FILE *otf,OUTPUT_FN output,void *context) // {{{
{
assert(otf->numTTC);
return otf_copy_sfnt(otf,NULL,output,context);
}
// }}}
// otw {0,}-terminated, will be modified; returns numTables for otf_write_sfnt
// also filters out (.action==NULL) entries and populates .copy.table_no
int otf_intersect_tables(OTF_FILE *otf,struct _OTF_WRITE_TABLE *tables) // {{{
{
int iA,iB,numTables=0;
for (iA=0,iB=0;(iA<otf->numTables)&&(tables[iB].tag);) {
if (otf->tables[iA].tag==tables[iB].tag) {
if (tables[iB].action) {
if (tables[iB].action==otf_action_copy) {
tables[iB].copy.table_no=iA; // original table location found.
}
if (iB!=numTables) { // >, actually
memmove(tables+numTables,tables+iB,sizeof(struct _OTF_WRITE_TABLE));
}
numTables++;
} // else skip
iA++;
iB++;
} else if (otf->tables[iA].tag<tables[iB].tag) {
iA++;
} else { // not in otf->tables
if ( (tables[iB].action)&&(tables[iB].action!=otf_action_copy) ) { // keep
if (iB!=numTables) { // >, actually
memmove(tables+numTables,tables+iB,sizeof(struct _OTF_WRITE_TABLE));
}
numTables++;
} // else delete
iB++;
}
}
return numTables;
}
// }}}
// include components (set bit in >glyphs) of currently loaded compound glyph (with >curgid)
// returns additional space requirements (when bits below >donegid are touched)
static int otf_subset_glyf(OTF_FILE *otf,int curgid,int donegid,BITSET glyphs) // {{{
{
int ret=0;
if (get_SHORT(otf->gly)>=0) { // not composite
return ret; // done
}
char *cur=otf->gly+10;
unsigned short flags;
do {
flags=get_USHORT(cur);
const unsigned short sub_gid=get_USHORT(cur+2);
assert(sub_gid<otf->numGlyphs);
if (!bit_check(glyphs,sub_gid)) {
// bad: temporarily load sub glyph
const int len=otf_get_glyph(otf,sub_gid);
assert(len>0);
bit_set(glyphs,sub_gid);
if (sub_gid<donegid) {
ret+=len;
ret+=otf_subset_glyf(otf,sub_gid,donegid,glyphs); // composite of composites?, e.g. in DejaVu
}
const int res=otf_get_glyph(otf,curgid); // reload current glyph
assert(res);
}
// skip parameters
cur+=6;
if (flags&0x01) {
cur+=2;
}
if (flags&0x08) {
cur+=2;
} else if (flags&0x40) {
cur+=4;
} else if (flags&0x80) {
cur+=8;
}
} while (flags&0x20); // more components
return ret;
}
// }}}
// TODO: cmap only required in non-CID context
int otf_subset2(OTF_FILE *otf,BITSET glyphs,SUBSET_DESTINATION dest,struct _OTF_WRITE_WOFF *woff,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
assert(otf);
assert(glyphs);
assert(output);
int iA,b,c;
// first pass: include all required glyphs
bit_set(glyphs,0); // .notdef always required
int glyfSize=0;
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
if (!c) {
b++;
c=1;
}
if (glyphs[b]&c) {
int len=otf_get_glyph(otf,iA);
if (len<0) {
assert(0);
return -1;
} else if (len>0) {
glyfSize+=len;
len=otf_subset_glyf(otf,iA,iA,glyphs);
if (len<0) {
assert(0);
return -1;
}
glyfSize+=len;
}
}
}
// second pass: calculate new glyf and loca
int locaSize=(otf->numGlyphs+1)*(otf->indexToLocFormat+1)*2;
char *new_loca=malloc(otf_padded(locaSize));
char *new_glyf=malloc(otf_padded(glyfSize));
if ( (!new_loca)||(!new_glyf) ) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
assert(0);
free(new_loca);
free(new_glyf);
return -1;
}
int offset=0;
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
if (!c) {
b++;
c=1;
}
assert(offset%2==0);
// TODO? change format? if glyfSize<0x20000
if (otf->indexToLocFormat==0) {
set_USHORT(new_loca+iA*2,offset/2);
} else { // ==1
set_ULONG(new_loca+iA*4,offset);
}
if (glyphs[b]&c) {
const int len=otf_get_glyph(otf,iA);
assert(len>=0);
memcpy(new_glyf+offset,otf->gly,len);
offset+=len;
}
}
// last entry
if (otf->indexToLocFormat==0) {
set_USHORT(new_loca+otf->numGlyphs*2,offset/2);
} else { // ==1
set_ULONG(new_loca+otf->numGlyphs*4,offset);
}
assert(offset==glyfSize);
// determine new tables.
struct _OTF_WRITE_TABLE tables[]={ // sorted
// TODO: cmap only required in non-CID context or always in CFF
{OTF_TAG('O','S','/','2'),NULL,.copy={otf,}}, // 0
{OTF_TAG('c','m','a','p'),otf_action_copy,.copy={otf,}},
{OTF_TAG('c','v','t',' '),otf_action_copy,.copy={otf,}},
{OTF_TAG('f','p','g','m'),otf_action_copy,.copy={otf,}},
{OTF_TAG('g','l','y','f'),otf_action_replace,.replace={new_glyf,glyfSize}},
{OTF_TAG('h','e','a','d'),otf_action_copy,.copy={otf,}}, // 5 // _copy_head
{OTF_TAG('h','h','e','a'),otf_action_copy,.copy={otf,}},
{OTF_TAG('h','m','t','x'),otf_action_copy,.copy={otf,}},
{OTF_TAG('l','o','c','a'),otf_action_replace,.replace={new_loca,locaSize}},
{OTF_TAG('m','a','x','p'),otf_action_copy,.copy={otf,}},
{OTF_TAG('n','a','m','e'),otf_action_copy,.copy={otf,}}, // 10
{OTF_TAG('p','o','s','t'),NULL,.copy={otf,}},
{OTF_TAG('p','r','e','p'),otf_action_copy,.copy={otf,}},
// vhea vmtx (never used in PDF, but possible in PS>=3011)
{0,0,}};
if (dest==SUBSET_DST_BROWSER) {
// esp. chrome:
tables[0].action=otf_action_copy; // OS/2
tables[11].action=otf_action_copy; // post
}
// and write them
int numTables=otf_intersect_tables(otf,tables);
struct _OTF_WRITE_INFO otw={
.version=otf->version,
.numTables=numTables,
.tables=tables,
.order=otf_tagorder_win_sort(tables,numTables)
};
const int ret=otf_write_sfnt(&otw,woff,output,context);
free(otw.order);
free(new_loca);
free(new_glyf);
return ret;
//TODO ? reduce cmap [to (1,0) ;-)]
//TODO (cmap for non-cid)
}
// }}}
int otf_subset(OTF_FILE *otf,BITSET glyphs,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
return otf_subset2(otf,glyphs,SUBSET_DST_PDF,NULL,output,context);
}
// }}}
// TODO no subsetting actually done (for now)
// TODO? which tables can be recreated from the CFF infos?
int otf_subset_cff2(OTF_FILE *otf,BITSET glyphs,SUBSET_DESTINATION dest,struct _OTF_WRITE_WOFF *woff,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
assert(otf);
assert(output);
// TODO char *new_cff=cff_subset(...);
// determine new tables.
struct _OTF_WRITE_TABLE tables[]={
{OTF_TAG('C','F','F',' '),otf_action_copy,.copy={otf,}}, // 0
// {OTF_TAG('C','F','F',' '),otf_action_replace,.replace={new_glyf,glyfSize}},
// also needed for (e.g.) chrome:
{OTF_TAG('O','S','/','2'),NULL,.copy={otf,}},
{OTF_TAG('c','m','a','p'),otf_action_copy,.copy={otf,}},
// not actually needed in pdf:
{OTF_TAG('h','e','a','d'),otf_action_copy,.copy={otf,}},
{OTF_TAG('h','h','e','a'),otf_action_copy,.copy={otf,}},
{OTF_TAG('h','m','t','x'),otf_action_copy,.copy={otf,}}, // 5
{OTF_TAG('m','a','x','p'),otf_action_copy,.copy={otf,}},
{OTF_TAG('n','a','m','e'),otf_action_copy,.copy={otf,}},
{OTF_TAG('p','o','s','t'),NULL,.copy={otf,}},
{0,0,}};
if (dest==SUBSET_DST_BROWSER) {
// e.g. chrome:
tables[1].action=otf_action_copy; // OS/2
tables[8].action=otf_action_copy; // post
} else {
tables[3].action=NULL; // head
tables[4].action=NULL;
tables[5].action=NULL;
tables[6].action=NULL;
tables[7].action=NULL; // name
}
// and write them
const int numTables=otf_intersect_tables(otf,tables);
struct _OTF_WRITE_INFO otw={
.version=otf->version,
.numTables=numTables,
.tables=tables,
.order=otf_tagorder_win_sort(tables,numTables)
};
const int ret=otf_write_sfnt(&otw,woff,output,context);
free(otw.order);
// free(new_cff);
return ret;
}
// }}}
int otf_subset_cff(OTF_FILE *otf,BITSET glyphs,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
return otf_subset_cff2(otf,glyphs,SUBSET_DST_PDF,NULL,output,context);
}
// }}}
//int copy_block(FILE *f,long pos,int length,OUTPUT_FN output,void *context); // copied bytes or -1 (also on premature EOF)
static int copy_block(FILE *f,long pos,int length,OUTPUT_FN output,void *context) // {{{
{
assert(f);
assert(output);
char buf[4096];
int iA,ret;
ret=fseek(f,pos,SEEK_SET);
if (ret==-1) {
fprintf(stderr,"Seek failed: %s\n", strerror(errno));
return -1;
}
ret=0;
while (length>4096) {
iA=fread(buf,1,4096,f);
if (iA<4096) {
return -1;
}
(*output)(buf,iA,context);
ret+=iA;
length-=iA;
};
iA=fread(buf,1,length,f);
if (iA<length) {
return -1;
}
(*output)(buf,iA,context);
ret+=iA;
return ret;
}
// }}}
int otf_cff_extract(OTF_FILE *otf,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
assert(otf);
assert(output);
if (otf->flags&OTF_F_WOFF) {
int len;
char *data=otf_get_table(otf,OTF_TAG('C','F','F',' '),&len);
if (!data) {
return -1;
}
(*output)(data,len,context);
return len;
}
int idx=otf_find_table(otf,OTF_TAG('C','F','F',' '));
if (idx==-1) {
return -1;
}
const OTF_DIRENT *table=otf->tables+idx;
return copy_block(otf->f,table->offset,table->length,output,context);
}
// }}}
// CFF *otf_get_cff(); // not load, but create by "substream"-in ctor
#if 0 // TODO elsewhere : char *cff_subset(...);
// first pass: include all required glyphs
bit_set(glyphs,0); // .notdef always required
int glyfSize=0;
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
if (!c) {
b++;
c=1;
}
if (glyphs[b]&c) {
// TODO: cff_glyph
}
}
// second pass: calculate new glyf and loca
char *new_cff=malloc(cffSize);
if (!new_cff) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
assert(0);
return -1;
}
int offset=0;
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
if (!c) {
b++;
c=1;
}
if (glyphs[b]&c) {
//...
}
}
return new_cff;
#endif