10
int otf_ttc_extract(OTF_FILE *otf,OUTPUT_FN output,void *context) // {{{
17
struct _OTF_WRITE *otw;
18
otw=malloc(sizeof(struct _OTF_WRITE)*otf->numTables);
20
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
24
// just copy everything
25
for (iA=0;iA<otf->numTables;iA++) {
26
otw[iA].tag=otf->tables[iA].tag;
27
otw[iA].action=otf_action_copy;
31
iA=otf_write_sfnt(otw,otf->version,otf->numTables,output,context);
38
// otw {0,}-terminated, will be modified; returns numTables for otf_write_sfnt
39
int otf_intersect_tables(OTF_FILE *otf,struct _OTF_WRITE *otw) // {{{
41
int iA,iB,numTables=0;
42
for (iA=0,iB=0;(iA<otf->numTables)&&(otw[iB].tag);) {
43
if (otf->tables[iA].tag==otw[iB].tag) {
44
if (otw[iB].action==otf_action_copy) {
45
otw[iB].length=iA; // original table location found.
47
if (iB!=numTables) { // >, actually
48
memmove(otw+numTables,otw+iB,sizeof(struct _OTF_WRITE));
53
} else if (otf->tables[iA].tag<otw[iB].tag) {
55
} else { // not in otf->tables
56
if (otw[iB].action!=otf_action_copy) { // keep
57
if (iB!=numTables) { // >, actually
58
memmove(otw+numTables,otw+iB,sizeof(struct _OTF_WRITE));
70
// include components (set bit in >glyphs) of currently loaded compound glyph (with >curgid)
71
// returns additional space requirements (when bits below >donegid are touched)
72
static int otf_subset_glyf(OTF_FILE *otf,int curgid,int donegid,BITSET glyphs) // {{{
75
if (get_SHORT(otf->gly)>=0) { // not composite
79
char *cur=otf->gly+10;
83
flags=get_USHORT(cur);
84
const unsigned short sub_gid=get_USHORT(cur+2);
85
assert(sub_gid<otf->numGlyphs);
86
if (!bit_check(glyphs,sub_gid)) {
87
// bad: temporarily load sub glyph
88
const int len=otf_get_glyph(otf,sub_gid);
90
bit_set(glyphs,sub_gid);
91
if (sub_gid<donegid) {
93
ret+=otf_subset_glyf(otf,sub_gid,donegid,glyphs); // composite of composites?, e.g. in DejaVu
95
const int res=otf_get_glyph(otf,curgid); // reload current glyph
106
} else if (flags&0x40) {
108
} else if (flags&0x80) {
111
} while (flags&0x20); // more components
117
// TODO: cmap only required in non-CID context
118
int otf_subset(OTF_FILE *otf,BITSET glyphs,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
126
// first pass: include all required glyphs
127
bit_set(glyphs,0); // .notdef always required
129
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
135
int len=otf_get_glyph(otf,iA);
141
len=otf_subset_glyf(otf,iA,iA,glyphs);
151
// second pass: calculate new glyf and loca
152
int locaSize=(otf->numGlyphs+1)*(otf->indexToLocFormat+1)*2;
154
char *new_loca=malloc(locaSize);
155
char *new_glyf=malloc(glyfSize);
156
if ( (!new_loca)||(!new_glyf) ) {
157
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
165
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
172
// TODO? change format? if glyfSize<0x20000
173
if (otf->indexToLocFormat==0) {
174
set_USHORT(new_loca+iA*2,offset/2);
176
set_ULONG(new_loca+iA*4,offset);
180
const int len=otf_get_glyph(otf,iA);
182
memcpy(new_glyf+offset,otf->gly,len);
187
if (otf->indexToLocFormat==0) {
188
set_USHORT(new_loca+otf->numGlyphs*2,offset/2);
190
set_ULONG(new_loca+otf->numGlyphs*4,offset);
192
assert(offset==glyfSize);
194
// determine new tables.
195
struct _OTF_WRITE otw[]={ // sorted
196
// TODO: cmap only required in non-CID context or always in CFF
197
{OTF_TAG('c','m','a','p'),otf_action_copy,otf,},
198
{OTF_TAG('c','v','t',' '),otf_action_copy,otf,},
199
{OTF_TAG('f','p','g','m'),otf_action_copy,otf,},
200
{OTF_TAG('g','l','y','f'),otf_action_replace,new_glyf,glyfSize},
201
{OTF_TAG('h','e','a','d'),otf_action_copy,otf,}, // _copy_head
202
{OTF_TAG('h','h','e','a'),otf_action_copy,otf,},
203
{OTF_TAG('h','m','t','x'),otf_action_copy,otf,},
204
{OTF_TAG('l','o','c','a'),otf_action_replace,new_loca,locaSize},
205
{OTF_TAG('m','a','x','p'),otf_action_copy,otf,},
206
{OTF_TAG('n','a','m','e'),otf_action_copy,otf,},
207
{OTF_TAG('p','r','e','p'),otf_action_copy,otf,},
208
// vhea vmtx (never used in PDF, but possible in PS>=3011)
212
int numTables=otf_intersect_tables(otf,otw);
213
int ret=otf_write_sfnt(otw,otf->version,numTables,output,context);
219
//TODO ? reduce cmap [to (1,0) ;-)]
220
//TODO (cmap for non-cid)
224
// TODO no subsetting actually done (for now)
225
int otf_subset_cff(OTF_FILE *otf,BITSET glyphs,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
230
// TODO char *new_cff=cff_subset(...);
232
// determine new tables.
233
struct _OTF_WRITE otw[]={
234
{OTF_TAG('C','F','F',' '),otf_action_copy,otf,},
235
// {OTF_TAG('C','F','F',' '),otf_action_replace,new_glyf,glyfSize},
236
{OTF_TAG('c','m','a','p'),otf_action_copy,otf,},
237
#if 0 // not actually needed!
238
{OTF_TAG('c','v','t',' '),otf_action_copy,otf,},
239
{OTF_TAG('f','p','g','m'),otf_action_copy,otf,},
240
{OTF_TAG('h','e','a','d'),otf_action_copy,otf,}, // _copy_head
241
{OTF_TAG('h','h','e','a'),otf_action_copy,otf,},
242
{OTF_TAG('h','m','t','x'),otf_action_copy,otf,},
243
{OTF_TAG('m','a','x','p'),otf_action_copy,otf,},
244
{OTF_TAG('n','a','m','e'),otf_action_copy,otf,},
245
{OTF_TAG('p','r','e','p'),otf_action_copy,otf,},
250
int numTables=otf_intersect_tables(otf,otw);
251
int ret=otf_write_sfnt(otw,otf->version,numTables,output,context);
258
//int copy_block(FILE *f,long pos,int length,OUTPUT_FN output,void *context); // copied bytes or -1 (also on premature EOF)
260
static int copy_block(FILE *f,long pos,int length,OUTPUT_FN output,void *context) // {{{
268
ret=fseek(f,pos,SEEK_SET);
270
fprintf(stderr,"Seek failed: %s\n", strerror(errno));
274
while (length>4096) {
275
iA=fread(buf,1,4096,f);
279
(*output)(buf,iA,context);
283
iA=fread(buf,1,length,f);
287
(*output)(buf,iA,context);
294
int otf_cff_extract(OTF_FILE *otf,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
299
int idx=otf_find_table(otf,OTF_TAG('C','F','F',' '));
303
const OTF_DIRENT *table=otf->tables+idx;
305
return copy_block(otf->f,table->offset,table->length,output,context);
309
// CFF *otf_get_cff(); // not load, but create by "substream"-in ctor
310
#if 0 // TODO elsewhere : char *cff_subset(...);
311
// first pass: include all required glyphs
312
bit_set(glyphs,0); // .notdef always required
314
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
324
// second pass: calculate new glyf and loca
325
char *new_cff=malloc(cffSize);
327
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
333
for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {