Debian Codegraph @

texk/dvipdfm-x/cidtype0.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 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 * Only CFF/OpenType CID-Keyed Font with Type 2 charstrings is supported.
25 *
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "system.h"
33 #include "numbers.h"
34 #include "mem.h"
35 #include "error.h"
36 #include "dpxconf.h"
37
38 #include "dpxfile.h"
39
40 #include "pdfobj.h"
41 /* pseudo unique tag */
42 #include "pdffont.h"
43
44 /* Font info. from OpenType tables */
45 #include "sfnt.h"
46 #include "tt_aux.h"
47 /* Metrics */
48 #include "tt_table.h"
49
50 #include "cff_types.h"
51 #include "cff_limits.h"
52 #include "cff.h"
53 #include "cff_dict.h"
54 #include "cs_type2.h"
55
56 /* typedef CID in cmap.h */
57 #include "cmap.h"
58 #include "type0.h"
59 #include "cid.h"
60 #include "cidtype0.h"
61
62 /* Type1 --> CFF CIDFont */
63 #include "unicode.h"
64 #include "t1_load.h"
65 #include "t1_char.h"
66
67 #include "agl.h"
68
69 #include "cmap.h"
70 #include "cmap_write.h"
71 #include "fontmap.h"
72
73 /*
74 * PDF Reference 3rd. ed., p.340, "Glyph Metrics in CID Fonts".
75 */
76 #ifndef PDFUNIT
77 #define PDFUNIT(v) (ROUND((1000.0*(v))/(head->unitsPerEm),1))
78 #endif
79
80 static void
81 add_CIDHMetrics (sfnt *sfont, pdf_obj *fontdict,
82 unsigned char *CIDToGIDMap, unsigned short last_cid,
83 struct tt_maxp_table *maxp,
84 struct tt_head_table *head, struct tt_longMetrics *hmtx)
85 {
86 pdf_obj *w_array, *an_array = NULL;
87 int cid, start = 0, prev = 0;
88 double defaultAdvanceWidth;
89 int empty = 1;
90
91 (void) sfont; /* unsed */
92 defaultAdvanceWidth = PDFUNIT(hmtx[0].advance);
93 /*
94 * We alway use format:
95 * c [w_1 w_2 ... w_n]
96 */
97 w_array = pdf_new_array();
98 for (cid = 0; cid <= last_cid; cid++) {
99 USHORT gid;
100 double advanceWidth;
101 gid = CIDToGIDMap ? ((CIDToGIDMap[2*cid] << 8)|CIDToGIDMap[2*cid+1]) : cid;
102 if (gid >= maxp->numGlyphs || (cid != 0 && gid == 0))
103 continue;
104 advanceWidth = PDFUNIT(hmtx[gid].advance);
105 if (advanceWidth == defaultAdvanceWidth) {
106 if (an_array) {
107 pdf_add_array(w_array, pdf_new_number(start));
108 pdf_add_array(w_array, an_array);
109 an_array = NULL;
110 empty = 0;
111 }
112 } else {
113 if (cid != prev + 1 && an_array) {
114 pdf_add_array(w_array, pdf_new_number(start));
115 pdf_add_array(w_array, an_array);
116 an_array = NULL;
117 empty = 0;
118 }
119 if (an_array == NULL) {
120 an_array = pdf_new_array();
121 start = cid;
122 }
123 pdf_add_array(an_array, pdf_new_number(advanceWidth));
124 prev = cid;
125 }
126 }
127
128 if (an_array) {
129 pdf_add_array(w_array, pdf_new_number(start));
130 pdf_add_array(w_array, an_array);
131 empty = 0;
132 }
133
134 /*
135 * We always write DW for older MacOS X's preview app.
136 * PDF Reference 2nd. ed, wrongly described default value of DW as 0, and
137 * MacOS X's (up to 10.2.8) preview app. implements this wrong description.
138 */
139 pdf_add_dict(fontdict,
140 pdf_new_name("DW"),
141 pdf_new_number(defaultAdvanceWidth));
142 if (!empty) {
143 pdf_add_dict(fontdict,
144 pdf_new_name("W"),
145 pdf_ref_obj(w_array));
146 }
147 pdf_release_obj(w_array);
148
149 return;
150 }
151
152 static void
153 add_CIDVMetrics (sfnt *sfont, pdf_obj *fontdict,
154 unsigned char *CIDToGIDMap, unsigned short last_cid,
155 struct tt_maxp_table *maxp,
156 struct tt_head_table *head, struct tt_longMetrics *hmtx)
157 {
158 pdf_obj *w2_array, *an_array = NULL;
159 int cid;
160 #if 0
161 int prev = 0, start = 0;
162 #endif
163 struct tt_VORG_table *vorg;
164 struct tt_vhea_table *vhea = NULL;
165 struct tt_longMetrics *vmtx = NULL;
166 double defaultAdvanceHeight, defaultVertOriginY;
167 int empty = 1;
168
169 /*
170 * No accurate vertical metrics can be obtained by simple way if the
171 * font does not have VORG table. Only CJK fonts may have VORG.
172 */
173 if (sfnt_find_table_pos(sfont, "VORG") <= 0)
174 return;
175
176 vorg = tt_read_VORG_table(sfont);
177 defaultVertOriginY = PDFUNIT(vorg->defaultVertOriginY);
178 if (sfnt_find_table_pos(sfont, "vhea") > 0)
179 vhea = tt_read_vhea_table(sfont);
180 if (vhea && sfnt_find_table_pos(sfont, "vmtx") > 0) {
181 sfnt_locate_table(sfont, "vmtx");
182 vmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics, vhea->numOfExSideBearings);
183 }
184
185 if (sfnt_find_table_pos(sfont, "OS/2") <= 0) {
186 struct tt_os2__table *os2;
187 /* OpenType font must have OS/2 table. */
188 os2 = tt_read_os2__table(sfont);
189 defaultVertOriginY = PDFUNIT(os2->sTypoAscender);
190 defaultAdvanceHeight = PDFUNIT(os2->sTypoAscender - os2->sTypoDescender);
191 RELEASE(os2);
192 } else {
193 /* Some TrueType fonts used in Macintosh does not have OS/2 table. */
194 defaultAdvanceHeight = 1000;
195 }
196
197 w2_array = pdf_new_array();
198 for (cid = 0; cid <= last_cid; cid++) {
199 USHORT i, gid;
200 double advanceHeight, vertOriginX, vertOriginY;
201 gid = CIDToGIDMap ? ((CIDToGIDMap[2*cid] << 8)|CIDToGIDMap[2*cid+1]) : cid;
202 if (gid >= maxp->numGlyphs || (cid != 0 && gid == 0))
203 continue;
204 advanceHeight = vmtx ? PDFUNIT(vmtx[gid].advance) : defaultAdvanceHeight;
205 vertOriginX = PDFUNIT(hmtx[gid].advance*0.5);
206 vertOriginY = defaultVertOriginY;
207 for (i = 0;
208 i < vorg->numVertOriginYMetrics && gid >= vorg->vertOriginYMetrics[i].glyphIndex;
209 i++) {
210 if (gid == vorg->vertOriginYMetrics[i].glyphIndex)
211 vertOriginY = PDFUNIT(vorg->vertOriginYMetrics[i].vertOriginY);
212 }
213 #if 0
214 /*
215 * c [w1_1y v_1x v_1y w1_2y v_2x v_2y ...]
216 * Not working... Why?
217 * Acrobat Reader:
218 * Wrong rendering, interpretation of position vector is wrong.
219 * Xpdf and gs: ignores W2?
220 */
221 if (vertOriginY == defaultVertOriginY &&
222 advanceHeight == defaultAdvanceHeight) {
223 if (an_array) {
224 pdf_add_array(w2_array, pdf_new_number(start));
225 pdf_add_array(w2_array, an_array);
226 an_array = NULL;
227 empty = 0;
228 }
229 } else {
230 if (cid != prev + 1 && an_array) {
231 pdf_add_array(w2_array, pdf_new_number(start));
232 pdf_add_array(w2_array, an_array);
233 an_array = NULL;
234 empty = 0;
235 }
236 if (an_array == NULL) {
237 an_array = pdf_new_array();
238 start = cid;
239 }
240 pdf_add_array(an_array, pdf_new_number(-advanceHeight));
241 pdf_add_array(an_array, pdf_new_number(vertOriginX));
242 pdf_add_array(an_array, pdf_new_number(vertOriginY));
243 prev = cid;
244 }
245 #else
246 /*
247 * c_first c_last w1_y v_x v_y
248 * This form may hit Acrobat's implementation limit of array element size, 8192.
249 * AFPL GhostScript 8.11 stops with rangecheck error with this. Maybe GS's bug?
250 */
251 if (vertOriginY != defaultVertOriginY ||
252 advanceHeight != defaultAdvanceHeight) {
253 pdf_add_array(w2_array, pdf_new_number(cid));
254 pdf_add_array(w2_array, pdf_new_number(cid));
255 pdf_add_array(w2_array, pdf_new_number(-advanceHeight));
256 pdf_add_array(w2_array, pdf_new_number(vertOriginX));
257 pdf_add_array(w2_array, pdf_new_number(vertOriginY));
258 empty = 0;
259 }
260 #endif
261 }
262
263 #if 0
264 if (an_array) {
265 pdf_add_array(w2_array, pdf_new_number(start));
266 pdf_add_array(w2_array, an_array);
267 empty = 0;
268 }
269 #endif
270
271 if (defaultVertOriginY != 880 || defaultAdvanceHeight != 1000) {
272 an_array = pdf_new_array();
273 pdf_add_array(an_array, pdf_new_number(defaultVertOriginY));
274 pdf_add_array(an_array, pdf_new_number(-defaultAdvanceHeight));
275 pdf_add_dict(fontdict, pdf_new_name ("DW2"), an_array);
276 }
277 if (!empty) {
278 pdf_add_dict(fontdict,
279 pdf_new_name("W2"), pdf_ref_obj(w2_array));
280 }
281 pdf_release_obj(w2_array);
282
283 if (vorg->vertOriginYMetrics)
284 RELEASE(vorg->vertOriginYMetrics);
285 RELEASE(vorg);
286
287 if (vmtx)
288 RELEASE(vmtx);
289 if (vhea)
290 RELEASE(vhea);
291
292 return;
293 }
294
295 static void
296 add_CIDMetrics (sfnt *sfont, pdf_obj *fontdict,
297 unsigned char *CIDToGIDMap, unsigned short last_cid, int need_vmetrics)
298 {
299 struct tt_longMetrics *hmtx;
300 struct tt_head_table *head;
301 struct tt_hhea_table *hhea;
302 struct tt_maxp_table *maxp;
303
304 /*
305 * Read head, hhea, maxp:
306 *
307 * unitsPerEm --> head
308 * numHMetrics --> hhea
309 * numGlyphs --> maxp
310 */
311 head = tt_read_head_table(sfont);
312 maxp = tt_read_maxp_table(sfont);
313 hhea = tt_read_hhea_table(sfont);
314
315 sfnt_locate_table(sfont, "hmtx");
316 hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numOfLongHorMetrics, hhea->numOfExSideBearings);
317
318 add_CIDHMetrics(sfont, fontdict, CIDToGIDMap, last_cid, maxp, head, hmtx);
319 if (need_vmetrics)
320 add_CIDVMetrics(sfont, fontdict, CIDToGIDMap, last_cid, maxp, head, hmtx);
321
322 RELEASE(hmtx);
323 RELEASE(hhea);
324 RELEASE(maxp);
325 RELEASE(head);
326
327 return;
328 }
329
330 /*
331 * Create an instance of embeddable font.
332 */
333 static int
334 write_fontfile (pdf_font *font, cff_font *cffont)
335 {
336 cff_index *topdict, *fdarray, *private;
337 unsigned char *dest;
338 int destlen = 0, i, size;
339 int offset, topdict_offset, fdarray_offset;
340
341 /* DICT sizes (offset set to long int) */
342 topdict = cff_new_index(1);
343 fdarray = cff_new_index(cffont->num_fds);
344 private = cff_new_index(cffont->num_fds);
345
346 cff_dict_remove(cffont->topdict, "UniqueID");
347 cff_dict_remove(cffont->topdict, "XUID");
348 cff_dict_remove(cffont->topdict, "Private"); /* some bad font may have */
349 cff_dict_remove(cffont->topdict, "Encoding"); /* some bad font may have */
350
351 topdict->offset[1] = cff_dict_pack(cffont->topdict,
352 (card8 *) work_buffer,
353 WORK_BUFFER_SIZE) + 1;
354 for (i = 0;i < cffont->num_fds; i++) {
355 size = 0;
356 if (cffont->private && cffont->private[i]) {
357 size = cff_dict_pack(cffont->private[i],
358 (card8 *) work_buffer, WORK_BUFFER_SIZE);
359 if (size < 1) { /* Private had contained only Subr */
360 cff_dict_remove(cffont->fdarray[i], "Private");
361 }
362 }
363 (private->offset)[i+1] = (private->offset)[i] + size;
364 (fdarray->offset)[i+1] = (fdarray->offset)[i] +
365 cff_dict_pack(cffont->fdarray[i],
366 (card8 *) work_buffer, WORK_BUFFER_SIZE);
367 }
368
369 destlen = 4; /* header size */
370 destlen += cff_set_name(cffont, font->fontname);
371 destlen += cff_index_size(topdict);
372 destlen += cff_index_size(cffont->string);
373 destlen += cff_index_size(cffont->gsubr);
374 destlen += (cffont->charsets->num_entries) * 2 + 1; /* charset format 0 */
375 destlen += (cffont->fdselect->num_entries) * 3 + 5; /* fdselect format 3 */
376 destlen += cff_index_size(cffont->cstrings);
377 destlen += cff_index_size(fdarray);
378 destlen += private->offset[private->count] - 1; /* Private is not INDEX */
379
380 dest = NEW(destlen, card8);
381
382 offset = 0;
383 /* Header */
384 offset += cff_put_header(cffont, dest + offset, destlen - offset);
385 /* Name */
386 offset += cff_pack_index(cffont->name, dest + offset, destlen - offset);
387 /* Top DICT */
388 topdict_offset = offset;
389 offset += cff_index_size(topdict);
390 /* Strings */
391 offset += cff_pack_index(cffont->string, dest + offset, destlen - offset);
392 /* Global Subrs */
393 offset += cff_pack_index(cffont->gsubr, dest + offset, destlen - offset);
394
395 /* charset */
396 cff_dict_set(cffont->topdict, "charset", 0, offset);
397 offset += cff_pack_charsets(cffont, dest + offset, destlen - offset);
398
399 /* FDSelect */
400 cff_dict_set(cffont->topdict, "FDSelect", 0, offset);
401 offset += cff_pack_fdselect(cffont, dest + offset, destlen - offset);
402
403 /* CharStrings */
404 cff_dict_set(cffont->topdict, "CharStrings", 0, offset);
405 offset += cff_pack_index(cffont->cstrings,
406 dest + offset, cff_index_size(cffont->cstrings));
407 cff_release_index(cffont->cstrings);
408 cffont->cstrings = NULL; /* Charstrings cosumes huge memory */
409
410 /* FDArray and Private */
411 cff_dict_set(cffont->topdict, "FDArray", 0, offset);
412 fdarray_offset = offset;
413 offset += cff_index_size(fdarray);
414
415 fdarray->data = NEW(fdarray->offset[fdarray->count] - 1, card8);
416 for (i = 0; i < cffont->num_fds; i++) {
417 size = private->offset[i+1] - private->offset[i];
418 if (cffont->private[i] && size > 0) {
419 cff_dict_pack(cffont->private[i], dest + offset, size);
420 cff_dict_set(cffont->fdarray[i], "Private", 0, size);
421 cff_dict_set(cffont->fdarray[i], "Private", 1, offset);
422 }
423 cff_dict_pack(cffont->fdarray[i],
424 fdarray->data + (fdarray->offset)[i] - 1,
425 fdarray->offset[fdarray->count] - 1);
426 offset += size;
427 }
428
429 cff_pack_index(fdarray, dest + fdarray_offset, cff_index_size(fdarray));
430 cff_release_index(fdarray);
431 cff_release_index(private);
432
433 /* Finally Top DICT */
434 topdict->data = NEW(topdict->offset[topdict->count] - 1, card8);
435 cff_dict_pack(cffont->topdict,
436 topdict->data, topdict->offset[topdict->count] - 1);
437 cff_pack_index(topdict, dest + topdict_offset, cff_index_size(topdict));
438 cff_release_index(topdict);
439
440 /*
441 * FontFile
442 */
443 {
444 pdf_obj *fontfile, *stream_dict;
445
446 fontfile = pdf_new_stream(STREAM_COMPRESS);
447 stream_dict = pdf_stream_dict(fontfile);
448 pdf_add_dict(font->descriptor,
449 pdf_new_name("FontFile3"),
450 pdf_ref_obj (fontfile));
451 pdf_add_dict(stream_dict,
452 pdf_new_name("Subtype"),
453 pdf_new_name("CIDFontType0C"));
454 pdf_add_stream(fontfile, (char *) dest, offset);
455 pdf_release_obj(fontfile);
456 RELEASE(dest);
457 }
458
459 return destlen;
460 }
461
462 static void
463 CIDFont_type0_add_CIDSet (pdf_font *font, char *used_chars, card16 last_cid) {
464 /*
465 * CIDSet:
466 * Length of CIDSet stream is not clear. Must be 8192 bytes long?
467 */
468 pdf_obj *cidset;
469
470 cidset = pdf_new_stream(STREAM_COMPRESS);
471 pdf_add_stream(cidset, used_chars, (last_cid / 8) + 1);
472 pdf_add_dict(font->descriptor,
473 pdf_new_name("CIDSet"), pdf_ref_obj(cidset));
474 pdf_release_obj(cidset);
475 }
476
477 int
478 CIDFont_type0_dofont (pdf_font *font)
479 {
480 FILE *fp;
481 sfnt *sfont;
482 cff_font *cffont;
483 cff_index *charstrings, *idx;
484 cff_charsets *charset = NULL;
485 cff_fdselect *fdselect = NULL;
486 int charstring_len, max_len;
487 int destlen = 0;
488 int size, offset = 0;
489 card8 *data;
490 card16 num_glyphs = 0, gid;
491 int cid;
492 card16 cs_count, last_cid = 0;
493 int fd, prev_fd;
494 char *used_chars;
495 unsigned char *CIDToGIDMap = NULL;
496
497 ASSERT(font);
498
499 if (!font->reference)
500 return 0;
501
502 pdf_add_dict(font->resource,
503 pdf_new_name("FontDescriptor"),
504 pdf_ref_obj (font->descriptor));
505
506 if (font->flags & PDF_FONT_FLAG_BASEFONT) {
507 return 0;
508 } else if (!font->cid.options.embed &&
509 (opt_flags_cidfont & CIDFONT_FORCE_FIXEDPITCH)) {
510 /* No metrics needed. */
511 pdf_add_dict(font->resource,
512 pdf_new_name("DW"), pdf_new_number(1000.0));
513 return 0;
514 }
515
516 used_chars = font->usedchars;
517
518 fp = DPXFOPEN(font->filename, DPX_RES_TYPE_OTFONT);
519 if (!fp) {
520 fp = DPXFOPEN(font->filename, DPX_RES_TYPE_TTFONT);
521 if (!fp) {
522 WARN("Could not open file: %s", font->filename);
523 return -1;
524 }
525 }
526
527 sfont = sfnt_open(fp);
528 if (!sfont) {
529 WARN("Failed to read font file: %s", font->filename);
530 DPXFCLOSE(fp);
531 return -1;
532 }
533
534 if (sfont->type == SFNT_TYPE_TTC)
535 offset = ttc_read_offset(sfont, font->index);
536
537 if ((sfont->type != SFNT_TYPE_TTC &&
538 sfont->type != SFNT_TYPE_POSTSCRIPT) ||
539 sfnt_read_table_directory(sfont, offset) < 0 ||
540 (offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) {
541 WARN("Not a CFF/OpenType font: %s", font->filename);
542 sfnt_close(sfont);
543 DPXFCLOSE(fp);
544 return -1;
545 }
546
547 cffont = cff_open(sfont->stream, offset, 0);
548 if (!cffont) {
549 WARN("Failed to read CFF font data: %s", font->filename);
550 sfnt_close(sfont);
551 DPXFCLOSE(fp);
552 return -1;
553 }
554
555 if (!(cffont->flag & FONTTYPE_CIDFONT)) {
556 WARN("Unexpected type (CIDFont expected): %s", font->filename);
557 cff_close(cffont);
558 sfnt_close(sfont);
559 DPXFCLOSE(fp);
560 return -1;
561 }
562
563 cff_read_charsets(cffont);
564
565 {
566 int cid_count;
567
568 if (cff_dict_known(cffont->topdict, "CIDCount")) {
569 cid_count = (int) cff_dict_get(cffont->topdict, "CIDCount", 0);
570 } else {
571 cid_count = CID_MAX + 1;
572 }
573
574 CIDToGIDMap = NEW(2 * cid_count, unsigned char);
575 memset(CIDToGIDMap, 0, 2 * cid_count);
576 add_to_used_chars2(used_chars, 0); /* .notdef */
577 for (cid = 0; cid <= CID_MAX; cid++) {
578 if (is_used_char2(used_chars, cid)) {
579 gid = cff_charsets_lookup(cffont, (card16)cid);
580 if (cid != 0 && gid == 0) {
581 WARN("Glyph for CID %u missing in font \"%s\".", (CID) cid, font->filename);
582 used_chars[cid/8] &= ~(1 << (7 - (cid % 8)));
583 continue;
584 }
585 CIDToGIDMap[2*cid] = (gid >> 8) & 0xff;
586 CIDToGIDMap[2*cid+1] = gid & 0xff;
587 last_cid = cid;
588 num_glyphs++;
589 }
590 }
591 }
592
593 /*
594 * DW, W, DW2 and W2:
595 * Those values are obtained from OpenType table (not TFM).
596 */
597 if (opt_flags_cidfont & CIDFONT_FORCE_FIXEDPITCH) {
598 pdf_add_dict(font->resource,
599 pdf_new_name("DW"), pdf_new_number(1000.0));
600 } else {
601 add_CIDMetrics(sfont, font->resource, CIDToGIDMap, last_cid,
602 font->cid.need_vmetrics ? 1 : 0);
603 }
604
605 if (!font->cid.options.embed) {
606 RELEASE(CIDToGIDMap);
607 cff_close(cffont);
608 sfnt_close(sfont);
609 DPXFCLOSE(fp);
610 return 0;
611 }
612
613 /*
614 * Embed font subset.
615 */
616 cff_read_fdselect(cffont);
617 cff_read_fdarray(cffont);
618 cff_read_private(cffont);
619
620 cff_read_subrs(cffont);
621
622 offset = (int) cff_dict_get(cffont->topdict, "CharStrings", 0);
623 cff_seek_set(cffont, offset);
624 idx = cff_get_index_header(cffont);
625 /* offset is now absolute offset ... bad */
626 offset = cff_tell(cffont);
627
628 if ((cs_count = idx->count) < 2) {
629 WARN("No valid charstring data found: %s", font->filename);
630 cff_close(cffont);
631 sfnt_close(sfont);
632 DPXFCLOSE(fp);
633 return -1;
634 }
635
636 /* New Charsets data */
637 charset = NEW(1, cff_charsets);
638 charset->format = 0;
639 charset->num_entries = 0;
640 charset->data.glyphs = NEW(num_glyphs, s_SID);
641
642 /* New FDSelect data */
643 fdselect = NEW(1, cff_fdselect);
644 fdselect->format = 3;
645 fdselect->num_entries = 0;
646 fdselect->data.ranges = NEW(num_glyphs, cff_range3);
647
648 /* New CharStrings INDEX */
649 charstrings = cff_new_index((card16)(num_glyphs+1));
650 max_len = 2 * CS_STR_LEN_MAX;
651 charstrings->data = NEW(max_len, card8);
652 charstring_len = 0;
653
654 /*
655 * TODO: Re-assign FD number.
656 */
657 prev_fd = -1; gid = 0;
658 data = NEW(CS_STR_LEN_MAX, card8);
659 for (cid = 0; cid <= last_cid; cid++) {
660 unsigned short gid_org;
661
662 if (!is_used_char2(used_chars, cid))
663 continue;
664
665 gid_org = (CIDToGIDMap[2*cid] << 8)|(CIDToGIDMap[2*cid+1]);
666 if ((size = (idx->offset)[gid_org+1] - (idx->offset)[gid_org])
667 > CS_STR_LEN_MAX) {
668 WARN("Charstring too long: %s (gid=%u)", font->filename, gid_org);
669 RELEASE(data);
670 RELEASE(charstrings);
671 RELEASE(fdselect);
672 RELEASE(charset);
673 cff_release_index(idx);
674 RELEASE(CIDToGIDMap);
675 cff_close(cffont);
676 sfnt_close(sfont);
677 DPXFCLOSE(fp);
678 return -1;
679 }
680 if (charstring_len + CS_STR_LEN_MAX >= max_len) {
681 max_len = charstring_len + 2 * CS_STR_LEN_MAX;
682 charstrings->data = RENEW(charstrings->data, max_len, card8);
683 }
684 (charstrings->offset)[gid] = charstring_len + 1;
685 cff_seek(cffont, offset + (idx->offset)[gid_org] - 1);
686 cff_read_data(data, size, cffont);
687 fd = cff_fdselect_lookup(cffont, gid_org);
688 charstring_len += cs_copy_charstring(charstrings->data + charstring_len,
689 max_len - charstring_len,
690 data, size,
691 cffont->gsubr, (cffont->subrs)[fd], 0, 0, NULL);
692 if (cid > 0 && gid_org > 0) {
693 charset->data.glyphs[charset->num_entries] = cid;
694 charset->num_entries += 1;
695 }
696 if (fd != prev_fd) {
697 fdselect->data.ranges[fdselect->num_entries].first = gid;
698 fdselect->data.ranges[fdselect->num_entries].fd = fd;
699 fdselect->num_entries += 1;
700 prev_fd = fd;
701 }
702 gid++;
703 }
704 if (gid != num_glyphs) {
705 WARN("Unexpeced error: %s", font->filename);
706 RELEASE(data);
707 RELEASE(charstrings);
708 RELEASE(fdselect);
709 RELEASE(charset);
710 cff_release_index(idx);
711 RELEASE(CIDToGIDMap);
712 cff_close(cffont);
713 sfnt_close(sfont);
714 DPXFCLOSE(fp);
715 return -1;
716 }
717 RELEASE(data);
718 cff_release_index(idx);
719
720 RELEASE(CIDToGIDMap);
721
722 (charstrings->offset)[num_glyphs] = charstring_len + 1;
723 charstrings->count = num_glyphs;
724 cffont->num_glyphs = num_glyphs;
725 cffont->cstrings = charstrings;
726
727 /* discard old one, set new data */
728 cff_release_charsets(cffont->charsets);
729 cffont->charsets = charset;
730 cff_release_fdselect(cffont->fdselect);
731 cffont->fdselect = fdselect;
732
733 /* no Global subr */
734 if (cffont->gsubr)
735 cff_release_index(cffont->gsubr);
736 cffont->gsubr = cff_new_index(0);
737
738 for (fd = 0; fd < cffont->num_fds; fd++) {
739 if (cffont->subrs && cffont->subrs[fd]) {
740 cff_release_index(cffont->subrs[fd]);
741 cffont->subrs[fd] = NULL;
742 }
743 if (cffont->private && (cffont->private)[fd]) {
744 cff_dict_remove((cffont->private)[fd], "Subrs"); /* no Subrs */
745 }
746 }
747
748 destlen = write_fontfile(font, cffont);
749
750 cff_close(cffont);
751 sfnt_close(sfont);
752 DPXFCLOSE(fp);
753
754 if (dpx_conf.verbose_level > 1)
755 MESG("[%u/%u glyphs][%ld bytes]", num_glyphs, cs_count, destlen);
756
757 if (pdf_check_version(2, 0) < 0) {
758 CIDFont_type0_add_CIDSet(font, used_chars, last_cid);
759 }
760
761 return 0;
762 }
763
764 int
765 CIDFont_type0_open_from_t1 (pdf_font *font, const char *name, int index, cid_opt *opt)
766 {
767 CIDSysInfo csi;
768 char *fontname;
769 cff_font *cffont;
770 FILE *fp = NULL;
771
772 ASSERT(font);
773
774 fp = DPXFOPEN(name, DPX_RES_TYPE_T1FONT);
775 if (!fp)
776 return -1;
777
778 cffont = t1_load_font(NULL, 1, fp);
779 if (!cffont) {
780 DPXFCLOSE(fp);
781 return -1;
782 }
783
784 {
785 char *shortname;
786 int fontname_len = 8;
787
788 shortname = cff_get_name(cffont);
789 if (!shortname) {
790 WARN("No valid FontName found: %s", name);
791 cff_close(cffont);
792 DPXFCLOSE(fp);
793 }
794 /*
795 * Mangled name requires more 7 bytes.
796 */
797 fontname = NEW(strlen(shortname) + fontname_len, char);
798 memset(fontname, 0, strlen(shortname) + fontname_len);
799 strcpy(fontname, shortname);
800 RELEASE(shortname);
801 }
802
803 csi.registry = NEW(strlen("Adobe") + 1, char);
804 strcpy(csi.registry, "Adobe");
805 csi.ordering = NEW(strlen("Identity") + 1, char);
806 strcpy(csi.ordering, "Identity");
807 csi.supplement = 0;
808 if (opt->style != FONT_STYLE_NONE) {
809 WARN(",Bold, ,Italic, ... not supported for this type of font...");
810 opt->style = FONT_STYLE_NONE;
811 }
812
813 font->fontname = fontname;
814 font->subtype = PDF_FONT_FONTTYPE_CIDTYPE0;
815 font->cid.csi = csi;
816 font->flags |= CIDFONT_FLAG_TYPE1;
817
818 font->resource = pdf_new_dict();
819 pdf_add_dict(font->resource,
820 pdf_new_name("Type"),
821 pdf_new_name("Font"));
822 pdf_add_dict(font->resource,
823 pdf_new_name("Subtype"),
824 pdf_new_name("CIDFontType0"));
825
826 pdf_font_make_uniqueTag(font->uniqueID);
827
828 font->descriptor = pdf_new_dict();
829 {
830 char *tmp;
831
832 tmp = NEW(strlen(font->fontname)+8, char);
833 sprintf(tmp, "%s+%s", font->uniqueID, font->fontname);
834 pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(tmp));
835 pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(tmp));
836 RELEASE(tmp);
837 }
838 {
839 pdf_obj *csi_dict = pdf_new_dict();
840 pdf_add_dict(csi_dict,
841 pdf_new_name("Registry"),
842 pdf_new_string(csi.registry, strlen(csi.registry)));
843 pdf_add_dict(csi_dict,
844 pdf_new_name("Ordering"),
845 pdf_new_string(csi.ordering, strlen(csi.ordering)));
846 pdf_add_dict(csi_dict,
847 pdf_new_name("Supplement"),
848 pdf_new_number(csi.supplement));
849 pdf_add_dict(font->resource, pdf_new_name("CIDSystemInfo"), csi_dict);
850 }
851
852 return 0;
853 }
854
855 int
856 CIDFont_type0_open (pdf_font *font, const char *name, int index, cid_opt *opt)
857 {
858 CIDSysInfo csi;
859 char *fontname;
860 sfnt *sfont = NULL;
861 cff_font *cffont;
862 FILE *fp = NULL;
863 ULONG offset = 0;
864
865 ASSERT(font);
866
867 fp = DPXFOPEN(name, DPX_RES_TYPE_OTFONT);
868 if (!fp) {
869 fp = DPXFOPEN(name, DPX_RES_TYPE_TTFONT);
870 if (!fp) return -1;
871 }
872
873 sfont = sfnt_open(fp);
874 if (!sfont) {
875 DPXFCLOSE(fp);
876 return -1;
877 }
878
879 if (sfont->type == SFNT_TYPE_TTC)
880 offset = ttc_read_offset(sfont, index);
881
882 if ((sfont->type != SFNT_TYPE_TTC && sfont->type != SFNT_TYPE_POSTSCRIPT) ||
883 sfnt_read_table_directory(sfont, offset) < 0 ||
884 (offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) {
885 sfnt_close(sfont);
886 DPXFCLOSE(fp);
887 return -1;
888 }
889
890 cffont = cff_open(sfont->stream, offset, 0);
891 if (!cffont) {
892 WARN("Cannot read CFF font data");
893 sfnt_close(sfont);
894 DPXFCLOSE(fp);
895 return -1;
896 }
897
898 if (!(cffont->flag & FONTTYPE_CIDFONT)) {
899 cff_close(cffont);
900 sfnt_close(sfont);
901 DPXFCLOSE(fp);
902 return -1;
903 }
904
905 {
906 char *shortname;
907 int fontname_len = 8;
908
909 shortname = cff_get_name(cffont);
910 if (!shortname) {
911 WARN("No valid FontName found.");
912 cff_close(cffont);
913 sfnt_close(sfont);
914 DPXFCLOSE(fp);
915 return -1;
916 }
917 /*
918 * Mangled name requires more 7 bytes.
919 * Style requires more 11 bytes.
920 */
921 fontname_len += 11;
922 fontname = NEW(strlen(shortname) + fontname_len, char);
923 memset(fontname, 0, strlen(shortname) + fontname_len);
924 strcpy(fontname, shortname);
925 RELEASE(shortname);
926 }
927 csi.registry = cff_get_string(cffont, (s_SID)cff_dict_get(cffont->topdict, "ROS", 0));
928 csi.ordering = cff_get_string(cffont, (s_SID)cff_dict_get(cffont->topdict, "ROS", 1));
929 csi.supplement = (int)cff_dict_get(cffont->topdict, "ROS", 2);
930
931 cff_close(cffont);
932
933 if (opt->embed && opt->style != FONT_STYLE_NONE) {
934 WARN("Embedding disabled due to style option for %s.", name);
935 opt->embed = 0;
936
937 switch (opt->style) {
938 case FONT_STYLE_BOLD:
939 strcat(fontname, ",Bold");
940 break;
941 case FONT_STYLE_ITALIC:
942 strcat(fontname, ",Italic");
943 break;
944 case FONT_STYLE_BOLDITALIC:
945 strcat(fontname, ",BoldItalic");
946 break;
947 }
948 }
949
950 /* getting font info. from TrueType tables */
951 font->descriptor = tt_get_fontdesc(sfont, &(opt->embed), opt->stemv, 0, name);
952 if (!font->descriptor) {
953 WARN("Could not obtain necessary font info: %s", name);
954 RELEASE(fontname);
955 RELEASE(csi.registry);
956 RELEASE(csi.ordering);
957 sfnt_close(sfont);
958 DPXFCLOSE(fp);
959 return -1;
960 }
961
962 font->fontname = fontname;
963 font->subtype = PDF_FONT_FONTTYPE_CIDTYPE0;
964 font->cid.csi = csi;
965
966 font->resource = pdf_new_dict();
967 pdf_add_dict(font->resource,
968 pdf_new_name("Type"),
969 pdf_new_name("Font"));
970 pdf_add_dict(font->resource,
971 pdf_new_name("Subtype"),
972 pdf_new_name("CIDFontType0"));
973
974 if (opt->embed) {
975 char *tmp;
976
977 tmp = NEW(strlen(font->fontname)+8, char);
978 pdf_font_make_uniqueTag(font->uniqueID);
979 sprintf(tmp, "%s+%s", font->uniqueID, font->fontname);
980 pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(tmp));
981 pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(tmp));
982 RELEASE(tmp);
983 } else {
984 pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(font->fontname));
985 pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(font->fontname));
986 }
987 {
988 pdf_obj *csi_dict = pdf_new_dict();
989 pdf_add_dict(csi_dict,
990 pdf_new_name("Registry"),
991 pdf_new_string(csi.registry, strlen(csi.registry)));
992 pdf_add_dict(csi_dict,
993 pdf_new_name("Ordering"),
994 pdf_new_string(csi.ordering, strlen(csi.ordering)));
995 pdf_add_dict(csi_dict,
996 pdf_new_name("Supplement"),
997 pdf_new_number(csi.supplement));
998 pdf_add_dict(font->resource, pdf_new_name("CIDSystemInfo"), csi_dict);
999 }
1000 pdf_add_dict(font->resource,
1001 pdf_new_name("DW"),
1002 pdf_new_number(1000)); /* not sure */
1003
1004 sfnt_close(sfont);
1005 DPXFCLOSE(fp);
1006
1007 return 0;
1008 }
1009
1010 int
1011 CIDFont_type0_open_from_t1c (pdf_font *font, const char *name, int index, cid_opt *opt)
1012 {
1013 CIDSysInfo csi;
1014 char *fontname;
1015 sfnt *sfont = NULL;
1016 cff_font *cffont;
1017 FILE *fp = NULL;
1018 ULONG offset = 0;
1019
1020 ASSERT(font);
1021
1022 fp = DPXFOPEN(name, DPX_RES_TYPE_OTFONT);
1023 if (!fp) {
1024 fp = DPXFOPEN(name, DPX_RES_TYPE_TTFONT);
1025 if (!fp) return -1;
1026 }
1027
1028 sfont = sfnt_open(fp);
1029 if (!sfont) {
1030 WARN("Not a CFF/OpenType font: %s", name);
1031 DPXFCLOSE(fp);
1032 }
1033
1034 if (sfont->type == SFNT_TYPE_TTC)
1035 offset = ttc_read_offset(sfont, index);
1036
1037 if ((sfont->type != SFNT_TYPE_TTC && sfont->type != SFNT_TYPE_POSTSCRIPT) ||
1038 sfnt_read_table_directory(sfont, offset) < 0 ||
1039 (offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) {
1040 sfnt_close(sfont);
1041 DPXFCLOSE(fp);
1042 return -1;
1043 }
1044
1045 cffont = cff_open(sfont->stream, offset, 0);
1046 if (!cffont) {
1047 WARN("Cannot read CFF font data: %s", name);
1048 sfnt_close(sfont);
1049 DPXFCLOSE(fp);
1050 return -1;
1051 }
1052
1053 if (cffont->flag & FONTTYPE_CIDFONT) {
1054 cff_close(cffont);
1055 sfnt_close(sfont);
1056 DPXFCLOSE(fp);
1057 return -1;
1058 }
1059
1060 {
1061 char *shortname;
1062 int fontname_len = 8;
1063
1064 shortname = cff_get_name(cffont);
1065 if (!shortname) {
1066 WARN("No valid FontName found: %s", name);
1067 cff_close(cffont);
1068 sfnt_close(sfont);
1069 DPXFCLOSE(fp);
1070 return -1;
1071 }
1072 /*
1073 * Mangled name requires more 7 bytes.
1074 */
1075 fontname = NEW(strlen(shortname) + fontname_len, char);
1076 memset(fontname, 0, strlen(shortname) + fontname_len);
1077 strcpy(fontname, shortname);
1078 RELEASE(shortname);
1079 }
1080 csi.registry = NEW(strlen("Adobe") + 1, char);
1081 strcpy(csi.registry, "Adobe");
1082 csi.ordering = NEW(strlen("Identity") + 1, char);
1083 strcpy(csi.ordering, "Identity");
1084 csi.supplement = 0;
1085
1086 cff_close(cffont);
1087
1088 opt->embed = 1;
1089 /* getting font info. from TrueType tables */
1090 font->descriptor = tt_get_fontdesc(sfont, &(opt->embed), opt->stemv, 0, name);
1091 if (!font->descriptor) {
1092 WARN("Could not obtain necessary font info: %s", name);
1093 RELEASE(fontname);
1094 RELEASE(csi.registry);
1095 RELEASE(csi.ordering);
1096 sfnt_close(sfont);
1097 DPXFCLOSE(fp);
1098 return -1;
1099 } else if (!opt->embed) {
1100 WARN("Cannot embed font due to font license: %s", name);
1101 RELEASE(fontname);
1102 RELEASE(csi.registry);
1103 RELEASE(csi.ordering);
1104 sfnt_close(sfont);
1105 DPXFCLOSE(fp);
1106 return -1;
1107 }
1108
1109 font->fontname = fontname;
1110 font->subtype = PDF_FONT_FONTTYPE_CIDTYPE0;
1111 font->cid.csi = csi;
1112 font->flags |= CIDFONT_FLAG_TYPE1C;
1113
1114 font->resource = pdf_new_dict();
1115 pdf_add_dict(font->resource,
1116 pdf_new_name("Type"),
1117 pdf_new_name("Font"));
1118 pdf_add_dict(font->resource,
1119 pdf_new_name("Subtype"),
1120 pdf_new_name("CIDFontType0"));
1121
1122 if (opt->embed) {
1123 char *tmp;
1124
1125 tmp = NEW(strlen(font->fontname)+8, char);
1126 pdf_font_make_uniqueTag(font->uniqueID);
1127 sprintf(tmp, "%s+%s", font->uniqueID, font->fontname);
1128 pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(tmp));
1129 pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(tmp));
1130 RELEASE(tmp);
1131 } else {
1132 pdf_add_dict(font->descriptor, pdf_new_name("FontName"), pdf_new_name(font->fontname));
1133 pdf_add_dict(font->resource, pdf_new_name("BaseFont"), pdf_new_name(font->fontname));
1134 }
1135 {
1136 pdf_obj *csi_dict = pdf_new_dict();
1137 pdf_add_dict(csi_dict,
1138 pdf_new_name("Registry"),
1139 pdf_new_string(csi.registry, strlen(csi.registry)));
1140 pdf_add_dict(csi_dict,
1141 pdf_new_name("Ordering"),
1142 pdf_new_string(csi.ordering, strlen(csi.ordering)));
1143 pdf_add_dict(csi_dict,
1144 pdf_new_name("Supplement"),
1145 pdf_new_number(csi.supplement));
1146 pdf_add_dict(font->resource, pdf_new_name("CIDSystemInfo"), csi_dict);
1147 }
1148
1149 sfnt_close(sfont);
1150 DPXFCLOSE(fp);
1151
1152 return 0;
1153 }
1154
1155 int
1156 CIDFont_type0_t1cdofont (pdf_font *font)
1157 {
1158 FILE *fp;
1159 sfnt *sfont;
1160 cff_font *cffont;
1161 cff_index *charstrings, *idx;
1162 int charstring_len, max_len;
1163 int destlen = 0;
1164 int size, offset = 0;
1165 card8 *data;
1166 card16 num_glyphs, gid, last_cid;
1167 int i, cid;
1168 char *used_chars;
1169 double default_width, nominal_width;
1170
1171 ASSERT(font);
1172
1173 if (!font->reference)
1174 return 0;
1175
1176 pdf_add_dict(font->resource,
1177 pdf_new_name("FontDescriptor"),
1178 pdf_ref_obj (font->descriptor));
1179
1180 used_chars = font->usedchars;
1181
1182 fp = DPXFOPEN(font->filename, DPX_RES_TYPE_OTFONT);
1183 if (!fp) {
1184 fp = DPXFOPEN(font->filename, DPX_RES_TYPE_TTFONT);
1185 if (!fp) {
1186 WARN("Could not open file: %s", font->filename);
1187 return -1;
1188 }
1189 }
1190
1191 sfont = sfnt_open(fp);
1192 if (!sfont) {
1193 WARN("Failed to read font data: %s", font->filename);
1194 DPXFCLOSE(fp);
1195 return -1;
1196 }
1197
1198 if (sfont->type == SFNT_TYPE_TTC)
1199 offset = ttc_read_offset(sfont, font->index);
1200
1201 if ((sfont->type != SFNT_TYPE_TTC &&
1202 sfont->type != SFNT_TYPE_POSTSCRIPT) ||
1203 sfnt_read_table_directory(sfont, offset) < 0 ||
1204 (offset = sfnt_find_table_pos(sfont, "CFF ")) == 0) {
1205 WARN("Not a CFF/OpenType font: %s", font->filename);
1206 sfnt_close(sfont);
1207 DPXFCLOSE(fp);
1208 return -1;
1209 }
1210
1211 cffont = cff_open(sfont->stream, offset, 0);
1212 if (!cffont) {
1213 WARN("Failed to read CFF font data: %s", font->filename);
1214 sfnt_close(sfont);
1215 DPXFCLOSE(fp);
1216 return -1;
1217 }
1218
1219 if (cffont->flag & FONTTYPE_CIDFONT) {
1220 WARN("Unxpected type (CIDFont) found: %s", font->filename);
1221 cff_close(cffont);
1222 sfnt_close(sfont);
1223 DPXFCLOSE(fp);
1224 return -1;
1225 }
1226
1227 cff_read_private(cffont);
1228 cff_read_subrs (cffont);
1229
1230 if (cffont->private[0] && cff_dict_known(cffont->private[0], "StdVW")) {
1231 double stemv;
1232 stemv = cff_dict_get(cffont->private[0], "StdVW", 0);
1233 pdf_add_dict(font->descriptor,
1234 pdf_new_name("StemV"), pdf_new_number(stemv));
1235 }
1236 if (cffont->private[0] && cff_dict_known(cffont->private[0], "defaultWidthX")) {
1237 default_width = (double) cff_dict_get(cffont->private[0], "defaultWidthX", 0);
1238 } else {
1239 default_width = CFF_DEFAULTWIDTHX_DEFAULT;
1240 }
1241 if (cffont->private[0] && cff_dict_known(cffont->private[0], "nominalWidthX")) {
1242 nominal_width = (double) cff_dict_get(cffont->private[0], "nominalWidthX", 0);
1243 } else {
1244 nominal_width = CFF_NOMINALWIDTHX_DEFAULT;
1245 }
1246
1247 num_glyphs = 0; last_cid = 0;
1248 add_to_used_chars2(used_chars, 0); /* .notdef */
1249 for (i = 0; i < (cffont->num_glyphs + 7)/8; i++) {
1250 int c, j;
1251
1252 c = used_chars[i];
1253 for (j = 7; j >= 0; j--) {
1254 if (c & (1 << j)) {
1255 num_glyphs++;
1256 last_cid = (i + 1) * 8 - j - 1;
1257 }
1258 }
1259 }
1260
1261 {
1262 cff_fdselect *fdselect;
1263
1264 fdselect = NEW(1, cff_fdselect);
1265 fdselect->format = 3;
1266 fdselect->num_entries = 1;
1267 fdselect->data.ranges = NEW(1, cff_range3);
1268 fdselect->data.ranges[0].first = 0;
1269 fdselect->data.ranges[0].fd = 0;
1270 cffont->fdselect = fdselect;
1271 }
1272
1273 {
1274 cff_charsets *charset;
1275
1276 charset = NEW(1, cff_charsets);
1277 charset->format = 0;
1278 charset->num_entries = num_glyphs-1;
1279 charset->data.glyphs = NEW(num_glyphs-1, s_SID);
1280
1281 for (gid = 0, cid = 0; cid <= last_cid; cid++) {
1282 if (is_used_char2(used_chars, cid)) {
1283 if (gid > 0)
1284 charset->data.glyphs[gid-1] = cid;
1285 gid++;
1286 }
1287 }
1288 /* cff_release_charsets(cffont->charsets); */
1289 cffont->charsets = charset;
1290 }
1291
1292 cff_dict_add(cffont->topdict, "CIDCount", 1);
1293 cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1);
1294
1295 cffont->fdarray = NEW(1, cff_dict *);
1296 cffont->fdarray[0] = cff_new_dict();
1297 cff_dict_add(cffont->fdarray[0], "FontName", 1);
1298 cff_dict_set(cffont->fdarray[0], "FontName", 0,
1299 (double) cff_add_string(cffont, font->fontname + 7, 1)); /* FIXME: Skip XXXXXX+ */
1300 cff_dict_add(cffont->fdarray[0], "Private", 2);
1301 cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
1302 cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
1303 /* FDArray - index offset, not known yet */
1304 cff_dict_add(cffont->topdict, "FDArray", 1);
1305 cff_dict_set(cffont->topdict, "FDArray", 0, 0.0);
1306 /* FDSelect - offset, not known yet */
1307 cff_dict_add(cffont->topdict, "FDSelect", 1);
1308 cff_dict_set(cffont->topdict, "FDSelect", 0, 0.0);
1309
1310 cff_dict_remove(cffont->topdict, "UniqueID");
1311 cff_dict_remove(cffont->topdict, "XUID");
1312 cff_dict_remove(cffont->topdict, "Private");
1313 cff_dict_remove(cffont->topdict, "Encoding");
1314
1315
1316 /* */
1317 offset = (int) cff_dict_get(cffont->topdict, "CharStrings", 0);
1318 cff_seek_set(cffont, offset);
1319 idx = cff_get_index_header(cffont);
1320 /* offset is now absolute offset ... bad */
1321 offset = cff_tell(cffont);
1322
1323 if (idx->count < 2) {
1324 WARN("No valid charstring data found: %s", font->filename);
1325 cff_release_index(idx);
1326 cff_close(cffont);
1327 sfnt_close(sfont);
1328 DPXFCLOSE(fp);
1329 return -1;
1330 }
1331
1332 /* New CharStrings INDEX */
1333 charstrings = cff_new_index((card16)(num_glyphs+1));
1334 max_len = 2 * CS_STR_LEN_MAX;
1335 charstrings->data = NEW(max_len, card8);
1336 charstring_len = 0;
1337
1338 gid = 0;
1339 data = NEW(CS_STR_LEN_MAX, card8);
1340 for (cid = 0; cid <= last_cid; cid++) {
1341 if (!is_used_char2(used_chars, cid))
1342 continue;
1343
1344 if ((size = (idx->offset)[cid+1] - (idx->offset)[cid])
1345 > CS_STR_LEN_MAX) {
1346 WARN("Charstring too long:%s (gid=%u)", font->filename, cid);
1347 RELEASE(data);
1348 cff_release_index(charstrings);
1349 cff_release_index(idx);
1350 cff_close(cffont);
1351 sfnt_close(sfont);
1352 DPXFCLOSE(fp);
1353 return -1;
1354 }
1355 if (charstring_len + CS_STR_LEN_MAX >= max_len) {
1356 max_len = charstring_len + 2 * CS_STR_LEN_MAX;
1357 charstrings->data = RENEW(charstrings->data, max_len, card8);
1358 }
1359 (charstrings->offset)[gid] = charstring_len + 1;
1360 cff_seek(cffont, offset + (idx->offset)[cid] - 1);
1361 cff_read_data(data, size, cffont);
1362 charstring_len += cs_copy_charstring(charstrings->data + charstring_len,
1363 max_len - charstring_len,
1364 data, size,
1365 cffont->gsubr, (cffont->subrs)[0],
1366 default_width, nominal_width, NULL);
1367 gid++;
1368 }
1369 if (gid != num_glyphs) {
1370 WARN("Unexpeced error: %s", font->filename);
1371 RELEASE(data);
1372 cff_release_index(charstrings);
1373 cff_release_index(idx);
1374 cff_close(cffont);
1375 sfnt_close(sfont);
1376 DPXFCLOSE(fp);
1377 return -1;
1378 }
1379 RELEASE(data);
1380 cff_release_index(idx);
1381
1382 (charstrings->offset)[num_glyphs] = charstring_len + 1;
1383 charstrings->count = num_glyphs;
1384 cffont->num_glyphs = num_glyphs;
1385 cffont->cstrings = charstrings;
1386
1387 /* no Global subr */
1388 if (cffont->gsubr)
1389 cff_release_index(cffont->gsubr);
1390 cffont->gsubr = cff_new_index(0);
1391
1392 if (cffont->subrs && cffont->subrs[0]) {
1393 cff_release_index(cffont->subrs[0]);
1394 cffont->subrs[0] = NULL;
1395 }
1396 if (cffont->private && (cffont->private)[0]) {
1397 cff_dict_remove((cffont->private)[0], "Subrs"); /* no Subrs */
1398 }
1399
1400 cff_add_string(cffont, "Adobe", 1);
1401 cff_add_string(cffont, "Identity", 1);
1402
1403 cff_dict_update(cffont->topdict, cffont);
1404
1405 if (cffont->private && cffont->private[0]) {
1406 cff_dict_update(cffont->private[0], cffont);
1407 }
1408
1409 cff_update_string(cffont);
1410
1411 /* CFF code need to be rewrote... */
1412 cff_dict_add(cffont->topdict, "ROS", 3);
1413 cff_dict_set(cffont->topdict, "ROS", 0,
1414 (double) cff_get_sid(cffont, "Adobe"));
1415 cff_dict_set(cffont->topdict, "ROS", 1,
1416 (double) cff_get_sid(cffont, "Identity"));
1417 cff_dict_set(cffont->topdict, "ROS", 2, 0.0);
1418
1419 destlen = write_fontfile(font, cffont);
1420
1421 /*
1422 * DW, W, DW2 and W2:
1423 * Those values are obtained from OpenType table (not TFM).
1424 */
1425 {
1426 unsigned char *CIDToGIDMap;
1427
1428 CIDToGIDMap = NEW(2 * (last_cid+1), unsigned char);
1429 memset(CIDToGIDMap, 0, 2 * (last_cid + 1));
1430 for (cid = 0; cid <= last_cid; cid++) {
1431 if (is_used_char2(used_chars, cid)) {
1432 CIDToGIDMap[2*cid ] = (cid >> 8) & 0xff;
1433 CIDToGIDMap[2*cid+1] = cid & 0xff;
1434 }
1435 }
1436 add_CIDMetrics(sfont, font->resource, CIDToGIDMap, last_cid,
1437 font->cid.need_vmetrics ? 1 : 0);
1438 RELEASE(CIDToGIDMap);
1439 }
1440
1441 cff_close(cffont);
1442 sfnt_close(sfont);
1443 DPXFCLOSE(fp);
1444
1445 if (dpx_conf.verbose_level > 1)
1446 MESG("[%u glyphs][%ld bytes]", num_glyphs, destlen);
1447
1448 if (pdf_check_version(2, 0) < 0) {
1449 CIDFont_type0_add_CIDSet(font, used_chars, last_cid);
1450 }
1451
1452 return 0;
1453 }
1454
1455 static int
1456 load_base_CMap (const char *font_name, int wmode, cff_font *cffont)
1457 {
1458 int cmap_id = -1;
1459 CMap *cmap;
1460 char *cmap_name;
1461 card16 gid;
1462 unsigned char range_min[4] = {0x00, 0x00, 0x00, 0x00};
1463 unsigned char range_max[4] = {0x7f, 0xff, 0xff, 0xff};
1464
1465 cmap_name = NEW(strlen(font_name)+strlen("-UCS4-H")+1, char);
1466 if (wmode) {
1467 sprintf(cmap_name, "%s-UCS4-V", font_name);
1468 } else {
1469 sprintf(cmap_name, "%s-UCS4-H", font_name);
1470 }
1471
1472 cmap_id = CMap_cache_find(cmap_name);
1473 if (cmap_id >= 0) {
1474 RELEASE(cmap_name);
1475 return cmap_id;
1476 }
1477
1478 cmap = CMap_new();
1479 CMap_set_name (cmap, cmap_name);
1480 CMap_set_type (cmap, CMAP_TYPE_CODE_TO_CID);
1481 CMap_set_wmode(cmap, wmode);
1482 CMap_add_codespacerange(cmap, range_min, range_max, 4);
1483 CMap_set_CIDSysInfo(cmap, &CSI_IDENTITY);
1484 RELEASE(cmap_name);
1485
1486 for (gid = 1; gid < cffont->num_glyphs; gid++) {
1487 int32_t ucv;
1488 s_SID sid;
1489 char *glyph, *name, *suffix;
1490 unsigned char srcCode[4];
1491
1492 sid = cff_charsets_lookup_inverse(cffont, gid);
1493 glyph = cff_get_string (cffont, sid);
1494
1495 name = agl_chop_suffix(glyph, &suffix);
1496 if (!name) {
1497 if (suffix)
1498 RELEASE(suffix);
1499 RELEASE(glyph);
1500 continue;
1501 }
1502
1503 if (suffix) {
1504 RELEASE(name);
1505 RELEASE(suffix);
1506 RELEASE(glyph);
1507 continue;
1508 }
1509
1510 if (agl_name_is_unicode(name)) {
1511 ucv = agl_name_convert_unicode(name);
1512 srcCode[0] = (ucv >> 24) & 0xff;
1513 srcCode[1] = (ucv >> 16) & 0xff;
1514 srcCode[2] = (ucv >> 8) & 0xff;
1515 srcCode[3] = ucv & 0xff;
1516 CMap_add_cidchar(cmap, srcCode, 4, gid);
1517 } else {
1518 agl_name *agln;
1519
1520 agln = agl_lookup_list(name);
1521 if (!agln)
1522 WARN("Glyph \"%s\" inaccessible (no Unicode mapping)", glyph);
1523 while (agln) {
1524 if (agln->n_components > 1) {
1525 WARN("Glyph \"%s\" inaccessible (composite character)", glyph);
1526 } else if (agln->n_components == 1) {
1527 ucv = agln->unicodes[0];
1528 srcCode[0] = (ucv >> 24) & 0xff;
1529 srcCode[1] = (ucv >> 16) & 0xff;
1530 srcCode[2] = (ucv >> 8) & 0xff;
1531 srcCode[3] = ucv & 0xff;
1532 CMap_add_cidchar(cmap, srcCode, 4, gid);
1533 }
1534 agln = agln->alternate;
1535 }
1536 }
1537 RELEASE(name);
1538 if (suffix)
1539 RELEASE(suffix);
1540 RELEASE(glyph);
1541 }
1542 cmap_id = CMap_cache_add(cmap);
1543
1544 return cmap_id;
1545 }
1546
1547 int
1548 t1_load_UnicodeCMap (const char *font_name,
1549 const char *otl_tags, /* not supported yet */
1550 int wmode)
1551 {
1552 int cmap_id = -1;
1553 cff_font *cffont;
1554 FILE *fp;
1555
1556 if (!font_name)
1557 return -1;
1558
1559 fp = DPXFOPEN(font_name, DPX_RES_TYPE_T1FONT);
1560 if (!fp)
1561 return -1;
1562
1563 cffont = t1_load_font(NULL, 1, fp);
1564 DPXFCLOSE(fp);
1565 if (!cffont)
1566 return -1;
1567
1568 cmap_id = load_base_CMap(font_name, wmode, cffont);
1569
1570 cff_close(cffont);
1571
1572 if (cmap_id < 0) {
1573 WARN("Failed to create Unicode charmap for font \"%s\".", font_name);
1574 return -1;
1575 }
1576
1577 if (otl_tags) {
1578 WARN("Glyph substitution not supported for Type1 font yet...");
1579 }
1580
1581 return cmap_id;
1582 }
1583
1584
1585 /*
1586 * ToUnicode CMap
1587 */
1588
1589 static pdf_obj *
1590 create_ToUnicode_stream (cff_font *cffont,
1591 const char *font_name, const char *used_glyphs)
1592 {
1593 pdf_obj *stream = NULL;
1594 CMap *cmap;
1595 CID cid;
1596 card16 gid;
1597 int glyph_count, total_fail_count;
1598 char *cmap_name;
1599 #define WBUF_SIZE 1024
1600 unsigned char wbuf[WBUF_SIZE];
1601 unsigned char *p, *endptr;
1602 static unsigned char range_min[2] = {0x00, 0x00};
1603 static unsigned char range_max[2] = {0xff, 0xff};
1604
1605 if (!font_name || !used_glyphs)
1606 return NULL;
1607
1608 cmap = CMap_new();
1609
1610 cmap_name = NEW(strlen(font_name)+strlen("-UTF16")+1, char);
1611 strcpy(cmap_name, font_name);
1612 strcat(cmap_name, "-UTF16");
1613 CMap_set_name (cmap, cmap_name);
1614 RELEASE(cmap_name);
1615
1616 CMap_set_wmode(cmap, 0);
1617 CMap_set_type (cmap, CMAP_TYPE_TO_UNICODE);
1618 CMap_set_CIDSysInfo(cmap, &CSI_UNICODE);
1619
1620 CMap_add_codespacerange(cmap, range_min, range_max, 2);
1621
1622 glyph_count = total_fail_count = 0;
1623 p = wbuf;
1624 endptr = wbuf + WBUF_SIZE;
1625 for (cid = 1; cid < cffont->num_glyphs; cid++) { /* Skip .notdef */
1626 if (is_used_char2(used_glyphs, cid)) {
1627 char *glyph;
1628 int32_t len;
1629 int fail_count;
1630
1631 wbuf[0] = (cid >> 8) & 0xff;
1632 wbuf[1] = (cid & 0xff);
1633
1634 p = wbuf + 2;
1635 gid = cff_charsets_lookup_inverse(cffont, cid);
1636 if (gid == 0)
1637 continue;
1638 glyph = cff_get_string(cffont, gid);
1639 if (glyph) {
1640 len = agl_sput_UTF16BE(glyph, &p, endptr, &fail_count);
1641 if (len < 1 || fail_count) {
1642 total_fail_count += fail_count;
1643 } else {
1644 CMap_add_bfchar(cmap, wbuf, 2, wbuf+2, len);
1645 }
1646 RELEASE(glyph);
1647 }
1648 glyph_count++;
1649 }
1650 }
1651
1652 if (total_fail_count != 0 &&
1653 total_fail_count >= glyph_count/10) {
1654 WARN("%d glyph names (out of %d) missing Unicode mapping.",
1655 total_fail_count, glyph_count);
1656 WARN("ToUnicode CMap \"%s-UTF16\" removed.", font_name);
1657 } else {
1658 stream = CMap_create_stream(cmap);
1659 }
1660 CMap_release(cmap);
1661
1662 return stream;
1663 }
1664
1665 pdf_obj *
1666 CIDFont_type0_t1create_ToUnicode_stream (const char *filename, const char *fontname, const char *used_chars)
1667 {
1668 pdf_obj *ref = NULL;
1669 pdf_obj *tounicode;
1670 cff_font *cffont;
1671 FILE *fp;
1672
1673 ASSERT(filename);
1674 ASSERT(fontname);
1675 ASSERT(used_chars);
1676
1677 fp = DPXFOPEN(filename, DPX_RES_TYPE_T1FONT);
1678 if (!fp) {
1679 return NULL;
1680 }
1681
1682 cffont = t1_load_font(NULL, 1, fp);
1683 if (cffont) {
1684 tounicode = create_ToUnicode_stream(cffont, fontname, used_chars);
1685 if (tounicode) {
1686 ref = pdf_ref_obj(tounicode);
1687 pdf_release_obj(tounicode);
1688 }
1689 }
1690 DPXFCLOSE(fp);
1691
1692 return ref;
1693 }
1694
1695 /* Duplicate from type1.c */
1696 #define TYPE1_NAME_LEN_MAX 127
1697
1698 #define FONT_FLAG_FIXEDPITCH (1 << 0) /* Fixed-width font */
1699 #define FONT_FLAG_SERIF (1 << 1) /* Serif font */
1700 #define FONT_FLAG_SYMBOLIC (1 << 2) /* Symbolic font */
1701 #define FONT_FLAG_SCRIPT (1 << 3) /* Script font */
1702 #define FONT_FLAG_STANDARD (1 << 5) /* Adobe Standard Character Set */
1703 #define FONT_FLAG_ITALIC (1 << 6) /* Italic */
1704 #define FONT_FLAG_ALLCAP (1 << 16) /* All-cap font */
1705 #define FONT_FLAG_SMALLCAP (1 << 17) /* Small-cap font */
1706 #define FONT_FLAG_FORCEBOLD (1 << 18) /* Force bold at small text sizes */
1707
1708 static void
1709 get_font_attr (pdf_font *font, cff_font *cffont)
1710 {
1711 double capheight, ascent, descent;
1712 double italicangle, stemv;
1713 double defaultwidth, nominalwidth;
1714 int flags = 0;
1715 int gid;
1716 int i;
1717 static const char *L_c[] = {
1718 "H", "P", "Pi", "Rho", NULL
1719 };
1720 static const char *L_d[] = {
1721 "p", "q", "mu", "eta", NULL
1722 };
1723 static const char *L_a[] = {
1724 "b", "h", "lambda", NULL
1725 };
1726 t1_ginfo gm;
1727
1728 defaultwidth = 500.0;
1729 nominalwidth = 0.0;
1730
1731 /*
1732 * CapHeight, Ascent, and Descent is meaningfull only for Latin/Greek/Cyrillic.
1733 * The BlueValues and OtherBlues also have those information.
1734 */
1735 if (cff_dict_known(cffont->topdict, "FontBBox")) {
1736 /* Default values */
1737 capheight = ascent = cff_dict_get(cffont->topdict, "FontBBox", 3);
1738 descent = cff_dict_get(cffont->topdict, "FontBBox", 1);
1739 } else {
1740 capheight = 680.0;
1741 ascent = 690.0;
1742 descent = -190.0;
1743 }
1744 if (cff_dict_known(cffont->private[0], "StdVW")) {
1745 stemv = cff_dict_get(cffont->private[0], "StdVW", 0);
1746 } else {
1747 /*
1748 * We may use the following values for StemV:
1749 * Thin - ExtraLight: <= 50
1750 * Light: 71
1751 * Regular(Normal): 88
1752 * Medium: 109
1753 * SemiBold(DemiBold): 135
1754 * Bold - Heavy: >= 166
1755 */
1756 stemv = 88.0;
1757 }
1758 if (cff_dict_known(cffont->topdict, "ItalicAngle")) {
1759 italicangle = cff_dict_get(cffont->topdict, "ItalicAngle", 0);
1760 if (italicangle != 0.0)
1761 flags |= FONT_FLAG_ITALIC;
1762 } else {
1763 italicangle = 0.0;
1764 }
1765
1766 /*
1767 * Use "space", "H", "p", and "b" for various values.
1768 * Those characters should not "seac". (no accent)
1769 */
1770 gid = cff_glyph_lookup(cffont, "space");
1771 if (gid >= 0 && gid < cffont->cstrings->count) {
1772 t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1,
1773 cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid],
1774 cffont->subrs[0], &gm);
1775 defaultwidth = gm.wx;
1776 }
1777
1778 for (i = 0; L_c[i] != NULL; i++) {
1779 gid = cff_glyph_lookup(cffont, L_c[i]);
1780 if (gid >= 0 && gid < cffont->cstrings->count) {
1781 t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1,
1782 cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid],
1783 cffont->subrs[0], &gm);
1784 capheight = gm.bbox.ury;
1785 break;
1786 }
1787 }
1788
1789 for (i = 0; L_d[i] != NULL; i++) {
1790 gid = cff_glyph_lookup(cffont, L_d[i]);
1791 if (gid >= 0 && gid < cffont->cstrings->count) {
1792 t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1,
1793 cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid],
1794 cffont->subrs[0], &gm);
1795 descent = gm.bbox.lly;
1796 break;
1797 }
1798 }
1799
1800 for (i = 0; L_a[i] != NULL; i++) {
1801 gid = cff_glyph_lookup(cffont, L_a[i]);
1802 if (gid >= 0 && gid < cffont->cstrings->count) {
1803 t1char_get_metrics(cffont->cstrings->data + cffont->cstrings->offset[gid] - 1,
1804 cffont->cstrings->offset[gid+1] - cffont->cstrings->offset[gid],
1805 cffont->subrs[0], &gm);
1806 ascent = gm.bbox.ury;
1807 break;
1808 }
1809 }
1810
1811 if (defaultwidth != 0.0) {
1812 cff_dict_add(cffont->private[0], "defaultWidthX", 1);
1813 cff_dict_set(cffont->private[0], "defaultWidthX", 0, defaultwidth);
1814 }
1815 if (nominalwidth != 0.0) {
1816 cff_dict_add(cffont->private[0], "nominalWidthX", 1);
1817 cff_dict_set(cffont->private[0], "nominalWidthX", 0, nominalwidth);
1818 }
1819 if (cff_dict_known(cffont->private[0], "ForceBold") &&
1820 cff_dict_get(cffont->private[0], "ForceBold", 0)) {
1821 flags |= FONT_FLAG_FORCEBOLD;
1822 }
1823 if (cff_dict_known(cffont->private[0], "IsFixedPitch") &&
1824 cff_dict_get(cffont->private[0], "IsFixedPitch", 0)) {
1825 flags |= FONT_FLAG_FIXEDPITCH;
1826 }
1827 if (font->fontname &&
1828 !strstr(font->fontname, "Sans")) {
1829 flags |= FONT_FLAG_SERIF;
1830 }
1831 flags |= FONT_FLAG_SYMBOLIC;
1832
1833 pdf_add_dict(font->descriptor,
1834 pdf_new_name("CapHeight"), pdf_new_number(capheight));
1835 pdf_add_dict(font->descriptor,
1836 pdf_new_name("Ascent"), pdf_new_number(ascent));
1837 pdf_add_dict(font->descriptor,
1838 pdf_new_name("Descent"), pdf_new_number(descent));
1839 pdf_add_dict(font->descriptor,
1840 pdf_new_name("ItalicAngle"), pdf_new_number(italicangle));
1841 pdf_add_dict(font->descriptor,
1842 pdf_new_name("StemV"), pdf_new_number(stemv));
1843 pdf_add_dict(font->descriptor,
1844 pdf_new_name("Flags"), pdf_new_number(flags));
1845 }
1846
1847 static void
1848 add_metrics (pdf_font *font, cff_font *cffont,
1849 unsigned char *CIDToGIDMap,
1850 double *widths, double default_width, CID last_cid)
1851 {
1852 pdf_obj *tmp;
1853 double val;
1854 card16 cid, gid;
1855 char *used_chars;
1856 int i;
1857
1858 /*
1859 * The original FontBBox of the font is preserved, instead
1860 * of replacing it with tight bounding box calculated from
1861 * charstrings, to prevent Acrobat 4 from greeking text as
1862 * much as possible.
1863 */
1864 if (!cff_dict_known(cffont->topdict, "FontBBox")) {
1865 WARN("No FontBBox found: %s", font->filename);
1866 } else {
1867 tmp = pdf_new_array();
1868 for (i = 0; i < 4; i++) {
1869 val = cff_dict_get(cffont->topdict, "FontBBox", i);
1870 pdf_add_array(tmp, pdf_new_number(ROUND(val, 1.0)));
1871 }
1872 pdf_add_dict(font->descriptor, pdf_new_name("FontBBox"), tmp);
1873 }
1874
1875 used_chars = font->usedchars;
1876
1877 /* FIXME:
1878 * This writes "CID CID width".
1879 * I think it's better to handle each 8 char block
1880 * and to use "CID_start [ w0 w1 ...]".
1881 */
1882 tmp = pdf_new_array();
1883 for (cid = 0; cid <= last_cid; cid++) {
1884 if (is_used_char2(used_chars, cid)) {
1885 gid = (CIDToGIDMap[2*cid] << 8)|CIDToGIDMap[2*cid+1];
1886 if (widths[gid] != default_width) {
1887 pdf_add_array(tmp, pdf_new_number(cid));
1888 pdf_add_array(tmp, pdf_new_number(cid));
1889 pdf_add_array(tmp, pdf_new_number(ROUND(widths[gid], 1.0)));
1890 }
1891 }
1892 }
1893 pdf_add_dict(font->resource,
1894 pdf_new_name("DW"), pdf_new_number(default_width));
1895 if (pdf_array_length(tmp) > 0) {
1896 pdf_add_dict(font->resource, pdf_new_name("W"), pdf_ref_obj(tmp));
1897 }
1898 pdf_release_obj(tmp);
1899 }
1900
1901 int
1902 CIDFont_type0_t1dofont (pdf_font *font)
1903 {
1904 cff_font *cffont;
1905 double defaultwidth, nominalwidth;
1906 int num_glyphs = 0;
1907 FILE *fp;
1908 int i, offset;
1909 char *used_chars = NULL;
1910 card16 last_cid, gid, cid;
1911 unsigned char *CIDToGIDMap;
1912
1913 ASSERT(font);
1914
1915 if (!font->reference) {
1916 return 0;
1917 }
1918
1919 pdf_add_dict(font->resource,
1920 pdf_new_name("FontDescriptor"),
1921 pdf_ref_obj (font->descriptor));
1922
1923 fp = DPXFOPEN(font->filename, DPX_RES_TYPE_T1FONT);
1924 if (!fp) {
1925 WARN("Type1: Could not open Type1 font.");
1926 return -1;
1927 }
1928
1929 cffont = t1_load_font(NULL, 0, fp);
1930 if (!cffont) {
1931 WARN("Could not read Type 1 font...");
1932 DPXFCLOSE(fp);
1933 return -1;
1934 }
1935 DPXFCLOSE(fp);
1936
1937 ASSERT(font->fontname);
1938 ASSERT(font->usedchars);
1939
1940 used_chars = font->usedchars;
1941
1942 cff_set_name(cffont, font->fontname);
1943
1944 /* defaultWidthX, CapHeight, etc. */
1945 get_font_attr(font, cffont);
1946 if (cff_dict_known(cffont->private[0], "defaultWidthX")) {
1947 defaultwidth = cff_dict_get(cffont->private[0], "defaultWidthX", 0);
1948 } else {
1949 defaultwidth = 0.0;
1950 }
1951 if (cff_dict_known(cffont->private[0], "nominalWidthX")) {
1952 nominalwidth = cff_dict_get(cffont->private[0], "nominalWidthX", 0);
1953 } else {
1954 nominalwidth = 0.0;
1955 }
1956
1957 num_glyphs = 0; last_cid = 0;
1958 add_to_used_chars2(used_chars, 0); /* .notdef */
1959 for (i = 0; i < (cffont->num_glyphs + 7)/8; i++) {
1960 int c, j;
1961
1962 c = used_chars[i];
1963 for (j = 7; j >= 0; j--) {
1964 if (c & (1 << j)) {
1965 num_glyphs++;
1966 last_cid = (i + 1) * 8 - j - 1;
1967 }
1968 }
1969 }
1970
1971 {
1972 cff_fdselect *fdselect;
1973
1974 fdselect = NEW(1, cff_fdselect);
1975 fdselect->format = 3;
1976 fdselect->num_entries = 1;
1977 fdselect->data.ranges = NEW(1, cff_range3);
1978 fdselect->data.ranges[0].first = 0;
1979 fdselect->data.ranges[0].fd = 0;
1980 cffont->fdselect = fdselect;
1981 }
1982
1983 CIDToGIDMap = NEW(2*(last_cid+1), unsigned char);
1984 memset(CIDToGIDMap, 0, 2*(last_cid+1));
1985 {
1986 cff_charsets *charset;
1987
1988 charset = NEW(1, cff_charsets);
1989 charset->format = 0;
1990 charset->num_entries = num_glyphs-1;
1991 charset->data.glyphs = NEW(num_glyphs-1, s_SID);
1992
1993 for (gid = 0, cid = 0; cid <= last_cid; cid++) {
1994 if (is_used_char2(used_chars, cid)) {
1995 if (gid > 0)
1996 charset->data.glyphs[gid-1] = cid;
1997 CIDToGIDMap[2*cid ] = (gid >> 8) & 0xff;
1998 CIDToGIDMap[2*cid+1] = gid & 0xff;
1999 gid++;
2000 }
2001 }
2002
2003 cff_release_charsets(cffont->charsets);
2004 cffont->charsets = charset;
2005 }
2006
2007 cff_dict_add(cffont->topdict, "CIDCount", 1);
2008 cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1);
2009
2010 cffont->fdarray = NEW(1, cff_dict *);
2011 cffont->fdarray[0] = cff_new_dict();
2012 cff_dict_add(cffont->fdarray[0], "FontName", 1);
2013 cff_dict_set(cffont->fdarray[0], "FontName", 0,
2014 (double) cff_add_string(cffont, font->fontname + 7, 1)); /* FIXME: Skip XXXXXX+ */
2015 cff_dict_add(cffont->fdarray[0], "Private", 2);
2016 cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
2017 cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
2018
2019 /* FDArray - index offset, not known yet */
2020 cff_dict_add(cffont->topdict, "FDArray", 1);
2021 cff_dict_set(cffont->topdict, "FDArray", 0, 0.0);
2022 /* FDSelect - offset, not known yet */
2023 cff_dict_add(cffont->topdict, "FDSelect", 1);
2024 cff_dict_set(cffont->topdict, "FDSelect", 0, 0.0);
2025
2026 cff_dict_add(cffont->topdict, "charset", 1);
2027 cff_dict_set(cffont->topdict, "charset", 0, 0.0);
2028
2029 cff_dict_add(cffont->topdict, "CharStrings", 1);
2030 cff_dict_set(cffont->topdict, "CharStrings", 0, 0.0);
2031
2032 {
2033 cff_index *cstring;
2034 t1_ginfo gm;
2035 int max = 0;
2036 double *widths;
2037 int w_stat[1001], max_count, dw;
2038
2039 widths = NEW(num_glyphs, double);
2040 memset(w_stat, 0, sizeof(int)*1001);
2041 offset = 0L;
2042 cstring = cff_new_index((card16)num_glyphs);
2043 cstring->data = NULL;
2044 cstring->offset[0] = 1;
2045 gid = 0;
2046 for (cid = 0; cid <= last_cid; cid++) {
2047 if (!is_used_char2(used_chars, cid))
2048 continue;
2049
2050 if (offset + CS_STR_LEN_MAX >= max) {
2051 max += CS_STR_LEN_MAX*2;
2052 cstring->data = RENEW(cstring->data, max, card8);
2053 }
2054 offset += t1char_convert_charstring(cstring->data + cstring->offset[gid] - 1, CS_STR_LEN_MAX,
2055 cffont->cstrings->data + cffont->cstrings->offset[cid] - 1,
2056 cffont->cstrings->offset[cid+1] - cffont->cstrings->offset[cid],
2057 cffont->subrs[0], defaultwidth, nominalwidth, &gm);
2058 cstring->offset[gid+1] = offset + 1;
2059 if (gm.use_seac) {
2060 WARN("The \"seac\" command for an accented character found: %s", font->filename);
2061 RELEASE(widths);
2062 cff_release_index(cstring);
2063 RELEASE(CIDToGIDMap);
2064 cff_close(cffont);
2065 return -1;
2066 }
2067 widths[gid] = gm.wx;
2068 if (gm.wx >= 0.0 && gm.wx <= 1000.0) {
2069 w_stat[((int) gm.wx)] += 1;
2070 }
2071 gid++;
2072 }
2073
2074 cff_release_index(cffont->cstrings);
2075 cffont->cstrings = cstring;
2076
2077 max_count = 0; dw = -1;
2078 for (i = 0; i <= 1000; i++) {
2079 if (w_stat[i] > max_count) {
2080 dw = i;
2081 max_count = w_stat[i];
2082 }
2083 }
2084 if (dw >= 0) {
2085 add_metrics(font, cffont, CIDToGIDMap, widths, dw, last_cid);
2086 } else {
2087 add_metrics(font, cffont, CIDToGIDMap, widths, defaultwidth, last_cid);
2088 }
2089 RELEASE(widths);
2090 }
2091 cff_release_index(cffont->subrs[0]);
2092 cffont->subrs[0] = NULL;
2093
2094 RELEASE(CIDToGIDMap);
2095
2096 cff_add_string(cffont, "Adobe", 1);
2097 cff_add_string(cffont, "Identity", 1);
2098
2099 cff_dict_update(cffont->topdict, cffont);
2100 cff_dict_update(cffont->private[0], cffont);
2101
2102 cff_update_string(cffont);
2103
2104 /* CFF code need to be rewrote... */
2105 cff_dict_add(cffont->topdict, "ROS", 3);
2106 cff_dict_set(cffont->topdict, "ROS", 0,
2107 (double) cff_get_sid(cffont, "Adobe"));
2108 cff_dict_set(cffont->topdict, "ROS", 1,
2109 (double) cff_get_sid(cffont, "Identity"));
2110 cff_dict_set(cffont->topdict, "ROS", 2, 0.0);
2111
2112 cffont->num_glyphs = num_glyphs;
2113 offset = write_fontfile(font, cffont);
2114
2115 cff_close(cffont);
2116
2117 if (pdf_check_version(2, 0) < 0) {
2118 CIDFont_type0_add_CIDSet(font, used_chars, last_cid);
2119 }
2120
2121 return 0;
2122 }