mktrfn: otfdump and improved afm support
[neatmkfn.git] / mktrfn.c
blob16290edfc72b62f6efbdefc7f20d34d096d177d9
1 /*
2 * mktrfn - produce troff font descriptions
4 * Copyright (C) 2012-2013 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
7 */
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include "trfn.h"
16 #define TOKLEN 256
18 static void otfdump_read(void)
20 char cmd[TOKLEN];
21 char name[TOKLEN];
22 char ch[TOKLEN];
23 char c1[TOKLEN], c2[TOKLEN];
24 char wid[TOKLEN];
25 while (scanf("%s", cmd) == 1) {
26 if (!strcmp("name", cmd)) {
27 scanf("%s", name);
28 trfn_psfont(name);
30 if (!strcmp("char", cmd)) {
31 scanf("%s width %s", ch, wid);
32 trfn_char(ch, NULL, atoi(wid), -1);
34 if (!strcmp("kernpair", cmd)) {
35 scanf("%s %s width %s", c1, c2, wid);
36 trfn_kern(c1, c2, atoi(wid));
38 if (!strcmp("feature", cmd)) {
39 scanf("%s substitution %s %s", name, c1, c2);
44 static void afm_read(void)
46 char ch[TOKLEN], pos[TOKLEN];
47 char c1[TOKLEN], c2[TOKLEN];
48 char wid[TOKLEN];
49 char ln[1024];
50 while (fgets(ln, sizeof(ln), stdin)) {
51 if (ln[0] == '#')
52 continue;
53 if (!strncmp("FontName ", ln, 8)) {
54 sscanf(ln, "FontName %s", ch);
55 trfn_psfont(ch);
56 continue;
58 if (!strncmp("StartCharMetrics", ln, 16))
59 break;
61 while (fgets(ln, sizeof(ln), stdin)) {
62 if (ln[0] == '#')
63 continue;
64 if (!strncmp("EndCharMetrics", ln, 14))
65 break;
66 if (sscanf(ln, "C %s ; WX %s ; N %s", pos, wid, ch) == 3)
67 trfn_char(ch, pos, atoi(wid), -1);
69 while (fgets(ln, sizeof(ln), stdin)) {
70 if (ln[0] == '#')
71 continue;
72 if (!strncmp("StartKernPairs", ln, 14))
73 break;
75 while (fgets(ln, sizeof(ln), stdin)) {
76 if (ln[0] == '#')
77 continue;
78 if (!strncmp("EndKernPairs", ln, 12))
79 break;
80 if (sscanf(ln, "KPX %s %s %s", c1, c2, wid) == 3)
81 trfn_kern(c1, c2, atoi(wid));
85 int main(int argc, char *argv[])
87 int afm = 0;
88 int i = 1;
89 int res = 720;
90 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
91 switch (argv[i][1]) {
92 case 'a':
93 afm = 1;
94 break;
95 case 'o':
96 afm = 0;
97 break;
98 case 'r':
99 res = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
100 break;
101 case 't':
102 trfn_trfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
103 break;
104 case 'p':
105 trfn_psfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
106 break;
107 default:
108 printf("usage: mktrfn -a -o -t name -p psname\n");
109 return 0;
112 trfn_init(res);
113 if (afm)
114 afm_read();
115 else
116 otfdump_read();
117 trfn_print();
118 trfn_done();
119 return 0;