Annotation of mandoc/term_ps.c, Revision 1.56
1.56 ! schwarze 1: /* $Id: term_ps.c,v 1.55 2014/01/05 19:10:56 joerg Exp $ */
1.1 kristaps 2: /*
1.53 schwarze 3: * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.43 kristaps 21: #include <sys/types.h>
1.4 kristaps 22:
1.2 kristaps 23: #include <assert.h>
1.4 kristaps 24: #include <stdarg.h>
1.17 kristaps 25: #include <stdint.h>
1.2 kristaps 26: #include <stdio.h>
1.1 kristaps 27: #include <stdlib.h>
1.4 kristaps 28: #include <string.h>
1.13 kristaps 29: #include <time.h>
1.26 kristaps 30: #include <unistd.h>
1.1 kristaps 31:
1.42 schwarze 32: #include "mandoc.h"
1.56 ! schwarze 33: #include "mandoc_aux.h"
1.1 kristaps 34: #include "out.h"
35: #include "main.h"
36: #include "term.h"
37:
1.50 kristaps 38: /* These work the buffer used by the header and footer. */
39: #define PS_BUFSLOP 128
40:
1.21 kristaps 41: /* Convert PostScript point "x" to an AFM unit. */
1.25 kristaps 42: #define PNT2AFM(p, x) /* LINTED */ \
1.50 kristaps 43: (size_t)((double)(x) * (1000.0 / (double)(p)->ps->scale))
1.21 kristaps 44:
45: /* Convert an AFM unit "x" to a PostScript points */
1.25 kristaps 46: #define AFM2PNT(p, x) /* LINTED */ \
1.50 kristaps 47: ((double)(x) / (1000.0 / (double)(p)->ps->scale))
1.21 kristaps 48:
1.13 kristaps 49: struct glyph {
1.36 kristaps 50: unsigned short wx; /* WX in AFM */
1.13 kristaps 51: };
52:
53: struct font {
54: const char *name; /* FontName in AFM */
1.15 kristaps 55: #define MAXCHAR 95 /* total characters we can handle */
1.13 kristaps 56: struct glyph gly[MAXCHAR]; /* glyph metrics */
57: };
58:
1.50 kristaps 59: struct termp_ps {
60: int flags;
61: #define PS_INLINE (1 << 0) /* we're in a word */
62: #define PS_MARGINS (1 << 1) /* we're in the margins */
63: #define PS_NEWPAGE (1 << 2) /* new page, no words yet */
64: size_t pscol; /* visible column (AFM units) */
65: size_t psrow; /* visible row (AFM units) */
66: char *psmarg; /* margin buf */
67: size_t psmargsz; /* margin buf size */
68: size_t psmargcur; /* cur index in margin buf */
69: char last; /* character buffer */
70: enum termfont lastf; /* last set font */
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 left; /* body left (AFM units) */
79: size_t header; /* header pos (AFM units) */
80: size_t footer; /* footer pos (AFM units) */
81: size_t pdfbytes; /* current output byte */
82: size_t pdflastpg; /* byte of last page mark */
83: size_t pdfbody; /* start of body object */
84: size_t *pdfobjs; /* table of object offsets */
85: size_t pdfobjsz; /* size of pdfobjs */
86: };
87:
88: static double ps_hspan(const struct termp *,
89: const struct roffsu *);
90: static size_t ps_width(const struct termp *, int);
91: static void ps_advance(struct termp *, size_t);
92: static void ps_begin(struct termp *);
93: static void ps_closepage(struct termp *);
94: static void ps_end(struct termp *);
95: static void ps_endline(struct termp *);
96: static void ps_fclose(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 *);
100: static void ps_pletter(struct termp *, int);
1.55 joerg 101: #if __GNUC__ - 0 >= 4
102: __attribute__((__format__ (__printf__, 2, 3)))
103: #endif
1.50 kristaps 104: static void ps_printf(struct termp *, const char *, ...);
105: static void ps_putchar(struct termp *, char);
106: static void ps_setfont(struct termp *, enum termfont);
107: static struct termp *pspdf_alloc(char *);
108: static void pdf_obj(struct termp *, size_t);
109:
1.13 kristaps 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:
1.16 kristaps 116: static const struct font fonts[TERMFONT__MAX] = {
1.24 kristaps 117: { "Times-Roman", {
118: { 250 },
119: { 333 },
120: { 408 },
121: { 500 },
122: { 500 },
123: { 833 },
124: { 778 },
125: { 333 },
126: { 333 },
127: { 333 },
128: { 500 },
129: { 564 },
130: { 250 },
131: { 333 },
132: { 250 },
133: { 278 },
134: { 500 },
135: { 500 },
136: { 500 },
137: { 500 },
138: { 500 },
139: { 500 },
140: { 500 },
141: { 500 },
142: { 500 },
143: { 500 },
144: { 278 },
145: { 278 },
146: { 564 },
147: { 564 },
148: { 564 },
149: { 444 },
150: { 921 },
151: { 722 },
152: { 667 },
153: { 667 },
154: { 722 },
155: { 611 },
156: { 556 },
157: { 722 },
158: { 722 },
159: { 333 },
160: { 389 },
161: { 722 },
162: { 611 },
163: { 889 },
164: { 722 },
165: { 722 },
166: { 556 },
167: { 722 },
168: { 667 },
169: { 556 },
170: { 611 },
171: { 722 },
172: { 722 },
173: { 944 },
174: { 722 },
175: { 722 },
176: { 611 },
177: { 333 },
178: { 278 },
179: { 333 },
180: { 469 },
181: { 500 },
182: { 333 },
183: { 444 },
184: { 500 },
185: { 444 },
186: { 500},
187: { 444},
188: { 333},
189: { 500},
190: { 500},
191: { 278},
192: { 278},
193: { 500},
194: { 278},
195: { 778},
196: { 500},
197: { 500},
198: { 500},
199: { 500},
200: { 333},
201: { 389},
202: { 278},
203: { 500},
204: { 500},
205: { 722},
206: { 500},
207: { 500},
208: { 444},
209: { 480},
210: { 200},
211: { 480},
212: { 541},
213: } },
214: { "Times-Bold", {
215: { 250 },
216: { 333 },
217: { 555 },
218: { 500 },
219: { 500 },
220: { 1000 },
221: { 833 },
222: { 333 },
223: { 333 },
224: { 333 },
225: { 500 },
226: { 570 },
227: { 250 },
228: { 333 },
229: { 250 },
230: { 278 },
231: { 500 },
232: { 500 },
233: { 500 },
234: { 500 },
235: { 500 },
236: { 500 },
237: { 500 },
238: { 500 },
239: { 500 },
240: { 500 },
241: { 333 },
242: { 333 },
243: { 570 },
244: { 570 },
245: { 570 },
246: { 500 },
247: { 930 },
248: { 722 },
249: { 667 },
250: { 722 },
251: { 722 },
252: { 667 },
253: { 611 },
254: { 778 },
255: { 778 },
256: { 389 },
257: { 500 },
258: { 778 },
259: { 667 },
260: { 944 },
261: { 722 },
262: { 778 },
263: { 611 },
264: { 778 },
265: { 722 },
266: { 556 },
267: { 667 },
268: { 722 },
269: { 722 },
270: { 1000 },
271: { 722 },
272: { 722 },
273: { 667 },
274: { 333 },
275: { 278 },
276: { 333 },
277: { 581 },
278: { 500 },
279: { 333 },
280: { 500 },
281: { 556 },
282: { 444 },
1.22 kristaps 283: { 556 },
1.24 kristaps 284: { 444 },
285: { 333 },
1.22 kristaps 286: { 500 },
1.24 kristaps 287: { 556 },
1.22 kristaps 288: { 278 },
1.24 kristaps 289: { 333 },
290: { 556 },
1.22 kristaps 291: { 278 },
1.24 kristaps 292: { 833 },
1.22 kristaps 293: { 556 },
1.24 kristaps 294: { 500 },
1.22 kristaps 295: { 556 },
296: { 556 },
297: { 444 },
1.24 kristaps 298: { 389 },
299: { 333 },
1.22 kristaps 300: { 556 },
301: { 500 },
1.24 kristaps 302: { 722 },
303: { 500 },
1.22 kristaps 304: { 500 },
1.24 kristaps 305: { 444 },
306: { 394 },
307: { 220 },
308: { 394 },
309: { 520 },
1.13 kristaps 310: } },
1.24 kristaps 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 },
1.22 kristaps 380: { 500 },
1.24 kristaps 381: { 444 },
382: { 278 },
1.22 kristaps 383: { 500 },
384: { 500 },
385: { 278 },
1.24 kristaps 386: { 278 },
387: { 444 },
388: { 278 },
389: { 722 },
390: { 500 },
391: { 500 },
392: { 500 },
1.22 kristaps 393: { 500 },
1.24 kristaps 394: { 389 },
395: { 389 },
1.22 kristaps 396: { 278 },
397: { 500 },
1.24 kristaps 398: { 444 },
399: { 667 },
400: { 444 },
401: { 444 },
402: { 389 },
403: { 400 },
1.22 kristaps 404: { 275 },
1.24 kristaps 405: { 400 },
406: { 541 },
1.13 kristaps 407: } },
408: };
409:
1.37 kristaps 410: void *
411: pdf_alloc(char *outopts)
412: {
413: struct termp *p;
414:
1.38 kristaps 415: if (NULL != (p = pspdf_alloc(outopts)))
416: p->type = TERMTYPE_PDF;
1.37 kristaps 417:
418: return(p);
419: }
1.2 kristaps 420:
1.1 kristaps 421: void *
1.17 kristaps 422: ps_alloc(char *outopts)
1.1 kristaps 423: {
424: struct termp *p;
1.37 kristaps 425:
1.38 kristaps 426: if (NULL != (p = pspdf_alloc(outopts)))
427: p->type = TERMTYPE_PS;
1.37 kristaps 428:
429: return(p);
430: }
431:
432: static struct termp *
433: pspdf_alloc(char *outopts)
434: {
435: struct termp *p;
1.52 kristaps 436: unsigned int pagex, pagey;
437: size_t marginx, marginy, lineheight;
1.17 kristaps 438: const char *toks[2];
1.25 kristaps 439: const char *pp;
1.17 kristaps 440: char *v;
1.1 kristaps 441:
1.51 kristaps 442: p = mandoc_calloc(1, sizeof(struct termp));
443: p->enc = TERMENC_ASCII;
1.50 kristaps 444: p->ps = mandoc_calloc(1, sizeof(struct termp_ps));
1.1 kristaps 445:
1.19 kristaps 446: p->advance = ps_advance;
1.2 kristaps 447: p->begin = ps_begin;
448: p->end = ps_end;
449: p->endline = ps_endline;
1.19 kristaps 450: p->hspan = ps_hspan;
451: p->letter = ps_letter;
1.11 kristaps 452: p->width = ps_width;
1.21 kristaps 453:
1.17 kristaps 454: toks[0] = "paper";
455: toks[1] = NULL;
456:
1.25 kristaps 457: pp = NULL;
1.19 kristaps 458:
1.17 kristaps 459: while (outopts && *outopts)
460: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
461: case (0):
1.25 kristaps 462: pp = v;
1.17 kristaps 463: break;
464: default:
465: break;
466: }
1.16 kristaps 467:
1.25 kristaps 468: /* Default to US letter (millimetres). */
469:
1.31 kristaps 470: pagex = 216;
471: pagey = 279;
1.25 kristaps 472:
473: /*
474: * The ISO-269 paper sizes can be calculated automatically, but
475: * it would require bringing in -lm for pow() and I'd rather not
476: * do that. So just do it the easy way for now. Since this
477: * only happens once, I'm not terribly concerned.
478: */
479:
480: if (pp && strcasecmp(pp, "letter")) {
481: if (0 == strcasecmp(pp, "a3")) {
482: pagex = 297;
483: pagey = 420;
484: } else if (0 == strcasecmp(pp, "a4")) {
485: pagex = 210;
486: pagey = 297;
487: } else if (0 == strcasecmp(pp, "a5")) {
488: pagex = 148;
489: pagey = 210;
490: } else if (0 == strcasecmp(pp, "legal")) {
491: pagex = 216;
492: pagey = 356;
1.52 kristaps 493: } else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey))
1.25 kristaps 494: fprintf(stderr, "%s: Unknown paper\n", pp);
1.54 schwarze 495: }
1.27 kristaps 496:
497: /*
498: * This MUST be defined before any PNT2AFM or AFM2PNT
499: * calculations occur.
500: */
501:
1.50 kristaps 502: p->ps->scale = 11;
1.27 kristaps 503:
1.25 kristaps 504: /* Remember millimetres -> AFM units. */
505:
506: pagex = PNT2AFM(p, ((double)pagex * 2.834));
507: pagey = PNT2AFM(p, ((double)pagey * 2.834));
508:
1.28 kristaps 509: /* Margins are 1/9 the page x and y. */
1.27 kristaps 510:
1.29 kristaps 511: marginx = /* LINTED */
512: (size_t)((double)pagex / 9.0);
513: marginy = /* LINTED */
514: (size_t)((double)pagey / 9.0);
1.27 kristaps 515:
1.31 kristaps 516: /* Line-height is 1.4em. */
517:
1.50 kristaps 518: lineheight = PNT2AFM(p, ((double)p->ps->scale * 1.4));
1.16 kristaps 519:
1.52 kristaps 520: p->ps->width = (size_t)pagex;
521: p->ps->height = (size_t)pagey;
1.50 kristaps 522: p->ps->header = pagey - (marginy / 2) - (lineheight / 2);
523: p->ps->top = pagey - marginy;
524: p->ps->footer = (marginy / 2) - (lineheight / 2);
525: p->ps->bottom = marginy;
526: p->ps->left = marginx;
527: p->ps->lineheight = lineheight;
1.16 kristaps 528:
1.28 kristaps 529: p->defrmargin = pagex - (marginx * 2);
1.1 kristaps 530: return(p);
531: }
532:
533:
534: void
1.37 kristaps 535: pspdf_free(void *arg)
1.1 kristaps 536: {
1.4 kristaps 537: struct termp *p;
538:
539: p = (struct termp *)arg;
540:
1.50 kristaps 541: if (p->ps->psmarg)
542: free(p->ps->psmarg);
543: if (p->ps->pdfobjs)
544: free(p->ps->pdfobjs);
1.4 kristaps 545:
1.50 kristaps 546: free(p->ps);
1.4 kristaps 547: term_free(p);
548: }
549:
550:
551: static void
552: ps_printf(struct termp *p, const char *fmt, ...)
553: {
554: va_list ap;
1.37 kristaps 555: int pos, len;
1.4 kristaps 556:
557: va_start(ap, fmt);
558:
559: /*
560: * If we're running in regular mode, then pipe directly into
561: * vprintf(). If we're processing margins, then push the data
562: * into our growable margin buffer.
563: */
1.1 kristaps 564:
1.50 kristaps 565: if ( ! (PS_MARGINS & p->ps->flags)) {
1.37 kristaps 566: len = vprintf(fmt, ap);
1.4 kristaps 567: va_end(ap);
1.50 kristaps 568: p->ps->pdfbytes += /* LINTED */
1.37 kristaps 569: len < 0 ? 0 : (size_t)len;
1.4 kristaps 570: return;
571: }
572:
573: /*
574: * XXX: I assume that the in-margin print won't exceed
575: * PS_BUFSLOP (128 bytes), which is reasonable but still an
576: * assumption that will cause pukeage if it's not the case.
577: */
578:
1.40 joerg 579: ps_growbuf(p, PS_BUFSLOP);
1.4 kristaps 580:
1.50 kristaps 581: pos = (int)p->ps->psmargcur;
1.54 schwarze 582: vsnprintf(&p->ps->psmarg[pos], PS_BUFSLOP, fmt, ap);
1.5 kristaps 583:
584: va_end(ap);
1.37 kristaps 585:
1.50 kristaps 586: p->ps->psmargcur = strlen(p->ps->psmarg);
1.4 kristaps 587: }
588:
589:
590: static void
591: ps_putchar(struct termp *p, char c)
592: {
593: int pos;
594:
595: /* See ps_printf(). */
596:
1.50 kristaps 597: if ( ! (PS_MARGINS & p->ps->flags)) {
1.44 kristaps 598: /* LINTED */
1.4 kristaps 599: putchar(c);
1.50 kristaps 600: p->ps->pdfbytes++;
1.4 kristaps 601: return;
602: }
603:
1.40 joerg 604: ps_growbuf(p, 2);
1.4 kristaps 605:
1.50 kristaps 606: pos = (int)p->ps->psmargcur++;
607: p->ps->psmarg[pos++] = c;
608: p->ps->psmarg[pos] = '\0';
1.2 kristaps 609: }
610:
611:
1.37 kristaps 612: static void
1.39 kristaps 613: pdf_obj(struct termp *p, size_t obj)
614: {
615:
616: assert(obj > 0);
617:
1.50 kristaps 618: if ((obj - 1) >= p->ps->pdfobjsz) {
619: p->ps->pdfobjsz = obj + 128;
620: p->ps->pdfobjs = realloc
621: (p->ps->pdfobjs,
622: p->ps->pdfobjsz * sizeof(size_t));
623: if (NULL == p->ps->pdfobjs) {
1.39 kristaps 624: perror(NULL);
1.44 kristaps 625: exit((int)MANDOCLEVEL_SYSERR);
1.39 kristaps 626: }
627: }
628:
1.50 kristaps 629: p->ps->pdfobjs[(int)obj - 1] = p->ps->pdfbytes;
1.39 kristaps 630: ps_printf(p, "%zu 0 obj\n", obj);
631: }
632:
633:
634: static void
1.37 kristaps 635: ps_closepage(struct termp *p)
636: {
637: int i;
1.38 kristaps 638: size_t len, base;
639:
640: /*
641: * Close out a page that we've already flushed to output. In
642: * PostScript, we simply note that the page must be showed. In
643: * PDF, we must now create the Length, Resource, and Page node
644: * for the page contents.
645: */
1.37 kristaps 646:
1.50 kristaps 647: assert(p->ps->psmarg && p->ps->psmarg[0]);
648: ps_printf(p, "%s", p->ps->psmarg);
1.37 kristaps 649:
1.38 kristaps 650: if (TERMTYPE_PS != p->type) {
1.37 kristaps 651: ps_printf(p, "ET\n");
1.38 kristaps 652:
1.50 kristaps 653: len = p->ps->pdfbytes - p->ps->pdflastpg;
654: base = p->ps->pages * 4 + p->ps->pdfbody;
1.38 kristaps 655:
656: ps_printf(p, "endstream\nendobj\n");
657:
658: /* Length of content. */
1.39 kristaps 659: pdf_obj(p, base + 1);
1.38 kristaps 660: ps_printf(p, "%zu\nendobj\n", len);
661:
662: /* Resource for content. */
1.39 kristaps 663: pdf_obj(p, base + 2);
1.38 kristaps 664: ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
1.37 kristaps 665: ps_printf(p, "/Font <<\n");
1.39 kristaps 666: for (i = 0; i < (int)TERMFONT__MAX; i++)
1.37 kristaps 667: ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
1.38 kristaps 668: ps_printf(p, ">>\n>>\n");
669:
670: /* Page node. */
1.39 kristaps 671: pdf_obj(p, base + 3);
672: ps_printf(p, "<<\n");
1.37 kristaps 673: ps_printf(p, "/Type /Page\n");
674: ps_printf(p, "/Parent 2 0 R\n");
1.38 kristaps 675: ps_printf(p, "/Resources %zu 0 R\n", base + 2);
676: ps_printf(p, "/Contents %zu 0 R\n", base);
677: ps_printf(p, ">>\nendobj\n");
678: } else
679: ps_printf(p, "showpage\n");
1.37 kristaps 680:
1.50 kristaps 681: p->ps->pages++;
682: p->ps->psrow = p->ps->top;
683: assert( ! (PS_NEWPAGE & p->ps->flags));
684: p->ps->flags |= PS_NEWPAGE;
1.37 kristaps 685: }
686:
687:
1.3 kristaps 688: /* ARGSUSED */
1.2 kristaps 689: static void
690: ps_end(struct termp *p)
691: {
1.38 kristaps 692: size_t i, xref, base;
1.2 kristaps 693:
1.4 kristaps 694: /*
695: * At the end of the file, do one last showpage. This is the
696: * same behaviour as groff(1) and works for multiple pages as
697: * well as just one.
698: */
699:
1.50 kristaps 700: if ( ! (PS_NEWPAGE & p->ps->flags)) {
701: assert(0 == p->ps->flags);
702: assert('\0' == p->ps->last);
1.37 kristaps 703: ps_closepage(p);
1.30 kristaps 704: }
1.16 kristaps 705:
1.37 kristaps 706: if (TERMTYPE_PS == p->type) {
707: ps_printf(p, "%%%%Trailer\n");
1.50 kristaps 708: ps_printf(p, "%%%%Pages: %zu\n", p->ps->pages);
1.37 kristaps 709: ps_printf(p, "%%%%EOF\n");
710: return;
711: }
712:
1.39 kristaps 713: pdf_obj(p, 2);
714: ps_printf(p, "<<\n/Type /Pages\n");
1.37 kristaps 715: ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
1.50 kristaps 716: (size_t)AFM2PNT(p, p->ps->width),
717: (size_t)AFM2PNT(p, p->ps->height));
1.37 kristaps 718:
1.50 kristaps 719: ps_printf(p, "/Count %zu\n", p->ps->pages);
1.37 kristaps 720: ps_printf(p, "/Kids [");
721:
1.50 kristaps 722: for (i = 0; i < p->ps->pages; i++)
1.38 kristaps 723: ps_printf(p, " %zu 0 R", i * 4 +
1.50 kristaps 724: p->ps->pdfbody + 3);
1.38 kristaps 725:
1.50 kristaps 726: base = (p->ps->pages - 1) * 4 +
727: p->ps->pdfbody + 4;
1.38 kristaps 728:
729: ps_printf(p, "]\n>>\nendobj\n");
1.39 kristaps 730: pdf_obj(p, base);
1.37 kristaps 731: ps_printf(p, "<<\n");
732: ps_printf(p, "/Type /Catalog\n");
733: ps_printf(p, "/Pages 2 0 R\n");
734: ps_printf(p, ">>\n");
1.50 kristaps 735: xref = p->ps->pdfbytes;
1.37 kristaps 736: ps_printf(p, "xref\n");
1.38 kristaps 737: ps_printf(p, "0 %zu\n", base + 1);
1.39 kristaps 738: ps_printf(p, "0000000000 65535 f \n");
739:
740: for (i = 0; i < base; i++)
741: ps_printf(p, "%.10zu 00000 n \n",
1.50 kristaps 742: p->ps->pdfobjs[(int)i]);
1.39 kristaps 743:
1.37 kristaps 744: ps_printf(p, "trailer\n");
745: ps_printf(p, "<<\n");
1.38 kristaps 746: ps_printf(p, "/Size %zu\n", base + 1);
747: ps_printf(p, "/Root %zu 0 R\n", base);
1.37 kristaps 748: ps_printf(p, "/Info 1 0 R\n");
749: ps_printf(p, ">>\n");
750: ps_printf(p, "startxref\n");
751: ps_printf(p, "%zu\n", xref);
752: ps_printf(p, "%%%%EOF\n");
1.2 kristaps 753: }
754:
755:
756: static void
757: ps_begin(struct termp *p)
758: {
1.13 kristaps 759: time_t t;
1.16 kristaps 760: int i;
1.2 kristaps 761:
1.9 kristaps 762: /*
763: * Print margins into margin buffer. Nothing gets output to the
764: * screen yet, so we don't need to initialise the primary state.
1.2 kristaps 765: */
766:
1.50 kristaps 767: if (p->ps->psmarg) {
768: assert(p->ps->psmargsz);
769: p->ps->psmarg[0] = '\0';
1.4 kristaps 770: }
771:
1.50 kristaps 772: /*p->ps->pdfbytes = 0;*/
773: p->ps->psmargcur = 0;
774: p->ps->flags = PS_MARGINS;
775: p->ps->pscol = p->ps->left;
776: p->ps->psrow = p->ps->header;
1.4 kristaps 777:
1.9 kristaps 778: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 779:
780: (*p->headf)(p, p->argf);
781: (*p->endline)(p);
782:
1.50 kristaps 783: p->ps->pscol = p->ps->left;
784: p->ps->psrow = p->ps->footer;
1.4 kristaps 785:
786: (*p->footf)(p, p->argf);
787: (*p->endline)(p);
788:
1.50 kristaps 789: p->ps->flags &= ~PS_MARGINS;
1.9 kristaps 790:
1.50 kristaps 791: assert(0 == p->ps->flags);
792: assert(p->ps->psmarg);
793: assert('\0' != p->ps->psmarg[0]);
1.9 kristaps 794:
795: /*
796: * Print header and initialise page state. Following this,
797: * stuff gets printed to the screen, so make sure we're sane.
798: */
1.4 kristaps 799:
1.13 kristaps 800: t = time(NULL);
801:
1.37 kristaps 802: if (TERMTYPE_PS == p->type) {
803: ps_printf(p, "%%!PS-Adobe-3.0\n");
804: ps_printf(p, "%%%%CreationDate: %s", ctime(&t));
805: ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
806: ps_printf(p, "%%%%Orientation: Portrait\n");
807: ps_printf(p, "%%%%Pages: (atend)\n");
808: ps_printf(p, "%%%%PageOrder: Ascend\n");
809: ps_printf(p, "%%%%DocumentMedia: "
810: "Default %zu %zu 0 () ()\n",
1.50 kristaps 811: (size_t)AFM2PNT(p, p->ps->width),
812: (size_t)AFM2PNT(p, p->ps->height));
1.37 kristaps 813: ps_printf(p, "%%%%DocumentNeededResources: font");
814:
815: for (i = 0; i < (int)TERMFONT__MAX; i++)
816: ps_printf(p, " %s", fonts[i].name);
817:
818: ps_printf(p, "\n%%%%EndComments\n");
819: } else {
820: ps_printf(p, "%%PDF-1.1\n");
1.39 kristaps 821: pdf_obj(p, 1);
1.37 kristaps 822: ps_printf(p, "<<\n");
823: ps_printf(p, ">>\n");
824: ps_printf(p, "endobj\n");
825:
826: for (i = 0; i < (int)TERMFONT__MAX; i++) {
1.39 kristaps 827: pdf_obj(p, (size_t)i + 3);
1.37 kristaps 828: ps_printf(p, "<<\n");
829: ps_printf(p, "/Type /Font\n");
830: ps_printf(p, "/Subtype /Type1\n");
1.55 joerg 831: ps_printf(p, "/Name /F%d\n", i);
1.37 kristaps 832: ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
833: ps_printf(p, ">>\n");
834: }
835: }
1.16 kristaps 836:
1.50 kristaps 837: p->ps->pdfbody = (size_t)TERMFONT__MAX + 3;
838: p->ps->pscol = p->ps->left;
839: p->ps->psrow = p->ps->top;
840: p->ps->flags |= PS_NEWPAGE;
1.33 kristaps 841: ps_setfont(p, TERMFONT_NONE);
1.2 kristaps 842: }
843:
844:
845: static void
1.13 kristaps 846: ps_pletter(struct termp *p, int c)
1.2 kristaps 847: {
1.14 kristaps 848: int f;
1.32 kristaps 849:
1.9 kristaps 850: /*
1.32 kristaps 851: * If we haven't opened a page context, then output that we're
852: * in a new page and make sure the font is correctly set.
1.9 kristaps 853: */
854:
1.50 kristaps 855: if (PS_NEWPAGE & p->ps->flags) {
1.37 kristaps 856: if (TERMTYPE_PS == p->type) {
857: ps_printf(p, "%%%%Page: %zu %zu\n",
1.50 kristaps 858: p->ps->pages + 1,
859: p->ps->pages + 1);
1.37 kristaps 860: ps_printf(p, "/%s %zu selectfont\n",
1.50 kristaps 861: fonts[(int)p->ps->lastf].name,
862: p->ps->scale);
1.37 kristaps 863: } else {
1.50 kristaps 864: pdf_obj(p, p->ps->pdfbody +
865: p->ps->pages * 4);
1.37 kristaps 866: ps_printf(p, "<<\n");
867: ps_printf(p, "/Length %zu 0 R\n",
1.50 kristaps 868: p->ps->pdfbody + 1 +
869: p->ps->pages * 4);
1.37 kristaps 870: ps_printf(p, ">>\nstream\n");
871: }
1.50 kristaps 872: p->ps->pdflastpg = p->ps->pdfbytes;
873: p->ps->flags &= ~PS_NEWPAGE;
1.32 kristaps 874: }
875:
876: /*
877: * If we're not in a PostScript "word" context, then open one
878: * now at the current cursor.
879: */
1.30 kristaps 880:
1.50 kristaps 881: if ( ! (PS_INLINE & p->ps->flags)) {
1.37 kristaps 882: if (TERMTYPE_PS != p->type) {
883: ps_printf(p, "BT\n/F%d %zu Tf\n",
1.50 kristaps 884: (int)p->ps->lastf,
885: p->ps->scale);
1.37 kristaps 886: ps_printf(p, "%.3f %.3f Td\n(",
1.50 kristaps 887: AFM2PNT(p, p->ps->pscol),
888: AFM2PNT(p, p->ps->psrow));
1.37 kristaps 889: } else
890: ps_printf(p, "%.3f %.3f moveto\n(",
1.50 kristaps 891: AFM2PNT(p, p->ps->pscol),
892: AFM2PNT(p, p->ps->psrow));
893: p->ps->flags |= PS_INLINE;
1.2 kristaps 894: }
895:
1.50 kristaps 896: assert( ! (PS_NEWPAGE & p->ps->flags));
1.29 kristaps 897:
1.2 kristaps 898: /*
899: * We need to escape these characters as per the PostScript
900: * specification. We would also escape non-graphable characters
901: * (like tabs), but none of them would get to this point and
902: * it's superfluous to abort() on them.
903: */
904:
905: switch (c) {
906: case ('('):
907: /* FALLTHROUGH */
908: case (')'):
909: /* FALLTHROUGH */
910: case ('\\'):
1.4 kristaps 911: ps_putchar(p, '\\');
1.2 kristaps 912: break;
913: default:
914: break;
915: }
916:
917: /* Write the character and adjust where we are on the page. */
1.9 kristaps 918:
1.50 kristaps 919: f = (int)p->ps->lastf;
1.13 kristaps 920:
1.45 schwarze 921: if (c <= 32 || (c - 32 >= MAXCHAR)) {
1.13 kristaps 922: ps_putchar(p, ' ');
1.50 kristaps 923: p->ps->pscol += (size_t)fonts[f].gly[0].wx;
1.13 kristaps 924: return;
925: }
926:
1.25 kristaps 927: ps_putchar(p, (char)c);
1.14 kristaps 928: c -= 32;
1.50 kristaps 929: p->ps->pscol += (size_t)fonts[f].gly[c].wx;
1.2 kristaps 930: }
931:
932:
933: static void
1.9 kristaps 934: ps_pclose(struct termp *p)
935: {
936:
937: /*
938: * Spit out that we're exiting a word context (this is a
939: * "partial close" because we don't check the last-char buffer
940: * or anything).
941: */
942:
1.50 kristaps 943: if ( ! (PS_INLINE & p->ps->flags))
1.9 kristaps 944: return;
945:
1.37 kristaps 946: if (TERMTYPE_PS != p->type) {
947: ps_printf(p, ") Tj\nET\n");
948: } else
949: ps_printf(p, ") show\n");
950:
1.50 kristaps 951: p->ps->flags &= ~PS_INLINE;
1.9 kristaps 952: }
953:
954:
955: static void
956: ps_fclose(struct termp *p)
957: {
958:
959: /*
960: * Strong closure: if we have a last-char, spit it out after
961: * checking that we're in the right font mode. This will of
962: * course open a new scope, if applicable.
963: *
964: * Following this, close out any scope that's open.
965: */
966:
1.50 kristaps 967: if ('\0' != p->ps->last) {
968: if (p->ps->lastf != TERMFONT_NONE) {
1.9 kristaps 969: ps_pclose(p);
970: ps_setfont(p, TERMFONT_NONE);
971: }
1.50 kristaps 972: ps_pletter(p, p->ps->last);
973: p->ps->last = '\0';
1.9 kristaps 974: }
975:
1.50 kristaps 976: if ( ! (PS_INLINE & p->ps->flags))
1.9 kristaps 977: return;
978:
979: ps_pclose(p);
980: }
981:
982:
983: static void
1.49 kristaps 984: ps_letter(struct termp *p, int arg)
1.8 kristaps 985: {
1.49 kristaps 986: char cc, c;
987:
988: /* LINTED */
989: c = arg >= 128 || arg <= 0 ? '?' : arg;
1.9 kristaps 990:
991: /*
992: * State machine dictates whether to buffer the last character
993: * or not. Basically, encoded words are detected by checking if
994: * we're an "8" and switching on the buffer. Then we put "8" in
995: * our buffer, and on the next charater, flush both character
996: * and buffer. Thus, "regular" words are detected by having a
997: * regular character and a regular buffer character.
998: */
999:
1.50 kristaps 1000: if ('\0' == p->ps->last) {
1.8 kristaps 1001: assert(8 != c);
1.50 kristaps 1002: p->ps->last = c;
1.8 kristaps 1003: return;
1.50 kristaps 1004: } else if (8 == p->ps->last) {
1.8 kristaps 1005: assert(8 != c);
1.50 kristaps 1006: p->ps->last = '\0';
1.8 kristaps 1007: } else if (8 == c) {
1.50 kristaps 1008: assert(8 != p->ps->last);
1009: if ('_' == p->ps->last) {
1010: if (p->ps->lastf != TERMFONT_UNDER) {
1.9 kristaps 1011: ps_pclose(p);
1012: ps_setfont(p, TERMFONT_UNDER);
1013: }
1.50 kristaps 1014: } else if (p->ps->lastf != TERMFONT_BOLD) {
1.9 kristaps 1015: ps_pclose(p);
1016: ps_setfont(p, TERMFONT_BOLD);
1017: }
1.50 kristaps 1018: p->ps->last = c;
1.8 kristaps 1019: return;
1020: } else {
1.50 kristaps 1021: if (p->ps->lastf != TERMFONT_NONE) {
1.9 kristaps 1022: ps_pclose(p);
1023: ps_setfont(p, TERMFONT_NONE);
1024: }
1.50 kristaps 1025: cc = p->ps->last;
1026: p->ps->last = c;
1.8 kristaps 1027: c = cc;
1028: }
1029:
1.9 kristaps 1030: ps_pletter(p, c);
1.8 kristaps 1031: }
1032:
1033:
1034: static void
1.2 kristaps 1035: ps_advance(struct termp *p, size_t len)
1036: {
1037:
1.9 kristaps 1038: /*
1039: * Advance some spaces. This can probably be made smarter,
1040: * i.e., to have multiple space-separated words in the same
1041: * scope, but this is easier: just close out the current scope
1042: * and readjust our column settings.
1043: */
1.8 kristaps 1044:
1.9 kristaps 1045: ps_fclose(p);
1.50 kristaps 1046: p->ps->pscol += len;
1.2 kristaps 1047: }
1048:
1049:
1050: static void
1051: ps_endline(struct termp *p)
1052: {
1.8 kristaps 1053:
1.9 kristaps 1054: /* Close out any scopes we have open: we're at eoln. */
1055:
1056: ps_fclose(p);
1.2 kristaps 1057:
1.9 kristaps 1058: /*
1059: * If we're in the margin, don't try to recalculate our current
1060: * row. XXX: if the column tries to be fancy with multiple
1061: * lines, we'll do nasty stuff.
1062: */
1.2 kristaps 1063:
1.50 kristaps 1064: if (PS_MARGINS & p->ps->flags)
1.29 kristaps 1065: return;
1066:
1067: /* Left-justify. */
1068:
1.50 kristaps 1069: p->ps->pscol = p->ps->left;
1.29 kristaps 1070:
1071: /* If we haven't printed anything, return. */
1072:
1.50 kristaps 1073: if (PS_NEWPAGE & p->ps->flags)
1.2 kristaps 1074: return;
1075:
1.9 kristaps 1076: /*
1077: * Put us down a line. If we're at the page bottom, spit out a
1078: * showpage and restart our row.
1079: */
1080:
1.50 kristaps 1081: if (p->ps->psrow >= p->ps->lineheight +
1082: p->ps->bottom) {
1083: p->ps->psrow -= p->ps->lineheight;
1.2 kristaps 1084: return;
1085: }
1086:
1.37 kristaps 1087: ps_closepage(p);
1.1 kristaps 1088: }
1.9 kristaps 1089:
1090:
1091: static void
1092: ps_setfont(struct termp *p, enum termfont f)
1093: {
1094:
1.16 kristaps 1095: assert(f < TERMFONT__MAX);
1.50 kristaps 1096: p->ps->lastf = f;
1.32 kristaps 1097:
1098: /*
1099: * If we're still at the top of the page, let the font-setting
1100: * be delayed until we actually have stuff to print.
1101: */
1102:
1.50 kristaps 1103: if (PS_NEWPAGE & p->ps->flags)
1.32 kristaps 1104: return;
1105:
1.37 kristaps 1106: if (TERMTYPE_PS == p->type)
1107: ps_printf(p, "/%s %zu selectfont\n",
1108: fonts[(int)f].name,
1.50 kristaps 1109: p->ps->scale);
1.37 kristaps 1110: else
1111: ps_printf(p, "/F%d %zu Tf\n",
1112: (int)f,
1.50 kristaps 1113: p->ps->scale);
1.9 kristaps 1114: }
1115:
1.11 kristaps 1116:
1.12 kristaps 1117: /* ARGSUSED */
1.11 kristaps 1118: static size_t
1.49 kristaps 1119: ps_width(const struct termp *p, int c)
1.11 kristaps 1120: {
1121:
1.14 kristaps 1122: if (c <= 32 || c - 32 >= MAXCHAR)
1.36 kristaps 1123: return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14 kristaps 1124:
1125: c -= 32;
1.49 kristaps 1126: return((size_t)fonts[(int)TERMFONT_NONE].gly[c].wx);
1.11 kristaps 1127: }
1.19 kristaps 1128:
1129:
1.20 kristaps 1130: static double
1.19 kristaps 1131: ps_hspan(const struct termp *p, const struct roffsu *su)
1132: {
1133: double r;
1134:
1135: /*
1136: * All of these measurements are derived by converting from the
1.21 kristaps 1137: * native measurement to AFM units.
1.19 kristaps 1138: */
1139:
1140: switch (su->unit) {
1141: case (SCALE_CM):
1.21 kristaps 1142: r = PNT2AFM(p, su->scale * 28.34);
1.19 kristaps 1143: break;
1144: case (SCALE_IN):
1.21 kristaps 1145: r = PNT2AFM(p, su->scale * 72);
1.19 kristaps 1146: break;
1147: case (SCALE_PC):
1.21 kristaps 1148: r = PNT2AFM(p, su->scale * 12);
1.19 kristaps 1149: break;
1150: case (SCALE_PT):
1.21 kristaps 1151: r = PNT2AFM(p, su->scale * 100);
1.19 kristaps 1152: break;
1153: case (SCALE_EM):
1.36 kristaps 1154: r = su->scale *
1.19 kristaps 1155: fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
1156: break;
1157: case (SCALE_MM):
1.21 kristaps 1158: r = PNT2AFM(p, su->scale * 2.834);
1.19 kristaps 1159: break;
1160: case (SCALE_EN):
1.36 kristaps 1161: r = su->scale *
1.19 kristaps 1162: fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
1163: break;
1164: case (SCALE_VS):
1.50 kristaps 1165: r = su->scale * p->ps->lineheight;
1.19 kristaps 1166: break;
1167: default:
1168: r = su->scale;
1169: break;
1170: }
1171:
1.20 kristaps 1172: return(r);
1.50 kristaps 1173: }
1174:
1175: static void
1176: ps_growbuf(struct termp *p, size_t sz)
1177: {
1178: if (p->ps->psmargcur + sz <= p->ps->psmargsz)
1179: return;
1180:
1181: if (sz < PS_BUFSLOP)
1182: sz = PS_BUFSLOP;
1183:
1184: p->ps->psmargsz += sz;
1185:
1186: p->ps->psmarg = mandoc_realloc
1187: (p->ps->psmarg, p->ps->psmargsz);
1.19 kristaps 1188: }
1189:
CVSweb