]> git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
Turn the non-trivial PS_GROWBUF macro into a function. Don't use MAX, it
[mandoc.git] / term_ps.c
1 /* $Id: term_ps.c,v 1.40 2010/08/01 15:46:18 joerg 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
357 static void
358 ps_growbuf(struct termp *p, size_t sz)
359 {
360 if (p->engine.ps.psmargcur + sz <= p->engine.ps.psmargsz)
361 return;
362
363 if (sz < PS_BUFSLOP)
364 sz = PS_BUFSLOP;
365 p->engine.ps.psmargsz += sz;
366
367 p->engine.ps.psmarg = realloc(p->engine.ps.psmarg,
368 p->engine.ps.psmargsz);
369 if (NULL == p->engine.ps.psmarg)
370 perror(NULL);
371 exit(EXIT_FAILURE);
372 }
373
374 static double ps_hspan(const struct termp *,
375 const struct roffsu *);
376 static size_t ps_width(const struct termp *, char);
377 static void ps_advance(struct termp *, size_t);
378 static void ps_begin(struct termp *);
379 static void ps_closepage(struct termp *);
380 static void ps_end(struct termp *);
381 static void ps_endline(struct termp *);
382 static void ps_fclose(struct termp *);
383 static void ps_letter(struct termp *, char);
384 static void ps_pclose(struct termp *);
385 static void ps_pletter(struct termp *, int);
386 static void ps_printf(struct termp *, const char *, ...);
387 static void ps_putchar(struct termp *, char);
388 static void ps_setfont(struct termp *, enum termfont);
389 static struct termp *pspdf_alloc(char *);
390 static void pdf_obj(struct termp *, size_t);
391
392
393 void *
394 pdf_alloc(char *outopts)
395 {
396 struct termp *p;
397
398 if (NULL != (p = pspdf_alloc(outopts)))
399 p->type = TERMTYPE_PDF;
400
401 return(p);
402 }
403
404
405 void *
406 ps_alloc(char *outopts)
407 {
408 struct termp *p;
409
410 if (NULL != (p = pspdf_alloc(outopts)))
411 p->type = TERMTYPE_PS;
412
413 return(p);
414 }
415
416
417 static struct termp *
418 pspdf_alloc(char *outopts)
419 {
420 struct termp *p;
421 size_t pagex, pagey, marginx, marginy, lineheight;
422 const char *toks[2];
423 const char *pp;
424 char *v;
425
426 if (NULL == (p = term_alloc(TERMENC_ASCII)))
427 return(NULL);
428
429 p->advance = ps_advance;
430 p->begin = ps_begin;
431 p->end = ps_end;
432 p->endline = ps_endline;
433 p->hspan = ps_hspan;
434 p->letter = ps_letter;
435 p->width = ps_width;
436
437 toks[0] = "paper";
438 toks[1] = NULL;
439
440 pp = NULL;
441
442 while (outopts && *outopts)
443 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
444 case (0):
445 pp = v;
446 break;
447 default:
448 break;
449 }
450
451 /* Default to US letter (millimetres). */
452
453 pagex = 216;
454 pagey = 279;
455
456 /*
457 * The ISO-269 paper sizes can be calculated automatically, but
458 * it would require bringing in -lm for pow() and I'd rather not
459 * do that. So just do it the easy way for now. Since this
460 * only happens once, I'm not terribly concerned.
461 */
462
463 if (pp && strcasecmp(pp, "letter")) {
464 if (0 == strcasecmp(pp, "a3")) {
465 pagex = 297;
466 pagey = 420;
467 } else if (0 == strcasecmp(pp, "a4")) {
468 pagex = 210;
469 pagey = 297;
470 } else if (0 == strcasecmp(pp, "a5")) {
471 pagex = 148;
472 pagey = 210;
473 } else if (0 == strcasecmp(pp, "legal")) {
474 pagex = 216;
475 pagey = 356;
476 } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
477 fprintf(stderr, "%s: Unknown paper\n", pp);
478 } else if (NULL == pp)
479 pp = "letter";
480
481 /*
482 * This MUST be defined before any PNT2AFM or AFM2PNT
483 * calculations occur.
484 */
485
486 p->engine.ps.scale = 11;
487
488 /* Remember millimetres -> AFM units. */
489
490 pagex = PNT2AFM(p, ((double)pagex * 2.834));
491 pagey = PNT2AFM(p, ((double)pagey * 2.834));
492
493 /* Margins are 1/9 the page x and y. */
494
495 marginx = /* LINTED */
496 (size_t)((double)pagex / 9.0);
497 marginy = /* LINTED */
498 (size_t)((double)pagey / 9.0);
499
500 /* Line-height is 1.4em. */
501
502 lineheight = PNT2AFM(p, ((double)p->engine.ps.scale * 1.4));
503
504 p->engine.ps.width = pagex;
505 p->engine.ps.height = pagey;
506 p->engine.ps.header = pagey - (marginy / 2) - (lineheight / 2);
507 p->engine.ps.top = pagey - marginy;
508 p->engine.ps.footer = (marginy / 2) - (lineheight / 2);
509 p->engine.ps.bottom = marginy;
510 p->engine.ps.left = marginx;
511 p->engine.ps.lineheight = lineheight;
512
513 p->defrmargin = pagex - (marginx * 2);
514 return(p);
515 }
516
517
518 void
519 pspdf_free(void *arg)
520 {
521 struct termp *p;
522
523 p = (struct termp *)arg;
524
525 if (p->engine.ps.psmarg)
526 free(p->engine.ps.psmarg);
527 if (p->engine.ps.pdfobjs)
528 free(p->engine.ps.pdfobjs);
529
530 term_free(p);
531 }
532
533
534 static void
535 ps_printf(struct termp *p, const char *fmt, ...)
536 {
537 va_list ap;
538 int pos, len;
539
540 va_start(ap, fmt);
541
542 /*
543 * If we're running in regular mode, then pipe directly into
544 * vprintf(). If we're processing margins, then push the data
545 * into our growable margin buffer.
546 */
547
548 if ( ! (PS_MARGINS & p->engine.ps.flags)) {
549 len = vprintf(fmt, ap);
550 va_end(ap);
551 p->engine.ps.pdfbytes += /* LINTED */
552 len < 0 ? 0 : (size_t)len;
553 return;
554 }
555
556 /*
557 * XXX: I assume that the in-margin print won't exceed
558 * PS_BUFSLOP (128 bytes), which is reasonable but still an
559 * assumption that will cause pukeage if it's not the case.
560 */
561
562 ps_growbuf(p, PS_BUFSLOP);
563
564 pos = (int)p->engine.ps.psmargcur;
565 len = vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
566
567 va_end(ap);
568
569 p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
570 }
571
572
573 static void
574 ps_putchar(struct termp *p, char c)
575 {
576 int pos;
577
578 /* See ps_printf(). */
579
580 if ( ! (PS_MARGINS & p->engine.ps.flags)) {
581 putchar(c);
582 p->engine.ps.pdfbytes++;
583 return;
584 }
585
586 ps_growbuf(p, 2);
587
588 pos = (int)p->engine.ps.psmargcur++;
589 p->engine.ps.psmarg[pos++] = c;
590 p->engine.ps.psmarg[pos] = '\0';
591 }
592
593
594 static void
595 pdf_obj(struct termp *p, size_t obj)
596 {
597
598 assert(obj > 0);
599
600 if ((obj - 1) >= p->engine.ps.pdfobjsz) {
601 p->engine.ps.pdfobjsz = obj + 128;
602 p->engine.ps.pdfobjs = realloc
603 (p->engine.ps.pdfobjs,
604 p->engine.ps.pdfobjsz * sizeof(size_t));
605 if (NULL == p->engine.ps.pdfobjs) {
606 perror(NULL);
607 exit(EXIT_FAILURE);
608 }
609 }
610
611 p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
612 ps_printf(p, "%zu 0 obj\n", obj);
613 }
614
615
616 static void
617 ps_closepage(struct termp *p)
618 {
619 int i;
620 size_t len, base;
621
622 /*
623 * Close out a page that we've already flushed to output. In
624 * PostScript, we simply note that the page must be showed. In
625 * PDF, we must now create the Length, Resource, and Page node
626 * for the page contents.
627 */
628
629 assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
630 ps_printf(p, "%s", p->engine.ps.psmarg);
631
632 if (TERMTYPE_PS != p->type) {
633 ps_printf(p, "ET\n");
634
635 len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
636 base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
637
638 ps_printf(p, "endstream\nendobj\n");
639
640 /* Length of content. */
641 pdf_obj(p, base + 1);
642 ps_printf(p, "%zu\nendobj\n", len);
643
644 /* Resource for content. */
645 pdf_obj(p, base + 2);
646 ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
647 ps_printf(p, "/Font <<\n");
648 for (i = 0; i < (int)TERMFONT__MAX; i++)
649 ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
650 ps_printf(p, ">>\n>>\n");
651
652 /* Page node. */
653 pdf_obj(p, base + 3);
654 ps_printf(p, "<<\n");
655 ps_printf(p, "/Type /Page\n");
656 ps_printf(p, "/Parent 2 0 R\n");
657 ps_printf(p, "/Resources %zu 0 R\n", base + 2);
658 ps_printf(p, "/Contents %zu 0 R\n", base);
659 ps_printf(p, ">>\nendobj\n");
660 } else
661 ps_printf(p, "showpage\n");
662
663 p->engine.ps.pages++;
664 p->engine.ps.psrow = p->engine.ps.top;
665 assert( ! (PS_NEWPAGE & p->engine.ps.flags));
666 p->engine.ps.flags |= PS_NEWPAGE;
667 }
668
669
670 /* ARGSUSED */
671 static void
672 ps_end(struct termp *p)
673 {
674 size_t i, xref, base;
675
676 /*
677 * At the end of the file, do one last showpage. This is the
678 * same behaviour as groff(1) and works for multiple pages as
679 * well as just one.
680 */
681
682 if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
683 assert(0 == p->engine.ps.flags);
684 assert('\0' == p->engine.ps.last);
685 ps_closepage(p);
686 }
687
688 if (TERMTYPE_PS == p->type) {
689 ps_printf(p, "%%%%Trailer\n");
690 ps_printf(p, "%%%%Pages: %zu\n", p->engine.ps.pages);
691 ps_printf(p, "%%%%EOF\n");
692 return;
693 }
694
695 pdf_obj(p, 2);
696 ps_printf(p, "<<\n/Type /Pages\n");
697 ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
698 (size_t)AFM2PNT(p, p->engine.ps.width),
699 (size_t)AFM2PNT(p, p->engine.ps.height));
700
701 ps_printf(p, "/Count %zu\n", p->engine.ps.pages);
702 ps_printf(p, "/Kids [");
703
704 for (i = 0; i < p->engine.ps.pages; i++)
705 ps_printf(p, " %zu 0 R", i * 4 +
706 p->engine.ps.pdfbody + 3);
707
708 base = (p->engine.ps.pages - 1) * 4 +
709 p->engine.ps.pdfbody + 4;
710
711 ps_printf(p, "]\n>>\nendobj\n");
712 pdf_obj(p, base);
713 ps_printf(p, "<<\n");
714 ps_printf(p, "/Type /Catalog\n");
715 ps_printf(p, "/Pages 2 0 R\n");
716 ps_printf(p, ">>\n");
717 xref = p->engine.ps.pdfbytes;
718 ps_printf(p, "xref\n");
719 ps_printf(p, "0 %zu\n", base + 1);
720 ps_printf(p, "0000000000 65535 f \n");
721
722 for (i = 0; i < base; i++)
723 ps_printf(p, "%.10zu 00000 n \n",
724 p->engine.ps.pdfobjs[(int)i]);
725
726 ps_printf(p, "trailer\n");
727 ps_printf(p, "<<\n");
728 ps_printf(p, "/Size %zu\n", base + 1);
729 ps_printf(p, "/Root %zu 0 R\n", base);
730 ps_printf(p, "/Info 1 0 R\n");
731 ps_printf(p, ">>\n");
732 ps_printf(p, "startxref\n");
733 ps_printf(p, "%zu\n", xref);
734 ps_printf(p, "%%%%EOF\n");
735 }
736
737
738 static void
739 ps_begin(struct termp *p)
740 {
741 time_t t;
742 int i;
743
744 /*
745 * Print margins into margin buffer. Nothing gets output to the
746 * screen yet, so we don't need to initialise the primary state.
747 */
748
749 if (p->engine.ps.psmarg) {
750 assert(p->engine.ps.psmargsz);
751 p->engine.ps.psmarg[0] = '\0';
752 }
753
754 /*p->engine.ps.pdfbytes = 0;*/
755 p->engine.ps.psmargcur = 0;
756 p->engine.ps.flags = PS_MARGINS;
757 p->engine.ps.pscol = p->engine.ps.left;
758 p->engine.ps.psrow = p->engine.ps.header;
759
760 ps_setfont(p, TERMFONT_NONE);
761
762 (*p->headf)(p, p->argf);
763 (*p->endline)(p);
764
765 p->engine.ps.pscol = p->engine.ps.left;
766 p->engine.ps.psrow = p->engine.ps.footer;
767
768 (*p->footf)(p, p->argf);
769 (*p->endline)(p);
770
771 p->engine.ps.flags &= ~PS_MARGINS;
772
773 assert(0 == p->engine.ps.flags);
774 assert(p->engine.ps.psmarg);
775 assert('\0' != p->engine.ps.psmarg[0]);
776
777 /*
778 * Print header and initialise page state. Following this,
779 * stuff gets printed to the screen, so make sure we're sane.
780 */
781
782 t = time(NULL);
783
784 if (TERMTYPE_PS == p->type) {
785 ps_printf(p, "%%!PS-Adobe-3.0\n");
786 ps_printf(p, "%%%%Creator: mandoc-%s\n", VERSION);
787 ps_printf(p, "%%%%CreationDate: %s", ctime(&t));
788 ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
789 ps_printf(p, "%%%%Orientation: Portrait\n");
790 ps_printf(p, "%%%%Pages: (atend)\n");
791 ps_printf(p, "%%%%PageOrder: Ascend\n");
792 ps_printf(p, "%%%%DocumentMedia: "
793 "Default %zu %zu 0 () ()\n",
794 (size_t)AFM2PNT(p, p->engine.ps.width),
795 (size_t)AFM2PNT(p, p->engine.ps.height));
796 ps_printf(p, "%%%%DocumentNeededResources: font");
797
798 for (i = 0; i < (int)TERMFONT__MAX; i++)
799 ps_printf(p, " %s", fonts[i].name);
800
801 ps_printf(p, "\n%%%%EndComments\n");
802 } else {
803 ps_printf(p, "%%PDF-1.1\n");
804 pdf_obj(p, 1);
805 ps_printf(p, "<<\n");
806 ps_printf(p, "/Creator mandoc-%s\n", VERSION);
807 ps_printf(p, ">>\n");
808 ps_printf(p, "endobj\n");
809
810 for (i = 0; i < (int)TERMFONT__MAX; i++) {
811 pdf_obj(p, (size_t)i + 3);
812 ps_printf(p, "<<\n");
813 ps_printf(p, "/Type /Font\n");
814 ps_printf(p, "/Subtype /Type1\n");
815 ps_printf(p, "/Name /F%zu\n", i);
816 ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
817 ps_printf(p, ">>\n");
818 }
819 }
820
821 p->engine.ps.pdfbody = (size_t)TERMFONT__MAX + 3;
822 p->engine.ps.pscol = p->engine.ps.left;
823 p->engine.ps.psrow = p->engine.ps.top;
824 p->engine.ps.flags |= PS_NEWPAGE;
825 ps_setfont(p, TERMFONT_NONE);
826 }
827
828
829 static void
830 ps_pletter(struct termp *p, int c)
831 {
832 int f;
833
834 /*
835 * If we haven't opened a page context, then output that we're
836 * in a new page and make sure the font is correctly set.
837 */
838
839 if (PS_NEWPAGE & p->engine.ps.flags) {
840 if (TERMTYPE_PS == p->type) {
841 ps_printf(p, "%%%%Page: %zu %zu\n",
842 p->engine.ps.pages + 1,
843 p->engine.ps.pages + 1);
844 ps_printf(p, "/%s %zu selectfont\n",
845 fonts[(int)p->engine.ps.lastf].name,
846 p->engine.ps.scale);
847 } else {
848 pdf_obj(p, p->engine.ps.pdfbody +
849 p->engine.ps.pages * 4);
850 ps_printf(p, "<<\n");
851 ps_printf(p, "/Length %zu 0 R\n",
852 p->engine.ps.pdfbody + 1 +
853 p->engine.ps.pages * 4);
854 ps_printf(p, ">>\nstream\n");
855 }
856 p->engine.ps.pdflastpg = p->engine.ps.pdfbytes;
857 p->engine.ps.flags &= ~PS_NEWPAGE;
858 }
859
860 /*
861 * If we're not in a PostScript "word" context, then open one
862 * now at the current cursor.
863 */
864
865 if ( ! (PS_INLINE & p->engine.ps.flags)) {
866 if (TERMTYPE_PS != p->type) {
867 ps_printf(p, "BT\n/F%d %zu Tf\n",
868 (int)p->engine.ps.lastf,
869 p->engine.ps.scale);
870 ps_printf(p, "%.3f %.3f Td\n(",
871 AFM2PNT(p, p->engine.ps.pscol),
872 AFM2PNT(p, p->engine.ps.psrow));
873 } else
874 ps_printf(p, "%.3f %.3f moveto\n(",
875 AFM2PNT(p, p->engine.ps.pscol),
876 AFM2PNT(p, p->engine.ps.psrow));
877 p->engine.ps.flags |= PS_INLINE;
878 }
879
880 assert( ! (PS_NEWPAGE & p->engine.ps.flags));
881
882 /*
883 * We need to escape these characters as per the PostScript
884 * specification. We would also escape non-graphable characters
885 * (like tabs), but none of them would get to this point and
886 * it's superfluous to abort() on them.
887 */
888
889 switch (c) {
890 case ('('):
891 /* FALLTHROUGH */
892 case (')'):
893 /* FALLTHROUGH */
894 case ('\\'):
895 ps_putchar(p, '\\');
896 break;
897 default:
898 break;
899 }
900
901 /* Write the character and adjust where we are on the page. */
902
903 f = (int)p->engine.ps.lastf;
904
905 if (c <= 32 || (c - 32 > MAXCHAR)) {
906 ps_putchar(p, ' ');
907 p->engine.ps.pscol += (size_t)fonts[f].gly[0].wx;
908 return;
909 }
910
911 ps_putchar(p, (char)c);
912 c -= 32;
913 p->engine.ps.pscol += (size_t)fonts[f].gly[c].wx;
914 }
915
916
917 static void
918 ps_pclose(struct termp *p)
919 {
920
921 /*
922 * Spit out that we're exiting a word context (this is a
923 * "partial close" because we don't check the last-char buffer
924 * or anything).
925 */
926
927 if ( ! (PS_INLINE & p->engine.ps.flags))
928 return;
929
930 if (TERMTYPE_PS != p->type) {
931 ps_printf(p, ") Tj\nET\n");
932 } else
933 ps_printf(p, ") show\n");
934
935 p->engine.ps.flags &= ~PS_INLINE;
936 }
937
938
939 static void
940 ps_fclose(struct termp *p)
941 {
942
943 /*
944 * Strong closure: if we have a last-char, spit it out after
945 * checking that we're in the right font mode. This will of
946 * course open a new scope, if applicable.
947 *
948 * Following this, close out any scope that's open.
949 */
950
951 if ('\0' != p->engine.ps.last) {
952 if (p->engine.ps.lastf != TERMFONT_NONE) {
953 ps_pclose(p);
954 ps_setfont(p, TERMFONT_NONE);
955 }
956 ps_pletter(p, p->engine.ps.last);
957 p->engine.ps.last = '\0';
958 }
959
960 if ( ! (PS_INLINE & p->engine.ps.flags))
961 return;
962
963 ps_pclose(p);
964 }
965
966
967 static void
968 ps_letter(struct termp *p, char c)
969 {
970 char cc;
971
972 /*
973 * State machine dictates whether to buffer the last character
974 * or not. Basically, encoded words are detected by checking if
975 * we're an "8" and switching on the buffer. Then we put "8" in
976 * our buffer, and on the next charater, flush both character
977 * and buffer. Thus, "regular" words are detected by having a
978 * regular character and a regular buffer character.
979 */
980
981 if ('\0' == p->engine.ps.last) {
982 assert(8 != c);
983 p->engine.ps.last = c;
984 return;
985 } else if (8 == p->engine.ps.last) {
986 assert(8 != c);
987 p->engine.ps.last = '\0';
988 } else if (8 == c) {
989 assert(8 != p->engine.ps.last);
990 if ('_' == p->engine.ps.last) {
991 if (p->engine.ps.lastf != TERMFONT_UNDER) {
992 ps_pclose(p);
993 ps_setfont(p, TERMFONT_UNDER);
994 }
995 } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
996 ps_pclose(p);
997 ps_setfont(p, TERMFONT_BOLD);
998 }
999 p->engine.ps.last = c;
1000 return;
1001 } else {
1002 if (p->engine.ps.lastf != TERMFONT_NONE) {
1003 ps_pclose(p);
1004 ps_setfont(p, TERMFONT_NONE);
1005 }
1006 cc = p->engine.ps.last;
1007 p->engine.ps.last = c;
1008 c = cc;
1009 }
1010
1011 ps_pletter(p, c);
1012 }
1013
1014
1015 static void
1016 ps_advance(struct termp *p, size_t len)
1017 {
1018
1019 /*
1020 * Advance some spaces. This can probably be made smarter,
1021 * i.e., to have multiple space-separated words in the same
1022 * scope, but this is easier: just close out the current scope
1023 * and readjust our column settings.
1024 */
1025
1026 ps_fclose(p);
1027 p->engine.ps.pscol += len;
1028 }
1029
1030
1031 static void
1032 ps_endline(struct termp *p)
1033 {
1034
1035 /* Close out any scopes we have open: we're at eoln. */
1036
1037 ps_fclose(p);
1038
1039 /*
1040 * If we're in the margin, don't try to recalculate our current
1041 * row. XXX: if the column tries to be fancy with multiple
1042 * lines, we'll do nasty stuff.
1043 */
1044
1045 if (PS_MARGINS & p->engine.ps.flags)
1046 return;
1047
1048 /* Left-justify. */
1049
1050 p->engine.ps.pscol = p->engine.ps.left;
1051
1052 /* If we haven't printed anything, return. */
1053
1054 if (PS_NEWPAGE & p->engine.ps.flags)
1055 return;
1056
1057 /*
1058 * Put us down a line. If we're at the page bottom, spit out a
1059 * showpage and restart our row.
1060 */
1061
1062 if (p->engine.ps.psrow >= p->engine.ps.lineheight +
1063 p->engine.ps.bottom) {
1064 p->engine.ps.psrow -= p->engine.ps.lineheight;
1065 return;
1066 }
1067
1068 ps_closepage(p);
1069 }
1070
1071
1072 static void
1073 ps_setfont(struct termp *p, enum termfont f)
1074 {
1075
1076 assert(f < TERMFONT__MAX);
1077 p->engine.ps.lastf = f;
1078
1079 /*
1080 * If we're still at the top of the page, let the font-setting
1081 * be delayed until we actually have stuff to print.
1082 */
1083
1084 if (PS_NEWPAGE & p->engine.ps.flags)
1085 return;
1086
1087 if (TERMTYPE_PS == p->type)
1088 ps_printf(p, "/%s %zu selectfont\n",
1089 fonts[(int)f].name,
1090 p->engine.ps.scale);
1091 else
1092 ps_printf(p, "/F%d %zu Tf\n",
1093 (int)f,
1094 p->engine.ps.scale);
1095 }
1096
1097
1098 /* ARGSUSED */
1099 static size_t
1100 ps_width(const struct termp *p, char c)
1101 {
1102
1103 if (c <= 32 || c - 32 >= MAXCHAR)
1104 return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1105
1106 c -= 32;
1107 return((size_t)fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1108 }
1109
1110
1111 static double
1112 ps_hspan(const struct termp *p, const struct roffsu *su)
1113 {
1114 double r;
1115
1116 /*
1117 * All of these measurements are derived by converting from the
1118 * native measurement to AFM units.
1119 */
1120
1121 switch (su->unit) {
1122 case (SCALE_CM):
1123 r = PNT2AFM(p, su->scale * 28.34);
1124 break;
1125 case (SCALE_IN):
1126 r = PNT2AFM(p, su->scale * 72);
1127 break;
1128 case (SCALE_PC):
1129 r = PNT2AFM(p, su->scale * 12);
1130 break;
1131 case (SCALE_PT):
1132 r = PNT2AFM(p, su->scale * 100);
1133 break;
1134 case (SCALE_EM):
1135 r = su->scale *
1136 fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
1137 break;
1138 case (SCALE_MM):
1139 r = PNT2AFM(p, su->scale * 2.834);
1140 break;
1141 case (SCALE_EN):
1142 r = su->scale *
1143 fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
1144 break;
1145 case (SCALE_VS):
1146 r = su->scale * p->engine.ps.lineheight;
1147 break;
1148 default:
1149 r = su->scale;
1150 break;
1151 }
1152
1153 return(r);
1154 }
1155