Sync-to-go: update copyright for 2015
[s-roff.git] / src / ute-addftinfo / addftinfo.cpp
blob914108ab573426b6b996bd20e5ccd49c074eb67e
1 /*@
2 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1989 - 1992, 2000, 2001 Free Software Foundation, Inc.
5 * Written by James Clark (jjc@jclark.com)
7 * This is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any later
10 * version.
12 * This is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with groff; see the file COPYING. If not, write to the Free Software
19 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "config.h"
23 #include "addftinfo-config.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdlib.h>
30 #include "cset.h"
31 #include "errarg.h"
32 #include "error.h"
33 #include "guess.h"
34 #include "lib.h"
35 #include "stringclass.h"
37 static void usage(FILE *stream);
38 static void usage();
39 static void version();
40 static void convert_font(const font_params &, FILE *, FILE *);
42 typedef int font_params::*param_t;
44 static struct {
45 const char *name;
46 param_t par;
47 } param_table[] = { // FIXME const
48 { "x-height", &font_params::x_height },
49 { "fig-height", &font_params::fig_height },
50 { "asc-height", &font_params::asc_height },
51 { "body-height", &font_params::body_height },
52 { "cap-height", &font_params::cap_height },
53 { "comma-depth", &font_params::comma_depth },
54 { "desc-depth", &font_params::desc_depth },
55 { "body-depth", &font_params::body_depth },
58 int main(int argc, char **argv)
60 program_name = argv[0];
61 int i;
62 for (i = 1; i < argc; i++) {
63 if (!strcmp(argv[i], "-v") || !strcmp(argv[i],"--version"))
64 version();
65 if (!strcmp(argv[i],"--help")) {
66 usage(stdout);
67 exit(0);
70 if (argc < 4)
71 usage();
72 int resolution;
73 if (sscanf(argv[argc-3], "%d", &resolution) != 1)
74 usage();
75 if (resolution <= 0)
76 fatal("resolution must be > 0");
77 int unitwidth;
78 if (sscanf(argv[argc-2], "%d", &unitwidth) != 1)
79 usage();
80 if (unitwidth <= 0)
81 fatal("unitwidth must be > 0");
82 font_params param;
83 const char *font = argv[argc-1];
84 param.italic = (font[0] != '\0' && strchr(font, '\0')[-1] == 'I');
85 param.em = (resolution*unitwidth)/72;
86 param.x_height = DEFAULT_X_HEIGHT;
87 param.fig_height = DEFAULT_FIG_HEIGHT;
88 param.asc_height = DEFAULT_ASC_HEIGHT;
89 param.body_height = DEFAULT_BODY_HEIGHT;
90 param.cap_height = DEFAULT_CAP_HEIGHT;
91 param.comma_depth = DEFAULT_COMMA_DEPTH;
92 param.desc_depth = DEFAULT_DESC_DEPTH;
93 param.body_depth = DEFAULT_BODY_DEPTH;
94 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
95 if (argv[i][1] == '-' && argv[i][2] == '\0') {
96 i++;
97 break;
99 if (i + 1 >= argc)
100 usage();
101 size_t j;
102 for (j = 0;; j++) {
103 if (j >= NELEM(param_table))
104 fatal("parameter `%1' not recognized", argv[i] + 1);
105 if (strcmp(param_table[j].name, argv[i] + 1) == 0)
106 break;
108 if (sscanf(argv[i+1], "%d", &(param.*(param_table[j].par))) != 1)
109 fatal("invalid argument `%1'", argv[i+1]);
110 i++;
112 if (argc - i != 3)
113 usage();
114 errno = 0;
115 FILE *infp = fopen(font, "r");
116 if (infp == 0)
117 fatal("can't open `%1': %2", font, strerror(errno));
118 convert_font(param, infp, stdout);
119 return 0;
122 static void usage(FILE *stream)
124 fprintf(stream, "Synopsis: %s [-v] [-param value] ... "
125 "resolution unitwidth font\n",
126 program_name);
128 static void usage()
130 usage(stderr);
131 exit(1);
134 static void version()
136 printf(L_ADDFTINFO " (" T_ROFF ") v" VERSION);
137 exit(0);
140 static int get_line(FILE *fp, string *p)
142 int c;
143 p->clear();
144 while ((c = getc(fp)) != EOF) {
145 *p += char(c);
146 if (c == '\n')
147 break;
149 return p->length() > 0;
152 static void convert_font(const font_params &param, FILE *infp, FILE *outfp)
154 string s;
155 while (get_line(infp, &s)) {
156 put_string(s, outfp);
157 if (s.length() >= 8
158 && strncmp(&s[0], "charset", 7))
159 break;
161 while (get_line(infp, &s)) {
162 s += '\0';
163 string name;
164 const char *p = s.contents();
165 while (csspace(*p))
166 p++;
167 while (*p != '\0' && !csspace(*p))
168 name += *p++;
169 while (csspace(*p))
170 p++;
171 for (const char *q = s.contents(); q < p; q++)
172 putc(*q, outfp);
173 char *next;
174 char_metric metric;
175 metric.width = (int)strtol(p, &next, 10);
176 if (next != p) {
177 printf("%d", metric.width);
178 p = next;
179 metric.type = (int)strtol(p, &next, 10);
180 if (next != p) {
181 name += '\0';
182 guess(name.contents(), param, &metric);
183 if (metric.sk == 0) {
184 if (metric.left_ic == 0) {
185 if (metric.ic == 0) {
186 if (metric.depth == 0) {
187 if (metric.height != 0)
188 printf(",%d", metric.height);
190 else
191 printf(",%d,%d", metric.height, metric.depth);
193 else
194 printf(",%d,%d,%d", metric.height, metric.depth, metric.ic);
196 else
197 printf(",%d,%d,%d,%d", metric.height, metric.depth, metric.ic,
198 metric.left_ic);
200 else
201 printf(",%d,%d,%d,%d,%d", metric.height, metric.depth, metric.ic,
202 metric.left_ic, metric.sk);
205 fputs(p, outfp);
209 // s-it2-mode