Debian Codegraph @

texk/xdvik/font-open.c

raw
1 /*
2 font-open.c: find font files. The routine font_open() itself bears
3 no relation (except for the interface) to the original font_open().
4
5 Copyright (c) 1999-2013 The texk project
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to
9 deal in the Software without restriction, including without limitation the
10 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 sell copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 */
26
27 #include "xdvi-config.h"
28 #include "xdvi.h"
29 #include "dvi-draw.h"
30 #include "util.h"
31 #include "events.h"
32 #include "dvi-init.h"
33 #include "my-snprintf.h"
34 #include "print-log.h"
35
36 #include "statusline.h"
37 #include "message-window.h"
38 #include "font-open.h"
39
40 #include "kpathsea/c-fopen.h"
41 #include "kpathsea/tex-glyph.h"
42
43 #include <stdlib.h>
44 #include <ctype.h>
45
46 #if HAVE_SYS_WAIT_H
47 # include <sys/wait.h>
48 #endif
49 #ifndef WIFEXITED
50 # define WIFEXITED(status) (((status) & 255) == 0)
51 #endif
52 #ifndef WEXITSTATUS
53 # define WEXITSTATUS(status) ((unsigned)(status) >> 8)
54 #endif
55 #ifndef WIFSIGNALED
56 # ifndef WIFSTOPPED
57 # define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
58 # endif
59 # define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
60 #endif
61 #ifndef WTERMSIG
62 # define WTERMSIG(status) ((status) & 0x7f)
63 #endif
64
65 /* if POSIX O_NONBLOCK is not available, use O_NDELAY */
66 #if !defined(O_NONBLOCK) && defined(O_NDELAY)
67 # define O_NONBLOCK O_NDELAY
68 #endif
69
70 #ifdef EWOULDBLOCK
71 # ifdef EAGAIN
72 # define AGAIN_CONDITION (errno == EWOULDBLOCK || errno == EAGAIN)
73 # else
74 # define AGAIN_CONDITION (errno == EWOULDBLOCK)
75 # endif
76 #else /* EWOULDBLOCK */
77 # ifdef EAGAIN
78 # define AGAIN_CONDITION (errno == EAGAIN)
79 # endif
80 #endif /* EWOULDBLOCK */
81
82 #if HAVE_POLL
83 # include <poll.h>
84 # define XIO_IN POLLIN
85 # define XIO_OUT POLLOUT
86 #else
87 # define XIO_IN 1
88 # define XIO_OUT 2
89 #endif /* HAVE_POLL */
90
91
92 /* see just above the second init_t1_lookup for why this was chosen */
93 #define DVIPS_SETUP 1
94
95 #if FREETYPE || PS
96
97 /*
98 * The following code handles lookup of Type 1 fonts for use as FreeType
99 * fonts, and for use within MetaPost output.
100 * The system psfonts.map file is read into an AVL tree as needed (lazy
101 * evaluation), and searched for Type 1 fonts.
102 */
103
104 struct p_list { /* list of map file names */
105 struct p_list *next;
106 const char *value;
107 };
108
109 /* Initialize this list to "psfonts.map". */
110
111 static struct p_list psfonts_map = {NULL, "psfonts.map"};
112
113 static struct p_list *p_head = &psfonts_map;
114 static struct p_list **p_tail = &psfonts_map.next;
115
116 static FILE *mapfile = NULL;
117
118 static char *ffline = NULL; /* an array used to store */
119 /* the file name being formed. */
120 /* It expands as needed. */
121 /* Also used elsewhere. */
122 static size_t ffline_len = 0; /* current length of ffline[] */
123
124 /*
125 * Expand ffline[] to at least the given size.
126 */
127
128 static void
129 expandline(int n)
130 {
131 int newlen = n + 128;
132
133 ffline = (ffline == NULL) ? xmalloc(newlen) : xrealloc(ffline, newlen);
134 ffline_len = newlen;
135 }
136
137
138 /*
139 * Like fgets(), but read an arbitrarily long line into ffline[].
140 */
141
142 static Boolean
143 fgets_long(FILE *f)
144 {
145 int len;
146
147 if (fgets(ffline, ffline_len, f) == NULL)
148 return False;
149
150 len = 0;
151 for (;;) {
152 len += strlen(ffline + len);
153 if (len > 0 && ffline[len - 1] == '\n') {
154 ffline[--len] = '\0';
155 break;
156 }
157 if (len < ffline_len - 1)
158 break;
159 expandline(len);
160 fgets(ffline + len, ffline_len - len, f);
161 }
162
163 return True;
164 }
165
166 # if DVIPS_SETUP
167
168 /*
169 * Get list of map files from dvips config file(s).
170 */
171
172 static void
173 getdefaults(FILE *f)
174 {
175 char *p, *q;
176 int len;
177 struct p_list *p_node;
178
179 while (fgets_long(f)) {
180 p = ffline;
181 while (*p == ' ' || *p == '\t') ++p;
182 if (*p == 'p') {
183 do ++p;
184 while (*p == ' ' || *p == '\t');
185
186 if (*p == '+')
187 do ++p;
188 while (*p == ' ' || *p == '\t');
189 else { /* discard old list */
190 struct p_list *pl, *pl2;
191
192 *p_tail = NULL;
193 pl = p_head;
194 if (pl == &psfonts_map) pl = pl->next;
195 while (pl != NULL) {
196 free((char *) pl->value);
197 pl2 = pl->next;
198 free(pl);
199 pl = pl2;
200 }
201 p_tail = &p_head;
202 }
203
204 /* get white-delimited argument */
205 len = strlen(p);
206 q = memchr(p, ' ', len);
207 if (q != NULL) len = q - p;
208 q = memchr(p, '\t', len);
209 if (q != NULL) len = q - p;
210 p[len] = '\0';
211
212 p_node = xmalloc(sizeof *p_node);
213 p_node->value = xmemdup(p, len + 1);
214 *p_tail = p_node;
215 p_tail = &p_node->next;
216 }
217 }
218
219 fclose(f);
220 }
221
222 # endif /* DVIPS_SETUP */
223
224 /*
225 * Open the next (available) map file.
226 */
227
228 static Boolean
229 open_next_mapfile(void)
230 {
231 char *filename;
232
233 /*
234 * Look for the first (openable) map file. Others will be read later
235 * as needed.
236 */
237
238 while (p_head != NULL) {
239 filename = kpse_find_file(p_head->value, kpse_program_text_format,
240 True);
241 if (filename == NULL)
242 filename = kpse_find_file(p_head->value, kpse_fontmap_format,
243 True);
244 if (filename == NULL) {
245 XDVI_WARNING((stderr,
246 "could not find dvips map file %s; skipping\n",
247 p_head->value));
248 }
249 else {
250 mapfile = XFOPEN(filename, OPEN_MODE);
251 if (mapfile == NULL) {
252 XDVI_WARNING((stderr,
253 "could not open dvips map file %s: %s\n",
254 filename, strerror(errno)));
255 free(filename);
256 }
257 else {
258 TRACE_FT((stderr, "Map file: %s\n", filename));
259 free(filename);
260 return True;
261 }
262 }
263 p_head = p_head->next;
264 }
265
266 return False;
267 }
268
269
270 /*
271 * Initialize these lookup routines.
272 */
273
274 # if DVIPS_SETUP
275
276 Boolean
277 init_t1_lookup(void)
278 {
279 char *filename;
280 struct stat statbuf;
281 FILE *f;
282 char *dvipsrc;
283
284 if (ffline == NULL) expandline(80);
285
286 filename = find_file("config.ps", &statbuf, kpse_dvips_config_format);
287 if (filename == NULL) {
288 TRACE_FT((stderr, "could not find file config.ps; skipping"));
289 }
290 else {
291 f = XFOPEN(filename, OPEN_MODE);
292 if (f == NULL) {
293 TRACE_FT((stderr, "could not open file %s: %s",
294 filename, strerror(errno)));
295 }
296 else
297 getdefaults(f);
298 free(filename);
299 }
300
301 dvipsrc = getenv("DVIPSRC");
302 if (dvipsrc == NULL) {
303 dvipsrc = getenv("HOME");
304 if (dvipsrc != NULL) {
305 size_t n;
306
307 n = strlen(dvipsrc);
308 if (n + 10 > ffline_len) expandline(n + 10);
309 memcpy(ffline, dvipsrc, n);
310 memcpy(ffline + n, "/.dvipsrc", 10);
311 dvipsrc = ffline;
312 }
313 }
314 if (dvipsrc != NULL) {
315 f = XFOPEN(dvipsrc, OPEN_MODE);
316 if (f == NULL) {
317 TRACE_FT((stderr,
318 "could not open dvipsrc file %s: %s; skipping",
319 dvipsrc, strerror(errno)));
320 }
321 else
322 getdefaults(f);
323 }
324
325 filename = find_file("config.xdvi", &statbuf, kpse_dvips_config_format);
326 if (filename == NULL) {
327 TRACE_FT((stderr, "could not find file config.xdvi; skipping"));
328 }
329 else {
330 f = XFOPEN(filename, OPEN_MODE);
331 if (f == NULL) {
332 TRACE_FT((stderr, "could not open file %s: %s",
333 filename, strerror(errno)));
334 }
335 else
336 getdefaults(f);
337 free(filename);
338 }
339
340 *p_tail = NULL;
341
342 return open_next_mapfile();
343 }
344
345 # else /* !DVIPS_SETUP */
346
347 /*
348 * Under the earlier regime, T1 lookup was controlled by xdvi.cfg.
349 *
350 * This has been disabled, for the following reasons.
351 *
352 * It only supported "enc" and "dvipsmap" directives.
353 *
354 * The "enc" directives selected implicit encoding files for fonts ending
355 * in certain character strings (8r, 8c, 8y), unless the map file
356 * explicitly referenced a .enc file. However, among the dvips map files
357 * supplied with TeX Live 2012 (at least) there were no such fonts ending
358 * in 8r and 8y, and the encoding file for the 8c fonts was cork.enc,
359 * which is no longer included in TeX Live.
360 *
361 * As for the dvipsmap directives, it seemed better to use the dvips
362 * configuration method.
363 */
364
365 Boolean
366 init_t1_lookup(void)
367 {
368 char *filename;
369 FILE *fp;
370 char *keyword;
371 char *ptr;
372 struct p_list *p_node;
373 char *enc;
374 char *name;
375 static const char delim[] = "\t \n\r";
376
377 if (ffline == NULL) expandline(80);
378
379 filename = kpse_find_file("xdvi.cfg", kpse_program_text_format, 1);
380 if (filename == NULL) {
381 statusline_error(STATUS_MEDIUM,
382 "Warning: Unable to find \"xdvi.cfg\"!");
383 return open_next_mapfile();
384 }
385
386 if ((fp = XFOPEN(filename, "r")) == NULL) {
387 XDVI_ERROR((stderr, "Cannot open config file `%s' for reading: %s",
388 filename, strerror(errno)));
389 free(filename);
390 return open_next_mapfile();
391 }
392
393 TRACE_FT((stderr, "Reading cfg file %s", filename));
394
395 while (fgets_long(fp)) {
396 keyword = ffline;
397
398 /* Skip leading whitespace */
399 while (*keyword == ' ' || *keyword == '\t')
400 keyword++;
401
402 /* % in first column is a correct comment */
403 if (*keyword == '%' || *keyword == '\0' || *keyword == '\n')
404 continue;
405
406 keyword = strtok(keyword, delim);
407
408 if (strcmp(keyword, "dvipsmap") == 0) {
409 if ((ptr = strtok(NULL, delim)) == NULL) {
410 XDVI_WARNING((stderr, "Syntax error in entry \"%s\"",
411 ffline));
412 continue;
413 }
414 TRACE_FT((stderr, "DVIPSMAP: '%s'", ptr));
415
416 /* Add it to the list for later reading */
417 p_node = xmalloc(sizeof *p_node);
418 p_node->value = xmemdup(ptr, strlen(ptr) + 1);
419 /* If we're still pointing to the default list, remove it */
420 if (p_tail == &psfonts_map.next)
421 p_tail = &p_head;
422 *p_tail = p_node;
423 p_tail = &p_node->next;
424 }
425 else if (strcmp(keyword, "encmap") == 0) {
426 popup_message(globals.widgets.top_level,
427 MSG_ERR,
428 "Your xdvi.cfg file is for a previous version "
429 "of xdvik. Please replace it with the xdvi.cfg file "
430 "in the current xdvik distribution.",
431 "The keyword \"encmap\" in xdvi.cfg is no longer "
432 "supported. Please update the config file %s.",
433 filename);
434 }
435 else if (strcmp(keyword, "enc") == 0) {
436 enc = strtok(NULL, delim);
437 name = strtok(NULL, delim);
438 if ((ptr = strtok(NULL, delim)) == NULL) {
439 XDVI_WARNING((stderr,
440 "Syntax error in entry \"%s\" (skipping line)",
441 ffline));
442 continue;
443 }
444 # if 0
445 i = new_encoding(enc, ptr);
446 TRACE_FT((stderr, "Encoding[%d]: '%s' = '%s' -> '%s'",
447 i, enc, name, ptr));
448 # endif
449 } else {
450 /* again, nag them with a popup so that they'll do something
451 about this ... */
452 popup_message(globals.widgets.top_level,
453 MSG_ERR,
454 "Please check the syntax of your config file. "
455 "Valid keywords are: \"enc\" and \"dvipsmap\".",
456 "Skipping unknown keyword \"%s\" in config file %s.",
457 keyword, filename);
458 }
459 }
460 *p_tail = NULL; /* terminate linked list of map files */
461
462 fclose(fp);
463 free(filename);
464
465 return open_next_mapfile();
466 }
467
468 # endif /* not DVIPS_SETUP */
469
470
471 /*
472 * Information about Type 1 fonts is stored in an AVL tree.
473 */
474
475 static struct avl_t1 *t1_head = NULL;
476
477
478 /*
479 * Parse line from psfonts.map file.
480 */
481
482 static struct avl_t1 *
483 dvips_parse(const char *line)
484 {
485 const char *w1p, *encp, *pfp, *qp;
486 size_t w1l, encl, pfl, ql;
487 const char *w2p;
488 size_t w2l;
489 const char *p, *p0;
490 const char *err;
491 struct avl_t1 *t1p;
492 char *q;
493
494 w2l = w1l = encl = pfl = ql = 0;
495 err = NULL;
496 p = line;
497 for (;;) { /* loop over words */
498 while (*p == ' ' || *p == '\t') ++p;
499 if (*p == '\0')
500 break;
501
502 if (*p == '"') { /* quoted string */
503 const char *p_end;
504
505 p0 = p + 1;
506 p_end = p0 + strlen(p0);
507 p = memchr(p0, '"', p_end - p0);
508 if (p == NULL) p = p_end;
509 qp = (ql == 0 ? p0 : NULL);
510 ql += p - p0 + 1;
511 if (*p == '"') ++p;
512 continue;
513 }
514
515 if (*p == '<') { /* encoding or pfa/b file */
516 int wtype = 0;
517
518 ++p;
519 if (*p == '<') {
520 wtype = 1; /* font file */
521 ++p;
522 }
523 else if (*p == '[') {
524 wtype = -1; /* encoding file */
525 ++p;
526 }
527
528 /* find word */
529 while (*p == ' ' || *p == '\t') ++p;
530 p0 = p;
531 while (*p != '\0' && *p != ' ' && *p != '\t') ++p;
532
533 if (wtype == 0 && p > p0 + 4 && p[-4] == '.') {
534 if (memcmp(p - 3, "enc", 3) == 0
535 || memcmp(p - 3, "ENC", 3) == 0)
536 wtype = -1;
537 else if (memcmp(p - 3, "pfa", 3) == 0
538 || memcmp(p - 3, "pfb", 3) == 0
539 || memcmp(p - 3, "PFA", 3) == 0
540 || memcmp(p - 3, "PFB", 3) == 0)
541 wtype = 1;
542 }
543
544 if (wtype > 0) {
545 if (pfl != 0)
546 err = "more than one font file given";
547 else {
548 pfp = p0;
549 pfl = p - p0 + 1;
550 }
551 }
552 else if (wtype < 0) {
553 if (encl != 0)
554 err = "more than one encoding file given";
555 else {
556 encp = p0;
557 encl = p - p0 + 1;
558 }
559 }
560 else
561 err = "cannot identify file type";
562 }
563 else { /* if ordinary word */
564 p0 = p;
565 while (*p != '\0' && *p != ' ' && *p != '\t') ++p;
566 if (w1l == 0) {
567 w1p = p0;
568 w1l = p - p0;
569 }
570 else if (w2l == 0) {
571 w2p = p0;
572 w2l = p - p0;
573 }
574 else
575 err = "more than two non-download words given";
576 }
577 } /* end loop over words */
578
579 if (w1l == 0) {
580 TRACE_FT((stderr,
581 "map file %s: line \"%s\" does not give a font name.",
582 p_head->value, line));
583 return NULL;
584 }
585
586 if (err != NULL) {
587 TRACE_FT((stderr, "map file %s, font %.*s: %s", p_head->value,
588 (int) w1l, w1p, err));
589 return NULL;
590 }
591
592 t1p = (struct avl_t1 *) avladd(w1p, w1l, (struct avl **) &t1_head,
593 sizeof(struct avl_t1));
594
595 if (t1p->key != w1p) { /* if existing record */
596 TRACE_FT((stderr,
597 "map file %s, font %.*s: duplicate record; using first one",
598 p_head->value, (int) w1l, w1p));
599 return NULL;
600 }
601
602 t1p->key = q = xmalloc(w1l + w2l + 1 + pfl + encl + ql);
603
604 memcpy(q, w1p, w1l);
605 q += w1l;
606 t1p->psname = t1p->key;
607 if (w2l != 0) {
608 t1p->psname = q;
609 memcpy(q, w2p, w2l);
610 q += w2l;
611 }
612 *q++ = '\0';
613
614 t1p->fontfile = NULL;
615 if (pfl != 0) {
616 t1p->fontfile = q;
617 memcpy(q, pfp, pfl - 1);
618 q += pfl;
619 q[-1] = '\0';
620 }
621
622 t1p->encname = t1p->addinfo = NULL;
623 # if FREETYPE
624 t1p->bad = False;
625 t1p->ft = NULL;
626 # endif
627
628 if (encl != 0) {
629 t1p->encname = q;
630 memcpy(q, encp, encl - 1);
631 q += encl;
632 q[-1] = '\0';
633 }
634
635 if (ql != 0) {
636 t1p->addinfo = q;
637 if (qp != 0) {
638 memcpy(q, qp, ql - 1);
639 q += ql;
640 }
641 else { /* multiple quoted strings; rescan to get them */
642 const char *p_end;
643
644 p = line;
645 p_end = p + strlen(p);
646 for (;;) {
647 while (*p == ' ' || *p == '\t') ++p;
648 if (*p == '\0')
649 break;
650
651 /* found a word */
652 if (*p == '"') {
653 ++p;
654 p0 = p;
655 p = memchr(p0, '"', p_end - p0);
656 if (p == NULL) p = p_end;
657 memcpy(q, p0, p - p0);
658 q += p - p0;
659 *q++ = ' ';
660 if (*p == '\0') break;
661 ++p;
662 }
663 else /* skip unquoted word */
664 while (*p != '\0' && *p != ' ' && *p != '\t') ++p;
665 }
666 }
667 q[-1] = '\0';
668 }
669
670 return t1p;
671 }
672
673 /*
674 * Information on the Ghostscript font aliasing mechanism is kept in
675 * another AVL tree. Each identifier points to a list of strings,
676 * up to one from each Fontmap file. Within a Fontmap file, later entries
677 * for a given string override earlier ones.
678 */
679
680 struct avl_gs { /* structure for gs font information */
681 AVL_COMMON;
682 short fontmap_number; /* sequence of most recent entry */
683 Boolean in_use;
684 struct gs_list *list;
685 };
686
687 struct gs_list { /* list of Fontmap entries for this font name */
688 struct gs_list *next;
689 const char value[0];
690 };
691
692 static struct avl_gs *gs_head = NULL;
693
694 typedef void (*gs_path_proc)(FILE *);
695
696 static const char *env_gs_lib;
697
698 static FILE *
699 gs_try_fopen(const char *str, unsigned int len, const char *name)
700 {
701 unsigned int namelen = strlen(name) + 1;
702 FILE *f;
703
704 if (len + namelen + 1 > ffline_len) expandline(len + namelen + 1);
705 memcpy(ffline, str, len);
706 ffline[len] = '/';
707 memcpy(ffline + len + 1, name, namelen);
708
709 f = XFOPEN(ffline, OPEN_MODE);
710
711 TRACE_FT((stderr, "gs_try_fopen: %s: %s", ffline,
712 f != NULL ? "file opened" : strerror(errno)));
713
714 return f;
715 }
716
717 static FILE *
718 gs_path_fopen(const char *name, gs_path_proc proc)
719 {
720 const char *str1 = env_gs_lib;
721 const char *str2 = DEFAULT_GS_LIB_PATH;
722 const char *str1_end, *str2_end;
723 const char *p1, *p2;
724 FILE *f;
725 unsigned int namelen;
726
727 if (str1 == NULL) {
728 str1 = str2;
729 str2 = NULL;
730 }
731
732 str1_end = str1 + strlen(str1);
733
734 for (;;) {
735 p1 = memchr(str1, ':', str1_end - str1);
736 if (p1 == NULL) p1 = str1_end;
737 if (p1 == str1) {
738 if (str2 != NULL) {
739 str2_end = str2 + strlen(str2);
740 for (;;) {
741 p2 = memchr(str2, ':', str2_end - str2);
742 if (p2 == NULL) p2 = str2_end;
743 if (p2 > str2) {
744 f = gs_try_fopen(str2, p2 - str2, name);
745 if (f != NULL) {
746 if (proc != NULL)
747 proc(f);
748 else
749 return f;
750 }
751 }
752 if (*p2 == '\0')
753 break;
754 str2 = p2 + 1;
755 }
756 str2 = NULL;
757 }
758 }
759 else {
760 f = gs_try_fopen(str1, p1 - str1, name);
761 if (f != NULL) {
762 if (proc != NULL)
763 proc(f);
764 else
765 return f;
766 }
767 }
768
769 if (*p1 == '\0')
770 break;
771 str1 = p1 + 1;
772 }
773
774 /* leave the file name in ffline[] for error message */
775 namelen = strlen(name) + 1;
776 if (namelen > ffline_len) expandline(namelen);
777 memcpy(ffline, name, namelen);
778
779 return NULL;
780 }
781
782
783 static FILE *
784 lookup_gs_font(const char *font, const char **path_ret)
785 {
786 struct avl_gs *gsfp;
787 int font_len;
788 int i;
789
790 font_len = strlen(font);
791 gsfp = gs_head;
792 for (;;) {
793 if (gsfp == NULL) /* if not found */
794 return NULL;
795
796 i = font_len - gsfp->key_len;
797 if (i == 0)
798 i = memcmp(font, gsfp->key, font_len);
799 if (i == 0) { /* if found */
800 struct gs_list *gl, *gl2, *gl3;
801 FILE *f;
802
803 if (gsfp->in_use) {
804 TRACE_FT((stderr, "Alias loop for %s detected; ignoring.",
805 font));
806 return NULL;
807 }
808
809 /* If we haven't done it yet, reverse the linked list */
810 if (gsfp->fontmap_number != 0) {
811 gsfp->fontmap_number = 0;
812 gl = gsfp->list;
813 gl2 = NULL;
814 while (gl != NULL) {
815 gl3 = gl->next;
816 gl->next = gl2;
817 gl2 = gl;
818 gl = gl3;
819 }
820 gsfp->list = gl2;
821 }
822 gsfp->in_use = True;
823 f = NULL;
824 for (gl = gsfp->list; gl != NULL; gl = gl->next) {
825 if (gl->value[0] == '\0') { /* if alias */
826 TRACE_FT((stderr, "Found alias %s --> %s",
827 font, gl->value + 1));
828 f = lookup_gs_font(gl->value + 1, path_ret);
829 if (f == NULL)
830 TRACE_FT((stderr, "Alias %s not found.",
831 gl->value + 1));
832 if (f != NULL) break;
833 }
834 else {
835 TRACE_FT((stderr, "Checking file %s", gl->value));
836 if (gl->value[0] == '/' || (gl->value[0] == '.'
837 && (gl->value[1] == '/' || (gl->value[1] == '.'
838 && gl->value[2] == '/')))) {
839 f = XFOPEN(gl->value, OPEN_MODE);
840 if (f != NULL) {
841 *path_ret = xstrdup(gl->value);
842 break;
843 }
844 }
845 else {
846 f = gs_path_fopen(gl->value, NULL);
847 if (f != NULL) {
848 *path_ret = xstrdup(ffline);
849 break;
850 }
851 }
852 }
853 }
854 gsfp->in_use = False;
855 return f;
856 }
857 gsfp = (struct avl_gs *) (i < 0 ? gsfp->left : gsfp->right);
858 }
859 }
860
861
862 #define GS_BUF_SIZE 4096
863
864 struct gsfile {
865 FILE *f;
866 unsigned char *buffer;
867 const unsigned char *bufpos;
868 const unsigned char *buf_end;
869 };
870
871 static Boolean
872 gs_fillbuf(struct gsfile *gsf)
873 {
874 unsigned char *p;
875 unsigned int len;
876
877 if (gsf->buf_end < gsf->buffer + GS_BUF_SIZE)
878 return False;
879
880 gsf->bufpos = p = gsf->buffer;
881 for (;;) {
882 len = gsf->buf_end - p;
883 if (len <= 0) break;
884 len = fread(p, 1, len, gsf->f);
885 if (len <= 0) break;
886 p += len;
887 }
888 gsf->buf_end = p;
889 return (p > gsf->buffer);
890 }
891
892 static unsigned char gs_ctype[256];
893
894 #define GS_EOF '\0'
895 #define GS_ERR '%'
896 #define LPAREN '('
897 #define RPAREN ')'
898
899 static void
900 init_gs_ctype(void)
901 {
902 const char *p;
903
904 for (p = " \t\f\n\r";; ++p) {
905 gs_ctype[(unsigned char) *p] = 1; /* white space */
906 if (*p == '\0') break;
907 }
908 gs_ctype['/'] = 2; /* literal token */
909 gs_ctype['('] = 3; /* string */
910 for (p = ")<>[]{}%"; *p != '\0'; ++p)
911 gs_ctype[(unsigned char) *p] = 4; /* delimiter */
912 }
913
914 static unsigned char
915 get_gs_character(struct gsfile *gsfp)
916 {
917 unsigned char c;
918
919 for (;;) {
920 if (gsfp->bufpos >= gsfp->buf_end && !gs_fillbuf(gsfp))
921 return '%';
922 c = *gsfp->bufpos++;
923
924 /* Check for comments */
925 if (c == '%') {
926 for (;;) {
927 const unsigned char *p1, *p2;
928
929 p1 = memchr(gsfp->bufpos, '\n',
930 gsfp->buf_end - gsfp->bufpos);
931 if (p1 == NULL) p1 = gsfp->buf_end;
932 p2 = memchr(gsfp->bufpos, '\r', p1 - gsfp->bufpos);
933 if (p2 != NULL) p1 = p2;
934 p2 = memchr(gsfp->bufpos, '\f', p1 - gsfp->bufpos);
935 if (p2 != NULL) p1 = p2;
936 if (p1 < gsfp->buf_end) {
937 gsfp->bufpos = p1 + 1;
938 break;
939 }
940 if (!gs_fillbuf(gsfp))
941 return '%';
942 }
943 continue;
944 }
945
946 if (gs_ctype[c] != 1)
947 break;
948 }
949
950 return c;
951 }
952
953 static unsigned char
954 get_gs_token(struct gsfile *gsfp,
955 unsigned int pos,
956 unsigned int *pos_ret,
957 const char *file_type)
958 {
959 unsigned char gs_t_type;
960 unsigned char c;
961 const unsigned char *p0;
962 unsigned int pos0;
963 unsigned int depth;
964
965 gs_t_type = c = get_gs_character(gsfp);
966 if (c == '%')
967 return GS_EOF;
968
969 p0 = gsfp->bufpos;
970 switch (gs_ctype[c]) {
971 case 0: /* most characters */
972 case 2: /* '/' */
973 --p0; /* retain initial character */
974 pos0 = pos;
975 for (;;) {
976 if (gsfp->bufpos >= gsfp->buf_end) {
977 unsigned int len = gsfp->bufpos - p0;
978
979 if (pos + len >= ffline_len) expandline(pos + len);
980 memmove(ffline + pos, p0, len);
981 pos += len;
982 if (!gs_fillbuf(gsfp))
983 break;
984 p0 = gsfp->buffer;
985 }
986 if (gs_ctype[*gsfp->bufpos] != 0) {
987 unsigned int len = gsfp->bufpos - p0;
988
989 if (pos + len >= ffline_len) expandline(pos + len);
990 memmove(ffline + pos, p0, len);
991 pos += len;
992 break;
993 }
994 ++gsfp->bufpos;
995 }
996 /* Filter out DOS ^Z */
997 if (pos == pos0 + 1 && ffline[pos0] == '\032')
998 return GS_EOF;
999 break;
1000
1001 case 3: /* left parenthesis */
1002 depth = 1;
1003 for (;;) {
1004 const unsigned char *p1, *p2, *p3;
1005
1006 if (gsfp->bufpos >= gsfp->buf_end) {
1007 unsigned int len = gsfp->bufpos - p0;
1008
1009 if (pos + len >= ffline_len) expandline(pos + len);
1010 memmove(ffline + pos, p0, len);
1011 pos += len;
1012 if (!gs_fillbuf(gsfp)) {
1013 TRACE_FT((stderr,
1014 "unterminated string in %s file; giving up.",
1015 file_type));
1016 return GS_ERR;
1017 }
1018 p0 = gsfp->buffer;
1019 }
1020 p1 = memchr(gsfp->bufpos, RPAREN,
1021 gsfp->buf_end - gsfp->bufpos);
1022 if (p1 == NULL) p1 = gsfp->buf_end;
1023 for (;;) {
1024 p2 = memchr(gsfp->bufpos, LPAREN, p1 - gsfp->bufpos);
1025 if (p2 == NULL) p2 = p1;
1026 p3 = p2;
1027 for (;;) {
1028 if (p3 <= gsfp->bufpos) {
1029 if (c == '\\') --p3;
1030 break;
1031 }
1032 if (p3[-1] != '\\') break;
1033 --p3;
1034 }
1035 c = '\\' - 1 + ((p2 - p3) & 1);
1036 if (p2 >= p1)
1037 break;
1038 if (c != '\\')
1039 ++depth;
1040 gsfp->bufpos = p2 + 1;
1041 c = '\0';
1042 }
1043 if (p1 < gsfp->buf_end) { /* if left parenthesis at p1 */
1044 if (c != '\\') {
1045 if (--depth == 0) {
1046 unsigned int len = p1 - p0;
1047
1048 if (pos + len >= ffline_len)
1049 expandline(pos + len);
1050 memmove(ffline + pos, p0, len);
1051 pos += len;
1052 gsfp->bufpos = p1 + 1;
1053 break;
1054 }
1055 }
1056 ++p1;
1057 }
1058 gsfp->bufpos = p1;
1059 }
1060 /* We could do backslash escaping here, but it's probably
1061 unnecessary. */
1062 break;
1063
1064 default:
1065 TRACE_FT((stderr,
1066 "invalid character `%c' encountered in %s file; giving up.",
1067 c, file_type));
1068 return GS_ERR;
1069 }
1070
1071 *pos_ret = pos;
1072 return gs_t_type;
1073 }
1074
1075
1076 static short gs_fontmap_number = 0;
1077
1078 static void
1079 process_gs_fontmap(FILE *f)
1080 {
1081 struct gsfile gsf;
1082 unsigned char buffer[GS_BUF_SIZE];
1083 unsigned char ttype;
1084 unsigned int pos1, pos2, pos3;
1085
1086 ++gs_fontmap_number;
1087
1088 gsf.f = f;
1089 gsf.buffer = buffer;
1090 gsf.bufpos = gsf.buf_end = buffer + GS_BUF_SIZE;
1091
1092 /*
1093 * Allow entries of the following types:
1094 *
1095 * (string) .runlibfile
1096 * (string) .runlibfileifexists
1097 * /identifier (string) ;
1098 * /identifier /alias ;
1099 */
1100
1101 for (;;) {
1102 ttype = get_gs_token(&gsf, 0, &pos1, "Fontmap");
1103 if (ttype == GS_EOF || ttype == GS_ERR)
1104 break;
1105 if (ttype == LPAREN) {
1106 Boolean quiet = False;
1107 FILE *f1;
1108
1109 ttype = get_gs_token(&gsf, pos1, &pos2, "Fontmap");
1110 if (ttype == GS_ERR)
1111 break;
1112 if (ttype == GS_EOF) {
1113 TRACE_FT((stderr,
1114 "unexpected end of Fontmap file; giving up."));
1115 break;
1116 }
1117 if (ttype == '.' && pos2 - pos1 == 19
1118 && memcmp(ffline + pos1, ".runlibfileifexists", 19) == 0)
1119 quiet = True;
1120 else if (ttype != '.' || pos2 - pos1 != 11
1121 || memcmp(ffline + pos1, ".runlibfile", 11) != 0) {
1122 TRACE_FT((stderr, "invalid token following \"(%.*s)\" in Fontmap file; giving up.",
1123 (int) pos1, ffline));
1124 break;
1125 }
1126
1127 ffline[pos1] = '\0';
1128 if (ffline[0] == '/' || (ffline[0] == '.' && (ffline[1] == '/'
1129 || (ffline[1] == '.' && ffline[2] == '/'))))
1130 f1 = XFOPEN(ffline, OPEN_MODE);
1131 else {
1132 char *q;
1133
1134 q = xmemdup(ffline, pos1 + 1);
1135 f1 = gs_path_fopen(q, NULL);
1136 free(q);
1137 }
1138
1139 if (f1 == NULL) {
1140 if (!quiet)
1141 XDVI_WARNING((stderr, "Fontmap .runlibfile: %s: %s",
1142 ffline, strerror(errno)));
1143 else
1144 TRACE_FT((stderr,
1145 "Fontmap .runlibfileifexists: %s: %s\n",
1146 ffline, strerror(errno)));
1147 }
1148 else {
1149 --gs_fontmap_number;
1150 process_gs_fontmap(f1);
1151 }
1152 }
1153 else if (ttype == '/') {
1154 struct avl_gs *gsfp;
1155 struct gs_list *gslp;
1156
1157 ttype = get_gs_token(&gsf, pos1, &pos2, "Fontmap");
1158 if (ttype == GS_ERR)
1159 break;
1160 if (ttype == GS_EOF) {
1161 TRACE_FT((stderr,
1162 "unexpected end of Fontmap file; giving up."));
1163 break;
1164 }
1165 if ((ttype != '/' && ttype != LPAREN)
1166 || pos2 == pos1 /* empty string would mess things up */
1167 || get_gs_token(&gsf, pos2, &pos3, "Fontmap") != ';'
1168 || pos3 != pos2 + 1) {
1169 TRACE_FT((stderr,
1170 "invalid token following \"%.*s\" in Fontmap file; giving up.",
1171 (int) pos1, ffline));
1172 break;
1173 }
1174 if (ttype == '/')
1175 ffline[pos1] = '\0'; /* mark aliases by initial \0 */
1176 ffline[pos2++] = '\0'; /* terminate string */
1177
1178 /* Add to database */
1179 gsfp = (struct avl_gs *) avladd(ffline + 1, pos1 - 1,
1180 (struct avl **) &gs_head, sizeof *gsfp);
1181
1182 if (gsfp->key == ffline + 1) { /* if new record */
1183 gsfp->key = xmemdup(ffline + 1, pos1 - 1);
1184 gsfp->in_use = False;
1185 gsfp->list = NULL;
1186 }
1187 else {
1188 if (strlen(gsfp->list->value + 1) + 2 == pos2 - pos1
1189 && memcmp(gsfp->list->value, ffline + pos1, pos2 - pos1)
1190 == 0)
1191 continue; /* ignore duplicate entry */
1192 if (gsfp->fontmap_number == gs_fontmap_number) {
1193 /* Later entries in a Fontmap file override earlier
1194 ones */
1195 gslp = gsfp->list;
1196 gsfp->list = gslp->next;
1197 free(gslp);
1198 }
1199 }
1200 gslp = xmalloc(sizeof *gslp + pos2 - pos1);
1201 gslp->next = gsfp->list;
1202 gsfp->list = gslp;
1203 memcpy((char *) gslp->value, ffline + pos1, pos2 - pos1);
1204
1205 gsfp->fontmap_number = gs_fontmap_number;
1206 }
1207 else {
1208 TRACE_FT((stderr,
1209 "invalid token \"%s\" in Fontmap file; giving up.",
1210 ffline));
1211 }
1212 }
1213
1214 fclose(f);
1215 }
1216
1217 /*
1218 * Read Ghostscript Fontmap files. These are used if the line in
1219 * psfonts.map does not contain a filename for the font.
1220 *
1221 * For example:
1222 * n019003l NimbusSanL-Regu
1223 */
1224
1225 static void
1226 read_gs_fontmaps(void)
1227 {
1228 env_gs_lib = getenv("XDVI_GS_LIB");
1229 if (env_gs_lib == NULL)
1230 env_gs_lib = getenv("GS_LIB");
1231
1232 if (gs_ctype[0] == 0)
1233 init_gs_ctype();
1234
1235 (void) gs_path_fopen("Fontmap", process_gs_fontmap);
1236 }
1237
1238
1239 /*
1240 * pre_lookup_t1_font - Find a Type 1 font (or return NULL).
1241 */
1242
1243 static struct avl_t1 *
1244 pre_lookup_t1_font(const char *fontname)
1245 {
1246 struct avl_t1 *t1p;
1247 size_t len;
1248 int i;
1249
1250 /* first, search for the font */
1251
1252 len = strlen(fontname);
1253 t1p = t1_head;
1254 while (t1p != NULL) {
1255 i = len - t1p->key_len;
1256 if (i == 0)
1257 i = memcmp(fontname, t1p->key, len);
1258 if (i == 0)
1259 return t1p; /* found it */
1260 t1p = (struct avl_t1 *) (i < 0 ? t1p->left : t1p->right);
1261 }
1262
1263 /* next, read in more records in hopes of finding the font */
1264
1265 if (p_head != NULL)
1266 for (;;) {
1267 if (!fgets_long(mapfile)) { /* if end of file */
1268 fclose(mapfile);
1269 p_head = p_head->next;
1270 if (!open_next_mapfile())
1271 return NULL;
1272 continue;
1273 }
1274
1275 if (*ffline < ' ' || *ffline == '*' || *ffline == '#'
1276 || *ffline == ';' || *ffline == '%')
1277 continue;
1278
1279 t1p = dvips_parse(ffline);
1280
1281 if (t1p != NULL && t1p->key_len == len
1282 && memcmp(t1p->key, fontname, len) == 0)
1283 return t1p; /* found it */
1284 }
1285
1286 return NULL;
1287 }
1288
1289 /*
1290 * lookup_t1_font - Find a Type 1 font (or return NULL).
1291 */
1292
1293 Boolean
1294 lookup_t1_font(struct font *fontp,
1295 const char *fontname)
1296 {
1297 struct avl_t1 *t1p;
1298 struct ftfont *ftp;
1299
1300 t1p = pre_lookup_t1_font(fontname);
1301
1302 if (t1p == NULL)
1303 return False;
1304
1305 if (t1p->bad) {
1306 TRACE_FT((stderr,
1307 "Font %s is marked as bad: skipping scalable version",
1308 fontname));
1309 return False;
1310 }
1311
1312 ftp = t1p->ft;
1313 if (ftp != NULL) { /* if it's is already in use at another size */
1314 struct font *first_size;
1315
1316 /* The first node in the linked list of sizes contains the file */
1317 /* reference, so we link in the new node after the first node */
1318 first_size = ftp->first_size;
1319 fontp->next_size = first_size->next_size;
1320 first_size->next_size = fontp;
1321 }
1322 else { /* first use at this size */
1323 t1p->ft = ftp = xmalloc(sizeof *ftp);
1324 ftp->face = NULL;
1325 ftp->t1 = t1p;
1326 ftp->first_size = fontp;
1327 fontp->next_size = NULL;
1328 }
1329 fontp->ft = ftp;
1330 fontp->size = NULL;
1331
1332 return True;
1333 }
1334
1335 FILE *
1336 open_t1_font(struct avl_t1 *t1p,
1337 const char **path_ret)
1338 {
1339 FILE *f;
1340
1341 if (t1p->fontfile == NULL) { /* look up in GS Fontmap */
1342 static Boolean gs_fontmap_initialized = False;
1343
1344 if (!gs_fontmap_initialized) {
1345 read_gs_fontmaps();
1346 gs_fontmap_initialized = True;
1347 }
1348
1349 TRACE_FT((stderr,
1350 "Looking for font %.*s using gs method (PS name %s) --",
1351 t1p->key_len, t1p->key, t1p->psname));
1352 f = lookup_gs_font(t1p->psname, path_ret);
1353
1354 if (f == NULL) {
1355 TRACE_FT((stderr, "cannot find Type 1 font %s",
1356 t1p->psname));
1357 return NULL;
1358 }
1359
1360 TRACE_FT((stderr, "Found file %s", *path_ret));
1361 }
1362 else {
1363 char *filename;
1364
1365 filename = kpse_find_file(t1p->fontfile, kpse_type1_format, 0);
1366 if (filename == NULL) {
1367 TRACE_FT((stderr, "cannot find Type 1 font file %s "
1368 "(will try PK version instead).",
1369 t1p->fontfile));
1370 return NULL;
1371 }
1372
1373 f = XFOPEN(filename, OPEN_MODE);
1374 if (f == NULL) {
1375 TRACE_FT((stderr, "cannot open Type 1 font file %s: %s",
1376 filename, strerror(errno)));
1377 free(filename);
1378 return NULL;
1379 }
1380 *path_ret = filename;
1381 }
1382
1383 return f;
1384 }
1385
1386
1387 /*
1388 * Read the encoding vector file. This assumes the same format as afm2tfm.
1389 */
1390
1391 extern struct findrec search_header; /* from special.c */
1392
1393 void
1394 read_encoding(struct avl_enc *encp)
1395 {
1396 char *filename;
1397 FILE *f;
1398 struct gsfile gsf;
1399 unsigned char buffer[GS_BUF_SIZE];
1400 jmp_buf err_env;
1401 unsigned char ttype;
1402 unsigned int pos1, pos2;
1403 unsigned int identindex[256];
1404 const char *str;
1405 unsigned int i;
1406
1407 TRACE_FT((stderr, "Reading encoding file %s", encp->key));
1408
1409 encp->valid = False;
1410
1411 /*
1412 * With TDS 1.0/kpathsea 3.5.2(?), encoding files are in texmf/fonts/enc
1413 * and accessed via kpse_enc_format; see e.g.:
1414 * http://tug.org/mailman/htdig/tex-live/2004-January/004734.html
1415 *
1416 * The lookups under kpse_program_text_format and
1417 * kpse_tex_ps_header_format are kept for backwards compatibility.
1418 */
1419
1420 filename = kpse_find_file(encp->key, kpse_enc_format, 0);
1421 if (filename == NULL) {
1422 filename = kpse_find_file(filename, kpse_program_text_format, 0);
1423 if (filename == NULL)
1424 filename = kpse_find_file(filename, kpse_tex_ps_header_format,
1425 True);
1426 }
1427 if (filename == NULL) {
1428 TRACE_FT((stderr,
1429 "cannot find encoding file %s; ignoring encoding", encp->key));
1430 return;
1431 }
1432
1433 f = XFOPEN(filename, OPEN_MODE);
1434 if (f == NULL) {
1435 TRACE_FT((stderr, "cannot open encoding file %s: %s",
1436 filename, strerror(errno)));
1437 free(filename);
1438 return;
1439 }
1440 free(filename);
1441
1442 if (gs_ctype[0] == 0)
1443 init_gs_ctype();
1444
1445 gsf.f = f;
1446 gsf.buffer = buffer;
1447 gsf.bufpos = gsf.buf_end = buffer + GS_BUF_SIZE;
1448
1449 if (!setjmp(err_env)) {
1450 if (get_gs_token(&gsf, 0, &pos1, "encoding") != '/'
1451 || get_gs_character(&gsf) != '[')
1452 longjmp(err_env, 1);
1453
1454 pos1 = 0;
1455 for (i = 0; i < 256; ++i) {
1456 if (get_gs_token(&gsf, pos1, &pos2, "encoding") != '/')
1457 longjmp(err_env, 1);
1458 if (pos2 == pos1 + 8
1459 && memcmp(ffline + pos1, "/.notdef", 8) == 0)
1460 identindex[i] = 0;
1461 else {
1462 ffline[pos1] = '\0';
1463 identindex[i] = pos1 + 1;
1464 pos1 = pos2;
1465 }
1466 }
1467
1468 if (get_gs_character(&gsf) != ']')
1469 longjmp(err_env, 1);
1470
1471 ttype = get_gs_token(&gsf, pos1, &pos2, "encoding");
1472 if (!(ttype == GS_EOF
1473 || (ttype == 'd' && pos2 == pos1 + 3
1474 && memcmp(ffline + pos1, "def", 3) == 0
1475 && get_gs_token(&gsf, pos2, &pos2, "encoding") == GS_EOF)))
1476 longjmp(err_env, 1);
1477
1478 if (pos1 >= ffline_len) expandline(pos1 + 1);
1479 ffline[pos1] = '\0';
1480 str = xmemdup(ffline + 1, pos1);
1481 for (i = 0; i < 256; ++i)
1482 encp->vec[i] =
1483 (identindex[i] != 0 ? str + identindex[i] - 1 : NULL);
1484
1485 encp->valid = True;
1486 }
1487 else /* if error */
1488 TRACE_FT((stderr,
1489 "invalid format in encoding file %s; giving up.", encp->key));
1490
1491 fclose(f);
1492 }
1493
1494 #endif /* FREETYPE || PS */
1495
1496
1497 #if 0
1498 static int mktexpk_io[2];
1499 static struct xchild mktexpk_child = { NULL, 0, True, "font creation", NULL, NULL, mktexpk_ended };
1500
1501 static char *read_from_mktexpk(int ignored);
1502 static void write_to_mktexpk(int ignored);
1503
1504 static struct xio mktexpk_xio = { NULL, 0, XIO_IN,
1505 #if HAVE_POLL
1506 NULL,
1507 #endif
1508 read_from_mktexpk,
1509 NULL, NULL};
1510
1511
1512 static void
1513 mktexpk_ended(int status, struct xchild *this)
1514 {
1515 char str[1024] = "";
1516 char *err_msg = NULL;
1517
1518 fprintf(stderr, "------- MKTEXPK_ENDED!\n");
1519 if (this->io != NULL && WIFEXITED(status)) {
1520 err_msg = (this->io->read_proc)(this->io->fd, NULL);
1521 SNPRINTF(str, 1024, "\nProcess `%s' returned exit code %d.\n",
1522 this->name, WEXITSTATUS(status));
1523 str[1024 - 1] = '\0';
1524 printlog_append_str(str);
1525 if (err_msg != NULL) {
1526 fprintf(stderr, "FROM MKTEXPK: |%s|\n", err_msg);
1527 printlog_append_str(err_msg);
1528 }
1529 }
1530 printlog_enable_closebutton();
1531 /* free(this->name); */
1532 /* free(this->io); */
1533 /* free(this); */
1534
1535 read_from_mktexpk(0);
1536 clear_io(this->io);
1537 (void)close(mktexpk_xio.fd);
1538
1539 if (WIFEXITED(status)) {
1540 if (WEXITSTATUS(status) == 0) {
1541 printlog_append("Done.\n", strlen("Done.\n"));
1542 }
1543 else
1544 sprintf(str, "\nPrint process returned exit code %d.\n",
1545 WEXITSTATUS(status));
1546 }
1547 else if (WIFSIGNALED(status))
1548 sprintf(str, "\nPrint process terminated by signal %d.\n",
1549 WTERMSIG(status));
1550 else
1551 sprintf(str, "\nPrint process returned unknown status 0x%x.\n",
1552 status);
1553
1554
1555 }
1556
1557 static char *
1558 read_from_mktexpk(int fd)
1559 {
1560 int bytes;
1561 char line[80];
1562 char *buf;
1563
1564 fprintf(stderr, "------- READ_FROM_MKTEXPK!\n");
1565 for (;;) {
1566 #ifndef MOTIF
1567 bytes = read(fd, line, sizeof line);
1568 #else
1569 bytes = read(fd, line, sizeof line - 1);
1570 #endif
1571 if (bytes < 0) {
1572 if (AGAIN_CONDITION)
1573 break;
1574 perror("xdvi: read_from_mktexpk");
1575 break;
1576 }
1577
1578 if (bytes == 0)
1579 break;
1580 else {
1581 #ifdef MOTIF
1582 line[bytes] = '\0';
1583 #endif
1584 fprintf(stderr, "------- READ_FROM_MKTEXPK:|%s|\n", line);
1585 printlog_append(line, bytes);
1586 }
1587 }
1588 buf = xmalloc(bytes + 1);
1589 memcpy(buf, line, bytes);
1590 buf[bytes] = '\0';
1591 return buf;
1592 }
1593
1594 static void
1595 write_to_mktexpk(int ignored)
1596 {
1597 UNUSED(ignored);
1598
1599 return;
1600 }
1601 #endif /* 0 */
1602
1603
1604 #if DELAYED_MKTEXPK
1605
1606 /* hash table for names of missing fonts, and their indexes */
1607 static hashTableT missing_font_hash;
1608
1609 /* counters for missing fonts */
1610 static int missing_font_ctr = 0;
1611 static int missing_font_curr = 0;
1612
1613 static const char *const dummy_font_value = ""; /* used as value in hash table of missing fonts ... */
1614
1615 /* static char **all_fonts = NULL; */
1616 /* static size_t all_fonts_size = 0; */
1617
1618 void
1619 reset_missing_font_count(void)
1620 {
1621 missing_font_ctr = missing_font_curr = 0;
1622 }
1623
1624 /* Register font `fname' at size `dpi' as a font for which we'll need
1625 * to create a PK file
1626 */
1627 static void
1628 add_missing_font(const char *fname, int dpi)
1629 {
1630 char *buf = NULL;
1631
1632 if (missing_font_hash.size == 0) {
1633 missing_font_hash = hash_create(197);
1634 }
1635
1636 /* font name (hash key) needs to be dynamically allocated here */
1637 buf = xmalloc(strlen(fname) + strlen(" at ") + LENGTH_OF_INT + 1);
1638 sprintf(buf, "%s at %d", fname, dpi);
1639 if (hash_lookup(missing_font_hash, buf) == NULL) {
1640 missing_font_ctr++;
1641 hash_insert(&missing_font_hash, buf, dummy_font_value);
1642 }
1643 }
1644
1645 /* Check if font `fname' at size `dpi' is in the hash of fonts for which
1646 * we need to create a PK file. If it is, return its index (>= 0) and delete
1647 * it from the hash table; else, return -1.
1648 */
1649 static Boolean
1650 get_and_remove_missing_font(const char *fname, int dpi)
1651 {
1652 char buf[1024];
1653
1654 if (missing_font_hash.size == 0)
1655 return False;
1656
1657 SNPRINTF(buf, 1024, "%s at %d", fname, dpi);
1658 buf[1023] = '\0';
1659 if (hash_lookup(missing_font_hash, buf) == NULL) {
1660 return False;
1661 }
1662
1663 hash_remove(&missing_font_hash, buf, dummy_font_value);
1664 return True;
1665 }
1666
1667
1668 static Boolean
1669 message_font_creation(const char *fname, int dpi)
1670 {
1671 if (get_and_remove_missing_font(fname, dpi)) {
1672 missing_font_curr++;
1673 statusline_info(STATUS_MEDIUM,
1674 "Creating PK font: %s at %d dpi (%d of %d) ...",
1675 fname,
1676 dpi,
1677 missing_font_curr,
1678 missing_font_ctr);
1679 force_statusline_update();
1680 return True;
1681 }
1682 return False;
1683 }
1684 #endif /* DELAYED_MKTEXPK */
1685
1686 FILE *
1687 font_open(
1688 #if DELAYED_MKTEXPK
1689 Boolean load_font_now,
1690 #endif
1691 struct font *fontp,
1692 const char **font_ret,
1693 int *dpi_ret)
1694 {
1695 char *name;
1696 kpse_glyph_file_type file_ret;
1697 #if DELAYED_MKTEXPK
1698 Boolean message_done = False;
1699 Boolean need_statusline_update = False;
1700 #endif
1701 /* defaults in case of success; filename_ret will be
1702 non-NULL iff the fallback font is used.
1703 */
1704 *font_ret = NULL;
1705 /* filename_ret is NULL iff a T1 version of a font has been used */
1706 fontp->filename = NULL;
1707 *dpi_ret = fontp->fsize;
1708
1709 if (resource.omega) { /* for omega, first try 16-bit ovf's, then 8-bit vf's. */
1710 name = kpse_find_ovf(fontp->fontname);
1711 if (name == NULL)
1712 name = kpse_find_vf(fontp->fontname);
1713 }
1714 else {
1715 name = kpse_find_vf(fontp->fontname);
1716 }
1717
1718 if (name) { /* found a vf font */
1719 /* pretend it has the expected dpi value, else caller will complain */
1720 *dpi_ret = fontp->fsize;
1721 fontp->filename = name;
1722 return XFOPEN(name, FOPEN_R_MODE);
1723 }
1724
1725 #if FREETYPE
1726 if (resource.freetype
1727 # if DELAYED_MKTEXPK
1728 && load_font_now
1729 # endif
1730 ) {
1731 /* First attempt: freetype font of correct size
1732 * (for delayed_mtkexpk, only when scanning postamble for the first time)
1733 */
1734 if (lookup_t1_font(fontp, fontp->fontname)) {
1735 TRACE_FT((stderr, "found freetype font %s", fontp->fontname));
1736 return NULL;
1737 }
1738 TRACE_FT((stderr,
1739 "Freetype version of font %s not found, trying pixel version next, then fallback",
1740 fontp->fontname));
1741 }
1742 #endif /* FREETYPE */
1743
1744
1745 /*
1746 TODO:
1747
1748 Probably a better approach would be as follows:
1749
1750 1. Read the postamble to get all font definitions. Then, set:
1751
1752 kpse_set_program_enabled(kpse_any_glyph_format, False, kpse_src_compile);
1753
1754 and run load_font() on all of the fonts, with an array in which to save
1755 the names that don't exist (that returned NULL).
1756
1757 2. Run load_font() again on the fonts that didn't exist in step
1758 (1) and display the output in a window. This somehow needs to
1759 be fork()ed so that the window itself remains responsive.
1760 (Maybe it's easier to call mktexpk directly on the command-line?)
1761
1762 _________________________________________________________
1763 | |
1764 | Xdvi is creating fonts, please be patient ... |
1765 | |
1766 | Font xyz (n of m) |
1767 | |
1768 | Errors: 0 [ Show Details ... ] |
1769 | |
1770 | [ ... some progress meter or busy indicator ... ] |
1771 | |
1772 | |
1773 | [ Exit xdvi ] [ Help ] |
1774 | |
1775 ---------------------------------------------------------
1776
1777 This window can be shown before the main window is opened.
1778
1779 */
1780
1781 /* Second try: PK/GF/... font within allowable size range */
1782 /*
1783 NOTE SU: The problem with this is that it will already use the PK version
1784 of the fallback font (e.g. cmr10.600pk) if the PK version exists, so the
1785 Type1 version of the fallback won't get used at all. But maybe this isn't
1786 that severe, given that the font is grossly wrong anyway.
1787 */
1788 #if DELAYED_MKTEXPK
1789 if (load_font_now) {
1790 fprintf(stderr, "loading font now\n");
1791 if (message_font_creation(fontp->fontname, (int)(fontp->fsize + 0.5))) {
1792 message_done = True;
1793 name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
1794 kpse_any_glyph_format, &file_ret);
1795 }
1796 else {
1797 kpse_set_program_enabled(kpse_any_glyph_format, False, kpse_src_compile);
1798 name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
1799 kpse_any_glyph_format, &file_ret);
1800 /* no success if either name is NULL or the filename returned in file_ret is
1801 a different font */
1802 #if 1
1803 /* ??? Bug with tex/test2.tex if cmbr exists but cmr doesn't ??? */
1804 fprintf(stderr, "creating %s\n", fontp->fontname);
1805 if (!name || strcmp(file_ret.name, fontp->fontname) != 0) {
1806 statusline_info(STATUS_MEDIUM,
1807 "Creating PK font: %s at %d dpi ...",
1808 fontp->fontname,
1809 (int)(fontp->fsize + 0.5));
1810 need_statusline_update = True;
1811 force_statusline_update();
1812 kpse_set_program_enabled(kpse_any_glyph_format, resource.makepk, kpse_src_compile);
1813 name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
1814 kpse_any_glyph_format, &file_ret);
1815 }
1816 #endif
1817 }
1818 }
1819 else {
1820 name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
1821 kpse_any_glyph_format, &file_ret);
1822 }
1823 #else /* DELAYED_MKTEXPK */
1824 name = kpse_find_glyph(fontp->fontname, (unsigned)(fontp->fsize + .5),
1825 kpse_any_glyph_format, &file_ret);
1826 #endif /* DELAYED_MKTEXPK */
1827
1828 if (name) { /* success */
1829 #if DELAYED_MKTEXPK
1830 if (need_statusline_update) {
1831 statusline_info(STATUS_SHORT, "Creating PK font: %s at %d dpi ... done",
1832 fontp->fontname,
1833 (int)(fontp->fsize + 0.5));
1834 force_statusline_update();
1835 }
1836 #endif
1837 *dpi_ret = file_ret.dpi;
1838 fontp->filename = name;
1839 *font_ret = file_ret.name;
1840 TRACE_FT((stderr, "Found pixel version: %s at %d dpi", file_ret.name, *dpi_ret));
1841 #if DELAYED_MKTEXPK
1842 if (message_done) {
1843 statusline_append(STATUS_VERYSHORT, "DUMMY", /* append text, don't overwrite */
1844 "done.");
1845 force_statusline_update();
1846 }
1847 #endif
1848 return XFOPEN(name, FOPEN_R_MODE);
1849 }
1850 #if DELAYED_MKTEXPK
1851 else if (!load_font_now) {
1852 add_missing_font(fontp->fontname, (int)(fontp->fsize + 0.5));
1853 return NULL;
1854 }
1855 #endif
1856 else if (resource.alt_font != NULL) {
1857 /* The strange thing about kpse_find_glyph() is that it
1858 won't create a PK version of alt_font if it doesn't
1859 already exist. So we invoke it explicitly a second time
1860 for that one.
1861 */
1862 TRACE_FT((stderr, "Trying fallback"));
1863 #if FREETYPE
1864 if (resource.freetype
1865 #if DELAYED_MKTEXPK
1866 && load_font_now
1867 #endif
1868 ) {
1869 /* Third attempt: freetype version of fallback font */
1870 if (lookup_t1_font(fontp, resource.alt_font)) {
1871 TRACE_FT((stderr, "found fallback font for %s: %s",
1872 fontp->fontname, resource.alt_font));
1873 *font_ret = xstrdup(resource.alt_font);
1874 return NULL;
1875 }
1876 TRACE_FT((stderr,
1877 "Freetype version of fallback font %s not found, trying pixel version",
1878 resource.alt_font));
1879 }
1880 #endif /* FREETYPE */
1881 /* Fourth attempt: PK version of fallback font */
1882 name = kpse_find_glyph(resource.alt_font, (unsigned)(fontp->fsize + .5),
1883 kpse_any_glyph_format, &file_ret);
1884 if (name) { /* success */
1885 TRACE_FT((stderr, "Success for PK version of fallback"));
1886 *dpi_ret = file_ret.dpi;
1887 fontp->filename = name;
1888 *font_ret = xstrdup(resource.alt_font);
1889 return XFOPEN(name, FOPEN_R_MODE);
1890 }
1891 else {
1892 TRACE_FT((stderr, "Failure for PK version of fallback"));
1893 }
1894 }
1895
1896 /* all other cases are failure */
1897 TRACE_FT((stderr, "Failure"));
1898 return NULL;
1899 }