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