Debian Codegraph @

texk/dvipdfm-x/subfont.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <errno.h>
26
27 #include "system.h"
28 #include "mem.h"
29 #include "error.h"
30 #include "dpxconf.h"
31 #include "dpxfile.h"
32
33 #include "subfont.h"
34
35 /* Don't forget fontmap reading now requires information
36 * from SFD files. You must initialize at least sfd_file_
37 * cache before starting loading of fontmaps.
38 */
39
40 /* Subfont Definition File:
41 * struct sfd_file_ is for storing subfont identifiers
42 * contained in a SFD file and for mapping string pair
43 * <SFD_file, Subfont_id> to internal code mapping table
44 * ID which is index within an array of struct sfd_rec_.
45 * We store code mapping tables in different place than
46 * struct sfd_file_.
47 */
48 struct sfd_file_
49 {
50 char *ident; /* SFD file name */
51 char **sub_id; /* Subfont IDs */
52
53 int *rec_id; /* indices within struct sfd_rec_ array "sfd_record" */
54
55 int max_subfonts;
56 int num_subfonts;
57 };
58
59 /* Mapping table */
60 struct sfd_rec_
61 {
62 /* unsigned char misbit[32]; */
63 unsigned short vector[256]; /* 0 for undefined */
64 };
65
66 static void
67 init_sfd_file_ (struct sfd_file_ *sfd)
68 {
69 sfd->ident = NULL;
70 sfd->sub_id = NULL;
71 sfd->rec_id = NULL;
72 sfd->max_subfonts = sfd->num_subfonts = 0;
73 }
74
75 static void
76 clean_sfd_file_ (struct sfd_file_ *sfd)
77 {
78 int i;
79 if (sfd->ident)
80 RELEASE(sfd->ident);
81 if (sfd->sub_id) {
82 for (i = 0; i < sfd->num_subfonts; i++) {
83 if (sfd->sub_id[i])
84 RELEASE(sfd->sub_id[i]);
85 }
86 RELEASE(sfd->sub_id);
87 }
88 if (sfd->rec_id)
89 RELEASE(sfd->rec_id);
90 init_sfd_file_(sfd);
91 }
92
93 static struct sfd_file_ *sfd_files = NULL;
94 static int num_sfd_files = 0, max_sfd_files = 0;
95
96 static struct sfd_rec_ *sfd_record = NULL;
97 static int num_sfd_records = 0, max_sfd_records = 0;
98
99
100
101 /* Another buffer...
102 * We want buffer size at least 7 x 256 + a
103 * 4096 is usually enough.
104 */
105 #define LINE_BUF_SIZE 4096
106 static char line_buf[LINE_BUF_SIZE];
107
108 /* Each lines describes character code mapping for each
109 * subfonts. '#' is start of comment.
110 * SFD file format uses a '\' before newline sequence
111 * for line-continuation.
112 */
113 static char *
114 readline (char *buf, int buf_len, FILE *fp)
115 {
116 char *r, *q, *p = buf;
117 int n = 0, c = 0;
118
119 while (buf_len - n > 0 && (q = mfgets(p, buf_len - n, fp))) {
120 c++;
121 r = strchr(q, '#');
122 /* Comment is converted to single wsp (followed by a newline). */
123 if (r) {
124 *r = ' ';
125 *(r + 1) = '\0';
126 }
127 if (strlen(q) == 0)
128 break; /* empty line */
129 n += strlen(q);
130 q += strlen(q) - 1;
131 if (*q != '\\')
132 break;
133 else { /* line continued */
134 n -= 1;
135 p = buf + n;
136 }
137 }
138 if (n >= buf_len - 1) {
139 WARN("Possible buffer overflow in reading SFD file (buffer full, size=%d bytes)",
140 buf_len - 1);
141 }
142
143 return (c > 0 ? buf : NULL);
144 }
145
146 #define clear_vector(v) if ((v)) { \
147 int __i; \
148 for (__i = 0; __i < 256; __i++) \
149 (v)[__i] = 0; \
150 }
151
152 /* subfont_id ( integer ':' | integer '_' integer | integer )*
153 *
154 * 0x32: ==> Subsequent integers are place into slots starting at 0x32.
155 * 0x32: 0xA1A1 0xA1A2 ... ==> 0x32 is mappned to 0xA1A1, 0x33 to 0xA1A2
156 * 0xA1A1_0xA1A5 ==> Expanded to 0xA1A1 0xA1A2 ... 0xA1A5
157 */
158
159 /* subfont_id is already consumed here. */
160 static int
161 read_sfd_record (struct sfd_rec_ *rec, const char *lbuf)
162 {
163 const char *p = lbuf, *q;
164 char *r;
165 int repos = 0;
166 int c, v1 = 0, v2 = 0;
167 int curpos = 0;
168 int error = 0;
169
170 #define IS_TOKSEP(c) ((c) == '\0' || isspace((unsigned char)(c)))
171 for ( ; *p && isspace((unsigned char)*p); p++);
172 while (!error && *p) {
173 repos = 0; q = p;
174 v1 = strtol(p, &r, 0);
175 q = r;
176 if (q == p ||
177 (!IS_TOKSEP(*q) && *q != ':' && *q != '_')) {
178 WARN("Unknown token in subfont mapping table: %c", *q);
179 return -1;
180 }
181
182 switch (*q) {
183 case ':':
184 if (v1 < 0 || v1 > 0xff) {
185 WARN("Invalud value for subfont table offset: %ld", v1);
186 return -1;
187 }
188 repos = 1;
189 q++;
190 break;
191 case '_':
192 p = q + 1;
193 v2 = strtol(p, &r, 0);
194 q = r;
195 if (v1 < 0 || v1 > 0xffffL ||
196 v2 < 0 || v2 > 0xffffL) {
197 WARN("Invalid value in subfont mapping table: 0x%x_0x%x", v1, v2);
198 return -1;
199 } else if (q == p || !IS_TOKSEP(*q)) {
200 WARN("Invalid char in subfont mapping table: %c", *q);
201 return -1;
202 }
203 break;
204 default:
205 if (v1 < 0 || v1 > 0xffffL) {
206 WARN("Invalid character code in subfont mapping table: 0x%x", v1);
207 return -1;
208 }
209 v2 = v1;
210 break;
211 }
212
213 if (repos)
214 curpos = v1;
215 else {
216 if (v2 < v1 || curpos + (v2 - v1) > 0xff) {
217 WARN("Invalid range in subfont mapping: curpos=\"0x%02x\" range=\"0x%04x,0x%04x\"",
218 curpos, v1, v2);
219 return -1;
220 }
221 for (c = v1; c <= v2; c++) {
222 if (rec->vector[curpos] != 0) {
223 WARN("Subfont mapping for slot=\"0x%02x\" already defined...", curpos);
224 return -1;
225 }
226 ASSERT( curpos >= 0 && curpos <= 255 );
227 rec->vector[curpos++] = (unsigned short) c;
228 }
229 }
230 for (p = q; *p && isspace((unsigned char)*p); p++);
231 }
232
233 return error;
234 }
235
236 /* Scan for subfont IDs */
237 static int
238 scan_sfd_file (struct sfd_file_ *sfd, FILE *fp)
239 {
240 char *id;
241 char *q, *p;
242 int n, lpos = 0;
243
244 ASSERT( sfd && fp );
245
246 if (dpx_conf.verbose_level > 3) {
247 MESG("\nsubfont>> Scanning SFD file \"%s\"...\n", sfd->ident);
248 }
249
250 rewind(fp);
251 sfd->max_subfonts = sfd->num_subfonts = 0;
252 while ((p = readline(line_buf, LINE_BUF_SIZE, fp)) != NULL) {
253 lpos++;
254 for ( ; *p && isspace((unsigned char)*p); p++);
255 if (*p == 0)
256 continue; /* empty */
257
258 /* Saw non-wsp here */
259 for (n = 0, q = p; *p && !isspace((unsigned char)*p); p++, n++);
260 id = NEW(n + 1, char);
261 memcpy(id, q, n); id[n] = '\0';
262 if (sfd->num_subfonts >= sfd->max_subfonts) {
263 sfd->max_subfonts += 16;
264 sfd->sub_id = RENEW(sfd->sub_id, sfd->max_subfonts, char *);
265 }
266
267 if (dpx_conf.verbose_level > 3) {
268 MESG("subfont>> id=\"%s\" at line=\"%d\"\n", id, lpos);
269 }
270 sfd->sub_id[sfd->num_subfonts] = id;
271 sfd->num_subfonts++;
272 }
273
274 sfd->rec_id = NEW(sfd->num_subfonts, int);
275 for (n = 0; n < sfd->num_subfonts; n++) {
276 sfd->rec_id[n] = -1; /* Not loaded yet. We do lazy loading of map definitions. */
277 }
278
279 if (dpx_conf.verbose_level > 3) {
280 MESG("subfont>> %d entries found in SFD file \"%s\".\n", sfd->num_subfonts, sfd->ident);
281 }
282
283 return 0;
284 }
285
286
287 /* Open SFD file and gather subfont IDs. We do not read mapping tables
288 * here but only read subfont IDs used in SFD file.
289 */
290 static int
291 find_sfd_file (const char *sfd_name)
292 {
293 int id = -1;
294 int i, error = -1;
295
296 /* Check if we already opened SFD file */
297 for (i = 0; i < num_sfd_files; i++) {
298 if (!strcmp(sfd_files[i].ident, sfd_name)) {
299 id = i;
300 break;
301 }
302 }
303
304 if (id < 0) {
305 struct sfd_file_ *sfd = NULL;
306 FILE *fp;
307
308 if (num_sfd_files >= max_sfd_files) {
309 max_sfd_files += 8;
310 sfd_files = RENEW(sfd_files, max_sfd_files, struct sfd_file_);
311 }
312 sfd = &sfd_files[num_sfd_files];
313 init_sfd_file_(sfd);
314 sfd->ident = NEW(strlen(sfd_name) + 1, char);
315 strcpy(sfd->ident, sfd_name);
316 fp = DPXFOPEN(sfd->ident, DPX_RES_TYPE_SFD);
317 if (!fp) {
318 clean_sfd_file_(sfd);
319 return -1;
320 }
321 error = scan_sfd_file(sfd, fp);
322 DPXFCLOSE(fp);
323 if (!error)
324 id = num_sfd_files++;
325 else {
326 WARN("Error occured while reading SFD file \"%s\"", sfd_name);
327 clean_sfd_file_(sfd);
328 id = -1;
329 }
330 }
331
332 return id;
333 }
334
335 char **
336 sfd_get_subfont_ids (const char *sfd_name, int *num_ids)
337 {
338 int sfd_id;
339
340 if (!sfd_name)
341 return NULL;
342
343 sfd_id = find_sfd_file(sfd_name);
344 if (sfd_id < 0)
345 return NULL;
346
347 if (num_ids)
348 *num_ids = sfd_files[sfd_id].num_subfonts;
349 return sfd_files[sfd_id].sub_id;
350 }
351
352 /* Make sure that sfd_name does not have the extension '.sfd'.
353 * Mapping tables are actually read here.
354 */
355 int
356 sfd_load_record (const char *sfd_name, const char *subfont_id)
357 {
358 int rec_id = -1;
359 struct sfd_file_ *sfd;
360 FILE *fp;
361 int sfd_id, i, error = 0;
362 char *p, *q;
363
364 if (!sfd_name || !subfont_id)
365 return -1;
366
367 sfd_id = find_sfd_file(sfd_name);
368 if (sfd_id < 0)
369 return -1;
370
371 sfd = &sfd_files[sfd_id];
372 /* Check if we already loaded mapping table. */
373 for (i = 0;
374 i < sfd->num_subfonts && strcmp(sfd->sub_id[i], subfont_id); i++);
375 if (i == sfd->num_subfonts) {
376 WARN("Subfont id=\"%s\" not exist in SFD file \"%s\"...",
377 subfont_id, sfd->ident);
378 return -1;
379 } else if (sfd->rec_id[i] >= 0) {
380 return sfd->rec_id[i];
381 }
382
383 if (dpx_conf.verbose_level > 3) {
384 MESG("\nsubfont>> Loading SFD mapping table for <%s,%s>...",
385 sfd->ident, subfont_id);
386 }
387
388 /* reopen */
389 fp = DPXFOPEN(sfd->ident, DPX_RES_TYPE_SFD);
390 if (!fp) {
391 return -1;
392 /* ERROR("Could not open SFD file \"%s\"", sfd_name); */
393 }
394
395 /* Seek to record for 'sub_name'. */
396 while ((p = readline(line_buf, LINE_BUF_SIZE, fp))) {
397 for ( ; *p && isspace((unsigned char)*p); p++);
398 if (*p == 0)
399 continue; /* empty line */
400
401 /* q = parse_ident(&p, p + strlen(p)); */
402 for (q = p; *p && !isspace((unsigned char)*p); p++);
403 *p = '\0'; p++;
404 if (!strcmp(q, subfont_id)) {
405 if (num_sfd_records >= max_sfd_records) {
406 max_sfd_records += 16;
407 sfd_record = RENEW(sfd_record, max_sfd_records, struct sfd_rec_);
408 }
409 clear_vector(sfd_record[num_sfd_records].vector);
410 error = read_sfd_record(&sfd_record[num_sfd_records], p);
411 if (error)
412 WARN("Error occured while reading SFD file: file=\"%s\" subfont_id=\"%s\"",
413 sfd->ident, subfont_id);
414 else {
415 rec_id = num_sfd_records++;
416 }
417 }
418 }
419 if (rec_id < 0) {
420 WARN("Failed to load subfont mapping table for SFD=\"%s\" subfont_id=\"%s\"",
421 sfd->ident, subfont_id);
422 }
423 sfd->rec_id[i] = rec_id;
424 DPXFCLOSE(fp);
425
426 if (dpx_conf.verbose_level > 3) {
427 int __i;
428 if (rec_id >= 0) {
429 MESG(" at id=\"%d\"", rec_id);
430 MESG("\nsubfont>> Content of mapping table:");
431 for (__i = 0; __i < 256; __i++) {
432 if (__i % 16 == 0)
433 MESG("\nsubfont>> ");
434 MESG(" %04x", sfd_record[rec_id].vector[__i]);
435 }
436 }
437 MESG("\n");
438 }
439
440 return rec_id;
441 }
442
443
444 /* Lookup mapping table */
445 unsigned short
446 lookup_sfd_record (int rec_id, unsigned char c)
447 {
448 if (!sfd_record ||
449 rec_id < 0 || rec_id >= num_sfd_records)
450 ERROR("Invalid subfont_id: %d", rec_id);
451 return sfd_record[rec_id].vector[c];
452 }
453
454 void
455 release_sfd_record (void)
456 {
457 int i;
458
459 if (sfd_record) {
460 RELEASE(sfd_record);
461 }
462 if (sfd_files) {
463 for (i = 0; i < num_sfd_files; i++) {
464 clean_sfd_file_(&sfd_files[i]);
465 }
466 RELEASE(sfd_files);
467 }
468 sfd_record = NULL;
469 sfd_files = NULL;
470 num_sfd_records = max_sfd_records = 0;
471 num_sfd_files = max_sfd_files = 0;
472 }
473
474
475 #if DPXTEST
476 /* SFD file dumper */
477 #ifdef HAVE_ICONV
478 #include <iconv.h>
479 #else
480 typedef int iconv_t;
481 #endif
482 #include <string.h>
483
484 static void
485 dump_table (const char *sfd_name, const char *sub_name, iconv_t cd)
486 {
487 int rec_id, i;
488
489 rec_id = sfd_load_record(sfd_name, sub_name);
490 if (rec_id < 0) {
491 WARN("Could not load SFD mapping for \"%s\"", sub_name);
492 return;
493 }
494 fprintf(stdout, " <subfont id=\"%s\">\n", sub_name);
495 for (i = 0; i < 256; i++) {
496 unsigned short c = lookup_sfd_record(rec_id, i);
497 char *p, inbuf[2];
498 char *q, outbuf[32];
499 size_t r, inbufleft = 2, outbufleft = 32;
500
501 if (c == 0)
502 fprintf(stdout, " <!-- %02x: undefined -->", i);
503 else {
504 fprintf(stdout, " <a bi=\"%02x\" bo=\"%02x %02x\"", i, (c >> 8) & 0xff, c & 0xff);
505 if (cd != (iconv_t) -1) {
506 p = inbuf; q = outbuf;
507 inbuf[0] = (c >> 8) & 0xff;
508 inbuf[1] = c & 0xff;
509 #ifdef HAVE_ICONV
510 r = iconv(cd, &p, &inbufleft, &q, &outbufleft);
511 if (r == -1) {
512 if (dpx_conf.verbose_level > 0) {
513 WARN("Conversion to Unicode failed for subfont-id=\"%s\" code=\"0x%02x\"",
514 sub_name, i);
515 WARN(">> with: %s", strerror(errno));
516 }
517 } else {
518 outbuf[32-outbufleft] = 0;
519 fprintf(stdout, " uc=\"%s\"", outbuf);
520 }
521 #endif /* HAVE_ICONV */
522 }
523 fprintf(stdout, " />");
524 }
525 fprintf(stdout, "\n");
526 }
527 fprintf(stdout, " </subfont>\n");
528 return;
529 }
530
531 #define subfontDefinition_DTD "\
532 <!ELEMENT subfontDefinition (subfont+)>\n\
533 <!ATTLIST subfontDefinition\n\
534 id CDATA #REQUIRED\n\
535 output-encoding CDATA #IMPLIED\n\
536 >\n\
537 <!ELEMENT subfont (a*)>\n\
538 <!ATTLIST subfont\n\
539 id CDATA #REQUIRED\n\
540 >\n\
541 <!ELEMENT a EMPTY>\n\
542 <!ATTLIST a\n\
543 bi NMTOKENS #REQUIRED\n\
544 bo NMTOKENS #REQUIRED\n\
545 uc CDATA #IMPLIED\n\
546 >\
547 "
548
549 void
550 test_subfont_help (void)
551 {
552 fprintf(stdout, "usage: subfont [options] SFD_name\n");
553 fprintf(stdout, "-e, --encoding string\n");
554 fprintf(stdout, " Target (output) encoding of SFD mapping is 'string'.\n");
555 fprintf(stdout, " It must be an encoding name recognized by iconv.\n");
556 fprintf(stdout, " With this option write Unicode character in auxiliary attribute 'uc'.\n");
557 fprintf(stdout, "-s, --subfont-id string\n");
558 fprintf(stdout, " Load and dump mapping table only for subfont 'string'.\n");
559 }
560
561 int
562 test_subfont_main (int argc, char *argv[])
563 {
564 char *sfd_name = NULL, *sub_name = NULL;
565 char *from = NULL;
566 int i;
567 iconv_t cd = (iconv_t) -1;
568
569 for (;;) {
570 int c, optidx = 0;
571 static struct option long_options[] = {
572 {"encoding", 1, 0, 'e'}, /* for to-Unicode conversion */
573 {"subfont-id", 1, 0, 's'},
574 {"help", 0, 0, 'h'},
575 {0, 0, 0, 0}
576 };
577 c = getopt_long(argc, argv, "e:s:h", long_options, &optidx);
578 if (c == -1)
579 break;
580
581 switch (c) {
582 case 'e':
583 from = optarg;
584 break;
585 case 's':
586 sub_name = optarg;
587 break;
588 case 'h':
589 test_subfont_help();
590 return 0;
591 break;
592 default:
593 test_subfont_help();
594 return -1;
595 break;
596 }
597 }
598 if (optind < argc) {
599 sfd_name = argv[optind++];
600 }
601
602 if (sfd_name == NULL) {
603 WARN("No SFD file name specified.");
604 test_subfont_help();
605 return -1;
606 }
607 if (!from)
608 cd = (iconv_t) -1;
609 else {
610 #ifdef HAVE_ICONV
611 cd = iconv_open("utf-8", from);
612 if (cd == (iconv_t) -1) {
613 WARN("Can't open iconv conversion descriptor for %s --> utf-8", from);
614 return -1;
615 }
616 #else
617 WARN("Your system doesn't have iconv() in libc!");
618 #endif
619 }
620
621 fprintf(stdout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
622 fprintf(stdout, "<!DOCTYPE subfontDefinition [\n%s\n]>\n",
623 subfontDefinition_DTD);
624 fprintf(stdout, "<subfontDefinition id=\"%s\" output-encoding=\"%s\">\n",
625 sfd_name, from ? from : "unknown");
626 if (sub_name == NULL || !strcmp(sub_name, "all")) {
627 char **sub_id;
628 int num_ids = 0;
629 sub_id = sfd_get_subfont_ids(sfd_name, &num_ids);
630 if (!sub_id)
631 WARN("Could not open SFD file: %s", sfd_name);
632 else {
633 for (i = 0; i < num_ids; i++)
634 dump_table(sfd_name, sub_id[i], cd);
635 }
636 } else {
637 dump_table(sfd_name, sub_name, cd);
638 }
639 fprintf(stdout, "</subfontDefinition>\n");
640
641 #ifdef HAVE_ICONV
642 if (cd != (iconv_t) -1)
643 iconv_close(cd);
644 #endif
645
646 return 0;
647 }
648
649 #endif /* DPXTEST */