-
Notifications
You must be signed in to change notification settings - Fork 12
/
dev.c
241 lines (224 loc) · 5.26 KB
/
dev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* output device */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "roff.h"
static char dev_dir[PATHLEN]; /* device directory */
static char dev_dev[PATHLEN]; /* output device name */
int dev_res; /* device resolution */
int dev_uwid; /* device unitwidth */
int dev_hor; /* minimum horizontal movement */
int dev_ver; /* minimum vertical movement */
/* mounted fonts */
static char fn_name[NFONTS][FNLEN]; /* font names */
static struct font *fn_font[NFONTS]; /* font structs */
static int fn_n; /* number of device fonts */
/* .fspecial request */
static char fspecial_fn[NFONTS][FNLEN]; /* .fspecial first arguments */
static char fspecial_sp[NFONTS][FNLEN]; /* .fspecial special fonts */
static int fspecial_n; /* number of fonts in fspecial_sp[] */
static void skipline(FILE* filp)
{
int c;
do {
c = getc(filp);
} while (c != '\n' && c != EOF);
}
static void dev_prologue(void)
{
out("x T %s\n", dev_dev);
out("x res %d %d %d\n", dev_res, dev_hor, dev_ver);
out("x init\n");
}
/* find a position for the given font */
static int dev_position(char *id)
{
int i;
for (i = 1; i < NFONTS; i++) /* already mounted */
if (!strcmp(fn_name[i], id))
return i;
for (i = 1; i < NFONTS; i++) /* the first empty position */
if (!fn_font[i])
return i;
return 0; /* no room left */
}
int dev_mnt(int pos, char *id, char *name)
{
char path[PATHLEN];
struct font *fn;
if (pos >= NFONTS)
return -1;
if (strchr(name, '/'))
snprintf(path, sizeof(path), "%s", name);
else
snprintf(path, sizeof(path), "%s/dev%s/%s", dev_dir, dev_dev, name);
fn = font_open(path);
if (!fn)
return -1;
if (pos < 0)
pos = dev_position(id);
if (fn_font[pos])
font_close(fn_font[pos]);
if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
snprintf(fn_name[pos], sizeof(fn_name[pos]), "%s", id);
fn_font[pos] = fn;
out("x font %d %s\n", pos, name);
return pos;
}
int dev_open(char *dir, char *dev)
{
char path[PATHLEN];
char tok[128];
int i;
FILE *desc;
snprintf(dev_dir, sizeof(dev_dir), "%s", dir);
snprintf(dev_dev, sizeof(dev_dev), "%s", dev);
snprintf(path, sizeof(path), "%s/dev%s/DESC", dir, dev);
desc = fopen(path, "r");
if (!desc)
return 1;
while (fscanf(desc, "%127s", tok) == 1) {
if (tok[0] == '#') {
skipline(desc);
continue;
}
if (!strcmp("fonts", tok)) {
fscanf(desc, "%d", &fn_n);
for (i = 0; i < fn_n; i++)
fscanf(desc, "%s", fn_name[i + 1]);
fn_n++;
continue;
}
if (!strcmp("sizes", tok)) {
while (fscanf(desc, "%128s", tok) == 1)
if (!strcmp("0", tok))
break;
continue;
}
if (!strcmp("res", tok)) {
fscanf(desc, "%d", &dev_res);
continue;
}
if (!strcmp("unitwidth", tok)) {
fscanf(desc, "%d", &dev_uwid);
continue;
}
if (!strcmp("hor", tok)) {
fscanf(desc, "%d", &dev_hor);
continue;
}
if (!strcmp("ver", tok)) {
fscanf(desc, "%d", &dev_ver);
continue;
}
if (!strcmp("charset", tok))
break;
skipline(desc);
}
fclose(desc);
dev_prologue();
for (i = 0; i < fn_n; i++) {
if (*fn_name[i] && dev_mnt(i, fn_name[i], fn_name[i]) < 0) {
errmsg("neatroff: font %s is missing\n", fn_name[i]);
return 1;
}
}
return 0;
}
static void dev_epilogue(void)
{
out("x trailer\n");
out("x stop\n");
}
void dev_close(void)
{
int i;
dev_epilogue();
for (i = 0; i < NFONTS; i++) {
if (fn_font[i])
font_close(fn_font[i]);
fn_font[i] = NULL;
}
}
/* glyph handling functions */
static struct glyph *dev_find(char *c, int fn, int byid)
{
struct glyph *(*find)(struct font *fn, char *name);
struct glyph *g;
int i;
find = byid ? font_glyph : font_find;
if ((g = find(fn_font[fn], c)))
return g;
for (i = 0; i < fspecial_n; i++)
if (dev_pos(fspecial_fn[i]) == fn && dev_pos(fspecial_sp[i]) >= 0)
if ((g = find(dev_font(dev_pos(fspecial_sp[i])), c)))
return g;
for (i = 0; i < NFONTS; i++)
if (fn_font[i] && font_special(fn_font[i]))
if ((g = find(fn_font[i], c)))
return g;
return NULL;
}
struct glyph *dev_glyph(char *c, int fn)
{
if ((c[0] == c_ec || c[0] == c_ni) && c[1] == c_ec)
c++;
if (c[0] == c_ec && c[1] == '(')
c += 2;
c = cmap_map(c);
if (!strncmp("GID=", c, 4))
return dev_find(c + 4, fn, 1);
return dev_find(c, fn, 0);
}
/* return the mounted position of a font */
int dev_pos(char *id)
{
int i;
if (isdigit(id[0])) {
int num = atoi(id);
if (num < 0 || num >= NFONTS || !fn_font[num]) {
errmsg("neatroff: bad font position %s\n", id);
return -1;
}
return num;
}
for (i = 1; i < NFONTS; i++)
if (!strcmp(fn_name[i], id))
return i;
if (!strcmp(fn_name[0], id))
return 0;
return dev_mnt(0, id, id);
}
/* return the mounted position of a font struct */
int dev_fontpos(struct font *fn)
{
int i;
for (i = 0; i < NFONTS; i++)
if (fn_font[i] == fn)
return i;
return 0;
}
/* return the font struct at pos */
struct font *dev_font(int pos)
{
return pos >= 0 && pos < NFONTS ? fn_font[pos] : NULL;
}
void tr_fspecial(char **args)
{
char *fn = args[1];
int i;
if (!fn) {
fspecial_n = 0;
return;
}
for (i = 2; i < NARGS; i++) {
if (args[i] && fspecial_n < LEN(fspecial_fn)) {
snprintf(fspecial_fn[fspecial_n],
sizeof(fspecial_fn[fspecial_n]), "%s", fn);
snprintf(fspecial_sp[fspecial_n],
sizeof(fspecial_sp[fspecial_n]), "%s", args[i]);
fspecial_n++;
}
}
}