gen.sh: correct the mapping of Helvetica-Bold
[neatmkfn.git] / mkfn.c
blob46135299b4c4534363429bd5dca3f1625e41b81f
1 /*
2 * mktrfn - produce troff font descriptions
4 * Copyright (C) 2012-2014 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), 0, 0, 0, 0);
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);
40 trfn_sub(c1, c2);
45 static char *afm_charfield(char *s, char *d)
47 while (*s && !isspace(*s) && *s != ';')
48 *d++ = *s++;
49 while (isspace(*s) || *s == ';')
50 s++;
51 *d = '\0';
52 return s;
55 static void afm_read(void)
57 char ln[1024];
58 char ch[TOKLEN] = "", pos[TOKLEN] = "";
59 char c1[TOKLEN] = "", c2[TOKLEN] = "";
60 char wid[TOKLEN] = "", field[TOKLEN] = "";
61 char llx[TOKLEN] = "0", lly[TOKLEN] = "0";
62 char urx[TOKLEN] = "0", ury[TOKLEN] = "0";
63 char *s;
64 while (fgets(ln, sizeof(ln), stdin)) {
65 if (ln[0] == '#')
66 continue;
67 if (!strncmp("FontName ", ln, 8)) {
68 sscanf(ln, "FontName %s", ch);
69 trfn_psfont(ch);
70 continue;
72 if (!strncmp("StartCharMetrics", ln, 16))
73 break;
75 while (fgets(ln, sizeof(ln), stdin)) {
76 if (ln[0] == '#')
77 continue;
78 if (!strncmp("EndCharMetrics", ln, 14))
79 break;
80 s = ln;
81 while (*s) {
82 s = afm_charfield(s, field);
83 if (!strcmp("C", field)) {
84 s = afm_charfield(s, pos);
85 continue;
87 if (!strcmp("WX", field)) {
88 s = afm_charfield(s, wid);
89 continue;
91 if (!strcmp("N", field)) {
92 s = afm_charfield(s, ch);
93 continue;
95 if (!strcmp("B", field)) {
96 s = afm_charfield(s, llx);
97 s = afm_charfield(s, lly);
98 s = afm_charfield(s, urx);
99 s = afm_charfield(s, ury);
100 continue;
102 if (!strcmp("L", field)) {
103 s = afm_charfield(s, c1);
104 s = afm_charfield(s, c2);
105 continue;
107 break;
109 if (ch[0] && pos[0] && wid[0])
110 trfn_char(ch, pos, atoi(wid),
111 atoi(llx), atoi(lly), atoi(urx), atoi(ury));
113 while (fgets(ln, sizeof(ln), stdin)) {
114 if (ln[0] == '#')
115 continue;
116 if (!strncmp("StartKernPairs", ln, 14))
117 break;
119 while (fgets(ln, sizeof(ln), stdin)) {
120 if (ln[0] == '#')
121 continue;
122 if (!strncmp("EndKernPairs", ln, 12))
123 break;
124 if (sscanf(ln, "KPX %s %s %s", c1, c2, wid) == 3)
125 trfn_kern(c1, c2, atoi(wid));
129 static char *usage =
130 "Usage: mktrfn [options] <input >output\n"
131 "Options:\n"
132 " -a \tread an AFM file (default)\n"
133 " -o \tread the output of otfdump\n"
134 " -s \tspecial font\n"
135 " -p name \toverride font postscript name\n"
136 " -t name \tset font troff name\n"
137 " -r res \tset device resolution (720)\n"
138 " -k kmin \tspecify the minimum amount of kerning (0)\n"
139 " -b \tinclude glyph bounding box\n";
141 int main(int argc, char *argv[])
143 int afm = 1;
144 int i = 1;
145 int res = 720;
146 int spc = 0;
147 int kmin = 0;
148 int bbox = 0;
149 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
150 switch (argv[i][1]) {
151 case 'a':
152 afm = 1;
153 break;
154 case 'k':
155 kmin = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
156 break;
157 case 'o':
158 afm = 0;
159 break;
160 case 'p':
161 trfn_psfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
162 break;
163 case 'r':
164 res = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
165 break;
166 case 's':
167 spc = 1;
168 break;
169 case 't':
170 trfn_trfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
171 break;
172 case 'b':
173 bbox = 1;
174 break;
175 default:
176 printf("%s", usage);
177 return 0;
180 trfn_init(res, spc, kmin, bbox);
181 if (afm)
182 afm_read();
183 else
184 otfdump_read();
185 trfn_print();
186 trfn_done();
187 return 0;