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