ps: put the findfont call near scalefont and setfont calls
[neatpost.git] / out.c
blob6bc65a969bca6ada0fb5c9b138b67ca863163265
1 #include <ctype.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "post.h"
8 static int o_f, o_s, o_m; /* font and size */
9 static int o_h, o_v; /* current user position */
10 static int p_f, p_s, p_m; /* output postscript font */
11 static int o_qtype; /* queued character type */
12 static int o_qv, o_qh, o_qend; /* queued character position */
13 char o_fonts[FNLEN * NFONTS] = " ";
15 static void outvf(char *s, va_list ap)
17 vfprintf(stdout, s, ap);
20 void outf(char *s, ...)
22 va_list ap;
23 va_start(ap, s);
24 outvf(s, ap);
25 va_end(ap);
28 static void o_flush(void)
30 if (o_qtype == 1)
31 outf(") %d %d w\n", o_qh, o_qv);
32 if (o_qtype == 2)
33 outf("] %d %d g\n", o_qh, o_qv);
34 o_qtype = 0;
37 void outpage(void)
39 o_flush();
40 o_v = 0;
41 o_h = 0;
42 p_s = 0;
43 p_f = 0;
44 p_m = 0;
47 static void o_queue(struct glyph *g)
49 int type = 1 + !isdigit((g)->id[0]);
50 int num = atoi(g->id);
51 if (o_qtype != type || o_qend != o_h || o_qv != o_v) {
52 o_flush();
53 o_qh = o_h;
54 o_qv = o_v;
55 o_qtype = type;
56 outf(type == 1 ? "(" : "[");
58 if (o_qtype == 1) {
59 if (num >= ' ' && num <= '~')
60 outf("%s%c", strchr("()\\", num) ? "\\" : "", num);
61 else
62 outf("\\%d%d%d", (num >> 6) & 7, (num >> 3) & 7, num & 7);
63 } else {
64 outf("/%s", g->id);
66 o_qend = o_h + charwid(g->wid, o_s);
69 /* calls o_flush() if necessary */
70 void out(char *s, ...)
72 va_list ap;
73 o_flush();
74 va_start(ap, s);
75 outvf(s, ap);
76 va_end(ap);
79 /* glyph placement adjustments */
80 static struct fixlist {
81 char *name;
82 int dh, dv;
83 } fixlist[] = {
84 {"br", -5, 15},
85 {"lc", 20, 0},
86 {"lf", 20, 0},
87 {"rc", -11, 0},
88 {"rf", -11, 0},
89 {"rn", -50, 0},
92 static void fixpos(struct glyph *g, int *dh, int *dv)
94 struct font *fn = g->font;
95 int i;
96 *dh = 0;
97 *dv = 0;
98 if (!strcmp("S", fn->name)) {
99 for (i = 0; i < LEN(fixlist); i++) {
100 if (!strcmp(fixlist[i].name, g->name)) {
101 *dh = charwid(fixlist[i].dh, o_s);
102 *dv = charwid(fixlist[i].dv, o_s);
103 return;
109 static void out_fontup(int fid)
111 char fnname[FNLEN];
112 struct font *fn;
113 if (o_m != p_m) {
114 out("%d %d %d rgb\n", CLR_R(o_m), CLR_G(o_m), CLR_B(o_m));
115 p_m = o_m;
117 if (fid != p_f || o_s != p_s) {
118 fn = dev_font(fid);
119 out("%d /%s f\n", o_s, fn->fontname);
120 p_f = fid;
121 p_s = o_s;
122 sprintf(fnname, " %s ", fn->fontname);
123 if (!strstr(o_fonts, fnname))
124 sprintf(strchr(o_fonts, '\0'), "%s ", fn->fontname);
128 void outc(char *c)
130 struct glyph *g;
131 struct font *fn;
132 int dh, dv;
133 g = dev_glyph(c, o_f);
134 fn = g ? g->font : dev_font(o_f);
135 if (!g) {
136 outrel(*c == ' ' && fn ? charwid(fn->spacewid, o_s) : 1, 0);
137 return;
139 out_fontup(dev_fontid(fn));
140 fixpos(g, &dh, &dv);
141 o_h += dh;
142 o_v += dv;
143 o_queue(g);
144 o_h -= dh;
145 o_v -= dv;
148 void outh(int h)
150 o_h = h;
153 void outv(int v)
155 o_v = v;
158 void outrel(int h, int v)
160 o_h += h;
161 o_v += v;
164 void outfont(int f)
166 o_f = f;
169 /* a font was mounted at pos f */
170 void outmnt(int f)
172 if (p_f == f)
173 p_f = -1;
176 void outsize(int s)
178 o_s = s;
181 void outcolor(int c)
183 o_m = c;
186 static int draw_path; /* number of path segments */
187 static int draw_point; /* point was set for postscript newpath */
189 static void drawmv(void)
191 if (!draw_point)
192 outf("%d %d m ", o_h, o_v);
193 draw_point = 1;
196 /* if s is not NULL, start a multi-segment path */
197 void drawbeg(char *s)
199 o_flush();
200 out_fontup(o_f);
201 if (draw_path)
202 return;
203 draw_path = s != NULL;
204 if (s)
205 outf("gsave newpath %s\n", s);
206 else
207 outf("newpath ");
210 void drawend(char *s)
212 if (draw_path && !s)
213 return;
214 draw_path = 0;
215 draw_point = 0;
216 if (s)
217 outf("%s grestore\n", s);
218 else
219 outf("stroke\n");
222 void drawl(int h, int v)
224 drawmv();
225 outrel(h, v);
226 outf("%d %d drawl ", o_h, o_v);
229 void drawc(int c)
231 drawmv();
232 outrel(c, 0);
233 outf("%d %d drawe ", c, c);
236 void drawe(int h, int v)
238 drawmv();
239 outrel(h, 0);
240 outf("%d %d drawe ", h, v);
243 void drawa(int h1, int v1, int h2, int v2)
245 drawmv();
246 outf("%d %d %d %d drawa ", h1, v1, h2, v2);
247 outrel(h1 + h2, v1 + v2);
250 void draws(int h1, int v1, int h2, int v2)
252 drawmv();
253 outf("%d %d %d %d %d %d draws ", o_h, o_v, o_h + h1, o_v + v1,
254 o_h + h1 + h2, o_v + v1 + v2);
255 outrel(h1, v1);