]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
1 /* $Id: term_ps.c,v 1.8 2010/06/11 15:26:39 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
33 #define PS_CHAR_WIDTH 6
34 #define PS_CHAR_HEIGHT 12
35 #define PS_CHAR_TOPMARG (792 - 24)
36 #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
37 #define PS_CHAR_LEFT 36
38 #define PS_CHAR_BOTMARG 24
39 #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
41 #define PS_BUFSLOP 128
42 #define PS_GROWBUF(p, sz) \
43 do if ((p)->engine.ps.psmargcur + (sz) > \
44 (p)->engine.ps.psmargsz) { \
45 (p)->engine.ps.psmargsz += /* CONSTCOND */ \
46 MAX(PS_BUFSLOP, (sz)); \
47 (p)->engine.ps.psmarg = realloc \
48 ((p)->engine.ps.psmarg, \
49 (p)->engine.ps.psmargsz); \
50 if (NULL == (p)->engine.ps.psmarg) { \
54 } while (/* CONSTCOND */ 0)
56 static void ps_letter(struct termp
*, char);
57 static void ps_begin(struct termp
*);
58 static void ps_end(struct termp
*);
59 static void ps_advance(struct termp
*, size_t);
60 static void ps_endline(struct termp
*);
61 static void ps_pletter(struct termp
*, char);
62 static void ps_printf(struct termp
*, const char *, ...);
63 static void ps_putchar(struct termp
*, char);
71 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
74 p
->type
= TERMTYPE_PS
;
75 p
->letter
= ps_letter
;
78 p
->advance
= ps_advance
;
79 p
->endline
= ps_endline
;
89 p
= (struct termp
*)arg
;
91 if (p
->engine
.ps
.psmarg
)
92 free(p
->engine
.ps
.psmarg
);
99 ps_printf(struct termp
*p
, const char *fmt
, ...)
107 * If we're running in regular mode, then pipe directly into
108 * vprintf(). If we're processing margins, then push the data
109 * into our growable margin buffer.
112 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
119 * XXX: I assume that the in-margin print won't exceed
120 * PS_BUFSLOP (128 bytes), which is reasonable but still an
121 * assumption that will cause pukeage if it's not the case.
124 PS_GROWBUF(p
, PS_BUFSLOP
);
126 pos
= (int)p
->engine
.ps
.psmargcur
;
127 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
128 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
135 ps_putchar(struct termp
*p
, char c
)
139 /* See ps_printf(). */
141 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
148 pos
= (int)p
->engine
.ps
.psmargcur
++;
149 p
->engine
.ps
.psmarg
[pos
++] = c
;
150 p
->engine
.ps
.psmarg
[pos
] = '\0';
156 ps_end(struct termp
*p
)
160 * At the end of the file, do one last showpage. This is the
161 * same behaviour as groff(1) and works for multiple pages as
165 assert(0 == p
->engine
.ps
.psstate
);
166 assert('\0' == p
->engine
.ps
.last
);
167 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
168 printf("%s", p
->engine
.ps
.psmarg
);
169 printf("showpage\n");
170 printf("%s\n", "%%EOF");
175 ps_begin(struct termp
*p
)
179 * Emit the standard PostScript prologue, set our initial page
180 * position, then run pageopen() on the initial page.
183 printf("%s\n", "%!PS");
184 printf("%s\n", "/Courier");
185 printf("%s\n", "10 selectfont");
187 p
->engine
.ps
.psstate
= 0;
189 if (p
->engine
.ps
.psmarg
) {
190 assert(p
->engine
.ps
.psmargsz
);
191 p
->engine
.ps
.psmarg
[0] = '\0';
194 p
->engine
.ps
.psmargcur
= 0;
197 * Now dump the margins into our margin buffer. If we don't do
198 * this, we'd break any current state to run the header and
199 * footer with each and evern new page.
202 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
203 p
->engine
.ps
.psrow
= PS_CHAR_TOPMARG
;
205 p
->engine
.ps
.psstate
|= PS_MARGINS
;
207 (*p
->headf
)(p
, p
->argf
);
210 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
211 assert(0 == p
->engine
.ps
.psstate
);
213 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
214 p
->engine
.ps
.psrow
= PS_CHAR_BOTMARG
;
215 p
->engine
.ps
.psstate
|= PS_MARGINS
;
217 (*p
->footf
)(p
, p
->argf
);
220 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
221 assert(0 == p
->engine
.ps
.psstate
);
223 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
224 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;
226 assert(p
->engine
.ps
.psmarg
);
227 assert('\0' != p
->engine
.ps
.psmarg
[0]);
232 ps_pletter(struct termp
*p
, char c
)
235 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
)) {
237 * If we're not in a PostScript "word" context, then
238 * open one now at the current cursor.
240 ps_printf(p
, "%zu %zu moveto\n(",
243 p
->engine
.ps
.psstate
|= PS_INLINE
;
247 * We need to escape these characters as per the PostScript
248 * specification. We would also escape non-graphable characters
249 * (like tabs), but none of them would get to this point and
250 * it's superfluous to abort() on them.
265 /* Write the character and adjust where we are on the page. */
267 p
->engine
.ps
.pscol
+= PS_CHAR_WIDTH
;
272 ps_letter(struct termp
*p
, char c
)
276 if ('\0' == p
->engine
.ps
.last
) {
278 p
->engine
.ps
.last
= c
;
280 } else if (8 == p
->engine
.ps
.last
) {
282 p
->engine
.ps
.last
= c
;
285 assert(8 != p
->engine
.ps
.last
);
286 p
->engine
.ps
.last
= c
;
289 cc
= p
->engine
.ps
.last
;
290 p
->engine
.ps
.last
= c
;
294 return(ps_pletter(p
, c
));
299 ps_advance(struct termp
*p
, size_t len
)
303 if ('\0' != p
->engine
.ps
.last
) {
304 ps_pletter(p
, p
->engine
.ps
.last
);
305 p
->engine
.ps
.last
= '\0';
308 if (PS_INLINE
& p
->engine
.ps
.psstate
) {
309 assert(8 != p
->engine
.ps
.last
);
310 if (p
->engine
.ps
.last
)
311 ps_letter(p
, p
->engine
.ps
.last
);
312 p
->engine
.ps
.last
= '\0';
313 for (i
= 0; i
< len
; i
++)
318 assert('\0' == p
->engine
.ps
.last
);
319 p
->engine
.ps
.pscol
+= len
? len
* PS_CHAR_WIDTH
: 0;
324 ps_endline(struct termp
*p
)
327 if ('\0' != p
->engine
.ps
.last
) {
328 ps_pletter(p
, p
->engine
.ps
.last
);
329 p
->engine
.ps
.last
= '\0';
332 if (PS_INLINE
& p
->engine
.ps
.psstate
) {
333 assert(8 != p
->engine
.ps
.last
);
334 if (p
->engine
.ps
.last
)
335 ps_letter(p
, p
->engine
.ps
.last
);
336 p
->engine
.ps
.last
= '\0';
337 ps_printf(p
, ") show\n");
338 p
->engine
.ps
.psstate
&= ~PS_INLINE
;
340 assert('\0' == p
->engine
.ps
.last
);
342 if (PS_MARGINS
& p
->engine
.ps
.psstate
)
345 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
346 if (p
->engine
.ps
.psrow
>= PS_CHAR_HEIGHT
+ PS_CHAR_BOT
) {
347 p
->engine
.ps
.psrow
-= PS_CHAR_HEIGHT
;
351 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
352 printf("%s", p
->engine
.ps
.psmarg
);
353 printf("showpage\n");
354 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;