-
Notifications
You must be signed in to change notification settings - Fork 1
/
cff.c
616 lines (564 loc) · 13.1 KB
/
cff.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#include "cff.h"
#include "cff_int.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// NOTE: only support Type 2 charstrings for now
// TODO cff_read_dict
// TODO: add ,maxlen to read_integer(?), read_real and read_dict_token ... write_?
// cff_dict_get(dict,key...)
// TODO? cff_write_dict, cff_add_to_dict(?)
// DICT: only two, privateDict and topDict.
// - get_from_dict(dict*,operator data structure) // has to return default, if not there. TODO?! check/assert if op is valid for this dict?
// ? cff_index_realloc(count) cff_index_add_data(size?) [? only for r/w data]
#include <math.h>
double cff_read_real(const char **buf) // {{{ NAN on error
{
int iA,iB=0;
char tmp[250];
while (1) {
const unsigned char b=**buf;
const unsigned char n1=b&0x0f;
const unsigned char n[2]={b>>4, n1};
for (iA=0;iA<2;iA++) {
switch (n[iA]) {
case 0x0a:
tmp[iB++]='.';
break;
case 0x0b:
tmp[iB++]='E';
tmp[iB++]='+';
break;
case 0x0c:
tmp[iB++]='E';
tmp[iB++]='-';
break;
case 0x0d:
assert(0);
break;
case 0x0e:
tmp[iB++]='-';
break;
case 0x0f:
tmp[iB++]=0;
break;
default: // 0-9
tmp[iB++]=n[iA]+'0';
break;
}
}
(*buf)++;
assert(iB<200);
if (iB>=200) {
return NAN;
}
if (n1==0xf) {
break;
}
}
char *end;
double ret=strtod(tmp,&end);
assert(!*end);
if (*end) {
return NAN;
}
return ret;
}
// }}}
// writes max 10 chars
void cff_write_real(char **buf,double value) // {{{
{
char tmp[20];
int res=snprintf(tmp,250,"%G",value);
assert(res<15);
int iA,pos=0;
for (iA=0;iA<res;iA++) {
char t=tmp[iA];
if ( (t>='0')&&(t<='9') ) {
t-='0';
} else if (t=='-') {
t=0xe;
} else if (t=='.') {
t=0xa;
} else if (t=='E') {
if (tmp[iA+1]=='-') {
iA++;
t=0xc;
} else if (tmp[iA+1]=='+') {
iA++;
t=0xb;
} else { // snprintf does not produce this...
assert(0);
t=0xb;
}
if (tmp[iA+1]=='0') {
iA++;
}
} else {
assert(0);
break;
}
if (pos==0) {
(**buf)=t<<4;
} else {
*((*buf)++)|=t;
}
pos^=1;
}
// end marker
if (pos==0) {
*((*buf)++)=0xff;
} else {
*((*buf)++)|=0xf;
}
}
// }}}
int32_t cff_read_integer(const char **buf) // {{{
{
const char *b=*buf;
const unsigned char b0=*b;
if (b0==28) { // size 3
(*buf)+=3;
return (b[1]<<8)|
(unsigned char)b[2];
} else if (b0==29) { // size 5
(*buf)+=5;
return (b[1]<<24)|
((unsigned char)b[2]<<16)|
((unsigned char)b[3]<<8)|
(unsigned char)b[4];
} else if (b0<32) {
// 30 real number, 31 reserved, <28 opval/reserved
} else if (b0<247) { // size 1
(*buf)++;
return (int)b0-139;
} else if (b0<251) { // size 2
(*buf)+=2;
return (b0-247)*256+(unsigned char)b[1]+108;
} else if (b0<255) { // size 2
(*buf)+=2;
return -(b0-251)*256-(unsigned char)b[1]-108;
} // else: 255 reserved
assert(0);
return 0;
}
// }}}
#include "cff_tables.h"
// error: .type==CFF_VAL_ERROR
struct _CFF_VALUE cff_read_dict_token(const char **buf) // {{{
{
struct _CFF_VALUE ret;
const char *b=*buf;
const unsigned char b0=*b;
if (b0<22) { // operator, size 1 or size 2
int opvalue=b0;
(*buf)++;
if (opvalue==12) {
opvalue=(opvalue<<8)|((unsigned char)b[1]);
(*buf)++;
}
ret.op=cffop_get_by_value(opvalue);
if (!ret.op) {
ret.type=CFF_VAL_ERROR;
} else {
ret.type=CFF_VAL_OP;
}
} else if (b0==30) { // real, varsize
(*buf)++;
ret.real=cff_read_real(buf);
if (isnan(ret.real)) {
ret.type=CFF_VAL_ERROR;
} else {
ret.type=CFF_VAL_REAL;
}
} else if ( (b0>=28)&&(b0!=31)&&(b0<=254) ) {
ret.type=CFF_VAL_INT;
ret.number=cff_read_integer(buf);
} else { // 22-27,31,255 reserved
assert(0);
ret.type=CFF_VAL_ERROR;
}
return ret;
}
// }}}
void cff_write_integer(char **buf,int32_t value) // {{{
{
char *b=*buf;
if ( (value>=-107)&&(value<=107) ) { // size 1
b[0]=value+139;
(*buf)++;
} else if ( (value>=108)&&(value<=1131) ) { // size 2
value-=108;
b[0]=(value>>8)+247;
b[1]=value&0xff;
(*buf)+=2;
} else if ( (value>=-1131)&&(value<=-108) ) { // size 2
value=-value-108;
b[0]=(value>>8)+251;
b[1]=value&0xff;
(*buf)+=2;
} else if ( (value>=-32768)&&(value<=32767) ) { // size 3
b[0]=28;
b[1]=(value>>8)&0xff;
b[2]=value&0xff;
(*buf)+=3;
} else { // size 5
b[0]=29;
b[1]=(value>>24)&0xff;
b[2]=(value>>16)&0xff;
b[3]=(value>>8)&0xff;
b[4]=value&0xff;
(*buf)+=5;
}
}
// }}}
void cff_write_dict_token(char **buf,const struct _CFF_VALUE *value) // {{{
{
assert(value);
switch (value->type) {
case CFF_VAL_ERROR:
default:
assert(0);
return;
case CFF_VAL_OP:
if (value->op->value>=0x0c00) {
*((*buf)++)=12;
*((*buf)++)=(value->op->value)&0xff;
} else {
*((*buf)++)=value->op->value;
}
break;
case CFF_VAL_INT:
cff_write_integer(buf,value->number);
break;
case CFF_VAL_REAL:
*((*buf)++)=30;
cff_write_real(buf,value->real);
break;
}
}
// }}}
// TODO: length check
// TODO: basic type check (SID must be int<65000, etc...)
// TODO? verify dict type?
// TODO: verify empty stack at the end (last token is an op)
struct _CFF_DICT *cff_read_dict(const char *start,int length) // {{ {
{
assert(start);
const char *end=start+length;
struct _CFF_DICT *ret=malloc(sizeof(struct _CFF_DICT)+20*sizeof(struct _CFF_VALUE));
if (!ret) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
return NULL;
}
ret->alloc=20;
ret->len=0;
while (start<end) {
if (ret->len>=ret->alloc) {
ret->alloc+=20;
struct _CFF_DICT *tmp=realloc(ret,sizeof(struct _CFF_DICT)+ret->alloc*sizeof(struct _CFF_VALUE));
if (!tmp) {
fprintf(stderr,"Bad realloc: %s\n", strerror(errno));
free(ret);
return NULL;
}
ret=tmp;
}
// FIXME: length check
ret->data[ret->len]=cff_read_dict_token(&start);
if (ret->data[ret->len].type==CFF_VAL_ERROR) {
fprintf(stderr,"Bad DICT token\n");
free(ret);
return NULL;
}
ret->len++;
}
return ret;
}
// }} }
// Dict: (const char *dictStart,int length)
// maybe load into better datastructure, either a general ADT, or two specific ADTs, one for Top Dict and one for Private Dict
// general dict is easier (to populate) ... but we're in plain C
// idea: just { int key(op value) [or even CFF_OPS *]; union { ...value... } }[] and qsort AFTER populating it -> still fast lookup
const char *cff_get_topdict(/*???topdict[fontname]???,*/const char *key)
{
const struct CFF_OPS *res=cffop_get_by_name(key);
if (!res) {
assert(0);
return NULL;
}
// find
/*
int last=0;
for (int iB=0;iB<topdict.size();iB++) {
if (topdict[iB]==12) {
iB++;
if ( (opnames[iA].value>21)&&
(topdict[iB]==(opnames[iA].value&0xff)) ) { // found
return topdict+last;
}
last=iB+1;
} else if (topdict[iB]<22) {
if (topdict[iB]==opnames[iA].value) { // found
return topdict+last;
}
last=iB+1;
}
}
*/
return res->defval; // or NULL
}
const char *cff_read_privateDICT(const char *key)
{
return NULL;
}
void cff_load()
{
// Name INDEX
// names=cff_read_INDEX;
// assert(names.count>=1); // NOTE: .count==1 MUST for OTF
// ... *names[iA]==0 -> deleted
// Top DICT INDEX
// topdict=cff_read_INDEX;
// ... topdict[iA] for names[iA]...
// td=cff_read_DICT(topdict[iA]);
}
/*
Free:
Encodings
Charsets
FDSelect // only CIF
CharString INDEX
Font DICT INDEX // CID (=per-font Private DICT)
Private DICT
Local Subr INDEX
Copyright and Trademark
*/
const char *cff_get_string(CFF_FILE *cff,int sid,int *retlen) // {{{ or NULL
{
assert(cff);
assert( (sid>=0)&&(sid<=65000) );
if (sid<=390) {
const char *ret=cff_get_stdstr(sid);
if (retlen) {
*retlen=strlen(ret);
}
return ret;
}
sid-=391;
if (cff->stringIndex.count==0) {
return NULL;
}
assert(cff->stringIndex.offSize!=OFFERR);
if (retlen) {
*retlen=cff_index_length(&cff->stringIndex,sid);
}
return cff_index_get(&cff->stringIndex,sid);
}
// }}}
/* --- */
static void cff_index_free_data(struct CFF_Index *idx) // {{{
{
assert(idx);
if (idx) {
free(idx->offset);
}
}
// }}}
static int cff_load_index(CFF_FILE *cff,int pos,struct CFF_Index *idx) // {{{
{
assert(idx);
assert(idx->count==0); // not already initialized
idx->offset=NULL;
if (pos+2>=cff->length) {
return -1;
}
const char *buf=cff->font+pos;
idx->count=cff_read_Card16(&buf);
if (idx->count==0) {
return buf-cff->font; // pos+2
}
if (pos+3+idx->count+1>=cff->length) {
return -1;
}
idx->offSize=cff_read_OffSize(&buf);
if (idx->offSize==OFFERR) {
return -1;
} else if (pos+3+(idx->count+1)*idx->offSize>=cff->length) {
return -1;
}
idx->offset=malloc((idx->count+1)*sizeof(*idx->offset));
if (!idx->offset) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
return -1;
}
uint32_t last=1;
int iA;
const int len=idx->count+1;
for (iA=0;iA<len;iA++) {
const uint32_t res=cff_read_Offset(&buf,idx->offSize);
if (res<last) {
free(idx->offset);
idx->offset=NULL;
fprintf(stderr,"Bad INDEX\n");
return -1;
}
last=res;
idx->offset[iA]=res;
}
idx->dataStart=buf;
// pos+3+(idx->count+1)*idx->offSize+last-1;
const int ret=buf+last-1-cff->font;
if (ret>cff->length) { // == is ok (file ends directly after us)
return -1;
}
return ret;
}
// }}}
static int cff_read_header(CFF_FILE *cff) // {{{
{
const char *buf=cff->font;
if (cff->length<3) {
fprintf(stderr,"Not a cff font / too short\n");
return -1;
}
cff->majorVersion=cff_read_Card8(&buf);
cff->minorVersion=cff_read_Card8(&buf);
uint8_t hdrSize=cff_read_Card8(&buf);
cff->absOffSize=cff_read_OffSize(&buf);
if (hdrSize<4) {
fprintf(stderr,"cff header not recognized\n");
return -1;
}
if (cff->majorVersion!=1) {
fprintf(stderr,"Unsupported cff font version\n");
return -1;
}
assert(buf-cff->font<=hdrSize);
return hdrSize;
}
// }}}
// TODO?
static int cff_do_load(CFF_FILE *cff,int pos) // {{{
{
assert(cff);
pos=cff_load_index(cff,pos,&cff->nameIndex);
if (pos<0) {
fprintf(stderr,"Could not load Name INDEX\n");
return -1;
}
pos=cff_load_index(cff,pos,&cff->topDictIndex);
if (pos<0) {
fprintf(stderr,"Could not load Top Dict INDEX\n");
return -1;
}
pos=cff_load_index(cff,pos,&cff->stringIndex);
if (pos<0) {
fprintf(stderr,"Could not load String INDEX\n");
return -1;
}
pos=cff_load_index(cff,pos,&cff->globalSubrIndex);
if (pos<0) {
fprintf(stderr,"Could not load Global Subr INDEX\n");
return -1;
}
// ? encodings (via topIndex) - or: on first access
// ? charsets (via topIndex)
return pos;
}
// }}}
/* --- */
static CFF_FILE *cff_new() // {{{
{
CFF_FILE *ret=calloc(1,sizeof(CFF_FILE));
if (!ret) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
}
return ret;
}
// }}}
CFF_FILE *cff_load2(const char *buf,size_t len,int take) // {{{
{
assert(buf);
CFF_FILE *ret=cff_new();
if (ret) {
if (take) {
ret->flags|=CFF_F_FREE_DATA;
}
ret->font=buf;
ret->length=len;
int pos=cff_read_header(ret);
if (pos>=0) {
pos=cff_do_load(ret,pos);
}
if (pos<0) {
cff_close(ret);
return NULL;
}
} else if (take) {
free((char *)buf);
}
return ret;
}
// }}}
// idea: population mechanism (if (!there): load) [but we don't know many lengths]
// two cases: a) full buffer already loaded -> pointer
// b) memory must be allocated (and freed)
CFF_FILE *cff_load_file(FILE *f) // {{{
{
#define BLOCKSIZE 1024
char *buf=NULL;
int pos=0,len=0;
while (1) {
if (pos>=len) {
len+=BLOCKSIZE;
char *tmp=realloc(buf,len);
if (!tmp) {
fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
free(buf);
return NULL;
}
buf=tmp;
}
size_t res=fread(buf+pos,1,len-pos,f);
if (res==0) {
if (!feof(f)) {
fprintf(stderr,"File read error: %s\n", strerror(errno));
free(buf);
return NULL;
}
break;
}
pos+=res;
}
return cff_load2(buf,pos,1);
#undef BLOCKSIZE
}
// }}}
CFF_FILE *cff_load_filename(const char *filename) // {{{
{
FILE *f=fopen(filename,"rb");
if (!f) {
fprintf(stderr,"Could not open \"%s\": %s\n", filename, strerror(errno));
return NULL;
}
CFF_FILE *ret=cff_load_file(f);
fclose(f);
return ret;
}
// }}}
void cff_close(CFF_FILE *cff) // {{{
{
assert(cff);
if (cff) {
cff_index_free_data(&cff->globalSubrIndex);
cff_index_free_data(&cff->stringIndex);
cff_index_free_data(&cff->topDictIndex);
cff_index_free_data(&cff->nameIndex);
if (cff->flags&CFF_F_FREE_DATA) {
free((char *)cff->font);
}
free(cff);
}
}
// }}}