otf: clean up
[neatmkfn.git] / mkfn.c
blobc95a8c457b4895ac9c3694a95d33b4149c4184f5
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 char *afm_charfield(char *s, char *d)
20 while (*s && !isspace(*s) && *s != ';')
21 *d++ = *s++;
22 while (isspace(*s) || *s == ';')
23 s++;
24 *d = '\0';
25 return s;
28 static int afm_read(void)
30 char ln[1024];
31 char ch[TOKLEN] = "", pos[TOKLEN] = "";
32 char c1[TOKLEN] = "", c2[TOKLEN] = "";
33 char wid[TOKLEN] = "", field[TOKLEN] = "";
34 char llx[TOKLEN] = "0", lly[TOKLEN] = "0";
35 char urx[TOKLEN] = "0", ury[TOKLEN] = "0";
36 char *s;
37 while (fgets(ln, sizeof(ln), stdin)) {
38 if (ln[0] == '#')
39 continue;
40 if (!strncmp("FontName ", ln, 8)) {
41 sscanf(ln, "FontName %s", ch);
42 trfn_psfont(ch);
43 continue;
45 if (!strncmp("StartCharMetrics", ln, 16))
46 break;
48 while (fgets(ln, sizeof(ln), stdin)) {
49 if (ln[0] == '#')
50 continue;
51 if (!strncmp("EndCharMetrics", ln, 14))
52 break;
53 s = ln;
54 while (*s) {
55 s = afm_charfield(s, field);
56 if (!strcmp("C", field)) {
57 s = afm_charfield(s, pos);
58 continue;
60 if (!strcmp("WX", field)) {
61 s = afm_charfield(s, wid);
62 continue;
64 if (!strcmp("N", field)) {
65 s = afm_charfield(s, ch);
66 continue;
68 if (!strcmp("B", field)) {
69 s = afm_charfield(s, llx);
70 s = afm_charfield(s, lly);
71 s = afm_charfield(s, urx);
72 s = afm_charfield(s, ury);
73 continue;
75 if (!strcmp("L", field)) {
76 s = afm_charfield(s, c1);
77 s = afm_charfield(s, c2);
78 continue;
80 break;
82 if (ch[0] && pos[0] && wid[0])
83 trfn_char(ch, atoi(pos), 0, atoi(wid),
84 atoi(llx), atoi(lly), atoi(urx), atoi(ury));
86 while (fgets(ln, sizeof(ln), stdin)) {
87 if (ln[0] == '#')
88 continue;
89 if (!strncmp("StartKernPairs", ln, 14))
90 break;
92 while (fgets(ln, sizeof(ln), stdin)) {
93 if (ln[0] == '#')
94 continue;
95 if (!strncmp("EndKernPairs", ln, 12))
96 break;
97 if (sscanf(ln, "KPX %s %s %s", c1, c2, wid) == 3)
98 trfn_kern(c1, c2, atoi(wid));
100 return 0;
103 int otf_read(void);
104 void otf_feat(int res, int kmin);
106 static char *usage =
107 "Usage: mktrfn [options] <input >output\n"
108 "Options:\n"
109 " -a \tread an AFM file (default)\n"
110 " -o \tread an OTF file\n"
111 " -s \tspecial font\n"
112 " -p name \toverride font postscript name\n"
113 " -t name \tset font troff name\n"
114 " -r res \tset device resolution (720)\n"
115 " -k kmin \tspecify the minimum amount of kerning (0)\n"
116 " -b \tinclude glyph bounding box\n";
118 int main(int argc, char *argv[])
120 int afm = 1;
121 int i = 1;
122 int res = 720;
123 int spc = 0;
124 int kmin = 0;
125 int bbox = 0;
126 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
127 switch (argv[i][1]) {
128 case 'a':
129 afm = 1;
130 break;
131 case 'k':
132 kmin = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
133 break;
134 case 'o':
135 afm = 0;
136 break;
137 case 'p':
138 trfn_psfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
139 break;
140 case 'r':
141 res = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
142 break;
143 case 's':
144 spc = 1;
145 break;
146 case 't':
147 trfn_trfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
148 break;
149 case 'b':
150 bbox = 1;
151 break;
152 default:
153 printf("%s", usage);
154 return 0;
157 trfn_init(res, spc, kmin, bbox);
158 if (afm)
159 afm_read();
160 else
161 otf_read();
162 trfn_print();
163 if (!afm)
164 otf_feat(res, kmin);
165 trfn_done();
166 return 0;