tr: use cdef_*() for char definition and cmap_*() for char translation
[neatroff.git] / dev.c
blob9946c59c48c1d1c7041f83b08604d48fb20cc725
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "roff.h"
7 char dev_dir[PATHLEN]; /* device directory */
8 char dev_dev[PATHLEN]; /* output device name */
9 int dev_res; /* device resolution */
10 int dev_uwid; /* device unitwidth */
11 int dev_hor; /* minimum horizontal movement */
12 int dev_ver; /* minimum vertical movement */
14 /* mounted fonts */
15 static char fn_name[NFONTS][FNLEN]; /* font names */
16 static struct font *fn_font[NFONTS]; /* font structs */
17 static int fn_n; /* number of mounted fonts */
19 static void skipline(FILE* filp)
21 int c;
22 do {
23 c = getc(filp);
24 } while (c != '\n' && c != EOF);
27 static void dev_prologue(void)
29 out("x T %s\n", dev_dev);
30 out("x res %d %d %d\n", dev_res, dev_hor, dev_ver);
31 out("x init\n");
34 int dev_mnt(int pos, char *id, char *name)
36 char path[PATHLEN];
37 struct font *fn;
38 sprintf(path, "%s/dev%s/%s", dev_dir, dev_dev, name);
39 fn = font_open(path);
40 if (!fn)
41 return -1;
42 if (fn_font[pos])
43 font_close(fn_font[pos]);
44 if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
45 strcpy(fn_name[pos], id);
46 fn_font[pos] = fn;
47 out("x font %d %s\n", pos, name);
48 return pos;
51 int dev_open(char *dir, char *dev)
53 char path[PATHLEN];
54 char tok[ILNLEN];
55 int i;
56 FILE *desc;
57 strcpy(dev_dir, dir);
58 strcpy(dev_dev, dev);
59 sprintf(path, "%s/dev%s/DESC", dir, dev);
60 desc = fopen(path, "r");
61 if (!desc)
62 return 1;
63 while (fscanf(desc, "%s", tok) == 1) {
64 if (tok[0] == '#') {
65 skipline(desc);
66 continue;
68 if (!strcmp("fonts", tok)) {
69 fscanf(desc, "%d", &fn_n);
70 for (i = 0; i < fn_n; i++)
71 fscanf(desc, "%s", fn_name[i + 1]);
72 fn_n++;
73 continue;
75 if (!strcmp("sizes", tok)) {
76 while (fscanf(desc, "%s", tok) == 1)
77 if (!strcmp("0", tok))
78 break;
79 continue;
81 if (!strcmp("res", tok)) {
82 fscanf(desc, "%d", &dev_res);
83 continue;
85 if (!strcmp("unitwidth", tok)) {
86 fscanf(desc, "%d", &dev_uwid);
87 continue;
89 if (!strcmp("hor", tok)) {
90 fscanf(desc, "%d", &dev_hor);
91 continue;
93 if (!strcmp("ver", tok)) {
94 fscanf(desc, "%d", &dev_ver);
95 continue;
97 if (!strcmp("charset", tok))
98 break;
99 skipline(desc);
101 fclose(desc);
102 dev_prologue();
103 for (i = 0; i < fn_n; i++)
104 if (*fn_name[i])
105 dev_mnt(i, fn_name[i], fn_name[i]);
106 return 0;
109 static void dev_epilogue(void)
111 out("x trailer\n");
112 out("x stop\n");
115 void dev_close(void)
117 int i;
118 dev_epilogue();
119 for (i = 0; i < fn_n; i++) {
120 if (fn_font[i])
121 font_close(fn_font[i]);
122 fn_font[i] = NULL;
126 /* glyph handling functions */
128 struct glyph *dev_glyph(char *c, int fn)
130 struct glyph *g;
131 int i;
132 if ((c[0] == c_ec || c[0] == c_ni) && c[1] == c_ec)
133 c++;
134 if (c[0] == c_ec && c[1] == '(')
135 c += 2;
136 c = cmap_map(c);
137 g = font_find(fn_font[fn], c);
138 if (g)
139 return g;
140 for (i = 0; i < fn_n; i++)
141 if (fn_font[i] && fn_font[i]->special)
142 if ((g = font_find(fn_font[i], c)))
143 return g;
144 return NULL;
147 struct glyph *dev_glyph_byid(char *id, int fn)
149 return font_glyph(fn_font[fn], id);
152 int dev_kernpair(char *c1, char *c2)
154 return 0;
157 /* return the mounted position of font */
158 int dev_pos(char *id)
160 int i;
161 if (isdigit(id[0])) {
162 int num = atoi(id);
163 if (num < 0 || num >= fn_n || !fn_font[num]) {
164 errmsg("bad font position\n");
165 return -1;
167 return num;
169 for (i = 1; i < fn_n; i++)
170 if (!strcmp(fn_name[i], id))
171 return i;
172 if (!strcmp(fn_name[0], id))
173 return 0;
174 return dev_mnt(0, id, id);
177 /* return the font struct at pos */
178 struct font *dev_font(int pos)
180 return pos >= 0 && pos < fn_n ? fn_font[pos] : NULL;
183 int dev_getcs(int fn)
185 return dev_font(fn)->cs;
188 void dev_setcs(int fn, int cs)
190 if (fn >= 0)
191 dev_font(fn)->cs = cs;
194 int dev_getbd(int fn)
196 return dev_font(fn)->bd;
199 void dev_setbd(int fn, int bd)
201 if (fn >= 0)
202 dev_font(fn)->bd = bd;