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