]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
56d1be2400880cd705e3f402ee91410cb7490c69
1 /* $Id: term_ps.c,v 1.10 2010/06/19 20:46:28 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 void ps_pclose(struct termp
*);
64 static void ps_pletter(struct termp
*, char);
65 static void ps_printf(struct termp
*, const char *, ...);
66 static void ps_putchar(struct termp
*, char);
67 static void ps_setfont(struct termp
*, enum termfont
);
75 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
78 p
->type
= TERMTYPE_PS
;
79 p
->letter
= ps_letter
;
82 p
->advance
= ps_advance
;
83 p
->endline
= ps_endline
;
93 p
= (struct termp
*)arg
;
95 if (p
->engine
.ps
.psmarg
)
96 free(p
->engine
.ps
.psmarg
);
103 ps_printf(struct termp
*p
, const char *fmt
, ...)
111 * If we're running in regular mode, then pipe directly into
112 * vprintf(). If we're processing margins, then push the data
113 * into our growable margin buffer.
116 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
123 * XXX: I assume that the in-margin print won't exceed
124 * PS_BUFSLOP (128 bytes), which is reasonable but still an
125 * assumption that will cause pukeage if it's not the case.
128 PS_GROWBUF(p
, PS_BUFSLOP
);
130 pos
= (int)p
->engine
.ps
.psmargcur
;
131 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
132 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
139 ps_putchar(struct termp
*p
, char c
)
143 /* See ps_printf(). */
145 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
152 pos
= (int)p
->engine
.ps
.psmargcur
++;
153 p
->engine
.ps
.psmarg
[pos
++] = c
;
154 p
->engine
.ps
.psmarg
[pos
] = '\0';
160 ps_end(struct termp
*p
)
164 * At the end of the file, do one last showpage. This is the
165 * same behaviour as groff(1) and works for multiple pages as
169 assert(0 == p
->engine
.ps
.psstate
);
170 assert('\0' == p
->engine
.ps
.last
);
171 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
172 printf("%s", p
->engine
.ps
.psmarg
);
173 printf("showpage\n");
174 printf("%s\n", "%%EOF");
179 ps_begin(struct termp
*p
)
183 * Print margins into margin buffer. Nothing gets output to the
184 * screen yet, so we don't need to initialise the primary state.
187 if (p
->engine
.ps
.psmarg
) {
188 assert(p
->engine
.ps
.psmargsz
);
189 p
->engine
.ps
.psmarg
[0] = '\0';
192 p
->engine
.ps
.psmargcur
= 0;
193 p
->engine
.ps
.psstate
= PS_MARGINS
;
194 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
195 p
->engine
.ps
.psrow
= PS_CHAR_TOPMARG
;
197 ps_setfont(p
, TERMFONT_NONE
);
199 (*p
->headf
)(p
, p
->argf
);
202 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
203 p
->engine
.ps
.psrow
= PS_CHAR_BOTMARG
;
205 (*p
->footf
)(p
, p
->argf
);
208 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
210 assert(0 == p
->engine
.ps
.psstate
);
211 assert(p
->engine
.ps
.psmarg
);
212 assert('\0' != p
->engine
.ps
.psmarg
[0]);
215 * Print header and initialise page state. Following this,
216 * stuff gets printed to the screen, so make sure we're sane.
219 printf("%s\n", "%!PS");
220 ps_setfont(p
, TERMFONT_NONE
);
221 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
222 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;
227 ps_pletter(struct termp
*p
, char c
)
231 * If we're not in a PostScript "word" context, then open one
232 * now at the current cursor.
235 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
)) {
236 ps_printf(p
, "%zu %zu moveto\n(",
239 p
->engine
.ps
.psstate
|= PS_INLINE
;
243 * We need to escape these characters as per the PostScript
244 * specification. We would also escape non-graphable characters
245 * (like tabs), but none of them would get to this point and
246 * it's superfluous to abort() on them.
261 /* Write the character and adjust where we are on the page. */
264 p
->engine
.ps
.pscol
+= PS_CHAR_WIDTH
;
269 ps_pclose(struct termp
*p
)
273 * Spit out that we're exiting a word context (this is a
274 * "partial close" because we don't check the last-char buffer
278 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
281 ps_printf(p
, ") show\n");
282 p
->engine
.ps
.psstate
&= ~PS_INLINE
;
287 ps_fclose(struct termp
*p
)
291 * Strong closure: if we have a last-char, spit it out after
292 * checking that we're in the right font mode. This will of
293 * course open a new scope, if applicable.
295 * Following this, close out any scope that's open.
298 if ('\0' != p
->engine
.ps
.last
) {
299 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
301 ps_setfont(p
, TERMFONT_NONE
);
303 ps_pletter(p
, p
->engine
.ps
.last
);
304 p
->engine
.ps
.last
= '\0';
307 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
315 ps_letter(struct termp
*p
, char c
)
320 * State machine dictates whether to buffer the last character
321 * or not. Basically, encoded words are detected by checking if
322 * we're an "8" and switching on the buffer. Then we put "8" in
323 * our buffer, and on the next charater, flush both character
324 * and buffer. Thus, "regular" words are detected by having a
325 * regular character and a regular buffer character.
328 if ('\0' == p
->engine
.ps
.last
) {
330 p
->engine
.ps
.last
= c
;
332 } else if (8 == p
->engine
.ps
.last
) {
334 p
->engine
.ps
.last
= '\0';
336 assert(8 != p
->engine
.ps
.last
);
337 if ('_' == p
->engine
.ps
.last
) {
338 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
340 ps_setfont(p
, TERMFONT_UNDER
);
342 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
344 ps_setfont(p
, TERMFONT_BOLD
);
346 p
->engine
.ps
.last
= c
;
349 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
351 ps_setfont(p
, TERMFONT_NONE
);
353 cc
= p
->engine
.ps
.last
;
354 p
->engine
.ps
.last
= c
;
363 ps_advance(struct termp
*p
, size_t len
)
367 * Advance some spaces. This can probably be made smarter,
368 * i.e., to have multiple space-separated words in the same
369 * scope, but this is easier: just close out the current scope
370 * and readjust our column settings.
374 p
->engine
.ps
.pscol
+= len
? len
* PS_CHAR_WIDTH
: 0;
379 ps_endline(struct termp
*p
)
382 /* Close out any scopes we have open: we're at eoln. */
387 * If we're in the margin, don't try to recalculate our current
388 * row. XXX: if the column tries to be fancy with multiple
389 * lines, we'll do nasty stuff.
392 if (PS_MARGINS
& p
->engine
.ps
.psstate
)
396 * Put us down a line. If we're at the page bottom, spit out a
397 * showpage and restart our row.
400 p
->engine
.ps
.pscol
= PS_CHAR_LEFT
;
401 if (p
->engine
.ps
.psrow
>= PS_CHAR_HEIGHT
+ PS_CHAR_BOT
) {
402 p
->engine
.ps
.psrow
-= PS_CHAR_HEIGHT
;
406 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
407 printf("%s", p
->engine
.ps
.psmarg
);
408 printf("showpage\n");
409 p
->engine
.ps
.psrow
= PS_CHAR_TOP
;
414 ps_setfont(struct termp
*p
, enum termfont f
)
417 if (TERMFONT_BOLD
== f
)
418 ps_printf(p
, "/Courier-Bold\n");
419 else if (TERMFONT_UNDER
== f
)
420 ps_printf(p
, "/Courier-Oblique\n");
422 ps_printf(p
, "/Courier\n");
424 ps_printf(p
, "10 selectfont\n");
425 p
->engine
.ps
.lastf
= f
;