Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/cffwrite/cffwrite_dict.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 * Dictionary support.
7 */
8
9 #include "cffwrite_dict.h"
10 #include "cffwrite_sindex.h"
11 #include "dictops.h"
12 #include "txops.h"
13 #include "ctutil.h"
14 #include "varread.h"
15
16 #include <math.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 /* ----------------------------- Module Context ---------------------------- */
22
23 struct dictCtx_ {
24 dnaDCL(char, tmp); /* Temporary string storage */
25 cfwCtx g; /* Package context */
26 };
27
28 /* Initialize module. */
29 void cfwDictNew(cfwCtx g) {
30 dictCtx h = (dictCtx)cfwMemNew(g, sizeof(struct dictCtx_));
31
32 /* Link contexts */
33 h->g = g;
34 g->ctx.dict = h;
35
36 dnaINIT(g->ctx.dnaSafe, h->tmp, 30, 10);
37 }
38
39 /* Prepare module for reuse. */
40 void cfwDictReuse(cfwCtx g) {
41 dictCtx h = g->ctx.dict;
42 h->tmp.cnt = 0;
43 }
44
45 /* Free resources */
46 void cfwDictFree(cfwCtx g) {
47 dictCtx h = g->ctx.dict;
48
49 if (h == NULL) {
50 return;
51 }
52
53 dnaFREE(h->tmp);
54
55 cfwMemFree(g, h);
56 g->ctx.dict = NULL;
57 }
58
59 /* Add string to string index. */
60 static void addString(cfwCtx g, abfString *str) {
61 str->impl = cfwSindexAddString(g, str->ptr);
62 }
63
64 /* Save FSType in PostScript string. */
65 static void saveFSType(cfwCtx g, abfTopDict *dst) {
66 dictCtx h = g->ctx.dict;
67 char buf[50];
68 const size_t bufLen = sizeof(buf);
69 size_t postscriptLen;
70 size_t bufStringLen;
71
72 if (dst->PostScript.ptr != ABF_UNSET_PTR) {
73 if (strstr(dst->PostScript.ptr, "/FSType") != NULL) {
74 return; /* PostScript string already has /FSType */
75 }
76 /* Copy string to tmp buf */
77 /* 64-bit warning fixed by cast here */
78 postscriptLen = strlen(dst->PostScript.ptr) + 1;
79 STRCPY_S(dnaEXTEND(h->tmp, (long)postscriptLen),
80 postscriptLen,
81 dst->PostScript.ptr);
82 }
83
84 /* Append /FSType to tmp buf */
85 SPRINTF_S(buf, bufLen, "/FSType %ld def", dst->FSType);
86 /* 64-bit warning fixed by cast here */
87 bufStringLen = strnlen(buf, bufLen);
88 STRCPY_S(dnaEXTEND(h->tmp, (long)bufStringLen), bufStringLen, buf);
89
90 /* Set PostScript string to tmp buf */
91 dst->PostScript.ptr = h->tmp.array;
92 }
93
94 /* Save OrigFontType in PostScript string. */
95 static void saveOrigFontType(cfwCtx g, abfTopDict *dst) {
96 dictCtx h = g->ctx.dict;
97 char buf[50];
98 const size_t bufLen = sizeof(buf);
99 char *OrigFontType = NULL; /* Suppress optimizer warning */
100
101 switch (dst->OrigFontType) {
102 case abfOrigFontTypeType1:
103 OrigFontType = "Type1";
104 break;
105
106 case abfOrigFontTypeCID:
107 OrigFontType = "CID";
108 break;
109
110 case abfOrigFontTypeTrueType:
111 OrigFontType = "TrueType";
112 break;
113
114 case abfOrigFontTypeOCF:
115 OrigFontType = "OCF";
116 break;
117
118 default:
119 switch (dst->sup.srcFontType) {
120 case abfSrcFontTypeType1Name:
121 case abfSrcFontTypeCFFName:
122 if (!(dst->sup.flags & ABF_CID_FONT)) {
123 return; /* Nothing to save */
124 }
125 OrigFontType = "Type1"; /* Name-keyed to cid-keyed conversion */
126 break;
127
128 case abfSrcFontTypeType1CID:
129 case abfSrcFontTypeCFFCID:
130 if (dst->sup.flags & ABF_CID_FONT) {
131 return; /* Nothing to save */
132 }
133 OrigFontType = "CID"; /* CID-keyed to name-keyed conversion */
134 break;
135
136 case abfSrcFontTypeSVGName:
137 OrigFontType = "SVG";
138 break;
139
140 case abfSrcFontTypeUFOName:
141 OrigFontType = "UFO";
142 break;
143
144 case abfSrcFontTypeTrueType:
145 /* TrueType to name-keyed or cid-keyed conversion */
146 OrigFontType = "TrueType";
147 break;
148
149 default:
150 return;
151 }
152 break;
153 }
154
155 if (dst->PostScript.ptr != ABF_UNSET_PTR) {
156 size_t postscriptLen;
157 if (strstr(dst->PostScript.ptr, "/OrigFontType") != NULL) {
158 return; /* PostScript string already has /OrigFontType */
159 }
160 /* Copy string to tmp buf */
161 /* 64-bit warning fixed by cast here */
162 postscriptLen = strlen(dst->PostScript.ptr) + 1;
163 STRCPY_S(dnaEXTEND(h->tmp, (long)postscriptLen),
164 postscriptLen,
165 dst->PostScript.ptr);
166 }
167
168 /* Append /OrigFontType to tmp buf */
169 SPRINTF_S(buf, bufLen, "/OrigFontType /%s def", OrigFontType);
170 {
171 /* 64-bit warning fixed by cast here */
172 size_t bufStringLen = strnlen(buf, bufLen);
173 STRCPY_S(dnaEXTEND(h->tmp, (long)bufStringLen), bufStringLen, buf);
174 }
175 /* Set PostScript string to tmp buf */
176 dst->PostScript.ptr = h->tmp.array;
177 }
178
179 /* Copy top dict data from client specification. */
180 void cfwDictCopyTop(cfwCtx g, abfTopDict *dst, abfTopDict *src) {
181 /* Copy all fields */
182 *dst = *src;
183
184 if (g->flags & CFW_EMBED_OPT) {
185 /* When embedding save FSType or OrigFontType */
186 if (dst->FSType != ABF_UNSET_INT) {
187 saveFSType(g, dst);
188 } else {
189 saveOrigFontType(g, dst);
190 }
191 }
192
193 /* Add strings to index */
194 addString(g, &dst->version);
195 addString(g, &dst->Notice);
196 addString(g, &dst->Copyright);
197 addString(g, &dst->FullName);
198 addString(g, &dst->FamilyName);
199 addString(g, &dst->Weight);
200 addString(g, &dst->PostScript);
201 addString(g, &dst->BaseFontName);
202 addString(g, &dst->SynBaseFontName);
203 addString(g, &dst->cid.CIDFontName);
204 addString(g, &dst->cid.Registry);
205 addString(g, &dst->cid.Ordering);
206 }
207
208 /* Copy font dict data from client specification. */
209 void cfwDictCopyFont(cfwCtx g, abfFontDict *dst, abfFontDict *src) {
210 /* Copy all fields */
211 *dst = *src;
212
213 /* Add strings to index */
214 addString(g, &dst->FontName);
215 }
216
217 /* Copy Private dict data from client specification. */
218 void cfwDictCopyPrivate(cfwCtx g, abfPrivateDict *dst, abfPrivateDict *src) {
219 /* Copy all fields */
220 *dst = *src;
221 }
222
223 /* Save integer number arg in DICT. */
224 void cfwDictSaveInt(DICT *dict, long i) {
225 char *arg = dnaEXTEND(*dict, 5);
226 dict->cnt -= 5 - cfwEncInt(i, (unsigned char *)arg);
227 }
228
229 /* Save real number arg in DICT. If not fractional save as integer. */
230 void cfwDictSaveReal(DICT *dict, float r) {
231 char buf[50];
232 const size_t bufLen = sizeof(buf);
233 int value; /* Current nibble value */
234 int last = 0; /* Last nibble value */
235 int odd = 0; /* Flags odd nibble */
236 long i = (long)r;
237
238 if (i == r) {
239 /* Value was integer */
240 cfwDictSaveInt(dict, i);
241 return;
242 }
243
244 ctuDtostr(buf, bufLen, r, 0, 8); /* 8 places is as good as it gets when converting ASCII real numbers->float-> ASCII real numbers, as happens to all the PrivateDict values.*/
245
246 *dnaNEXT(*dict) = cff_BCD;
247 for (i = buf[0] == '0';; i++) {
248 switch (buf[i]) {
249 case '\0':
250 /* Terminate number */
251 *dnaNEXT(*dict) = (char)(odd ? last << 4 | 0xf : 0xff);
252 return;
253
254 case '+':
255 continue;
256
257 case '-':
258 value = 0xe;
259 break;
260
261 case '.':
262 value = 0xa;
263 break;
264
265 case 'E':
266 case 'e':
267 value = (buf[++i] == '-') ? 0xc : 0xb;
268 break;
269
270 default:
271 value = buf[i] - '0';
272 break;
273 }
274
275 if (odd) {
276 *dnaNEXT(*dict) = (char)(last << 4 | value);
277 } else {
278 last = value;
279 }
280 odd = !odd;
281 }
282 }
283
284 /* Save dict operator. */
285 void cfwDictSaveOp(DICT *dict, int op) {
286 if (op & 0xff00) {
287 *dnaNEXT(*dict) = cff_escape;
288 }
289 *dnaNEXT(*dict) = (unsigned char)op;
290 }
291
292 /* Save integer operator. */
293 void cfwDictSaveIntOp(DICT *dict, long i, int op) {
294 cfwDictSaveInt(dict, i);
295 cfwDictSaveOp(dict, op);
296 }
297
298 /* Save real number operator in DICT. */
299 void cfwDictSaveRealOp(DICT *dict, float r, int op) {
300 cfwDictSaveReal(dict, r);
301 cfwDictSaveOp(dict, op);
302 }
303
304 /* Save simple int array operator in DICT. */
305 static void saveIntArrayOp(DICT *dict, int cnt, long *array, int op) {
306 int i;
307 for (i = 0; i < cnt; i++) {
308 cfwDictSaveInt(dict, array[i]);
309 }
310 cfwDictSaveOp(dict, op);
311 }
312
313 /* Save simple real array operator in DICT. */
314 static void saveRealArrayOp(DICT *dict, int cnt, float *array, int op) {
315 int i;
316 for (i = 0; i < cnt; i++) {
317 cfwDictSaveReal(dict, array[i]);
318 }
319 cfwDictSaveOp(dict, op);
320 }
321
322 /* Save real delta-encoded array operator in DICT. */
323 static void saveRealDeltaOp(DICT *dict, int cnt, float *array, int op) {
324 int i;
325 /* Compute deltas */
326 for (i = cnt - 1; i > 0; i--) {
327 array[i] -= array[i - 1];
328 }
329 saveRealArrayOp(dict, cnt, array, op);
330 }
331
332 static void saveRealBlendOp(DICT *dict, int numRegions, abfOpEntry *opEntry, int op) {
333 int j, k, l;
334 float defaultValue = 0;
335 int numBlends = opEntry->numBlends;
336 float delta;
337 float *blendValues = opEntry->blendValues;
338 /* We can assume that numBlends > 0, else this function is not called.
339 blendValues contains all the numBlends absolute values for first the default region,
340 then for region 0, then for region 1, ..., region n.
341 convert the numBlends*(numRegions+1) values to delta values from previous absolute value
342 in the same region.
343 */
344 for (k = 0; k <= numRegions; k++) {
345 int curIndex = (k + 1) * numBlends - 1;
346 for (j = 1; j < numBlends; j++) {
347 blendValues[curIndex] -= blendValues[curIndex - 1];
348 curIndex--;
349 }
350 }
351
352 // Save the numBlends delta values for the default region
353 for (j = 0; j < numBlends; j++) {
354 delta = blendValues[j];
355 cfwDictSaveReal(dict, delta);
356 }
357 // save the region blend values. These are the difference between
358 // the current region delta value, and the default region delta value.
359 for (l = 0; l < numBlends; l++) {
360 float defaultDeltaValue = blendValues[l];
361 for (k = 0; k < numRegions; k++) {
362 float diff;
363 diff = blendValues[(k + 1) * numBlends + l] - defaultDeltaValue;
364 cfwDictSaveReal(dict, diff);
365 }
366 }
367
368 // save num blends
369 cfwDictSaveInt(dict, numBlends);
370
371 // Save the blend op.
372 cfwDictSaveOp(dict, cff_blend);
373
374 // Save the dict op.
375 cfwDictSaveOp(dict, op);
376 }
377
378 static void saveRealDeltaBlendOp(DICT *dict, int numRegions, abfOpEntryArray *entryArray, int op) {
379 int i, j, k, l;
380 float defaultValue = 0;
381
382 /* Each abfOpEntry* arrayEntry corresponds to one blend operator and its operands */
383 for (i = 0; i < entryArray->cnt; i++) {
384 abfOpEntry *arrayEntry = &entryArray->array[i];
385 int numBlends = arrayEntry->numBlends;
386 if (numBlends == 0) {
387 defaultValue = arrayEntry->value - defaultValue;
388 cfwDictSaveReal(dict, defaultValue);
389 } else {
390 float delta;
391 float *blendValues = arrayEntry->blendValues;
392 /* blendValues contains all the numBlends absolute values for first the default region,
393 then for region 0, then for region 1, ..., region n.
394 convert the numBlends*(numRegions+1) values to delta values from previous absolute value
395 in the same region.
396 */
397 for (k = 0; k <= numRegions; k++) {
398 int curIndex = (k + 1) * numBlends - 1;
399 for (j = 1; j < numBlends; j++) {
400 blendValues[curIndex] -= blendValues[curIndex - 1];
401 curIndex--;
402 }
403 }
404
405 // Save the numBlends delta values for the default region
406 for (j = 0; j < numBlends; j++) {
407 delta = blendValues[j];
408 cfwDictSaveReal(dict, delta);
409 }
410 // save the region blend values. These are the difference between
411 // the current region delta value, and the default region delta value.
412 for (l = 0; l < numBlends; l++) {
413 float defaultDeltaValue = blendValues[l];
414 for (k = 0; k < numRegions; k++) {
415 float diff;
416 diff = blendValues[(k + 1) * numBlends + l] - defaultDeltaValue;
417 cfwDictSaveReal(dict, diff);
418 }
419 }
420
421 // save num blends
422 cfwDictSaveInt(dict, numBlends);
423
424 // Save the blend op.
425 cfwDictSaveOp(dict, cff_blend);
426 }
427 }
428 // Save the PrivatDict op.
429 cfwDictSaveOp(dict, op);
430 }
431
432 /* Save integer delta-encoded array operator in DICT. */
433 static void saveIntDeltaOp(DICT *dict, int cnt, long *array, int op) {
434 int i;
435 /* Compute deltas */
436 for (i = cnt - 1; i > 0; i--) {
437 array[i] -= array[i - 1];
438 }
439 saveIntArrayOp(dict, cnt, array, op);
440 }
441
442 /* Save string in DICT. */
443 static void saveStringOp(cfwCtx g, DICT *dict, SRI sri, int op) {
444 cfwDictSaveInt(dict, cfwSindexAssignSID(g, sri));
445 cfwDictSaveOp(dict, op);
446 }
447
448 /* Save normal FontMatrix. */
449 static void saveFontMatrix(DICT *dict, abfFontMatrix *FontMatrix) {
450 if (!abfIsDefaultFontMatrix(FontMatrix)) {
451 saveRealArrayOp(dict, 6, FontMatrix->array, cff_FontMatrix);
452 }
453 }
454
455 /* Fill CFF top dict. */
456 void cfwDictFillTop(cfwCtx g, DICT *dst,
457 abfTopDict *top, abfFontDict *font0, long iSyntheticBase) {
458 int embed = g->flags & (CFW_EMBED_OPT | CFW_ROM_OPT);
459
460 dst->cnt = 0;
461 if (iSyntheticBase != -1) {
462 /* Make synthetic font dictionary */
463 cfwDictSaveIntOp(dst, iSyntheticBase, cff_SyntheticBase);
464
465 /* FullName */
466 if (top->FullName.impl != SRI_UNDEF) {
467 saveStringOp(g, dst, (SRI)top->FullName.impl, cff_FullName);
468 }
469
470 /* ItalicAngle */
471 if (top->ItalicAngle != cff_DFLT_ItalicAngle) {
472 cfwDictSaveRealOp(dst, top->ItalicAngle, cff_ItalicAngle);
473 }
474
475 /* FontMatrix */
476 saveFontMatrix(dst, &font0->FontMatrix);
477
478 return;
479 }
480
481 if (top->sup.flags & ABF_CID_FONT) {
482 /* FontMatrix; note default values are different from font dict */
483 abfFontMatrix *fm = &(top->cid.FontMatrix);
484 if (fm->cnt != ABF_EMPTY_ARRAY &&
485 (fm->array[0] != 1.0 ||
486 fm->array[1] != 0.0 ||
487 fm->array[2] != 0.0 ||
488 fm->array[3] != 1.0 ||
489 fm->array[4] != 0.0 ||
490 fm->array[5] != 0.0)) {
491 saveRealArrayOp(dst, 6, fm->array, cff_FontMatrix);
492 }
493 } else if (g->flags & CFW_WRITE_CFF2) {
494 abfFontMatrix *fm = &(font0->FontMatrix);
495 if (fm->cnt != ABF_EMPTY_ARRAY &&
496 (fm->array[0] != 1.0 ||
497 fm->array[1] != 0.0 ||
498 fm->array[2] != 0.0 ||
499 fm->array[3] != 1.0 ||
500 fm->array[4] != 0.0 ||
501 fm->array[5] != 0.0)) {
502 saveRealArrayOp(dst, 6, fm->array, cff_FontMatrix);
503 }
504 } else {
505 /* FontMatrix */
506 saveFontMatrix(dst, &font0->FontMatrix);
507 }
508
509 if (g->flags & CFW_WRITE_CFF2) {
510 // maxstack deprecated on April 2017
511 // if (top->maxstack != CFF2_DEFAULT_OP_STACK ) {
512 // cfwDictSaveIntOp(dst, top->maxstack, cff_maxstack);
513 // }
514 return;
515 }
516
517 /* ROS */
518 if (top->sup.flags & ABF_CID_FONT) {
519 cfwDictSaveInt(dst,
520 cfwSindexAssignSID(g, (SRI)top->cid.Registry.impl));
521 cfwDictSaveInt(dst,
522 cfwSindexAssignSID(g, (SRI)top->cid.Ordering.impl));
523 cfwDictSaveInt(dst, top->cid.Supplement);
524 cfwDictSaveOp(dst, cff_ROS);
525 }
526
527 /* version */
528 if (top->version.impl != SRI_UNDEF &&
529 !embed) {
530 saveStringOp(g, dst, (SRI)top->version.impl, cff_version);
531 }
532
533 /* Notice */
534 if (top->Notice.impl != SRI_UNDEF) {
535 saveStringOp(g, dst, (SRI)top->Notice.impl, cff_Notice);
536 }
537
538 if (top->Copyright.impl != SRI_UNDEF &&
539 (!embed || top->Notice.impl == SRI_UNDEF)) {
540 saveStringOp(g, dst, (SRI)top->Copyright.impl, cff_Copyright);
541 }
542
543 /* FullName */
544 if (top->FullName.impl != SRI_UNDEF &&
545 !embed) {
546 saveStringOp(g, dst, (SRI)top->FullName.impl, cff_FullName);
547 }
548
549 /* FamilyName */
550 if (top->FamilyName.impl != SRI_UNDEF &&
551 !embed) {
552 saveStringOp(g, dst, (SRI)top->FamilyName.impl, cff_FamilyName);
553 }
554
555 /* Weight */
556 if (top->Weight.impl != SRI_UNDEF) {
557 saveStringOp(g, dst, (SRI)top->Weight.impl, cff_Weight);
558 }
559
560 /* isFixedPitch */
561 if (top->isFixedPitch != cff_DFLT_isFixedPitch &&
562 !embed) {
563 cfwDictSaveIntOp(dst, top->isFixedPitch, cff_isFixedPitch);
564 }
565
566 /* ItalicAngle */
567 if (top->ItalicAngle != cff_DFLT_ItalicAngle) {
568 cfwDictSaveRealOp(dst, top->ItalicAngle, cff_ItalicAngle);
569 }
570
571 /* UnderlinePosition */
572 if (top->UnderlinePosition != cff_DFLT_UnderlinePosition &&
573 !embed) {
574 cfwDictSaveRealOp(dst, top->UnderlinePosition, cff_UnderlinePosition);
575 }
576
577 /* UnderlineThickness */
578 if (top->UnderlineThickness != cff_DFLT_UnderlineThickness &&
579 !embed) {
580 cfwDictSaveRealOp(dst, top->UnderlineThickness, cff_UnderlineThickness);
581 }
582
583 /* PostScript */
584 if (top->PostScript.impl != SRI_UNDEF) {
585 saveStringOp(g, dst, (SRI)top->PostScript.impl, cff_PostScript);
586 }
587
588 /* BaseFontName */
589 if (top->BaseFontName.impl != SRI_UNDEF) {
590 saveStringOp(g, dst, (SRI)top->BaseFontName.impl, cff_BaseFontName);
591 }
592
593 /* BaseFontBlend */
594 if (top->BaseFontBlend.cnt != ABF_EMPTY_ARRAY) {
595 saveIntDeltaOp(dst, top->BaseFontBlend.cnt, top->BaseFontBlend.array,
596 cff_BaseFontBlend);
597 }
598
599 /* FontBBox */
600 if (top->FontBBox[0] != 0 ||
601 top->FontBBox[1] != 0 ||
602 top->FontBBox[2] != 0 ||
603 top->FontBBox[3] != 0) {
604 top->FontBBox[0] = roundf(top->FontBBox[0]);
605 top->FontBBox[1] = roundf(top->FontBBox[1]);
606 top->FontBBox[2] = roundf(top->FontBBox[2]);
607 top->FontBBox[3] = roundf(top->FontBBox[3]);
608 saveRealArrayOp(dst, 4, top->FontBBox, cff_FontBBox);
609 }
610
611 /* UniqueID */
612 if (top->UniqueID != ABF_UNSET_INT) {
613 cfwDictSaveIntOp(dst, top->UniqueID, cff_UniqueID);
614 }
615
616 /* StrokeWidth */
617 if (top->StrokeWidth != cff_DFLT_StrokeWidth) {
618 cfwDictSaveRealOp(dst, top->StrokeWidth, cff_StrokeWidth);
619 }
620
621 if (top->sup.flags & ABF_CID_FONT) {
622 /* CIDFontVersion */
623 if (top->cid.CIDFontVersion != cff_DFLT_CIDFontVersion) {
624 cfwDictSaveRealOp(dst, top->cid.CIDFontVersion, cff_CIDFontVersion);
625 }
626
627 /* CIDFontRevision */
628 if (top->cid.CIDFontRevision != cff_DFLT_CIDFontRevision) {
629 cfwDictSaveIntOp(dst, top->cid.CIDFontRevision,
630 cff_CIDFontRevision);
631 }
632
633 /* CIDCount */
634 if (top->cid.CIDCount != cff_DFLT_CIDCount) {
635 cfwDictSaveIntOp(dst, top->cid.CIDCount, cff_CIDCount);
636 }
637
638 /* UIDBase */
639 if (top->cid.UIDBase != ABF_UNSET_INT) {
640 cfwDictSaveIntOp(dst, top->cid.UIDBase, cff_UIDBase);
641 }
642 } else {
643 /* PaintType */
644 if (font0->PaintType != cff_DFLT_PaintType) {
645 cfwDictSaveIntOp(dst, font0->PaintType, cff_PaintType);
646 }
647 }
648
649 /* XUID */
650 if (top->XUID.cnt != ABF_EMPTY_ARRAY) {
651 saveIntArrayOp(dst, top->XUID.cnt, top->XUID.array, cff_XUID);
652 }
653 }
654
655 /* Fill CFF font dict. */
656 void cfwDictFillFont(cfwCtx g, DICT *dst, abfFontDict *src) {
657 dst->cnt = 0;
658
659 if (g->flags & CFW_WRITE_CFF2) {
660 return;
661 }
662
663 /* FontName */
664 if (src->FontName.impl != SRI_UNDEF) {
665 saveStringOp(g, dst, (SRI)src->FontName.impl, cff_FontName);
666 }
667
668 /* PaintType */
669 if (src->PaintType != cff_DFLT_PaintType) {
670 cfwDictSaveIntOp(dst, src->PaintType, cff_PaintType);
671 }
672
673 /* FontMatrix */
674 saveFontMatrix(dst, &src->FontMatrix);
675 }
676
677 /* Delete duplicate family array. */
678 static void delFamilyDups(long bluescnt, float *blues,
679 long *familycnt, float *family, int other) {
680 long i;
681 long j;
682 long iBeg;
683 int matched;
684
685 if (bluescnt == 0 || bluescnt != *familycnt) {
686 return;
687 }
688
689 if (other) {
690 iBeg = 0;
691 matched = 0;
692 } else {
693 /* Match bottom zones */
694 if (blues[0] != family[0] ||
695 blues[1] != family[1]) {
696 return;
697 }
698 iBeg = 2;
699 matched = 2;
700 }
701
702 for (i = iBeg; i < *familycnt; i += 2) {
703 /* Get family pair for comparison */
704 float lo = family[i + 0];
705 float hi = family[i + 1];
706
707 /* Search blues for a match */
708 for (j = iBeg; j < bluescnt; j += 2) {
709 if (blues[j + 0] == lo && blues[j + 1] == hi) {
710 /* Match found; count it */
711 matched += 2;
712 break;
713 }
714 }
715 }
716
717 if (matched == bluescnt) {
718 *familycnt = 0;
719 }
720 }
721
722 /* Fill CFF FDInfo DICTs. */
723 void cfwDictFillPrivate(cfwCtx g, DICT *dst, abfPrivateDict *src) {
724 dst->cnt = 0;
725
726 if (!(g->flags & CFW_NO_FAMILY_OPT)) {
727 /* Delete duplicates from the Family arrays */
728 delFamilyDups(src->BlueValues.cnt, src->BlueValues.array,
729 &src->FamilyBlues.cnt, src->FamilyBlues.array, 0);
730 delFamilyDups(src->OtherBlues.cnt, src->OtherBlues.array,
731 &src->FamilyOtherBlues.cnt,
732 src->FamilyOtherBlues.array, 1);
733 }
734
735 /* Delete StemSnap arrays that match corresponding Std value */
736 if (src->StemSnapH.cnt == 1 && src->StemSnapH.array[0] == src->StdHW) {
737 src->StemSnapH.cnt = ABF_EMPTY_ARRAY;
738 }
739 if (src->StemSnapV.cnt == 1 && src->StemSnapV.array[0] == src->StdVW) {
740 src->StemSnapV.cnt = ABF_EMPTY_ARRAY;
741 }
742
743 /* BlueValues */
744 if (src->BlueValues.cnt != ABF_EMPTY_ARRAY) {
745 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.BlueValues.cnt > 0)) {
746 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.BlueValues), cff_BlueValues);
747 } else {
748 saveRealDeltaOp(dst, src->BlueValues.cnt, src->BlueValues.array, cff_BlueValues);
749 }
750 }
751
752 /* OtherBlues */
753 if (src->OtherBlues.cnt != ABF_EMPTY_ARRAY) {
754 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.OtherBlues.cnt > 0)) {
755 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.OtherBlues), cff_OtherBlues);
756 } else {
757 saveRealDeltaOp(dst, src->OtherBlues.cnt, src->OtherBlues.array,
758 cff_OtherBlues);
759 }
760 }
761
762 /* FamilyBlues */
763 if (src->FamilyBlues.cnt != ABF_EMPTY_ARRAY) {
764 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.FamilyBlues.cnt > 0)) {
765 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.FamilyBlues), cff_FamilyBlues);
766 } else {
767 saveRealDeltaOp(dst, src->FamilyBlues.cnt, src->FamilyBlues.array,
768 cff_FamilyBlues);
769 }
770 }
771
772 /* FamilyOtherBlues */
773 if (src->FamilyOtherBlues.cnt != ABF_EMPTY_ARRAY) {
774 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.FamilyOtherBlues.cnt > 0)) {
775 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.FamilyOtherBlues), cff_FamilyOtherBlues);
776 } else {
777 saveRealDeltaOp(dst, src->FamilyOtherBlues.cnt,
778 src->FamilyOtherBlues.array, cff_FamilyOtherBlues);
779 }
780 }
781
782 /* BlueScale */
783 if (src->BlueScale != (float)cff_DFLT_BlueScale) {
784 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.BlueScale.numBlends > 0)) {
785 saveRealBlendOp(dst, src->numRegions, (abfOpEntry *)&(src->blendValues.BlueScale), cff_BlueScale);
786 } else {
787 cfwDictSaveRealOp(dst, src->BlueScale, cff_BlueScale);
788 }
789 }
790
791 /* BlueShift */
792 if (src->BlueShift != cff_DFLT_BlueShift) {
793 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.BlueShift.numBlends > 0)) {
794 saveRealBlendOp(dst, src->numRegions, (abfOpEntry *)&(src->blendValues.BlueShift), cff_BlueShift);
795 } else {
796 cfwDictSaveRealOp(dst, src->BlueShift, cff_BlueShift);
797 }
798 }
799
800 /* BlueFuzz */
801 if (src->BlueFuzz != cff_DFLT_BlueFuzz) {
802 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.BlueFuzz.numBlends > 0)) {
803 saveRealBlendOp(dst, src->numRegions, (abfOpEntry *)&(src->blendValues.BlueFuzz), cff_BlueFuzz);
804 } else {
805 cfwDictSaveRealOp(dst, src->BlueFuzz, cff_BlueFuzz);
806 }
807 }
808
809 /* StdHW */
810 if (src->StdHW != ABF_UNSET_REAL) {
811 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.StdHW.numBlends > 0)) {
812 saveRealBlendOp(dst, src->numRegions, (abfOpEntry *)&(src->blendValues.StdHW), cff_StdHW);
813 } else {
814 cfwDictSaveRealOp(dst, src->StdHW, cff_StdHW);
815 }
816 }
817
818 /* StdVW */
819 if (src->StdVW != ABF_UNSET_REAL) {
820 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.StdVW.numBlends > 0)) {
821 saveRealBlendOp(dst, src->numRegions, (abfOpEntry *)&(src->blendValues.StdVW), cff_StdVW);
822 } else {
823 cfwDictSaveRealOp(dst, src->StdVW, cff_StdVW);
824 }
825 }
826
827 /* StemSnapH */
828 if (src->StemSnapH.cnt != ABF_EMPTY_ARRAY) {
829 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.StemSnapH.cnt > 0)) {
830 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.StemSnapH), cff_StemSnapH);
831 } else {
832 saveRealDeltaOp(dst, src->StemSnapH.cnt, src->StemSnapH.array,
833 cff_StemSnapH);
834 }
835 }
836
837 /* StemSnapV */
838 if (src->StemSnapV.cnt != ABF_EMPTY_ARRAY) {
839 if ((g->flags & CFW_WRITE_CFF2) && (src->blendValues.StemSnapV.cnt > 0)) {
840 saveRealDeltaBlendOp(dst, src->numRegions, (abfOpEntryArray *)&(src->blendValues.StemSnapV), cff_StemSnapV);
841 } else {
842 saveRealDeltaOp(dst, src->StemSnapV.cnt, src->StemSnapV.array,
843 cff_StemSnapV);
844 }
845 }
846
847 /* LanguageGroup */
848 if (src->LanguageGroup != cff_DFLT_LanguageGroup) {
849 cfwDictSaveIntOp(dst, src->LanguageGroup, cff_LanguageGroup);
850 }
851
852 /* ExpansionFactor */
853 if (src->ExpansionFactor != (float)cff_DFLT_ExpansionFactor) {
854 cfwDictSaveRealOp(dst, src->ExpansionFactor, cff_ExpansionFactor);
855 }
856
857 if (g->flags & CFW_WRITE_CFF2) {
858 if (src->vsindex != 0) {
859 cfwDictSaveRealOp(dst, src->vsindex, cff_vsindex);
860 }
861 return;
862 }
863
864 /* ForceBold */
865 if (src->ForceBold != cff_DFLT_ForceBold) {
866 cfwDictSaveIntOp(dst, src->ForceBold, cff_ForceBold);
867 }
868 /* initialRandomSeed */
869 if (src->initialRandomSeed != cff_DFLT_initialRandomSeed) {
870 cfwDictSaveRealOp(dst, src->initialRandomSeed, cff_initialRandomSeed);
871 }
872 }
873
874 /* ----------------------------- Debug Support ----------------------------- */
875
876 #if CFW_DEBUG
877
878 static void dbdict(DICT *dict) {
879 static char *opname[32] = {
880 /* 0 */ "version",
881 /* 1 */ "Notice",
882 /* 2 */ "FullName",
883 /* 3 */ "FamilyName",
884 /* 4 */ "Weight",
885 /* 5 */ "FontBBox",
886 /* 6 */ "BlueValues",
887 /* 7 */ "OtherBlues",
888 /* 8 */ "FamilyBlues",
889 /* 9 */ "FamilyOtherBlues",
890 /* 10 */ "StdHW",
891 /* 11 */ "StdVW",
892 /* 12 */ "escape",
893 /* 13 */ "UniqueID",
894 /* 14 */ "XUID",
895 /* 15 */ "charset",
896 /* 16 */ "Encoding",
897 /* 17 */ "CharStrings",
898 /* 18 */ "Private",
899 /* 19 */ "Subrs",
900 /* 20 */ "defaultWidthX",
901 /* 21 */ "nominalWidthX",
902 /* 22 */ "vsindex",
903 /* 23 */ "blend",
904 /* 24 */ "reserved24",
905 /* 25 */ "reserved25",
906 /* 26 */ "reserved26",
907 /* 27 */ "reserved27",
908 /* 28 */ "shortint",
909 /* 29 */ "longint",
910 /* 30 */ "BCD",
911 /* 31 */ "BlendLE",
912 };
913 static char *escopname[] = {
914 /* 0 */ "Copyright",
915 /* 1 */ "isFixedPitch",
916 /* 2 */ "ItalicAngle",
917 /* 3 */ "UnderlinePosition",
918 /* 4 */ "UnderlineThickness",
919 /* 5 */ "PaintType",
920 /* 6 */ "CharstringType",
921 /* 7 */ "FontMatrix",
922 /* 8 */ "StrokeWidth",
923 /* 9 */ "BlueScale",
924 /* 10 */ "BlueShift",
925 /* 11 */ "BlueFuzz",
926 /* 12 */ "StemSnapH",
927 /* 13 */ "StemSnapV",
928 /* 14 */ "ForceBold",
929 /* 15 */ "reservedESC15",
930 /* 16 */ "lenIV",
931 /* 17 */ "LanguageGroup",
932 /* 18 */ "ExpansionFactor",
933 /* 19 */ "initialRandomSeed",
934 /* 20 */ "SyntheticBase",
935 /* 21 */ "PostScript",
936 /* 22 */ "BaseFontName",
937 /* 23 */ "BaseFontBlend",
938 /* 24 */ "reservedESC24",
939 /* 25 */ "reservedESC25",
940 /* 26 */ "reservedESC26",
941 /* 27 */ "reservedESC27",
942 /* 28 */ "reservedESC28",
943 /* 29 */ "reservedESC29",
944 /* 30 */ "ROS",
945 /* 31 */ "CIDFontVersion",
946 /* 32 */ "CIDFontRevision",
947 /* 33 */ "CIDFontType",
948 /* 34 */ "CIDCount",
949 /* 35 */ "UIDBase",
950 /* 36 */ "FDArray",
951 /* 37 */ "FDIndex",
952 /* 38 */ "FontName",
953 /* 39 */ "Chameleon",
954 };
955 int i;
956 unsigned char *buf = (unsigned char *)dict->array;
957
958 printf("--- dict\n");
959 i = 0;
960 while (i < dict->cnt) {
961 int op = buf[i];
962 switch (op) {
963 case cff_version:
964 case cff_Notice:
965 case cff_FullName:
966 case cff_FamilyName:
967 case cff_Weight:
968 case cff_FontBBox:
969 case cff_BlueValues:
970 case cff_OtherBlues:
971 case cff_FamilyBlues:
972 case cff_FamilyOtherBlues:
973 case cff_StdHW:
974 case cff_StdVW:
975 case cff_UniqueID:
976 case cff_XUID:
977 case cff_charset:
978 case cff_Encoding:
979 case cff_CharStrings:
980 case cff_Private:
981 case cff_Subrs:
982 case cff_defaultWidthX:
983 case cff_nominalWidthX:
984 case cff_vsindex:
985 case cff_blend:
986 case cff_reserved26:
987 case cff_reserved27:
988 case cff_BlendLE:
989 printf("%s ", opname[op]);
990 i++;
991 break;
992
993 case cff_escape: {
994 /* Process escaped operator */
995 int escop = buf[i + 1];
996 if (escop > (long)ARRAY_LEN(escopname) - 1) {
997 printf("? ");
998 } else {
999 printf("%s ", escopname[escop]);
1000 }
1001 i += 2;
1002 break;
1003 }
1004
1005 case cff_shortint:
1006 /* 2 byte number */
1007 printf("%d ", buf[i + 1] << 8 | buf[i + 2]);
1008 i += 3;
1009 break;
1010
1011 case cff_longint:
1012 /* 5 byte number */
1013 printf("%ld ", ((long)buf[i + 1] << 24 | (long)buf[i + 2] << 16 |
1014 (long)buf[i + 3] << 8 | (long)buf[i + 4]));
1015 i += 5;
1016 break;
1017
1018 case cff_BCD: {
1019 int count = 0;
1020 int byte = 0; /* Suppress optimizer warning */
1021 for (;;) {
1022 int nibble;
1023
1024 if (count++ & 1) {
1025 nibble = byte & 0xf;
1026 } else {
1027 byte = buf[++i];
1028 nibble = byte >> 4;
1029 }
1030 if (nibble == 0xf) {
1031 break;
1032 }
1033
1034 printf("%c", "0123456789.EE?-?"[nibble]);
1035 if (nibble == 0xc) {
1036 printf("-");
1037 }
1038 }
1039 printf(" ");
1040 i++;
1041 } break;
1042
1043 case 247:
1044 case 248:
1045 case 249:
1046 case 250:
1047 /* +ve 2 byte number */
1048 printf("%d ", 108 + 256 * (buf[i] - 247) + buf[i + 1]);
1049 i += 2;
1050 break;
1051
1052 case 251:
1053 case 252:
1054 case 253:
1055 case 254:
1056 /* -ve 2 byte number */
1057 printf("%d ", -108 - 256 * (buf[i] - 251) - buf[i + 1]);
1058 i += 2;
1059 break;
1060
1061 case 255:
1062 printf("? ");
1063 i++;
1064 break;
1065
1066 default:
1067 /* 1 byte number */
1068 printf("%d ", buf[i] - 139);
1069 i++;
1070 break;
1071 }
1072 }
1073 printf("\n");
1074 }
1075
1076 /* This function just serves to suppress annoying "defined but not used"
1077 compiler messages when debugging */
1078 static void CTL_CDECL dbuse(int arg, ...) {
1079 dbuse(0, dbdict);
1080 }
1081
1082 #endif /* CFW_DEBUG */