Debian Codegraph @

texk/ttf2pk2/ttf2pk.c

raw
1 /*
2 * ttf2pk.c
3 *
4 * This file is part of the ttf2pk package, released under the
5 * GNU GPL version 2 or any later version.
6 *
7 * Copyright 1997-1999, 2000, 2002 by
8 * Frederic Loyer <[email protected]>
9 * Werner Lemberg <[email protected]>
10 * Copyright 2009-2013 by
11 * Peter Breitenlohner <[email protected]>
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h> /* for size_t */
21 #include <string.h>
22 #include <math.h>
23 #include <ctype.h>
24
25 #ifdef MIKTEX
26 #include <miktex.h>
27 #endif
28
29 #include "ttf2tfm.h"
30 #include "newobj.h"
31 #include "pklib.h"
32 #include "ttfenc.h"
33 #include "ttflib.h"
34 #include "errormsg.h"
35 #include "filesrch.h"
36 #include "parse.h"
37 #include "subfont.h"
38
39
40 char ident[] = "ttf2pk version 2.0";
41 const char *progname = "ttf2pk"; /* for error/warning messages */
42
43 static const char *cfg_filename = "ttf2pk.cfg";
44 char *mapfiles;
45
46 Boolean have_sfd = False;
47 Boolean have_pid = False;
48 Boolean have_eid = False;
49
50
51 /*
52 * Checks for an equal sign surrounded by whitespace.
53 */
54
55 static char *
56 strip_equal(char *s, char *os, char *p)
57 {
58 while (isspace((unsigned char)*p))
59 p++;
60 if (*p != '=')
61 boops(os, p - s, "Missing `='.");
62 p++;
63 while (isspace((unsigned char)*p))
64 p++;
65 return p;
66 }
67
68
69 #define USAGE "\
70 Convert a TrueType font to TeX's PK format.\n\
71 \n\
72 -q suppress informational output\n\
73 -n only use `.pk' as extension\n\
74 -t test for <font> (returns 0 on success)\n\
75 --help print this message and exit\n\
76 --version print version number and exit\n\
77 \n\
78 Report bugs to " PACKAGE_BUGREPORT ".\n\
79 "
80
81 static void
82 usage(void)
83 {
84 fputs("Usage: ttf2pk [-q] [-n] <font> <dpi>\n", stdout);
85 fputs(" ttf2pk -t [-q] <font>\n", stdout);
86 fputs(USAGE, stdout);
87 exit(0);
88 }
89
90
91 #define VERSION "\
92 Copyright (C) 1997-1999, 2000, 2002, 2009 Frederic Loyer and Werner Lemberg.\n\
93 2009-2013 Peter Breitenlohner\n\
94 There is NO warranty. You may redistribute this software\n\
95 under the terms of the GNU General Public License\n\
96 and the gsftopk copyright.\n\
97 \n\
98 For more information about these matters, see the files\n\
99 named COPYING and pklib.c.\n\
100 \n\
101 Primary authors of ttf2pk: F. Loyer and W. Lemberg.\n\
102 \n\
103 ttf2pk is partially based on gsftopk from P. Vojta\n\
104 and the FreeType project from\n\
105 David Turner, Robert Wilhelm, and Werner Lemberg\n\
106 "
107
108 static void
109 version(void)
110 {
111 fputs(ident, stdout);
112 fprintf(stdout, " (%s, %s)\n", TeX_search_version(), TL_VERSION);
113 fputs(VERSION, stdout);
114 exit(0);
115 }
116
117
118 static int
119 compare(Font *fnt, char *s, char *key)
120 {
121 char c;
122 char *p;
123 char *temp, *temp1;
124 int value;
125 int sfd_begin, postfix_begin;
126
127
128 /*
129 * We isolate the fontname.
130 */
131
132 while (isspace((unsigned char)*s))
133 s++;
134
135 p = s;
136 while (*p && !isspace((unsigned char)*p))
137 p++;
138
139 c = *p;
140 *p = '\0';
141 temp = newstring(s);
142 *p = c;
143
144 /*
145 * We search for a subfont definition file name.
146 */
147
148 handle_sfd(temp, &sfd_begin, &postfix_begin);
149
150 if (sfd_begin == -1)
151 value = strcmp(temp, key);
152 else
153 {
154 size_t len, len1, len2;
155
156
157 /*
158 * The sfd file will be only searched if prefix and postfix match.
159 */
160
161 len = strlen(key);
162 len1 = strlen(temp);
163 len2 = strlen(temp + postfix_begin);
164
165 if (len1 + len2 >= len)
166 value = -1;
167 else if (!strncmp(temp, key, len1) &&
168 !strcmp(temp + postfix_begin, key + (len - len2)))
169 {
170 c = key[len - len2];
171 key[len - len2] = '\0';
172 temp1 = newstring(key + len1);
173 key[len - len2] = c;
174
175 if (fnt->sfdname)
176 free(fnt->sfdname);
177 fnt->sfdname = newstring(temp + sfd_begin);
178
179 /*
180 * If the sfd file can't be opened the search is continued.
181 */
182
183 value = !init_sfd(fnt, False);
184
185 if (!value)
186 {
187 value = -1;
188
189 while (get_sfd(fnt, True))
190 {
191 if (!strcmp(fnt->subfont_name, temp1))
192 {
193 value = 0; /* success */
194 have_sfd = True;
195 break;
196 }
197 }
198
199 close_sfd();
200 }
201
202 free(temp1);
203 }
204 else
205 value = -1;
206 }
207
208 free(temp);
209
210 return value;
211 }
212
213
214 static void
215 add_mapfile(char *p)
216 {
217 char *q;
218
219 if (*p != '+') /* if the first character is not '+' reset mapfiles */
220 {
221 if (mapfiles)
222 {
223 free(mapfiles);
224 mapfiles = NULL;
225 }
226 }
227 else
228 while (isspace((unsigned char)*++p))
229 ;
230 for (q = p; *q != 0 && !isspace((unsigned char)*q); q++)
231 ;
232 *q = '\n'; /* '\n' is the splitting character */
233 if (mapfiles == NULL)
234 {
235 mapfiles = (char *)mymalloc(q - p + 2);
236 *mapfiles = '\0';
237 }
238 else
239 mapfiles = (char *)myrealloc(mapfiles,
240 strlen(mapfiles) + q - p + 2);
241 strncat(mapfiles, p, (unsigned)(q - p));
242 strcat(mapfiles, "\n");
243 }
244
245
246 static void
247 read_config_file(Boolean quiet)
248 {
249 FILE *config_file;
250 char *p, *configline, *real_cfg_filename;
251
252
253 real_cfg_filename = TeX_search_config_file(&cfg_filename);
254 if (real_cfg_filename)
255 {
256 config_file = fopen(real_cfg_filename, "rt");
257 if (config_file != NULL)
258 {
259 for (;;)
260 {
261 if (!(configline = get_line(config_file)))
262 oops("Error while reading %s.", cfg_filename);
263 if (!*configline)
264 break;
265 p = configline;
266 while (isspace((unsigned char)*p))
267 p++;
268 /* ignore comments */
269 if (*p == '*' || *p == '#' || *p == ';' || *p == '%')
270 continue;
271 if (strlen(p) > 4 && strncmp(p, "map", 3) == 0)
272 {
273 p += 3; /* move by the size of "map" */
274 while (isspace((unsigned char)*p))
275 p++;
276 if (*p)
277 add_mapfile(p);
278 }
279 else if (!quiet)
280 warning("Unknown configuration option `%s'.", p);
281 }
282 fclose(config_file);
283 }
284 else if (!quiet)
285 warning("Cannot open file `%s'.", cfg_filename);
286 }
287 else if (!quiet)
288 warning("Cannot find file `%s'.\n"
289 "Use `ttfonts.map' instead as map file.", cfg_filename);
290
291 if (mapfiles == NULL)
292 mapfiles = newstring("ttfonts.map\n");
293 }
294
295
296 int
297 main(int argc, char** argv)
298 {
299 size_t l;
300 unsigned int i;
301 long index, code;
302 FILE *map_file;
303 char *configline, *oldconfigline, *p, *q;
304 Font font;
305 encoding *enc;
306 long inenc_array[256];
307 char *fontname;
308 size_t fontname_len;
309 char *pk_filename, *tfm_filename, *enc_filename;
310 char *map_filename = NULL;
311 char *real_ttfname, *real_map_filename;
312 int dpi = 0, ptsize;
313 Boolean hinting = True;
314 Boolean quiet = False;
315 Boolean no_dpi = False;
316 Boolean testing = False;
317
318
319 #ifdef MIKTEX
320 miktex_initialize();
321 #endif
322
323 TeX_search_init(argv[0], "ttf2pk", "TTF2PK");
324
325 if (argc == 1)
326 oops("Need at least two arguments.\n"
327 "Try `ttf2pk --help' for more information.");
328
329 if (argc == 2)
330 {
331 if (strcmp(argv[1], "--help") == 0)
332 usage();
333 else if (strcmp(argv[1], "--version") == 0)
334 version();
335 }
336
337 while (argv[1] && argv[1][0] == '-')
338 {
339 if (argv[1][1] == 'q')
340 quiet = True;
341 else if (argv[1][1] == 'n')
342 no_dpi = True;
343 else if (argv[1][1] == 't')
344 testing = True;
345 else
346 oops("Unknown option `%s'.\n"
347 "Try `ttf2pk --help' for more information.", argv[1]);
348
349 argv++;
350 argc--;
351 }
352
353 if (testing)
354 {
355 if (argc != 2)
356 oops("Need exactly one parameter for `-t' option.\n"
357 "Try `ttf2pk --help' for more information.");
358 }
359 else if (argc != 3)
360 oops("Need at most two arguments.\n"
361 "Try `ttf2pk --help' for more information.");
362
363 if (!quiet)
364 printf("This is %s\n", ident);
365
366 if (!testing)
367 if ((dpi = atoi(argv[2])) <= 50)
368 oops("dpi value must be larger than 50.");
369
370 fontname = argv[1];
371 fontname_len = strlen(fontname);
372 enc_filename = NULL;
373
374 ptsize = 10;
375
376 init_font_structure(&font);
377
378 mapfiles = NULL;
379 read_config_file(quiet);
380
381 p = mapfiles;
382 while (*p)
383 {
384 if (map_filename)
385 free(map_filename);
386 map_filename = p;
387 while (*p != '\n')
388 p++;
389 *p++ = '\0';
390
391 map_filename = newstring(map_filename);
392 real_map_filename = TeX_search_map_file(&map_filename);
393 if (!real_map_filename)
394 {
395 if (!quiet)
396 warning("Cannot find file `%s'.", map_filename);
397 continue;
398 }
399
400 map_file = fopen(real_map_filename, "rt");
401 if (map_file == NULL)
402 {
403 if (!quiet)
404 warning("Cannot open file `%s'.", map_filename);
405 continue;
406 }
407
408 for (;;)
409 {
410 if (!(configline = get_line(map_file)))
411 oops("Error while reading %s.", map_filename);
412 if (!*configline)
413 break;
414 if (compare(&font, configline, fontname) == 0)
415 {
416 fclose(map_file);
417 goto font_found;
418 }
419 }
420
421 fclose(map_file);
422 }
423
424 /*
425 * This is the only error message we suppress if the -q flag
426 * is set, making it possible to call ttf2pk silently.
427 */
428 if (!quiet)
429 fprintf(stdout,
430 "%s: ERROR: Cannot find font %s in map files.\n",
431 progname, fontname);
432 exit(2);
433
434 font_found:
435 if (testing)
436 {
437 if (!quiet)
438 fprintf(stdout, "%s\n", configline);
439 exit(0);
440 }
441
442 /*
443 * Parse the line from the config file. We split the config line buffer
444 * into substrings according to the given options.
445 */
446
447 l = strlen(configline);
448 if (configline[l - 1] == '\n')
449 configline[l - 1] = '\0'; /* strip newline */
450
451 oldconfigline = newstring(configline);
452
453 p = configline;
454 while (isspace((unsigned char)*p))
455 p++;
456 while (*p && !isspace((unsigned char)*p))
457 p++;
458
459 q = p;
460
461 while (*p && isspace((unsigned char)*p))
462 p++;
463 if (!*p)
464 boops(oldconfigline, q - configline, "TTF file missing.");
465
466 font.ttfname = p;
467
468 while (*p && !isspace((unsigned char)*p))
469 p++;
470 if (*p)
471 *p++ = '\0';
472
473 for (; *p; p++)
474 {
475 if (isspace((unsigned char)*p))
476 continue;
477
478 if (!strncmp(p, "Slant", 5))
479 {
480 p = strip_equal(configline, oldconfigline, p + 5);
481 if (sscanf(p, "%f", &(font.slant)) == 0)
482 boops(oldconfigline, p - configline, "Bad `Slant' parameter.");
483 }
484 else if (!strncmp(p, "Encoding", 8))
485 {
486 if (have_sfd)
487 boops(oldconfigline, p - configline,
488 "No `Encoding' parameter allowed for subfonts.");
489
490 p = strip_equal(configline, oldconfigline, p + 8);
491 if (!*p)
492 boops(oldconfigline, p - configline, "Bad `Encoding' parameter.");
493 enc_filename = p;
494 }
495 else if (!strncmp(p, "Extend", 6))
496 {
497 p = strip_equal(configline, oldconfigline, p + 6);
498 if (sscanf(p, "%f", &(font.efactor)) == 0)
499 boops(oldconfigline, p - configline, "Bad `Extend' parameter.");
500 }
501 else if (!strncmp(p, "Fontindex", 9))
502 {
503 p = strip_equal(configline, oldconfigline, p + 9);
504 if (sscanf(p, "%lu", &(font.fontindex)) < 0)
505 boops(oldconfigline, p - configline, "Bad `Fontindex' parameter.");
506 }
507 else if (!strncmp(p, "Pid", 3))
508 {
509 p = strip_equal(configline, oldconfigline, p + 3);
510 if (sscanf(p, "%hu", &(font.pid)) < 0)
511 boops(oldconfigline, p - configline, "Bad `Pid' parameter.");
512 have_pid = True;
513 }
514 else if (!strncmp(p, "Eid", 3))
515 {
516 p = strip_equal(configline, oldconfigline, p + 3);
517 if (sscanf(p, "%hu", &(font.eid)) < 0)
518 boops(oldconfigline, p - configline, "Bad `Eid' parameter.");
519 have_eid = True;
520 }
521 else if (!strncmp(p, "Hinting", 7))
522 {
523 p = strip_equal(configline, oldconfigline, p + 7);
524 if (p[1] == 'N' || p[1] == 'n' ||
525 p[0] == 'Y' || p[1] == 'y' ||
526 p[0] == '1')
527 hinting = True;
528 else if (p[1] == 'F' || p[1] == 'f' ||
529 p[0] == 'N' || p[1] == 'n' ||
530 p[0] == '0')
531 hinting = False;
532 else
533 boops(oldconfigline, p - configline, "Bad `Hinting' parameter.");
534 }
535 else if (!strncmp(p, "PS", 2))
536 {
537 p = strip_equal(configline, oldconfigline, p + 2);
538 if (p[1] != '\0' &&
539 (p[2] == 'l' || p[2] == 'L'))
540 font.PSnames = Only;
541 else if (p[1] == 'N' || p[1] == 'n' ||
542 p[0] == 'Y' || p[0] == 'y' ||
543 p[0] == '1')
544 font.PSnames = Yes;
545 else if (p[1] == 'F' || p[1] == 'f' ||
546 p[0] == 'N' || p[0] == 'n' ||
547 p[0] == '0')
548 font.PSnames = No;
549 else
550 boops(oldconfigline, p - configline, "Bad `PS' parameter.");
551
552 if (have_sfd)
553 boops(oldconfigline, p - configline,
554 "No `PS' parameter allowed for subfonts.");
555 }
556 else if (!strncmp(p, "Rotate", 6))
557 {
558 p = strip_equal(configline, oldconfigline, p + 6);
559 if (p[1] == 'N' || p[1] == 'n' ||
560 p[0] == 'Y' || p[1] == 'y' ||
561 p[0] == '1')
562 font.rotate = True;
563 else if (p[1] == 'F' || p[1] == 'f' ||
564 p[0] == 'N' || p[1] == 'n' ||
565 p[0] == '0')
566 font.rotate = False;
567 else
568 boops(oldconfigline, p - configline, "Bad `Rotate' parameter.");
569
570 if (!have_sfd)
571 boops(oldconfigline, p - configline,
572 "No `Rotate' parameter allowed for non-subfonts.");
573 }
574 else if (!strncmp(p, "Y-Offset", 8))
575 {
576 p = strip_equal(configline, oldconfigline, p + 8);
577 if (sscanf(p, "%f", &(font.y_offset)) == 0)
578 boops(oldconfigline, p - configline, "Bad `Y-Offset' parameter.");
579 }
580 else if (!strncmp(p, "Replacement", 11))
581 {
582 p = strip_equal(configline, oldconfigline, p + 11);
583 if (!*p)
584 boops(oldconfigline, p - configline, "Bad `Replacement' parameter.");
585 font.replacementname = p;
586 }
587 else
588 {
589 char *new_name, *old_name;
590 stringlist *sl;
591
592
593 old_name = p;
594 while (*p && !isspace((unsigned char)*p) && *p != '=')
595 p++;
596
597 q = p;
598 p = strip_equal(configline, oldconfigline, p);
599 *q = '\0';
600
601 new_name = p;
602 while (*p && !isspace((unsigned char)*p))
603 p++;
604 if (*p)
605 *p++ = '\0';
606
607 sl = newstringlist();
608 sl->new_name = new_name;
609 sl->old_name = old_name;
610 sl->next = font.replacements;
611 font.replacements = sl;
612
613 p--; /* to make the next while loop work */
614 }
615
616 while (*p && !isspace((unsigned char)*p))
617 p++;
618 if (*p)
619 *p = '\0';
620 }
621
622 if (font.PSnames == Only)
623 if (have_pid || have_eid)
624 boops(oldconfigline, 0,
625 "No `Pid' or `Eid' parameters allowed if `PS=Only' is set.");
626
627 font.replacementname = newstring(font.replacementname);
628 get_replacements(&font);
629
630 tfm_filename = newstring(fontname);
631 TFMopen(&tfm_filename);
632
633 pk_filename = mymalloc(fontname_len + 10);
634 if (no_dpi)
635 sprintf(pk_filename, "%s.pk", fontname);
636 else
637 sprintf(pk_filename, "%s.%dpk", fontname, dpi);
638 PKopen(pk_filename, fontname, dpi);
639
640 font.ttfname = newstring(font.ttfname);
641 real_ttfname = TeX_search_ttf_file(&(font.ttfname));
642 if (!real_ttfname)
643 oops("Cannot find `%s'.", font.ttfname);
644 TTFopen(real_ttfname, &font, dpi, ptsize, quiet);
645
646 enc_filename = newstring(enc_filename);
647 enc = readencoding(&enc_filename, &font, True);
648 if (enc)
649 {
650 const char *name;
651
652
653 restore_glyph(enc, &font);
654
655 for (i = 0; i <= 0xFF; i++)
656 {
657 name = enc->vec[i];
658 if (!font.PSnames)
659 {
660 code = adobename_to_code(name);
661 if (code < 0 && strcmp(name, ".notdef") != 0)
662 warning("Cannot map character `%s'.", name);
663 inenc_array[i] = code;
664 }
665 else
666 {
667 /* we search the glyph index */
668 index = TTFsearch_PS_name(name);
669 if (index < 0)
670 warning("Cannot map character `%s'.", name);
671 inenc_array[i] = index | 0x1000000;
672 }
673 }
674 }
675 else
676 {
677 if (font.replacements)
678 warning("Replacement glyphs will be ignored.");
679
680 if (have_sfd)
681 TTFget_subfont(&font, inenc_array);
682 else
683 /* get the table of glyph names too */
684 enc = TTFget_first_glyphs(&font, inenc_array);
685 }
686
687 for (i = 0; i <= 0xFF; i++)
688 {
689 byte *bitmap;
690 int w, h, hoff, voff;
691
692
693 if ((code = inenc_array[i]) >= 0)
694 {
695 if (!quiet)
696 {
697 printf("Processing glyph %3ld %s index 0x%04lx %s\n",
698 (long)i, (code >= 0x1000000) ? "glyph" : "code",
699 (code & 0xFFFFFF), enc ? enc->vec[i] : "");
700 fflush(stdout);
701 }
702
703 if (TTFprocess(&font, code,
704 &bitmap, &w, &h, &hoff, &voff, hinting, quiet))
705 PKputglyph(i,
706 -hoff, -voff, w - hoff, h - voff,
707 w, h, bitmap);
708 else
709 warning("Cannot render glyph with %s index 0x%lx.",
710 (code >= 0x1000000) ? "glyph" : "code",
711 (code & 0xFFFFFF));
712 }
713 }
714
715 PKclose();
716 exit(0); /* for safety reasons */
717 return 0; /* never reached */
718 }
719
720
721 /* end */