xref: /netbsd-src/external/bsd/mdocml/dist/term_ps.c (revision c9bcef0391a5afc0280459d5c16c26ace267c496)
1*c9bcef03Schristos /*	Id: term_ps.c,v 1.91 2017/11/10 23:42:52 schwarze Exp  */
26c26a9aaSjoerg /*
3c5f73b34Sjoerg  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*c9bcef03Schristos  * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze <schwarze@openbsd.org>
5*c9bcef03Schristos  * Copyright (c) 2017 Marc Espie <espie@openbsd.org>
66c26a9aaSjoerg  *
76c26a9aaSjoerg  * Permission to use, copy, modify, and distribute this software for any
86c26a9aaSjoerg  * purpose with or without fee is hereby granted, provided that the above
96c26a9aaSjoerg  * copyright notice and this permission notice appear in all copies.
106c26a9aaSjoerg  *
119ff1f2acSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
126c26a9aaSjoerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
139ff1f2acSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
146c26a9aaSjoerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
156c26a9aaSjoerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
166c26a9aaSjoerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
176c26a9aaSjoerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
186c26a9aaSjoerg  */
196c26a9aaSjoerg #include "config.h"
206c26a9aaSjoerg 
21c0d9444aSjoerg #include <sys/types.h>
226c26a9aaSjoerg 
236c26a9aaSjoerg #include <assert.h>
249ff1f2acSchristos #if HAVE_ERR
259ff1f2acSchristos #include <err.h>
269ff1f2acSchristos #endif
276c26a9aaSjoerg #include <stdarg.h>
2882361f10Sjoerg #include <stdint.h>
296c26a9aaSjoerg #include <stdio.h>
306c26a9aaSjoerg #include <stdlib.h>
316c26a9aaSjoerg #include <string.h>
3282361f10Sjoerg #include <unistd.h>
336c26a9aaSjoerg 
34fec65c98Schristos #include "mandoc_aux.h"
356c26a9aaSjoerg #include "out.h"
366c26a9aaSjoerg #include "term.h"
379ff1f2acSchristos #include "manconf.h"
38fec65c98Schristos #include "main.h"
396c26a9aaSjoerg 
40c5f73b34Sjoerg /* These work the buffer used by the header and footer. */
41c5f73b34Sjoerg #define	PS_BUFSLOP	  128
42c5f73b34Sjoerg 
4382361f10Sjoerg /* Convert PostScript point "x" to an AFM unit. */
44fec65c98Schristos #define	PNT2AFM(p, x) \
45c5f73b34Sjoerg 	(size_t)((double)(x) * (1000.0 / (double)(p)->ps->scale))
466c26a9aaSjoerg 
4782361f10Sjoerg /* Convert an AFM unit "x" to a PostScript points */
48fec65c98Schristos #define	AFM2PNT(p, x) \
49c5f73b34Sjoerg 	((double)(x) / (1000.0 / (double)(p)->ps->scale))
5082361f10Sjoerg 
5182361f10Sjoerg struct	glyph {
527da9b934Sjoerg 	unsigned short	  wx; /* WX in AFM */
5382361f10Sjoerg };
5482361f10Sjoerg 
5582361f10Sjoerg struct	font {
5682361f10Sjoerg 	const char	 *name; /* FontName in AFM */
5782361f10Sjoerg #define	MAXCHAR		  95 /* total characters we can handle */
5882361f10Sjoerg 	struct glyph	  gly[MAXCHAR]; /* glyph metrics */
5982361f10Sjoerg };
6082361f10Sjoerg 
61c5f73b34Sjoerg struct	termp_ps {
62c5f73b34Sjoerg 	int		  flags;
63c5f73b34Sjoerg #define	PS_INLINE	 (1 << 0)	/* we're in a word */
64c5f73b34Sjoerg #define	PS_MARGINS	 (1 << 1)	/* we're in the margins */
65c5f73b34Sjoerg #define	PS_NEWPAGE	 (1 << 2)	/* new page, no words yet */
66fec65c98Schristos #define	PS_BACKSP	 (1 << 3)	/* last character was backspace */
67c5f73b34Sjoerg 	size_t		  pscol;	/* visible column (AFM units) */
68fec65c98Schristos 	size_t		  pscolnext;	/* used for overstrike */
69c5f73b34Sjoerg 	size_t		  psrow;	/* visible row (AFM units) */
70*c9bcef03Schristos 	size_t		  lastrow;	/* psrow of the previous word */
71c5f73b34Sjoerg 	char		 *psmarg;	/* margin buf */
72c5f73b34Sjoerg 	size_t		  psmargsz;	/* margin buf size */
73c5f73b34Sjoerg 	size_t		  psmargcur;	/* cur index in margin buf */
74fec65c98Schristos 	char		  last;		/* last non-backspace seen */
75c5f73b34Sjoerg 	enum termfont	  lastf;	/* last set font */
76fec65c98Schristos 	enum termfont	  nextf;	/* building next font here */
77c5f73b34Sjoerg 	size_t		  scale;	/* font scaling factor */
78c5f73b34Sjoerg 	size_t		  pages;	/* number of pages shown */
79c5f73b34Sjoerg 	size_t		  lineheight;	/* line height (AFM units) */
80c5f73b34Sjoerg 	size_t		  top;		/* body top (AFM units) */
81c5f73b34Sjoerg 	size_t		  bottom;	/* body bottom (AFM units) */
82*c9bcef03Schristos 	const char	 *medianame;	/* for DocumentMedia and PageSize */
83c5f73b34Sjoerg 	size_t		  height;	/* page height (AFM units */
84c5f73b34Sjoerg 	size_t		  width;	/* page width (AFM units) */
85fec65c98Schristos 	size_t		  lastwidth;	/* page width before last ll */
86c5f73b34Sjoerg 	size_t		  left;		/* body left (AFM units) */
87c5f73b34Sjoerg 	size_t		  header;	/* header pos (AFM units) */
88c5f73b34Sjoerg 	size_t		  footer;	/* footer pos (AFM units) */
89c5f73b34Sjoerg 	size_t		  pdfbytes;	/* current output byte */
90c5f73b34Sjoerg 	size_t		  pdflastpg;	/* byte of last page mark */
91c5f73b34Sjoerg 	size_t		  pdfbody;	/* start of body object */
92c5f73b34Sjoerg 	size_t		 *pdfobjs;	/* table of object offsets */
93c5f73b34Sjoerg 	size_t		  pdfobjsz;	/* size of pdfobjs */
94c5f73b34Sjoerg };
95c5f73b34Sjoerg 
969ff1f2acSchristos static	int		  ps_hspan(const struct termp *,
97c5f73b34Sjoerg 				const struct roffsu *);
98c5f73b34Sjoerg static	size_t		  ps_width(const struct termp *, int);
99c5f73b34Sjoerg static	void		  ps_advance(struct termp *, size_t);
100c5f73b34Sjoerg static	void		  ps_begin(struct termp *);
101c5f73b34Sjoerg static	void		  ps_closepage(struct termp *);
102c5f73b34Sjoerg static	void		  ps_end(struct termp *);
103c5f73b34Sjoerg static	void		  ps_endline(struct termp *);
104c5f73b34Sjoerg static	void		  ps_growbuf(struct termp *, size_t);
105c5f73b34Sjoerg static	void		  ps_letter(struct termp *, int);
106c5f73b34Sjoerg static	void		  ps_pclose(struct termp *);
1079508192eSchristos static	void		  ps_plast(struct termp *);
108c5f73b34Sjoerg static	void		  ps_pletter(struct termp *, int);
1099508192eSchristos static	void		  ps_printf(struct termp *, const char *, ...)
1109508192eSchristos 				__attribute__((__format__ (__printf__, 2, 3)));
111c5f73b34Sjoerg static	void		  ps_putchar(struct termp *, char);
112c5f73b34Sjoerg static	void		  ps_setfont(struct termp *, enum termfont);
1139ff1f2acSchristos static	void		  ps_setwidth(struct termp *, int, int);
114*c9bcef03Schristos static	struct termp	 *pspdf_alloc(const struct manoutput *, enum termtype);
115c5f73b34Sjoerg static	void		  pdf_obj(struct termp *, size_t);
116c5f73b34Sjoerg 
11782361f10Sjoerg /*
11882361f10Sjoerg  * We define, for the time being, three fonts: bold, oblique/italic, and
11982361f10Sjoerg  * normal (roman).  The following table hard-codes the font metrics for
12082361f10Sjoerg  * ASCII, i.e., 32--127.
12182361f10Sjoerg  */
12282361f10Sjoerg 
12382361f10Sjoerg static	const struct font fonts[TERMFONT__MAX] = {
12482361f10Sjoerg 	{ "Times-Roman", {
12582361f10Sjoerg 		{ 250 },
12682361f10Sjoerg 		{ 333 },
12782361f10Sjoerg 		{ 408 },
12882361f10Sjoerg 		{ 500 },
12982361f10Sjoerg 		{ 500 },
13082361f10Sjoerg 		{ 833 },
13182361f10Sjoerg 		{ 778 },
13282361f10Sjoerg 		{ 333 },
13382361f10Sjoerg 		{ 333 },
13482361f10Sjoerg 		{ 333 },
13582361f10Sjoerg 		{ 500 },
13682361f10Sjoerg 		{ 564 },
13782361f10Sjoerg 		{ 250 },
13882361f10Sjoerg 		{ 333 },
13982361f10Sjoerg 		{ 250 },
14082361f10Sjoerg 		{ 278 },
14182361f10Sjoerg 		{ 500 },
14282361f10Sjoerg 		{ 500 },
14382361f10Sjoerg 		{ 500 },
14482361f10Sjoerg 		{ 500 },
14582361f10Sjoerg 		{ 500 },
14682361f10Sjoerg 		{ 500 },
14782361f10Sjoerg 		{ 500 },
14882361f10Sjoerg 		{ 500 },
14982361f10Sjoerg 		{ 500 },
15082361f10Sjoerg 		{ 500 },
15182361f10Sjoerg 		{ 278 },
15282361f10Sjoerg 		{ 278 },
15382361f10Sjoerg 		{ 564 },
15482361f10Sjoerg 		{ 564 },
15582361f10Sjoerg 		{ 564 },
15682361f10Sjoerg 		{ 444 },
15782361f10Sjoerg 		{ 921 },
15882361f10Sjoerg 		{ 722 },
15982361f10Sjoerg 		{ 667 },
16082361f10Sjoerg 		{ 667 },
16182361f10Sjoerg 		{ 722 },
16282361f10Sjoerg 		{ 611 },
16382361f10Sjoerg 		{ 556 },
16482361f10Sjoerg 		{ 722 },
16582361f10Sjoerg 		{ 722 },
16682361f10Sjoerg 		{ 333 },
16782361f10Sjoerg 		{ 389 },
16882361f10Sjoerg 		{ 722 },
16982361f10Sjoerg 		{ 611 },
17082361f10Sjoerg 		{ 889 },
17182361f10Sjoerg 		{ 722 },
17282361f10Sjoerg 		{ 722 },
17382361f10Sjoerg 		{ 556 },
17482361f10Sjoerg 		{ 722 },
17582361f10Sjoerg 		{ 667 },
17682361f10Sjoerg 		{ 556 },
17782361f10Sjoerg 		{ 611 },
17882361f10Sjoerg 		{ 722 },
17982361f10Sjoerg 		{ 722 },
18082361f10Sjoerg 		{ 944 },
18182361f10Sjoerg 		{ 722 },
18282361f10Sjoerg 		{ 722 },
18382361f10Sjoerg 		{ 611 },
18482361f10Sjoerg 		{ 333 },
18582361f10Sjoerg 		{ 278 },
18682361f10Sjoerg 		{ 333 },
18782361f10Sjoerg 		{ 469 },
18882361f10Sjoerg 		{ 500 },
18982361f10Sjoerg 		{ 333 },
19082361f10Sjoerg 		{ 444 },
19182361f10Sjoerg 		{ 500 },
19282361f10Sjoerg 		{ 444 },
19382361f10Sjoerg 		{  500},
19482361f10Sjoerg 		{  444},
19582361f10Sjoerg 		{  333},
19682361f10Sjoerg 		{  500},
19782361f10Sjoerg 		{  500},
19882361f10Sjoerg 		{  278},
19982361f10Sjoerg 		{  278},
20082361f10Sjoerg 		{  500},
20182361f10Sjoerg 		{  278},
20282361f10Sjoerg 		{  778},
20382361f10Sjoerg 		{  500},
20482361f10Sjoerg 		{  500},
20582361f10Sjoerg 		{  500},
20682361f10Sjoerg 		{  500},
20782361f10Sjoerg 		{  333},
20882361f10Sjoerg 		{  389},
20982361f10Sjoerg 		{  278},
21082361f10Sjoerg 		{  500},
21182361f10Sjoerg 		{  500},
21282361f10Sjoerg 		{  722},
21382361f10Sjoerg 		{  500},
21482361f10Sjoerg 		{  500},
21582361f10Sjoerg 		{  444},
21682361f10Sjoerg 		{  480},
21782361f10Sjoerg 		{  200},
21882361f10Sjoerg 		{  480},
21982361f10Sjoerg 		{  541},
22082361f10Sjoerg 	} },
22182361f10Sjoerg 	{ "Times-Bold", {
22282361f10Sjoerg 		{ 250  },
22382361f10Sjoerg 		{ 333  },
22482361f10Sjoerg 		{ 555  },
22582361f10Sjoerg 		{ 500  },
22682361f10Sjoerg 		{ 500  },
22782361f10Sjoerg 		{ 1000 },
22882361f10Sjoerg 		{ 833  },
22982361f10Sjoerg 		{ 333  },
23082361f10Sjoerg 		{ 333  },
23182361f10Sjoerg 		{ 333  },
23282361f10Sjoerg 		{ 500  },
23382361f10Sjoerg 		{ 570  },
23482361f10Sjoerg 		{ 250  },
23582361f10Sjoerg 		{ 333  },
23682361f10Sjoerg 		{ 250  },
23782361f10Sjoerg 		{ 278  },
23882361f10Sjoerg 		{ 500  },
23982361f10Sjoerg 		{ 500  },
24082361f10Sjoerg 		{ 500  },
24182361f10Sjoerg 		{ 500  },
24282361f10Sjoerg 		{ 500  },
24382361f10Sjoerg 		{ 500  },
24482361f10Sjoerg 		{ 500  },
24582361f10Sjoerg 		{ 500  },
24682361f10Sjoerg 		{ 500  },
24782361f10Sjoerg 		{ 500  },
24882361f10Sjoerg 		{ 333  },
24982361f10Sjoerg 		{ 333  },
25082361f10Sjoerg 		{ 570  },
25182361f10Sjoerg 		{ 570  },
25282361f10Sjoerg 		{ 570  },
25382361f10Sjoerg 		{ 500  },
25482361f10Sjoerg 		{ 930  },
25582361f10Sjoerg 		{ 722  },
25682361f10Sjoerg 		{ 667  },
25782361f10Sjoerg 		{ 722  },
25882361f10Sjoerg 		{ 722  },
25982361f10Sjoerg 		{ 667  },
26082361f10Sjoerg 		{ 611  },
26182361f10Sjoerg 		{ 778  },
26282361f10Sjoerg 		{ 778  },
26382361f10Sjoerg 		{ 389  },
26482361f10Sjoerg 		{ 500  },
26582361f10Sjoerg 		{ 778  },
26682361f10Sjoerg 		{ 667  },
26782361f10Sjoerg 		{ 944  },
26882361f10Sjoerg 		{ 722  },
26982361f10Sjoerg 		{ 778  },
27082361f10Sjoerg 		{ 611  },
27182361f10Sjoerg 		{ 778  },
27282361f10Sjoerg 		{ 722  },
27382361f10Sjoerg 		{ 556  },
27482361f10Sjoerg 		{ 667  },
27582361f10Sjoerg 		{ 722  },
27682361f10Sjoerg 		{ 722  },
27782361f10Sjoerg 		{ 1000 },
27882361f10Sjoerg 		{ 722  },
27982361f10Sjoerg 		{ 722  },
28082361f10Sjoerg 		{ 667  },
28182361f10Sjoerg 		{ 333  },
28282361f10Sjoerg 		{ 278  },
28382361f10Sjoerg 		{ 333  },
28482361f10Sjoerg 		{ 581  },
28582361f10Sjoerg 		{ 500  },
28682361f10Sjoerg 		{ 333  },
28782361f10Sjoerg 		{ 500  },
28882361f10Sjoerg 		{ 556  },
28982361f10Sjoerg 		{ 444  },
29082361f10Sjoerg 		{  556 },
29182361f10Sjoerg 		{  444 },
29282361f10Sjoerg 		{  333 },
29382361f10Sjoerg 		{  500 },
29482361f10Sjoerg 		{  556 },
29582361f10Sjoerg 		{  278 },
29682361f10Sjoerg 		{  333 },
29782361f10Sjoerg 		{  556 },
29882361f10Sjoerg 		{  278 },
29982361f10Sjoerg 		{  833 },
30082361f10Sjoerg 		{  556 },
30182361f10Sjoerg 		{  500 },
30282361f10Sjoerg 		{  556 },
30382361f10Sjoerg 		{  556 },
30482361f10Sjoerg 		{  444 },
30582361f10Sjoerg 		{  389 },
30682361f10Sjoerg 		{  333 },
30782361f10Sjoerg 		{  556 },
30882361f10Sjoerg 		{  500 },
30982361f10Sjoerg 		{  722 },
31082361f10Sjoerg 		{  500 },
31182361f10Sjoerg 		{  500 },
31282361f10Sjoerg 		{  444 },
31382361f10Sjoerg 		{  394 },
31482361f10Sjoerg 		{  220 },
31582361f10Sjoerg 		{  394 },
31682361f10Sjoerg 		{  520 },
31782361f10Sjoerg 	} },
31882361f10Sjoerg 	{ "Times-Italic", {
31982361f10Sjoerg 		{ 250  },
32082361f10Sjoerg 		{ 333  },
32182361f10Sjoerg 		{ 420  },
32282361f10Sjoerg 		{ 500  },
32382361f10Sjoerg 		{ 500  },
32482361f10Sjoerg 		{ 833  },
32582361f10Sjoerg 		{ 778  },
32682361f10Sjoerg 		{ 333  },
32782361f10Sjoerg 		{ 333  },
32882361f10Sjoerg 		{ 333  },
32982361f10Sjoerg 		{ 500  },
33082361f10Sjoerg 		{ 675  },
33182361f10Sjoerg 		{ 250  },
33282361f10Sjoerg 		{ 333  },
33382361f10Sjoerg 		{ 250  },
33482361f10Sjoerg 		{ 278  },
33582361f10Sjoerg 		{ 500  },
33682361f10Sjoerg 		{ 500  },
33782361f10Sjoerg 		{ 500  },
33882361f10Sjoerg 		{ 500  },
33982361f10Sjoerg 		{ 500  },
34082361f10Sjoerg 		{ 500  },
34182361f10Sjoerg 		{ 500  },
34282361f10Sjoerg 		{ 500  },
34382361f10Sjoerg 		{ 500  },
34482361f10Sjoerg 		{ 500  },
34582361f10Sjoerg 		{ 333  },
34682361f10Sjoerg 		{ 333  },
34782361f10Sjoerg 		{ 675  },
34882361f10Sjoerg 		{ 675  },
34982361f10Sjoerg 		{ 675  },
35082361f10Sjoerg 		{ 500  },
35182361f10Sjoerg 		{ 920  },
35282361f10Sjoerg 		{ 611  },
35382361f10Sjoerg 		{ 611  },
35482361f10Sjoerg 		{ 667  },
35582361f10Sjoerg 		{ 722  },
35682361f10Sjoerg 		{ 611  },
35782361f10Sjoerg 		{ 611  },
35882361f10Sjoerg 		{ 722  },
35982361f10Sjoerg 		{ 722  },
36082361f10Sjoerg 		{ 333  },
36182361f10Sjoerg 		{ 444  },
36282361f10Sjoerg 		{ 667  },
36382361f10Sjoerg 		{ 556  },
36482361f10Sjoerg 		{ 833  },
36582361f10Sjoerg 		{ 667  },
36682361f10Sjoerg 		{ 722  },
36782361f10Sjoerg 		{ 611  },
36882361f10Sjoerg 		{ 722  },
36982361f10Sjoerg 		{ 611  },
37082361f10Sjoerg 		{ 500  },
37182361f10Sjoerg 		{ 556  },
37282361f10Sjoerg 		{ 722  },
37382361f10Sjoerg 		{ 611  },
37482361f10Sjoerg 		{ 833  },
37582361f10Sjoerg 		{ 611  },
37682361f10Sjoerg 		{ 556  },
37782361f10Sjoerg 		{ 556  },
37882361f10Sjoerg 		{ 389  },
37982361f10Sjoerg 		{ 278  },
38082361f10Sjoerg 		{ 389  },
38182361f10Sjoerg 		{ 422  },
38282361f10Sjoerg 		{ 500  },
38382361f10Sjoerg 		{ 333  },
38482361f10Sjoerg 		{ 500  },
38582361f10Sjoerg 		{ 500  },
38682361f10Sjoerg 		{ 444  },
38782361f10Sjoerg 		{  500 },
38882361f10Sjoerg 		{  444 },
38982361f10Sjoerg 		{  278 },
39082361f10Sjoerg 		{  500 },
39182361f10Sjoerg 		{  500 },
39282361f10Sjoerg 		{  278 },
39382361f10Sjoerg 		{  278 },
39482361f10Sjoerg 		{  444 },
39582361f10Sjoerg 		{  278 },
39682361f10Sjoerg 		{  722 },
39782361f10Sjoerg 		{  500 },
39882361f10Sjoerg 		{  500 },
39982361f10Sjoerg 		{  500 },
40082361f10Sjoerg 		{  500 },
40182361f10Sjoerg 		{  389 },
40282361f10Sjoerg 		{  389 },
40382361f10Sjoerg 		{  278 },
40482361f10Sjoerg 		{  500 },
40582361f10Sjoerg 		{  444 },
40682361f10Sjoerg 		{  667 },
40782361f10Sjoerg 		{  444 },
40882361f10Sjoerg 		{  444 },
40982361f10Sjoerg 		{  389 },
41082361f10Sjoerg 		{  400 },
41182361f10Sjoerg 		{  275 },
41282361f10Sjoerg 		{  400 },
41382361f10Sjoerg 		{  541 },
41482361f10Sjoerg 	} },
415fec65c98Schristos 	{ "Times-BoldItalic", {
416fec65c98Schristos 		{  250 },
417fec65c98Schristos 		{  389 },
418fec65c98Schristos 		{  555 },
419fec65c98Schristos 		{  500 },
420fec65c98Schristos 		{  500 },
421fec65c98Schristos 		{  833 },
422fec65c98Schristos 		{  778 },
423fec65c98Schristos 		{  333 },
424fec65c98Schristos 		{  333 },
425fec65c98Schristos 		{  333 },
426fec65c98Schristos 		{  500 },
427fec65c98Schristos 		{  570 },
428fec65c98Schristos 		{  250 },
429fec65c98Schristos 		{  333 },
430fec65c98Schristos 		{  250 },
431fec65c98Schristos 		{  278 },
432fec65c98Schristos 		{  500 },
433fec65c98Schristos 		{  500 },
434fec65c98Schristos 		{  500 },
435fec65c98Schristos 		{  500 },
436fec65c98Schristos 		{  500 },
437fec65c98Schristos 		{  500 },
438fec65c98Schristos 		{  500 },
439fec65c98Schristos 		{  500 },
440fec65c98Schristos 		{  500 },
441fec65c98Schristos 		{  500 },
442fec65c98Schristos 		{  333 },
443fec65c98Schristos 		{  333 },
444fec65c98Schristos 		{  570 },
445fec65c98Schristos 		{  570 },
446fec65c98Schristos 		{  570 },
447fec65c98Schristos 		{  500 },
448fec65c98Schristos 		{  832 },
449fec65c98Schristos 		{  667 },
450fec65c98Schristos 		{  667 },
451fec65c98Schristos 		{  667 },
452fec65c98Schristos 		{  722 },
453fec65c98Schristos 		{  667 },
454fec65c98Schristos 		{  667 },
455fec65c98Schristos 		{  722 },
456fec65c98Schristos 		{  778 },
457fec65c98Schristos 		{  389 },
458fec65c98Schristos 		{  500 },
459fec65c98Schristos 		{  667 },
460fec65c98Schristos 		{  611 },
461fec65c98Schristos 		{  889 },
462fec65c98Schristos 		{  722 },
463fec65c98Schristos 		{  722 },
464fec65c98Schristos 		{  611 },
465fec65c98Schristos 		{  722 },
466fec65c98Schristos 		{  667 },
467fec65c98Schristos 		{  556 },
468fec65c98Schristos 		{  611 },
469fec65c98Schristos 		{  722 },
470fec65c98Schristos 		{  667 },
471fec65c98Schristos 		{  889 },
472fec65c98Schristos 		{  667 },
473fec65c98Schristos 		{  611 },
474fec65c98Schristos 		{  611 },
475fec65c98Schristos 		{  333 },
476fec65c98Schristos 		{  278 },
477fec65c98Schristos 		{  333 },
478fec65c98Schristos 		{  570 },
479fec65c98Schristos 		{  500 },
480fec65c98Schristos 		{  333 },
481fec65c98Schristos 		{  500 },
482fec65c98Schristos 		{  500 },
483fec65c98Schristos 		{  444 },
484fec65c98Schristos 		{  500 },
485fec65c98Schristos 		{  444 },
486fec65c98Schristos 		{  333 },
487fec65c98Schristos 		{  500 },
488fec65c98Schristos 		{  556 },
489fec65c98Schristos 		{  278 },
490fec65c98Schristos 		{  278 },
491fec65c98Schristos 		{  500 },
492fec65c98Schristos 		{  278 },
493fec65c98Schristos 		{  778 },
494fec65c98Schristos 		{  556 },
495fec65c98Schristos 		{  500 },
496fec65c98Schristos 		{  500 },
497fec65c98Schristos 		{  500 },
498fec65c98Schristos 		{  389 },
499fec65c98Schristos 		{  389 },
500fec65c98Schristos 		{  278 },
501fec65c98Schristos 		{  556 },
502fec65c98Schristos 		{  444 },
503fec65c98Schristos 		{  667 },
504fec65c98Schristos 		{  500 },
505fec65c98Schristos 		{  444 },
506fec65c98Schristos 		{  389 },
507fec65c98Schristos 		{  348 },
508fec65c98Schristos 		{  220 },
509fec65c98Schristos 		{  348 },
510fec65c98Schristos 		{  570 },
511fec65c98Schristos 	} },
51282361f10Sjoerg };
51382361f10Sjoerg 
5147da9b934Sjoerg void *
pdf_alloc(const struct manoutput * outopts)5159ff1f2acSchristos pdf_alloc(const struct manoutput *outopts)
5167da9b934Sjoerg {
517*c9bcef03Schristos 	return pspdf_alloc(outopts, TERMTYPE_PDF);
5187da9b934Sjoerg }
5196c26a9aaSjoerg 
5206c26a9aaSjoerg void *
ps_alloc(const struct manoutput * outopts)5219ff1f2acSchristos ps_alloc(const struct manoutput *outopts)
5226c26a9aaSjoerg {
523*c9bcef03Schristos 	return pspdf_alloc(outopts, TERMTYPE_PS);
5247da9b934Sjoerg }
5257da9b934Sjoerg 
5267da9b934Sjoerg static struct termp *
pspdf_alloc(const struct manoutput * outopts,enum termtype type)527*c9bcef03Schristos pspdf_alloc(const struct manoutput *outopts, enum termtype type)
5287da9b934Sjoerg {
5297da9b934Sjoerg 	struct termp	*p;
530c5f73b34Sjoerg 	unsigned int	 pagex, pagey;
531c5f73b34Sjoerg 	size_t		 marginx, marginy, lineheight;
53282361f10Sjoerg 	const char	*pp;
5336c26a9aaSjoerg 
534*c9bcef03Schristos 	p = mandoc_calloc(1, sizeof(*p));
535*c9bcef03Schristos 	p->tcol = p->tcols = mandoc_calloc(1, sizeof(*p->tcol));
536*c9bcef03Schristos 	p->maxtcol = 1;
537*c9bcef03Schristos 	p->type = type;
538*c9bcef03Schristos 
539c5f73b34Sjoerg 	p->enc = TERMENC_ASCII;
540fec65c98Schristos 	p->fontq = mandoc_reallocarray(NULL,
541*c9bcef03Schristos 	    (p->fontsz = 8), sizeof(*p->fontq));
542fec65c98Schristos 	p->fontq[0] = p->fontl = TERMFONT_NONE;
543*c9bcef03Schristos 	p->ps = mandoc_calloc(1, sizeof(*p->ps));
5446c26a9aaSjoerg 
54582361f10Sjoerg 	p->advance = ps_advance;
5466c26a9aaSjoerg 	p->begin = ps_begin;
5476c26a9aaSjoerg 	p->end = ps_end;
5486c26a9aaSjoerg 	p->endline = ps_endline;
54982361f10Sjoerg 	p->hspan = ps_hspan;
55082361f10Sjoerg 	p->letter = ps_letter;
551fec65c98Schristos 	p->setwidth = ps_setwidth;
55282361f10Sjoerg 	p->width = ps_width;
55382361f10Sjoerg 
55482361f10Sjoerg 	/* Default to US letter (millimetres). */
55582361f10Sjoerg 
556*c9bcef03Schristos 	p->ps->medianame = "Letter";
55782361f10Sjoerg 	pagex = 216;
55882361f10Sjoerg 	pagey = 279;
55982361f10Sjoerg 
56082361f10Sjoerg 	/*
56182361f10Sjoerg 	 * The ISO-269 paper sizes can be calculated automatically, but
56282361f10Sjoerg 	 * it would require bringing in -lm for pow() and I'd rather not
56382361f10Sjoerg 	 * do that.  So just do it the easy way for now.  Since this
56482361f10Sjoerg 	 * only happens once, I'm not terribly concerned.
56582361f10Sjoerg 	 */
56682361f10Sjoerg 
5679ff1f2acSchristos 	pp = outopts->paper;
568*c9bcef03Schristos 	if (pp != NULL && strcasecmp(pp, "letter") != 0) {
569*c9bcef03Schristos 		if (strcasecmp(pp, "a3") == 0) {
570*c9bcef03Schristos 			p->ps->medianame = "A3";
57182361f10Sjoerg 			pagex = 297;
57282361f10Sjoerg 			pagey = 420;
573*c9bcef03Schristos 		} else if (strcasecmp(pp, "a4") == 0) {
574*c9bcef03Schristos 			p->ps->medianame = "A4";
57582361f10Sjoerg 			pagex = 210;
57682361f10Sjoerg 			pagey = 297;
577*c9bcef03Schristos 		} else if (strcasecmp(pp, "a5") == 0) {
578*c9bcef03Schristos 			p->ps->medianame = "A5";
57982361f10Sjoerg 			pagex = 148;
58082361f10Sjoerg 			pagey = 210;
581*c9bcef03Schristos 		} else if (strcasecmp(pp, "legal") == 0) {
582*c9bcef03Schristos 			p->ps->medianame = "Legal";
58382361f10Sjoerg 			pagex = 216;
58482361f10Sjoerg 			pagey = 356;
585*c9bcef03Schristos 		} else if (sscanf(pp, "%ux%u", &pagex, &pagey) == 2)
586*c9bcef03Schristos 			p->ps->medianame = "CustomSize";
587*c9bcef03Schristos 		else
5889ff1f2acSchristos 			warnx("%s: Unknown paper", pp);
589cf816816Sjoerg 	}
59082361f10Sjoerg 
59182361f10Sjoerg 	/*
59282361f10Sjoerg 	 * This MUST be defined before any PNT2AFM or AFM2PNT
59382361f10Sjoerg 	 * calculations occur.
59482361f10Sjoerg 	 */
59582361f10Sjoerg 
596c5f73b34Sjoerg 	p->ps->scale = 11;
59782361f10Sjoerg 
59882361f10Sjoerg 	/* Remember millimetres -> AFM units. */
59982361f10Sjoerg 
600*c9bcef03Schristos 	pagex = PNT2AFM(p, ((double)pagex * 72.0 / 25.4));
601*c9bcef03Schristos 	pagey = PNT2AFM(p, ((double)pagey * 72.0 / 25.4));
60282361f10Sjoerg 
60382361f10Sjoerg 	/* Margins are 1/9 the page x and y. */
60482361f10Sjoerg 
605fec65c98Schristos 	marginx = (size_t)((double)pagex / 9.0);
606fec65c98Schristos 	marginy = (size_t)((double)pagey / 9.0);
60782361f10Sjoerg 
60882361f10Sjoerg 	/* Line-height is 1.4em. */
60982361f10Sjoerg 
610c5f73b34Sjoerg 	lineheight = PNT2AFM(p, ((double)p->ps->scale * 1.4));
61182361f10Sjoerg 
612fec65c98Schristos 	p->ps->width = p->ps->lastwidth = (size_t)pagex;
613c5f73b34Sjoerg 	p->ps->height = (size_t)pagey;
614c5f73b34Sjoerg 	p->ps->header = pagey - (marginy / 2) - (lineheight / 2);
615c5f73b34Sjoerg 	p->ps->top = pagey - marginy;
616c5f73b34Sjoerg 	p->ps->footer = (marginy / 2) - (lineheight / 2);
617c5f73b34Sjoerg 	p->ps->bottom = marginy;
618c5f73b34Sjoerg 	p->ps->left = marginx;
619c5f73b34Sjoerg 	p->ps->lineheight = lineheight;
62082361f10Sjoerg 
62182361f10Sjoerg 	p->defrmargin = pagex - (marginx * 2);
6229ff1f2acSchristos 	return p;
6236c26a9aaSjoerg }
6246c26a9aaSjoerg 
625fec65c98Schristos static void
ps_setwidth(struct termp * p,int iop,int width)6269ff1f2acSchristos ps_setwidth(struct termp *p, int iop, int width)
627fec65c98Schristos {
628fec65c98Schristos 	size_t	 lastwidth;
629fec65c98Schristos 
630fec65c98Schristos 	lastwidth = p->ps->width;
631fec65c98Schristos 	if (iop > 0)
632fec65c98Schristos 		p->ps->width += width;
633fec65c98Schristos 	else if (iop == 0)
6349ff1f2acSchristos 		p->ps->width = width ? (size_t)width : p->ps->lastwidth;
6359ff1f2acSchristos 	else if (p->ps->width > (size_t)width)
636fec65c98Schristos 		p->ps->width -= width;
637fec65c98Schristos 	else
638fec65c98Schristos 		p->ps->width = 0;
639fec65c98Schristos 	p->ps->lastwidth = lastwidth;
640fec65c98Schristos }
6416c26a9aaSjoerg 
6426c26a9aaSjoerg void
pspdf_free(void * arg)6437da9b934Sjoerg pspdf_free(void *arg)
6446c26a9aaSjoerg {
6456c26a9aaSjoerg 	struct termp	*p;
6466c26a9aaSjoerg 
6476c26a9aaSjoerg 	p = (struct termp *)arg;
6486c26a9aaSjoerg 
649c5f73b34Sjoerg 	free(p->ps->psmarg);
650c5f73b34Sjoerg 	free(p->ps->pdfobjs);
6516c26a9aaSjoerg 
652c5f73b34Sjoerg 	free(p->ps);
6536c26a9aaSjoerg 	term_free(p);
6546c26a9aaSjoerg }
6556c26a9aaSjoerg 
6566c26a9aaSjoerg static void
ps_printf(struct termp * p,const char * fmt,...)6576c26a9aaSjoerg ps_printf(struct termp *p, const char *fmt, ...)
6586c26a9aaSjoerg {
6596c26a9aaSjoerg 	va_list		 ap;
6607da9b934Sjoerg 	int		 pos, len;
6616c26a9aaSjoerg 
6626c26a9aaSjoerg 	va_start(ap, fmt);
6636c26a9aaSjoerg 
6646c26a9aaSjoerg 	/*
6656c26a9aaSjoerg 	 * If we're running in regular mode, then pipe directly into
6666c26a9aaSjoerg 	 * vprintf().  If we're processing margins, then push the data
6676c26a9aaSjoerg 	 * into our growable margin buffer.
6686c26a9aaSjoerg 	 */
6696c26a9aaSjoerg 
670c5f73b34Sjoerg 	if ( ! (PS_MARGINS & p->ps->flags)) {
6717da9b934Sjoerg 		len = vprintf(fmt, ap);
6726c26a9aaSjoerg 		va_end(ap);
673fec65c98Schristos 		p->ps->pdfbytes += len < 0 ? 0 : (size_t)len;
6746c26a9aaSjoerg 		return;
6756c26a9aaSjoerg 	}
6766c26a9aaSjoerg 
6776c26a9aaSjoerg 	/*
6786c26a9aaSjoerg 	 * XXX: I assume that the in-margin print won't exceed
6796c26a9aaSjoerg 	 * PS_BUFSLOP (128 bytes), which is reasonable but still an
6806c26a9aaSjoerg 	 * assumption that will cause pukeage if it's not the case.
6816c26a9aaSjoerg 	 */
6826c26a9aaSjoerg 
683c0d9444aSjoerg 	ps_growbuf(p, PS_BUFSLOP);
6846c26a9aaSjoerg 
685c5f73b34Sjoerg 	pos = (int)p->ps->psmargcur;
686cf816816Sjoerg 	vsnprintf(&p->ps->psmarg[pos], PS_BUFSLOP, fmt, ap);
6876c26a9aaSjoerg 
6886c26a9aaSjoerg 	va_end(ap);
6897da9b934Sjoerg 
690c5f73b34Sjoerg 	p->ps->psmargcur = strlen(p->ps->psmarg);
6916c26a9aaSjoerg }
6926c26a9aaSjoerg 
6936c26a9aaSjoerg static void
ps_putchar(struct termp * p,char c)6946c26a9aaSjoerg ps_putchar(struct termp *p, char c)
6956c26a9aaSjoerg {
6966c26a9aaSjoerg 	int		 pos;
6976c26a9aaSjoerg 
6986c26a9aaSjoerg 	/* See ps_printf(). */
6996c26a9aaSjoerg 
700c5f73b34Sjoerg 	if ( ! (PS_MARGINS & p->ps->flags)) {
7016c26a9aaSjoerg 		putchar(c);
702c5f73b34Sjoerg 		p->ps->pdfbytes++;
7036c26a9aaSjoerg 		return;
7046c26a9aaSjoerg 	}
7056c26a9aaSjoerg 
706c0d9444aSjoerg 	ps_growbuf(p, 2);
7076c26a9aaSjoerg 
708c5f73b34Sjoerg 	pos = (int)p->ps->psmargcur++;
709c5f73b34Sjoerg 	p->ps->psmarg[pos++] = c;
710c5f73b34Sjoerg 	p->ps->psmarg[pos] = '\0';
7116c26a9aaSjoerg }
7126c26a9aaSjoerg 
7137da9b934Sjoerg static void
pdf_obj(struct termp * p,size_t obj)7147da9b934Sjoerg pdf_obj(struct termp *p, size_t obj)
7157da9b934Sjoerg {
7167da9b934Sjoerg 
7177da9b934Sjoerg 	assert(obj > 0);
7187da9b934Sjoerg 
719c5f73b34Sjoerg 	if ((obj - 1) >= p->ps->pdfobjsz) {
720c5f73b34Sjoerg 		p->ps->pdfobjsz = obj + 128;
721fec65c98Schristos 		p->ps->pdfobjs = mandoc_reallocarray(p->ps->pdfobjs,
722fec65c98Schristos 		    p->ps->pdfobjsz, sizeof(size_t));
7237da9b934Sjoerg 	}
7247da9b934Sjoerg 
725c5f73b34Sjoerg 	p->ps->pdfobjs[(int)obj - 1] = p->ps->pdfbytes;
7267da9b934Sjoerg 	ps_printf(p, "%zu 0 obj\n", obj);
7277da9b934Sjoerg }
7287da9b934Sjoerg 
7297da9b934Sjoerg static void
ps_closepage(struct termp * p)7307da9b934Sjoerg ps_closepage(struct termp *p)
7317da9b934Sjoerg {
7327da9b934Sjoerg 	int		 i;
7337da9b934Sjoerg 	size_t		 len, base;
7347da9b934Sjoerg 
7357da9b934Sjoerg 	/*
7367da9b934Sjoerg 	 * Close out a page that we've already flushed to output.  In
737*c9bcef03Schristos 	 * PostScript, we simply note that the page must be shown.  In
7387da9b934Sjoerg 	 * PDF, we must now create the Length, Resource, and Page node
7397da9b934Sjoerg 	 * for the page contents.
7407da9b934Sjoerg 	 */
7417da9b934Sjoerg 
742c5f73b34Sjoerg 	assert(p->ps->psmarg && p->ps->psmarg[0]);
743c5f73b34Sjoerg 	ps_printf(p, "%s", p->ps->psmarg);
7447da9b934Sjoerg 
7457da9b934Sjoerg 	if (TERMTYPE_PS != p->type) {
746c5f73b34Sjoerg 		len = p->ps->pdfbytes - p->ps->pdflastpg;
747c5f73b34Sjoerg 		base = p->ps->pages * 4 + p->ps->pdfbody;
7487da9b934Sjoerg 
7497da9b934Sjoerg 		ps_printf(p, "endstream\nendobj\n");
7507da9b934Sjoerg 
7517da9b934Sjoerg 		/* Length of content. */
7527da9b934Sjoerg 		pdf_obj(p, base + 1);
7537da9b934Sjoerg 		ps_printf(p, "%zu\nendobj\n", len);
7547da9b934Sjoerg 
7557da9b934Sjoerg 		/* Resource for content. */
7567da9b934Sjoerg 		pdf_obj(p, base + 2);
7577da9b934Sjoerg 		ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
7587da9b934Sjoerg 		ps_printf(p, "/Font <<\n");
7597da9b934Sjoerg 		for (i = 0; i < (int)TERMFONT__MAX; i++)
7607da9b934Sjoerg 			ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
761*c9bcef03Schristos 		ps_printf(p, ">>\n>>\nendobj\n");
7627da9b934Sjoerg 
7637da9b934Sjoerg 		/* Page node. */
7647da9b934Sjoerg 		pdf_obj(p, base + 3);
7657da9b934Sjoerg 		ps_printf(p, "<<\n");
7667da9b934Sjoerg 		ps_printf(p, "/Type /Page\n");
7677da9b934Sjoerg 		ps_printf(p, "/Parent 2 0 R\n");
7687da9b934Sjoerg 		ps_printf(p, "/Resources %zu 0 R\n", base + 2);
7697da9b934Sjoerg 		ps_printf(p, "/Contents %zu 0 R\n", base);
7707da9b934Sjoerg 		ps_printf(p, ">>\nendobj\n");
7717da9b934Sjoerg 	} else
7727da9b934Sjoerg 		ps_printf(p, "showpage\n");
7737da9b934Sjoerg 
774c5f73b34Sjoerg 	p->ps->pages++;
775c5f73b34Sjoerg 	p->ps->psrow = p->ps->top;
776c5f73b34Sjoerg 	assert( ! (PS_NEWPAGE & p->ps->flags));
777c5f73b34Sjoerg 	p->ps->flags |= PS_NEWPAGE;
7787da9b934Sjoerg }
7797da9b934Sjoerg 
7806c26a9aaSjoerg static void
ps_end(struct termp * p)7816c26a9aaSjoerg ps_end(struct termp *p)
7826c26a9aaSjoerg {
7837da9b934Sjoerg 	size_t		 i, xref, base;
7846c26a9aaSjoerg 
7859508192eSchristos 	ps_plast(p);
7869508192eSchristos 	ps_pclose(p);
7879508192eSchristos 
7886c26a9aaSjoerg 	/*
7896c26a9aaSjoerg 	 * At the end of the file, do one last showpage.  This is the
7906c26a9aaSjoerg 	 * same behaviour as groff(1) and works for multiple pages as
7916c26a9aaSjoerg 	 * well as just one.
7926c26a9aaSjoerg 	 */
7936c26a9aaSjoerg 
794c5f73b34Sjoerg 	if ( ! (PS_NEWPAGE & p->ps->flags)) {
795c5f73b34Sjoerg 		assert(0 == p->ps->flags);
796c5f73b34Sjoerg 		assert('\0' == p->ps->last);
7977da9b934Sjoerg 		ps_closepage(p);
79882361f10Sjoerg 	}
79982361f10Sjoerg 
8007da9b934Sjoerg 	if (TERMTYPE_PS == p->type) {
8017da9b934Sjoerg 		ps_printf(p, "%%%%Trailer\n");
802c5f73b34Sjoerg 		ps_printf(p, "%%%%Pages: %zu\n", p->ps->pages);
8037da9b934Sjoerg 		ps_printf(p, "%%%%EOF\n");
8047da9b934Sjoerg 		return;
8057da9b934Sjoerg 	}
8067da9b934Sjoerg 
8077da9b934Sjoerg 	pdf_obj(p, 2);
8087da9b934Sjoerg 	ps_printf(p, "<<\n/Type /Pages\n");
8097da9b934Sjoerg 	ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
810c5f73b34Sjoerg 			(size_t)AFM2PNT(p, p->ps->width),
811c5f73b34Sjoerg 			(size_t)AFM2PNT(p, p->ps->height));
8127da9b934Sjoerg 
813c5f73b34Sjoerg 	ps_printf(p, "/Count %zu\n", p->ps->pages);
8147da9b934Sjoerg 	ps_printf(p, "/Kids [");
8157da9b934Sjoerg 
816c5f73b34Sjoerg 	for (i = 0; i < p->ps->pages; i++)
817fec65c98Schristos 		ps_printf(p, " %zu 0 R", i * 4 + p->ps->pdfbody + 3);
8187da9b934Sjoerg 
819fec65c98Schristos 	base = (p->ps->pages - 1) * 4 + p->ps->pdfbody + 4;
8207da9b934Sjoerg 
8217da9b934Sjoerg 	ps_printf(p, "]\n>>\nendobj\n");
8227da9b934Sjoerg 	pdf_obj(p, base);
8237da9b934Sjoerg 	ps_printf(p, "<<\n");
8247da9b934Sjoerg 	ps_printf(p, "/Type /Catalog\n");
8257da9b934Sjoerg 	ps_printf(p, "/Pages 2 0 R\n");
826*c9bcef03Schristos 	ps_printf(p, ">>\nendobj\n");
827c5f73b34Sjoerg 	xref = p->ps->pdfbytes;
8287da9b934Sjoerg 	ps_printf(p, "xref\n");
8297da9b934Sjoerg 	ps_printf(p, "0 %zu\n", base + 1);
8307da9b934Sjoerg 	ps_printf(p, "0000000000 65535 f \n");
8317da9b934Sjoerg 
8327da9b934Sjoerg 	for (i = 0; i < base; i++)
8337da9b934Sjoerg 		ps_printf(p, "%.10zu 00000 n \n",
834c5f73b34Sjoerg 		    p->ps->pdfobjs[(int)i]);
8357da9b934Sjoerg 
8367da9b934Sjoerg 	ps_printf(p, "trailer\n");
8377da9b934Sjoerg 	ps_printf(p, "<<\n");
8387da9b934Sjoerg 	ps_printf(p, "/Size %zu\n", base + 1);
8397da9b934Sjoerg 	ps_printf(p, "/Root %zu 0 R\n", base);
8407da9b934Sjoerg 	ps_printf(p, "/Info 1 0 R\n");
8417da9b934Sjoerg 	ps_printf(p, ">>\n");
8427da9b934Sjoerg 	ps_printf(p, "startxref\n");
8437da9b934Sjoerg 	ps_printf(p, "%zu\n", xref);
8447da9b934Sjoerg 	ps_printf(p, "%%%%EOF\n");
8456c26a9aaSjoerg }
8466c26a9aaSjoerg 
8476c26a9aaSjoerg static void
ps_begin(struct termp * p)8486c26a9aaSjoerg ps_begin(struct termp *p)
8496c26a9aaSjoerg {
850*c9bcef03Schristos 	size_t		 width, height;
85182361f10Sjoerg 	int		 i;
8526c26a9aaSjoerg 
8536c26a9aaSjoerg 	/*
8546c26a9aaSjoerg 	 * Print margins into margin buffer.  Nothing gets output to the
8556c26a9aaSjoerg 	 * screen yet, so we don't need to initialise the primary state.
8566c26a9aaSjoerg 	 */
8576c26a9aaSjoerg 
858c5f73b34Sjoerg 	if (p->ps->psmarg) {
859c5f73b34Sjoerg 		assert(p->ps->psmargsz);
860c5f73b34Sjoerg 		p->ps->psmarg[0] = '\0';
8616c26a9aaSjoerg 	}
8626c26a9aaSjoerg 
863c5f73b34Sjoerg 	/*p->ps->pdfbytes = 0;*/
864c5f73b34Sjoerg 	p->ps->psmargcur = 0;
865c5f73b34Sjoerg 	p->ps->flags = PS_MARGINS;
866c5f73b34Sjoerg 	p->ps->pscol = p->ps->left;
867c5f73b34Sjoerg 	p->ps->psrow = p->ps->header;
868*c9bcef03Schristos 	p->ps->lastrow = 0; /* impossible row */
8696c26a9aaSjoerg 
8706c26a9aaSjoerg 	ps_setfont(p, TERMFONT_NONE);
8716c26a9aaSjoerg 
8726c26a9aaSjoerg 	(*p->headf)(p, p->argf);
8736c26a9aaSjoerg 	(*p->endline)(p);
8746c26a9aaSjoerg 
875c5f73b34Sjoerg 	p->ps->pscol = p->ps->left;
876c5f73b34Sjoerg 	p->ps->psrow = p->ps->footer;
8776c26a9aaSjoerg 
8786c26a9aaSjoerg 	(*p->footf)(p, p->argf);
8796c26a9aaSjoerg 	(*p->endline)(p);
8806c26a9aaSjoerg 
881c5f73b34Sjoerg 	p->ps->flags &= ~PS_MARGINS;
8826c26a9aaSjoerg 
883c5f73b34Sjoerg 	assert(0 == p->ps->flags);
884c5f73b34Sjoerg 	assert(p->ps->psmarg);
885c5f73b34Sjoerg 	assert('\0' != p->ps->psmarg[0]);
8866c26a9aaSjoerg 
8876c26a9aaSjoerg 	/*
8886c26a9aaSjoerg 	 * Print header and initialise page state.  Following this,
8896c26a9aaSjoerg 	 * stuff gets printed to the screen, so make sure we're sane.
8906c26a9aaSjoerg 	 */
8916c26a9aaSjoerg 
8927da9b934Sjoerg 	if (TERMTYPE_PS == p->type) {
893*c9bcef03Schristos 		width = AFM2PNT(p, p->ps->width);
894*c9bcef03Schristos 		height = AFM2PNT(p, p->ps->height);
895*c9bcef03Schristos 
8967da9b934Sjoerg 		ps_printf(p, "%%!PS-Adobe-3.0\n");
8977da9b934Sjoerg 		ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
8987da9b934Sjoerg 		ps_printf(p, "%%%%Orientation: Portrait\n");
8997da9b934Sjoerg 		ps_printf(p, "%%%%Pages: (atend)\n");
9007da9b934Sjoerg 		ps_printf(p, "%%%%PageOrder: Ascend\n");
901*c9bcef03Schristos 		ps_printf(p, "%%%%DocumentMedia: man-%s %zu %zu 0 () ()\n",
902*c9bcef03Schristos 		    p->ps->medianame, width, height);
9037da9b934Sjoerg 		ps_printf(p, "%%%%DocumentNeededResources: font");
90482361f10Sjoerg 
9057da9b934Sjoerg 		for (i = 0; i < (int)TERMFONT__MAX; i++)
9067da9b934Sjoerg 			ps_printf(p, " %s", fonts[i].name);
9077da9b934Sjoerg 
908*c9bcef03Schristos 		ps_printf(p, "\n%%%%DocumentSuppliedResources: "
909*c9bcef03Schristos 		    "procset MandocProcs 1.0 0\n");
910*c9bcef03Schristos 		ps_printf(p, "%%%%EndComments\n");
911*c9bcef03Schristos 		ps_printf(p, "%%%%BeginProlog\n");
912*c9bcef03Schristos 		ps_printf(p, "%%%%BeginResource: procset MandocProcs "
913*c9bcef03Schristos 		    "10170 10170\n");
914*c9bcef03Schristos 		/* The font size is effectively hard-coded for now. */
915*c9bcef03Schristos 		ps_printf(p, "/fs %zu def\n", p->ps->scale);
916*c9bcef03Schristos 		for (i = 0; i < (int)TERMFONT__MAX; i++)
917*c9bcef03Schristos 			ps_printf(p, "/f%d { /%s fs selectfont } def\n",
918*c9bcef03Schristos 			    i, fonts[i].name);
919*c9bcef03Schristos 		ps_printf(p, "/s { 3 1 roll moveto show } bind def\n");
920*c9bcef03Schristos 		ps_printf(p, "/c { exch currentpoint exch pop "
921*c9bcef03Schristos 		    "moveto show } bind def\n");
922*c9bcef03Schristos 		ps_printf(p, "%%%%EndResource\n");
923*c9bcef03Schristos 		ps_printf(p, "%%%%EndProlog\n");
924*c9bcef03Schristos 		ps_printf(p, "%%%%BeginSetup\n");
925*c9bcef03Schristos 		ps_printf(p, "%%%%BeginFeature: *PageSize %s\n",
926*c9bcef03Schristos 		    p->ps->medianame);
927*c9bcef03Schristos 		ps_printf(p, "<</PageSize [%zu %zu]>>setpagedevice\n",
928*c9bcef03Schristos 		    width, height);
929*c9bcef03Schristos 		ps_printf(p, "%%%%EndFeature\n");
930*c9bcef03Schristos 		ps_printf(p, "%%%%EndSetup\n");
9317da9b934Sjoerg 	} else {
9327da9b934Sjoerg 		ps_printf(p, "%%PDF-1.1\n");
9337da9b934Sjoerg 		pdf_obj(p, 1);
9347da9b934Sjoerg 		ps_printf(p, "<<\n");
9357da9b934Sjoerg 		ps_printf(p, ">>\n");
9367da9b934Sjoerg 		ps_printf(p, "endobj\n");
9377da9b934Sjoerg 
9387da9b934Sjoerg 		for (i = 0; i < (int)TERMFONT__MAX; i++) {
9397da9b934Sjoerg 			pdf_obj(p, (size_t)i + 3);
9407da9b934Sjoerg 			ps_printf(p, "<<\n");
9417da9b934Sjoerg 			ps_printf(p, "/Type /Font\n");
9427da9b934Sjoerg 			ps_printf(p, "/Subtype /Type1\n");
94370f041f9Sjoerg 			ps_printf(p, "/Name /F%d\n", i);
9447da9b934Sjoerg 			ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
945*c9bcef03Schristos 			ps_printf(p, ">>\nendobj\n");
9467da9b934Sjoerg 		}
9477da9b934Sjoerg 	}
9487da9b934Sjoerg 
949c5f73b34Sjoerg 	p->ps->pdfbody = (size_t)TERMFONT__MAX + 3;
950c5f73b34Sjoerg 	p->ps->pscol = p->ps->left;
951c5f73b34Sjoerg 	p->ps->psrow = p->ps->top;
952c5f73b34Sjoerg 	p->ps->flags |= PS_NEWPAGE;
9536c26a9aaSjoerg 	ps_setfont(p, TERMFONT_NONE);
9546c26a9aaSjoerg }
9556c26a9aaSjoerg 
9566c26a9aaSjoerg static void
ps_pletter(struct termp * p,int c)95782361f10Sjoerg ps_pletter(struct termp *p, int c)
9586c26a9aaSjoerg {
95982361f10Sjoerg 	int		 f;
96082361f10Sjoerg 
96182361f10Sjoerg 	/*
96282361f10Sjoerg 	 * If we haven't opened a page context, then output that we're
96382361f10Sjoerg 	 * in a new page and make sure the font is correctly set.
96482361f10Sjoerg 	 */
96582361f10Sjoerg 
966c5f73b34Sjoerg 	if (PS_NEWPAGE & p->ps->flags) {
9677da9b934Sjoerg 		if (TERMTYPE_PS == p->type) {
9687da9b934Sjoerg 			ps_printf(p, "%%%%Page: %zu %zu\n",
969fec65c98Schristos 			    p->ps->pages + 1, p->ps->pages + 1);
970*c9bcef03Schristos 			ps_printf(p, "f%d\n", (int)p->ps->lastf);
9717da9b934Sjoerg 		} else {
972c5f73b34Sjoerg 			pdf_obj(p, p->ps->pdfbody +
973c5f73b34Sjoerg 			    p->ps->pages * 4);
9747da9b934Sjoerg 			ps_printf(p, "<<\n");
9757da9b934Sjoerg 			ps_printf(p, "/Length %zu 0 R\n",
976fec65c98Schristos 			    p->ps->pdfbody + 1 + p->ps->pages * 4);
9777da9b934Sjoerg 			ps_printf(p, ">>\nstream\n");
9787da9b934Sjoerg 		}
979c5f73b34Sjoerg 		p->ps->pdflastpg = p->ps->pdfbytes;
980c5f73b34Sjoerg 		p->ps->flags &= ~PS_NEWPAGE;
98182361f10Sjoerg 	}
9826c26a9aaSjoerg 
9836c26a9aaSjoerg 	/*
9846c26a9aaSjoerg 	 * If we're not in a PostScript "word" context, then open one
9856c26a9aaSjoerg 	 * now at the current cursor.
9866c26a9aaSjoerg 	 */
9876c26a9aaSjoerg 
988c5f73b34Sjoerg 	if ( ! (PS_INLINE & p->ps->flags)) {
9897da9b934Sjoerg 		if (TERMTYPE_PS != p->type) {
9907da9b934Sjoerg 			ps_printf(p, "BT\n/F%d %zu Tf\n",
991fec65c98Schristos 			    (int)p->ps->lastf, p->ps->scale);
9927da9b934Sjoerg 			ps_printf(p, "%.3f %.3f Td\n(",
993c5f73b34Sjoerg 			    AFM2PNT(p, p->ps->pscol),
994c5f73b34Sjoerg 			    AFM2PNT(p, p->ps->psrow));
995*c9bcef03Schristos 		} else {
996*c9bcef03Schristos 			ps_printf(p, "%.3f", AFM2PNT(p, p->ps->pscol));
997*c9bcef03Schristos 			if (p->ps->psrow != p->ps->lastrow)
998*c9bcef03Schristos 				ps_printf(p, " %.3f",
999c5f73b34Sjoerg 				    AFM2PNT(p, p->ps->psrow));
1000*c9bcef03Schristos 			ps_printf(p, "(");
1001*c9bcef03Schristos 		}
1002c5f73b34Sjoerg 		p->ps->flags |= PS_INLINE;
10036c26a9aaSjoerg 	}
10046c26a9aaSjoerg 
1005c5f73b34Sjoerg 	assert( ! (PS_NEWPAGE & p->ps->flags));
100682361f10Sjoerg 
10076c26a9aaSjoerg 	/*
10086c26a9aaSjoerg 	 * We need to escape these characters as per the PostScript
10096c26a9aaSjoerg 	 * specification.  We would also escape non-graphable characters
10106c26a9aaSjoerg 	 * (like tabs), but none of them would get to this point and
10116c26a9aaSjoerg 	 * it's superfluous to abort() on them.
10126c26a9aaSjoerg 	 */
10136c26a9aaSjoerg 
10146c26a9aaSjoerg 	switch (c) {
1015fec65c98Schristos 	case '(':
1016fec65c98Schristos 	case ')':
1017fec65c98Schristos 	case '\\':
10186c26a9aaSjoerg 		ps_putchar(p, '\\');
10196c26a9aaSjoerg 		break;
10206c26a9aaSjoerg 	default:
10216c26a9aaSjoerg 		break;
10226c26a9aaSjoerg 	}
10236c26a9aaSjoerg 
10246c26a9aaSjoerg 	/* Write the character and adjust where we are on the page. */
10256c26a9aaSjoerg 
1026c5f73b34Sjoerg 	f = (int)p->ps->lastf;
102782361f10Sjoerg 
1028fec65c98Schristos 	if (c <= 32 || c - 32 >= MAXCHAR)
1029fec65c98Schristos 		c = 32;
103082361f10Sjoerg 
103182361f10Sjoerg 	ps_putchar(p, (char)c);
103282361f10Sjoerg 	c -= 32;
1033c5f73b34Sjoerg 	p->ps->pscol += (size_t)fonts[f].gly[c].wx;
10346c26a9aaSjoerg }
10356c26a9aaSjoerg 
10366c26a9aaSjoerg static void
ps_pclose(struct termp * p)10376c26a9aaSjoerg ps_pclose(struct termp *p)
10386c26a9aaSjoerg {
10396c26a9aaSjoerg 
10406c26a9aaSjoerg 	/*
10416c26a9aaSjoerg 	 * Spit out that we're exiting a word context (this is a
10426c26a9aaSjoerg 	 * "partial close" because we don't check the last-char buffer
10436c26a9aaSjoerg 	 * or anything).
10446c26a9aaSjoerg 	 */
10456c26a9aaSjoerg 
1046c5f73b34Sjoerg 	if ( ! (PS_INLINE & p->ps->flags))
10476c26a9aaSjoerg 		return;
10486c26a9aaSjoerg 
1049*c9bcef03Schristos 	if (TERMTYPE_PS != p->type)
10507da9b934Sjoerg 		ps_printf(p, ") Tj\nET\n");
1051*c9bcef03Schristos 	else if (p->ps->psrow == p->ps->lastrow)
1052*c9bcef03Schristos 		ps_printf(p, ")c\n");
1053*c9bcef03Schristos 	else {
1054*c9bcef03Schristos 		ps_printf(p, ")s\n");
1055*c9bcef03Schristos 		p->ps->lastrow = p->ps->psrow;
1056*c9bcef03Schristos 	}
10577da9b934Sjoerg 
1058c5f73b34Sjoerg 	p->ps->flags &= ~PS_INLINE;
10596c26a9aaSjoerg }
10606c26a9aaSjoerg 
10619508192eSchristos /* If we have a `last' char that wasn't printed yet, print it now. */
10626c26a9aaSjoerg static void
ps_plast(struct termp * p)10639508192eSchristos ps_plast(struct termp *p)
10646c26a9aaSjoerg {
10659508192eSchristos 	size_t	 wx;
10666c26a9aaSjoerg 
10679508192eSchristos 	if (p->ps->last == '\0')
10689508192eSchristos 		return;
10696c26a9aaSjoerg 
10709508192eSchristos 	/* Check the font mode; open a new scope if it doesn't match. */
10719508192eSchristos 
1072fec65c98Schristos 	if (p->ps->nextf != p->ps->lastf) {
10736c26a9aaSjoerg 		ps_pclose(p);
1074fec65c98Schristos 		ps_setfont(p, p->ps->nextf);
10756c26a9aaSjoerg 	}
1076fec65c98Schristos 	p->ps->nextf = TERMFONT_NONE;
10779508192eSchristos 
10789508192eSchristos 	/*
10799508192eSchristos 	 * For an overstrike, if a previous character
10809508192eSchristos 	 * was wider, advance to center the new one.
10819508192eSchristos 	 */
10829508192eSchristos 
10839508192eSchristos 	if (p->ps->pscolnext) {
10849508192eSchristos 		wx = fonts[p->ps->lastf].gly[(int)p->ps->last-32].wx;
10859508192eSchristos 		if (p->ps->pscol + wx < p->ps->pscolnext)
10869508192eSchristos 			p->ps->pscol = (p->ps->pscol +
10879508192eSchristos 			    p->ps->pscolnext - wx) / 2;
10886c26a9aaSjoerg 	}
10896c26a9aaSjoerg 
10909508192eSchristos 	ps_pletter(p, p->ps->last);
10919508192eSchristos 	p->ps->last = '\0';
10926c26a9aaSjoerg 
10939508192eSchristos 	/*
10949508192eSchristos 	 * For an overstrike, if a previous character
10959508192eSchristos 	 * was wider, advance to the end of the old one.
10969508192eSchristos 	 */
10979508192eSchristos 
10989508192eSchristos 	if (p->ps->pscol < p->ps->pscolnext) {
10996c26a9aaSjoerg 		ps_pclose(p);
11009508192eSchristos 		p->ps->pscol = p->ps->pscolnext;
11019508192eSchristos 	}
11026c26a9aaSjoerg }
11036c26a9aaSjoerg 
11046c26a9aaSjoerg static void
ps_letter(struct termp * p,int arg)1105c5f73b34Sjoerg ps_letter(struct termp *p, int arg)
11066c26a9aaSjoerg {
11079508192eSchristos 	size_t		savecol;
1108fec65c98Schristos 	char		c;
1109c5f73b34Sjoerg 
1110c5f73b34Sjoerg 	c = arg >= 128 || arg <= 0 ? '?' : arg;
11116c26a9aaSjoerg 
11126c26a9aaSjoerg 	/*
1113fec65c98Schristos 	 * When receiving a backspace, merely flag it.
1114fec65c98Schristos 	 * We don't know yet whether it is
1115fec65c98Schristos 	 * a font instruction or an overstrike.
11166c26a9aaSjoerg 	 */
11176c26a9aaSjoerg 
1118fec65c98Schristos 	if (c == '\b') {
1119fec65c98Schristos 		assert(p->ps->last != '\0');
1120fec65c98Schristos 		assert( ! (p->ps->flags & PS_BACKSP));
1121fec65c98Schristos 		p->ps->flags |= PS_BACKSP;
11226c26a9aaSjoerg 		return;
11236c26a9aaSjoerg 	}
11246c26a9aaSjoerg 
1125fec65c98Schristos 	/*
1126fec65c98Schristos 	 * Decode font instructions.
1127fec65c98Schristos 	 */
1128fec65c98Schristos 
1129fec65c98Schristos 	if (p->ps->flags & PS_BACKSP) {
1130fec65c98Schristos 		if (p->ps->last == '_') {
1131fec65c98Schristos 			switch (p->ps->nextf) {
1132fec65c98Schristos 			case TERMFONT_BI:
1133fec65c98Schristos 				break;
1134fec65c98Schristos 			case TERMFONT_BOLD:
1135fec65c98Schristos 				p->ps->nextf = TERMFONT_BI;
1136fec65c98Schristos 				break;
1137fec65c98Schristos 			default:
1138fec65c98Schristos 				p->ps->nextf = TERMFONT_UNDER;
1139fec65c98Schristos 			}
1140fec65c98Schristos 			p->ps->last = c;
1141fec65c98Schristos 			p->ps->flags &= ~PS_BACKSP;
1142fec65c98Schristos 			return;
1143fec65c98Schristos 		}
1144fec65c98Schristos 		if (p->ps->last == c) {
1145fec65c98Schristos 			switch (p->ps->nextf) {
1146fec65c98Schristos 			case TERMFONT_BI:
1147fec65c98Schristos 				break;
1148fec65c98Schristos 			case TERMFONT_UNDER:
1149fec65c98Schristos 				p->ps->nextf = TERMFONT_BI;
1150fec65c98Schristos 				break;
1151fec65c98Schristos 			default:
1152fec65c98Schristos 				p->ps->nextf = TERMFONT_BOLD;
1153fec65c98Schristos 			}
1154fec65c98Schristos 			p->ps->flags &= ~PS_BACKSP;
1155fec65c98Schristos 			return;
11566c26a9aaSjoerg 		}
11576c26a9aaSjoerg 
1158fec65c98Schristos 		/*
1159fec65c98Schristos 		 * This is not a font instruction, but rather
1160fec65c98Schristos 		 * the next character.  Prepare for overstrike.
1161fec65c98Schristos 		 */
1162fec65c98Schristos 
1163fec65c98Schristos 		savecol = p->ps->pscol;
1164fec65c98Schristos 	} else
1165fec65c98Schristos 		savecol = SIZE_MAX;
1166fec65c98Schristos 
1167fec65c98Schristos 	/*
1168fec65c98Schristos 	 * We found the next character, so the font instructions
1169fec65c98Schristos 	 * for the previous one are complete.
1170fec65c98Schristos 	 * Use them and print it.
1171fec65c98Schristos 	 */
1172fec65c98Schristos 
11739508192eSchristos 	ps_plast(p);
1174fec65c98Schristos 
1175fec65c98Schristos 	/*
1176fec65c98Schristos 	 * Do not print the current character yet because font
11779508192eSchristos 	 * instructions might follow; only remember the character.
11789508192eSchristos 	 * It will get printed later from ps_plast().
1179fec65c98Schristos 	 */
1180fec65c98Schristos 
1181fec65c98Schristos 	p->ps->last = c;
1182fec65c98Schristos 
1183fec65c98Schristos 	/*
1184fec65c98Schristos 	 * For an overstrike, back up to the previous position.
1185fec65c98Schristos 	 * If the previous character is wider than any it overstrikes,
1186fec65c98Schristos 	 * remember the current position, because it might also be
1187fec65c98Schristos 	 * wider than all that will overstrike it.
1188fec65c98Schristos 	 */
1189fec65c98Schristos 
1190fec65c98Schristos 	if (savecol != SIZE_MAX) {
1191fec65c98Schristos 		if (p->ps->pscolnext < p->ps->pscol)
1192fec65c98Schristos 			p->ps->pscolnext = p->ps->pscol;
1193fec65c98Schristos 		ps_pclose(p);
1194fec65c98Schristos 		p->ps->pscol = savecol;
1195fec65c98Schristos 		p->ps->flags &= ~PS_BACKSP;
1196fec65c98Schristos 	} else
1197fec65c98Schristos 		p->ps->pscolnext = 0;
1198fec65c98Schristos }
11996c26a9aaSjoerg 
12006c26a9aaSjoerg static void
ps_advance(struct termp * p,size_t len)12016c26a9aaSjoerg ps_advance(struct termp *p, size_t len)
12026c26a9aaSjoerg {
12036c26a9aaSjoerg 
12046c26a9aaSjoerg 	/*
12056c26a9aaSjoerg 	 * Advance some spaces.  This can probably be made smarter,
12066c26a9aaSjoerg 	 * i.e., to have multiple space-separated words in the same
12076c26a9aaSjoerg 	 * scope, but this is easier:  just close out the current scope
12086c26a9aaSjoerg 	 * and readjust our column settings.
12096c26a9aaSjoerg 	 */
12106c26a9aaSjoerg 
12119508192eSchristos 	ps_plast(p);
12129508192eSchristos 	ps_pclose(p);
1213c5f73b34Sjoerg 	p->ps->pscol += len;
12146c26a9aaSjoerg }
12156c26a9aaSjoerg 
12166c26a9aaSjoerg static void
ps_endline(struct termp * p)12176c26a9aaSjoerg ps_endline(struct termp *p)
12186c26a9aaSjoerg {
12196c26a9aaSjoerg 
12206c26a9aaSjoerg 	/* Close out any scopes we have open: we're at eoln. */
12216c26a9aaSjoerg 
12229508192eSchristos 	ps_plast(p);
12239508192eSchristos 	ps_pclose(p);
12246c26a9aaSjoerg 
12256c26a9aaSjoerg 	/*
12266c26a9aaSjoerg 	 * If we're in the margin, don't try to recalculate our current
12276c26a9aaSjoerg 	 * row.  XXX: if the column tries to be fancy with multiple
12286c26a9aaSjoerg 	 * lines, we'll do nasty stuff.
12296c26a9aaSjoerg 	 */
12306c26a9aaSjoerg 
1231c5f73b34Sjoerg 	if (PS_MARGINS & p->ps->flags)
123282361f10Sjoerg 		return;
123382361f10Sjoerg 
123482361f10Sjoerg 	/* Left-justify. */
123582361f10Sjoerg 
1236c5f73b34Sjoerg 	p->ps->pscol = p->ps->left;
123782361f10Sjoerg 
123882361f10Sjoerg 	/* If we haven't printed anything, return. */
123982361f10Sjoerg 
1240c5f73b34Sjoerg 	if (PS_NEWPAGE & p->ps->flags)
12416c26a9aaSjoerg 		return;
12426c26a9aaSjoerg 
12436c26a9aaSjoerg 	/*
12446c26a9aaSjoerg 	 * Put us down a line.  If we're at the page bottom, spit out a
12456c26a9aaSjoerg 	 * showpage and restart our row.
12466c26a9aaSjoerg 	 */
12476c26a9aaSjoerg 
1248fec65c98Schristos 	if (p->ps->psrow >= p->ps->lineheight + p->ps->bottom) {
1249c5f73b34Sjoerg 		p->ps->psrow -= p->ps->lineheight;
12506c26a9aaSjoerg 		return;
12516c26a9aaSjoerg 	}
12526c26a9aaSjoerg 
12537da9b934Sjoerg 	ps_closepage(p);
1254*c9bcef03Schristos 
1255*c9bcef03Schristos 	p->tcol->offset -= p->ti;
1256*c9bcef03Schristos 	p->ti = 0;
12576c26a9aaSjoerg }
12586c26a9aaSjoerg 
12596c26a9aaSjoerg static void
ps_setfont(struct termp * p,enum termfont f)12606c26a9aaSjoerg ps_setfont(struct termp *p, enum termfont f)
12616c26a9aaSjoerg {
12626c26a9aaSjoerg 
126382361f10Sjoerg 	assert(f < TERMFONT__MAX);
1264c5f73b34Sjoerg 	p->ps->lastf = f;
126582361f10Sjoerg 
126682361f10Sjoerg 	/*
126782361f10Sjoerg 	 * If we're still at the top of the page, let the font-setting
126882361f10Sjoerg 	 * be delayed until we actually have stuff to print.
126982361f10Sjoerg 	 */
127082361f10Sjoerg 
1271c5f73b34Sjoerg 	if (PS_NEWPAGE & p->ps->flags)
127282361f10Sjoerg 		return;
127382361f10Sjoerg 
12747da9b934Sjoerg 	if (TERMTYPE_PS == p->type)
1275*c9bcef03Schristos 		ps_printf(p, "f%d\n", (int)f);
12767da9b934Sjoerg 	else
12777da9b934Sjoerg 		ps_printf(p, "/F%d %zu Tf\n",
1278fec65c98Schristos 		    (int)f, p->ps->scale);
127982361f10Sjoerg }
128082361f10Sjoerg 
128182361f10Sjoerg static size_t
ps_width(const struct termp * p,int c)1282c5f73b34Sjoerg ps_width(const struct termp *p, int c)
128382361f10Sjoerg {
128482361f10Sjoerg 
128582361f10Sjoerg 	if (c <= 32 || c - 32 >= MAXCHAR)
1286fec65c98Schristos 		c = 0;
1287fec65c98Schristos 	else
128882361f10Sjoerg 		c -= 32;
1289fec65c98Schristos 
12909ff1f2acSchristos 	return (size_t)fonts[(int)TERMFONT_NONE].gly[c].wx;
129182361f10Sjoerg }
129282361f10Sjoerg 
12939ff1f2acSchristos static int
ps_hspan(const struct termp * p,const struct roffsu * su)129482361f10Sjoerg ps_hspan(const struct termp *p, const struct roffsu *su)
129582361f10Sjoerg {
129682361f10Sjoerg 	double		 r;
129782361f10Sjoerg 
129882361f10Sjoerg 	/*
129982361f10Sjoerg 	 * All of these measurements are derived by converting from the
130082361f10Sjoerg 	 * native measurement to AFM units.
130182361f10Sjoerg 	 */
130282361f10Sjoerg 	switch (su->unit) {
1303fec65c98Schristos 	case SCALE_BU:
1304fec65c98Schristos 		/*
1305fec65c98Schristos 		 * Traditionally, the default unit is fixed to the
1306fec65c98Schristos 		 * output media.  So this would refer to the point.  In
1307fec65c98Schristos 		 * mandoc(1), however, we stick to the default terminal
1308fec65c98Schristos 		 * scaling unit so that output is the same regardless
1309fec65c98Schristos 		 * the media.
1310fec65c98Schristos 		 */
1311fec65c98Schristos 		r = PNT2AFM(p, su->scale * 72.0 / 240.0);
131282361f10Sjoerg 		break;
1313fec65c98Schristos 	case SCALE_CM:
1314fec65c98Schristos 		r = PNT2AFM(p, su->scale * 72.0 / 2.54);
131582361f10Sjoerg 		break;
1316fec65c98Schristos 	case SCALE_EM:
131782361f10Sjoerg 		r = su->scale *
131882361f10Sjoerg 		    fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
131982361f10Sjoerg 		break;
1320fec65c98Schristos 	case SCALE_EN:
132182361f10Sjoerg 		r = su->scale *
132282361f10Sjoerg 		    fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
132382361f10Sjoerg 		break;
1324fec65c98Schristos 	case SCALE_IN:
1325fec65c98Schristos 		r = PNT2AFM(p, su->scale * 72.0);
1326fec65c98Schristos 		break;
1327fec65c98Schristos 	case SCALE_MM:
1328fec65c98Schristos 		r = su->scale *
1329fec65c98Schristos 		    fonts[(int)TERMFONT_NONE].gly[109 - 32].wx / 100.0;
1330fec65c98Schristos 		break;
1331fec65c98Schristos 	case SCALE_PC:
1332fec65c98Schristos 		r = PNT2AFM(p, su->scale * 12.0);
1333fec65c98Schristos 		break;
1334fec65c98Schristos 	case SCALE_PT:
1335fec65c98Schristos 		r = PNT2AFM(p, su->scale * 1.0);
1336fec65c98Schristos 		break;
1337fec65c98Schristos 	case SCALE_VS:
1338c5f73b34Sjoerg 		r = su->scale * p->ps->lineheight;
133982361f10Sjoerg 		break;
134082361f10Sjoerg 	default:
134182361f10Sjoerg 		r = su->scale;
134282361f10Sjoerg 		break;
134382361f10Sjoerg 	}
134482361f10Sjoerg 
13459ff1f2acSchristos 	return r * 24.0;
13466c26a9aaSjoerg }
13476c26a9aaSjoerg 
1348c5f73b34Sjoerg static void
ps_growbuf(struct termp * p,size_t sz)1349c5f73b34Sjoerg ps_growbuf(struct termp *p, size_t sz)
1350c5f73b34Sjoerg {
1351c5f73b34Sjoerg 	if (p->ps->psmargcur + sz <= p->ps->psmargsz)
1352c5f73b34Sjoerg 		return;
1353c5f73b34Sjoerg 
1354c5f73b34Sjoerg 	if (sz < PS_BUFSLOP)
1355c5f73b34Sjoerg 		sz = PS_BUFSLOP;
1356c5f73b34Sjoerg 
1357c5f73b34Sjoerg 	p->ps->psmargsz += sz;
1358fec65c98Schristos 	p->ps->psmarg = mandoc_realloc(p->ps->psmarg, p->ps->psmargsz);
1359c5f73b34Sjoerg }
1360