Debian Codegraph @

texk/xdvipsk/writecid.c

raw
1 /*
2 * Creates CID font resource from a SFNT tables based font file.
3 */
4 #include "xdvips.h" /* The copyright notice in that file is included too! */
5 /*
6 * The external declarations:
7 */
8 #include "protos.h"
9 #include "protos_add.h" /* external declarations */
10
11 #include "mem.h"
12 #include "error.h"
13 #include "dpxfile.h"
14 #include "sfnt.h"
15 #include "cmap.h"
16
17 #include "tt_aux.h"
18 #include "tt_cmap.h"
19 #include "tt_table.h"
20 #include "tt_post.h"
21
22 #include "cff_types.h"
23 #include "cff_dict.h"
24 #include "cff.h"
25
26 #include "cidsysinfo.h"
27 #include "cidtype0.h"
28 #include "cidtype2.h"
29
30 #if defined(WIN32)
31 #include <io.h>
32 #include <direct.h>
33 #define access _access
34 #define mkdir _mkdir
35 #else
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #ifndef ACCESSPERMS
39 #define ACCESSPERMS 0755
40 #endif
41 #endif
42
43 #define CID_HEXLINE_WIDTH 64
44 #define cid_putchar(c) fputc(c, bitfile)
45
46 static long hexline_length;
47
48 /* from Adobe Technical Specification #5014, Adobe CMap and CIDFont Files */
49 /* Specification, Version 1.0. */
50 static const char *cid_othersubrs[] = {
51 "[ {} {} {}",
52 " { systemdict /internaldict known not",
53 " { pop 3 }",
54 " { 1183615869 systemdict /internaldict get exec dup",
55 " /startlock known",
56 " { /startlock get exec }",
57 " { dup /strlck known",
58 " { /strlck get exec }",
59 " { pop 3 }",
60 " ifelse",
61 " }",
62 " ifelse",
63 " }",
64 " ifelse",
65 " } bind",
66 " {} {} {} {} {} {} {} {} {}",
67 " { 2 { cvi { { pop 0 lt { exit } if } loop } repeat }",
68 " repeat } bind",
69 "]",
70 NULL
71 };
72
73 static void end_hexline(void)
74 {
75 if (hexline_length == CID_HEXLINE_WIDTH) {
76 fputs("\n", bitfile);
77 hexline_length = 0;
78 }
79 }
80
81 static void cid_outhex(BYTE b)
82 {
83 static const char *hexdigits = "0123456789ABCDEF";
84 cid_putchar(hexdigits[b/16]);
85 cid_putchar(hexdigits[b%16]);
86 hexline_length += 2;
87 end_hexline();
88 }
89
90 static void cid_outbin(BYTE b)
91 {
92 cid_putchar(b);
93 }
94
95 static char *rand_string(char *str, size_t size)
96 {
97 size_t n;
98 const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
99 if (size) {
100 --size;
101 for (n = 0; n < size; n++) {
102 #ifdef WIN32
103 int key = rand() % (int) (sizeof(charset) - 1);
104 #else
105 int key = random() % (int) (sizeof(charset) - 1);
106 #endif
107 str[n] = charset[key];
108 }
109 str[size] = '\0';
110 }
111 return str;
112 }
113
114 static void add_otf_g2u_table(char *ps_name)
115 {
116 charusetype_ref *cu_ref;
117 charusetype_entry *cu_head = NULL;
118 boolean used;
119 charusetype_entry *cu_entry;
120 int gid;
121 const UsedMapElem *usedchars;
122 static UsedMapElem processed_gids[USED_CHARS_BUF_SIZE];
123 char empty_str[] = "";
124 char *tex_name;
125 luamaptype *lmap_ptr = NULL, *lmap_iter, *lmap_tmp;
126 struct resfont *cur_resfont = NULL;
127 int ii;
128
129 fprintf(bitfile, " /GlyphNames2Unicode <<\n");
130
131 if (ps_name)
132 {
133 cu_ref = lookup_charused(ps_name, Any_otf_font);
134 if (cu_ref)
135 cu_head = cu_ref->head;
136 }
137 else
138 ps_name = "";
139 if (cu_head)
140 {
141 memset(processed_gids, 0, sizeof(processed_gids));
142 cu_entry = cu_head;
143 while (cu_entry)
144 {
145 lmap_ptr = NULL;
146 cur_resfont = NULL;
147 tex_name = empty_str;
148 if (cu_entry->charused_ptr && cu_entry->charused_ptr->fd && cu_entry->charused_ptr->fd->resfont)
149 {
150 if (cu_entry->charused_ptr->fd->resfont->TeXname)
151 {
152 tex_name = cu_entry->charused_ptr->fd->resfont->TeXname;
153 cur_resfont = lookup(tex_name);
154 }
155 }
156 if (cur_resfont)
157 {
158 ii = cur_resfont->luamap_idx;
159 if (ii >= 0)
160 {
161 lmap_ptr = LuaMap_cache_get(ii);
162 if (lmap_ptr == NULL)
163 fprintf(stderr, "\nError: Font %s encoding not found", tex_name);
164 }
165 else
166 fprintf(stderr, "\nError: No font encoding for %s", tex_name);
167 }
168 else
169 fprintf(stderr, "\nError: Font %s not found", tex_name);
170
171 if (lmap_ptr)
172 {
173 HASH_ITER(hh, lmap_ptr, lmap_iter, lmap_tmp)
174 {
175 gid = lmap_iter->gid;
176 used = FALSE;
177 if (cu_entry->charused_ptr)
178 {
179 usedchars = cu_entry->charused_ptr->bitmap;
180 if (usedchars && IS_USED_CHAR(usedchars, gid) && (!IS_USED_CHAR(processed_gids, gid)))
181 {
182 used = TRUE;
183 ADD_TO_USED_CHARS(processed_gids, gid);
184 }
185 }
186 if (used)
187 {
188 if (lmap_iter->tounicode && lmap_iter->tu_count)
189 {
190 fprintf(bitfile, " %d <", gid);
191 for (ii = 0; ii < lmap_iter->tu_count; ii++)
192 fprintf(bitfile, "%04X", lmap_iter->tounicode[ii]);
193 fprintf(bitfile, ">\n");
194 }
195 else
196 fprintf(stderr, "Warning: Unicode value for the glyph %d not found; font: %s.\n", gid, ps_name);
197 }
198 }
199 }
200
201 cu_entry = cu_entry->next;
202 }
203 }
204 fprintf(bitfile, " >> def\n");
205 }
206
207 #define FMT_BUF_LEN 50
208 static char *dtoa_(double val)
209 {
210 static char fmt_buf[FMT_BUF_LEN + 1];
211 char *buf_ptr;
212 int num_chars;
213
214 num_chars = snprintf(fmt_buf, FMT_BUF_LEN, "%f", val);
215 if (strchr(fmt_buf, '.'))
216 {
217 buf_ptr = fmt_buf + num_chars;
218 while (*(--buf_ptr) == '0');
219 if (*buf_ptr == '.')
220 buf_ptr++;
221 *(buf_ptr + 1) = '\0';
222 }
223
224 return(fmt_buf);
225 }
226
227 static void writecidtype0(cff_font *cffont, struct tt_head_table *head, CIDSysInfo *csi, struct cidbytes *cdb, BYTE *binarydata, long bin_size)
228 {
229 char *str, start[256];
230 char name[64];
231 long i, j, k, cnt;
232 double dbl, dbl_prev;
233 s_SID sid;
234 struct fddata *incid;
235 int isbold=false;
236
237 assert(cffont);
238 assert(csi);
239 assert(cdb);
240
241 fprintf(bitfile, "%%%%BeginResource: font (%s)\n",cffont->fontname);
242 fprintf(bitfile, "%%%%Title: (%s)\n",cffont->fontname);
243 fprintf(bitfile, "%%%%Version: 1 0\n");
244 linepos = 1;
245 newline();
246 fprintf(bitfile, "/CIDInit /ProcSet findresource begin\n");
247
248 fprintf(bitfile, "20 dict begin\n");
249
250 fprintf(bitfile, "/CIDFontName /%s def\n",cffont->fontname);
251 if ( cff_dict_known(cffont->topdict,"CIDFontVersion") )
252 dbl = cff_dict_get(cffont->topdict, "CIDFontVersion", 0);
253 else
254 dbl = 1;
255 fprintf(bitfile, "/CIDFontVersion %g def\n",dbl);
256 if ( cff_dict_known(cffont->topdict,"CIDFontType") )
257 dbl = cff_dict_get(cffont->topdict, "CIDFontType", 0);
258 else
259 dbl = 0;
260 fprintf(bitfile, "/CIDFontType %g def\n",dbl);
261
262 fprintf(bitfile, "/CIDSystemInfo 3 dict dup begin\n");
263 fprintf(bitfile, " /Registry (%s) def\n",csi->registry);
264 fprintf(bitfile, " /Ordering (%s) def\n",csi->ordering);
265 fprintf(bitfile, " /Supplement %d def\n",csi->supplement);
266 fprintf(bitfile, "end def\n");
267
268 fprintf(bitfile, "/FontBBox [");
269 dbl = cff_dict_get(cffont->topdict, "FontBBox", 0);
270 fprintf(bitfile, " %g",dbl);
271 dbl = cff_dict_get(cffont->topdict, "FontBBox", 1);
272 fprintf(bitfile, " %g",dbl);
273 dbl = cff_dict_get(cffont->topdict, "FontBBox", 2);
274 fprintf(bitfile, " %g",dbl);
275 dbl = cff_dict_get(cffont->topdict, "FontBBox", 3);
276 fprintf(bitfile, " %g",dbl);
277 fprintf(bitfile, " ] def\n");
278
279 cnt = 1;
280 if ( cff_dict_known(cffont->topdict,"Notice") )
281 cnt++;
282 if ( cff_dict_known(cffont->topdict,"FullName") )
283 cnt++;
284 if ( cff_dict_known(cffont->topdict,"FamilyName") )
285 cnt++;
286 if ( cff_dict_known(cffont->topdict,"Weight") )
287 cnt++;
288 if ( cff_dict_known(cffont->topdict,"ItalicAngle") )
289 cnt++;
290 if ( cff_dict_known(cffont->topdict,"isFixedPitch") )
291 cnt++;
292 /* TODO: move scanning of encoding table to the place, where other cffont->topdict elements are filled */
293 /* if (cff_dict_known(cffont->topdict, "GlyphNames2Unicode"))
294 cnt++; */
295
296 if ( cnt ) {
297 fprintf(bitfile, "/FontInfo %ld dict dup begin\n", cnt + (((Otf_Enc_Type == enc_gid) || charcode_otf_g2u)? 1 : 0)); /* unconditional additional entry for /GlyphNames2Unicode */
298 if ( cff_dict_known(cffont->topdict,"Notice") ) {
299 sid = (s_SID)cff_dict_get(cffont->topdict, "Notice", 0);
300 str = cff_get_string(cffont,sid);
301 fprintf(bitfile, " /Notice (");
302 for ( k=0; k<strlen(str); ++k ) {
303 if ( str[k]<' ' || str[k]>=0x7f || str[k]=='\\' || str[k]=='(' || str[k]==')' ) {
304 fputc('\\',bitfile);
305 fputc('0'+(str[k]>>6),bitfile);
306 fputc('0'+((str[k]>>3)&0x7),bitfile);
307 fputc('0'+(str[k]&0x7),bitfile);
308 } else
309 fputc(str[k],bitfile);
310 }
311 fprintf(bitfile, ") readonly def\n");
312 }
313 if ( cff_dict_known(cffont->topdict,"FullName") ) {
314 sid = (s_SID)cff_dict_get(cffont->topdict, "FullName", 0);
315 str = cff_get_string(cffont,sid);
316 fprintf(bitfile, " /FullName (%s) readonly def\n",str);
317 }
318 if ( cff_dict_known(cffont->topdict,"FamilyName") ) {
319 sid = (s_SID)cff_dict_get(cffont->topdict, "FamilyName", 0);
320 str = cff_get_string(cffont,sid);
321 fprintf(bitfile, " /FamilyName (%s) readonly def\n",str);
322 }
323 if ( cff_dict_known(cffont->topdict,"Weight") ) {
324 sid = (s_SID)cff_dict_get(cffont->topdict, "Weight", 0);
325 str = cff_get_string(cffont,sid);
326 fprintf(bitfile, " /Weight (%s) readonly def\n",str);
327 }
328 if ( cff_dict_known(cffont->topdict,"ItalicAngle") ) {
329 dbl = cff_dict_get(cffont->topdict, "ItalicAngle", 0);
330 fprintf(bitfile, " /ItalicAngle %g def\n",dbl);
331 }
332 if ( cff_dict_known(cffont->topdict,"isFixedPitch") ) {
333 dbl = cff_dict_get(cffont->topdict, "isFixedPitch", 0);
334 fprintf(bitfile, " /isFixedPitch %s readonly def\n",dbl > 0 ? "true" : "false");
335 }
336 fprintf(bitfile, " /FSType 8 def\n");
337
338 if ((Otf_Enc_Type == enc_gid) || charcode_otf_g2u)
339 add_otf_g2u_table(cffont->fontname);
340
341 fprintf(bitfile, "end readonly def\n");
342 }
343
344 fprintf(bitfile, "/CIDMapOffset %d def\n",cdb->cidmapoffset);
345 fprintf(bitfile, "/FDBytes %d def\n",cdb->fdbytes);
346 fprintf(bitfile, "/GDBytes %d def\n",cdb->gdbytes);
347 fprintf(bitfile, "/CIDCount %d def\n",cdb->cidcnt);
348
349 fprintf(bitfile, "/FDArray %d array\n",cdb->fdcnt);
350 for ( i=0; i<cdb->fdcnt; ++i ) {
351 fprintf(bitfile,"dup %ld\n",i);
352 fprintf(bitfile, "%%ADOBeginFontDict\n");
353 fprintf(bitfile, "15 dict begin\n");
354
355 fprintf(bitfile, " /FontName /%s def\n",cffont->fontname);
356 fprintf(bitfile, " /FontType 1 def\n");
357 fprintf(bitfile, " /FontMatrix [");
358 if ( cff_dict_known(cffont->topdict,"FontMatrix") ) {
359 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 0);
360 fprintf(bitfile, " ");
361 fprintf(bitfile, "%s", dtoa_(dbl));
362 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 1);
363 fprintf(bitfile, " ");
364 fprintf(bitfile, "%s", dtoa_(dbl));
365 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 2);
366 fprintf(bitfile, " ");
367 fprintf(bitfile, "%s", dtoa_(dbl));
368 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 3);
369 fprintf(bitfile, " ");
370 fprintf(bitfile, "%s", dtoa_(dbl));
371 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 4);
372 fprintf(bitfile, " ");
373 fprintf(bitfile, "%s", dtoa_(dbl));
374 dbl = cff_dict_get(cffont->topdict, "FontMatrix", 5);
375 fprintf(bitfile, " ");
376 fprintf(bitfile, "%s", dtoa_(dbl));
377 }
378 else
379 {
380 /* fprintf(bitfile, " 0.001 0.0 0.0 0.001 0.0 0.0"); */
381 assert(head);
382 assert(head->unitsPerEm);
383 char *scale_str = dtoa_(1. / head->unitsPerEm);
384 fprintf(bitfile, " %s 0.0 0.0 %s 0.0 0.0", scale_str, scale_str);
385 }
386 fprintf(bitfile, " ] def\n");
387 fprintf(bitfile, " /PaintType 0 def\n");
388 fprintf(bitfile, " %%ADOBeginPrivateDict\n");
389 fprintf(bitfile, " /Private 14 dict dup begin\n");
390 fprintf(bitfile, " /MinFeature {16 16} def\n");
391 fprintf(bitfile, " /password 5839 def\n");
392
393 incid = &cdb->fds[i];
394 for ( j=0; j<cffont->privat[i]->count; j++ ) {
395 if ( strcmp(cffont->privat[i]->entries[j].key,"BlueValues") == 0 ) {
396 fprintf(bitfile, " /BlueValues [");
397 dbl_prev = 0;
398 for ( k=0; k<cffont->privat[i]->entries[j].count; k++ ) {
399 dbl = cffont->privat[i]->entries[j].values[k];
400 if ( k != 0 )
401 dbl += dbl_prev;
402 dbl_prev = dbl;
403 fprintf(bitfile, " %g",dbl);
404 }
405 fprintf(bitfile, " ] def\n");
406 break;
407 }
408 }
409 for ( j=0; j<cffont->privat[i]->count; j++ ) {
410 if ( strcmp(cffont->privat[i]->entries[j].key,"OtherBlues") == 0 ) {
411 fprintf(bitfile, " /OtherBlues [");
412 dbl_prev = 0;
413 for ( k=0; k<cffont->privat[i]->entries[j].count; k++ ) {
414 dbl = cffont->privat[i]->entries[j].values[k];
415 if ( k != 0 )
416 dbl += dbl_prev;
417 dbl_prev = dbl;
418 fprintf(bitfile, " %g",dbl);
419 }
420 fprintf(bitfile, " ] def\n");
421 break;
422 }
423 }
424 for ( j=0; j<cffont->privat[i]->count; j++ ) {
425 if ( strcmp(cffont->privat[i]->entries[j].key,"StdHW") == 0 ) {
426 dbl = cffont->privat[i]->entries[j].values[0];
427 fprintf(bitfile, " /StdHW [%g] def\n",dbl);
428 break;
429 }
430 }
431 for ( j=0; j<cffont->privat[i]->count; j++ ) {
432 if ( strcmp(cffont->privat[i]->entries[j].key,"StemSnapH") == 0 ) {
433 fprintf(bitfile, " /StemSnapH [");
434 dbl_prev = 0;
435 for ( k=0; k<cffont->privat[i]->entries[j].count; k++ ) {
436 dbl = cffont->privat[i]->entries[j].values[k];
437 if ( k != 0 )
438 dbl += dbl_prev;
439 dbl_prev = dbl;
440 fprintf(bitfile, " %g",dbl);
441 }
442 fprintf(bitfile, " ] def\n");
443 break;
444 }
445 }
446 for ( j=0; j<cffont->privat[i]->count; j++ ) {
447 if ( strcmp(cffont->privat[i]->entries[j].key,"StdVW") == 0 ) {
448 dbl = cffont->privat[i]->entries[j].values[0];
449 fprintf(bitfile, " /StdVW [%g] def\n",dbl);
450 break;
451 }
452 }
453 for ( j=0; j<cffont->privat[i]->count; j++ ) {
454 if ( strcmp(cffont->privat[i]->entries[j].key,"StemSnapV") == 0 ) {
455 fprintf(bitfile, " /StemSnapV [");
456 dbl_prev = 0;
457 for ( k=0; k<cffont->privat[i]->entries[j].count; k++ ) {
458 dbl = cffont->privat[i]->entries[j].values[k];
459 if ( k != 0 )
460 dbl += dbl_prev;
461 dbl_prev = dbl;
462 fprintf(bitfile, " %g",dbl);
463 }
464 fprintf(bitfile, " ] def\n");
465 break;
466 }
467 }
468 for ( j=0; j<cffont->privat[i]->count; j++ ) {
469 if ( strcmp(cffont->privat[i]->entries[j].key,"BlueShift") == 0 ) {
470 dbl = cffont->privat[i]->entries[j].values[0];
471 fprintf(bitfile, " /BlueShift %g def\n",dbl);
472 break;
473 }
474 }
475 for ( j=0; j<cffont->privat[i]->count; j++ ) {
476 if ( strcmp(cffont->privat[i]->entries[j].key,"BlueScale") == 0 ) {
477 dbl = cffont->privat[i]->entries[j].values[0];
478 fprintf(bitfile, " /BlueScale %g def\n",dbl);
479 break;
480 }
481 }
482 if ( cff_dict_known(cffont->topdict,"Weight") ) {
483 sid = (s_SID)cff_dict_get(cffont->topdict, "Weight", 0);
484 str = cff_get_string(cffont,sid);
485 if (strcmp(str,"Bold")==0 ||
486 strcmp(str,"Heavy")==0 ||
487 strcmp(str,"Black")==0 ||
488 strcmp(str,"Grass")==0 ||
489 strcmp(str,"Fett")==0)
490 isbold = true;
491 }
492 if ( cff_dict_known(cffont->privat[i],"ForceBold") ) {
493 dbl = cff_dict_get(cffont->privat[i],"ForceBold",0);
494 fprintf(bitfile, " /ForceBold %s def\n",dbl>0 ? "true" : "false");
495 }
496 else if ( isbold ) {
497 fprintf(bitfile, " /ForceBold true def\n");
498 }
499 fprintf(bitfile, " /LanguageGroup 1 def\n");
500 if ( incid->leniv != -1 ) {
501 fprintf(bitfile, " /lenIV %d def\n",incid->leniv);
502 }
503 fprintf(bitfile, " /OtherSubrs");
504 j = 0;
505 while ( (str = (char *)cid_othersubrs[j]) ) {
506 fprintf(bitfile, " %s\n",str);
507 j++;
508 }
509 fprintf(bitfile, " def\n");
510
511 fprintf(bitfile, " /SubrMapOffset %d def\n",incid->subrmapoff);
512 fprintf(bitfile, " /SDBytes %d def\n",incid->sdbytes);
513 fprintf(bitfile, " /SubrCount %d def\n",incid->subrcnt);
514
515 fprintf(bitfile, " end def\n");
516 fprintf(bitfile, " %%ADOEndPrivateDict\n");
517
518 fprintf(bitfile, " currentdict end\n");
519 fprintf(bitfile, " %%ADOEndFontDict\n");
520 fprintf(bitfile, " put\n");
521 }
522 fprintf(bitfile, "def\n");
523
524 sprintf(start,"(Hex) %ld StartData\n",bin_size);
525 fprintf(bitfile, "%%%%BeginData: %ld Binary Bytes\n",(long)strlen(start) + bin_size * 2 + 1);
526 fprintf(bitfile, "%s", start);
527 hexline_length = 0;
528 if (bin_size)
529 assert(binarydata);
530 for ( i=0; i<bin_size; i++ ) {
531 cid_outhex(binarydata[i]);
532 }
533 fputc('>',bitfile);
534 linepos = 1;
535 newline();
536 fprintf(bitfile, "%%%%EndData\n");
537
538 fprintf(bitfile, "%%%%EndResource\n");
539 strcpy(name, "CID+");
540 strcat(name, cffont->fontname);
541 fprintf(bitfile, "/%s /TeX_Identity-H [/%s] composefont pop\n", name, cffont->fontname);
542 }
543
544 static char *get_sfnt_name(sfnt *sfont, char *name)
545 {
546 char *dest, *ret;
547 USHORT destlen, k, n;
548 USHORT namelen = 0;
549
550 dest = NEW(64000,char);
551 destlen = 64000;
552
553 if ( strcmp(name,"Version") == 0 ) { //id=5
554 namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 5);
555 }
556 else if ( strcmp(name,"Notice") == 0 ) { //id=0
557 namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 0);
558 }
559 else if ( strcmp(name,"FullName") == 0 ) { //id=4
560 namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 4);
561 }
562 else if ( strcmp(name,"FamilyName") == 0 ) { //id=1
563 namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 1);
564 }
565 else if ( strcmp(name,"Weight") == 0 ) { //id=2
566 namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 2);
567 }
568 else
569 return NULL;
570 if ( namelen == 0 )
571 return NULL;
572 dest[namelen] = '\0';
573 k = namelen;
574 for (n = 0; n < namelen; n++) {
575 if (dest[n] == 0) {
576 memmove(dest + n, dest + n + 1, namelen - n - 1);
577 k--;
578 }
579 }
580 dest[k] = '\0';
581 if (strlen(dest) == 0)
582 return NULL;
583 ret = NEW(strlen(dest) + 1, char);
584 strcpy(ret,dest);
585 free(dest);
586 return ret;
587 }
588
589 static void dumpsfnt(BYTE *sfnt_table,ULONG length) {
590 int ch, ch1;
591 ULONG i, bytesout;
592 BYTE *sfnt_tbl;
593
594 if ( length&1 )
595 ERROR("SFNT table length should not be odd");
596
597 sfnt_tbl = sfnt_table;
598 while ( length>65534 ) {
599 dumpsfnt(sfnt_tbl,65534);
600 length -= 65534;
601 sfnt_tbl += 65534;
602 }
603
604 fprintf( bitfile, " <\n " );
605 bytesout = 0;
606 for ( i=0; i<length; ++i ) {
607 ch = sfnt_tbl[i];
608 if ( ch==EOF )
609 break;
610 if ( bytesout>=31 ) {
611 fprintf( bitfile, "\n " );
612 bytesout = 0;
613 }
614 ch1 = ch>>4;
615 if ( ch1>=10 )
616 ch1 += 'A'-10;
617 else
618 ch1 += '0';
619 putc(ch1,bitfile);
620 ch1 = ch&0xf;
621 if ( ch1>=10 )
622 ch1 += 'A'-10;
623 else
624 ch1 += '0';
625 putc(ch1,bitfile);
626 ++bytesout;
627 }
628 fprintf( bitfile, "\n 00\n >\n" );
629 }
630
631 /*
632 * Computes the max power of 2 <= n
633 */
634 static unsigned
635 max2floor (unsigned n)
636 {
637 int val = 1;
638
639 while (n > 1) {
640 n /= 2;
641 val *= 2;
642 }
643
644 return val;
645 }
646
647 /*
648 * Computes the log2 of the max power of 2 <= n
649 */
650 static unsigned
651 log2floor (unsigned n)
652 {
653 unsigned val = 0;
654
655 while (n > 1) {
656 n /= 2;
657 val++;
658 }
659
660 return val;
661 }
662
663 static ULONG *makeglyphslocation(sfnt *sfont, SHORT indexToLocFormat, halfword num_glyphs) {
664 struct sfnt_table_directory *td;
665 int idx, i, j;
666 ULONG *loca;
667
668 td = sfont->directory;
669
670 for (idx = 0; idx < td->num_tables; idx++) {
671 if ( memcmp(td->tables[idx].tag, "loca", 4) == 0 )
672 break;
673 }
674 if ( idx >= td->num_tables )
675 return NULL;
676
677 loca = NEW(num_glyphs+1,ULONG);
678 if (!td->tables[idx].data) {
679 sfnt_seek_set(sfont, td->tables[idx].offset);
680 if (indexToLocFormat == 0) {
681 for (i = 0; i <= num_glyphs; i++)
682 loca[i] = 2*((ULONG) sfnt_get_ushort(sfont));
683 } else if (indexToLocFormat == 1) {
684 for (i = 0; i <= num_glyphs; i++)
685 loca[i] = sfnt_get_ulong(sfont);
686 } else {
687 free(loca);
688 return NULL;
689 }
690 }
691 else {
692 if (indexToLocFormat == 0) {
693 for (i = 0; i <= num_glyphs; i++) {
694 unsigned short pair = td->tables[idx].data[2 * i];
695 loca[i] = 2 * ((pair << 8) | (unsigned char)td->tables[idx].data[2 * i + 1]);
696 }
697 } else if (indexToLocFormat == 1) {
698 for (i = 0; i <= num_glyphs; i++) {
699 loca[i] = 0;
700 for (j=i * 4; j<i * 4 + 4; j++) {
701 loca[i] = (loca[i] << 8) | (unsigned char)td->tables[idx].data[j];
702 }
703 }
704 } else {
705 free(loca);
706 return NULL;
707 }
708 }
709
710 return loca;
711 }
712
713 static void writecidtype2(sfnt *sfont, struct tt_head_table *head, struct tt_hhea_table *hhea,
714 CIDSysInfo *csi, /* const UsedMapElem *usedchars, */ char *ps_name, long maxcid)
715 {
716 char *str;
717 long k;
718 double dbl;
719 struct sfnt_table_directory *td;
720 long offset, nb_read, length, len_pad;
721 int i, j, sr;
722 char *p, *p1;
723 char name[64];
724 BYTE *wbuf, wtbl[1024], padbytes[4] = {0, 0, 0, 0};
725
726 struct tt_post_table *post;
727
728 assert(head);
729 assert(hhea);
730 assert(csi);
731
732 post = tt_read_post_table(sfont);
733 if (!post) {
734 return;
735 }
736
737 fprintf(bitfile, "%%%%BeginResource: font (%s)\n", ps_name);
738 fprintf(bitfile, "%%%%Title: (%s)\n", ps_name);
739 fprintf(bitfile, "%%%%Version: 1 0\n");
740 linepos = 1;
741 newline();
742 fprintf(bitfile, "/CIDInit /ProcSet findresource begin\n");
743
744 fprintf(bitfile, "16 dict begin\n");
745
746 fprintf(bitfile, "/CIDFontName /%s def\n", ps_name);
747 fprintf(bitfile, "/CIDFontType 2 def\n");
748 fprintf(bitfile, "/FontType 11 def\n");
749
750 fprintf(bitfile, "/CIDSystemInfo 3 dict dup begin\n");
751 fprintf(bitfile, " /Registry (%s) def\n",csi->registry);
752 fprintf(bitfile, " /Ordering (%s) def\n",csi->ordering);
753 fprintf(bitfile, " /Supplement %d def\n",csi->supplement);
754 fprintf(bitfile, "end def\n");
755
756 fprintf(bitfile, "/FontMatrix [ 1 0 0 1 0 0 ] def\n");
757 fprintf(bitfile, "/PaintType 0 def\n");
758
759 fprintf(bitfile, "/FontBBox [");
760 dbl = (double)head->xMin / (hhea->ascent + hhea->descent);
761 fprintf(bitfile, " %.4f",dbl);
762 dbl = (double)head->yMin / (hhea->ascent + hhea->descent);
763 fprintf(bitfile, " %.4f",dbl);
764 dbl = (double)head->xMax / (hhea->ascent + hhea->descent);
765 fprintf(bitfile, " %.4f",dbl);
766 dbl = (double)head->yMax / (hhea->ascent + hhea->descent);
767 fprintf(bitfile, " %.4f",dbl);
768 fprintf(bitfile, " ] readonly def\n");
769
770 fprintf(bitfile, "/FontInfo %d dict dup begin\n", 11 + (((Otf_Enc_Type == enc_gid) || charcode_otf_g2u) ? 1 : 0)); /* additional entry for /GlyphNames2Unicode */
771 if ( (str = get_sfnt_name(sfont,"Version")) ) {
772 fprintf(bitfile, " /Version (%s) readonly def\n",str + 8);
773 }
774 if ( (str = get_sfnt_name(sfont,"Notice")) ) {
775 fprintf(bitfile, " /Notice (");
776 for ( k=0; k<strlen(str); ++k ) {
777 if ( str[k]<' ' || str[k]>=0x7f || str[k]=='\\' || str[k]=='(' || str[k]==')' ) {
778 fputc('\\',bitfile);
779 fputc('0'+(str[k]>>6),bitfile);
780 fputc('0'+((str[k]>>3)&0x7),bitfile);
781 fputc('0'+(str[k]&0x7),bitfile);
782 } else
783 fputc(str[k],bitfile);
784 }
785 fprintf(bitfile, ") readonly def\n");
786 }
787 if ( (str = get_sfnt_name(sfont,"FullName")) ) {
788 fprintf(bitfile, " /FullName (%s) readonly def\n",str);
789 }
790 if ( (str = get_sfnt_name(sfont,"FamilyName")) ) {
791 fprintf(bitfile, " /FamilyName (%s) readonly def\n",str);
792 }
793 if ( (str = get_sfnt_name(sfont,"Weight")) ) {
794 fprintf(bitfile, " /Weight (%s) readonly def\n",str);
795 }
796 dbl = (double)post->italicAngle / 65536;
797 fprintf(bitfile, " /ItalicAngle %g def\n",dbl);
798 dbl = post->isFixedPitch;
799 fprintf(bitfile, " /isFixedPitch %s def\n",dbl > 0 ? "true" : "false");
800 dbl = (double)post->underlinePosition / (hhea->ascent+hhea->descent);
801 fprintf(bitfile, " /UnderlinePosition %g def\n",dbl);
802 dbl = (double)post->underlineThickness / (hhea->ascent+hhea->descent);
803 fprintf(bitfile, " /UnderlineThickness %g def\n",dbl);
804 if ( hhea->ascent != 8*(hhea->ascent+hhea->descent)/10 ) {
805 fprintf(bitfile, " /ascent %d def\n",hhea->ascent);
806 }
807 fprintf(bitfile, " /FSType 8 def\n");
808
809 if ((Otf_Enc_Type == enc_gid) || charcode_otf_g2u)
810 add_otf_g2u_table(ps_name);
811
812 fprintf(bitfile, "end readonly def\n");
813
814 fprintf( bitfile, "/sfnts [\n" );
815
816 td = sfont->directory;
817 wbuf = NEW(12 + 16 * td->num_kept_tables,BYTE);
818
819 /* Header */
820 p = (char *) wbuf;
821 p += sfnt_put_ulong (p, td->version);
822 p += sfnt_put_ushort(p, td->num_kept_tables);
823 sr = max2floor(td->num_kept_tables) * 16;
824 p += sfnt_put_ushort(p, sr);
825 p += sfnt_put_ushort(p, log2floor(td->num_kept_tables));
826 p += sfnt_put_ushort(p, td->num_kept_tables * 16 - sr);
827
828 /*
829 * Compute start of actual tables (after headers).
830 */
831 offset = 12 + 16 * td->num_kept_tables;
832 for (i = 0; i < td->num_tables; i++) {
833 /* This table must exist in FontFile */
834 if (td->flags[i] & SFNT_TABLE_REQUIRED) {
835 if ((offset % 4) != 0) {
836 offset += 4 - (offset % 4);
837 }
838 p1 = (char *) wtbl;
839 memcpy(p1, td->tables[i].tag, 4);
840 p1 += 4;
841 p1 += sfnt_put_ulong(p1, td->tables[i].check_sum);
842 p1 += sfnt_put_ulong(p1, offset);
843 p1 += sfnt_put_ulong(p1, td->tables[i].length);
844 memcpy(p, wtbl, 16);
845 p += 16;
846 offset += td->tables[i].length;
847 }
848 }
849 length = 12 + 16 * td->num_kept_tables;
850 dumpsfnt(wbuf,length);
851 free(wbuf);
852
853 offset = 12 + 16 * td->num_kept_tables;
854 wbuf = NULL;
855 for (i = 0; i < td->num_tables; i++) {
856 if (td->flags[i] & SFNT_TABLE_REQUIRED) {
857 if ((td->tables[i].length % 4) != 0)
858 len_pad = 4 - (td->tables[i].length % 4);
859 else
860 len_pad = 0;
861 if ( (memcmp(td->tables[i].tag,"glyf",4) == 0) && (td->tables[i].length > 65534) ) {
862 ULONG *loca = makeglyphslocation(sfont,head->indexToLocFormat,maxcid+1);
863 ULONG last = 0;
864 length = 0;
865 wbuf = NEW(65534,BYTE);
866 for ( j=0; j<maxcid+1; ++j ) {
867 if ( loca[j+1]-last > 65534 ) {
868 dumpsfnt(wbuf,loca[j]-last);
869 last = loca[j];
870 length = 0;
871 }
872 if (!td->tables[i].data) {
873 sfnt_seek_set(sfont, td->tables[i].offset + loca[j]);
874 nb_read = sfnt_read(wbuf + length, loca[j + 1] - loca[j], sfont);
875 if (nb_read < 0) {
876 if ( wbuf )
877 RELEASE(wbuf);
878 ERROR("Reading file failed...");
879 return;
880 }
881 }
882 else
883 memcpy(wbuf + length, td->tables[i].data + loca[j], loca[j + 1] - loca[j]);
884 length += loca[j + 1] - loca[j];
885 }
886 if ( len_pad > 0 )
887 memcpy(wbuf + length,padbytes,len_pad);
888 dumpsfnt(wbuf,length + len_pad);
889 RELEASE(td->tables[i].data);
890 td->tables[i].data = NULL;
891 free(loca);
892 free(wbuf);
893 }
894 else {
895 if (!td->tables[i].data) {
896 if (!sfont->stream) {
897 if ( wbuf )
898 RELEASE(wbuf);
899 ERROR("Font file not opened or already closed...");
900 return;
901 }
902 length = td->tables[i].length;
903 wbuf = NEW(length + len_pad,BYTE);
904 sfnt_seek_set(sfont, td->tables[i].offset);
905 nb_read = sfnt_read(wbuf, td->tables[i].length, sfont);
906 if (nb_read < 0) {
907 if ( wbuf )
908 RELEASE(wbuf);
909 ERROR("Reading file failed...");
910 return;
911 }
912 }
913 else {
914 length = td->tables[i].length;
915 wbuf = NEW(length + len_pad,BYTE);
916 memcpy(wbuf, td->tables[i].data, td->tables[i].length);
917 RELEASE(td->tables[i].data);
918 td->tables[i].data = NULL;
919 }
920 if ( len_pad > 0 )
921 memcpy(wbuf + length,padbytes,len_pad);
922 dumpsfnt(wbuf,length + len_pad);
923 free(wbuf);
924 wbuf = NULL;
925 /* Set offset for next table */
926 offset += td->tables[i].length + len_pad;
927 }
928 }
929 }
930 fprintf( bitfile, " ] def\n" );
931
932 fprintf(bitfile, " /CharStrings 1 dict dup begin\n");
933 fprintf(bitfile, " /.notdef 0 def\n");
934 fprintf(bitfile, " end readonly def\n");
935
936 fprintf( bitfile, " /CIDCount %ld def\n", maxcid+1 );
937 fprintf( bitfile, " /GDBytes %d def\n", maxcid+1>65535?3:2 );
938 fprintf( bitfile, " /CIDMap 0 def\n" );
939
940 fprintf( bitfile, "currentdict end dup /CIDFontName get exch /CIDFont defineresource pop\nend\n" );
941
942 fprintf(bitfile, "%%%%EndResource\n");
943 strcpy(name, "CID+");
944 strcat(name, ps_name);
945 fprintf(bitfile, "/%s /TeX_Identity-H [/%s] composefont pop\n", name, ps_name);
946
947 RELEASE(post);
948 }
949
950 int writecid(charusetype *p)
951 {
952 struct resfont *rf;
953 FILE *fp;
954 char *path;
955 sfnt *sfont;
956 cff_font *cffont;
957 unsigned offset = 0;
958 int is_dfont = 0, index = 0;
959 int n;
960 CIDSysInfo csi;
961 struct tt_maxp_table *maxp;
962 struct tt_head_table *head;
963 struct tt_hhea_table *hhea;
964 CMap *code_to_cid_cmap;
965 halfword num_glyphs;
966 char xname[16];
967 BYTE *buffer;
968 long buf_size, maxcid;
969 int cidCount;
970 struct cidbytes cdb;
971 otfcmaptype *otfcmap;
972
973 curfnt = p->fd;
974 rf = curfnt->resfont;
975 code_to_cid_cmap = NULL;
976
977 if (!(fp = lookup_font_file(rf->Fontfile, &path, &is_dfont)))
978 {
979 char *msg = concatn("! Cannot proceed without the font: ", rf->Fontfile, NULL);
980 error(msg);
981 }
982 realnameoffile = path;
983 strcpy(name, realnameoffile);
984 index = rf->index;
985 if (is_dfont)
986 sfont = dfont_open(fp, index);
987 else
988 sfont = sfnt_open(fp);
989 if (sfont->type == SFNT_TYPE_TTC) {
990 offset = ttc_read_offset(sfont, index);
991 if (offset == 0) {
992 ERROR("Invalid TTC index for font %s",curfnt->resfont->PSname);
993 }
994 }
995 else if (sfont->type == SFNT_TYPE_DFONT) {
996 offset = sfont->offset;
997 }
998 if (sfnt_read_table_directory(sfont, offset) < 0) {
999 ERROR("Could not read OpenType/TrueType table directory for font %s",rf->PSname);
1000 }
1001 rand_string(xname,9);
1002 csi.registry = mymalloc(6);
1003 csi.ordering = mymalloc(24);
1004 strcpy(csi.registry,"Adobe");
1005 strcpy(csi.ordering, "Identity");
1006 csi.supplement = 0;
1007 switch ( rf->otftype ) {
1008 case 1:
1009 case 3:
1010 case 4:
1011 //Make Type2 CID Font (SFNT)
1012 maxp = tt_read_maxp_table(sfont);
1013 num_glyphs = maxp->numGlyphs;
1014 RELEASE(maxp);
1015 if (num_glyphs > 0) {
1016 head = tt_read_head_table(sfont);
1017 assert(head);
1018 hhea = tt_read_hhea_table(sfont);
1019 assert(hhea);
1020 if ( cid_partialdownload ) {
1021 CIDFont_type2_dofont(rf->PSname, sfont, rf->index, p->bitmap, &maxcid);
1022 head->indexToLocFormat = 1;
1023 }
1024 else {
1025 for (n = 1; n < num_glyphs; n++)
1026 ADD_TO_USED_CHARS(p->bitmap, n);
1027 maxcid = num_glyphs - 1;
1028 }
1029 CIDFont_type2_checktables(sfont);
1030 writecidtype2(sfont, head, hhea, &csi, /* p->bitmap, */ rf->PSname, maxcid);
1031 RELEASE(head);
1032 RELEASE(hhea);
1033 }
1034 break;
1035 case 2:
1036 head = tt_read_head_table(sfont);
1037 assert(head);
1038 //Make Type0 CID Font (CFF)
1039 offset = sfnt_find_table_pos(sfont, "CFF ");
1040 if (offset > 0) {
1041 maxp = tt_read_maxp_table(sfont);
1042 num_glyphs = maxp->numGlyphs;
1043 RELEASE(maxp);
1044 if (num_glyphs > 0) {
1045 cffont = cff_open(sfont->stream, offset, 0);
1046 if (!cffont)
1047 ERROR("Could not open CFF font for font %s", rf->Fontfile);
1048 if (!cid_partialdownload) {
1049 for (n = 1; n < num_glyphs; n++)
1050 ADD_TO_USED_CHARS(p->bitmap, n);
1051 }
1052 if ( cffont->flag & FONTTYPE_CIDFONT )
1053 buf_size = CIDFont_type0_dofont(rf->PSname, cffont, p->bitmap, &cdb, &buffer);
1054 else
1055 buf_size = CIDFont_type0_t1cdofont(rf->PSname, cffont, p->bitmap, &cdb, &buffer);
1056 writecidtype0(cffont, head, &csi, &cdb, buffer, buf_size);
1057 RELEASE(cdb.fds);
1058 RELEASE(buffer);
1059 cff_close(cffont);
1060 }
1061 }
1062 RELEASE(head);
1063 break;
1064 }
1065 if ( !noToUnicode && (((rf->cmap_fmt == 4) || (rf->cmap_fmt == 12)) || (rf->luamap_idx >= 0) )) {
1066 char *expath, *cmap_name, *cmap_ext, *tmp, cmap_tmp[260];
1067 #ifdef KPATHSEA
1068 char *path = xdirname(fulliname);
1069 #else
1070 char *path = mymalloc(strlen(fulliname) + 1);
1071 strcpy(path,fulliname);
1072 for ( n=(int)strlen(path); n>=0; n-- ) {
1073 if ( (path[n] == '\\') || (path[n] == '/') )
1074 break;
1075 }
1076 if ( n < 0 ) {
1077 strcpy(path,".");
1078 }
1079 else
1080 path[n] = 0;
1081 #endif
1082 expath = mymalloc(strlen(path) +32);
1083 strcpy(expath, path);
1084 strcat(expath, "/");
1085 strcat(expath, OTFMAPFILEDIR);
1086 free(path);
1087 if (access(expath, 0) == -1)
1088 #if defined(_WIN32) || defined(_WIN64)
1089 mkdir(expath);
1090 #else
1091 mkdir(expath, ACCESSPERMS);
1092 #endif
1093
1094 strcpy(csi.registry, "TeX");
1095 sprintf(csi.ordering, "%s+%03d", xname, curfnt->psname);
1096 cmap_name = mymalloc(260);
1097 for (n = strlen(fulliname); n >= 0; n--) {
1098 if ((fulliname[n] == '\\') || (fulliname[n] == '/'))
1099 break;
1100 }
1101 n++;
1102 strcpy(cmap_tmp, &fulliname[n]);
1103 tmp = strrchr(cmap_tmp, '.');
1104 if (tmp)
1105 *tmp = 0;
1106 sprintf(cmap_name, "%s-cid%03d", cmap_tmp, curfnt->psname);
1107 cmap_ext = mymalloc(16);
1108 strcpy(cmap_ext, "tounicode");
1109 p->bitmap[0] &= 0xFF7F;
1110 otf_create_ToUnicode_stream(sfont, rf->PSname, code_to_cid_cmap, &csi, p->bitmap, expath, cmap_name, cmap_ext, p->map_chain);
1111 if ( code_to_cid_cmap )
1112 CMap_release(code_to_cid_cmap);
1113 index = curfnt->psname;
1114 HASH_FIND_INT(OTF_list, &index, otfcmap); /* id already in the hash? */
1115 if (otfcmap==NULL) {
1116 otfcmap = (otfcmaptype*)malloc(sizeof(otfcmaptype));
1117 otfcmap->index = index;
1118 otfcmap->fontname = (char *)malloc(strlen(curfnt->resfont->PSname) + 1);
1119 strcpy(otfcmap->fontname,curfnt->resfont->PSname);
1120 otfcmap->cmapname = (char *)malloc(strlen(cmap_name) + 1);
1121 strcpy(otfcmap->cmapname, cmap_name);
1122 otfcmap->cmapext = (char *)malloc(strlen(cmap_ext) + 1);
1123 strcpy(otfcmap->cmapext, cmap_ext);
1124 HASH_ADD_INT(OTF_list, index, otfcmap);
1125 }
1126 if (expath)
1127 free(expath);
1128 free(cmap_name);
1129 free(cmap_ext);
1130 }
1131 free(csi.registry);
1132 free(csi.ordering);
1133 sfnt_close(sfont);
1134 fclose(fp);
1135 return 1;
1136 }