Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/cffwrite/cffwrite.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * Compact Font Format Generation Library.
7 */
8
9 #include "txops.h"
10 #include "dictops.h"
11 #include "supportexcept.h"
12
13 #include "cffwrite_charset.h"
14 #include "cffwrite_encoding.h"
15 #include "cffwrite_fdselect.h"
16 #include "cffwrite_sindex.h"
17 #include "cffwrite_dict.h"
18 #include "cffwrite_varstore.h"
19 #include "cffwrite_t2cstr.h"
20 #include "cffwrite_subr.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #define CFF_HDR_SIZE 4 /* CFF v1.0 header size */
27 #define CFF2_HDR_SIZE 5 /* CFF v2.0 header size */
28
29 /* Width optimization seeks to find two values called the default and nominal
30 width. Glyphs with the default width omit the width altogether and all other
31 widths are reconstituted by subtracting the nominal width from the encoded
32 width found in the charstring. The choice of nominal width and default width
33 interact. For example, choosing a nominal width near a default width will
34 make the potential default width savings smaller. By carefully choosing a
35 nominal width many widths will shrink by one byte because smaller numbers
36 are encoded in fewer bytes. */
37
38 typedef struct /* Width frequency record */
39 {
40 float width;
41 long count;
42 } WidthFreq;
43
44 typedef struct /* FDArray element */
45 {
46 abfFontDict dict; /* Font dict data (copied from client) */
47 abfPrivateDict Private; /* Private dict data (copied from client) */
48 struct /* CFF DICT data */
49 {
50 DICT dict;
51 DICT Private;
52 } cff;
53 INDEX Subrs;
54 subr_CSData subrData;
55 struct /* CFF data sizes */
56 {
57 long Private;
58 long Subrs;
59 } size;
60 struct /* CFF data offsets */
61 {
62 Offset Private;
63 Offset Subrs;
64 } offset;
65 struct /* Width analysis */
66 {
67 dnaDCL(WidthFreq, freqs);
68 float dflt;
69 float nominal;
70 } width;
71 } FDInfo;
72
73 typedef struct Glyph_ /* Glyph data */
74 {
75 abfGlyphInfo *info;
76 float hAdv;
77 struct /* Charstring */
78 {
79 long length;
80 long offset;
81 } cstr;
82 uint16_t iFD; /* Modifiable and persistent copy of info->iFD */
83 } Glyph;
84
85 typedef struct /* Glyph data */
86 {
87 long glyphsIndex; /* index of this glyph in Font.glyphs.array */
88 abfGlyphInfo info; /* if the entry exists, it has been seen. */
89 } SeenGlyph;
90
91 typedef struct /* sorted FontDict data */
92 {
93 long fdIndex; /* index of this dict in h->_new.FDArray */
94 } SeenDict;
95
96 typedef struct /* Per-font data */
97 {
98 long flags; /* Font-specific flags */
99 #define FONT_CID (1 << 0) /* CID font */
100 #define FONT_SYN (1 << 1) /* Synthetic font */
101 abfTopDict top; /* Top dict data (copied from client) */
102 struct /* CFF DICT data */
103 {
104 DICT top;
105 DICT varStore;
106 DICT synthetic; /* Synthetic Top DICT */
107 } cff;
108 dnaDCL(FDInfo, FDArray); /* FD array */
109 dnaDCL(Glyph, glyphs); /* Per-glyph data */
110 dnaDCL(SeenGlyph, seenGlyphs); /* Per-glyph data */
111 dnaDCL(SeenDict, seenDicts); /* Per-fontdict data */
112
113 INDEX CharStrings; /* CharStrings INDEX data */
114 struct /* Object indexes */
115 {
116 long charset;
117 long Encoding;
118 long FDSelect;
119 } iObject;
120 struct /* CFF data sizes */
121 {
122 long top;
123 long CharStrings;
124 long VarStore;
125 long FDArray; /* CID and CFF2 only */
126 long Private;
127 long Subrs;
128 long widths;
129 } size;
130 struct /* CFF data offsets */
131 {
132 Offset charset;
133 Offset Encoding;
134 Offset VarStore;
135 Offset FDSelect;
136 Offset CharStrings;
137 Offset FDArray; /* CID and CFF2 only */
138 Offset Private;
139 Offset Subrs;
140 } offset;
141 cfwMapCallback *map; /* Client-supplied map callbacks */
142 } cff_Font;
143
144 /* ----------------------------- Module Context ---------------------------- */
145
146 struct controlCtx_ {
147 long flags; /* FontSet specific flags */
148 #define FONTSET_SUBRIZE (1 << 0) /* Subroutinize */
149 #define FONTSET_EMBED (1 << 1) /* Perform embedding space optimization */
150 #define SEEN_NAME_KEYED_GLYPH (1 << 2) /* Seen name-keyed glyph */
151 #define SEEN_CID_KEYED_GLYPH (1 << 3) /* Seen cid-keyed glyph */
152 #define FONTSET_CFF2 (1 << 4) /* We are writing a CFF2 font. */
153 dnaDCL(cff_Font, FontSet); /* FontSet */
154 cff_Font *_new; /* New font */
155 INDEX name; /* Name INDEX */
156 struct /* Global data sizes */
157 {
158 long header;
159 long name;
160 long top;
161 long varStore;
162 long string;
163 long gsubr;
164 long charset;
165 long Encoding;
166 long FDSelect; /* CID only */
167 } size;
168 struct /* Global data offsets */
169 {
170 Offset name;
171 Offset top;
172 Offset varStore;
173 Offset string;
174 Offset gsubr;
175 Offset charset;
176 Offset Encoding;
177 Offset FDSelect; /* CID only */
178 Offset end; /* 1 byte beyond end of CFF data */
179 } offset;
180 struct /* Global data offsets */
181 {
182 char *oldGlyph; /* buffers used in merging glyphs */
183 char *newGlyph;
184 } mergeBuffers;
185 int mergedDicts; /* flag set if client has merged any dicts during glyph processing */
186 cfwCtx g; /* Package context */
187 };
188
189 static void initSubrData(subr_CSData *subrData);
190
191 /* Initialize FDInfo. */
192 static void initFDInfo(void *ctx, long cnt, FDInfo *fd) {
193 cfwCtx g = (cfwCtx)ctx;
194 while (cnt--) {
195 dnaINIT(g->ctx.dnaSafe, fd->cff.dict, 25, 50);
196 dnaINIT(g->ctx.dnaSafe, fd->cff.Private, 100, 50);
197 dnaINIT(g->ctx.dnaSafe, fd->width.freqs, 50, 50);
198 initSubrData(&fd->subrData);
199 fd++;
200 }
201 }
202
203 /* Initialize font. */
204 static void initFont(void *ctx, long cnt, cff_Font *font) {
205 cfwCtx g = (cfwCtx)ctx;
206 while (cnt--) {
207 memset(font, 0, sizeof(*font));
208 dnaINIT(g->ctx.dnaSafe, font->cff.top, 50, 50);
209 dnaINIT(g->ctx.dnaSafe, font->cff.varStore, 100, 100);
210 dnaINIT(g->ctx.dnaSafe, font->cff.synthetic, 25, 50);
211 dnaINIT(g->ctx.dnaSafe, font->FDArray, 1, 14);
212 font->FDArray.func = initFDInfo;
213 dnaINIT(g->ctx.dnaFail, font->glyphs, 256, 750);
214 dnaINIT(g->ctx.dnaFail, font->seenGlyphs, 256, 256);
215 dnaINIT(g->ctx.dnaFail, font->seenDicts, 2, 2);
216 font++;
217 }
218 }
219
220 /* Initialize module. */
221 static void cfwControlNew(cfwCtx g) {
222 controlCtx h = (controlCtx)cfwMemNew(g, sizeof(struct controlCtx_));
223
224 dnaINIT(g->ctx.dnaFail, h->FontSet, 1, 150);
225 h->FontSet.func = initFont;
226 h->mergeBuffers.newGlyph = NULL;
227 h->mergeBuffers.oldGlyph = NULL;
228 /* Link contexts */
229 h->g = g;
230 g->ctx.control = h;
231 }
232
233 static void freeSubrData(cfwCtx g, subr_CSData *subrData);
234
235 /* Free resources. */
236 static void cfwControlFree(cfwCtx g) {
237 controlCtx h = g->ctx.control;
238 int i;
239
240 if (h == NULL) {
241 return;
242 }
243
244 for (i = 0; i < h->FontSet.size; i++) {
245 int j;
246 cff_Font *font = &h->FontSet.array[i];
247 for (j = 0; j < font->FDArray.size; j++) {
248 FDInfo *fd = &font->FDArray.array[j];
249 dnaFREE(fd->cff.dict);
250 dnaFREE(fd->cff.Private);
251 dnaFREE(fd->width.freqs);
252 freeSubrData(g, &fd->subrData);
253 }
254 dnaFREE(font->cff.top);
255 dnaFREE(font->cff.varStore);
256 dnaFREE(font->cff.synthetic);
257 dnaFREE(font->FDArray);
258 dnaFREE(font->glyphs);
259 dnaFREE(font->seenGlyphs);
260 dnaFREE(font->seenDicts);
261 }
262 dnaFREE(h->FontSet);
263 if (h->mergeBuffers.newGlyph != NULL) {
264 g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.newGlyph, 0);
265 }
266 if (h->mergeBuffers.oldGlyph != NULL) {
267 g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.oldGlyph, 0);
268 }
269
270 cfwMemFree(g, h);
271 g->ctx.control = NULL;
272 }
273
274 /* ----------------------------- Glyph Merge Handling ---------------------------- */
275
276 static void fillbuf(cfwCtx g, long offset);
277
278 /* Copy count bytes from tmp stream. */
279 static void tmp2bufCopy(cfwCtx g, long length, long offset, char *ptr) {
280 long left;
281 long delta = offset - g->tmp.offset;
282
283 if (delta >= 0 && (size_t)delta < g->tmp.length) {
284 /* Offset within current buffer; reposition next byte */
285 g->tmp.next = g->tmp.buf + delta;
286 } else {
287 /* Offset outside current buffer; seek to offset and fill buffer */
288 if (g->cb.stm.seek(&g->cb.stm, g->stm.tmp, offset)) {
289 cfwFatal(g, cfwErrTmpStream, NULL);
290 }
291 fillbuf(g, offset);
292 }
293
294 /* Compute bytes left in buffer */
295 /* 64-bit warning fixed by cast here */
296 left = (long)(g->tmp.end - g->tmp.next);
297 while (left < length) {
298 /* Copy buffer */
299 memcpy(ptr, g->tmp.next, left);
300 ptr += left;
301 length -= left;
302
303 /* Refill buffer */
304 /* 64-bit warning fixed by cast here */
305 fillbuf(g, (long)(g->tmp.offset + g->tmp.length));
306 left = (long)g->tmp.length;
307 }
308
309 memcpy(ptr, g->tmp.next, length);
310 g->tmp.next += length;
311 }
312
313 /* Match glyph name. */
314 static int CTL_CDECL matchSeenName(const void *key, const void *value, void *ctx) {
315 char *compStr = ((SeenGlyph *)value)->info.gname.ptr;
316
317 return strcmp((char *)key,
318 compStr);
319 }
320
321 /* Match glyph CID. */
322 static int CTL_CDECL matchSeenCID(const void *key, const void *value, void *ctx) {
323 long match;
324 match = *(unsigned short *)key - ((SeenGlyph *)value)->info.cid;
325
326 if (match == 0) {
327 return match;
328 } else {
329 return (match > 0) ? 1 : -1;
330 }
331 }
332
333 /* Match font FD dict FontName. */
334 static int CTL_CDECL matchFontName(const void *srcName, const void *value, void *ctx) {
335 long fdIndex = ((SeenDict *)value)->fdIndex;
336 controlCtx h = (controlCtx)ctx;
337 int result;
338 char *destName = h->_new->FDArray.array[fdIndex].dict.FontName.ptr;
339
340 /* new dict with NULL FontName is always sorted last */
341 /* We also treat NULL FontName as a unique individual name. */
342 if ((srcName == NULL) || (destName == NULL)) {
343 if (destName == srcName) {
344 result = 0;
345 } else {
346 if (srcName == NULL) {
347 result = 1;
348 } else {
349 result = -1;
350 }
351 }
352 } else {
353 result = strcmp((char *)srcName, destName);
354 }
355
356 return result;
357 }
358
359 /*
360 Test if glyph has already been added to the font.
361 The function sets the result code:
362 0 glyph not yet seen
363 cfwErrGlyphPresent, "identical charstring is already present"
364 cfwErrGlyphDiffers, "different charstring of same name is already present"
365 and returns the index of the glyph in the sorted array of glyph names in the CFF. If not
366 yet in the font, the index is one greater than the index of the glyph which sorts before the new name.
367
368 */
369 long cfwSeenGlyph(cfwCtx g, abfGlyphInfo *info, int *result, long startNew, long endNew) {
370 controlCtx h = g->ctx.control;
371 long seenIndex = 0;
372 int nameFound;
373
374 *result = 0;
375
376 if (info->flags & ABF_GLYPH_CID) {
377 nameFound = ctuLookup(&info->cid, h->_new->seenGlyphs.array, h->_new->seenGlyphs.cnt,
378 sizeof(h->_new->seenGlyphs.array[0]), matchSeenCID, (size_t *)&seenIndex, h);
379 } else {
380 nameFound = ctuLookup(info->gname.ptr, h->_new->seenGlyphs.array, h->_new->seenGlyphs.cnt,
381 sizeof(h->_new->seenGlyphs.array[0]), matchSeenName, (size_t *)&seenIndex, h);
382 }
383
384 if (nameFound) {
385 int noMatch = 0;
386 long lenNewStr = endNew - startNew;
387 long glyphIndex = h->_new->seenGlyphs.array[seenIndex].glyphsIndex;
388 long lenOldStr = h->_new->glyphs.array[glyphIndex].cstr.length;
389
390 if (lenNewStr != lenOldStr) {
391 noMatch = 1;
392 } else {
393 h->mergeBuffers.newGlyph = (char *)g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.newGlyph, lenNewStr);
394 h->mergeBuffers.oldGlyph = (char *)g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.oldGlyph, lenOldStr);
395
396 /* get strings */
397 /* This is used in the context of the glyphEnd function, where
398 successive writes are used to the tmp stream. The g->tmp values
399 may therefore be all out of date, and we have to reset the
400 g->tmp values. */
401 /* re-init temp buffer */
402 g->cb.stm.seek(&g->cb.stm, g->stm.tmp, h->_new->glyphs.array[glyphIndex].cstr.offset);
403 g->tmp.offset = h->_new->glyphs.array[glyphIndex].cstr.offset;
404 g->tmp.next = g->tmp.buf; /* say that there is no current stream content in the tmp.buf */
405 g->tmp.length = 0;
406
407 tmp2bufCopy(g, lenOldStr, h->_new->glyphs.array[glyphIndex].cstr.offset, h->mergeBuffers.oldGlyph);
408 tmp2bufCopy(g, lenNewStr, startNew, h->mergeBuffers.newGlyph);
409
410 /* fix temp file position */
411 g->cb.stm.seek(&g->cb.stm, g->stm.tmp, endNew);
412
413 if (g->flags & CFW_SUBRIZE) {
414 lenNewStr -= 4; /* Skip the unique terminator for subroutinizer */
415 }
416 noMatch = strncmp(h->mergeBuffers.oldGlyph, h->mergeBuffers.newGlyph, lenNewStr);
417 }
418
419 if (noMatch == 0) {
420 *result = cfwErrGlyphPresent;
421 } else {
422 *result = cfwErrGlyphDiffers;
423 }
424 }
425
426 return seenIndex;
427 }
428
429 static long mergeDict(cfwCtx g, abfFontDict *srcDict) {
430 controlCtx h = g->ctx.control;
431 long seenIndex = -1;
432 long newFDIndex;
433 int dictFound;
434
435 h->mergedDicts = 1;
436
437 dictFound = ctuLookup(srcDict->FontName.ptr, h->_new->seenDicts.array, h->_new->seenDicts.cnt,
438 sizeof(SeenDict), matchFontName, (size_t *)&seenIndex, h);
439
440 /* if it is a new dict, then add it to the destination font */
441 if (!dictFound) {
442 int needHole = seenIndex < h->_new->seenDicts.cnt;
443 SeenDict *newSeenDict;
444 FDInfo *newFDInfo;
445 long nDicts;
446
447 /* grow h->_new.FDArray array, increment FDArray.cnt */
448 if (dnaNext(&h->_new->FDArray, sizeof(FDInfo)) == -1) {
449 g->err.code = cfwErrNoMemory;
450 return ABF_UNSET_INT;
451 }
452 nDicts = h->_new->FDArray.cnt;
453 newFDInfo = &h->_new->FDArray.array[nDicts - 1];
454
455 /* grow seenDicts array, increment SeenDict.cnt */
456 if (dnaNext(&h->_new->seenDicts, sizeof(SeenDict)) == -1) {
457 g->err.code = cfwErrNoMemory;
458 return ABF_UNSET_INT;
459 }
460
461 /* ptr to insertion-point index */
462 newSeenDict = &h->_new->seenDicts.array[seenIndex];
463
464 /* Move higher elements (if there are any) up one index to make hole,
465 and fix all the glyph->iFD indices. */
466 if (needHole) {
467 memmove(newSeenDict + 1, newSeenDict,
468 ((h->_new->seenDicts.cnt - 1) - seenIndex) * sizeof(SeenDict));
469 }
470
471 /* fill new FD array entry */
472 cfwDictCopyFont(g, &newFDInfo->dict, srcDict);
473 cfwDictCopyPrivate(g, &newFDInfo->Private, &srcDict->Private);
474 newFDInfo->Subrs.count = 0;
475 memset(&newFDInfo->subrData, 0, sizeof(subr_CSData));
476 newFDInfo->width.freqs.cnt = 0;
477
478 /* Fill new seenDicts entry */
479 newSeenDict->fdIndex = nDicts - 1;
480
481 /* What we return is the index of the new font dict in the h->_new.FDArray.
482 A new dict is always the last dist in the array. */
483 newFDIndex = nDicts - 1;
484 }
485
486 newFDIndex = h->_new->seenDicts.array[seenIndex].fdIndex;
487
488 return newFDIndex;
489 }
490
491 int cfwMergeFDArray(cfwCtx g, abfTopDict *top, int *newFDIndex) {
492 abfFontDict *srcDict = &top->FDArray.array[0];
493 int i = 0;
494
495 while (i++ < top->FDArray.cnt) {
496 if (g->err.code) {
497 *newFDIndex = ABF_UNSET_INT;
498 } else {
499 *newFDIndex = mergeDict(g, srcDict);
500 }
501 newFDIndex++;
502 srcDict++;
503 }
504
505 return g->err.code;
506 }
507
508 int cfwCompareFDArrays(cfwCtx g, abfTopDict *srcTop) {
509 controlCtx h = g->ctx.control;
510 int srcCount = srcTop->FDArray.cnt;
511 int destCount = h->_new->FDArray.cnt;
512
513 if (destCount > 0) {
514 int j = 0;
515 while (j < srcCount) {
516 long seenIndex = 0;
517 int dictFound;
518 abfFontDict *srcDict = &srcTop->FDArray.array[j++];
519
520 dictFound = ctuLookup(srcDict->FontName.ptr, h->_new->seenDicts.array, h->_new->seenDicts.cnt,
521 sizeof(SeenDict), matchFontName, (size_t *)&seenIndex, h);
522
523 if (dictFound) {
524 int iFD = h->_new->seenDicts.array[seenIndex].fdIndex;
525 abfFontDict *destDict = &h->_new->FDArray.array[iFD].dict;
526
527 if (abfCompareFontDicts(destDict, srcDict)) {
528 return 1;
529 }
530 }
531 }
532 }
533
534 return 0;
535 }
536
537 /* ----------------------------- Glyph Handling ---------------------------- */
538
539 /* Add new glyph. */
540 void cfwAddGlyph(cfwCtx g,
541 abfGlyphInfo *info, float hAdv, long length, long offset, long seen_index) {
542 controlCtx h = g->ctx.control;
543 Glyph *glyph = NULL;
544 SeenGlyph *seenGlyph = NULL;
545 long index = 0;
546
547 if (info->flags & ABF_CID_FONT) {
548 /* CID-keyed font */
549 if (info->cid == 0) {
550 /* Force CID 0 to GID 0 */
551 glyph = &h->_new->glyphs.array[0];
552 }
553 } else {
554 /* Name-keyed font */
555 if (strcmp(info->gname.ptr, ".notdef") == 0) {
556 /* Force .notdef glyph to GID 0 */
557 glyph = &h->_new->glyphs.array[0];
558 }
559
560 /* Add glyph name to string index, if we have not already seen it. */
561 info->gname.impl = cfwSindexAddString(g, info->gname.ptr);
562 }
563
564 /* h->_new->glyphs.array is initialized with one glyph entry reserved for
565 notdef, so when we see the notdef glyph info, we just copy it to that
566 slot. We need to add a new glyph entry for all other glyphs. */
567 if (glyph == NULL) {
568 /* if it is anything but notdef */
569 index = dnaNext(&h->_new->glyphs, sizeof(Glyph));
570 if (index == -1) {
571 g->err.code = cfwErrNoMemory;
572 return;
573 }
574 glyph = &h->_new->glyphs.array[index];
575 }
576
577 /* Copy glyph details */
578 glyph->info = info;
579 glyph->hAdv = hAdv;
580 glyph->cstr.length = length;
581 glyph->cstr.offset = offset;
582 glyph->iFD = info->iFD; /* Make modifiable/persistent copy */
583 /* When merging CID fonts, this must specify the
584 destination font FD array index, not the source
585 font. See mergeDict(). */
586
587 /* If we will be merging glyphs from more then one font,
588 update the seenGlyphs list. */
589
590 /* We get to here only if the new glyph does NOT have the same name as a
591 glyph which has already been seen. It is therefore added to the list.
592 'seen_index' gives the index of the new name in the alphabetic list of
593 glyph names. */
594
595 /* For CID glyphs, we also need to check if the FD is new to the dest font,
596 and if so add it, and we need to fix the glyph->iFD value. This is
597 currently an index into the source font FD array */
598 if (g->flags & CFW_CHECK_IF_GLYPHS_DIFFER) {
599 int needHole = seen_index < h->_new->seenGlyphs.cnt;
600
601 /* grow array, increment seenGlyphs.cnt */
602 if (dnaNext(&h->_new->seenGlyphs, sizeof(SeenGlyph)) == -1) {
603 g->err.code = cfwErrNoMemory;
604 return;
605 }
606
607 /* ptr to insertion-point index */
608 seenGlyph = &h->_new->seenGlyphs.array[seen_index];
609
610 /* Move higher elements (if there are any) up one index to make hole. */
611 if (needHole) {
612 memmove(seenGlyph + 1, seenGlyph,
613 ((h->_new->seenGlyphs.cnt - 1) - seen_index) * sizeof(SeenGlyph));
614 }
615
616 seenGlyph->glyphsIndex = index;
617 seenGlyph->info = *info; // I can't save a ptr to the info, as the original array of abfGlyphInfo moves when resized.
618 }
619
620 if (info->flags & ABF_GLYPH_UNICODE) {
621 /* Remove Unicode encoding since it can't be represented */
622 abfEncoding *enc = &info->encoding;
623 do {
624 enc->code = ABF_GLYPH_UNENC;
625 enc = enc->next;
626 } while (enc != NULL);
627 info->flags &= ~ABF_GLYPH_UNICODE;
628 }
629
630 /* Accumulate total charstring size */
631 h->_new->CharStrings.datasize += length;
632
633 if (h->_new->map != NULL && g->flags & CFW_PRESERVE_GLYPH_ORDER) {
634 h->_new->map->glyphmap(h->_new->map,
635 (unsigned short)(glyph - h->_new->glyphs.array),
636 glyph->info);
637 }
638 }
639
640 /* ---------------------------- FontSet Filling ---------------------------- */
641
642 /* Fill CharStrings INDEX. */
643 static long fillCharStringsINDEX(cfwCtx g, cff_Font *font) {
644 long size;
645 font->CharStrings.count = (unsigned short)font->glyphs.cnt;
646 if (!(g->flags & CFW_WRITE_CFF2)) {
647 font->CharStrings.datasize += font->size.widths;
648 }
649 font->CharStrings.offSize = INDEX_OFF_SIZE(font->CharStrings.datasize);
650
651 if (g->flags & CFW_WRITE_CFF2) {
652 size = INDEX2_SIZE(font->CharStrings.count, font->CharStrings.datasize);
653 } else {
654 size = INDEX_SIZE(font->CharStrings.count, font->CharStrings.datasize);
655 }
656 return size;
657 }
658
659 /* Calculate initial font sizes. */
660 static void initFontSizes(controlCtx h, cff_Font *font) {
661 int i;
662 cfwCtx g = h->g;
663
664 font->size.top = font->cff.top.cnt;
665
666 /* Calculate CharStrings INDEX size */
667 font->size.CharStrings = fillCharStringsINDEX(g, font);
668
669 /* Calculate VarStore INDEX size */
670 font->size.VarStore = font->cff.varStore.cnt;
671
672 /* Calculate FDArray size */
673 if ((font->flags & FONT_CID) || (g->flags & CFW_WRITE_CFF2)) {
674 long size = 0;
675 for (i = 0; i < font->FDArray.cnt; i++) {
676 size += font->FDArray.array[i].cff.dict.cnt;
677 }
678 if (g->flags & CFW_WRITE_CFF2) {
679 font->size.FDArray = INDEX2_SIZE(font->FDArray.cnt, size);
680 } else {
681 font->size.FDArray = INDEX_SIZE(font->FDArray.cnt, size);
682 }
683 } else {
684 font->size.FDArray = 0;
685 }
686
687 /* Calculate Private DICT and Subr INDEX sizes */
688 font->size.Private = 0;
689 font->size.Subrs = 0;
690 for (i = 0; i < font->FDArray.cnt; i++) {
691 FDInfo *fd = &font->FDArray.array[i];
692 font->size.Private += fd->size.Private = fd->cff.Private.cnt;
693 font->size.Subrs += fd->size.Subrs = cfwSubrSizeLocal(g, &fd->subrData);
694 }
695 }
696
697 /* Calculate font offsets. */
698 static Offset calcFontOffsets(controlCtx h, cff_Font *font, Offset offset) {
699 cfwCtx g = h->g;
700 int i;
701
702 font->offset.charset =
703 cfwCharsetGetOffset(g, font->iObject.charset, h->offset.charset);
704 font->offset.Encoding =
705 cfwEncodingGetOffset(g, font->iObject.Encoding, h->offset.Encoding);
706 font->offset.FDSelect =
707 cfwFdselectGetOffset(g, font->iObject.FDSelect, h->offset.FDSelect);
708
709 if (font->size.VarStore > 0) {
710 font->offset.VarStore = h->offset.varStore;
711 }
712 font->offset.CharStrings = offset;
713 font->offset.FDArray = font->offset.CharStrings + font->size.CharStrings;
714 font->offset.Private = font->offset.FDArray + font->size.FDArray;
715 font->offset.Subrs = font->offset.Private + font->size.Private;
716 /* Compute individual Private DICT offsets */
717 offset = font->offset.Private;
718 for (i = 0; i < font->FDArray.cnt; i++) {
719 FDInfo *fd = &font->FDArray.array[i];
720 fd->offset.Private = offset;
721 offset += fd->size.Private;
722 }
723
724 /* Compute individual Subrs INDEX offsets */
725 for (i = 0; i < font->FDArray.cnt; i++) {
726 FDInfo *fd = &font->FDArray.array[i];
727 fd->offset.Subrs = offset;
728 offset += fd->size.Subrs;
729 }
730 return offset;
731 }
732
733 /* Return size of int argument (in bytes) if formatted for DICT. */
734 static int intsize(long i) {
735 if (-107 <= i && i <= 107) {
736 return 1;
737 } else if (-1131 <= i && i <= 1131) {
738 return 2;
739 } else if (-32768 <= i && i <= 32767) {
740 return 3;
741 } else {
742 return 5;
743 }
744 }
745
746 /* Calculate font sizes. */
747 static void calcFontSizes(controlCtx h, cff_Font *font) {
748 int i;
749 long size;
750 cfwCtx g = h->g;
751
752 /* Calculate Top DICT size */
753 if (g->flags & CFW_WRITE_CFF2) {
754 size = font->cff.top.cnt;
755 size += intsize(font->offset.CharStrings) + DICT_OP_SIZE(cff_CharStrings);
756
757 if (font->offset.VarStore > 0)
758 size += (intsize(font->offset.VarStore) + DICT_OP_SIZE(cff_VarStore));
759
760 if (font->offset.FDSelect > 0)
761 size += (intsize(font->offset.FDSelect) + DICT_OP_SIZE(cff_FDSelect));
762
763 size += (intsize(font->offset.FDArray) + DICT_OP_SIZE(cff_FDArray));
764 font->size.top = size;
765
766 /* Calculate FDArray size */
767 size = 0;
768 for (i = 0; i < font->FDArray.cnt; i++) {
769 FDInfo *fd = &font->FDArray.array[i];
770 size += (fd->cff.dict.cnt +
771 intsize(fd->size.Private) + intsize(fd->offset.Private) +
772 DICT_OP_SIZE(cff_Private));
773 }
774
775 font->size.FDArray = INDEX2_SIZE(font->FDArray.cnt, size);
776
777 } else {
778 size = font->cff.top.cnt;
779 switch (font->offset.charset) {
780 case cff_ISOAdobeCharset:
781 break;
782
783 case cff_ExpertCharset:
784 case cff_ExpertSubsetCharset:
785 default:
786 size += intsize(font->offset.charset) + DICT_OP_SIZE(cff_charset);
787 break;
788 }
789
790 switch (font->offset.Encoding) {
791 case cff_StandardEncoding:
792 break;
793
794 case cff_ExpertEncoding:
795 default:
796 size += intsize(font->offset.Encoding) + DICT_OP_SIZE(cff_Encoding);
797 break;
798 }
799
800 size += intsize(font->offset.CharStrings) + DICT_OP_SIZE(cff_CharStrings);
801
802 if (font->flags & FONT_CID) {
803 size += (intsize(font->offset.FDSelect) + DICT_OP_SIZE(cff_FDSelect) +
804 intsize(font->offset.FDArray) + DICT_OP_SIZE(cff_FDArray));
805 } else {
806 size += (intsize(font->size.Private) + intsize(font->offset.Private) +
807 DICT_OP_SIZE(cff_Private));
808 }
809 font->size.top = size;
810
811 if (font->flags & FONT_CID) {
812 /* Calculate FDArray size */
813 size = 0;
814 for (i = 0; i < font->FDArray.cnt; i++) {
815 FDInfo *fd = &font->FDArray.array[i];
816 size += (fd->cff.dict.cnt +
817 intsize(fd->size.Private) + intsize(fd->offset.Private) +
818 DICT_OP_SIZE(cff_Private));
819 }
820
821 font->size.FDArray = INDEX_SIZE(font->FDArray.cnt, size);
822 }
823 }
824
825 /* Calculate total size of Private DICTs */
826 font->size.Private = 0;
827 for (i = 0; i < font->FDArray.cnt; i++) {
828 FDInfo *fd = &font->FDArray.array[i];
829 fd->size.Private = fd->cff.Private.cnt;
830 if (fd->Subrs.count > 0) {
831 /* Subrs offset is relative to start of the Private DICT */
832 fd->size.Private +=
833 (intsize(fd->offset.Subrs - fd->offset.Private) +
834 DICT_OP_SIZE(cff_Subrs));
835 }
836 font->size.Private += fd->size.Private;
837 }
838 }
839
840 /* Fill Name INDEX. */
841 static long fillNameINDEX(controlCtx h) {
842 long i;
843
844 h->name.datasize = 0;
845 for (i = 0; i < h->FontSet.cnt; i++) {
846 cff_Font *font = &h->FontSet.array[i];
847 if (font->FDArray.cnt > 0) {
848 char *name =
849 cfwSindexGetString(h->g, (SRI)((font->flags & FONT_CID) ? font->top.cid.CIDFontName.impl : font->FDArray.array[0].dict.FontName.impl));
850 /* 64-bit warning fixed by cast here */
851 h->name.datasize += (long)strlen(name);
852 }
853 }
854
855 h->name.count = (unsigned short)h->FontSet.cnt;
856 h->name.offSize = INDEX_OFF_SIZE(h->name.datasize);
857
858 return INDEX_SIZE(h->name.count, h->name.datasize);
859 }
860
861 /* Size top DICT INDEX. */
862 static long sizeTopDICT_INDEX(controlCtx h) {
863 long i;
864 long size = 0;
865 for (i = 0; i < h->FontSet.cnt; i++) {
866 size += h->FontSet.array[i].cff.top.cnt;
867 }
868 return INDEX_SIZE(h->FontSet.cnt, size);
869 }
870
871 static long sizeTopDICT_Data(controlCtx h) {
872 return h->FontSet.array[0].cff.top.cnt;
873 }
874
875 static long sizeVarStore(controlCtx h) {
876 return h->FontSet.array[0].cff.varStore.cnt;
877 }
878
879 /* Calculate initial FontSet sizes. */
880 static void initSetSizes(controlCtx h) {
881 cfwCtx g = h->g;
882 int i;
883
884 /* Compute initial sizes */
885 if (g->flags & CFW_WRITE_CFF2) {
886 h->size.string = 0;
887 h->size.charset = 0;
888 h->size.Encoding = 0;
889 h->size.header = CFF2_HDR_SIZE;
890 h->size.top = sizeTopDICT_Data(h);
891 h->size.varStore = sizeVarStore(h);
892 h->size.gsubr = cfwSubrSizeGlobal(g);
893 h->size.FDSelect = cfwFdselectFill(g);
894 } else {
895 h->size.header = CFF_HDR_SIZE;
896 h->size.name = fillNameINDEX(h);
897 h->size.top = sizeTopDICT_INDEX(h);
898 h->size.varStore = 0;
899 h->size.string = cfwSindexSize(g);
900 h->size.gsubr = cfwSubrSizeGlobal(g);
901 h->size.charset = cfwCharsetFill(g);
902 h->size.Encoding = cfwEncodingFill(g);
903 h->size.FDSelect = cfwFdselectFill(g);
904 }
905
906 for (i = 0; i < h->FontSet.cnt; i++) {
907 initFontSizes(h, &h->FontSet.array[i]);
908 }
909 }
910
911 /* Calculate offsets of CFF "structures". Return 0 when offset calculation has
912 stabilized else return 1. */
913 static int calcSetOffsets(controlCtx h) {
914 int i;
915 Offset lastend = h->offset.end;
916 cfwCtx g = h->g;
917
918 if (g->flags & CFW_WRITE_CFF2) {
919 Offset offset;
920 h->offset.name = 0;
921 h->offset.string = 0;
922 h->offset.charset = 0;
923 h->offset.Encoding = 0;
924
925 h->offset.top = h->size.header;
926 h->offset.gsubr = h->offset.top + h->size.top;
927 offset = h->offset.gsubr + h->size.gsubr;
928 if (h->size.varStore > 0) {
929 h->offset.varStore = offset;
930 offset += h->size.varStore;
931 }
932 if (h->size.FDSelect > 0) {
933 h->offset.FDSelect = offset;
934 offset += h->size.FDSelect;
935 }
936 h->offset.end = offset;
937 } else {
938 h->offset.varStore = 0;
939
940 h->offset.name = 0 + h->size.header;
941 h->offset.top = h->offset.name + h->size.name;
942 h->offset.string = h->offset.top + h->size.top;
943 h->offset.gsubr = h->offset.string + h->size.string;
944 h->offset.charset = h->offset.gsubr + h->size.gsubr;
945 h->offset.Encoding = h->offset.charset + h->size.charset;
946 h->offset.FDSelect = h->offset.Encoding + h->size.Encoding;
947 h->offset.end = h->offset.FDSelect + h->size.FDSelect;
948 }
949
950 /* Calculate offset for each font in FontSet */
951 for (i = 0; i < h->FontSet.cnt; i++) {
952 h->offset.end = calcFontOffsets(h, &h->FontSet.array[i], h->offset.end);
953 }
954
955 return h->offset.end != lastend;
956 }
957
958 /* Calculate sizes of dynamic CFF "structures". */
959 static void calcSetSizes(controlCtx h) {
960 int i;
961 long size = 0;
962 cfwCtx g = h->g;
963
964 for (i = 0; i < h->FontSet.cnt; i++) {
965 cff_Font *font = &h->FontSet.array[i];
966 calcFontSizes(h, font);
967 size += font->size.top;
968 }
969 if (g->flags & CFW_WRITE_CFF2) {
970 h->size.top = h->FontSet.array[0].size.top;
971 } else {
972 h->size.top = INDEX_SIZE(h->FontSet.cnt, size);
973 }
974 }
975
976 /* Fill font. */
977 static void fillFont(controlCtx h, cff_Font *font) {
978 int i;
979 cfwCtx g = h->g;
980
981 if (!(g->flags & CFW_WRITE_CFF2)) {
982 /* Fill top DICT with dynamic data */
983 switch (font->offset.charset) {
984 case cff_ISOAdobeCharset:
985 break;
986
987 case cff_ExpertCharset:
988 case cff_ExpertSubsetCharset:
989 default:
990 cfwDictSaveIntOp(&font->cff.top, font->offset.charset, cff_charset);
991 break;
992 }
993
994 switch (font->offset.Encoding) {
995 case cff_StandardEncoding:
996 break;
997
998 case cff_ExpertEncoding:
999 default:
1000 cfwDictSaveIntOp(&font->cff.top, font->offset.Encoding, cff_Encoding);
1001 break;
1002 }
1003 }
1004
1005 cfwDictSaveIntOp(&font->cff.top, font->offset.CharStrings, cff_CharStrings);
1006
1007 if (g->flags & CFW_WRITE_CFF2) {
1008 if (font->offset.VarStore != 0)
1009 cfwDictSaveIntOp(&font->cff.top, font->offset.VarStore, cff_VarStore);
1010 if (font->offset.FDSelect != 0)
1011 cfwDictSaveIntOp(&font->cff.top, font->offset.FDSelect, cff_FDSelect);
1012 cfwDictSaveIntOp(&font->cff.top, font->offset.FDArray, cff_FDArray);
1013
1014 /* Fill font DICTs with dynamic data */
1015 for (i = 0; i < font->FDArray.cnt; i++) {
1016 FDInfo *fd = &font->FDArray.array[i];
1017 cfwDictSaveInt(&fd->cff.dict, fd->size.Private);
1018 cfwDictSaveInt(&fd->cff.dict, fd->offset.Private);
1019 cfwDictSaveOp(&fd->cff.dict, cff_Private);
1020 }
1021 } else if (font->flags & FONT_CID) {
1022 cfwDictSaveIntOp(&font->cff.top, font->offset.FDSelect, cff_FDSelect);
1023 cfwDictSaveIntOp(&font->cff.top, font->offset.FDArray, cff_FDArray);
1024
1025 /* Fill font DICTs with dynamic data */
1026 for (i = 0; i < font->FDArray.cnt; i++) {
1027 FDInfo *fd = &font->FDArray.array[i];
1028 cfwDictSaveInt(&fd->cff.dict, fd->size.Private);
1029 cfwDictSaveInt(&fd->cff.dict, fd->offset.Private);
1030 cfwDictSaveOp(&fd->cff.dict, cff_Private);
1031 }
1032 } else {
1033 cfwDictSaveInt(&font->cff.top, font->size.Private);
1034 cfwDictSaveInt(&font->cff.top, font->offset.Private);
1035 cfwDictSaveOp(&font->cff.top, cff_Private);
1036 }
1037
1038 /* Fill Private DICTs with dynamic data */
1039 for (i = 0; i < font->FDArray.cnt; i++) {
1040 FDInfo *fd = &font->FDArray.array[i];
1041 if (fd->Subrs.count > 0) {
1042 cfwDictSaveIntOp(&fd->cff.Private,
1043 fd->offset.Subrs - fd->offset.Private, cff_Subrs);
1044 }
1045 }
1046 }
1047
1048 /* Fill FontSet. */
1049 static void fillSet(controlCtx h) {
1050 cfwCtx g = h->g;
1051 int i;
1052
1053 /* Fill DICTs with static data */
1054 for (i = 0; i < h->FontSet.cnt; i++) {
1055 long j;
1056 cff_Font *font = &h->FontSet.array[i];
1057
1058 if (font->FDArray.cnt > 0)
1059 cfwDictFillTop(g, &font->cff.top, &font->top,
1060 &font->FDArray.array[0].dict, -1);
1061
1062 if ((font->top.varStore != NULL) && (g->flags & CFW_WRITE_CFF2)) {
1063 cfwDictFillVarStore(g, &font->cff.varStore, &font->top);
1064 }
1065
1066 for (j = 0; j < font->FDArray.cnt; j++) {
1067 FDInfo *fd = &font->FDArray.array[j];
1068 if ((font->flags & FONT_CID) || (g->flags & CFW_WRITE_CFF2)) {
1069 cfwDictFillFont(h->g, &fd->cff.dict, &fd->dict);
1070 }
1071 cfwDictFillPrivate(h->g, &fd->cff.Private, &fd->Private);
1072 if (!(g->flags & CFW_WRITE_CFF2)) {
1073 if (fd->width.dflt != cff_DFLT_defaultWidthX) {
1074 cfwDictSaveRealOp(&fd->cff.Private,
1075 fd->width.dflt, cff_defaultWidthX);
1076 }
1077 if (fd->width.nominal != cff_DFLT_nominalWidthX) {
1078 cfwDictSaveRealOp(&fd->cff.Private,
1079 fd->width.nominal, cff_nominalWidthX);
1080 }
1081 }
1082 }
1083 }
1084
1085 initSetSizes(h);
1086
1087 /* Iterate offset calculation until stable */
1088 h->offset.end = 0;
1089 while (calcSetOffsets(h)) {
1090 calcSetSizes(h);
1091 }
1092
1093 for (i = 0; i < h->FontSet.cnt; i++) {
1094 fillFont(h, &h->FontSet.array[i]);
1095 }
1096 }
1097
1098 /* ---------------------------- FontSet Writing ---------------------------- */
1099
1100 /* Write CFF header. */
1101 static void writeHeader(controlCtx h) {
1102 cfwCtx g = h->g;
1103
1104 if (g->flags & CFW_WRITE_CFF2) {
1105 cfwWrite1(g, 2); /* major */
1106 cfwWrite1(g, 0); /* minor */
1107 cfwWrite1(g, CFF2_HDR_SIZE); /* hdrSize */
1108 cfwWrite2(g, (unsigned short)h->FontSet.array[0].size.top); /* topDict data length */
1109 } else {
1110 cfwWrite1(g, 1); /* major */
1111 cfwWrite1(g, 0); /* minor */
1112 cfwWrite1(g, CFF_HDR_SIZE); /* hdrSize */
1113 cfwWrite1(g, OFF_SIZE(h->offset.end)); /* offSize */
1114 }
1115 }
1116
1117 /* Write name INDEX. */
1118 static void writeNameINDEX(controlCtx h) {
1119 cfwCtx g = h->g;
1120 int i;
1121 Offset offset;
1122
1123 /* Write header */
1124 cfwWrite2(g, (unsigned short)h->name.count);
1125 cfwWrite1(g, h->name.offSize);
1126
1127 /* Write offset array */
1128 offset = 1;
1129 cfwWriteN(g, h->name.offSize, offset);
1130 for (i = 0; i < h->FontSet.cnt; i++) {
1131 cff_Font *font = &h->FontSet.array[i];
1132 if (font->FDArray.cnt > 0) {
1133 char *name =
1134 cfwSindexGetString(g, (SRI)((font->flags & FONT_CID) ? font->top.cid.CIDFontName.impl : font->FDArray.array[0].dict.FontName.impl));
1135 /* 64-bit warning fixed by cast here */
1136 offset += (Offset)strlen(name);
1137 cfwWriteN(g, h->name.offSize, offset);
1138 }
1139 }
1140
1141 /* Write object data */
1142 for (i = 0; i < h->FontSet.cnt; i++) {
1143 cff_Font *font = &h->FontSet.array[i];
1144 if (font->FDArray.cnt > 0) {
1145 char *name =
1146 cfwSindexGetString(g, (SRI)((font->flags & FONT_CID) ? font->top.cid.CIDFontName.impl : font->FDArray.array[0].dict.FontName.impl));
1147 cfwWrite(g, strlen(name), name);
1148 }
1149 }
1150 }
1151
1152 /* Write top DICT INDEX */
1153 static void writeTopDICT_INDEX(controlCtx h) {
1154 cfwCtx g = h->g;
1155 int i;
1156 INDEX index;
1157 Offset offset;
1158
1159 /* Fill INDEX */
1160 index.datasize = 0;
1161 for (i = 0; i < h->FontSet.cnt; i++) {
1162 index.datasize += h->FontSet.array[i].size.top;
1163 }
1164 index.count = (unsigned short)h->FontSet.cnt;
1165 index.offSize = INDEX_OFF_SIZE(index.datasize);
1166
1167 /* Write header */
1168 cfwWrite2(g, (unsigned short)index.count);
1169 cfwWrite1(g, index.offSize);
1170
1171 /* Write offset array */
1172 offset = 1;
1173 cfwWriteN(g, index.offSize, offset);
1174 for (i = 0; i < h->FontSet.cnt; i++) {
1175 offset += h->FontSet.array[i].size.top;
1176 cfwWriteN(g, index.offSize, offset);
1177 }
1178
1179 /* Write object data */
1180 for (i = 0; i < h->FontSet.cnt; i++) {
1181 DICT *top = &h->FontSet.array[i].cff.top;
1182 cfwWrite(g, top->cnt, top->array);
1183 }
1184 }
1185
1186 static void writeTopDICT_Data(controlCtx h) {
1187 cfwCtx g = h->g;
1188
1189 DICT *top = &h->FontSet.array[0].cff.top;
1190 cfwWrite(g, top->cnt, top->array);
1191 }
1192
1193 static void writeVarStoreData_Data(controlCtx h) {
1194 cfwCtx g = h->g;
1195
1196 DICT *varStore = &h->FontSet.array[0].cff.varStore;
1197 cfwWrite(g, varStore->cnt, varStore->array);
1198 }
1199
1200 /* Return size of argument (in bytes) if formatted for charstring. */
1201 static int numsize(float r) {
1202 long i = (long)r;
1203 if (i == r) {
1204 /* Integer */
1205 if (-107 <= i && i <= 107) {
1206 return 1;
1207 } else if (-1131 <= i && i <= 1131) {
1208 return 2;
1209 } else if (-32768 <= i && i <= 32767) {
1210 return 3;
1211 } else {
1212 return 5;
1213 }
1214 } else {
1215 /* Fractional */
1216 return 5;
1217 }
1218 }
1219
1220 /* Fill tmp buffer. */
1221 /* Assumes tmp stream file position == offset */
1222 static void fillbuf(cfwCtx g, long offset) {
1223 g->tmp.length = g->cb.stm.read(&g->cb.stm, g->stm.tmp, &g->tmp.buf);
1224 if (g->tmp.length == 0) {
1225 cfwFatal(g, cfwErrTmpStream, NULL);
1226 }
1227 g->tmp.offset = offset;
1228 g->tmp.next = g->tmp.buf;
1229 g->tmp.end = g->tmp.buf + g->tmp.length;
1230 }
1231
1232 /* Copy length bytes from tmp stream to dst stream. */
1233 static void tmp2dstCopy(cfwCtx g, long length, long offset) {
1234 long left;
1235 long delta = offset - g->tmp.offset;
1236 if (delta >= 0 && (size_t)delta < g->tmp.length) {
1237 /* Offset within current buffer; reposition next byte */
1238 g->tmp.next = g->tmp.buf + delta;
1239 } else {
1240 /* Offset outside current buffer; seek to offset and fill buffer */
1241 if (g->cb.stm.seek(&g->cb.stm, g->stm.tmp, offset)) {
1242 cfwFatal(g, cfwErrTmpStream, NULL);
1243 }
1244 fillbuf(g, offset);
1245 }
1246
1247 /* Compute bytes left in buffer */
1248 /* 64-bit warning fixed by cast here */
1249 left = (long)(g->tmp.end - g->tmp.next);
1250 while (left < length) {
1251 /* Write buffer */
1252 cfwWrite(g, left, g->tmp.next);
1253 length -= left;
1254
1255 /* Refill buffer */
1256 /* 64-bit warning fixed by cast here */
1257 fillbuf(g, (long)(g->tmp.offset + g->tmp.length));
1258 left = (long)g->tmp.length;
1259 }
1260
1261 /* Write buffer */
1262 cfwWrite(g, length, g->tmp.next);
1263 g->tmp.next += length;
1264 }
1265
1266 /* Write CharStrings INDEX. */
1267 static void writeCharStringsINDEX(controlCtx h, cff_Font *font) {
1268 cfwCtx g = h->g;
1269 long i;
1270 Offset offset;
1271
1272 /* Write header */
1273 if (g->flags & CFW_WRITE_CFF2) {
1274 cfwWriteN(g, 4, font->CharStrings.count);
1275 } else {
1276 cfwWrite2(g, (unsigned short)font->CharStrings.count);
1277 }
1278 cfwWrite1(g, font->CharStrings.offSize);
1279
1280 /* Write offset array */
1281 offset = 1;
1282 cfwWriteN(g, font->CharStrings.offSize, offset);
1283 for (i = 0; i < font->glyphs.cnt; i++) {
1284 long chrstr_length;
1285 Glyph *glyph = &font->glyphs.array[i];
1286 FDInfo *fd;
1287
1288 if (glyph->iFD >= font->FDArray.cnt)
1289 cfwFatal(g, cfwErrBadFDArray, NULL);
1290
1291 fd = &font->FDArray.array[glyph->iFD];
1292
1293 chrstr_length = glyph->cstr.length;
1294 if (chrstr_length > 65535) {
1295 cfwMessage(g,
1296 "Warning: CharString of GID %ld is %ld bytes long. "
1297 "CharStrings longer than 65535 bytes might not be "
1298 "supported by some implementations.",
1299 i, chrstr_length);
1300 }
1301
1302 if ((!(g->flags & CFW_WRITE_CFF2)) && (glyph->hAdv != fd->width.dflt)) {
1303 /* Add glyph width size */
1304 offset += numsize(glyph->hAdv - fd->width.nominal);
1305 }
1306 offset += chrstr_length;
1307
1308 cfwWriteN(g, font->CharStrings.offSize, offset);
1309 }
1310
1311 /* Write charstring data */
1312 for (i = 0; i < font->glyphs.cnt; i++) {
1313 Glyph *glyph = &font->glyphs.array[i];
1314 FDInfo *fd = &font->FDArray.array[glyph->iFD];
1315
1316 if ((!(g->flags & CFW_WRITE_CFF2)) && (glyph->hAdv != fd->width.dflt)) {
1317 /* Write width */
1318 long length;
1319 uint8_t t[5];
1320 long n;
1321 float r = glyph->hAdv - fd->width.nominal;
1322 if (r < -32768 || r >= 32768)
1323 cfwMessage(h->g, "out of numeric range width %g in glyph %ld", r, i);
1324 n = (long)r;
1325 length = (numsize(r) <= 3) ? cfwEncInt(n, t) : cfwEncReal(r, t);
1326 cfwWrite(g, length, (char *)t);
1327 }
1328
1329 /* Copy charstring */
1330 if (glyph->cstr.length > 0)
1331 tmp2dstCopy(g, glyph->cstr.length, glyph->cstr.offset);
1332 }
1333 }
1334
1335 /* Write FDArray. */
1336 static void writeFDArray(controlCtx h, cff_Font *font) {
1337 cfwCtx g = h->g;
1338 int i;
1339
1340 if ((font->flags & FONT_CID) || (g->flags & CFW_WRITE_CFF2)) {
1341 /* Compute FDArray offSize */
1342 INDEX index;
1343 Offset offset;
1344
1345 /* Fill INDEX */
1346 index.datasize = 0;
1347 for (i = 0; i < font->FDArray.cnt; i++) {
1348 index.datasize += font->FDArray.array[i].cff.dict.cnt;
1349 }
1350 index.count = (unsigned short)font->FDArray.cnt;
1351 index.offSize = INDEX_OFF_SIZE(index.datasize);
1352
1353 /* Write header */
1354 if (g->flags & CFW_WRITE_CFF2) {
1355 cfwWriteN(g, 4, index.count);
1356 } else {
1357 cfwWrite2(g, (unsigned short)index.count);
1358 }
1359 cfwWrite1(g, index.offSize);
1360
1361 /* Write offset array */
1362 offset = 1;
1363 cfwWriteN(g, index.offSize, offset);
1364 for (i = 0; i < font->FDArray.cnt; i++) {
1365 offset += font->FDArray.array[i].cff.dict.cnt;
1366 cfwWriteN(g, index.offSize, offset);
1367 }
1368
1369 /* Write object data */
1370 for (i = 0; i < font->FDArray.cnt; i++) {
1371 DICT *dict = &font->FDArray.array[i].cff.dict;
1372 cfwWrite(g, dict->cnt, dict->array);
1373 }
1374 }
1375
1376 /* Write Private DICTs */
1377 for (i = 0; i < font->FDArray.cnt; i++) {
1378 DICT *Private = &font->FDArray.array[i].cff.Private;
1379 cfwWrite(g, Private->cnt, Private->array);
1380 }
1381
1382 /* Write local Subrs INDEXes */
1383 for (i = 0; i < font->FDArray.cnt; i++) {
1384 FDInfo *fd = &font->FDArray.array[i];
1385 cfwSubrWriteLocal(g, &fd->subrData);
1386 }
1387 }
1388
1389 /* Write FontSet. */
1390 static void writeSet(controlCtx h) {
1391 cfwCtx g = h->g;
1392 int i;
1393
1394 /* Initialize tmp file read */
1395 g->tmp.offset = 0;
1396 g->tmp.length = 0;
1397
1398 /* Open output stream */
1399 g->stm.dst = g->cb.stm.open(&g->cb.stm, CFW_DST_STREAM_ID, h->offset.end);
1400 if (g->stm.dst == NULL) {
1401 cfwFatal(g, cfwErrDstStream, NULL);
1402 }
1403
1404 /* Write aggregate data structures */
1405 writeHeader(h);
1406 if (g->flags & CFW_WRITE_CFF2) {
1407 writeTopDICT_Data(h);
1408 cfwSubrWriteGlobal(g);
1409 writeVarStoreData_Data(h);
1410 cfwFdselectWrite(g);
1411 } else {
1412 writeNameINDEX(h);
1413 writeTopDICT_INDEX(h);
1414 cfwSindexWrite(g);
1415 cfwSubrWriteGlobal(g);
1416 cfwCharsetWrite(g);
1417 cfwEncodingWrite(g);
1418 cfwFdselectWrite(g);
1419 }
1420
1421 /* Write per-font data structures */
1422 for (i = 0; i < h->FontSet.cnt; i++) {
1423 cff_Font *font = &h->FontSet.array[i];
1424 writeCharStringsINDEX(h, font);
1425 writeFDArray(h, font);
1426 }
1427
1428 /* Close output stream */
1429 if (g->cb.stm.close(&g->cb.stm, g->stm.dst)) {
1430 cfwFatal(g, cfwErrDstStream, NULL);
1431 }
1432 }
1433
1434 /* Begin new FontSet. */
1435 int cfwBegSet(cfwCtx g, long flags) {
1436 controlCtx h = g->ctx.control;
1437 g->flags = flags;
1438 h->FontSet.cnt = 0;
1439 if (flags & CFW_WRITE_CFF2)
1440 h->flags |= FONTSET_CFF2;
1441
1442 /* Open debug stream */
1443 g->stm.dbg = g->cb.stm.open(&g->cb.stm, CFW_DBG_STREAM_ID, 0);
1444
1445 return 0;
1446 }
1447
1448 /* Begin new font. */
1449 int cfwBegFont(cfwCtx g, cfwMapCallback *map, unsigned long maxNumSubrs) {
1450 controlCtx h = g->ctx.control;
1451 long index;
1452
1453 /* set tmp buffer buffers safely */
1454 g->tmp.offset = 0;
1455 g->tmp.length = 0;
1456 g->maxNumSubrs = maxNumSubrs;
1457
1458 /* Allocate and initialize new font */
1459 index = dnaNext(&h->FontSet, sizeof(cff_Font));
1460 if (index == -1) {
1461 return cfwErrNoMemory;
1462 }
1463 h->_new = &h->FontSet.array[index];
1464 h->_new->CharStrings.datasize = 0;
1465 h->_new->map = map;
1466
1467 /* Allocate slot for .notdef glyph */
1468 index = dnaSetCnt(&h->_new->glyphs, sizeof(Glyph), 1);
1469 if (index == -1) {
1470 return cfwErrNoMemory;
1471 }
1472 h->_new->glyphs.array[0].info = NULL;
1473
1474 /* For h->_new->seenGlyphs, we do NOT need to pre-allocate for .notdef
1475 as we are not forcing it to the beginning of the list */
1476
1477 h->flags &= ~(SEEN_NAME_KEYED_GLYPH | SEEN_CID_KEYED_GLYPH);
1478 h->mergedDicts = 0;
1479
1480 /* Set up metrics facility */
1481 g->glyph_metrics.cb = abfGlyphMetricsCallbacks;
1482 g->glyph_metrics.cb.direct_ctx = &g->glyph_metrics.ctx;
1483 g->glyph_metrics.ctx.flags = 0;
1484 g->font_bbox.left = INT16_MAX;
1485 g->font_bbox.bottom = INT16_MAX;
1486 g->font_bbox.right = INT16_MIN;
1487 g->font_bbox.top = INT16_MIN;
1488
1489 return cfwSuccess;
1490 }
1491
1492 /* Order glyphs in CID-keyed font. */
1493 static void orderCIDKeyedGlyphs(controlCtx h) {
1494 cfwCtx g = h->g;
1495 long i;
1496 long nGlyphs = h->_new->glyphs.cnt;
1497 Glyph *glyphs = h->_new->glyphs.array;
1498 dnaDCL(uint16_t, fdmap);
1499
1500 dnaINIT(g->ctx.dnaSafe, fdmap, 1, 1);
1501 dnaSET_CNT(fdmap, h->_new->FDArray.cnt);
1502 memset(fdmap.array, 0, sizeof(uint16_t) * h->_new->FDArray.cnt);
1503
1504 if (glyphs[0].info == NULL) {
1505 cfwFatal(h->g, cfwErrNoCID0, NULL);
1506 }
1507
1508 if (!(g->flags & CFW_PRESERVE_GLYPH_ORDER)) {
1509 /* Insertion sort glyphs by CID order */
1510 for (i = 2; i < nGlyphs; i++) {
1511 long j = i;
1512 Glyph tmp = glyphs[i];
1513 while (tmp.info->cid < glyphs[j - 1].info->cid) {
1514 glyphs[j] = glyphs[j - 1];
1515 j--;
1516 }
1517 if (j != i) {
1518 glyphs[j] = tmp;
1519 }
1520 }
1521 }
1522
1523 /* Mark used FDs */
1524 for (i = 0; i < nGlyphs; i++) {
1525 fdmap.array[glyphs[i].iFD] = 1;
1526 }
1527
1528 /* Remove unused FDs from FDArray */
1529 {
1530 long j = 0;
1531 for (i = 0; i < h->_new->FDArray.cnt; i++) {
1532 if (fdmap.array[i]) {
1533 if (i != j) {
1534 /* Swap elements preserving dynamic arrays for later freeing */
1535 FDInfo tmp = h->_new->FDArray.array[j];
1536 h->_new->FDArray.array[j] = h->_new->FDArray.array[i];
1537 h->_new->FDArray.array[i] = tmp;
1538 }
1539 fdmap.array[i] = (uint16_t)j++;
1540 }
1541 }
1542
1543 if (i != j) {
1544 /* Unused FDs; remap glyphs to new FDArray */
1545 for (i = 0; i < nGlyphs; i++) {
1546 glyphs[i].iFD = fdmap.array[glyphs[i].iFD];
1547 }
1548 h->_new->FDArray.cnt = j;
1549 }
1550 }
1551
1552 dnaFREE(fdmap);
1553
1554 cfwCharsetBeg(g, 1);
1555 cfwFdselectBeg(g);
1556
1557 cfwFdselectAddIndex(g, glyphs[0].iFD);
1558 for (i = 1; i < nGlyphs; i++) {
1559 Glyph *glyph = &glyphs[i];
1560 cfwCharsetAddGlyph(g, glyph->info->cid);
1561 cfwFdselectAddIndex(g, glyph->iFD);
1562 }
1563
1564 h->_new->iObject.charset = cfwCharsetEnd(g);
1565 h->_new->iObject.Encoding = 0;
1566 h->_new->iObject.FDSelect = cfwFdselectEnd(g);
1567
1568 h->_new->top.cid.CIDCount = glyphs[nGlyphs - 1].info->cid + 1;
1569 }
1570
1571 /* Order glyphs in name-keyed font. */
1572 static void orderNameKeyedGlyphs(controlCtx h) {
1573 cfwCtx g = h->g;
1574 int predef;
1575 int id;
1576 long i;
1577 long j;
1578 long k;
1579 Glyph tmp;
1580 long nGlyphs = h->_new->glyphs.cnt;
1581 Glyph *glyphs = h->_new->glyphs.array;
1582
1583 if (glyphs[0].info == NULL) {
1584 cfwFatal(h->g, cfwErrNoNotdef, NULL);
1585 }
1586
1587 if (!(g->flags & CFW_PRESERVE_GLYPH_ORDER)) {
1588 /* Set sentinel for insertion sort */
1589 glyphs[0].info->encoding.code = 0;
1590
1591 /* Sort glyphs by encoding order using insertion sort */
1592 for (i = 2; i < nGlyphs; i++) {
1593 j = i;
1594 tmp = glyphs[i];
1595 while (tmp.info->encoding.code < glyphs[j - 1].info->encoding.code) {
1596 glyphs[j] = glyphs[j - 1];
1597 j--;
1598 }
1599 if (j != i) {
1600 glyphs[j] = tmp;
1601 }
1602 }
1603 glyphs[0].info->encoding.code = ABF_GLYPH_UNENC;
1604 }
1605
1606 /* Assign SIDs and remember last encoded glyph */
1607 j = 0;
1608 for (i = 1; i < nGlyphs; i++) {
1609 abfGlyphInfo *info = glyphs[i].info;
1610 abfString *gname = &info->gname;
1611
1612 gname->impl = cfwSindexAssignSID(g, (SRI)gname->impl);
1613
1614 if (info->encoding.code != ABF_GLYPH_UNENC) {
1615 j = i;
1616 }
1617 }
1618
1619 if (!(g->flags & CFW_PRESERVE_GLYPH_ORDER)) {
1620 if (++j < nGlyphs) {
1621 /* Font has unencoded glyphs; set sentinel for insertion sort */
1622 k = j;
1623 for (i = j + 1; i < nGlyphs; i++) {
1624 if (glyphs[i].info->gname.impl < glyphs[k].info->gname.impl) {
1625 k = i;
1626 }
1627 }
1628 if (k != j) {
1629 tmp = glyphs[j];
1630 glyphs[j] = glyphs[k];
1631 glyphs[k] = tmp;
1632 }
1633 /* Sort unencoded glyphs by SID order using insertion sort */
1634 for (i = j + 2; i < nGlyphs; i++) {
1635 j = i;
1636 tmp = glyphs[i];
1637 while (tmp.info->gname.impl < glyphs[j - 1].info->gname.impl) {
1638 glyphs[j] = glyphs[j - 1];
1639 j--;
1640 }
1641 if (j != i) {
1642 glyphs[j] = tmp;
1643 }
1644 }
1645 }
1646 }
1647
1648 /* Check for predefined encoding */
1649 predef = 0;
1650 if (g->flags & CFW_FORCE_STD_ENCODING) {
1651 h->_new->iObject.Encoding = 0;
1652 predef = 1;
1653 } else {
1654 for (id = 0;; id++) {
1655 int cnt;
1656 const uint8_t *array = cfwEncodingGetPredef(id, &cnt);
1657 if (array == NULL) {
1658 break;
1659 }
1660
1661 /* Check compatibility against predef encoding */
1662 for (i = 0; i < nGlyphs; i++) {
1663 abfGlyphInfo *info = glyphs[i].info;
1664 SID sid = (SID)info->gname.impl;
1665 short font_enc = (info->encoding.code == ABF_GLYPH_UNENC) ? -1 : (short)info->encoding.code;
1666 short predef_enc = (sid >= cnt || array[sid] == 0) ? -1 : array[sid];
1667
1668 if (font_enc != predef_enc) {
1669 goto incompat;
1670 }
1671 }
1672
1673 /* Found compatible predef encoding */
1674 h->_new->iObject.Encoding = id;
1675 predef = 1;
1676 break;
1677
1678 incompat:;
1679 }
1680 }
1681 cfwCharsetBeg(g, 0);
1682 if (!predef) {
1683 cfwEncodingBeg(g);
1684 }
1685
1686 for (i = 1; i < nGlyphs; i++) {
1687 abfGlyphInfo *info = glyphs[i].info;
1688 abfString *gname = &info->gname;
1689
1690 /* Add glyph to charset */
1691 cfwCharsetAddGlyph(g, (unsigned short)gname->impl);
1692
1693 if (!predef) {
1694 abfEncoding *enc = &info->encoding;
1695 if (enc->code != ABF_GLYPH_UNENC) {
1696 /* Add glyph to encoding */
1697 cfwEncodingAddCode(g, (uint8_t)enc->code);
1698 while ((enc = enc->next) != NULL) {
1699 cfwEncodingAddSupCode(g, (uint8_t)enc->code,
1700 (SID)gname->impl);
1701 }
1702 }
1703 }
1704 }
1705
1706 h->_new->iObject.charset = cfwCharsetEnd(g);
1707 if (!predef) {
1708 h->_new->iObject.Encoding = cfwEncodingEnd(g);
1709 }
1710 h->_new->iObject.FDSelect = 0;
1711 }
1712
1713 /* Assign the default and nominal widths (see comment at head of file). */
1714 static int assignWidths(controlCtx h, FDInfo *fd) {
1715 /* Assign default values */
1716 fd->width.dflt = cff_DFLT_defaultWidthX;
1717 fd->width.nominal = cff_DFLT_nominalWidthX;
1718
1719 if (fd->width.freqs.cnt == 0) {
1720 /* Unused FD */
1721 return 0;
1722 } else if (fd->width.freqs.cnt == 1) {
1723 /* Fixed-pitch font */
1724 WidthFreq *rec = &fd->width.freqs.array[0];
1725 fd->width.dflt = rec->width;
1726 return 0;
1727 } else {
1728 int i;
1729 int minsize;
1730 int nonoptsize;
1731 int iNominal = 0; /* Suppress optimizer warning */
1732 int iDefault = 0; /* Suppress optimizer warning */
1733
1734 /* Find non-optimized size */
1735 nonoptsize = 0;
1736 for (i = 0; i < fd->width.freqs.cnt; i++) {
1737 WidthFreq *rec = &fd->width.freqs.array[i];
1738 if (rec->width != 0) {
1739 nonoptsize += numsize(rec->width) * rec->count;
1740 }
1741 }
1742
1743 /* Find best combination of nominal and default widths */
1744 minsize = nonoptsize;
1745 for (i = 0; i < fd->width.freqs.cnt; i++) {
1746 int j;
1747 int nomsize;
1748 float nomwidth = fd->width.freqs.array[i].width + 107;
1749
1750 /* Compute total size for this nominal width */
1751 nomsize = 0;
1752 for (j = 0; j < fd->width.freqs.cnt; j++) {
1753 WidthFreq *rec = &fd->width.freqs.array[j];
1754 nomsize += numsize(rec->width - nomwidth) * rec->count;
1755 }
1756
1757 /* Try this nominal width against all possible defaults */
1758 for (j = 0; j < fd->width.freqs.cnt; j++) {
1759 WidthFreq *dflt = &fd->width.freqs.array[j];
1760 int totsize =
1761 nomsize - numsize(dflt->width - nomwidth) * dflt->count;
1762 if (totsize < minsize) {
1763 minsize = totsize;
1764 iNominal = i;
1765 iDefault = j;
1766 }
1767 }
1768 }
1769
1770 {
1771 float nominal;
1772 float dflt;
1773 int dictsize;
1774
1775 dflt = fd->width.freqs.array[iDefault].width;
1776 nominal = fd->width.freqs.array[iNominal].width + 107;
1777
1778 /* Compute size of dictionary entries */
1779 dictsize = 0;
1780 if (dflt != 0) {
1781 dictsize += numsize(dflt) + DICT_OP_SIZE(cff_defaultWidthX);
1782 }
1783 if (nominal != 0) {
1784 dictsize += numsize(nominal) + DICT_OP_SIZE(cff_nominalWidthX);
1785 }
1786
1787 if (minsize + dictsize < nonoptsize) {
1788 /* Optimized savings; set optimization values */
1789 fd->width.dflt = dflt;
1790 fd->width.nominal = nominal;
1791 return minsize;
1792 } else {
1793 /* No savings; set non-optimized size */
1794 return nonoptsize;
1795 }
1796 }
1797 }
1798 }
1799
1800 /* Match width. */
1801 static int CTL_CDECL matchWidth(const void *key, const void *value, void *ctx) {
1802 float a = *(float *)key;
1803 float b = ((WidthFreq *)value)->width;
1804 if (a < b) {
1805 return -1;
1806 } else if (a > b) {
1807 return 1;
1808 } else {
1809 return 0;
1810 }
1811 }
1812
1813 /* Analyze width frequencies and assign default and nominal widths. */
1814 static void analyzeWidths(controlCtx h) {
1815 long i;
1816
1817 /* Make width frequency records */
1818 for (i = 0; i < h->_new->glyphs.cnt; i++) {
1819 size_t index;
1820 Glyph *glyph = &h->_new->glyphs.array[i];
1821 FDInfo *fd = &h->_new->FDArray.array[glyph->iFD];
1822
1823 /* Check if width already present */
1824 if (ctuLookup(&glyph->hAdv,
1825 fd->width.freqs.array, fd->width.freqs.cnt,
1826 sizeof(WidthFreq), matchWidth, &index, NULL)) {
1827 /* Match found; bump count */
1828 fd->width.freqs.array[index].count++;
1829 } else {
1830 /* Not found; add new record */
1831 WidthFreq *_new;
1832
1833 dnaGROW(fd->width.freqs, fd->width.freqs.cnt)[index];
1834
1835 /* Make hole for new record */
1836 _new = &fd->width.freqs.array[index];
1837 memmove(_new + 1, _new,
1838 (fd->width.freqs.cnt++ - index) * sizeof(WidthFreq));
1839
1840 /* Fill new record */
1841 _new->width = glyph->hAdv;
1842 _new->count = 1;
1843 }
1844 }
1845
1846 /* Choose nominal and default widths */
1847 h->_new->size.widths = 0;
1848 for (i = 0; i < h->_new->FDArray.cnt; i++) {
1849 h->_new->size.widths += assignWidths(h, &h->_new->FDArray.array[i]);
1850 }
1851 }
1852
1853 static void zeroOutEmptyFontBBox(cfwCtx g) {
1854 if ( (g->font_bbox.left == INT16_MAX)
1855 && (g->font_bbox.bottom == INT16_MAX)
1856 && (g->font_bbox.right == INT16_MIN)
1857 && (g->font_bbox.top == INT16_MIN)) {
1858 g->font_bbox.left = 0;
1859 g->font_bbox.right = 0;
1860 g->font_bbox.top = 0;
1861 g->font_bbox.bottom = 0;
1862 }
1863 }
1864
1865 /* End font. */
1866 int cfwEndFont(cfwCtx g, abfTopDict *top) {
1867 controlCtx h = g->ctx.control;
1868 long i;
1869
1870 if (top == NULL) {
1871 /* Discard the last font */
1872 if (h->FontSet.cnt > 0) {
1873 h->FontSet.cnt--;
1874 }
1875 return cfwSuccess;
1876 }
1877
1878 /* Set error handler */
1879 DURING_EX(g->err.env)
1880
1881 /* If there are more warnings than get shown, write out how many there were. */
1882 printFinalWarn(g);
1883
1884 /* Set font-wide flags */
1885 h->_new->flags = 0;
1886 if (top->sup.flags & ABF_CID_FONT) {
1887 /* Validate CID data */
1888 if (cfwSindexInvalidString(top->cid.Registry.ptr) ||
1889 cfwSindexInvalidString(top->cid.Ordering.ptr) ||
1890 top->cid.Supplement == ABF_UNSET_INT) {
1891 return cfwErrBadDict;
1892 }
1893
1894 if (h->flags & SEEN_NAME_KEYED_GLYPH) {
1895 return cfwErrGlyphType;
1896 }
1897 if (top->FDArray.cnt < 1 || top->FDArray.cnt > 65536) {
1898 return cfwErrBadFDArray;
1899 }
1900
1901 h->_new->flags |= FONT_CID;
1902 } else {
1903 if (h->flags & SEEN_CID_KEYED_GLYPH) {
1904 return cfwErrGlyphType;
1905 }
1906 if (top->FDArray.cnt != 1) {
1907 return cfwErrBadFDArray;
1908 }
1909
1910 if (top->sup.flags & ABF_SYN_FONT) {
1911 h->_new->flags |= FONT_SYN;
1912 }
1913 }
1914
1915 /* if we only had empty glyphs, set bbox to zeros */
1916 zeroOutEmptyFontBBox(g);
1917
1918 /* update top dict's font bounding box with aggregate values */
1919 top->FontBBox[0] = (float)g->font_bbox.left;
1920 top->FontBBox[1] = (float)g->font_bbox.bottom;
1921 top->FontBBox[2] = (float)g->font_bbox.right;
1922 top->FontBBox[3] = (float)g->font_bbox.top;
1923
1924 /* Copy dict data from client */
1925 cfwDictCopyTop(g, &h->_new->top, top);
1926
1927 if (!(h->mergedDicts && (g->flags & CFW_CHECK_IF_GLYPHS_DIFFER))) {
1928 /* Initialize FDArray */
1929 dnaSET_CNT(h->_new->FDArray, top->FDArray.cnt);
1930 for (i = 0; i < top->FDArray.cnt; i++) {
1931 abfFontDict *src = &top->FDArray.array[i];
1932 FDInfo *dst = &h->_new->FDArray.array[i];
1933
1934 /* Copy font and Private dict data from client */
1935 cfwDictCopyFont(g, &dst->dict, src);
1936 cfwDictCopyPrivate(g, &dst->Private, &src->Private);
1937
1938 dst->Subrs.count = 0;
1939 memset(&dst->subrData, 0, sizeof(subr_CSData));
1940 dst->width.freqs.cnt = 0;
1941 }
1942 }
1943 /* else
1944 {
1945 FDArray has already been added by client during glyph processing
1946 Warning: at this point, the h->_new->FDArray is out of sync with the h->_new->top.FDArray!
1947 There may be more dicts in h->_new->FDArray.
1948 }
1949 */
1950
1951 if (h->_new->flags & FONT_CID) {
1952 orderCIDKeyedGlyphs(h);
1953 } else {
1954 orderNameKeyedGlyphs(h);
1955 }
1956
1957 analyzeWidths(h);
1958
1959 if (h->_new->map != NULL && !(g->flags & CFW_PRESERVE_GLYPH_ORDER)) {
1960 /* Callback glyph mapping */
1961 for (i = 0; i < h->_new->glyphs.cnt; i++) {
1962 h->_new->map->glyphmap(h->_new->map, (unsigned short)i,
1963 h->_new->glyphs.array[i].info);
1964 }
1965 }
1966
1967 /* The "glyph->info" field points to the client structure that was supplied
1968 to the glyphBeg() callback. This isn't required to be stable after
1969 returning from from this function so we set to null to ensure it isn't
1970 subsequently used by mistake. */
1971 for (i = 0; i < h->_new->glyphs.cnt; i++) {
1972 h->_new->glyphs.array[i].info = NULL;
1973 }
1974
1975 /* release the merge copy buffers */
1976 if (h->mergeBuffers.newGlyph != NULL) {
1977 g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.newGlyph, 0);
1978 h->mergeBuffers.newGlyph = NULL;
1979 }
1980 if (h->mergeBuffers.oldGlyph != NULL) {
1981 g->cb.mem.manage(&g->cb.mem, h->mergeBuffers.oldGlyph, 0);
1982 h->mergeBuffers.oldGlyph = NULL;
1983 }
1984
1985 HANDLER
1986 return g->err.code;
1987 END_HANDLER
1988
1989 return cfwSuccess;
1990 }
1991
1992 /* Preparse module for reuse. */
1993 static void cfwControlReuse(cfwCtx g) {
1994 }
1995
1996 static void initSubrData(subr_CSData *subrData) {
1997 subrData->data = 0;
1998 subrData->offset = 0;
1999 }
2000
2001 static void freeSubrData(cfwCtx g, subr_CSData *subrData) {
2002 if (subrData->data) {
2003 cfwMemFree(g, subrData->data);
2004 }
2005 if (subrData->offset) {
2006 cfwMemFree(g, subrData->offset);
2007 }
2008 }
2009
2010 /* Call subroutinizer with copies of charstrings */
2011 static void cfwCallSubrizer(cfwCtx g) {
2012 controlCtx h = g->ctx.control;
2013 int nFonts = h->FontSet.cnt;
2014 int iFont;
2015 int iGlyph;
2016 cff_Font *cffFont;
2017 subr_Font *subrFonts = (subr_Font *)cfwMemNew(g, sizeof(subr_Font) * nFonts);
2018 subr_Font *subrFont;
2019 Offset offset;
2020
2021 /* Repackage font data in formats subroutinizer expects */
2022 memset(subrFonts, 0, sizeof(subr_Font) * nFonts);
2023
2024 DURING_EX(g->err.env)
2025
2026 for (iFont = 0; iFont < nFonts; iFont++) {
2027 cffFont = &h->FontSet.array[iFont];
2028 subrFont = &subrFonts[iFont];
2029
2030 /* Set up FDInfo and FDIndex arrays */
2031 subrFont->fdCount = (short)cffFont->FDArray.cnt;
2032 subrFont->fdInfo = (subr_FDInfo *)cfwMemNew(g, sizeof(subr_FDInfo) * subrFont->fdCount);
2033 memset(subrFont->fdInfo, 0, sizeof(subr_FDInfo) * subrFont->fdCount);
2034
2035 if ((cffFont->flags & FONT_CID) || (g->flags & CFW_WRITE_CFF2)) {
2036 subrFont->flags = SUBR_FONT_CID;
2037 subrFont->fdIndex = (subr_FDIndex *)cfwMemNew(g, sizeof(subr_FDIndex) * cffFont->glyphs.cnt);
2038 for (iGlyph = 0; iGlyph < cffFont->glyphs.cnt; iGlyph++) {
2039 Glyph *glyph = &cffFont->glyphs.array[iGlyph];
2040 subrFont->fdIndex[iGlyph] = glyph->iFD;
2041 }
2042 }
2043
2044 /* Set up charoffsets and charstrings in memory */
2045 subrFont->chars.nStrings = (unsigned short)cffFont->glyphs.cnt;
2046 subrFont->chars.offset = (Offset *)cfwMemNew(g, sizeof(Offset) * (subrFont->chars.nStrings));
2047 subrFont->chars.data = (char *)cfwMemNew(g, g->cb.stm.tell(&g->cb.stm, g->stm.tmp));
2048
2049 offset = 0;
2050 for (iGlyph = 0; iGlyph < cffFont->glyphs.cnt; iGlyph++) {
2051 Glyph *glyph = &cffFont->glyphs.array[iGlyph];
2052 tmp2bufCopy(g, glyph->cstr.length, glyph->cstr.offset, &subrFont->chars.data[offset]);
2053 offset += glyph->cstr.length;
2054 subrFont->chars.offset[iGlyph] = offset;
2055 }
2056 }
2057
2058 /* Call the subroutinizer. The result replaces the charstring in the buffer in place. */
2059 cfwSubrSubrize(g, nFonts, &subrFonts[0]);
2060
2061 /* Copy back charstrings back to tmp buffer */
2062 if (g->cb.stm.seek(&g->cb.stm, g->stm.tmp, 0)) {
2063 cfwFatal(g, cfwErrTmpStream, NULL);
2064 }
2065
2066 for (iFont = 0; iFont < nFonts; iFont++) {
2067 long length;
2068 FDInfo *fd;
2069 int i;
2070 cffFont = &h->FontSet.array[iFont];
2071 subrFont = &subrFonts[iFont];
2072
2073 length = subrFont->chars.offset[subrFont->chars.nStrings - 1];
2074 if ((int)g->cb.stm.write(&g->cb.stm, g->stm.tmp,
2075 length, subrFont->chars.data) != length) {
2076 cfwFatal(g, cfwErrTmpStream, NULL);
2077 }
2078
2079 offset = 0;
2080 for (iGlyph = 0; iGlyph < cffFont->glyphs.cnt; iGlyph++) {
2081 Glyph *glyph = &cffFont->glyphs.array[iGlyph];
2082 Offset nextOffset = subrFont->chars.offset[iGlyph];
2083 glyph->cstr.offset = offset;
2084 glyph->cstr.length = nextOffset - offset;
2085 offset = nextOffset;
2086 }
2087
2088 /* Copy back local subr info */
2089 for (i = 0; i < cffFont->FDArray.cnt; i++) {
2090 fd = &cffFont->FDArray.array[i];
2091 if ((cffFont->flags & FONT_CID) || (g->flags & CFW_WRITE_CFF2)) {
2092 fd->subrData = subrFont->fdInfo[i].subrs;
2093 } else {
2094 fd->subrData = subrFont->subrs;
2095 }
2096 fd->Subrs.count = fd->subrData.nStrings;
2097 }
2098 }
2099
2100 h->_new->CharStrings.datasize = g->cb.stm.tell(&g->cb.stm, g->stm.tmp);
2101
2102 HANDLER
2103 END_HANDLER
2104
2105 /* Free temporary data used by subroutinizer */
2106 for (iFont = 0; iFont < nFonts; iFont++) {
2107 subrFont = &subrFonts[iFont];
2108 freeSubrData(g, &subrFont->chars);
2109 if (subrFont->fdInfo) {
2110 cfwMemFree(g, subrFont->fdInfo);
2111 }
2112 if (subrFont->fdIndex) {
2113 cfwMemFree(g, subrFont->fdIndex);
2114 }
2115 }
2116
2117 if (subrFonts)
2118 cfwMemFree(g, subrFonts);
2119 }
2120
2121 /* End FontSet. */
2122 int cfwEndSet(cfwCtx g) {
2123 controlCtx h = g->ctx.control;
2124
2125 if (h->FontSet.cnt == 0) {
2126 return cfwSuccess; /* Nothing to do */
2127 }
2128 /* Set error handler */
2129 DURING_EX(g->err.env)
2130
2131 /* Subroutinize charstrings */
2132 if (g->flags & CFW_SUBRIZE) {
2133 cfwCallSubrizer(g);
2134 }
2135
2136 fillSet(h); /* all subrs and charstrings must be in place by the time this is called. */
2137 writeSet(h);
2138
2139 /* Prepare for reuse */
2140 cfwControlReuse(g);
2141 cfwCharsetReuse(g);
2142 cfwEncodingReuse(g);
2143 cfwFdselectReuse(g);
2144 cfwSindexReuse(g);
2145 cfwDictReuse(g);
2146 cfwCstrReuse(g);
2147 cfwSubrReuse(g);
2148
2149 /* Close debug stream */
2150 if (g->stm.dbg != NULL) {
2151 (void)g->cb.stm.close(&g->cb.stm, g->stm.dbg);
2152 }
2153
2154 HANDLER
2155 return g->err.code;
2156 END_HANDLER
2157
2158 return cfwSuccess;
2159 }
2160
2161 /* -------------------------- Safe dynarr Context -------------------------- */
2162
2163 /* Manage memory and handle failure. */
2164 static void *safeManage(ctlMemoryCallbacks *cb, void *old, size_t size) {
2165 cfwCtx g = (cfwCtx)cb->ctx;
2166 void *ptr = g->cb.mem.manage(&g->cb.mem, old, size);
2167 if (size > 0 && ptr == NULL) {
2168 cfwFatal(g, cfwErrNoMemory, NULL);
2169 }
2170 return ptr;
2171 }
2172
2173 /* Initialize error handling dynarr context. */
2174 static void dnaSafeInit(cfwCtx g) {
2175 ctlMemoryCallbacks cb;
2176 cb.ctx = g;
2177 cb.manage = safeManage;
2178 g->ctx.dnaSafe = dnaNew(&cb, DNA_CHECK_ARGS);
2179 }
2180
2181 /* -------------------------- Fail dynarr Context -------------------------- */
2182
2183 /* Manage memory and handle failure. */
2184 static void *failManage(ctlMemoryCallbacks *cb, void *old, size_t size) {
2185 cfwCtx g = (cfwCtx)cb->ctx;
2186 return g->cb.mem.manage(&g->cb.mem, old, size);
2187 }
2188
2189 /* Initialize error returning dynarr context. */
2190 static void dnaFailInit(cfwCtx g) {
2191 ctlMemoryCallbacks cb;
2192 cb.ctx = g;
2193 cb.manage = failManage;
2194 g->ctx.dnaFail = dnaNew(&cb, DNA_CHECK_ARGS);
2195 if (g->ctx.dnaFail == NULL) {
2196 cfwFatal(g, cfwErrNoMemory, NULL);
2197 }
2198 }
2199
2200 /* ---------------------------- Library Context ---------------------------- */
2201
2202 /* Validate client and create context. */
2203 cfwCtx cfwNew(ctlMemoryCallbacks *mem_cb, ctlStreamCallbacks *stm_cb,
2204 CTL_CHECK_ARGS_DCL) {
2205 cfwCtx g;
2206
2207 /* Check client/library compatibility */
2208 if (CTL_CHECK_ARGS_TEST(CFW_VERSION)) {
2209 return NULL;
2210 }
2211
2212 /* Allocate context */
2213 g = (cfwCtx)mem_cb->manage(mem_cb, NULL, sizeof(struct cfwCtx_));
2214 if (g == NULL) {
2215 return NULL;
2216 }
2217
2218 /* Safety initialization */
2219 memset(g, 0, sizeof(*g));
2220
2221 g->cb.mem = *mem_cb;
2222 g->cb.stm = *stm_cb;
2223 g->stm.dst = NULL;
2224 g->stm.tmp = NULL;
2225 g->stm.dbg = NULL;
2226
2227 /* Set error handler */
2228 DURING_EX(g->err.env)
2229
2230 /* Initialize service libraries */
2231 dnaSafeInit(g);
2232 dnaFailInit(g);
2233
2234 /* Initialize modules */
2235 cfwControlNew(g);
2236 cfwCharsetNew(g);
2237 cfwEncodingNew(g);
2238 cfwFdselectNew(g);
2239 cfwSindexNew(g);
2240 cfwDictNew(g);
2241 cfwCstrNew(g);
2242 cfwSubrNew(g);
2243
2244 g->err.code = cfwSuccess;
2245
2246 HANDLER
2247 /* Initialization failed */
2248 cfwFree(g);
2249 g = NULL;
2250 END_HANDLER
2251
2252 return g;
2253 }
2254
2255 /* Free context. */
2256 void cfwFree(cfwCtx g) {
2257 if (g == NULL) {
2258 return;
2259 }
2260
2261 /* Free other modules */
2262 cfwControlFree(g);
2263 cfwCharsetFree(g);
2264 cfwEncodingFree(g);
2265 cfwFdselectFree(g);
2266 cfwSindexFree(g);
2267 cfwDictFree(g);
2268 cfwCstrFree(g);
2269 cfwSubrFree(g);
2270
2271 /* Free service libraries */
2272 dnaFree(g->ctx.dnaSafe);
2273 dnaFree(g->ctx.dnaFail);
2274
2275 /* Free library context */
2276 g->cb.mem.manage(&g->cb.mem, g, 0);
2277 }
2278
2279 /* ----------------------------- Error Handling ---------------------------- */
2280
2281 /* Write message to debug stream from va_list. */
2282 static void vmessage(cfwCtx g, char *fmt, va_list ap) {
2283 char text[500];
2284
2285 if (g->stm.dbg == NULL) {
2286 return; /* Debug stream not available */
2287 }
2288 VSPRINTF_S(text, sizeof(text), fmt, ap);
2289 (void)g->cb.stm.write(&g->cb.stm, g->stm.dbg, strlen(text), text);
2290 }
2291
2292 /* Write message to debug stream from varargs. */
2293 void CTL_CDECL cfwMessage(cfwCtx g, char *fmt, ...) {
2294 va_list ap;
2295 va_start(ap, fmt);
2296 vmessage(g, fmt, ap);
2297 va_end(ap);
2298 }
2299
2300 /* Handle fatal error. */
2301 void CTL_CDECL cfwFatal(cfwCtx g, int err_code, char *fmt, ...) {
2302 if (fmt == NULL) {
2303 /* Write standard error message */
2304 cfwMessage(g, "%s", cfwErrStr(err_code));
2305 } else {
2306 /* Write font-specific error message */
2307 va_list ap;
2308 va_start(ap, fmt);
2309 vmessage(g, fmt, ap);
2310 va_end(ap);
2311 }
2312 g->err.code = (short)err_code;
2313 RAISE(&g->err.env, err_code, NULL);
2314 }
2315
2316 /* --------------------------- Memory Management --------------------------- */
2317
2318 /* Allocate memory. */
2319 void *cfwMemNew(cfwCtx g, size_t size) {
2320 void *ptr = g->cb.mem.manage(&g->cb.mem, NULL, size);
2321 if (ptr == NULL) {
2322 cfwFatal(g, cfwErrNoMemory, NULL);
2323 }
2324
2325 /* Safety initialization */
2326 memset(ptr, 0, size);
2327
2328 return ptr;
2329 }
2330
2331 /* Free memory. */
2332 void cfwMemFree(cfwCtx g, void *ptr) {
2333 (void)g->cb.mem.manage(&g->cb.mem, ptr, 0);
2334 }
2335
2336 /* ------------------------------ Data Output ------------------------------ */
2337
2338 /* Write 1-byte number. */
2339 void cfwWrite1(cfwCtx g, uint8_t value) {
2340 if (g->cb.stm.write(&g->cb.stm, g->stm.dst, 1, (char *)&value) != 1) {
2341 cfwFatal(g, cfwErrDstStream, NULL);
2342 }
2343 }
2344
2345 /* Write 2-byte number. */
2346 void cfwWrite2(cfwCtx g, unsigned short value) {
2347 uint8_t buf[2];
2348 buf[0] = (uint8_t)(value >> 8);
2349 buf[1] = (uint8_t)value;
2350 if (g->cb.stm.write(&g->cb.stm, g->stm.dst, 2, (char *)buf) != 2) {
2351 cfwFatal(g, cfwErrDstStream, NULL);
2352 }
2353 }
2354
2355 /* Write N-byte number. */
2356 void cfwWriteN(cfwCtx g, int N, unsigned long value) {
2357 char buf[4];
2358 uint8_t *p = (uint8_t *)buf;
2359 switch (N) {
2360 case 4:
2361 *p++ = (uint8_t)(value >> 24);
2362
2363 case 3:
2364 *p++ = (uint8_t)(value >> 16);
2365
2366 case 2:
2367 *p++ = (uint8_t)(value >> 8);
2368
2369 case 1:
2370 *p = (uint8_t)value;
2371 }
2372 if (g->cb.stm.write(&g->cb.stm, g->stm.dst, N, buf) != (size_t)N) {
2373 cfwFatal(g, cfwErrDstStream, NULL);
2374 }
2375 }
2376
2377 /* Write data buffer. */
2378 void cfwWrite(cfwCtx g, size_t count, char *buf) {
2379 if (g->cb.stm.write(&g->cb.stm, g->stm.dst, count, buf) != count) {
2380 cfwFatal(g, cfwErrDstStream, NULL);
2381 }
2382 }
2383
2384 /* Encode integer in array and return length. */
2385 int cfwEncInt(long i, uint8_t *t) {
2386 if (-107 <= i && i <= 107) {
2387 /* Single byte number */
2388 t[0] = (uint8_t)(i + 139);
2389 return 1;
2390 } else if (108 <= i && i <= 1131) {
2391 /* Positive 2-byte number */
2392 i -= 108;
2393 t[0] = (uint8_t)((i >> 8) + 247);
2394 t[1] = (uint8_t)i;
2395 return 2;
2396 } else if (-1131 <= i && i <= -108) {
2397 /* Negative 2-byte number */
2398 i += 108;
2399 t[0] = (uint8_t)((-i >> 8) + 251);
2400 t[1] = (uint8_t)-i;
2401 return 2;
2402 } else if (-32768 <= i && i <= 32767) {
2403 /* Positive/negative 3-byte number (shared with dict ops) */
2404 t[0] = (uint8_t)t2_shortint;
2405 t[1] = (uint8_t)(i >> 8);
2406 t[2] = (uint8_t)i;
2407 return 3;
2408 } else {
2409 /* Positive/negative 5-byte number (dict ops only) */
2410 t[0] = (uint8_t)cff_longint;
2411 t[1] = (uint8_t)(i >> 24);
2412 t[2] = (uint8_t)(i >> 16);
2413 t[3] = (uint8_t)(i >> 8);
2414 t[4] = (uint8_t)i;
2415 return 5;
2416 }
2417 }
2418
2419 /* Encode real number as 5-byte fixed point in array and return length. */
2420 int cfwEncReal(float r, uint8_t *t) {
2421 long i = (long)(r * 65536.0 + ((r < 0) ? -0.5 : 0.5));
2422 t[0] = (uint8_t)255;
2423 t[1] = (uint8_t)(i >> 24);
2424 t[2] = (uint8_t)(i >> 16);
2425 t[3] = (uint8_t)(i >> 8);
2426 t[4] = (uint8_t)i;
2427 return 5;
2428 }
2429
2430 /* Get version numbers of libraries. */
2431 void cfwGetVersion(ctlVersionCallbacks *cb) {
2432 if (cb->called & 1 << CFW_LIB_ID) {
2433 return; /* Already enumerated */
2434 }
2435 /* Support libraries */
2436 dnaGetVersion(cb);
2437 ctuGetVersion(cb);
2438
2439 /* This library */
2440 cb->getversion(cb, CFW_VERSION, "cffwrite");
2441
2442 /* Record this call */
2443 cb->called |= 1 << CFW_LIB_ID;
2444 }
2445
2446 /* ----------------------------- Error Support ----------------------------- */
2447
2448 /* Map error code to error string. */
2449 char *cfwErrStr(int err_code) {
2450 static char *errstrs[] = {
2451 #undef CTL_DCL_ERR
2452 #define CTL_DCL_ERR(name, string) string,
2453 #include "cfwerr.h"
2454 };
2455 return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
2456 }
2457
2458 /* return current error status. */
2459 int cfwGetErrCode(cfwCtx h) {
2460 return h->err.code;
2461 }
2462
2463 /* ----------------------------- Debug Support ----------------------------- */
2464
2465 #if CFW_DEBUG
2466
2467 /* Dump glyphs. */
2468 static void dbglyphs(controlCtx h, cff_Font *font) {
2469 long i;
2470 if (h->flags & FONT_CID) {
2471 printf("--- glyph[index]={tag,cid,iFD,hAdv,length,offset}\n");
2472 for (i = 0; i < font->glyphs.cnt; i++) {
2473 Glyph *glyph = &font->glyphs.array[i];
2474 abfGlyphInfo *info = glyph->info;
2475 printf("[%3ld]={%hu,%hu,%u,%g,%ld,%ld}\n",
2476 i, info->tag, info->cid, glyph->iFD,
2477 glyph->hAdv, glyph->cstr.length, glyph->cstr.offset);
2478 }
2479 } else {
2480 printf(
2481 "--- glyph[index]="
2482 "{tag,hAdv,length,offset,gname,code[+code]+}\n");
2483 for (i = 0; i < font->glyphs.cnt; i++) {
2484 Glyph *glyph = &font->glyphs.array[i];
2485 abfGlyphInfo *info = glyph->info;
2486 abfEncoding *enc = &info->encoding;
2487 printf("[%3ld]={%3hu,%4g,%3ld,%5ld,",
2488 i, info->tag, glyph->hAdv,
2489 glyph->cstr.length, glyph->cstr.offset);
2490 if (enc->code == ABF_GLYPH_UNENC) {
2491 printf(" -");
2492 } else {
2493 printf("0x%02lX", enc->code);
2494 for (;;) {
2495 enc = enc->next;
2496 if (enc == NULL) {
2497 break;
2498 }
2499 printf("+0x%02lX", enc->code);
2500 }
2501 }
2502 printf(",%s}\n", info->gname.ptr);
2503 }
2504 }
2505 }
2506
2507 /* Dump width frequencies. */
2508 static void dbwidths(controlCtx h, cff_Font *font) {
2509 int i;
2510 int j;
2511 for (i = 0; i < font->FDArray.cnt; i++) {
2512 FDInfo *fd = &font->FDArray.array[i];
2513 printf("--- FD[%d]\n", i);
2514 printf("default=%g\n", fd->width.dflt);
2515 printf("nominal=%g\n", fd->width.nominal);
2516 printf("--- freqs[index]={width,count}\n");
2517 if (fd->width.freqs.cnt == 0) {
2518 printf("empty\n");
2519 } else {
2520 for (j = 0; j < fd->width.freqs.cnt; j++) {
2521 WidthFreq *rec = &fd->width.freqs.array[j];
2522 printf("[%d]={%g,%ld}\n", j, rec->width, rec->count);
2523 }
2524 }
2525 }
2526 }
2527
2528 /* Dump CFF layout. */
2529 static void dblayout(controlCtx h) {
2530 int i;
2531
2532 printf("object offset size\n");
2533 printf("---------- ------- -------\n");
2534 printf("header 0 %7ld\n", h->size.header);
2535 printf("name %7ld %7ld\n", h->offset.name, h->size.name);
2536 printf("top %7ld %7ld\n", h->offset.top, h->size.top);
2537 printf("string %7ld %7ld\n", h->offset.string, h->size.string);
2538 printf("gsubr %7ld %7ld\n", h->offset.gsubr, h->size.gsubr);
2539 printf("charset %7ld %7ld\n", h->offset.charset, h->size.charset);
2540 printf("Encoding %7ld %7ld\n", h->offset.Encoding, h->size.Encoding);
2541 printf("FDSelect %7ld %7ld\n", h->offset.FDSelect, h->size.FDSelect);
2542 printf("end %7ld -\n", h->offset.end);
2543
2544 for (i = 0; i < h->FontSet.cnt; i++) {
2545 int j;
2546 cff_Font *font = &h->FontSet.array[i];
2547
2548 printf("--- font[%d]\n", i);
2549 printf("top - %7ld\n", font->size.top);
2550 printf("charset %7ld -\n", font->offset.charset);
2551 printf("Encoding %7ld -\n", font->offset.Encoding);
2552 printf("FDSelect %7ld -\n", font->offset.FDSelect);
2553 printf("CharStrings %7ld %7ld\n",
2554 font->offset.CharStrings, font->size.CharStrings);
2555 printf("VarStore %7ld %7ld\n",
2556 font->offset.VarStore, font->size.VarStore);
2557 printf("FDArray %7ld %7ld\n",
2558 font->offset.FDArray, font->size.FDArray);
2559 printf("Private %7ld %7ld\n",
2560 font->offset.Private, font->size.Private);
2561 printf("Subrs %7ld %7ld\n",
2562 font->offset.Subrs, font->size.Subrs);
2563
2564 for (j = 0; j < font->FDArray.cnt; j++) {
2565 FDInfo *fd = &font->FDArray.array[j];
2566
2567 printf("-- FDArray[%d]\n", j);
2568 printf("Private %7ld %7ld\n",
2569 fd->offset.Private, fd->size.Private);
2570 printf("Subrs %7ld %7ld\n",
2571 fd->offset.Subrs, fd->size.Subrs);
2572 }
2573 }
2574 }
2575
2576 /* This function just serves to suppress annoying "defined but not used"
2577 compiler messages when debugging */
2578 static void CTL_CDECL dbuse(int arg, ...) {
2579 dbuse(0, dbglyphs, dbwidths, dblayout);
2580 }
2581
2582 #endif /* CFW_DEBUG */