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