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