]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
d4eaa9ba4cbd2b8fc059164d618935d53a7ffd51
1 /* $Id: term_ps.c,v 1.12 2010/06/25 19:50:23 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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)
57 static void ps_letter(struct termp
*, char);
58 static void ps_begin(struct termp
*);
59 static void ps_end(struct termp
*);
60 static void ps_advance(struct termp
*, size_t);
61 static void ps_endline(struct termp
*);
62 static void ps_fclose(struct termp
*);
63 static size_t ps_width(const struct termp
*, char);
64 static void ps_pclose(struct termp
*);
65 static void ps_pletter(struct termp
*, char);
66 static void ps_printf(struct termp
*, const char *, ...);
67 static void ps_putchar(struct termp
*, char);
68 static void ps_setfont(struct termp
*, enum termfont
);
76 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
82 p
->type
= TERMTYPE_PS
;
83 p
->letter
= ps_letter
;
86 p
->advance
= ps_advance
;
87 p
->endline
= ps_endline
;
98 p
= (struct termp
*)arg
;
100 if (p
->engine
.ps
.psmarg
)
101 free(p
->engine
.ps
.psmarg
);
108 ps_printf(struct termp
*p
, const char *fmt
, ...)
116 * If we're running in regular mode, then pipe directly into
117 * vprintf(). If we're processing margins, then push the data
118 * into our growable margin buffer.
121 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
128 * XXX: I assume that the in-margin print won't exceed
129 * PS_BUFSLOP (128 bytes), which is reasonable but still an
130 * assumption that will cause pukeage if it's not the case.
133 PS_GROWBUF(p
, PS_BUFSLOP
);
135 pos
= (int)p
->engine
.ps
.psmargcur
;
136 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
137 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
144 ps_putchar(struct termp
*p
, char c
)
148 /* See ps_printf(). */
150 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
157 pos
= (int)p
->engine
.ps
.psmargcur
++;
158 p
->engine
.ps
.psmarg
[pos
++] = c
;
159 p
->engine
.ps
.psmarg
[pos
] = '\0';
165 ps_end(struct termp
*p
)
169 * At the end of the file, do one last showpage. This is the
170 * same behaviour as groff(1) and works for multiple pages as
174 assert(0 == p
->engine
.ps
.psstate
);
175 assert('\0' == p
->engine
.ps
.last
);
176 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
177 printf("%s", p
->engine
.ps
.psmarg
);
178 printf("showpage\n");
179 printf("%s\n", "%%EOF");
184 ps_begin(struct termp
*p
)
188 * Print margins into margin buffer. Nothing gets output to the
189 * screen yet, so we don't need to initialise the primary state.
192 if (p
->engine
.ps
.psmarg
) {
193 assert(p
->engine
.ps
.psmargsz
);
194 p
->engine
.ps
.psmarg
[0] = '\0';
197 p
->engine
.ps
.psmargcur
= 0;
198 p
->engine
.ps
.psstate
= PS_MARGINS
;
199 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
200 p
->engine
.ps
.psrow
= PS_CHAR_TOPMARG
;
202 ps_setfont(p
, TERMFONT_NONE
);
204 (*p
->headf
)(p
, p
->argf
);
207 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
208 p
->engine
.ps
.psrow
= PS_CHAR_BOTMARG
;
210 (*p
->footf
)(p
, p
->argf
);
213 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
215 assert(0 == p
->engine
.ps
.psstate
);
216 assert(p
->engine
.ps
.psmarg
);
217 assert('\0' != p
->engine
.ps
.psmarg
[0]);
220 * Print header and initialise page state. Following this,
221 * stuff gets printed to the screen, so make sure we're sane.
224 printf("%s\n", "%!PS");
225 ps_setfont(p
, TERMFONT_NONE
);
226 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
227 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;
232 ps_pletter(struct termp
*p
, char c
)
236 * If we're not in a PostScript "word" context, then open one
237 * now at the current cursor.
240 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
)) {
241 ps_printf(p
, "%zu %zu moveto\n(",
244 p
->engine
.ps
.psstate
|= PS_INLINE
;
248 * We need to escape these characters as per the PostScript
249 * specification. We would also escape non-graphable characters
250 * (like tabs), but none of them would get to this point and
251 * it's superfluous to abort() on them.
266 /* Write the character and adjust where we are on the page. */
269 p
->engine
.ps
.pscol
+= PS_CHAR_WIDTH
;
274 ps_pclose(struct termp
*p
)
278 * Spit out that we're exiting a word context (this is a
279 * "partial close" because we don't check the last-char buffer
283 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
286 ps_printf(p
, ") show\n");
287 p
->engine
.ps
.psstate
&= ~PS_INLINE
;
292 ps_fclose(struct termp
*p
)
296 * Strong closure: if we have a last-char, spit it out after
297 * checking that we're in the right font mode. This will of
298 * course open a new scope, if applicable.
300 * Following this, close out any scope that's open.
303 if ('\0' != p
->engine
.ps
.last
) {
304 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
306 ps_setfont(p
, TERMFONT_NONE
);
308 ps_pletter(p
, p
->engine
.ps
.last
);
309 p
->engine
.ps
.last
= '\0';
312 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
320 ps_letter(struct termp
*p
, char c
)
325 * State machine dictates whether to buffer the last character
326 * or not. Basically, encoded words are detected by checking if
327 * we're an "8" and switching on the buffer. Then we put "8" in
328 * our buffer, and on the next charater, flush both character
329 * and buffer. Thus, "regular" words are detected by having a
330 * regular character and a regular buffer character.
333 if ('\0' == p
->engine
.ps
.last
) {
335 p
->engine
.ps
.last
= c
;
337 } else if (8 == p
->engine
.ps
.last
) {
339 p
->engine
.ps
.last
= '\0';
341 assert(8 != p
->engine
.ps
.last
);
342 if ('_' == p
->engine
.ps
.last
) {
343 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
345 ps_setfont(p
, TERMFONT_UNDER
);
347 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
349 ps_setfont(p
, TERMFONT_BOLD
);
351 p
->engine
.ps
.last
= c
;
354 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
356 ps_setfont(p
, TERMFONT_NONE
);
358 cc
= p
->engine
.ps
.last
;
359 p
->engine
.ps
.last
= c
;
368 ps_advance(struct termp
*p
, size_t len
)
372 * Advance some spaces. This can probably be made smarter,
373 * i.e., to have multiple space-separated words in the same
374 * scope, but this is easier: just close out the current scope
375 * and readjust our column settings.
379 p
->engine
.ps
.pscol
+= len
? len
* PS_CHAR_WIDTH
: 0;
384 ps_endline(struct termp
*p
)
387 /* Close out any scopes we have open: we're at eoln. */
392 * If we're in the margin, don't try to recalculate our current
393 * row. XXX: if the column tries to be fancy with multiple
394 * lines, we'll do nasty stuff.
397 if (PS_MARGINS
& p
->engine
.ps
.psstate
)
401 * Put us down a line. If we're at the page bottom, spit out a
402 * showpage and restart our row.
405 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
406 if (p
->engine
.ps
.psrow
>= PS_CHAR_HEIGHT
+ PS_CHAR_BOT
) {
407 p
->engine
.ps
.psrow
-= PS_CHAR_HEIGHT
;
411 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
412 printf("%s", p
->engine
.ps
.psmarg
);
413 printf("showpage\n");
414 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;
419 ps_setfont(struct termp
*p
, enum termfont f
)
422 if (TERMFONT_BOLD
== f
)
423 ps_printf(p
, "/Courier-Bold\n");
424 else if (TERMFONT_UNDER
== f
)
425 ps_printf(p
, "/Courier-Oblique\n");
427 ps_printf(p
, "/Courier\n");
429 ps_printf(p
, "10 selectfont\n");
430 p
->engine
.ps
.lastf
= f
;
436 ps_width(const struct termp
*p
, char c
)