xref: /openbsd/usr.bin/mandoc/term_ps.c (revision 91f110e0)
1 /*	$Id: term_ps.c,v 1.21 2014/03/21 22:17:01 schwarze Exp $ */
2 /*
3  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
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.
8  *
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.
16  */
17 #include <sys/types.h>
18 
19 #include <assert.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30 #include "out.h"
31 #include "main.h"
32 #include "term.h"
33 
34 /* These work the buffer used by the header and footer. */
35 #define	PS_BUFSLOP	  128
36 
37 /* Convert PostScript point "x" to an AFM unit. */
38 #define	PNT2AFM(p, x) /* LINTED */ \
39 	(size_t)((double)(x) * (1000.0 / (double)(p)->ps->scale))
40 
41 /* Convert an AFM unit "x" to a PostScript points */
42 #define	AFM2PNT(p, x) /* LINTED */ \
43 	((double)(x) / (1000.0 / (double)(p)->ps->scale))
44 
45 struct	glyph {
46 	unsigned short	  wx; /* WX in AFM */
47 };
48 
49 struct	font {
50 	const char	 *name; /* FontName in AFM */
51 #define	MAXCHAR		  95 /* total characters we can handle */
52 	struct glyph	  gly[MAXCHAR]; /* glyph metrics */
53 };
54 
55 struct	termp_ps {
56 	int		  flags;
57 #define	PS_INLINE	 (1 << 0)	/* we're in a word */
58 #define	PS_MARGINS	 (1 << 1)	/* we're in the margins */
59 #define	PS_NEWPAGE	 (1 << 2)	/* new page, no words yet */
60 	size_t		  pscol;	/* visible column (AFM units) */
61 	size_t		  psrow;	/* visible row (AFM units) */
62 	char		 *psmarg;	/* margin buf */
63 	size_t		  psmargsz;	/* margin buf size */
64 	size_t		  psmargcur;	/* cur index in margin buf */
65 	char		  last;		/* character buffer */
66 	enum termfont	  lastf;	/* last set font */
67 	size_t		  scale;	/* font scaling factor */
68 	size_t		  pages;	/* number of pages shown */
69 	size_t		  lineheight;	/* line height (AFM units) */
70 	size_t		  top;		/* body top (AFM units) */
71 	size_t		  bottom;	/* body bottom (AFM units) */
72 	size_t		  height;	/* page height (AFM units */
73 	size_t		  width;	/* page width (AFM units) */
74 	size_t		  left;		/* body left (AFM units) */
75 	size_t		  header;	/* header pos (AFM units) */
76 	size_t		  footer;	/* footer pos (AFM units) */
77 	size_t		  pdfbytes; 	/* current output byte */
78 	size_t		  pdflastpg;	/* byte of last page mark */
79 	size_t		  pdfbody;	/* start of body object */
80 	size_t		 *pdfobjs;	/* table of object offsets */
81 	size_t		  pdfobjsz;	/* size of pdfobjs */
82 };
83 
84 static	double		  ps_hspan(const struct termp *,
85 				const struct roffsu *);
86 static	size_t		  ps_width(const struct termp *, int);
87 static	void		  ps_advance(struct termp *, size_t);
88 static	void		  ps_begin(struct termp *);
89 static	void		  ps_closepage(struct termp *);
90 static	void		  ps_end(struct termp *);
91 static	void		  ps_endline(struct termp *);
92 static	void		  ps_fclose(struct termp *);
93 static	void		  ps_growbuf(struct termp *, size_t);
94 static	void		  ps_letter(struct termp *, int);
95 static	void		  ps_pclose(struct termp *);
96 static	void		  ps_pletter(struct termp *, int);
97 static	void		  ps_printf(struct termp *, const char *, ...);
98 static	void		  ps_putchar(struct termp *, char);
99 static	void		  ps_setfont(struct termp *, enum termfont);
100 static	struct termp	 *pspdf_alloc(char *);
101 static	void		  pdf_obj(struct termp *, size_t);
102 
103 /*
104  * We define, for the time being, three fonts: bold, oblique/italic, and
105  * normal (roman).  The following table hard-codes the font metrics for
106  * ASCII, i.e., 32--127.
107  */
108 
109 static	const struct font fonts[TERMFONT__MAX] = {
110 	{ "Times-Roman", {
111 		{ 250 },
112 		{ 333 },
113 		{ 408 },
114 		{ 500 },
115 		{ 500 },
116 		{ 833 },
117 		{ 778 },
118 		{ 333 },
119 		{ 333 },
120 		{ 333 },
121 		{ 500 },
122 		{ 564 },
123 		{ 250 },
124 		{ 333 },
125 		{ 250 },
126 		{ 278 },
127 		{ 500 },
128 		{ 500 },
129 		{ 500 },
130 		{ 500 },
131 		{ 500 },
132 		{ 500 },
133 		{ 500 },
134 		{ 500 },
135 		{ 500 },
136 		{ 500 },
137 		{ 278 },
138 		{ 278 },
139 		{ 564 },
140 		{ 564 },
141 		{ 564 },
142 		{ 444 },
143 		{ 921 },
144 		{ 722 },
145 		{ 667 },
146 		{ 667 },
147 		{ 722 },
148 		{ 611 },
149 		{ 556 },
150 		{ 722 },
151 		{ 722 },
152 		{ 333 },
153 		{ 389 },
154 		{ 722 },
155 		{ 611 },
156 		{ 889 },
157 		{ 722 },
158 		{ 722 },
159 		{ 556 },
160 		{ 722 },
161 		{ 667 },
162 		{ 556 },
163 		{ 611 },
164 		{ 722 },
165 		{ 722 },
166 		{ 944 },
167 		{ 722 },
168 		{ 722 },
169 		{ 611 },
170 		{ 333 },
171 		{ 278 },
172 		{ 333 },
173 		{ 469 },
174 		{ 500 },
175 		{ 333 },
176 		{ 444 },
177 		{ 500 },
178 		{ 444 },
179 		{  500},
180 		{  444},
181 		{  333},
182 		{  500},
183 		{  500},
184 		{  278},
185 		{  278},
186 		{  500},
187 		{  278},
188 		{  778},
189 		{  500},
190 		{  500},
191 		{  500},
192 		{  500},
193 		{  333},
194 		{  389},
195 		{  278},
196 		{  500},
197 		{  500},
198 		{  722},
199 		{  500},
200 		{  500},
201 		{  444},
202 		{  480},
203 		{  200},
204 		{  480},
205 		{  541},
206 	} },
207 	{ "Times-Bold", {
208 		{ 250  },
209 		{ 333  },
210 		{ 555  },
211 		{ 500  },
212 		{ 500  },
213 		{ 1000 },
214 		{ 833  },
215 		{ 333  },
216 		{ 333  },
217 		{ 333  },
218 		{ 500  },
219 		{ 570  },
220 		{ 250  },
221 		{ 333  },
222 		{ 250  },
223 		{ 278  },
224 		{ 500  },
225 		{ 500  },
226 		{ 500  },
227 		{ 500  },
228 		{ 500  },
229 		{ 500  },
230 		{ 500  },
231 		{ 500  },
232 		{ 500  },
233 		{ 500  },
234 		{ 333  },
235 		{ 333  },
236 		{ 570  },
237 		{ 570  },
238 		{ 570  },
239 		{ 500  },
240 		{ 930  },
241 		{ 722  },
242 		{ 667  },
243 		{ 722  },
244 		{ 722  },
245 		{ 667  },
246 		{ 611  },
247 		{ 778  },
248 		{ 778  },
249 		{ 389  },
250 		{ 500  },
251 		{ 778  },
252 		{ 667  },
253 		{ 944  },
254 		{ 722  },
255 		{ 778  },
256 		{ 611  },
257 		{ 778  },
258 		{ 722  },
259 		{ 556  },
260 		{ 667  },
261 		{ 722  },
262 		{ 722  },
263 		{ 1000 },
264 		{ 722  },
265 		{ 722  },
266 		{ 667  },
267 		{ 333  },
268 		{ 278  },
269 		{ 333  },
270 		{ 581  },
271 		{ 500  },
272 		{ 333  },
273 		{ 500  },
274 		{ 556  },
275 		{ 444  },
276 		{  556 },
277 		{  444 },
278 		{  333 },
279 		{  500 },
280 		{  556 },
281 		{  278 },
282 		{  333 },
283 		{  556 },
284 		{  278 },
285 		{  833 },
286 		{  556 },
287 		{  500 },
288 		{  556 },
289 		{  556 },
290 		{  444 },
291 		{  389 },
292 		{  333 },
293 		{  556 },
294 		{  500 },
295 		{  722 },
296 		{  500 },
297 		{  500 },
298 		{  444 },
299 		{  394 },
300 		{  220 },
301 		{  394 },
302 		{  520 },
303 	} },
304 	{ "Times-Italic", {
305 		{ 250  },
306 		{ 333  },
307 		{ 420  },
308 		{ 500  },
309 		{ 500  },
310 		{ 833  },
311 		{ 778  },
312 		{ 333  },
313 		{ 333  },
314 		{ 333  },
315 		{ 500  },
316 		{ 675  },
317 		{ 250  },
318 		{ 333  },
319 		{ 250  },
320 		{ 278  },
321 		{ 500  },
322 		{ 500  },
323 		{ 500  },
324 		{ 500  },
325 		{ 500  },
326 		{ 500  },
327 		{ 500  },
328 		{ 500  },
329 		{ 500  },
330 		{ 500  },
331 		{ 333  },
332 		{ 333  },
333 		{ 675  },
334 		{ 675  },
335 		{ 675  },
336 		{ 500  },
337 		{ 920  },
338 		{ 611  },
339 		{ 611  },
340 		{ 667  },
341 		{ 722  },
342 		{ 611  },
343 		{ 611  },
344 		{ 722  },
345 		{ 722  },
346 		{ 333  },
347 		{ 444  },
348 		{ 667  },
349 		{ 556  },
350 		{ 833  },
351 		{ 667  },
352 		{ 722  },
353 		{ 611  },
354 		{ 722  },
355 		{ 611  },
356 		{ 500  },
357 		{ 556  },
358 		{ 722  },
359 		{ 611  },
360 		{ 833  },
361 		{ 611  },
362 		{ 556  },
363 		{ 556  },
364 		{ 389  },
365 		{ 278  },
366 		{ 389  },
367 		{ 422  },
368 		{ 500  },
369 		{ 333  },
370 		{ 500  },
371 		{ 500  },
372 		{ 444  },
373 		{  500 },
374 		{  444 },
375 		{  278 },
376 		{  500 },
377 		{  500 },
378 		{  278 },
379 		{  278 },
380 		{  444 },
381 		{  278 },
382 		{  722 },
383 		{  500 },
384 		{  500 },
385 		{  500 },
386 		{  500 },
387 		{  389 },
388 		{  389 },
389 		{  278 },
390 		{  500 },
391 		{  444 },
392 		{  667 },
393 		{  444 },
394 		{  444 },
395 		{  389 },
396 		{  400 },
397 		{  275 },
398 		{  400 },
399 		{  541 },
400 	} },
401 };
402 
403 void *
404 pdf_alloc(char *outopts)
405 {
406 	struct termp	*p;
407 
408 	if (NULL != (p = pspdf_alloc(outopts)))
409 		p->type = TERMTYPE_PDF;
410 
411 	return(p);
412 }
413 
414 void *
415 ps_alloc(char *outopts)
416 {
417 	struct termp	*p;
418 
419 	if (NULL != (p = pspdf_alloc(outopts)))
420 		p->type = TERMTYPE_PS;
421 
422 	return(p);
423 }
424 
425 static struct termp *
426 pspdf_alloc(char *outopts)
427 {
428 	struct termp	*p;
429 	unsigned int	 pagex, pagey;
430 	size_t		 marginx, marginy, lineheight;
431 	const char	*toks[2];
432 	const char	*pp;
433 	char		*v;
434 
435 	p = mandoc_calloc(1, sizeof(struct termp));
436 	p->enc = TERMENC_ASCII;
437 	p->ps = mandoc_calloc(1, sizeof(struct termp_ps));
438 
439 	p->advance = ps_advance;
440 	p->begin = ps_begin;
441 	p->end = ps_end;
442 	p->endline = ps_endline;
443 	p->hspan = ps_hspan;
444 	p->letter = ps_letter;
445 	p->width = ps_width;
446 
447 	toks[0] = "paper";
448 	toks[1] = NULL;
449 
450 	pp = NULL;
451 
452 	while (outopts && *outopts)
453 		switch (getsubopt(&outopts, UNCONST(toks), &v)) {
454 		case (0):
455 			pp = v;
456 			break;
457 		default:
458 			break;
459 		}
460 
461 	/* Default to US letter (millimetres). */
462 
463 	pagex = 216;
464 	pagey = 279;
465 
466 	/*
467 	 * The ISO-269 paper sizes can be calculated automatically, but
468 	 * it would require bringing in -lm for pow() and I'd rather not
469 	 * do that.  So just do it the easy way for now.  Since this
470 	 * only happens once, I'm not terribly concerned.
471 	 */
472 
473 	if (pp && strcasecmp(pp, "letter")) {
474 		if (0 == strcasecmp(pp, "a3")) {
475 			pagex = 297;
476 			pagey = 420;
477 		} else if (0 == strcasecmp(pp, "a4")) {
478 			pagex = 210;
479 			pagey = 297;
480 		} else if (0 == strcasecmp(pp, "a5")) {
481 			pagex = 148;
482 			pagey = 210;
483 		} else if (0 == strcasecmp(pp, "legal")) {
484 			pagex = 216;
485 			pagey = 356;
486 		} else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey))
487 			fprintf(stderr, "%s: Unknown paper\n", pp);
488 	}
489 
490 	/*
491 	 * This MUST be defined before any PNT2AFM or AFM2PNT
492 	 * calculations occur.
493 	 */
494 
495 	p->ps->scale = 11;
496 
497 	/* Remember millimetres -> AFM units. */
498 
499 	pagex = PNT2AFM(p, ((double)pagex * 2.834));
500 	pagey = PNT2AFM(p, ((double)pagey * 2.834));
501 
502 	/* Margins are 1/9 the page x and y. */
503 
504 	marginx = /* LINTED */
505 		(size_t)((double)pagex / 9.0);
506 	marginy = /* LINTED */
507 		(size_t)((double)pagey / 9.0);
508 
509 	/* Line-height is 1.4em. */
510 
511 	lineheight = PNT2AFM(p, ((double)p->ps->scale * 1.4));
512 
513 	p->ps->width = (size_t)pagex;
514 	p->ps->height = (size_t)pagey;
515 	p->ps->header = pagey - (marginy / 2) - (lineheight / 2);
516 	p->ps->top = pagey - marginy;
517 	p->ps->footer = (marginy / 2) - (lineheight / 2);
518 	p->ps->bottom = marginy;
519 	p->ps->left = marginx;
520 	p->ps->lineheight = lineheight;
521 
522 	p->defrmargin = pagex - (marginx * 2);
523 	return(p);
524 }
525 
526 
527 void
528 pspdf_free(void *arg)
529 {
530 	struct termp	*p;
531 
532 	p = (struct termp *)arg;
533 
534 	if (p->ps->psmarg)
535 		free(p->ps->psmarg);
536 	if (p->ps->pdfobjs)
537 		free(p->ps->pdfobjs);
538 
539 	free(p->ps);
540 	term_free(p);
541 }
542 
543 
544 static void
545 ps_printf(struct termp *p, const char *fmt, ...)
546 {
547 	va_list		 ap;
548 	int		 pos, len;
549 
550 	va_start(ap, fmt);
551 
552 	/*
553 	 * If we're running in regular mode, then pipe directly into
554 	 * vprintf().  If we're processing margins, then push the data
555 	 * into our growable margin buffer.
556 	 */
557 
558 	if ( ! (PS_MARGINS & p->ps->flags)) {
559 		len = vprintf(fmt, ap);
560 		va_end(ap);
561 		p->ps->pdfbytes += /* LINTED */
562 			len < 0 ? 0 : (size_t)len;
563 		return;
564 	}
565 
566 	/*
567 	 * XXX: I assume that the in-margin print won't exceed
568 	 * PS_BUFSLOP (128 bytes), which is reasonable but still an
569 	 * assumption that will cause pukeage if it's not the case.
570 	 */
571 
572 	ps_growbuf(p, PS_BUFSLOP);
573 
574 	pos = (int)p->ps->psmargcur;
575 	vsnprintf(&p->ps->psmarg[pos], PS_BUFSLOP, fmt, ap);
576 
577 	va_end(ap);
578 
579 	p->ps->psmargcur = strlen(p->ps->psmarg);
580 }
581 
582 
583 static void
584 ps_putchar(struct termp *p, char c)
585 {
586 	int		 pos;
587 
588 	/* See ps_printf(). */
589 
590 	if ( ! (PS_MARGINS & p->ps->flags)) {
591 		/* LINTED */
592 		putchar(c);
593 		p->ps->pdfbytes++;
594 		return;
595 	}
596 
597 	ps_growbuf(p, 2);
598 
599 	pos = (int)p->ps->psmargcur++;
600 	p->ps->psmarg[pos++] = c;
601 	p->ps->psmarg[pos] = '\0';
602 }
603 
604 
605 static void
606 pdf_obj(struct termp *p, size_t obj)
607 {
608 
609 	assert(obj > 0);
610 
611 	if ((obj - 1) >= p->ps->pdfobjsz) {
612 		p->ps->pdfobjsz = obj + 128;
613 		p->ps->pdfobjs = realloc
614 			(p->ps->pdfobjs,
615 			 p->ps->pdfobjsz * sizeof(size_t));
616 		if (NULL == p->ps->pdfobjs) {
617 			perror(NULL);
618 			exit((int)MANDOCLEVEL_SYSERR);
619 		}
620 	}
621 
622 	p->ps->pdfobjs[(int)obj - 1] = p->ps->pdfbytes;
623 	ps_printf(p, "%zu 0 obj\n", obj);
624 }
625 
626 
627 static void
628 ps_closepage(struct termp *p)
629 {
630 	int		 i;
631 	size_t		 len, base;
632 
633 	/*
634 	 * Close out a page that we've already flushed to output.  In
635 	 * PostScript, we simply note that the page must be showed.  In
636 	 * PDF, we must now create the Length, Resource, and Page node
637 	 * for the page contents.
638 	 */
639 
640 	assert(p->ps->psmarg && p->ps->psmarg[0]);
641 	ps_printf(p, "%s", p->ps->psmarg);
642 
643 	if (TERMTYPE_PS != p->type) {
644 		ps_printf(p, "ET\n");
645 
646 		len = p->ps->pdfbytes - p->ps->pdflastpg;
647 		base = p->ps->pages * 4 + p->ps->pdfbody;
648 
649 		ps_printf(p, "endstream\nendobj\n");
650 
651 		/* Length of content. */
652 		pdf_obj(p, base + 1);
653 		ps_printf(p, "%zu\nendobj\n", len);
654 
655 		/* Resource for content. */
656 		pdf_obj(p, base + 2);
657 		ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
658 		ps_printf(p, "/Font <<\n");
659 		for (i = 0; i < (int)TERMFONT__MAX; i++)
660 			ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
661 		ps_printf(p, ">>\n>>\n");
662 
663 		/* Page node. */
664 		pdf_obj(p, base + 3);
665 		ps_printf(p, "<<\n");
666 		ps_printf(p, "/Type /Page\n");
667 		ps_printf(p, "/Parent 2 0 R\n");
668 		ps_printf(p, "/Resources %zu 0 R\n", base + 2);
669 		ps_printf(p, "/Contents %zu 0 R\n", base);
670 		ps_printf(p, ">>\nendobj\n");
671 	} else
672 		ps_printf(p, "showpage\n");
673 
674 	p->ps->pages++;
675 	p->ps->psrow = p->ps->top;
676 	assert( ! (PS_NEWPAGE & p->ps->flags));
677 	p->ps->flags |= PS_NEWPAGE;
678 }
679 
680 
681 /* ARGSUSED */
682 static void
683 ps_end(struct termp *p)
684 {
685 	size_t		 i, xref, base;
686 
687 	/*
688 	 * At the end of the file, do one last showpage.  This is the
689 	 * same behaviour as groff(1) and works for multiple pages as
690 	 * well as just one.
691 	 */
692 
693 	if ( ! (PS_NEWPAGE & p->ps->flags)) {
694 		assert(0 == p->ps->flags);
695 		assert('\0' == p->ps->last);
696 		ps_closepage(p);
697 	}
698 
699 	if (TERMTYPE_PS == p->type) {
700 		ps_printf(p, "%%%%Trailer\n");
701 		ps_printf(p, "%%%%Pages: %zu\n", p->ps->pages);
702 		ps_printf(p, "%%%%EOF\n");
703 		return;
704 	}
705 
706 	pdf_obj(p, 2);
707 	ps_printf(p, "<<\n/Type /Pages\n");
708 	ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
709 			(size_t)AFM2PNT(p, p->ps->width),
710 			(size_t)AFM2PNT(p, p->ps->height));
711 
712 	ps_printf(p, "/Count %zu\n", p->ps->pages);
713 	ps_printf(p, "/Kids [");
714 
715 	for (i = 0; i < p->ps->pages; i++)
716 		ps_printf(p, " %zu 0 R", i * 4 +
717 				p->ps->pdfbody + 3);
718 
719 	base = (p->ps->pages - 1) * 4 +
720 		p->ps->pdfbody + 4;
721 
722 	ps_printf(p, "]\n>>\nendobj\n");
723 	pdf_obj(p, base);
724 	ps_printf(p, "<<\n");
725 	ps_printf(p, "/Type /Catalog\n");
726 	ps_printf(p, "/Pages 2 0 R\n");
727 	ps_printf(p, ">>\n");
728 	xref = p->ps->pdfbytes;
729 	ps_printf(p, "xref\n");
730 	ps_printf(p, "0 %zu\n", base + 1);
731 	ps_printf(p, "0000000000 65535 f \n");
732 
733 	for (i = 0; i < base; i++)
734 		ps_printf(p, "%.10zu 00000 n \n",
735 				p->ps->pdfobjs[(int)i]);
736 
737 	ps_printf(p, "trailer\n");
738 	ps_printf(p, "<<\n");
739 	ps_printf(p, "/Size %zu\n", base + 1);
740 	ps_printf(p, "/Root %zu 0 R\n", base);
741 	ps_printf(p, "/Info 1 0 R\n");
742 	ps_printf(p, ">>\n");
743 	ps_printf(p, "startxref\n");
744 	ps_printf(p, "%zu\n", xref);
745 	ps_printf(p, "%%%%EOF\n");
746 }
747 
748 
749 static void
750 ps_begin(struct termp *p)
751 {
752 	time_t		 t;
753 	int		 i;
754 
755 	/*
756 	 * Print margins into margin buffer.  Nothing gets output to the
757 	 * screen yet, so we don't need to initialise the primary state.
758 	 */
759 
760 	if (p->ps->psmarg) {
761 		assert(p->ps->psmargsz);
762 		p->ps->psmarg[0] = '\0';
763 	}
764 
765 	/*p->ps->pdfbytes = 0;*/
766 	p->ps->psmargcur = 0;
767 	p->ps->flags = PS_MARGINS;
768 	p->ps->pscol = p->ps->left;
769 	p->ps->psrow = p->ps->header;
770 
771 	ps_setfont(p, TERMFONT_NONE);
772 
773 	(*p->headf)(p, p->argf);
774 	(*p->endline)(p);
775 
776 	p->ps->pscol = p->ps->left;
777 	p->ps->psrow = p->ps->footer;
778 
779 	(*p->footf)(p, p->argf);
780 	(*p->endline)(p);
781 
782 	p->ps->flags &= ~PS_MARGINS;
783 
784 	assert(0 == p->ps->flags);
785 	assert(p->ps->psmarg);
786 	assert('\0' != p->ps->psmarg[0]);
787 
788 	/*
789 	 * Print header and initialise page state.  Following this,
790 	 * stuff gets printed to the screen, so make sure we're sane.
791 	 */
792 
793 	t = time(NULL);
794 
795 	if (TERMTYPE_PS == p->type) {
796 		ps_printf(p, "%%!PS-Adobe-3.0\n");
797 		ps_printf(p, "%%%%CreationDate: %s", ctime(&t));
798 		ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
799 		ps_printf(p, "%%%%Orientation: Portrait\n");
800 		ps_printf(p, "%%%%Pages: (atend)\n");
801 		ps_printf(p, "%%%%PageOrder: Ascend\n");
802 		ps_printf(p, "%%%%DocumentMedia: "
803 				"Default %zu %zu 0 () ()\n",
804 				(size_t)AFM2PNT(p, p->ps->width),
805 				(size_t)AFM2PNT(p, p->ps->height));
806 		ps_printf(p, "%%%%DocumentNeededResources: font");
807 
808 		for (i = 0; i < (int)TERMFONT__MAX; i++)
809 			ps_printf(p, " %s", fonts[i].name);
810 
811 		ps_printf(p, "\n%%%%EndComments\n");
812 	} else {
813 		ps_printf(p, "%%PDF-1.1\n");
814 		pdf_obj(p, 1);
815 		ps_printf(p, "<<\n");
816 		ps_printf(p, ">>\n");
817 		ps_printf(p, "endobj\n");
818 
819 		for (i = 0; i < (int)TERMFONT__MAX; i++) {
820 			pdf_obj(p, (size_t)i + 3);
821 			ps_printf(p, "<<\n");
822 			ps_printf(p, "/Type /Font\n");
823 			ps_printf(p, "/Subtype /Type1\n");
824 			ps_printf(p, "/Name /F%d\n", i);
825 			ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
826 			ps_printf(p, ">>\n");
827 		}
828 	}
829 
830 	p->ps->pdfbody = (size_t)TERMFONT__MAX + 3;
831 	p->ps->pscol = p->ps->left;
832 	p->ps->psrow = p->ps->top;
833 	p->ps->flags |= PS_NEWPAGE;
834 	ps_setfont(p, TERMFONT_NONE);
835 }
836 
837 
838 static void
839 ps_pletter(struct termp *p, int c)
840 {
841 	int		 f;
842 
843 	/*
844 	 * If we haven't opened a page context, then output that we're
845 	 * in a new page and make sure the font is correctly set.
846 	 */
847 
848 	if (PS_NEWPAGE & p->ps->flags) {
849 		if (TERMTYPE_PS == p->type) {
850 			ps_printf(p, "%%%%Page: %zu %zu\n",
851 					p->ps->pages + 1,
852 					p->ps->pages + 1);
853 			ps_printf(p, "/%s %zu selectfont\n",
854 					fonts[(int)p->ps->lastf].name,
855 					p->ps->scale);
856 		} else {
857 			pdf_obj(p, p->ps->pdfbody +
858 					p->ps->pages * 4);
859 			ps_printf(p, "<<\n");
860 			ps_printf(p, "/Length %zu 0 R\n",
861 					p->ps->pdfbody + 1 +
862 					p->ps->pages * 4);
863 			ps_printf(p, ">>\nstream\n");
864 		}
865 		p->ps->pdflastpg = p->ps->pdfbytes;
866 		p->ps->flags &= ~PS_NEWPAGE;
867 	}
868 
869 	/*
870 	 * If we're not in a PostScript "word" context, then open one
871 	 * now at the current cursor.
872 	 */
873 
874 	if ( ! (PS_INLINE & p->ps->flags)) {
875 		if (TERMTYPE_PS != p->type) {
876 			ps_printf(p, "BT\n/F%d %zu Tf\n",
877 					(int)p->ps->lastf,
878 					p->ps->scale);
879 			ps_printf(p, "%.3f %.3f Td\n(",
880 					AFM2PNT(p, p->ps->pscol),
881 					AFM2PNT(p, p->ps->psrow));
882 		} else
883 			ps_printf(p, "%.3f %.3f moveto\n(",
884 					AFM2PNT(p, p->ps->pscol),
885 					AFM2PNT(p, p->ps->psrow));
886 		p->ps->flags |= PS_INLINE;
887 	}
888 
889 	assert( ! (PS_NEWPAGE & p->ps->flags));
890 
891 	/*
892 	 * We need to escape these characters as per the PostScript
893 	 * specification.  We would also escape non-graphable characters
894 	 * (like tabs), but none of them would get to this point and
895 	 * it's superfluous to abort() on them.
896 	 */
897 
898 	switch (c) {
899 	case ('('):
900 		/* FALLTHROUGH */
901 	case (')'):
902 		/* FALLTHROUGH */
903 	case ('\\'):
904 		ps_putchar(p, '\\');
905 		break;
906 	default:
907 		break;
908 	}
909 
910 	/* Write the character and adjust where we are on the page. */
911 
912 	f = (int)p->ps->lastf;
913 
914 	if (c <= 32 || (c - 32 >= MAXCHAR)) {
915 		ps_putchar(p, ' ');
916 		p->ps->pscol += (size_t)fonts[f].gly[0].wx;
917 		return;
918 	}
919 
920 	ps_putchar(p, (char)c);
921 	c -= 32;
922 	p->ps->pscol += (size_t)fonts[f].gly[c].wx;
923 }
924 
925 
926 static void
927 ps_pclose(struct termp *p)
928 {
929 
930 	/*
931 	 * Spit out that we're exiting a word context (this is a
932 	 * "partial close" because we don't check the last-char buffer
933 	 * or anything).
934 	 */
935 
936 	if ( ! (PS_INLINE & p->ps->flags))
937 		return;
938 
939 	if (TERMTYPE_PS != p->type) {
940 		ps_printf(p, ") Tj\nET\n");
941 	} else
942 		ps_printf(p, ") show\n");
943 
944 	p->ps->flags &= ~PS_INLINE;
945 }
946 
947 
948 static void
949 ps_fclose(struct termp *p)
950 {
951 
952 	/*
953 	 * Strong closure: if we have a last-char, spit it out after
954 	 * checking that we're in the right font mode.  This will of
955 	 * course open a new scope, if applicable.
956 	 *
957 	 * Following this, close out any scope that's open.
958 	 */
959 
960 	if ('\0' != p->ps->last) {
961 		if (p->ps->lastf != TERMFONT_NONE) {
962 			ps_pclose(p);
963 			ps_setfont(p, TERMFONT_NONE);
964 		}
965 		ps_pletter(p, p->ps->last);
966 		p->ps->last = '\0';
967 	}
968 
969 	if ( ! (PS_INLINE & p->ps->flags))
970 		return;
971 
972 	ps_pclose(p);
973 }
974 
975 
976 static void
977 ps_letter(struct termp *p, int arg)
978 {
979 	char		cc, c;
980 
981 	/* LINTED */
982 	c = arg >= 128 || arg <= 0 ? '?' : arg;
983 
984 	/*
985 	 * State machine dictates whether to buffer the last character
986 	 * or not.  Basically, encoded words are detected by checking if
987 	 * we're an "8" and switching on the buffer.  Then we put "8" in
988 	 * our buffer, and on the next charater, flush both character
989 	 * and buffer.  Thus, "regular" words are detected by having a
990 	 * regular character and a regular buffer character.
991 	 */
992 
993 	if ('\0' == p->ps->last) {
994 		assert(8 != c);
995 		p->ps->last = c;
996 		return;
997 	} else if (8 == p->ps->last) {
998 		assert(8 != c);
999 		p->ps->last = '\0';
1000 	} else if (8 == c) {
1001 		assert(8 != p->ps->last);
1002 		if ('_' == p->ps->last) {
1003 			if (p->ps->lastf != TERMFONT_UNDER) {
1004 				ps_pclose(p);
1005 				ps_setfont(p, TERMFONT_UNDER);
1006 			}
1007 		} else if (p->ps->lastf != TERMFONT_BOLD) {
1008 			ps_pclose(p);
1009 			ps_setfont(p, TERMFONT_BOLD);
1010 		}
1011 		p->ps->last = c;
1012 		return;
1013 	} else {
1014 		if (p->ps->lastf != TERMFONT_NONE) {
1015 			ps_pclose(p);
1016 			ps_setfont(p, TERMFONT_NONE);
1017 		}
1018 		cc = p->ps->last;
1019 		p->ps->last = c;
1020 		c = cc;
1021 	}
1022 
1023 	ps_pletter(p, c);
1024 }
1025 
1026 
1027 static void
1028 ps_advance(struct termp *p, size_t len)
1029 {
1030 
1031 	/*
1032 	 * Advance some spaces.  This can probably be made smarter,
1033 	 * i.e., to have multiple space-separated words in the same
1034 	 * scope, but this is easier:  just close out the current scope
1035 	 * and readjust our column settings.
1036 	 */
1037 
1038 	ps_fclose(p);
1039 	p->ps->pscol += len;
1040 }
1041 
1042 
1043 static void
1044 ps_endline(struct termp *p)
1045 {
1046 
1047 	/* Close out any scopes we have open: we're at eoln. */
1048 
1049 	ps_fclose(p);
1050 
1051 	/*
1052 	 * If we're in the margin, don't try to recalculate our current
1053 	 * row.  XXX: if the column tries to be fancy with multiple
1054 	 * lines, we'll do nasty stuff.
1055 	 */
1056 
1057 	if (PS_MARGINS & p->ps->flags)
1058 		return;
1059 
1060 	/* Left-justify. */
1061 
1062 	p->ps->pscol = p->ps->left;
1063 
1064 	/* If we haven't printed anything, return. */
1065 
1066 	if (PS_NEWPAGE & p->ps->flags)
1067 		return;
1068 
1069 	/*
1070 	 * Put us down a line.  If we're at the page bottom, spit out a
1071 	 * showpage and restart our row.
1072 	 */
1073 
1074 	if (p->ps->psrow >= p->ps->lineheight +
1075 			p->ps->bottom) {
1076 		p->ps->psrow -= p->ps->lineheight;
1077 		return;
1078 	}
1079 
1080 	ps_closepage(p);
1081 }
1082 
1083 
1084 static void
1085 ps_setfont(struct termp *p, enum termfont f)
1086 {
1087 
1088 	assert(f < TERMFONT__MAX);
1089 	p->ps->lastf = f;
1090 
1091 	/*
1092 	 * If we're still at the top of the page, let the font-setting
1093 	 * be delayed until we actually have stuff to print.
1094 	 */
1095 
1096 	if (PS_NEWPAGE & p->ps->flags)
1097 		return;
1098 
1099 	if (TERMTYPE_PS == p->type)
1100 		ps_printf(p, "/%s %zu selectfont\n",
1101 				fonts[(int)f].name,
1102 				p->ps->scale);
1103 	else
1104 		ps_printf(p, "/F%d %zu Tf\n",
1105 				(int)f,
1106 				p->ps->scale);
1107 }
1108 
1109 
1110 /* ARGSUSED */
1111 static size_t
1112 ps_width(const struct termp *p, int c)
1113 {
1114 
1115 	if (c <= 32 || c - 32 >= MAXCHAR)
1116 		return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1117 
1118 	c -= 32;
1119 	return((size_t)fonts[(int)TERMFONT_NONE].gly[c].wx);
1120 }
1121 
1122 
1123 static double
1124 ps_hspan(const struct termp *p, const struct roffsu *su)
1125 {
1126 	double		 r;
1127 
1128 	/*
1129 	 * All of these measurements are derived by converting from the
1130 	 * native measurement to AFM units.
1131 	 */
1132 
1133 	switch (su->unit) {
1134 	case (SCALE_CM):
1135 		r = PNT2AFM(p, su->scale * 28.34);
1136 		break;
1137 	case (SCALE_IN):
1138 		r = PNT2AFM(p, su->scale * 72);
1139 		break;
1140 	case (SCALE_PC):
1141 		r = PNT2AFM(p, su->scale * 12);
1142 		break;
1143 	case (SCALE_PT):
1144 		r = PNT2AFM(p, su->scale * 100);
1145 		break;
1146 	case (SCALE_EM):
1147 		r = su->scale *
1148 			fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
1149 		break;
1150 	case (SCALE_MM):
1151 		r = PNT2AFM(p, su->scale * 2.834);
1152 		break;
1153 	case (SCALE_EN):
1154 		r = su->scale *
1155 			fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
1156 		break;
1157 	case (SCALE_VS):
1158 		r = su->scale * p->ps->lineheight;
1159 		break;
1160 	default:
1161 		r = su->scale;
1162 		break;
1163 	}
1164 
1165 	return(r);
1166 }
1167 
1168 static void
1169 ps_growbuf(struct termp *p, size_t sz)
1170 {
1171 	if (p->ps->psmargcur + sz <= p->ps->psmargsz)
1172 		return;
1173 
1174 	if (sz < PS_BUFSLOP)
1175 		sz = PS_BUFSLOP;
1176 
1177 	p->ps->psmargsz += sz;
1178 
1179 	p->ps->psmarg = mandoc_realloc
1180 		(p->ps->psmarg, p->ps->psmargsz);
1181 }
1182 
1183