out: ignore invalid font requests
[neatpost.git] / out.c
blobbdfa89c9e0fcd0910396488128a0e13f4a5dfcea
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "post.h"
7 static int o_f, o_s, o_m; /* font and size */
8 static int o_h, o_v; /* current user position */
9 static int p_f, p_s, p_m; /* output postscript font */
10 static int o_qtype; /* queued character type */
11 static int o_qv, o_qh, o_qend; /* queued character position */
12 static int o_rh, o_rv, o_rdeg; /* previous rotation position and degree */
14 char o_fonts[FNLEN * NFONTS] = " ";
16 static void outvf(char *s, va_list ap)
18 vfprintf(stdout, s, ap);
21 static void outf(char *s, ...)
23 va_list ap;
24 va_start(ap, s);
25 outvf(s, ap);
26 va_end(ap);
29 static void o_flush(void)
31 if (o_qtype == 1)
32 outf(") %d %d w\n", o_qh, o_qv);
33 if (o_qtype == 2)
34 outf("] %d %d g\n", o_qh, o_qv);
35 o_qtype = 0;
38 void outpage(void)
40 o_flush();
41 o_v = 0;
42 o_h = 0;
43 p_s = 0;
44 p_f = 0;
45 p_m = 0;
46 o_rdeg = 0;
49 static void o_queue(struct glyph *g)
51 int type = 1 + (g->pos <= 0);
52 if (o_qtype != type || o_qend != o_h || o_qv != o_v) {
53 o_flush();
54 o_qh = o_h;
55 o_qv = o_v;
56 o_qtype = type;
57 outf(type == 1 ? "(" : "[");
59 if (o_qtype == 1) {
60 if (g->pos >= ' ' && g->pos <= '~')
61 outf("%s%c", strchr("()\\", g->pos) ? "\\" : "", g->pos);
62 else
63 outf("\\%d%d%d", (g->pos >> 6) & 7,
64 (g->pos >> 3) & 7, g->pos & 7);
65 } else {
66 outf("/%s", g->id);
68 o_qend = o_h + font_wid(g->font, o_s, g->wid);
71 /* calls o_flush() if necessary */
72 void out(char *s, ...)
74 va_list ap;
75 o_flush();
76 va_start(ap, s);
77 outvf(s, ap);
78 va_end(ap);
81 static void out_fontup(int fid)
83 char fnname[FNLEN];
84 struct font *fn;
85 if (o_m != p_m) {
86 out("%d %d %d rgb\n", CLR_R(o_m), CLR_G(o_m), CLR_B(o_m));
87 p_m = o_m;
89 if (fid != p_f || o_s != p_s) {
90 fn = dev_font(fid);
91 out("%d /%s f\n", o_s, font_name(fn));
92 p_f = fid;
93 p_s = o_s;
94 sprintf(fnname, " %s ", font_name(fn));
95 if (!strstr(o_fonts, fnname))
96 sprintf(strchr(o_fonts, '\0'), "%s ", font_name(fn));
100 void outc(char *c)
102 struct glyph *g;
103 struct font *fn;
104 g = dev_glyph(c, o_f);
105 fn = g ? g->font : dev_font(o_f);
106 if (!g) {
107 outrel(*c == ' ' && fn ? font_swid(fn, o_s) : 1, 0);
108 return;
110 out_fontup(dev_fontid(fn));
111 o_queue(g);
114 void outh(int h)
116 o_h = h;
119 void outv(int v)
121 o_v = v;
124 void outrel(int h, int v)
126 o_h += h;
127 o_v += v;
130 void outfont(int f)
132 if (dev_font(f))
133 o_f = f;
136 /* a font was mounted at pos f */
137 void outmnt(int f)
139 if (p_f == f)
140 p_f = -1;
143 void outsize(int s)
145 o_s = s;
148 void outcolor(int c)
150 o_m = c;
153 void outrotate(int deg)
155 o_flush();
156 out_fontup(o_f);
157 if (o_rdeg)
158 outf("%d %d %d rot\n", -o_rdeg, o_rh, o_rv);
159 o_rdeg = deg;
160 o_rh = o_h;
161 o_rv = o_v;
162 outf("%d %d %d rot\n", deg, o_h, o_v);
165 static int draw_path; /* number of path segments */
166 static int draw_point; /* point was set for postscript newpath */
168 static void drawmv(void)
170 if (!draw_point)
171 outf("%d %d m ", o_h, o_v);
172 draw_point = 1;
175 /* start a multi-segment path */
176 void drawmbeg(char *s)
178 o_flush();
179 out_fontup(o_f);
180 draw_path = 1;
181 outf("gsave newpath %s\n", s);
184 /* end a multi-segment path */
185 void drawmend(char *s)
187 draw_path = 0;
188 draw_point = 0;
189 outf("%s grestore\n", s);
192 void drawbeg(void)
194 o_flush();
195 out_fontup(o_f);
196 if (draw_path)
197 return;
198 outf("newpath ");
201 void drawend(int close, int fill)
203 if (draw_path)
204 return;
205 draw_point = 0;
206 if (close)
207 outf("closepath ");
208 if (fill)
209 outf("fill\n");
210 else
211 outf("stroke\n");
214 void drawl(int h, int v)
216 drawmv();
217 outrel(h, v);
218 outf("%d %d drawl ", o_h, o_v);
221 void drawc(int c)
223 drawmv();
224 outrel(c, 0);
225 outf("%d %d drawe ", c, c);
228 void drawe(int h, int v)
230 drawmv();
231 outrel(h, 0);
232 outf("%d %d drawe ", h, v);
235 void drawa(int h1, int v1, int h2, int v2)
237 drawmv();
238 outf("%d %d %d %d drawa ", h1, v1, h2, v2);
239 outrel(h1 + h2, v1 + v2);
242 void draws(int h1, int v1, int h2, int v2)
244 drawmv();
245 outf("%d %d %d %d %d %d draws ", o_h, o_v, o_h + h1, o_v + v1,
246 o_h + h1 + h2, o_v + v1 + v2);
247 outrel(h1, v1);
250 void outeps(char *spec)
252 char eps[1 << 12];
253 char buf[1 << 12];
254 int llx, lly, urx, ury;
255 int hwid, vwid;
256 FILE *filp;
257 int nspec, nbb;
258 if ((nspec = sscanf(spec, "%s %d %d", eps, &hwid, &vwid)) < 1)
259 return;
260 if (nspec < 2)
261 hwid = 0;
262 if (nspec < 3)
263 vwid = 0;
264 if (!(filp = fopen(eps, "r")))
265 return;
266 if (!fgets(buf, sizeof(buf), filp) ||
267 (strcmp(buf, "%!PS-Adobe-2.0 EPSF-2.0\n") &&
268 strcmp(buf, "%!PS-Adobe-3.0 EPSF-3.0\n"))) {
269 fclose(filp);
270 return;
272 nbb = 0;
273 while (fgets(buf, sizeof(buf), filp))
274 if (!strncmp(buf, "%%BoundingBox: ", 15))
275 if ((nbb = sscanf(buf + 15, "%d %d %d %d",
276 &llx, &lly, &urx, &ury)) == 4)
277 break;
278 fclose(filp);
279 if (nbb < 4) /* no BoundingBox comment */
280 return;
281 if (hwid <= 0 && vwid <= 0)
282 hwid = urx - llx;
283 if (vwid <= 0)
284 vwid = (ury - lly) * hwid / (urx - llx);
285 if (hwid <= 0)
286 hwid = (urx - llx) * vwid / (ury - lly);
287 /* output the EPS file */
288 o_flush();
289 out_fontup(o_f);
290 outf("%d %d %d %d %d %d %d %d EPSFBEG\n",
291 llx, lly, hwid, urx - llx, vwid, ury - lly, o_h, o_v);
292 outf("%%%%BeginDocument: %s\n", eps);
293 filp = fopen(eps, "r");
294 while (fgets(buf, sizeof(buf), filp))
295 out("%s", buf);
296 fclose(filp);
297 outf("%%%%EndDocument\n");
298 outf("EPSFEND\n");