tr: fix .cc and .c2
[neatroff.git] / dev.c
blob9da11c16b9ce9d291b01fa0a634d2160d9d9569a
1 /* output device */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "roff.h"
8 static char dev_dir[PATHLEN]; /* device directory */
9 static char dev_dev[PATHLEN]; /* output device name */
10 int dev_res; /* device resolution */
11 int dev_uwid; /* device unitwidth */
12 int dev_hor; /* minimum horizontal movement */
13 int dev_ver; /* minimum vertical movement */
15 /* mounted fonts */
16 static char fn_name[NFONTS][FNLEN]; /* font names */
17 static struct font *fn_font[NFONTS]; /* font structs */
18 static int fn_n; /* number of device fonts */
20 /* .fspecial request */
21 static char fspecial_fn[NFONTS][FNLEN]; /* .fspecial first arguments */
22 static char fspecial_sp[NFONTS][FNLEN]; /* .fspecial special fonts */
23 static int fspecial_n; /* number of fonts in fspecial_sp[] */
25 static void skipline(FILE* filp)
27 int c;
28 do {
29 c = getc(filp);
30 } while (c != '\n' && c != EOF);
33 static void dev_prologue(void)
35 out("x T %s\n", dev_dev);
36 out("x res %d %d %d\n", dev_res, dev_hor, dev_ver);
37 out("x init\n");
40 int dev_mnt(int pos, char *id, char *name)
42 char path[PATHLEN];
43 struct font *fn;
44 if (pos >= NFONTS)
45 return -1;
46 if (strchr(name, '/'))
47 strcpy(path, name);
48 else
49 sprintf(path, "%s/dev%s/%s", dev_dir, dev_dev, name);
50 fn = font_open(path);
51 if (!fn)
52 return -1;
53 if (fn_font[pos])
54 font_close(fn_font[pos]);
55 if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
56 strcpy(fn_name[pos], id);
57 fn_font[pos] = fn;
58 out("x font %d %s\n", pos, name);
59 return pos;
62 int dev_open(char *dir, char *dev)
64 char path[PATHLEN];
65 char tok[ILNLEN];
66 int i;
67 FILE *desc;
68 strcpy(dev_dir, dir);
69 strcpy(dev_dev, dev);
70 sprintf(path, "%s/dev%s/DESC", dir, dev);
71 desc = fopen(path, "r");
72 if (!desc)
73 return 1;
74 while (fscanf(desc, "%s", tok) == 1) {
75 if (tok[0] == '#') {
76 skipline(desc);
77 continue;
79 if (!strcmp("fonts", tok)) {
80 fscanf(desc, "%d", &fn_n);
81 for (i = 0; i < fn_n; i++)
82 fscanf(desc, "%s", fn_name[i + 1]);
83 fn_n++;
84 continue;
86 if (!strcmp("sizes", tok)) {
87 while (fscanf(desc, "%s", tok) == 1)
88 if (!strcmp("0", tok))
89 break;
90 continue;
92 if (!strcmp("res", tok)) {
93 fscanf(desc, "%d", &dev_res);
94 continue;
96 if (!strcmp("unitwidth", tok)) {
97 fscanf(desc, "%d", &dev_uwid);
98 continue;
100 if (!strcmp("hor", tok)) {
101 fscanf(desc, "%d", &dev_hor);
102 continue;
104 if (!strcmp("ver", tok)) {
105 fscanf(desc, "%d", &dev_ver);
106 continue;
108 if (!strcmp("charset", tok))
109 break;
110 skipline(desc);
112 fclose(desc);
113 dev_prologue();
114 for (i = 0; i < fn_n; i++)
115 if (*fn_name[i])
116 dev_mnt(i, fn_name[i], fn_name[i]);
117 return 0;
120 static void dev_epilogue(void)
122 out("x trailer\n");
123 out("x stop\n");
126 void dev_close(void)
128 int i;
129 dev_epilogue();
130 for (i = 0; i < NFONTS; i++) {
131 if (fn_font[i])
132 font_close(fn_font[i]);
133 fn_font[i] = NULL;
137 /* glyph handling functions */
139 static struct glyph *dev_find(char *c, int fn, int byid)
141 struct glyph *(*find)(struct font *fn, char *name);
142 struct glyph *g;
143 int i;
144 find = byid ? font_glyph : font_find;
145 if ((g = find(fn_font[fn], c)))
146 return g;
147 for (i = 0; i < fspecial_n; i++)
148 if (dev_pos(fspecial_fn[i]) == fn && dev_pos(fspecial_sp[i]) >= 0)
149 if ((g = find(dev_font(dev_pos(fspecial_sp[i])), c)))
150 return g;
151 for (i = 0; i < NFONTS; i++)
152 if (fn_font[i] && font_special(fn_font[i]))
153 if ((g = find(fn_font[i], c)))
154 return g;
155 return NULL;
158 struct glyph *dev_glyph(char *c, int fn)
160 if ((c[0] == c_ec || c[0] == c_ni) && c[1] == c_ec)
161 c++;
162 if (c[0] == c_ec && c[1] == '(')
163 c += 2;
164 c = cmap_map(c);
165 if (!strncmp("GID=", c, 4))
166 return dev_find(c + 4, fn, 1);
167 return dev_find(c, fn, 0);
170 /* return the mounted position of a font */
171 int dev_pos(char *id)
173 int i;
174 if (isdigit(id[0])) {
175 int num = atoi(id);
176 if (num < 0 || num >= NFONTS || !fn_font[num]) {
177 errmsg("bad font position\n");
178 return -1;
180 return num;
182 for (i = 1; i < NFONTS; i++)
183 if (!strcmp(fn_name[i], id))
184 return i;
185 if (!strcmp(fn_name[0], id))
186 return 0;
187 return dev_mnt(0, id, id);
190 /* return the mounted position of a font struct */
191 int dev_fontpos(struct font *fn)
193 int i;
194 for (i = 0; i < NFONTS; i++)
195 if (fn_font[i] == fn)
196 return i;
197 return 0;
200 /* return the font struct at pos */
201 struct font *dev_font(int pos)
203 return pos >= 0 && pos < NFONTS ? fn_font[pos] : NULL;
206 void tr_fspecial(char **args)
208 char *fn = args[1];
209 int i;
210 if (!fn) {
211 fspecial_n = 0;
212 return;
214 for (i = 2; i < NARGS; i++) {
215 if (args[i] && fspecial_n < LEN(fspecial_fn)) {
216 strcpy(fspecial_fn[fspecial_n], fn);
217 strcpy(fspecial_sp[fspecial_n], args[i]);
218 fspecial_n++;