Debian Codegraph @

texk/dvipdfm-x/tt_cmap.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 /*
22 * A large part of codes are brought from ttfdump-0.5.5.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "system.h"
30 #include "mem.h"
31 #include "error.h"
32 #include "dpxconf.h"
33
34 #include "sfnt.h"
35
36
37 /* Sorry for placing this here.
38 * We need to rewrite TrueType font support code...
39 */
40 #include "cmap.h"
41 #include "cmap_write.h"
42
43 #include "tt_aux.h"
44 #include "tt_gsub.h"
45 #include "tt_post.h"
46
47 #include "unicode.h"
48 #include "agl.h"
49
50 #include "pdfresource.h"
51 #include "dpxfile.h"
52 /* Hash */
53 #include "dpxutil.h"
54
55 #include "tt_cmap.h"
56
57 #define VERBOSE_LEVEL_MIN 0
58
59 /* format 0: byte encoding table */
60 struct cmap0
61 {
62 BYTE glyphIndexArray[256];
63 };
64
65 static struct cmap0 *
66 read_cmap0 (sfnt *sfont, ULONG len)
67 {
68 struct cmap0 *map;
69 int i;
70
71 if (len < 256) {
72 WARN("invalid format 0 TT cmap subtable");
73 return NULL;
74 }
75
76 map = NEW(1, struct cmap0);
77 for (i = 0; i < 256; i++)
78 map->glyphIndexArray[i] = sfnt_get_byte(sfont);
79
80 return map;
81 }
82
83 static void
84 release_cmap0(struct cmap0 *map)
85 {
86 if (map)
87 RELEASE(map);
88 }
89
90 static USHORT
91 lookup_cmap0 (struct cmap0 *map, USHORT cc)
92 {
93 return ((cc > 255) ? 0 : map->glyphIndexArray[cc]);
94 }
95
96 /* format 2: high-byte mapping through table */
97 struct SubHeader
98 {
99 USHORT firstCode;
100 USHORT entryCount;
101 SHORT idDelta;
102 USHORT idRangeOffset;
103 };
104
105 struct cmap2
106 {
107 USHORT subHeaderKeys[256];
108 struct SubHeader *subHeaders;
109 USHORT *glyphIndexArray;
110 };
111
112 static struct cmap2 *
113 read_cmap2 (sfnt *sfont, ULONG len)
114 {
115 struct cmap2 *map;
116 USHORT i, n;
117
118 if (len < 512) {
119 WARN("invalid fromt2 TT cmap subtable");
120 return NULL;
121 }
122
123 map = NEW(1, struct cmap2);
124 for (i = 0; i < 256; i++)
125 map->subHeaderKeys[i] = sfnt_get_ushort(sfont);
126 for (n = 0, i = 0; i < 256; i++) {
127 map->subHeaderKeys[i] /= 8;
128 if (n < map->subHeaderKeys[i])
129 n = map->subHeaderKeys[i];
130 }
131 n += 1; /* the number of subHeaders is one plus the max of subHeaderKeys */
132
133 if (len < 512 + n * 8 ) {
134 WARN("invalid/truncated format2 TT cmap subtable");
135 RELEASE(map);
136 return NULL;
137 }
138
139 map->subHeaders = NEW(n, struct SubHeader);
140 for (i = 0; i < n; i++) {
141 map->subHeaders[i].firstCode = sfnt_get_ushort(sfont);
142 map->subHeaders[i].entryCount = sfnt_get_ushort(sfont);
143 map->subHeaders[i].idDelta = sfnt_get_short(sfont);
144 map->subHeaders[i].idRangeOffset = sfnt_get_ushort(sfont);
145
146 /* It makes things easier to let the offset starts from
147 * the beginning of glyphIndexArray.
148 */
149 if (map->subHeaders[i].idRangeOffset != 0)
150 map->subHeaders[i].idRangeOffset -= (2 + (n - i - 1) * 8);
151 }
152
153 /* Caculate the length of glyphIndexArray, this is ugly,
154 * there should be a better way to get this information.
155 */
156 n = (USHORT) (len - 518 - n * 8) / 2;
157
158 map->glyphIndexArray = NEW(n, USHORT);
159 for (i = 0; i < n; i++)
160 map->glyphIndexArray[i] = sfnt_get_ushort(sfont);
161
162 return map;
163 }
164
165 static void
166 release_cmap2 (struct cmap2 *map)
167 {
168 if (map) {
169 if (map->subHeaders)
170 RELEASE(map->subHeaders);
171 if (map->glyphIndexArray)
172 RELEASE(map->glyphIndexArray);
173 RELEASE(map);
174 }
175 }
176
177 static USHORT
178 lookup_cmap2 (struct cmap2 *map, USHORT cc)
179 {
180 USHORT idx = 0;
181 SHORT idDelta;
182 USHORT firstCode, entryCount, idRangeOffset;
183 int hi, lo;
184 USHORT i;
185
186 hi = (cc >> 8) & 0xff;
187 lo = cc & 0xff;
188
189 /* select which subHeader to use */
190 i = map->subHeaderKeys[hi];
191
192 firstCode = map->subHeaders[i].firstCode;
193 entryCount = map->subHeaders[i].entryCount;
194 idDelta = map->subHeaders[i].idDelta;
195 idRangeOffset = map->subHeaders[i].idRangeOffset / 2;
196
197 if (lo >= firstCode &&
198 lo < firstCode + entryCount) {
199 idRangeOffset += lo - firstCode;
200 idx = map->glyphIndexArray[idRangeOffset];
201 if (idx != 0)
202 idx = (idx + idDelta) & 0xffff;
203 }
204
205 return idx;
206 }
207
208 /*
209 * format 4: segment mapping to delta values
210 * - Microsoft standard character to glyph index mapping table
211 */
212 struct cmap4
213 {
214 USHORT segCountX2;
215 USHORT searchRange;
216 USHORT entrySelector;
217 USHORT rangeShift;
218 USHORT *endCount;
219 USHORT reservedPad;
220 USHORT *startCount;
221 USHORT *idDelta;
222 USHORT *idRangeOffset;
223 USHORT *glyphIndexArray;
224 };
225
226 static struct cmap4 *
227 read_cmap4(sfnt *sfont, ULONG len)
228 {
229 struct cmap4 *map;
230 USHORT i, n, segCount;
231
232 if (len < 8) {
233 WARN("invalid format 4 TT cmap subtable");
234 return NULL;
235 }
236
237 map = NEW(1, struct cmap4);
238
239 map->segCountX2 = segCount = sfnt_get_ushort(sfont);
240 map->searchRange = sfnt_get_ushort(sfont);
241 map->entrySelector = sfnt_get_ushort(sfont);
242 map->rangeShift = sfnt_get_ushort(sfont);
243
244 segCount /= 2;
245
246 map->endCount = NEW(segCount, USHORT);
247 for (i = 0; i < segCount; i++)
248 map->endCount[i] = sfnt_get_ushort(sfont);
249
250 map->reservedPad = sfnt_get_ushort(sfont);
251
252 map->startCount = NEW(segCount, USHORT);
253 for (i = 0; i < segCount; i++)
254 map->startCount[i] = sfnt_get_ushort(sfont);
255
256 map->idDelta = NEW(segCount, USHORT);
257 for (i = 0; i < segCount; i++)
258 map->idDelta[i] = sfnt_get_ushort(sfont);
259
260 map->idRangeOffset = NEW(segCount, USHORT);
261 for (i = 0; i < segCount; i++)
262 map->idRangeOffset[i] = sfnt_get_ushort(sfont);
263
264 n = (len - 16 - 8 * segCount) / 2;
265 if (n == 0)
266 map->glyphIndexArray = NULL;
267 else {
268 map->glyphIndexArray = NEW(n, USHORT);
269 for (i = 0; i < n; i++)
270 map->glyphIndexArray[i] = sfnt_get_ushort(sfont);
271 }
272
273 return map;
274 }
275
276 static void
277 release_cmap4 (struct cmap4 *map)
278 {
279 if (map) {
280 if (map->endCount) RELEASE(map->endCount);
281 if (map->startCount) RELEASE(map->startCount);
282 if (map->idDelta) RELEASE(map->idDelta);
283 if (map->idRangeOffset) RELEASE(map->idRangeOffset);
284 if (map->glyphIndexArray) RELEASE(map->glyphIndexArray);
285 RELEASE(map);
286 }
287 }
288
289 static USHORT
290 lookup_cmap4 (struct cmap4 *map, USHORT cc)
291 {
292 USHORT gid = 0;
293 USHORT i, j, segCount;
294
295 /*
296 * Segments are sorted in order of increasing endCode values.
297 * Last segment maps 0xffff to gid 0 (?)
298 */
299 i = segCount = map->segCountX2 / 2;
300 while (i-- > 0 && cc <= map->endCount[i]) {
301 if (cc >= map->startCount[i]) {
302 if (map->idRangeOffset[i] == 0) {
303 gid = (cc + map->idDelta[i]) & 0xffff;
304 } else if (cc == 0xffff && map->idRangeOffset[i] == 0xffff) {
305 /* this is for protection against some old broken fonts... */
306 gid = 0;
307 } else {
308 j = map->idRangeOffset[i] - (segCount - i) * 2;
309 j = (cc - map->startCount[i]) + (j / 2);
310 gid = map->glyphIndexArray[j];
311 if (gid != 0)
312 gid = (gid + map->idDelta[i]) & 0xffff;
313 }
314 break;
315 }
316 }
317
318 return gid;
319 }
320
321 /* format 6: trimmed table mapping */
322 struct cmap6
323 {
324 USHORT firstCode;
325 USHORT entryCount;
326 USHORT *glyphIndexArray;
327 };
328
329 static struct cmap6 *
330 read_cmap6 (sfnt *sfont, ULONG len)
331 {
332 struct cmap6 *map;
333 USHORT i;
334
335 if (len < 4) {
336 WARN("invalid format 6 TT cmap subtable");
337 return NULL;
338 }
339
340 map = NEW(1, struct cmap6);
341 map->firstCode = sfnt_get_ushort(sfont);
342 map->entryCount = sfnt_get_ushort(sfont);
343 map->glyphIndexArray = NEW(map->entryCount, USHORT);
344 for (i = 0; i < map->entryCount; i++)
345 map->glyphIndexArray[i] = sfnt_get_ushort(sfont);
346
347 return map;
348 }
349
350 static void
351 release_cmap6 (struct cmap6 *map)
352 {
353 if (map) {
354 if (map->glyphIndexArray)
355 RELEASE(map->glyphIndexArray);
356 RELEASE(map);
357 }
358 }
359
360 static USHORT
361 lookup_cmap6 (struct cmap6 *map, USHORT cc)
362 {
363 USHORT idx;
364
365 idx = cc - map->firstCode;
366 if (idx < map->entryCount)
367 return map->glyphIndexArray[idx];
368 return 0;
369 }
370
371 /* Format 8 and 10 not supported...
372 *
373 * format 8: mixed 16-bit and 32-bit coverage
374 * format 10: trimmed array
375 */
376
377 /*
378 * format 12: segmented coverage
379 *
380 * startGlyphID is 32-bit long, however, GlyphID is still 16-bit long !
381 */
382
383 struct charGroup
384 {
385 ULONG startCharCode;
386 ULONG endCharCode;
387 ULONG startGlyphID;
388 };
389
390 struct cmap12
391 {
392 ULONG nGroups;
393 struct charGroup *groups;
394 };
395
396 /* ULONG length */
397 static struct cmap12 *
398 read_cmap12 (sfnt *sfont, ULONG len)
399 {
400 struct cmap12 *map;
401 ULONG i;
402
403 if (len < 4) {
404 WARN("invalid format 12 TT cmap subtable");
405 return NULL;
406 }
407
408 map = NEW(1, struct cmap12);
409 map->nGroups = sfnt_get_ulong(sfont);
410 map->groups = NEW(map->nGroups, struct charGroup);
411 for (i = 0; i < map->nGroups; i++) {
412 map->groups[i].startCharCode = sfnt_get_ulong(sfont);
413 map->groups[i].endCharCode = sfnt_get_ulong(sfont);
414 map->groups[i].startGlyphID = sfnt_get_ulong(sfont);
415 }
416
417 return map;
418 }
419
420 static void
421 release_cmap12 (struct cmap12 *map)
422 {
423 if (map) {
424 if (map->groups)
425 RELEASE(map->groups);
426 RELEASE(map);
427 }
428 }
429
430 static USHORT
431 lookup_cmap12 (struct cmap12 *map, ULONG cccc)
432 {
433 USHORT gid = 0;
434 int i;
435
436 i = map->nGroups;
437 while (i-- > 0 &&
438 cccc <= map->groups[i].endCharCode) {
439 if (cccc >= map->groups[i].startCharCode) {
440 gid = (USHORT) ((cccc -
441 map->groups[i].startCharCode +
442 map->groups[i].startGlyphID) & 0xffff);
443 break;
444 }
445 }
446
447 return gid;
448 }
449
450 /* format 14: unicode variation sequences */
451
452 struct variationSelector
453 {
454 /* Variation selector */
455 ULONG varSelector;
456
457 /* Default UVS table */
458 ULONG numUnicodeValueRanges;
459 ULONG *rangesStartUnicodeValue;
460 BYTE *rangesAdditionalCount;
461
462 /* Non-Default UVS table */
463 ULONG numUVSMappings;
464 ULONG *uvsMappingsUnicodeValue;
465 USHORT *uvsMappingsGlyphID;
466 };
467
468 struct cmap14
469 {
470 ULONG numVarSelectorRecords;
471 struct variationSelector *varSelector;
472 };
473
474 /* ULONG length */
475 static struct cmap14 *
476 read_cmap14 (sfnt *sfont, ULONG offset, ULONG len)
477 {
478 struct cmap14 *map;
479 ULONG *defaultUVSOffset, *nonDefaultUVSOffset;
480 ULONG i, j;
481
482 if (len < 4) {
483 WARN("invalid format 14 TT cmap subtable");
484 return NULL;
485 }
486
487 map = NEW(1, struct cmap14);
488 map->numVarSelectorRecords = sfnt_get_ulong(sfont);
489 map->varSelector = NEW(map->numVarSelectorRecords, struct variationSelector);
490 defaultUVSOffset = NEW(map->numVarSelectorRecords, ULONG);
491 nonDefaultUVSOffset = NEW(map->numVarSelectorRecords, ULONG);
492 /* Read VariationSelector Record */
493 for (i = 0; i < map->numVarSelectorRecords; i++) {
494 map->varSelector[i].varSelector = sfnt_get_uint24(sfont);
495 defaultUVSOffset[i] = sfnt_get_ulong(sfont);
496 nonDefaultUVSOffset[i] = sfnt_get_ulong(sfont);
497 }
498 for (i = 0; i < map->numVarSelectorRecords; i++) {
499 /* Read DefaultUVS Table */
500 if (defaultUVSOffset[i] > 0) {
501 sfnt_seek_set(sfont, offset + defaultUVSOffset[i]);
502 map->varSelector[i].numUnicodeValueRanges = sfnt_get_ulong(sfont);
503 map->varSelector[i].rangesStartUnicodeValue = NEW(map->varSelector[i].numUnicodeValueRanges, ULONG);
504 map->varSelector[i].rangesAdditionalCount = NEW(map->varSelector[i].numUnicodeValueRanges, BYTE);
505 for (j = 0; j < map->varSelector[i].numUnicodeValueRanges; j++) {
506 map->varSelector[i].rangesStartUnicodeValue[j] = sfnt_get_uint24(sfont);
507 map->varSelector[i].rangesAdditionalCount[j] = sfnt_get_byte(sfont);
508 }
509 } else {
510 map->varSelector[i].numUnicodeValueRanges = 0;
511 map->varSelector[i].rangesStartUnicodeValue = NULL;
512 map->varSelector[i].rangesAdditionalCount = NULL;
513 }
514 /* Read NonDefaultUVS Table */
515 if (nonDefaultUVSOffset[i] > 0) {
516 sfnt_seek_set(sfont, offset + nonDefaultUVSOffset[i]);
517 map->varSelector[i].numUVSMappings = sfnt_get_ulong(sfont);
518 map->varSelector[i].uvsMappingsUnicodeValue = NEW(map->varSelector[i].numUVSMappings, ULONG);
519 map->varSelector[i].uvsMappingsGlyphID = NEW(map->varSelector[i].numUVSMappings, USHORT);
520 for (j = 0; j < map->varSelector[i].numUVSMappings; j++) {
521 map->varSelector[i].uvsMappingsUnicodeValue[j] = sfnt_get_uint24(sfont);
522 map->varSelector[i].uvsMappingsGlyphID[j] = sfnt_get_ushort(sfont);
523 }
524 } else {
525 map->varSelector[i].numUVSMappings = 0;
526 map->varSelector[i].uvsMappingsUnicodeValue = NULL;
527 map->varSelector[i].uvsMappingsGlyphID = NULL;
528 }
529 }
530
531 RELEASE(defaultUVSOffset);
532 RELEASE(nonDefaultUVSOffset);
533 return map;
534 }
535
536 static void
537 release_cmap14 (struct cmap14 *map)
538 {
539 ULONG i;
540 if (map) {
541 if (map->varSelector) {
542 for (i = 0; i < map->numVarSelectorRecords; i++) {
543 if (map->varSelector[i].rangesStartUnicodeValue)
544 RELEASE(map->varSelector[i].rangesStartUnicodeValue);
545 if (map->varSelector[i].rangesAdditionalCount)
546 RELEASE(map->varSelector[i].rangesAdditionalCount);
547 if (map->varSelector[i].uvsMappingsUnicodeValue)
548 RELEASE(map->varSelector[i].uvsMappingsUnicodeValue);
549 if (map->varSelector[i].uvsMappingsGlyphID)
550 RELEASE(map->varSelector[i].uvsMappingsGlyphID);
551 }
552 RELEASE(map->varSelector);
553 }
554 RELEASE(map);
555 }
556 }
557
558 /* read cmap */
559 tt_cmap *
560 tt_cmap_read (sfnt *sfont, USHORT platform, USHORT encoding)
561 {
562 tt_cmap *cmap = NULL;
563 ULONG offset, length = 0;
564 USHORT p_id, e_id;
565 USHORT i, n_subtabs;
566
567 ASSERT(sfont);
568
569 offset = sfnt_locate_table(sfont, "cmap");
570 (void) sfnt_get_ushort(sfont);
571 n_subtabs = sfnt_get_ushort(sfont);
572
573 for (i = 0; i < n_subtabs; i++) {
574 p_id = sfnt_get_ushort(sfont);
575 e_id = sfnt_get_ushort(sfont);
576 if (p_id != platform || e_id != encoding)
577 sfnt_get_ulong(sfont);
578 else {
579 offset += sfnt_get_ulong(sfont);
580 break;
581 }
582 }
583
584 if (i == n_subtabs)
585 return NULL;
586
587 cmap = NEW(1, tt_cmap);
588 cmap->map = NULL;
589 cmap->platform = platform;
590 cmap->encoding = encoding;
591
592 sfnt_seek_set(sfont, offset);
593 cmap->format = sfnt_get_ushort(sfont);
594 /* Length and version (language) is ULONG for
595 * format 8, 10, 12 !
596 */
597 if (cmap->format <= 6) {
598 length = sfnt_get_ushort(sfont);
599 cmap->language = sfnt_get_ushort(sfont); /* language (Mac) */
600 } else if (cmap->format != 14) {
601 if (sfnt_get_ushort(sfont) != 0) { /* reverved - 0 */
602 WARN("Unrecognized cmap subtable format.");
603 tt_cmap_release(cmap);
604 return NULL;
605 } else {
606 length = sfnt_get_ulong(sfont);
607 cmap->language = sfnt_get_ulong(sfont);
608 }
609 } else {
610 length = sfnt_get_ulong(sfont);
611 cmap->language = 0;
612 }
613
614 switch(cmap->format) {
615 case 0:
616 cmap->map = read_cmap0(sfont, length);
617 break;
618 case 2:
619 cmap->map = read_cmap2(sfont, length);
620 break;
621 case 4:
622 cmap->map = read_cmap4(sfont, length);
623 break;
624 case 6:
625 cmap->map = read_cmap6(sfont, length);
626 break;
627 case 12:
628 /* WARN("UCS-4 TrueType cmap table..."); */
629 cmap->map = read_cmap12(sfont, length);
630 break;
631 case 14:
632 cmap->map = read_cmap14(sfont, offset, length);
633 break;
634 default:
635 WARN("Unrecognized OpenType/TrueType cmap format.");
636 tt_cmap_release(cmap);
637 return NULL;
638 break;
639 }
640
641 if (!cmap->map) {
642 tt_cmap_release(cmap);
643 cmap = NULL;
644 }
645
646 return cmap;
647 }
648
649 void
650 tt_cmap_release (tt_cmap *cmap)
651 {
652
653 if (cmap) {
654 if (cmap->map) {
655 switch (cmap->format) {
656 case 0:
657 release_cmap0(cmap->map);
658 break;
659 case 2:
660 release_cmap2(cmap->map);
661 break;
662 case 4:
663 release_cmap4(cmap->map);
664 break;
665 case 6:
666 release_cmap6(cmap->map);
667 break;
668 case 12:
669 release_cmap12(cmap->map);
670 break;
671 case 14:
672 release_cmap14(cmap->map);
673 break;
674 default:
675 WARN("Unrecognized OpenType/TrueType cmap format: %d", cmap->format);
676 break;
677 }
678 }
679 RELEASE(cmap);
680 }
681
682 return;
683 }
684
685
686 USHORT
687 tt_cmap_lookup (tt_cmap *cmap, ULONG cc)
688 {
689 USHORT gid = 0;
690
691 ASSERT(cmap);
692
693 if (cc > 0xffffL && cmap->format < 12) {
694 WARN("Four bytes charcode not supported in OpenType/TrueType cmap format 0...6.");
695 return 0;
696 }
697
698 switch (cmap->format) {
699 case 0:
700 gid = lookup_cmap0(cmap->map, (USHORT) cc);
701 break;
702 case 2:
703 gid = lookup_cmap2(cmap->map, (USHORT) cc);
704 break;
705 case 4:
706 gid = lookup_cmap4(cmap->map, (USHORT) cc);
707 break;
708 case 6:
709 gid = lookup_cmap6(cmap->map, (USHORT) cc);
710 break;
711 case 12:
712 gid = lookup_cmap12(cmap->map, (ULONG) cc);
713 break;
714 default:
715 WARN("Unrecognized OpenType/TrueType cmap subtable format: %d", cmap->format);
716 break;
717 }
718
719 return gid;
720 }
721
722 static USHORT
723 lookup_cmap14 (struct cmap14 *map, tt_cmap* cmap_default, ULONG unicode, ULONG uvs)
724 {
725 ULONG i, j;
726
727 for (i = 0; i < map->numVarSelectorRecords; i++) {
728 if (map->varSelector[i].varSelector == uvs) {
729 for (j = 0; j < map->varSelector[i].numUnicodeValueRanges; j++) {
730 if (map->varSelector[i].rangesStartUnicodeValue[j] <= unicode &&
731 unicode <= map->varSelector[i].rangesStartUnicodeValue[j] + map->varSelector[i].rangesAdditionalCount[j])
732 return tt_cmap_lookup(cmap_default, unicode);
733 }
734 for (j = 0; j < map->varSelector[i].numUVSMappings; j++) {
735 if (map->varSelector[i].uvsMappingsUnicodeValue[j] == unicode)
736 return map->varSelector[i].uvsMappingsGlyphID[j];
737 }
738 return 0;
739 }
740 }
741
742 return 0;
743 }
744
745 USHORT
746 tt_cmap_uvs_lookup(tt_cmap* cmap_uvs, tt_cmap* cmap_default, ULONG unicode, ULONG uvs)
747 {
748 ASSERT(cmap_uvs);
749
750 if (cmap_uvs->format != 14) {
751 WARN("Unicode Variation Sequences in OpenType/TrueType cmap must be format 14.");
752 return 0;
753 }
754
755 return lookup_cmap14(cmap_uvs->map, cmap_default, unicode, uvs);
756 }
757
758
759
760 static unsigned char srange_min[2] = {0x00, 0x00};
761 static unsigned char srange_max[2] = {0xff, 0xff};
762 static unsigned char lrange_min[4] = {0x00, 0x00, 0x00, 0x00};
763 static unsigned char lrange_max[4] = {0x7f, 0xff, 0xff, 0xff};
764
765 /* OpenType CIDFont:
766 *
767 * We don't use GID for them. OpenType cmap table is for
768 * charcode to GID mapping rather than to-CID mapping.
769 */
770 #include "cid.h"
771
772 #include "tt_table.h"
773 #include "cff_types.h"
774 #include "cff_dict.h"
775 #include "cff.h"
776
777 /* This should be moved to cff.c */
778 static void
779 create_GIDToCIDMap (uint16_t *GIDToCIDMap, uint16_t num_glyphs, cff_font *cffont)
780 {
781 cff_charsets *charset;
782 uint16_t gid, i;
783
784 ASSERT(GIDToCIDMap);
785
786 if (!cffont || !(cffont->flag & FONTTYPE_CIDFONT)) {
787 for (gid = 0; gid < num_glyphs; gid++) {
788 GIDToCIDMap[gid] = gid;
789 }
790
791 return;
792 }
793
794 memset(GIDToCIDMap, 0, num_glyphs*sizeof(uint16_t));
795
796 charset = cffont->charsets;
797 if (!charset)
798 return;
799 switch (charset->format) {
800 case 0:
801 {
802 s_SID *cids; /* CID... */
803
804 cids = charset->data.glyphs;
805 for (gid = 1, i = 0; i < charset->num_entries; i++) {
806 GIDToCIDMap[gid] = cids[i];
807 gid++;
808 }
809 }
810 break;
811 case 1:
812 {
813 cff_range1 *ranges;
814 card16 cid, count;
815
816 ranges = charset->data.range1;
817 for (gid = 1, i = 0; i < charset->num_entries; i++) {
818 cid = ranges[i].first;
819 count = ranges[i].n_left + 1; /* card8 */
820 while (count-- > 0 && gid <= num_glyphs) {
821 GIDToCIDMap[gid] = cid;
822 gid++;
823 cid++;
824 }
825 }
826 }
827 break;
828 case 2:
829 {
830 cff_range2 *ranges;
831 card16 cid, count;
832
833 ranges = charset->data.range2;
834 if (charset->num_entries == 1 && ranges[0].first == 1) {
835 /* "Complete" CIDFont */
836 for (gid = 0; gid < num_glyphs; gid++) {
837 GIDToCIDMap[gid] = gid;
838 }
839 } else {
840 /* Not trivial mapping */
841 for (gid = 1, i = 0; i < charset->num_entries; i++) {
842 cid = ranges[i].first;
843 count = ranges[i].n_left + 1;
844 while (count-- > 0 && gid <= num_glyphs) {
845 GIDToCIDMap[gid] = cid;
846 gid++;
847 cid++;
848 }
849 }
850 }
851 }
852 break;
853 default:
854 WARN("Unknown CFF charset format...: %d", charset->format);
855 break;
856 }
857
858 return;
859 }
860
861 /* Soft-hyphen (U+00AD) to lower its priority... added here for convenience */
862 static int is_PUA_or_presentation (unsigned int uni)
863 {
864 /* Some of CJK Radicals Supplement and Kangxi Radicals
865 * are commonly double encoded, lower the priority.
866 * CJK Compatibility Ideographs & Supplement added.
867 */
868 return ((uni >= 0x2E80 && uni <= 0x2EF3) || (uni >= 0x2F00 && uni <= 0x2FD5) ||
869 (uni >= 0xE000 && uni <= 0xF8FF) || (uni >= 0xFB00 && uni <= 0xFB4F) ||
870 (uni >= 0xF900 && uni <= 0xFAFF) || (uni >= 0x2F800 && uni <= 0x2FA1F) ||
871 (uni >= 0xF0000 && uni <= 0xFFFFD) || (uni >= 0x100000 && uni <= 0x10FFFD) ||
872 (uni == 0x00AD));
873 }
874
875 static char *
876 lookup_glyph_name (struct tt_post_table *post, cff_font *cffont, USHORT gid)
877 {
878 char *name = NULL;
879
880 if (post)
881 name = tt_get_glyphname(post, gid);
882 if (!name && cffont)
883 name = cff_get_glyphname(cffont, gid);
884
885 return name;
886 }
887
888 /*
889 * Substituted glyphs:
890 *
891 * Mapping information stored in cmap_add.
892 */
893 #ifndef is_used_char2
894 #define is_used_char2(b,c) (((b)[(c)/8]) & (1 << (7-((c)%8))))
895 #endif
896
897 static int32_t
898 handle_subst_glyphs (CMap *cmap, CMap *cmap_add, char *used_chars)
899 {
900 int32_t count = 0;
901 int32_t cid;
902
903 for (cid = 0; cid < 65536; cid++) {
904 if (!is_used_char2(used_chars, cid))
905 continue;
906 else {
907 unsigned char buf[256];
908 int inbytesleft = 2, outbytesleft = 254;
909 size_t len;
910 unsigned char *outbuf = buf + 2;
911 const unsigned char *inbuf = buf;
912
913 buf[0] = (cid >> 8) & 0xff;
914 buf[1] = cid & 0xff;
915 CMap_decode(cmap_add, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
916 if (inbytesleft == 0) {
917 len = 254 - outbytesleft;
918 CMap_add_bfchar(cmap, buf, 2, buf + 2, len);
919 used_chars[cid / 8] &= ~(1 << (7 - (cid % 8)));
920 count++;
921 }
922 }
923 }
924
925 return count;
926 }
927
928 static int32_t
929 add_ToUnicode_via_glyph_name (CMap *cmap, char *used_chars, USHORT num_glyphs,
930 uint16_t *GIDToCIDMap,
931 sfnt *sfont, cff_font *cffont)
932 {
933 int32_t count = 0;
934 USHORT gid;
935 struct tt_post_table *post = NULL;
936
937 post = tt_read_post_table(sfont);
938 if (!post && !cffont)
939 return count;
940
941 for (gid = 0; gid < num_glyphs; gid++) {
942 uint16_t cid = GIDToCIDMap[gid];
943 if (is_used_char2(used_chars, cid)) {
944 #define MAX_UNICODES 32
945 char *name;
946 int32_t unicodes[MAX_UNICODES];
947 int unicode_count = -1;
948
949 name = lookup_glyph_name(post, cffont, gid);
950 if (name) {
951 unicode_count = agl_get_unicodes(name, unicodes, MAX_UNICODES);
952 #undef MAX_UNICODES
953 RELEASE(name);
954 if (unicode_count > 0) {
955 unsigned char *buf;
956 unsigned char *p, *endptr;
957 int k;
958 size_t len = 0;
959
960 buf = NEW(unicode_count*4+2, unsigned char);
961 p = buf + 2;
962 endptr = buf + (unicode_count * 4 + 2);
963 for (k = 0; k < unicode_count; ++k) {
964 len += UC_UTF16BE_encode_char(unicodes[k], &p, endptr);
965 }
966 buf[0] = (cid >> 8) & 0xff;
967 buf[1] = cid & 0xff;
968 CMap_add_bfchar(cmap, buf, 2, buf + 2, len);
969 used_chars[cid / 8] &= ~(1 << (7 - (cid % 8)));
970 count++;
971
972 RELEASE(buf);
973 }
974 }
975 }
976 }
977
978 if (post)
979 tt_release_post_table(post);
980
981 return count;
982 }
983
984 static void
985 create_inverse_cmap4 (int32_t *map_base, int32_t *map_sub, USHORT num_glyphs,
986 struct cmap4 *map)
987 {
988 USHORT segCount = map->segCountX2 / 2;
989 USHORT i, j;
990
991 for (i = 0; i < segCount; i++) {
992 USHORT c0 = map->startCount[i];
993 USHORT c1 = map->endCount[i];
994 USHORT d = map->idRangeOffset[i] / 2 - (segCount - i);
995 for (j = 0; j <= c1 - c0; j++) {
996 USHORT ch = c0 + j;
997 USHORT gid;
998
999 if (map->idRangeOffset[i] == 0) {
1000 gid = (ch + map->idDelta[i]) & 0xffff;
1001 } else if (c0 == 0xffff && c1 == 0xffff && map->idRangeOffset[i] == 0xffff) {
1002 /* this is for protection against some old broken fonts... */
1003 gid = 0;
1004 } else {
1005 gid = (map->glyphIndexArray[j + d] + map->idDelta[i]) & 0xffff;
1006 }
1007 if (is_PUA_or_presentation(ch)) {
1008 map_sub[gid] = ch;
1009 } else {
1010 map_base[gid] = ch;
1011 }
1012 }
1013 }
1014 }
1015
1016 static void
1017 create_inverse_cmap12 (int32_t *map_base, int32_t *map_sub, USHORT num_glyphs,
1018 struct cmap12 *map)
1019 {
1020 ULONG i, ch;
1021
1022 for (i = 0; i < map->nGroups; i++) {
1023 for (ch = map->groups[i].startCharCode;
1024 ch <= map->groups[i].endCharCode; ch++) {
1025 int d = ch - map->groups[i].startCharCode;
1026 USHORT gid = (USHORT) ((map->groups[i].startGlyphID + d) & 0xffff);
1027 if (is_PUA_or_presentation(ch)) {
1028 map_sub[gid] = ch;
1029 } else {
1030 map_base[gid] = ch;
1031 }
1032 }
1033 }
1034 }
1035
1036 /* NOTE: Reverse mapping code which had been placed here is removed since:
1037 * - Implementation of reserve CMap mapping itself is imcomplete.
1038 * - It is wrong to assume that all CMap passed here is Unicode to CID mapping.
1039 * Especially, the second one causes problems.
1040 */
1041 static pdf_obj *
1042 create_ToUnicode_cmap (tt_cmap *ttcmap,
1043 const char *cmap_name,
1044 CMap *cmap_add,
1045 const char *used_chars,
1046 sfnt *sfont)
1047 {
1048 pdf_obj *stream = NULL;
1049 int32_t *map_base = NULL, *map_sub = NULL;
1050 USHORT gid, num_glyphs = 0;
1051
1052 ASSERT(ttcmap);
1053
1054 /* Get num_glyphs from maxp talbe */
1055 {
1056 struct tt_maxp_table *maxp;
1057
1058 maxp = tt_read_maxp_table(sfont);
1059 if (maxp) {
1060 num_glyphs = maxp->numGlyphs;
1061 RELEASE(maxp);
1062 }
1063 }
1064
1065 /* Initialize GID to Unicode mapping table */
1066 map_base = NEW(num_glyphs, int32_t);
1067 map_sub = NEW(num_glyphs, int32_t);
1068 for (gid = 0; gid < num_glyphs; gid++) {
1069 map_base[gid] = -1;
1070 map_sub [gid] = -1;
1071 }
1072
1073 /* Create "base" mapping from inverse mapping of OpenType cmap */
1074 switch (ttcmap->format) {
1075 case 4:
1076 create_inverse_cmap4(map_base, map_sub, num_glyphs, ttcmap->map);
1077 break;
1078 case 12:
1079 create_inverse_cmap12(map_base, map_sub, num_glyphs, ttcmap->map);
1080 break;
1081 }
1082
1083 /* Now create ToUnicode CMap stream */
1084 {
1085 CMap *cmap;
1086 int32_t count;
1087 cff_font *cffont = NULL;
1088 char is_cidfont = 0;
1089 uint16_t *GIDToCIDMap = NULL;
1090 char *used_chars_copy = NULL;
1091
1092 if (sfont->type == SFNT_TYPE_POSTSCRIPT) {
1093 ULONG offset;
1094 offset = sfnt_find_table_pos(sfont, "CFF ");
1095 /* "CFF " table must exist here. Just abort... */
1096 if (offset == 0) {
1097 ERROR("\"CFF \" table not found. Must be found before... Can't continue.");
1098 }
1099 cffont = cff_open(sfont->stream, offset, 0);
1100 cff_read_charsets(cffont);
1101 }
1102 is_cidfont = cffont && (cffont->flag & FONTTYPE_CIDFONT);
1103
1104 /* GIT to CID mapping info. */
1105 GIDToCIDMap = NEW(num_glyphs, uint16_t);
1106 if (is_cidfont) {
1107 create_GIDToCIDMap(GIDToCIDMap, num_glyphs, cffont);
1108 } else {
1109 for (gid = 0; gid < num_glyphs; gid++) {
1110 GIDToCIDMap[gid] = gid;
1111 }
1112 }
1113 cmap = CMap_new();
1114 CMap_set_name (cmap, cmap_name);
1115 CMap_set_wmode(cmap, 0);
1116 CMap_set_type (cmap, CMAP_TYPE_TO_UNICODE);
1117 CMap_set_CIDSysInfo(cmap, &CSI_UNICODE);
1118 CMap_add_codespacerange(cmap, srange_min, srange_max, 2);
1119
1120 count = 0;
1121 used_chars_copy = NEW(8192, char);
1122 memcpy(used_chars_copy, used_chars, 8192);
1123 for (gid = 0; gid < num_glyphs; gid++) {
1124 uint16_t cid = GIDToCIDMap[gid];
1125 if (is_used_char2(used_chars_copy, cid)) {
1126 int32_t ch;
1127 unsigned char src[2], dst[4];
1128 unsigned char *p = dst, *endptr = dst + 4;
1129 size_t len;
1130
1131 ch = map_base[gid];
1132 if (UC_is_valid(ch)) {
1133 src[0] = (cid >> 8) & 0xff;
1134 src[1] = cid & 0xff;
1135 len = UC_UTF16BE_encode_char(ch, &p, endptr);
1136 CMap_add_bfchar(cmap, src, 2, dst, len);
1137 used_chars_copy[cid / 8] &= ~(1 << (7 - (cid % 8)));
1138 count++;
1139 }
1140 }
1141 }
1142
1143 /* cmap_add here stores information about all unencoded glyphs which can be
1144 * accessed only through OT Layout GSUB table.
1145 * This is only availabel when encoding is "unicode".
1146 */
1147 if (cmap_add) {
1148 count += handle_subst_glyphs(cmap, cmap_add, used_chars_copy);
1149 } else {
1150 /* Else, try gathering information from GSUB tables */
1151 count += otl_gsub_add_ToUnicode(cmap, used_chars_copy,
1152 map_base, map_sub, num_glyphs,
1153 GIDToCIDMap, sfont);
1154 }
1155 /* Find Unicode mapping via PostScript glyph names... */
1156 count += add_ToUnicode_via_glyph_name(cmap, used_chars_copy, num_glyphs,
1157 GIDToCIDMap, sfont, is_cidfont ? NULL : cffont);
1158 if (cffont)
1159 cff_close(cffont);
1160
1161 /* Finaly, PUA and presentation forms... */
1162 for (gid = 0; gid < num_glyphs; gid++) {
1163 uint16_t cid = GIDToCIDMap[gid];
1164 if (is_used_char2(used_chars_copy, cid)) {
1165 int32_t ch;
1166 unsigned char src[2], dst[4];
1167 unsigned char *p = dst, *endptr = dst + 4;
1168 size_t len;
1169
1170 ch = map_sub[gid];
1171 if (UC_is_valid(ch)) {
1172 src[0] = (cid >> 8) & 0xff;
1173 src[1] = cid & 0xff;
1174 len = UC_UTF16BE_encode_char(ch, &p, endptr);
1175 CMap_add_bfchar(cmap, src, 2, dst, len);
1176 used_chars_copy[cid / 8] &= ~(1 << (7 - (cid % 8)));
1177 count++;
1178 }
1179 }
1180 }
1181
1182 /* Check for missing mapping */
1183 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) {
1184 for (gid = 0; gid < num_glyphs; gid++) {
1185 uint16_t cid = GIDToCIDMap[gid];
1186 if (is_used_char2(used_chars_copy, cid)) {
1187 WARN("Unable to find ToUnicode mapping for glyph CID=%u (GID=%u)", cid, gid);
1188 }
1189 }
1190 }
1191 RELEASE(GIDToCIDMap);
1192 RELEASE(used_chars_copy);
1193
1194 if (count < 1)
1195 stream = NULL;
1196 else {
1197 stream = CMap_create_stream(cmap);
1198 }
1199 CMap_release(cmap);
1200 }
1201 RELEASE(map_base);
1202 RELEASE(map_sub);
1203
1204 return stream;
1205 }
1206
1207 typedef struct {
1208 short platform;
1209 short encoding;
1210 } cmap_plat_enc_rec;
1211
1212 static cmap_plat_enc_rec cmap_plat_encs[] = {
1213 { 3, 10 },
1214 { 0, 3 },
1215 { 0, 4 },
1216 { 0, 0 },
1217 { 3, 1 },
1218 { 0, 1 }
1219 };
1220
1221 pdf_obj *
1222 otf_create_ToUnicode_stream (const char *font_name,
1223 uint32_t ttc_index, /* 0 for non-TTC */
1224 const char *basefont,
1225 const char *used_chars)
1226 {
1227 pdf_obj *cmap_ref = NULL; /* returned value */
1228 CMap *cmap_add = NULL;
1229 char *cmap_name;
1230 FILE *fp = NULL;
1231 sfnt *sfont;
1232 ULONG offset = 0;
1233 tt_cmap *ttcmap;
1234 int cmap_id, cmap_add_id;
1235 int i;
1236
1237 cmap_name = NEW(strlen(basefont)+strlen("-UTF16")+1, char);
1238 sprintf(cmap_name, "%s-UTF16", basefont);
1239
1240 cmap_id = pdf_findresource("CMap", cmap_name);
1241 if (cmap_id >= 0) {
1242 RELEASE(cmap_name);
1243 cmap_ref = pdf_get_resource_reference(cmap_id);
1244 return cmap_ref;
1245 }
1246
1247 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) {
1248 MESG("\n");
1249 MESG("otf_cmap>> Creating ToUnicode CMap for \"%s\"...\n", font_name);
1250 }
1251
1252
1253 if ((fp = DPXFOPEN(font_name, DPX_RES_TYPE_TTFONT)) ||
1254 (fp = DPXFOPEN(font_name, DPX_RES_TYPE_OTFONT))) {
1255 sfont = sfnt_open(fp);
1256 } else if ((fp = DPXFOPEN(font_name, DPX_RES_TYPE_DFONT))) {
1257 sfont = dfont_open(fp, ttc_index);
1258 } else {
1259 RELEASE(cmap_name);
1260 return NULL;
1261 }
1262
1263 if (!sfont) {
1264 WARN("Could not open OpenType/TrueType font file \"%s\"", font_name);
1265 RELEASE(cmap_name);
1266 DPXFCLOSE(fp);
1267 return NULL;
1268 }
1269
1270 switch (sfont->type) {
1271 case SFNT_TYPE_DFONT:
1272 offset = sfont->offset;
1273 break;
1274 case SFNT_TYPE_TTC:
1275 offset = ttc_read_offset(sfont, ttc_index);
1276 if (offset == 0) {
1277 WARN("Invalid TTC index for font: %s", font_name);
1278 sfnt_close(sfont);
1279 DPXFCLOSE(fp);
1280 RELEASE(cmap_name);
1281 return NULL;
1282 }
1283 break;
1284 default:
1285 offset = 0;
1286 break;
1287 }
1288
1289 if (sfnt_read_table_directory(sfont, offset) < 0) {
1290 WARN("Could not read OpenType/TrueType table directory: %s", font_name);
1291 sfnt_close(sfont);
1292 DPXFCLOSE(fp);
1293 RELEASE(cmap_name);
1294 return NULL;
1295 }
1296
1297 /* cmap_add is used for storing information on ToUnicode mapping for
1298 * unencoded glyphs which can be reached only through GSUB substitution.
1299 * This is available only when "unicode" is specified in the encoding
1300 * field of fontmap. We remember the inverse mapping via cmap_add in this
1301 * case.
1302 */
1303 {
1304 char *cmap_add_name;
1305 size_t len;
1306
1307 len = strlen(font_name)+strlen("-UCS32-Add")+32;
1308 cmap_add_name = NEW(len, char);
1309 snprintf(cmap_add_name, len, "%s:%d-UCS32-Add", font_name, ttc_index);
1310 cmap_add_name[len-1] = '\0';
1311 cmap_add_id = CMap_cache_find(cmap_add_name);
1312 RELEASE(cmap_add_name);
1313 if (cmap_add_id < 0) {
1314 cmap_add = NULL;
1315 } else {
1316 cmap_add = CMap_cache_get(cmap_add_id);
1317 }
1318 }
1319
1320 ttcmap = NULL;
1321 for (i = 0; i < sizeof(cmap_plat_encs) / sizeof(cmap_plat_enc_rec); ++i) {
1322 ttcmap = tt_cmap_read(sfont, cmap_plat_encs[i].platform, cmap_plat_encs[i].encoding);
1323 if (!ttcmap)
1324 continue;
1325
1326 if (ttcmap->format == 4 || ttcmap->format == 12) {
1327 break;
1328 } else {
1329 tt_cmap_release(ttcmap);
1330 ttcmap = NULL;
1331 }
1332 }
1333 if (ttcmap) {
1334 pdf_obj *cmap_obj;
1335
1336 CMap_set_silent(1); /* many warnings without this... */
1337 cmap_obj = create_ToUnicode_cmap(ttcmap, cmap_name, cmap_add, used_chars, sfont);
1338 CMap_set_silent(0);
1339 if (cmap_obj) {
1340 cmap_id = pdf_defineresource("CMap", cmap_name,
1341 cmap_obj, PDF_RES_FLUSH_IMMEDIATE);
1342 cmap_ref = pdf_get_resource_reference(cmap_id);
1343 }
1344 tt_cmap_release(ttcmap);
1345 }
1346
1347 /* Cleanup */
1348 RELEASE(cmap_name);
1349 sfnt_close(sfont);
1350 DPXFCLOSE(fp);
1351
1352 #ifndef LIBDPX
1353 if (!cmap_ref) {
1354 WARN("Creating ToUnicode CMap failed for \"%s\"", font_name);
1355 }
1356 #endif
1357
1358 return cmap_ref;
1359 }
1360
1361
1362 /* Creating input CMaps from OT cmap table */
1363
1364 static void
1365 load_cmap4 (struct cmap4 *map, uint16_t *GIDToCIDMap, USHORT num_glyphs,
1366 otl_gsub *gsub_vert, otl_gsub *gsub_list,
1367 CMap *cmap, int32_t *map_base, int32_t *map_sub)
1368 {
1369 USHORT c0, c1, gid, cid;
1370 USHORT j, d, segCount;
1371 USHORT ch;
1372 int i;
1373 unsigned char buf[4];
1374
1375 segCount = map->segCountX2 / 2;
1376 for (i = segCount - 1; i >= 0 ; i--) {
1377 c0 = map->startCount[i];
1378 c1 = map->endCount[i];
1379 d = map->idRangeOffset[i] / 2 - (segCount - i);
1380 for (j = 0; j <= c1 - c0; j++) {
1381 ch = c0 + j;
1382 if (map->idRangeOffset[i] == 0) {
1383 gid = (ch + map->idDelta[i]) & 0xffff;
1384 } else if (c0 == 0xffff && c1 == 0xffff && map->idRangeOffset[i] == 0xffff) {
1385 /* this is for protection against some old broken fonts... */
1386 gid = 0;
1387 } else {
1388 gid = (map->glyphIndexArray[j+d] + map->idDelta[i]) & 0xffff;
1389 }
1390 if (gid != 0 && gid != 0xffff) {
1391 /* Apply GSUB features */
1392 if (gsub_list)
1393 otl_gsub_apply_chain(gsub_list, &gid);
1394 if (gsub_vert)
1395 otl_gsub_apply(gsub_vert, &gid);
1396 cid = (gid < num_glyphs) ? GIDToCIDMap[gid] : 0;
1397 buf[0] = 0;
1398 buf[1] = 0;
1399 buf[2] = (ch >> 8) & 0xff;
1400 buf[3] = ch & 0xff;
1401 CMap_add_cidchar(cmap, buf, 4, cid);
1402 /* For ToUnicode creation */
1403 if (map_base && map_sub) {
1404 if (is_PUA_or_presentation(ch)) {
1405 map_sub[gid] = ch;
1406 } else {
1407 map_base[gid] = ch;
1408 }
1409 }
1410 }
1411 }
1412 }
1413
1414 return;
1415 }
1416
1417 static void
1418 load_cmap12 (struct cmap12 *map, uint16_t *GIDToCIDMap, USHORT num_glyphs,
1419 otl_gsub *gsub_vert, otl_gsub *gsub_list,
1420 CMap *cmap, int32_t *map_base, int32_t *map_sub)
1421 {
1422 ULONG i, ch;
1423 USHORT gid, cid;
1424 unsigned char buf[4];
1425
1426 for (i = 0; i < map->nGroups; i++) {
1427 for (ch = map->groups[i].startCharCode;
1428 ch <= map->groups[i].endCharCode; ch++) {
1429 int d = ch - map->groups[i].startCharCode;
1430 gid = (USHORT) ((map->groups[i].startGlyphID + d) & 0xffff);
1431 if (gsub_list)
1432 otl_gsub_apply_chain(gsub_list, &gid);
1433 if (gsub_vert)
1434 otl_gsub_apply(gsub_vert, &gid);
1435 cid = (gid < num_glyphs) ? GIDToCIDMap[gid] : 0;
1436 buf[0] = (ch >> 24) & 0xff;
1437 buf[1] = (ch >> 16) & 0xff;
1438 buf[2] = (ch >> 8) & 0xff;
1439 buf[3] = ch & 0xff;
1440 CMap_add_cidchar(cmap, buf, 4, cid);
1441 if (map_base && map_sub) {
1442 if (is_PUA_or_presentation(ch)) {
1443 map_sub[gid] = ch;
1444 } else {
1445 map_base[gid] = ch;
1446 }
1447 }
1448 }
1449 }
1450
1451 return;
1452 }
1453
1454 int
1455 otf_load_Unicode_CMap (const char *map_name, uint32_t ttc_index, /* 0 for non-TTC font */
1456 const char *otl_tags, int wmode)
1457 {
1458 int cmap_id = -1;
1459 char *cmap_name = NULL;
1460 sfnt *sfont = NULL;
1461 ULONG offset = 0;
1462 uint16_t num_glyphs = 0;
1463 FILE *fp = NULL;
1464 tt_cmap *ttcmap = NULL;
1465 CIDSysInfo csi = {NULL, NULL, 0};
1466 uint16_t *GIDToCIDMap = NULL;
1467
1468 if (!map_name)
1469 return -1;
1470
1471 /* First look for cache if it was already loaded */
1472 if (otl_tags) {
1473 size_t len;
1474 len = strlen(map_name)+strlen("-UCS4-H")+strlen(otl_tags)+32;
1475 cmap_name = NEW(len, char);
1476 if (wmode)
1477 snprintf(cmap_name, len, "%s:%d:%s-UCS4-V", map_name, ttc_index, otl_tags);
1478 else
1479 snprintf(cmap_name, len, "%s:%d:%s-UCS4-H", map_name, ttc_index, otl_tags);
1480 cmap_name[len-1] = '\0';
1481 } else {
1482 size_t len;
1483 len = strlen(map_name)+strlen("-UCS4-H")+32;
1484 cmap_name = NEW(len, char);
1485 if (wmode)
1486 snprintf(cmap_name, len, "%s:%d-UCS4-V", map_name, ttc_index);
1487 else {
1488 snprintf(cmap_name, len, "%s:%d-UCS4-H", map_name, ttc_index);
1489 }
1490 cmap_name[len-1] = '\0';
1491 }
1492 cmap_id = CMap_cache_find(cmap_name);
1493 if (cmap_id >= 0) {
1494 RELEASE(cmap_name);
1495 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN)
1496 MESG("otf_cmap>> Found at cmap_id=%d.\n", cmap_id);
1497
1498 return cmap_id;
1499 }
1500
1501 /* CMap not found */
1502 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) {
1503 MESG("\n");
1504 MESG("otf_cmap>> Creating Unicode charmap for font=\"%s\" layout=\"%s\"\n",
1505 map_name, (otl_tags ? otl_tags : "none"));
1506 }
1507
1508 fp = DPXFOPEN(map_name, DPX_RES_TYPE_TTFONT);
1509 if (!fp) {
1510 fp = DPXFOPEN(map_name, DPX_RES_TYPE_OTFONT);
1511 }
1512 if (!fp) {
1513 fp = DPXFOPEN(map_name, DPX_RES_TYPE_DFONT);
1514 if (!fp) {
1515 RELEASE(cmap_name);
1516 return -1;
1517 }
1518 sfont = dfont_open(fp, ttc_index);
1519 } else {
1520 sfont = sfnt_open(fp);
1521 }
1522
1523 if (!sfont) {
1524 WARN("Could not open OpenType/TrueType/dfont font file \"%s\"", map_name);
1525 RELEASE(cmap_name);
1526 DPXFCLOSE(fp);
1527 return -1;
1528 }
1529 switch (sfont->type) {
1530 case SFNT_TYPE_TTC:
1531 offset = ttc_read_offset(sfont, ttc_index);
1532 if (offset == 0) {
1533 WARN("Offset=0 returned for font=%s, TTC_index=%d", map_name, ttc_index);
1534 RELEASE(cmap_name);
1535 sfnt_close(sfont);
1536 DPXFCLOSE(fp);
1537 return -1;
1538 }
1539 break;
1540 case SFNT_TYPE_TRUETYPE:
1541 case SFNT_TYPE_POSTSCRIPT:
1542 offset = 0;
1543 break;
1544 case SFNT_TYPE_DFONT:
1545 offset = sfont->offset;
1546 break;
1547 default:
1548 WARN("Not a OpenType/TrueType/TTC font?: %s", map_name);
1549 RELEASE(cmap_name);
1550 sfnt_close(sfont);
1551 DPXFCLOSE(fp);
1552 return -1;
1553 break;
1554 }
1555
1556 if (sfnt_read_table_directory(sfont, offset) < 0) {
1557 WARN("Could not read OpenType/TrueType table directory: %s", map_name);
1558 RELEASE(cmap_name);
1559 sfnt_close(sfont);
1560 DPXFCLOSE(fp);
1561 return -1;
1562 }
1563
1564 {
1565 struct tt_maxp_table *maxp;
1566
1567 maxp = tt_read_maxp_table(sfont);
1568 num_glyphs = (card16) maxp->numGlyphs;
1569 RELEASE(maxp);
1570 }
1571
1572 GIDToCIDMap = NEW(num_glyphs, uint16_t);
1573 memset(GIDToCIDMap, 0, num_glyphs*sizeof(uint16_t));
1574 if (sfont->type == SFNT_TYPE_POSTSCRIPT) {
1575 cff_font *cffont;
1576 card16 gid;
1577
1578 offset = sfnt_find_table_pos(sfont, "CFF ");
1579 /* Possibly "CFF2" table for variable font: not supported. */
1580 if (offset == 0) {
1581 WARN("PS OpenType but no \"CFF \" table.. Maybe variable font? (not supported)");
1582 RELEASE(cmap_name);
1583 RELEASE(GIDToCIDMap);
1584 sfnt_close(sfont);
1585 DPXFCLOSE(fp);
1586 return -1;
1587 }
1588 cffont = cff_open(sfont->stream, offset, 0);
1589 if (!cffont) {
1590 RELEASE(cmap_name);
1591 RELEASE(GIDToCIDMap);
1592 sfnt_close(sfont);
1593 DPXFCLOSE(fp);
1594 return -1;
1595 }
1596 if (!(cffont->flag & FONTTYPE_CIDFONT)) {
1597 csi.registry = strdup("Adobe");
1598 csi.ordering = strdup("Identity");
1599 csi.supplement = 0;
1600 for (gid = 0; gid < num_glyphs; gid++) {
1601 GIDToCIDMap[gid] = gid;
1602 }
1603 } else {
1604 if (!cff_dict_known(cffont->topdict, "ROS")) {
1605 csi.registry = strdup("Adobe");
1606 csi.ordering = strdup("Identity");
1607 csi.supplement = 0;
1608 } else {
1609 card16 reg, ord;
1610
1611 reg = (card16) cff_dict_get(cffont->topdict, "ROS", 0);
1612 ord = (card16) cff_dict_get(cffont->topdict, "ROS", 1);
1613 csi.registry = cff_get_string(cffont, reg);
1614 csi.ordering = cff_get_string(cffont, ord);
1615 csi.supplement = (int) cff_dict_get(cffont->topdict, "ROS", 2);
1616 }
1617 cff_read_charsets(cffont);
1618 create_GIDToCIDMap(GIDToCIDMap, num_glyphs, cffont);
1619 }
1620 cff_close(cffont);
1621 } else {
1622 uint16_t gid;
1623
1624 csi.registry = strdup("Adobe");
1625 csi.ordering = strdup("Identity");
1626 csi.supplement = 0;
1627 for (gid = 0; gid < num_glyphs; gid++) {
1628 GIDToCIDMap[gid] = gid;
1629 }
1630 }
1631
1632 ttcmap = tt_cmap_read(sfont, 3, 10); /* Microsoft UCS4 */
1633 if (!ttcmap) {
1634 ttcmap = tt_cmap_read(sfont, 3, 1); /* Microsoft UCS2 */
1635 if (!ttcmap) {
1636 ttcmap = tt_cmap_read(sfont, 0, 3); /* Unicode 2.0 or later */
1637 if (!ttcmap) {
1638 ttcmap = tt_cmap_read(sfont, 0, 4);
1639 }
1640 }
1641 }
1642
1643 if (ttcmap) {
1644 CMap *cmap = NULL;
1645 int32_t *map_base, *map_sub;
1646 otl_gsub *gsub_vert = NULL;
1647 otl_gsub *gsub_list = NULL;
1648 uint32_t gid;
1649
1650 if (wmode == 1) {
1651 gsub_vert = otl_gsub_new();
1652 if (otl_gsub_add_feat(gsub_vert, "*", "*", "vrt2", sfont) < 0) {
1653 if (otl_gsub_add_feat(gsub_vert, "*", "*", "vert", sfont) < 0) {
1654 WARN("GSUB feature vrt2/vert not found.");
1655 otl_gsub_release(gsub_vert);
1656 gsub_vert = NULL;
1657 } else {
1658 otl_gsub_select(gsub_vert, "*", "*", "vert");
1659 }
1660 } else {
1661 otl_gsub_select(gsub_vert, "*", "*", "vrt2");
1662 }
1663 } else {
1664 gsub_vert = NULL;
1665 }
1666 if (otl_tags) {
1667 gsub_list = otl_gsub_new();
1668 if (otl_gsub_add_feat_list(gsub_list, otl_tags, sfont) < 0) {
1669 WARN("Reading GSUB feature table(s) failed for \"%s\"", otl_tags);
1670 } else {
1671 otl_gsub_set_chain(gsub_list, otl_tags);
1672 }
1673 } else {
1674 gsub_list = NULL;
1675 }
1676 cmap = CMap_new();
1677 CMap_set_name(cmap, cmap_name);
1678 CMap_set_type(cmap, CMAP_TYPE_CODE_TO_CID);
1679 CMap_set_wmode(cmap, wmode);
1680 CMap_add_codespacerange(cmap, lrange_min, lrange_max, 4);
1681 CMap_set_CIDSysInfo(cmap, &csi);
1682 map_base = NEW(num_glyphs, int32_t);
1683 map_sub = NEW(num_glyphs, int32_t);
1684 for (gid = 0; gid < num_glyphs; gid++) {
1685 map_base[gid] = -1;
1686 map_sub[gid] = -1;
1687 }
1688 switch (ttcmap->format) {
1689 case 12:
1690 load_cmap12(ttcmap->map, GIDToCIDMap, num_glyphs,
1691 gsub_vert, gsub_list,
1692 cmap, map_base, map_sub);
1693 break;
1694 case 4:
1695 load_cmap4(ttcmap->map, GIDToCIDMap, num_glyphs,
1696 gsub_vert, gsub_list,
1697 cmap, map_base, map_sub);
1698 break;
1699 }
1700 if (gsub_vert)
1701 otl_gsub_release(gsub_vert);
1702 if (gsub_list)
1703 otl_gsub_release(gsub_list);
1704 tt_cmap_release(ttcmap);
1705
1706 if (otl_tags) {
1707 CMap *tounicode = NULL;
1708 char *tounicode_name;
1709 int tounicode_id;
1710 size_t name_len;
1711
1712 name_len = strlen(map_name)+strlen("-UCS32-Add")+32;
1713 tounicode_name = NEW(name_len, char);
1714 snprintf(tounicode_name, name_len, "%s:%d-UCS32-Add", map_name, ttc_index);
1715 tounicode_name[name_len-1] = '\0';
1716 tounicode_id = CMap_cache_find(tounicode_name);
1717 if (tounicode_id >= 0)
1718 tounicode = CMap_cache_get(tounicode_id);
1719 else {
1720 tounicode = CMap_new();
1721 CMap_set_name (tounicode, tounicode_name);
1722 CMap_set_type (tounicode, CMAP_TYPE_TO_UNICODE);
1723 CMap_set_wmode(tounicode, 0);
1724 CMap_add_codespacerange(tounicode, srange_min, srange_max, 2);
1725 CMap_set_CIDSysInfo(tounicode, &CSI_UNICODE);
1726 CMap_add_bfchar(tounicode, srange_min, 2, srange_max, 2);
1727 tounicode_id = CMap_cache_add(tounicode);
1728 }
1729 RELEASE(tounicode_name);
1730
1731 for (gid = 0; gid < num_glyphs; gid++) {
1732 uint16_t cid = GIDToCIDMap[gid];
1733 unsigned char src[2], dst[4];
1734 if (cid > 0) {
1735 int32_t ch = UC_is_valid(map_base[gid]) ? map_base[gid] : map_sub[gid];
1736 if (UC_is_valid(ch)) {
1737 unsigned char *p = dst;
1738 unsigned char *endptr = dst + 4;
1739 size_t len;
1740 src[0] = (cid >> 8) & 0xff;
1741 src[1] = cid & 0xff;
1742 len = UC_UTF16BE_encode_char(ch, &p, endptr);
1743 if (len > 0) {
1744 CMap_add_bfchar(tounicode, src, 2, dst, len);
1745 }
1746 }
1747 }
1748 }
1749 }
1750 cmap_id = CMap_cache_add(cmap);
1751 }
1752
1753 RELEASE(cmap_name);
1754 RELEASE(GIDToCIDMap);
1755 if (csi.registry)
1756 RELEASE(csi.registry);
1757 if (csi.ordering)
1758 RELEASE(csi.ordering);
1759 sfnt_close(sfont);
1760 DPXFCLOSE(fp);
1761
1762 return cmap_id;
1763 }
1764
1765 int
1766 otf_try_load_GID_to_CID_map (const char *map_name, uint32_t ttc_index, int wmode)
1767 {
1768 int cmap_id = -1;
1769 sfnt *sfont = NULL;
1770 ULONG offset = 0;
1771 char *cmap_name = NULL;
1772 FILE *fp = NULL;
1773 int len;
1774
1775 if (!map_name)
1776 return -1;
1777
1778 /* Check if already loaded */
1779 len = strlen(map_name) + 32;
1780 cmap_name = NEW(len, char);
1781 snprintf(cmap_name, len, "%s:%d-%1d-GID", map_name, ttc_index, wmode);
1782 cmap_name[len-1] = '\0';
1783 cmap_id = CMap_cache_find(cmap_name);
1784 if (cmap_id >= 0) {
1785 RELEASE(cmap_name);
1786 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN)
1787 MESG("otf_cmap>> GID-to-CID mapping found at cmap_id=%d.\n", cmap_id);
1788
1789 return cmap_id;
1790 }
1791
1792 fp = DPXFOPEN(map_name, DPX_RES_TYPE_TTFONT);
1793 if (!fp) {
1794 fp = DPXFOPEN(map_name, DPX_RES_TYPE_OTFONT);
1795 }
1796 if (!fp) {
1797 fp = DPXFOPEN(map_name, DPX_RES_TYPE_DFONT);
1798 if (!fp) {
1799 RELEASE(cmap_name);
1800 return -1;
1801 }
1802 sfont = dfont_open(fp, ttc_index);
1803 } else {
1804 sfont = sfnt_open(fp);
1805 }
1806
1807 if (!sfont) {
1808 WARN("Could not open OpenType/TrueType/dfont font file \"%s\"", map_name);
1809 RELEASE(cmap_name);
1810 DPXFCLOSE(fp);
1811 return -1;
1812 }
1813 switch (sfont->type) {
1814 case SFNT_TYPE_TTC:
1815 offset = ttc_read_offset(sfont, ttc_index);
1816 if (offset == 0) {
1817 WARN("Invalid TTC index for font \"%s\": %d", map_name, ttc_index);
1818 sfnt_close(sfont);
1819 DPXFCLOSE(fp);
1820 RELEASE(cmap_name);
1821 return -1;
1822 }
1823 break;
1824 case SFNT_TYPE_TRUETYPE:
1825 case SFNT_TYPE_POSTSCRIPT:
1826 offset = 0;
1827 break;
1828 case SFNT_TYPE_DFONT:
1829 offset = sfont->offset;
1830 break;
1831 default:
1832 WARN("Not a OpenType/TrueType/TTC font?: %s", map_name);
1833 sfnt_close(sfont);
1834 DPXFCLOSE(fp);
1835 RELEASE(cmap_name);
1836 return -1;
1837 }
1838
1839 if (sfnt_read_table_directory(sfont, offset) < 0) {
1840 WARN("Could not read OpenType/TrueType table directory: %s", map_name);
1841 sfnt_close(sfont);
1842 DPXFCLOSE(fp);
1843 RELEASE(cmap_name);
1844 return -1;
1845 }
1846 if (sfont->type != SFNT_TYPE_POSTSCRIPT) {
1847 RELEASE(cmap_name);
1848 sfnt_close(sfont);
1849 DPXFCLOSE(fp);
1850 return -1;
1851 }
1852
1853 /* Read GID-to-CID mapping if CFF OpenType is found. */
1854 if (sfont->type == SFNT_TYPE_POSTSCRIPT) {
1855 cff_font *cffont;
1856 struct tt_maxp_table *maxp;
1857 const unsigned char csrange[4] = {0x00, 0x00, 0xff, 0xff};
1858 uint16_t num_glyphs = 0;
1859
1860 maxp = tt_read_maxp_table(sfont);
1861 num_glyphs = (card16) maxp->numGlyphs;
1862 RELEASE(maxp);
1863
1864 offset = sfnt_find_table_pos(sfont, "CFF ");
1865 if (offset == 0) {
1866 WARN("PS OpenType but no \"CFF \" table.. Maybe variable font? (not supported)");
1867 RELEASE(cmap_name);
1868 sfnt_close(sfont);
1869 DPXFCLOSE(fp);
1870 return -1;
1871 }
1872 cffont = cff_open(sfont->stream, offset, 0);
1873 if (cffont && cffont->flag & FONTTYPE_CIDFONT) {
1874 CMap *cmap;
1875 uint16_t gid;
1876 uint16_t *GIDToCIDMap = NULL;
1877 CIDSysInfo csi = {NULL, NULL, 0};
1878
1879 if (!cff_dict_known(cffont->topdict, "ROS")) {
1880 csi.registry = strdup("Adobe");
1881 csi.ordering = strdup("Identity");
1882 csi.supplement = 0;
1883 } else {
1884 card16 reg, ord;
1885
1886 reg = (card16) cff_dict_get(cffont->topdict, "ROS", 0);
1887 ord = (card16) cff_dict_get(cffont->topdict, "ROS", 1);
1888 csi.registry = cff_get_string(cffont, reg);
1889 csi.ordering = cff_get_string(cffont, ord);
1890 csi.supplement = (int) cff_dict_get(cffont->topdict, "ROS", 2);
1891 }
1892 cff_read_charsets(cffont);
1893 GIDToCIDMap = NEW(num_glyphs, uint16_t);
1894 memset(GIDToCIDMap, 0, num_glyphs*sizeof(uint16_t));
1895 create_GIDToCIDMap(GIDToCIDMap, num_glyphs, cffont);
1896 cmap = CMap_new();
1897 CMap_set_name (cmap, cmap_name);
1898 CMap_set_type (cmap, CMAP_TYPE_CODE_TO_CID);
1899 CMap_set_wmode(cmap, wmode);
1900 CMap_add_codespacerange(cmap, &csrange[0], &csrange[2], 2);
1901 CMap_set_CIDSysInfo(cmap, &csi);
1902 for (gid = 0; gid < num_glyphs; gid++) {
1903 unsigned char src[2], dst[2];
1904 src[0] = (gid >> 8) & 0xff;
1905 src[1] = gid & 0xff;
1906 dst[0] = (GIDToCIDMap[gid] >> 8) & 0xff;
1907 dst[1] = GIDToCIDMap[gid] & 0xff;
1908 CMap_add_bfchar(cmap, src, 2, dst, 2);
1909 }
1910 cmap_id = CMap_cache_add(cmap);
1911 if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) {
1912 MESG("\n");
1913 MESG("otf_cmap>> Creating GID-to-CID mapping for font=\"%s\"\n", map_name);
1914 }
1915 RELEASE(GIDToCIDMap);
1916 if (csi.registry)
1917 RELEASE(csi.registry);
1918 if (csi.ordering)
1919 RELEASE(csi.ordering);
1920 }
1921 if (cffont)
1922 cff_close(cffont);
1923 }
1924
1925 RELEASE(cmap_name);
1926 sfnt_close(sfont);
1927 DPXFCLOSE(fp);
1928
1929 return cmap_id;
1930 }