gen.sh: for creating a neatroff output device
[neatmkfn.git] / mktrfn.c
blob7453bdb6363d40ea57051224f5038670369737db
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 static char *usage =
86 "Usage: mktrfn [options] <input >output\n"
87 "Options:\n"
88 " -o \tread the output of otfdump for otf and ttf files (default)\n"
89 " -a \tread an AFM file\n"
90 " -s \tspecial font\n"
91 " -p name \toverride font postscript name\n"
92 " -t name \tset font troff name\n"
93 " -r res \tset device resolution (720)\n";
95 int main(int argc, char *argv[])
97 int afm = 0;
98 int i = 1;
99 int res = 720;
100 int spc = 0;
101 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
102 switch (argv[i][1]) {
103 case 'a':
104 afm = 1;
105 break;
106 case 'o':
107 afm = 0;
108 break;
109 case 'r':
110 res = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
111 break;
112 case 's':
113 spc = 1;
114 break;
115 case 't':
116 trfn_trfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
117 break;
118 case 'p':
119 trfn_psfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
120 break;
121 default:
122 printf("%s", usage);
123 return 0;
126 trfn_init(res, spc);
127 if (afm)
128 afm_read();
129 else
130 otfdump_read();
131 trfn_print();
132 trfn_done();
133 return 0;