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