Debian Codegraph @

texk/dvipdfm-x/cid.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 * CID-keyed font support:
23 *
24 * See also, cidtype0, and cidtype2
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <string.h>
32
33 #include "system.h"
34 #include "mem.h"
35 #include "error.h"
36 #include "dpxconf.h"
37 #include "dpxutil.h"
38
39 #include "pdfobj.h"
40
41 #include "cidtype0.h"
42 #include "cidtype2.h"
43 #include "cid.h"
44
45 #define PDF_CID_SUPPORT_MIN 2
46 #define PDF_CID_SUPPORT_MAX 6
47
48 /*
49 * Unicode and PDF Standard Character Collections.
50 *
51 * Adobe-Identity is only for TrueType fonts and it means font's
52 * internal glyph ordering.
53 */
54 static struct {
55 const char *registry;
56 const char *ordering;
57 /* Heighest Supplement values supported by PDF-1.0, 1.1, ...; see
58 * also http://partners.adobe.com/public/developer/font/index.html#ckf
59 */
60 int supplement[21];
61 } CIDFont_stdcc_def[] = {
62 {"Adobe", "UCS", {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0}},
64 {"Adobe", "GB1", {-1, -1, 0, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
65 4, 4, 4, 4, 4}},
66 {"Adobe", "CNS1", {-1, -1, 0, 0, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
67 4, 4, 4, 4, 4}},
68 {"Adobe", "Japan1", {-1, -1, 2, 2, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
69 6, 6, 6, 6, 6}},
70 {"Adobe", "Korea1", {-1, -1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 2, 2, 2, 2, 2}},
72 {"Adobe", "Identity", {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0}},
74 {NULL, NULL, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0}}
76 };
77 #define SUP_IDX_MAX 20
78 #define UCS_CC 0
79 #define ACC_START 1
80 #define ACC_END 4
81
82 static char registry_Adobe[] = "Adobe";
83 static char ordering_Identity[] = "Identity";
84 static char ordering_UCS[] = "UCS";
85
86 CIDSysInfo CSI_IDENTITY = {
87 registry_Adobe,
88 ordering_Identity,
89 0
90 };
91
92 CIDSysInfo CSI_UNICODE = {
93 registry_Adobe,
94 ordering_UCS,
95 0
96 };
97
98 /*
99 * Optional supplement after alias name.
100 */
101
102 static struct {
103 const char *name;
104 int index;
105 } CIDFont_stdcc_alias[] = {
106 {"AU", 0}, {"AG1", 1}, {"AC1", 2}, {"AJ1", 3}, {"AK1", 4}, {"AI", 5},
107 {"UCS", 0}, {"GB1", 1}, {"CNS1", 2}, {"Japan1", 3}, {"Korea1", 4}, {"Identity", 5},
108 {"U", 0}, {"G", 1}, {"C", 2}, {"J", 3}, {"K", 4}, {"I", 5},
109 {NULL, 0}
110 };
111
112 static void release_opt (cid_opt *opt);
113 static int get_cidsysinfo (CIDSysInfo *csi, const char *map_name, const fontmap_opt *fmap_opt);
114
115 int opt_flags_cidfont = 0;
116
117 int
118 CIDFont_is_ACCFont (pdf_font *font)
119 {
120 int i;
121 CIDSysInfo *csi;
122
123 ASSERT(font);
124
125 csi = &font->cid.csi;
126 for (i = ACC_START; i <= ACC_END ; i++) {
127 if (!strcmp(csi->registry, CIDFont_stdcc_def[i].registry) &&
128 !strcmp(csi->ordering, CIDFont_stdcc_def[i].ordering))
129 return 1;
130 }
131
132 return 0;
133 }
134
135 int
136 CIDFont_is_UCSFont (pdf_font *font)
137 {
138 CIDSysInfo *csi;
139
140 ASSERT(font);
141
142 csi = &font->cid.csi;
143
144 if (!strcmp(csi->ordering, "UCS") ||
145 !strcmp(csi->ordering, "UCS2"))
146 return 1;
147
148 return 0;
149 }
150
151 char *
152 CIDFont_get_usedchars (pdf_font *font)
153 {
154 if (!font->usedchars) {
155 font->usedchars = NEW(8192, char);
156 memset(font->usedchars, 0, 8192*sizeof(char));
157 }
158
159 return font->usedchars;
160 }
161
162 char *
163 CIDFont_get_usedchars_v (pdf_font *font)
164 {
165 if (!font->cid.usedchars_v) {
166 font->cid.usedchars_v = NEW(8192, char);
167 memset(font->cid.usedchars_v, 0, 8192*sizeof(char));
168 }
169
170 return font->cid.usedchars_v;
171 }
172
173
174 static int
175 source_font_type (pdf_font *font)
176 {
177 int type = PDF_FONT_FONTTYPE_CIDTYPE0;
178
179 ASSERT(font);
180
181 if (font->flags & CIDFONT_FLAG_TYPE1) {
182 type = PDF_FONT_FONTTYPE_TYPE1;
183 } else if (font->flags & CIDFONT_FLAG_TYPE1C) {
184 type = PDF_FONT_FONTTYPE_TYPE1C;
185 } else if (font->flags & CIDFONT_FLAG_TRUETYPE) {
186 type = PDF_FONT_FONTTYPE_TRUETYPE;
187 }
188
189 return type;
190 }
191
192 void
193 pdf_font_load_cidfont (pdf_font *font)
194 {
195 int error = 0;
196
197 if (!font || !font->reference)
198 return;
199
200 if (dpx_conf.verbose_level > 0)
201 MESG(":%s", font->filename);
202 if (dpx_conf.verbose_level > 1) {
203 if (font->fontname)
204 MESG("[%s]", font->fontname);
205 }
206
207 switch (font->subtype) {
208 case PDF_FONT_FONTTYPE_CIDTYPE0:
209 if(dpx_conf.verbose_level > 0)
210 MESG("[CIDFontType0]");
211 switch (source_font_type(font)) {
212 case PDF_FONT_FONTTYPE_TYPE1:
213 error = CIDFont_type0_t1dofont(font);
214 break;
215 case PDF_FONT_FONTTYPE_TYPE1C:
216 error = CIDFont_type0_t1cdofont(font);
217 break;
218 default:
219 error = CIDFont_type0_dofont(font);
220 break;
221 }
222 break;
223 case PDF_FONT_FONTTYPE_CIDTYPE2:
224 if(dpx_conf.verbose_level > 0)
225 MESG("[CIDFontType2]");
226 error = CIDFont_type2_dofont(font);
227 break;
228 }
229
230 if (error)
231 ERROR("Error occurred while loading font: %s", font->filename);
232
233 return;
234 }
235
236 #include "pdfparse.h"
237 #include "cid_basefont.h"
238
239 static int
240 CIDFont_base_open (pdf_font *font, const char *name, cid_opt *opt)
241 {
242 pdf_obj *fontdict, *descriptor;
243 char *fontname = NULL;
244 int idx;
245
246 ASSERT(font);
247
248 for (idx = 0; cid_basefont[idx].fontname != NULL; idx++) {
249 if (!strcmp(name, cid_basefont[idx].fontname) ||
250 (strlen(name) == strlen(cid_basefont[idx].fontname) - strlen("-Acro") &&
251 !strncmp(name, cid_basefont[idx].fontname,
252 strlen(cid_basefont[idx].fontname)-strlen("-Acro")))
253 )
254 break;
255 }
256
257 if (cid_basefont[idx].fontname == NULL)
258 return -1;
259
260 fontname = NEW(strlen(name)+12, char);
261 memset(fontname, 0, strlen(name)+12);
262 strcpy(fontname, name);
263
264 switch (opt->style) {
265 case FONT_STYLE_BOLD:
266 strcat(fontname, ",Bold");
267 break;
268 case FONT_STYLE_ITALIC:
269 strcat(fontname, ",Italic");
270 break;
271 case FONT_STYLE_BOLDITALIC:
272 strcat(fontname, ",BoldItalic");
273 break;
274 }
275 {
276 const char *start;
277 const char *end;
278
279 start = cid_basefont[idx].fontdict;
280 end = start + strlen(start);
281 fontdict = parse_pdf_dict(&start, end, NULL);
282 start = cid_basefont[idx].descriptor;
283 end = start + strlen(start);
284 descriptor = parse_pdf_dict(&start, end, NULL);
285
286 ASSERT(fontdict && descriptor);
287 }
288
289 font->fontname = fontname;
290 font->flags |= PDF_FONT_FLAG_BASEFONT;
291 {
292 char *registry, *ordering;
293 int supplement;
294 pdf_obj *tmp;
295
296 tmp = pdf_lookup_dict(fontdict, "CIDSystemInfo");
297
298 ASSERT( tmp && pdf_obj_typeof(tmp) == PDF_DICT );
299
300 registry = pdf_string_value(pdf_lookup_dict(tmp, "Registry"));
301 ordering = pdf_string_value(pdf_lookup_dict(tmp, "Ordering"));
302 supplement = pdf_number_value(pdf_lookup_dict(tmp, "Supplement"));
303
304 font->cid.csi.registry = NEW(strlen(registry)+1, char);
305 font->cid.csi.ordering = NEW(strlen(ordering)+1, char);
306 strcpy(font->cid.csi.registry, registry);
307 strcpy(font->cid.csi.ordering, ordering);
308 font->cid.csi.supplement = supplement;
309 }
310
311 {
312 pdf_obj *tmp;
313 char *type;
314
315 tmp = pdf_lookup_dict(fontdict, "Subtype");
316 ASSERT( tmp != NULL && pdf_obj_typeof(tmp) == PDF_NAME );
317
318 type = pdf_name_value(tmp);
319 if (!strcmp(type, "CIDFontType0"))
320 font->subtype = PDF_FONT_FONTTYPE_CIDTYPE0;
321 else if (!strcmp(type, "CIDFontType2"))
322 font->subtype = PDF_FONT_FONTTYPE_CIDTYPE2;
323 else {
324 ERROR("Unknown CIDFontType \"%s\"", type);
325 }
326 }
327
328 if (opt_flags_cidfont & CIDFONT_FORCE_FIXEDPITCH) {
329 if (pdf_lookup_dict(fontdict, "W")) {
330 pdf_remove_dict(fontdict, "W");
331 }
332 if (pdf_lookup_dict(fontdict, "W2")) {
333 pdf_remove_dict(fontdict, "W2");
334 }
335 }
336
337 pdf_add_dict(fontdict, pdf_new_name("Type"), pdf_new_name("Font"));
338 pdf_add_dict(fontdict, pdf_new_name("BaseFont"), pdf_new_name(fontname));
339 pdf_add_dict(descriptor, pdf_new_name("Type"), pdf_new_name("FontDescriptor"));
340 pdf_add_dict(descriptor, pdf_new_name("FontName"), pdf_new_name(fontname));
341
342 font->resource = fontdict;
343 font->descriptor = descriptor;
344
345 opt->embed = 0;
346
347 return 0;
348 }
349
350 int
351 pdf_font_cidfont_lookup_cache (pdf_font *fonts, int count, const char *map_name,
352 CIDSysInfo *cmap_csi, const fontmap_opt *fmap_opt)
353 {
354 int font_id = -1;
355 pdf_font *font = NULL;
356 cid_opt opt;
357 int has_csi;
358
359 ASSERT(fonts);
360
361 opt.style = fmap_opt->style;
362 opt.embed = (fmap_opt->flags & FONTMAP_OPT_NOEMBED) ? 0 : 1;
363 opt.csi.registry = NULL;
364 opt.csi.ordering = NULL;
365 opt.csi.supplement = 0;
366 has_csi = get_cidsysinfo(&opt.csi, map_name, fmap_opt);
367 opt.stemv = fmap_opt->stemv;
368
369 if (!has_csi && cmap_csi) {
370 /*
371 * No CIDSystemInfo supplied explicitly. Copy from CMap's one if available.
372 * It is not neccesary for CID-keyed fonts. But TrueType requires them.
373 */
374 opt.csi.registry = NEW(strlen(cmap_csi->registry)+1, char);
375 strcpy(opt.csi.registry, cmap_csi->registry);
376 opt.csi.ordering = NEW(strlen(cmap_csi->ordering)+1, char);
377 strcpy(opt.csi.ordering, cmap_csi->ordering);
378 opt.csi.supplement = cmap_csi->supplement;
379 has_csi = 1;
380 }
381 /*
382 * Here, we do not compare font->ident and map_name because of
383 * implicit CIDSystemInfo supplied by CMap for TrueType.
384 */
385 for (font_id = 0; font_id < count; font_id++) {
386 font = &fonts[font_id];
387 if (font->subtype != PDF_FONT_FONTTYPE_CIDTYPE0 &&
388 font->subtype != PDF_FONT_FONTTYPE_CIDTYPE2)
389 continue;
390 if (!strcmp(font->filename, map_name) &&
391 font->cid.options.style == opt.style &&
392 font->index == fmap_opt->index) {
393 if (font->cid.options.embed == opt.embed) {
394 /*
395 * Case 1: CSI not available (Identity CMap)
396 * Font is TrueType --> continue
397 * Font is CIDFont --> break
398 * Case 2: CSI matched --> break
399 */
400 if (!has_csi) {
401 if (font->subtype == PDF_FONT_FONTTYPE_CIDTYPE2)
402 continue;
403 else
404 break;
405 } else if (!strcmp(font->cid.csi.registry, opt.csi.registry) &&
406 !strcmp(font->cid.csi.ordering, opt.csi.ordering)) {
407 if (font->subtype == PDF_FONT_FONTTYPE_CIDTYPE2)
408 font->cid.csi.supplement =
409 MAX(opt.csi.supplement, font->cid.csi.supplement); /* FIXME: font modified */
410 break;
411 }
412 } else if (font->flags & PDF_FONT_FLAG_BASEFONT) {
413 break;
414 }
415 }
416 }
417 release_opt(&opt);
418
419 return (font_id < count) ? font_id : -1;
420 }
421
422 int
423 pdf_font_open_cidfont (pdf_font *font, const char *map_name, CIDSysInfo *cmap_csi, const fontmap_opt *fmap_opt)
424 {
425 cid_opt opt;
426 int has_csi;
427
428 opt.style = fmap_opt->style;
429 opt.embed = (fmap_opt->flags & FONTMAP_OPT_NOEMBED) ? 0 : 1;
430 opt.csi.registry = NULL;
431 opt.csi.ordering = NULL;
432 opt.csi.supplement = 0;
433 has_csi = get_cidsysinfo(&opt.csi, map_name, fmap_opt);
434 opt.stemv = fmap_opt->stemv;
435
436 if (!has_csi && cmap_csi) {
437 /*
438 * No CIDSystemInfo supplied explicitly. Copy from CMap's one if available.
439 * It is not neccesary for CID-keyed fonts. But TrueType requires them.
440 */
441 opt.csi.registry = NEW(strlen(cmap_csi->registry)+1, char);
442 strcpy(opt.csi.registry, cmap_csi->registry);
443 opt.csi.ordering = NEW(strlen(cmap_csi->ordering)+1, char);
444 strcpy(opt.csi.ordering, cmap_csi->ordering);
445 opt.csi.supplement = cmap_csi->supplement;
446 has_csi = 1;
447 }
448
449 if (CIDFont_type0_open(font, map_name, fmap_opt->index, &opt) < 0 &&
450 CIDFont_type2_open(font, map_name, fmap_opt->index, &opt) < 0 &&
451 CIDFont_type0_open_from_t1 (font, map_name, fmap_opt->index, &opt) < 0 &&
452 CIDFont_type0_open_from_t1c(font, map_name, fmap_opt->index, &opt) < 0 &&
453 CIDFont_base_open (font, map_name, &opt) < 0) {
454 release_opt(&opt);
455 return -1;
456 }
457
458 font->filename = NEW(strlen(map_name)+1, char);
459 strcpy(font->filename, map_name);
460 font->ident = NEW(strlen(map_name)+1, char);
461 strcpy(font->ident, map_name);
462 font->index = fmap_opt->index;
463 font->cid.options = opt;
464
465 if (font->cid.csi.registry && font->cid.csi.ordering) {
466 if (cmap_csi) {
467 if (strcmp(font->cid.csi.registry, cmap_csi->registry) ||
468 strcmp(font->cid.csi.ordering, cmap_csi->ordering)) {
469 WARN("Inconsistent ROS found:\n");
470 MESG("\tFont: %s-%s-%d\n", font->cid.csi.registry, font->cid.csi.ordering, font->cid.csi.supplement);
471 MESG("\tCMap: %s-%s-%d\n", cmap_csi->registry, cmap_csi->ordering, cmap_csi->supplement);
472 ERROR("Incompatible CMap specified for this font.");
473 }
474 if (font->cid.csi.supplement < cmap_csi->supplement) {
475 font->cid.csi.supplement = cmap_csi->supplement;
476 }
477 }
478 } else {
479 ASSERT(font->subtype == PDF_FONT_FONTTYPE_CIDTYPE2);
480 if (cmap_csi) {
481 font->cid.csi.registry = NEW(strlen(cmap_csi->registry)+1, char);
482 strcpy(font->cid.csi.registry, cmap_csi->registry);
483 font->cid.csi.ordering = NEW(strlen(cmap_csi->ordering)+1, char);
484 strcpy(font->cid.csi.ordering, cmap_csi->ordering);
485 font->cid.csi.supplement = cmap_csi->supplement;
486 } else { /* This means font's internal glyph ordering. */
487 font->cid.csi.registry = NEW(strlen("Adobe")+1, char);
488 strcpy(font->cid.csi.registry, "Adobe");
489 font->cid.csi.ordering = NEW(strlen("Identity")+1, char);
490 strcpy(font->cid.csi.ordering, "Identity");
491 font->cid.csi.supplement = 0;
492 }
493 }
494
495 return 0;
496 }
497
498
499 /******************************* OPTIONS *******************************/
500
501 /*
502 * FORMAT:
503 *
504 * (:int:)?!?string(/string)?(,string)?
505 */
506
507 static void
508 release_opt (cid_opt *opt)
509 {
510 if (opt->csi.registry)
511 RELEASE(opt->csi.registry);
512 if (opt->csi.ordering)
513 RELEASE(opt->csi.ordering);
514 }
515
516 static int
517 get_cidsysinfo (CIDSysInfo *csi, const char *map_name, const fontmap_opt *fmap_opt)
518 {
519 int has_csi = 0;
520 int sup_idx;
521 int i, csi_idx = -1, n, m;
522
523 sup_idx = pdf_get_version() - 10;
524 sup_idx = (sup_idx > SUP_IDX_MAX) ? SUP_IDX_MAX : sup_idx;
525
526 if (!fmap_opt || !fmap_opt->charcoll)
527 return 0;
528
529 /* First try alias for standard one. */
530 for (i = 0; CIDFont_stdcc_alias[i].name != NULL; i++) {
531 n = strlen(CIDFont_stdcc_alias[i].name);
532 if (!strncmp(fmap_opt->charcoll,
533 CIDFont_stdcc_alias[i].name, n)) {
534 csi_idx = CIDFont_stdcc_alias[i].index;
535 csi->registry = NEW(strlen(CIDFont_stdcc_def[csi_idx].registry)+1, char);
536 strcpy(csi->registry, CIDFont_stdcc_def[csi_idx].registry);
537 csi->ordering = NEW(strlen(CIDFont_stdcc_def[csi_idx].ordering)+1, char);
538 strcpy(csi->ordering, CIDFont_stdcc_def[csi_idx].ordering);
539 if (strlen(fmap_opt->charcoll) > n) {
540 csi->supplement = (int) strtoul(&(fmap_opt->charcoll[n]), NULL, 10);
541 } else { /* Use heighest supported value for current output PDF version. */
542 csi->supplement = CIDFont_stdcc_def[csi_idx].supplement[sup_idx];
543 }
544 has_csi = 1;
545 break;
546 }
547 }
548 if (!has_csi) {
549 char *p, *q;
550
551 p = (char *) fmap_opt->charcoll;
552
553 /* Full REGISTRY-ORDERING-SUPPLEMENT */
554 p = strchr(fmap_opt->charcoll, '-');
555 if (!p || p[1] == '\0')
556 ERROR("String can't be converted to REGISTRY-ORDERING-SUPPLEMENT: %s", fmap_opt->charcoll);
557 p++;
558
559 q = strchr(p, '-');
560 if (!q || q[1] == '\0')
561 ERROR("String can't be converted to REGISTRY-ORDERING-SUPPLEMENT: %s", fmap_opt->charcoll);
562 q++;
563
564 if (!isdigit((unsigned char)q[0]))
565 ERROR("String can't be converted to REGISTRY-ORDERING-SUPPLEMENT: %s", fmap_opt->charcoll);
566
567 n = strlen(fmap_opt->charcoll) - strlen(p) - 1;
568 csi->registry = NEW(n+1, char);
569 memcpy(csi->registry, fmap_opt->charcoll, n);
570 csi->registry[n] = '\0';
571
572 m = strlen(p) - strlen(q) - 1;
573 csi->ordering = NEW(m+1, char);
574 memcpy(csi->ordering, p, m);
575 csi->ordering[m] = '\0';
576
577 csi->supplement = (int) strtoul(q, NULL, 10);
578
579 has_csi = 1;
580
581 /* Check for standart character collections. */
582 for (i = 0; CIDFont_stdcc_def[i].ordering != NULL; i++) {
583 if ((CIDFont_stdcc_def[i].registry &&
584 !strcmp(csi->registry, CIDFont_stdcc_def[i].registry)) &&
585 !strcmp(csi->ordering, CIDFont_stdcc_def[i].ordering)) {
586 csi_idx = i;
587 break;
588 }
589 }
590 }
591
592 if (csi && csi_idx >= 0) {
593 if (csi->supplement > CIDFont_stdcc_def[csi_idx].supplement[sup_idx]
594 && (fmap_opt->flags & FONTMAP_OPT_NOEMBED)) {
595 WARN("Heighest supplement number supported in PDF-%d.%d for %s-%s is %d.",
596 pdf_get_version_major(), pdf_get_version_minor(),
597 csi->registry, csi->ordering,
598 CIDFont_stdcc_def[csi_idx].supplement[sup_idx]);
599 WARN("Some character may not shown without embedded font (--> %s).", map_name);
600 }
601 }
602
603 return has_csi;
604 }
605
606 void
607 CIDFont_set_flags (int flags)
608 {
609 opt_flags_cidfont |= flags;
610 }