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