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