Annotation of mandoc/term_ps.c, Revision 1.33
1.33 ! kristaps 1: /* $Id: term_ps.c,v 1.32 2010/07/04 20:17:42 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: p->engine.ps.pscol = p->engine.ps.left;
646: p->engine.ps.psrow = p->engine.ps.top;
1.29 kristaps 647: p->engine.ps.flags |= PS_NEWPAGE;
1.33 ! kristaps 648: ps_setfont(p, TERMFONT_NONE);
1.2 kristaps 649: }
650:
651:
652: static void
1.13 kristaps 653: ps_pletter(struct termp *p, int c)
1.2 kristaps 654: {
1.14 kristaps 655: int f;
1.32 kristaps 656:
1.9 kristaps 657: /*
1.32 kristaps 658: * If we haven't opened a page context, then output that we're
659: * in a new page and make sure the font is correctly set.
1.9 kristaps 660: */
661:
1.32 kristaps 662: if (PS_NEWPAGE & p->engine.ps.flags) {
1.30 kristaps 663: printf("%%%%Page: %zu %zu\n",
664: p->engine.ps.pages + 1,
665: p->engine.ps.pages + 1);
1.32 kristaps 666: ps_printf(p, "/%s %zu selectfont\n",
667: fonts[(int)p->engine.ps.lastf].name,
668: p->engine.ps.scale);
669: p->engine.ps.flags &= ~PS_NEWPAGE;
670: }
671:
672: /*
673: * If we're not in a PostScript "word" context, then open one
674: * now at the current cursor.
675: */
1.30 kristaps 676:
1.29 kristaps 677: if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.4 kristaps 678: ps_printf(p, "%zu %zu moveto\n(",
1.21 kristaps 679: AFM2PNT(p, p->engine.ps.pscol),
680: AFM2PNT(p, p->engine.ps.psrow));
1.29 kristaps 681: p->engine.ps.flags |= PS_INLINE;
1.2 kristaps 682: }
683:
1.29 kristaps 684: assert( ! (PS_NEWPAGE & p->engine.ps.flags));
685:
1.2 kristaps 686: /*
687: * We need to escape these characters as per the PostScript
688: * specification. We would also escape non-graphable characters
689: * (like tabs), but none of them would get to this point and
690: * it's superfluous to abort() on them.
691: */
692:
693: switch (c) {
694: case ('('):
695: /* FALLTHROUGH */
696: case (')'):
697: /* FALLTHROUGH */
698: case ('\\'):
1.4 kristaps 699: ps_putchar(p, '\\');
1.2 kristaps 700: break;
701: default:
702: break;
703: }
704:
705: /* Write the character and adjust where we are on the page. */
1.9 kristaps 706:
1.14 kristaps 707: f = (int)p->engine.ps.lastf;
1.13 kristaps 708:
1.14 kristaps 709: if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13 kristaps 710: ps_putchar(p, ' ');
1.18 kristaps 711: p->engine.ps.pscol += fonts[f].gly[0].wx;
1.13 kristaps 712: return;
713: }
714:
1.25 kristaps 715: ps_putchar(p, (char)c);
1.14 kristaps 716: c -= 32;
1.18 kristaps 717: p->engine.ps.pscol += fonts[f].gly[c].wx;
1.2 kristaps 718: }
719:
720:
721: static void
1.9 kristaps 722: ps_pclose(struct termp *p)
723: {
724:
725: /*
726: * Spit out that we're exiting a word context (this is a
727: * "partial close" because we don't check the last-char buffer
728: * or anything).
729: */
730:
1.29 kristaps 731: if ( ! (PS_INLINE & p->engine.ps.flags))
1.9 kristaps 732: return;
733:
734: ps_printf(p, ") show\n");
1.29 kristaps 735: p->engine.ps.flags &= ~PS_INLINE;
1.9 kristaps 736: }
737:
738:
739: static void
740: ps_fclose(struct termp *p)
741: {
742:
743: /*
744: * Strong closure: if we have a last-char, spit it out after
745: * checking that we're in the right font mode. This will of
746: * course open a new scope, if applicable.
747: *
748: * Following this, close out any scope that's open.
749: */
750:
751: if ('\0' != p->engine.ps.last) {
752: if (p->engine.ps.lastf != TERMFONT_NONE) {
753: ps_pclose(p);
754: ps_setfont(p, TERMFONT_NONE);
755: }
756: ps_pletter(p, p->engine.ps.last);
757: p->engine.ps.last = '\0';
758: }
759:
1.29 kristaps 760: if ( ! (PS_INLINE & p->engine.ps.flags))
1.9 kristaps 761: return;
762:
763: ps_pclose(p);
764: }
765:
766:
767: static void
1.8 kristaps 768: ps_letter(struct termp *p, char c)
769: {
770: char cc;
1.9 kristaps 771:
772: /*
773: * State machine dictates whether to buffer the last character
774: * or not. Basically, encoded words are detected by checking if
775: * we're an "8" and switching on the buffer. Then we put "8" in
776: * our buffer, and on the next charater, flush both character
777: * and buffer. Thus, "regular" words are detected by having a
778: * regular character and a regular buffer character.
779: */
780:
1.8 kristaps 781: if ('\0' == p->engine.ps.last) {
782: assert(8 != c);
783: p->engine.ps.last = c;
784: return;
785: } else if (8 == p->engine.ps.last) {
786: assert(8 != c);
1.9 kristaps 787: p->engine.ps.last = '\0';
1.8 kristaps 788: } else if (8 == c) {
789: assert(8 != p->engine.ps.last);
1.9 kristaps 790: if ('_' == p->engine.ps.last) {
791: if (p->engine.ps.lastf != TERMFONT_UNDER) {
792: ps_pclose(p);
793: ps_setfont(p, TERMFONT_UNDER);
794: }
795: } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
796: ps_pclose(p);
797: ps_setfont(p, TERMFONT_BOLD);
798: }
1.8 kristaps 799: p->engine.ps.last = c;
800: return;
801: } else {
1.9 kristaps 802: if (p->engine.ps.lastf != TERMFONT_NONE) {
803: ps_pclose(p);
804: ps_setfont(p, TERMFONT_NONE);
805: }
1.8 kristaps 806: cc = p->engine.ps.last;
807: p->engine.ps.last = c;
808: c = cc;
809: }
810:
1.9 kristaps 811: ps_pletter(p, c);
1.8 kristaps 812: }
813:
814:
815: static void
1.2 kristaps 816: ps_advance(struct termp *p, size_t len)
817: {
818:
1.9 kristaps 819: /*
820: * Advance some spaces. This can probably be made smarter,
821: * i.e., to have multiple space-separated words in the same
822: * scope, but this is easier: just close out the current scope
823: * and readjust our column settings.
824: */
1.8 kristaps 825:
1.9 kristaps 826: ps_fclose(p);
1.14 kristaps 827: p->engine.ps.pscol += len;
1.2 kristaps 828: }
829:
830:
831: static void
832: ps_endline(struct termp *p)
833: {
1.8 kristaps 834:
1.9 kristaps 835: /* Close out any scopes we have open: we're at eoln. */
836:
837: ps_fclose(p);
1.2 kristaps 838:
1.9 kristaps 839: /*
840: * If we're in the margin, don't try to recalculate our current
841: * row. XXX: if the column tries to be fancy with multiple
842: * lines, we'll do nasty stuff.
843: */
1.2 kristaps 844:
1.29 kristaps 845: if (PS_MARGINS & p->engine.ps.flags)
846: return;
847:
848: /* Left-justify. */
849:
850: p->engine.ps.pscol = p->engine.ps.left;
851:
852: /* If we haven't printed anything, return. */
853:
854: if (PS_NEWPAGE & p->engine.ps.flags)
1.2 kristaps 855: return;
856:
1.9 kristaps 857: /*
858: * Put us down a line. If we're at the page bottom, spit out a
859: * showpage and restart our row.
860: */
861:
1.16 kristaps 862: if (p->engine.ps.psrow >= p->engine.ps.lineheight +
863: p->engine.ps.bottom) {
864: p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2 kristaps 865: return;
866: }
867:
1.4 kristaps 868: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
869: printf("%s", p->engine.ps.psmarg);
1.23 kristaps 870: printf("showpage\n");
871: p->engine.ps.pages++;
1.16 kristaps 872: p->engine.ps.psrow = p->engine.ps.top;
1.29 kristaps 873: assert( ! (PS_NEWPAGE & p->engine.ps.flags));
874: p->engine.ps.flags |= PS_NEWPAGE;
1.1 kristaps 875: }
1.9 kristaps 876:
877:
878: static void
879: ps_setfont(struct termp *p, enum termfont f)
880: {
881:
1.16 kristaps 882: assert(f < TERMFONT__MAX);
1.32 kristaps 883: p->engine.ps.lastf = f;
884:
885: /*
886: * If we're still at the top of the page, let the font-setting
887: * be delayed until we actually have stuff to print.
888: */
889:
890: if (PS_NEWPAGE & p->engine.ps.flags)
891: return;
892:
1.21 kristaps 893: ps_printf(p, "/%s %zu selectfont\n",
894: fonts[(int)f].name, p->engine.ps.scale);
1.9 kristaps 895: }
896:
1.11 kristaps 897:
1.12 kristaps 898: /* ARGSUSED */
1.11 kristaps 899: static size_t
900: ps_width(const struct termp *p, char c)
901: {
902:
1.14 kristaps 903: if (c <= 32 || c - 32 >= MAXCHAR)
1.18 kristaps 904: return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14 kristaps 905:
906: c -= 32;
1.18 kristaps 907: return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11 kristaps 908: }
1.19 kristaps 909:
910:
1.20 kristaps 911: static double
1.19 kristaps 912: ps_hspan(const struct termp *p, const struct roffsu *su)
913: {
914: double r;
915:
916: /*
917: * All of these measurements are derived by converting from the
1.21 kristaps 918: * native measurement to AFM units.
1.19 kristaps 919: */
920:
921: switch (su->unit) {
922: case (SCALE_CM):
1.21 kristaps 923: r = PNT2AFM(p, su->scale * 28.34);
1.19 kristaps 924: break;
925: case (SCALE_IN):
1.21 kristaps 926: r = PNT2AFM(p, su->scale * 72);
1.19 kristaps 927: break;
928: case (SCALE_PC):
1.21 kristaps 929: r = PNT2AFM(p, su->scale * 12);
1.19 kristaps 930: break;
931: case (SCALE_PT):
1.21 kristaps 932: r = PNT2AFM(p, su->scale * 100);
1.19 kristaps 933: break;
934: case (SCALE_EM):
935: r = su->scale *
936: fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
937: break;
938: case (SCALE_MM):
1.21 kristaps 939: r = PNT2AFM(p, su->scale * 2.834);
1.19 kristaps 940: break;
941: case (SCALE_EN):
942: r = su->scale *
943: fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
944: break;
945: case (SCALE_VS):
946: r = su->scale * p->engine.ps.lineheight;
947: break;
948: default:
949: r = su->scale;
950: break;
951: }
952:
1.20 kristaps 953: return(r);
1.19 kristaps 954: }
955:
CVSweb