Annotation of mandoc/term_ps.c, Revision 1.32
1.32 ! kristaps 1: /* $Id: term_ps.c,v 1.31 2010/07/04 20:06:59 kristaps Exp $ */
1.1 kristaps 2: /*
1.10 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.4 kristaps 21: #include <sys/param.h>
22:
1.2 kristaps 23: #include <assert.h>
1.4 kristaps 24: #include <stdarg.h>
1.17 kristaps 25: #include <stdint.h>
1.2 kristaps 26: #include <stdio.h>
1.1 kristaps 27: #include <stdlib.h>
1.4 kristaps 28: #include <string.h>
1.13 kristaps 29: #include <time.h>
1.26 kristaps 30: #include <unistd.h>
1.1 kristaps 31:
32: #include "out.h"
33: #include "main.h"
34: #include "term.h"
35:
1.21 kristaps 36: /* Convert PostScript point "x" to an AFM unit. */
1.25 kristaps 37: #define PNT2AFM(p, x) /* LINTED */ \
1.21 kristaps 38: (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
39:
40: /* Convert an AFM unit "x" to a PostScript points */
1.25 kristaps 41: #define AFM2PNT(p, x) /* LINTED */ \
1.21 kristaps 42: (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
43:
1.13 kristaps 44: struct glyph {
1.25 kristaps 45: size_t wx; /* WX in AFM */
1.13 kristaps 46: };
47:
48: struct font {
49: const char *name; /* FontName in AFM */
1.15 kristaps 50: #define MAXCHAR 95 /* total characters we can handle */
1.13 kristaps 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:
1.16 kristaps 60: static const struct font fonts[TERMFONT__MAX] = {
1.24 kristaps 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 },
1.22 kristaps 227: { 556 },
1.24 kristaps 228: { 444 },
229: { 333 },
1.22 kristaps 230: { 500 },
1.24 kristaps 231: { 556 },
1.22 kristaps 232: { 278 },
1.24 kristaps 233: { 333 },
234: { 556 },
1.22 kristaps 235: { 278 },
1.24 kristaps 236: { 833 },
1.22 kristaps 237: { 556 },
1.24 kristaps 238: { 500 },
1.22 kristaps 239: { 556 },
240: { 556 },
241: { 444 },
1.24 kristaps 242: { 389 },
243: { 333 },
1.22 kristaps 244: { 556 },
245: { 500 },
1.24 kristaps 246: { 722 },
247: { 500 },
1.22 kristaps 248: { 500 },
1.24 kristaps 249: { 444 },
250: { 394 },
251: { 220 },
252: { 394 },
253: { 520 },
1.13 kristaps 254: } },
1.24 kristaps 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 },
1.22 kristaps 324: { 500 },
1.24 kristaps 325: { 444 },
326: { 278 },
1.22 kristaps 327: { 500 },
328: { 500 },
329: { 278 },
1.24 kristaps 330: { 278 },
331: { 444 },
332: { 278 },
333: { 722 },
334: { 500 },
335: { 500 },
336: { 500 },
1.22 kristaps 337: { 500 },
1.24 kristaps 338: { 389 },
339: { 389 },
1.22 kristaps 340: { 278 },
341: { 500 },
1.24 kristaps 342: { 444 },
343: { 667 },
344: { 444 },
345: { 444 },
346: { 389 },
347: { 400 },
1.22 kristaps 348: { 275 },
1.24 kristaps 349: { 400 },
350: { 541 },
1.13 kristaps 351: } },
352: };
353:
1.15 kristaps 354: /* These work the buffer used by the header and footer. */
1.4 kristaps 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:
1.9 kristaps 370:
1.20 kristaps 371: static double ps_hspan(const struct termp *,
1.19 kristaps 372: const struct roffsu *);
373: static size_t ps_width(const struct termp *, char);
374: static void ps_advance(struct termp *, size_t);
1.2 kristaps 375: static void ps_begin(struct termp *);
376: static void ps_end(struct termp *);
377: static void ps_endline(struct termp *);
1.9 kristaps 378: static void ps_fclose(struct termp *);
1.19 kristaps 379: static void ps_letter(struct termp *, char);
1.9 kristaps 380: static void ps_pclose(struct termp *);
1.13 kristaps 381: static void ps_pletter(struct termp *, int);
1.4 kristaps 382: static void ps_printf(struct termp *, const char *, ...);
383: static void ps_putchar(struct termp *, char);
1.9 kristaps 384: static void ps_setfont(struct termp *, enum termfont);
1.2 kristaps 385:
386:
1.1 kristaps 387: void *
1.17 kristaps 388: ps_alloc(char *outopts)
1.1 kristaps 389: {
390: struct termp *p;
1.28 kristaps 391: size_t pagex, pagey, marginx, marginy, lineheight;
1.17 kristaps 392: const char *toks[2];
1.25 kristaps 393: const char *pp;
1.17 kristaps 394: char *v;
1.1 kristaps 395:
396: if (NULL == (p = term_alloc(TERMENC_ASCII)))
397: return(NULL);
398:
1.19 kristaps 399: p->advance = ps_advance;
1.2 kristaps 400: p->begin = ps_begin;
401: p->end = ps_end;
402: p->endline = ps_endline;
1.19 kristaps 403: p->hspan = ps_hspan;
404: p->letter = ps_letter;
405: p->type = TERMTYPE_PS;
1.11 kristaps 406: p->width = ps_width;
1.21 kristaps 407:
1.17 kristaps 408: toks[0] = "paper";
409: toks[1] = NULL;
410:
1.25 kristaps 411: pp = NULL;
1.19 kristaps 412:
1.17 kristaps 413: while (outopts && *outopts)
414: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
415: case (0):
1.25 kristaps 416: pp = v;
1.17 kristaps 417: break;
418: default:
419: break;
420: }
1.16 kristaps 421:
1.25 kristaps 422: /* Default to US letter (millimetres). */
423:
1.31 kristaps 424: pagex = 216;
425: pagey = 279;
1.25 kristaps 426:
427: /*
428: * The ISO-269 paper sizes can be calculated automatically, but
429: * it would require bringing in -lm for pow() and I'd rather not
430: * do that. So just do it the easy way for now. Since this
431: * only happens once, I'm not terribly concerned.
432: */
433:
434: if (pp && strcasecmp(pp, "letter")) {
435: if (0 == strcasecmp(pp, "a3")) {
436: pagex = 297;
437: pagey = 420;
438: } else if (0 == strcasecmp(pp, "a4")) {
439: pagex = 210;
440: pagey = 297;
441: } else if (0 == strcasecmp(pp, "a5")) {
442: pagex = 148;
443: pagey = 210;
444: } else if (0 == strcasecmp(pp, "legal")) {
445: pagex = 216;
446: pagey = 356;
447: } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
448: fprintf(stderr, "%s: Unknown paper\n", pp);
1.27 kristaps 449: } else if (NULL == pp)
450: pp = "letter";
451:
452: /*
453: * This MUST be defined before any PNT2AFM or AFM2PNT
454: * calculations occur.
455: */
456:
457: p->engine.ps.scale = 11;
458:
1.25 kristaps 459: /* Remember millimetres -> AFM units. */
460:
461: pagex = PNT2AFM(p, ((double)pagex * 2.834));
462: pagey = PNT2AFM(p, ((double)pagey * 2.834));
463:
1.28 kristaps 464: /* Margins are 1/9 the page x and y. */
1.27 kristaps 465:
1.29 kristaps 466: marginx = /* LINTED */
467: (size_t)((double)pagex / 9.0);
468: marginy = /* LINTED */
469: (size_t)((double)pagey / 9.0);
1.27 kristaps 470:
1.31 kristaps 471: /* Line-height is 1.4em. */
472:
473: lineheight = PNT2AFM(p, ((double)p->engine.ps.scale * 1.4));
1.16 kristaps 474:
475: p->engine.ps.width = pagex;
476: p->engine.ps.height = pagey;
1.28 kristaps 477: p->engine.ps.header = pagey - (marginy / 2) - (lineheight / 2);
478: p->engine.ps.top = pagey - marginy;
479: p->engine.ps.footer = (marginy / 2) - (lineheight / 2);
480: p->engine.ps.bottom = marginy;
481: p->engine.ps.left = marginx;
1.18 kristaps 482: p->engine.ps.lineheight = lineheight;
1.16 kristaps 483:
1.28 kristaps 484: p->defrmargin = pagex - (marginx * 2);
1.1 kristaps 485: return(p);
486: }
487:
488:
489: void
490: ps_free(void *arg)
491: {
1.4 kristaps 492: struct termp *p;
493:
494: p = (struct termp *)arg;
495:
496: if (p->engine.ps.psmarg)
497: free(p->engine.ps.psmarg);
498:
499: term_free(p);
500: }
501:
502:
503: static void
504: ps_printf(struct termp *p, const char *fmt, ...)
505: {
506: va_list ap;
507: int pos;
508:
509: va_start(ap, fmt);
510:
511: /*
512: * If we're running in regular mode, then pipe directly into
513: * vprintf(). If we're processing margins, then push the data
514: * into our growable margin buffer.
515: */
1.1 kristaps 516:
1.29 kristaps 517: if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.4 kristaps 518: vprintf(fmt, ap);
519: va_end(ap);
520: return;
521: }
522:
523: /*
524: * XXX: I assume that the in-margin print won't exceed
525: * PS_BUFSLOP (128 bytes), which is reasonable but still an
526: * assumption that will cause pukeage if it's not the case.
527: */
528:
529: PS_GROWBUF(p, PS_BUFSLOP);
530:
531: pos = (int)p->engine.ps.psmargcur;
532: vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
533: p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.5 kristaps 534:
535: va_end(ap);
1.4 kristaps 536: }
537:
538:
539: static void
540: ps_putchar(struct termp *p, char c)
541: {
542: int pos;
543:
544: /* See ps_printf(). */
545:
1.29 kristaps 546: if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.4 kristaps 547: putchar(c);
548: return;
549: }
550:
551: PS_GROWBUF(p, 2);
552:
553: pos = (int)p->engine.ps.psmargcur++;
1.5 kristaps 554: p->engine.ps.psmarg[pos++] = c;
1.4 kristaps 555: p->engine.ps.psmarg[pos] = '\0';
1.2 kristaps 556: }
557:
558:
1.3 kristaps 559: /* ARGSUSED */
1.2 kristaps 560: static void
561: ps_end(struct termp *p)
562: {
563:
1.4 kristaps 564: /*
565: * At the end of the file, do one last showpage. This is the
566: * same behaviour as groff(1) and works for multiple pages as
567: * well as just one.
568: */
569:
1.30 kristaps 570: if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
571: assert(0 == p->engine.ps.flags);
572: assert('\0' == p->engine.ps.last);
573: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
574: printf("%s", p->engine.ps.psmarg);
575: p->engine.ps.pages++;
576: printf("showpage\n");
577: }
1.16 kristaps 578:
579: printf("%%%%Trailer\n");
580: printf("%%%%Pages: %zu\n", p->engine.ps.pages);
581: printf("%%%%EOF\n");
1.2 kristaps 582: }
583:
584:
585: static void
586: ps_begin(struct termp *p)
587: {
1.13 kristaps 588: time_t t;
1.16 kristaps 589: int i;
1.2 kristaps 590:
1.9 kristaps 591: /*
592: * Print margins into margin buffer. Nothing gets output to the
593: * screen yet, so we don't need to initialise the primary state.
1.2 kristaps 594: */
595:
1.4 kristaps 596: if (p->engine.ps.psmarg) {
597: assert(p->engine.ps.psmargsz);
598: p->engine.ps.psmarg[0] = '\0';
599: }
600:
601: p->engine.ps.psmargcur = 0;
1.29 kristaps 602: p->engine.ps.flags = PS_MARGINS;
1.16 kristaps 603: p->engine.ps.pscol = p->engine.ps.left;
604: p->engine.ps.psrow = p->engine.ps.header;
1.4 kristaps 605:
1.9 kristaps 606: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 607:
608: (*p->headf)(p, p->argf);
609: (*p->endline)(p);
610:
1.16 kristaps 611: p->engine.ps.pscol = p->engine.ps.left;
612: p->engine.ps.psrow = p->engine.ps.footer;
1.4 kristaps 613:
614: (*p->footf)(p, p->argf);
615: (*p->endline)(p);
616:
1.29 kristaps 617: p->engine.ps.flags &= ~PS_MARGINS;
1.9 kristaps 618:
1.29 kristaps 619: assert(0 == p->engine.ps.flags);
1.9 kristaps 620: assert(p->engine.ps.psmarg);
621: assert('\0' != p->engine.ps.psmarg[0]);
622:
623: /*
624: * Print header and initialise page state. Following this,
625: * stuff gets printed to the screen, so make sure we're sane.
626: */
1.4 kristaps 627:
1.13 kristaps 628: t = time(NULL);
629:
1.16 kristaps 630: printf("%%!PS-Adobe-3.0\n");
1.13 kristaps 631: printf("%%%%Creator: mandoc-%s\n", VERSION);
632: printf("%%%%CreationDate: %s", ctime(&t));
1.16 kristaps 633: printf("%%%%DocumentData: Clean7Bit\n");
634: printf("%%%%Orientation: Portrait\n");
635: printf("%%%%Pages: (atend)\n");
1.13 kristaps 636: printf("%%%%PageOrder: Ascend\n");
1.16 kristaps 637: printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
1.23 kristaps 638: AFM2PNT(p, p->engine.ps.width),
639: AFM2PNT(p, p->engine.ps.height));
1.16 kristaps 640: printf("%%%%DocumentNeededResources: font");
641: for (i = 0; i < (int)TERMFONT__MAX; i++)
642: printf(" %s", fonts[i].name);
643: printf("\n%%%%EndComments\n");
644:
645: printf("%%%%Page: %zu %zu\n",
646: p->engine.ps.pages + 1,
647: p->engine.ps.pages + 1);
1.13 kristaps 648:
1.9 kristaps 649: ps_setfont(p, TERMFONT_NONE);
1.16 kristaps 650: p->engine.ps.pscol = p->engine.ps.left;
651: p->engine.ps.psrow = p->engine.ps.top;
1.29 kristaps 652: p->engine.ps.flags |= PS_NEWPAGE;
1.2 kristaps 653: }
654:
655:
656: static void
1.13 kristaps 657: ps_pletter(struct termp *p, int c)
1.2 kristaps 658: {
1.14 kristaps 659: int f;
1.32 ! kristaps 660:
1.9 kristaps 661: /*
1.32 ! kristaps 662: * If we haven't opened a page context, then output that we're
! 663: * in a new page and make sure the font is correctly set.
1.9 kristaps 664: */
665:
1.32 ! kristaps 666: if (PS_NEWPAGE & p->engine.ps.flags) {
1.30 kristaps 667: printf("%%%%Page: %zu %zu\n",
668: p->engine.ps.pages + 1,
669: p->engine.ps.pages + 1);
1.32 ! kristaps 670: ps_printf(p, "/%s %zu selectfont\n",
! 671: fonts[(int)p->engine.ps.lastf].name,
! 672: p->engine.ps.scale);
! 673: p->engine.ps.flags &= ~PS_NEWPAGE;
! 674: }
! 675:
! 676: /*
! 677: * If we're not in a PostScript "word" context, then open one
! 678: * now at the current cursor.
! 679: */
1.30 kristaps 680:
1.29 kristaps 681: if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.4 kristaps 682: ps_printf(p, "%zu %zu moveto\n(",
1.21 kristaps 683: AFM2PNT(p, p->engine.ps.pscol),
684: AFM2PNT(p, p->engine.ps.psrow));
1.29 kristaps 685: p->engine.ps.flags |= PS_INLINE;
1.2 kristaps 686: }
687:
1.29 kristaps 688: assert( ! (PS_NEWPAGE & p->engine.ps.flags));
689:
1.2 kristaps 690: /*
691: * We need to escape these characters as per the PostScript
692: * specification. We would also escape non-graphable characters
693: * (like tabs), but none of them would get to this point and
694: * it's superfluous to abort() on them.
695: */
696:
697: switch (c) {
698: case ('('):
699: /* FALLTHROUGH */
700: case (')'):
701: /* FALLTHROUGH */
702: case ('\\'):
1.4 kristaps 703: ps_putchar(p, '\\');
1.2 kristaps 704: break;
705: default:
706: break;
707: }
708:
709: /* Write the character and adjust where we are on the page. */
1.9 kristaps 710:
1.14 kristaps 711: f = (int)p->engine.ps.lastf;
1.13 kristaps 712:
1.14 kristaps 713: if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13 kristaps 714: ps_putchar(p, ' ');
1.18 kristaps 715: p->engine.ps.pscol += fonts[f].gly[0].wx;
1.13 kristaps 716: return;
717: }
718:
1.25 kristaps 719: ps_putchar(p, (char)c);
1.14 kristaps 720: c -= 32;
1.18 kristaps 721: p->engine.ps.pscol += fonts[f].gly[c].wx;
1.2 kristaps 722: }
723:
724:
725: static void
1.9 kristaps 726: ps_pclose(struct termp *p)
727: {
728:
729: /*
730: * Spit out that we're exiting a word context (this is a
731: * "partial close" because we don't check the last-char buffer
732: * or anything).
733: */
734:
1.29 kristaps 735: if ( ! (PS_INLINE & p->engine.ps.flags))
1.9 kristaps 736: return;
737:
738: ps_printf(p, ") show\n");
1.29 kristaps 739: p->engine.ps.flags &= ~PS_INLINE;
1.9 kristaps 740: }
741:
742:
743: static void
744: ps_fclose(struct termp *p)
745: {
746:
747: /*
748: * Strong closure: if we have a last-char, spit it out after
749: * checking that we're in the right font mode. This will of
750: * course open a new scope, if applicable.
751: *
752: * Following this, close out any scope that's open.
753: */
754:
755: if ('\0' != p->engine.ps.last) {
756: if (p->engine.ps.lastf != TERMFONT_NONE) {
757: ps_pclose(p);
758: ps_setfont(p, TERMFONT_NONE);
759: }
760: ps_pletter(p, p->engine.ps.last);
761: p->engine.ps.last = '\0';
762: }
763:
1.29 kristaps 764: if ( ! (PS_INLINE & p->engine.ps.flags))
1.9 kristaps 765: return;
766:
767: ps_pclose(p);
768: }
769:
770:
771: static void
1.8 kristaps 772: ps_letter(struct termp *p, char c)
773: {
774: char cc;
1.9 kristaps 775:
776: /*
777: * State machine dictates whether to buffer the last character
778: * or not. Basically, encoded words are detected by checking if
779: * we're an "8" and switching on the buffer. Then we put "8" in
780: * our buffer, and on the next charater, flush both character
781: * and buffer. Thus, "regular" words are detected by having a
782: * regular character and a regular buffer character.
783: */
784:
1.8 kristaps 785: if ('\0' == p->engine.ps.last) {
786: assert(8 != c);
787: p->engine.ps.last = c;
788: return;
789: } else if (8 == p->engine.ps.last) {
790: assert(8 != c);
1.9 kristaps 791: p->engine.ps.last = '\0';
1.8 kristaps 792: } else if (8 == c) {
793: assert(8 != p->engine.ps.last);
1.9 kristaps 794: if ('_' == p->engine.ps.last) {
795: if (p->engine.ps.lastf != TERMFONT_UNDER) {
796: ps_pclose(p);
797: ps_setfont(p, TERMFONT_UNDER);
798: }
799: } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
800: ps_pclose(p);
801: ps_setfont(p, TERMFONT_BOLD);
802: }
1.8 kristaps 803: p->engine.ps.last = c;
804: return;
805: } else {
1.9 kristaps 806: if (p->engine.ps.lastf != TERMFONT_NONE) {
807: ps_pclose(p);
808: ps_setfont(p, TERMFONT_NONE);
809: }
1.8 kristaps 810: cc = p->engine.ps.last;
811: p->engine.ps.last = c;
812: c = cc;
813: }
814:
1.9 kristaps 815: ps_pletter(p, c);
1.8 kristaps 816: }
817:
818:
819: static void
1.2 kristaps 820: ps_advance(struct termp *p, size_t len)
821: {
822:
1.9 kristaps 823: /*
824: * Advance some spaces. This can probably be made smarter,
825: * i.e., to have multiple space-separated words in the same
826: * scope, but this is easier: just close out the current scope
827: * and readjust our column settings.
828: */
1.8 kristaps 829:
1.9 kristaps 830: ps_fclose(p);
1.14 kristaps 831: p->engine.ps.pscol += len;
1.2 kristaps 832: }
833:
834:
835: static void
836: ps_endline(struct termp *p)
837: {
1.8 kristaps 838:
1.9 kristaps 839: /* Close out any scopes we have open: we're at eoln. */
840:
841: ps_fclose(p);
1.2 kristaps 842:
1.9 kristaps 843: /*
844: * If we're in the margin, don't try to recalculate our current
845: * row. XXX: if the column tries to be fancy with multiple
846: * lines, we'll do nasty stuff.
847: */
1.2 kristaps 848:
1.29 kristaps 849: if (PS_MARGINS & p->engine.ps.flags)
850: return;
851:
852: /* Left-justify. */
853:
854: p->engine.ps.pscol = p->engine.ps.left;
855:
856: /* If we haven't printed anything, return. */
857:
858: if (PS_NEWPAGE & p->engine.ps.flags)
1.2 kristaps 859: return;
860:
1.9 kristaps 861: /*
862: * Put us down a line. If we're at the page bottom, spit out a
863: * showpage and restart our row.
864: */
865:
1.16 kristaps 866: if (p->engine.ps.psrow >= p->engine.ps.lineheight +
867: p->engine.ps.bottom) {
868: p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2 kristaps 869: return;
870: }
871:
1.4 kristaps 872: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
873: printf("%s", p->engine.ps.psmarg);
1.23 kristaps 874: printf("showpage\n");
875: p->engine.ps.pages++;
1.16 kristaps 876: p->engine.ps.psrow = p->engine.ps.top;
1.29 kristaps 877: assert( ! (PS_NEWPAGE & p->engine.ps.flags));
878: p->engine.ps.flags |= PS_NEWPAGE;
1.1 kristaps 879: }
1.9 kristaps 880:
881:
882: static void
883: ps_setfont(struct termp *p, enum termfont f)
884: {
885:
1.16 kristaps 886: assert(f < TERMFONT__MAX);
1.32 ! kristaps 887: p->engine.ps.lastf = f;
! 888:
! 889: /*
! 890: * If we're still at the top of the page, let the font-setting
! 891: * be delayed until we actually have stuff to print.
! 892: */
! 893:
! 894: if (PS_NEWPAGE & p->engine.ps.flags)
! 895: return;
! 896:
1.21 kristaps 897: ps_printf(p, "/%s %zu selectfont\n",
898: fonts[(int)f].name, p->engine.ps.scale);
1.9 kristaps 899: }
900:
1.11 kristaps 901:
1.12 kristaps 902: /* ARGSUSED */
1.11 kristaps 903: static size_t
904: ps_width(const struct termp *p, char c)
905: {
906:
1.14 kristaps 907: if (c <= 32 || c - 32 >= MAXCHAR)
1.18 kristaps 908: return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14 kristaps 909:
910: c -= 32;
1.18 kristaps 911: return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11 kristaps 912: }
1.19 kristaps 913:
914:
1.20 kristaps 915: static double
1.19 kristaps 916: ps_hspan(const struct termp *p, const struct roffsu *su)
917: {
918: double r;
919:
920: /*
921: * All of these measurements are derived by converting from the
1.21 kristaps 922: * native measurement to AFM units.
1.19 kristaps 923: */
924:
925: switch (su->unit) {
926: case (SCALE_CM):
1.21 kristaps 927: r = PNT2AFM(p, su->scale * 28.34);
1.19 kristaps 928: break;
929: case (SCALE_IN):
1.21 kristaps 930: r = PNT2AFM(p, su->scale * 72);
1.19 kristaps 931: break;
932: case (SCALE_PC):
1.21 kristaps 933: r = PNT2AFM(p, su->scale * 12);
1.19 kristaps 934: break;
935: case (SCALE_PT):
1.21 kristaps 936: r = PNT2AFM(p, su->scale * 100);
1.19 kristaps 937: break;
938: case (SCALE_EM):
939: r = su->scale *
940: fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
941: break;
942: case (SCALE_MM):
1.21 kristaps 943: r = PNT2AFM(p, su->scale * 2.834);
1.19 kristaps 944: break;
945: case (SCALE_EN):
946: r = su->scale *
947: fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
948: break;
949: case (SCALE_VS):
950: r = su->scale * p->engine.ps.lineheight;
951: break;
952: default:
953: r = su->scale;
954: break;
955: }
956:
1.20 kristaps 957: return(r);
1.19 kristaps 958: }
959:
CVSweb