Annotation of mandoc/term_ps.c, Revision 1.15
1.15 ! kristaps 1: /* $Id: term_ps.c,v 1.14 2010/06/28 22:46:21 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.2 kristaps 25: #include <stdio.h>
1.1 kristaps 26: #include <stdlib.h>
1.4 kristaps 27: #include <string.h>
1.13 kristaps 28: #include <time.h>
1.1 kristaps 29:
30: #include "out.h"
31: #include "main.h"
32: #include "term.h"
33:
1.15 ! kristaps 34: /* TODO: all this will go away with different paper sizes. */
! 35: #define PS_CHAR_HEIGHT 12
! 36: #define PS_CHAR_TOPMARG (792 - 24)
! 37: #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
! 38: #define PS_CHAR_LEFT 36
! 39: #define PS_CHAR_BOTMARG 24
! 40: #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
! 41:
1.13 kristaps 42: struct glyph {
43: int wx; /* WX in AFM */
44: };
45:
46: struct font {
47: const char *name; /* FontName in AFM */
1.15 ! kristaps 48: #define MAXCHAR 95 /* total characters we can handle */
1.13 kristaps 49: struct glyph gly[MAXCHAR]; /* glyph metrics */
50: };
51:
52: /*
53: * We define, for the time being, three fonts: bold, oblique/italic, and
54: * normal (roman). The following table hard-codes the font metrics for
55: * ASCII, i.e., 32--127.
56: */
57:
58: static const struct font fonts[3] = {
59: { "Courier", {
60: { 600 },
61: { 600 },
62: { 600 },
63: { 600 },
64: { 600 },
65: { 600 },
66: { 600 },
67: { 600 },
68: { 600 },
69: { 600 },
70: { 600 },
71: { 600 },
72: { 600 },
73: { 600 },
74: { 600 },
75: { 600 },
76: { 600 },
77: { 600 },
78: { 600 },
79: { 600 },
80: { 600 },
81: { 600 },
82: { 600 },
83: { 600 },
84: { 600 },
85: { 600 },
86: { 600 },
87: { 600 },
88: { 600 },
89: { 600 },
90: { 600 },
91: { 600 },
92: { 600 },
93: { 600 },
94: { 600 },
95: { 600 },
96: { 600 },
97: { 600 },
98: { 600 },
99: { 600 },
100: { 600 },
101: { 600 },
102: { 600 },
103: { 600 },
104: { 600 },
105: { 600 },
106: { 600 },
107: { 600 },
108: { 600 },
109: { 600 },
110: { 600 },
111: { 600 },
112: { 600 },
113: { 600 },
114: { 600 },
115: { 600 },
116: { 600 },
117: { 600 },
118: { 600 },
119: { 600 },
120: { 600 },
121: { 600 },
122: { 600 },
123: { 600 },
124: { 600 },
125: { 600 },
126: { 600 },
127: { 600 },
128: { 600 },
129: { 600 },
130: { 600 },
131: { 600 },
132: { 600 },
133: { 600 },
134: { 600 },
135: { 600 },
136: { 600 },
137: { 600 },
138: { 600 },
139: { 600 },
140: { 600 },
141: { 600 },
142: { 600 },
143: { 600 },
144: { 600 },
145: { 600 },
146: { 600 },
147: { 600 },
148: { 600 },
149: { 600 },
150: { 600 },
151: { 600 },
152: { 600 },
153: { 600 },
154: { 600 },
155: } },
156: { "Courier-Bold", {
157: { 600 },
158: { 600 },
159: { 600 },
160: { 600 },
161: { 600 },
162: { 600 },
163: { 600 },
164: { 600 },
165: { 600 },
166: { 600 },
167: { 600 },
168: { 600 },
169: { 600 },
170: { 600 },
171: { 600 },
172: { 600 },
173: { 600 },
174: { 600 },
175: { 600 },
176: { 600 },
177: { 600 },
178: { 600 },
179: { 600 },
180: { 600 },
181: { 600 },
182: { 600 },
183: { 600 },
184: { 600 },
185: { 600 },
186: { 600 },
187: { 600 },
188: { 600 },
189: { 600 },
190: { 600 },
191: { 600 },
192: { 600 },
193: { 600 },
194: { 600 },
195: { 600 },
196: { 600 },
197: { 600 },
198: { 600 },
199: { 600 },
200: { 600 },
201: { 600 },
202: { 600 },
203: { 600 },
204: { 600 },
205: { 600 },
206: { 600 },
207: { 600 },
208: { 600 },
209: { 600 },
210: { 600 },
211: { 600 },
212: { 600 },
213: { 600 },
214: { 600 },
215: { 600 },
216: { 600 },
217: { 600 },
218: { 600 },
219: { 600 },
220: { 600 },
221: { 600 },
222: { 600 },
223: { 600 },
224: { 600 },
225: { 600 },
226: { 600 },
227: { 600 },
228: { 600 },
229: { 600 },
230: { 600 },
231: { 600 },
232: { 600 },
233: { 600 },
234: { 600 },
235: { 600 },
236: { 600 },
237: { 600 },
238: { 600 },
239: { 600 },
240: { 600 },
241: { 600 },
242: { 600 },
243: { 600 },
244: { 600 },
245: { 600 },
246: { 600 },
247: { 600 },
248: { 600 },
249: { 600 },
250: { 600 },
251: { 600 },
252: } },
253: { "Courier-Oblique", {
254: { 600 },
255: { 600 },
256: { 600 },
257: { 600 },
258: { 600 },
259: { 600 },
260: { 600 },
261: { 600 },
262: { 600 },
263: { 600 },
264: { 600 },
265: { 600 },
266: { 600 },
267: { 600 },
268: { 600 },
269: { 600 },
270: { 600 },
271: { 600 },
272: { 600 },
273: { 600 },
274: { 600 },
275: { 600 },
276: { 600 },
277: { 600 },
278: { 600 },
279: { 600 },
280: { 600 },
281: { 600 },
282: { 600 },
283: { 600 },
284: { 600 },
285: { 600 },
286: { 600 },
287: { 600 },
288: { 600 },
289: { 600 },
290: { 600 },
291: { 600 },
292: { 600 },
293: { 600 },
294: { 600 },
295: { 600 },
296: { 600 },
297: { 600 },
298: { 600 },
299: { 600 },
300: { 600 },
301: { 600 },
302: { 600 },
303: { 600 },
304: { 600 },
305: { 600 },
306: { 600 },
307: { 600 },
308: { 600 },
309: { 600 },
310: { 600 },
311: { 600 },
312: { 600 },
313: { 600 },
314: { 600 },
315: { 600 },
316: { 600 },
317: { 600 },
318: { 600 },
319: { 600 },
320: { 600 },
321: { 600 },
322: { 600 },
323: { 600 },
324: { 600 },
325: { 600 },
326: { 600 },
327: { 600 },
328: { 600 },
329: { 600 },
330: { 600 },
331: { 600 },
332: { 600 },
333: { 600 },
334: { 600 },
335: { 600 },
336: { 600 },
337: { 600 },
338: { 600 },
339: { 600 },
340: { 600 },
341: { 600 },
342: { 600 },
343: { 600 },
344: { 600 },
345: { 600 },
346: { 600 },
347: { 600 },
348: { 600 },
349: } },
350: };
351:
1.15 ! kristaps 352: /* These work the buffer used by the header and footer. */
1.4 kristaps 353: #define PS_BUFSLOP 128
354: #define PS_GROWBUF(p, sz) \
355: do if ((p)->engine.ps.psmargcur + (sz) > \
356: (p)->engine.ps.psmargsz) { \
357: (p)->engine.ps.psmargsz += /* CONSTCOND */ \
358: MAX(PS_BUFSLOP, (sz)); \
359: (p)->engine.ps.psmarg = realloc \
360: ((p)->engine.ps.psmarg, \
361: (p)->engine.ps.psmargsz); \
362: if (NULL == (p)->engine.ps.psmarg) { \
363: perror(NULL); \
364: exit(EXIT_FAILURE); \
365: } \
366: } while (/* CONSTCOND */ 0)
367:
1.9 kristaps 368:
1.2 kristaps 369: static void ps_letter(struct termp *, char);
370: static void ps_begin(struct termp *);
371: static void ps_end(struct termp *);
372: static void ps_advance(struct termp *, size_t);
373: static void ps_endline(struct termp *);
1.9 kristaps 374: static void ps_fclose(struct termp *);
1.11 kristaps 375: static size_t ps_width(const struct termp *, char);
1.9 kristaps 376: static void ps_pclose(struct termp *);
1.13 kristaps 377: static void ps_pletter(struct termp *, int);
1.4 kristaps 378: static void ps_printf(struct termp *, const char *, ...);
379: static void ps_putchar(struct termp *, char);
1.9 kristaps 380: static void ps_setfont(struct termp *, enum termfont);
1.2 kristaps 381:
382:
1.1 kristaps 383: void *
384: ps_alloc(void)
385: {
386: struct termp *p;
387:
388: if (NULL == (p = term_alloc(TERMENC_ASCII)))
389: return(NULL);
390:
1.14 kristaps 391: p->defrmargin = 612 - (PS_CHAR_LEFT * 2);
1.15 ! kristaps 392:
1.1 kristaps 393: p->type = TERMTYPE_PS;
1.2 kristaps 394: p->letter = ps_letter;
395: p->begin = ps_begin;
396: p->end = ps_end;
397: p->advance = ps_advance;
398: p->endline = ps_endline;
1.11 kristaps 399: p->width = ps_width;
1.1 kristaps 400: return(p);
401: }
402:
403:
404: void
405: ps_free(void *arg)
406: {
1.4 kristaps 407: struct termp *p;
408:
409: p = (struct termp *)arg;
410:
411: if (p->engine.ps.psmarg)
412: free(p->engine.ps.psmarg);
413:
414: term_free(p);
415: }
416:
417:
418: static void
419: ps_printf(struct termp *p, const char *fmt, ...)
420: {
421: va_list ap;
422: int pos;
423:
424: va_start(ap, fmt);
425:
426: /*
427: * If we're running in regular mode, then pipe directly into
428: * vprintf(). If we're processing margins, then push the data
429: * into our growable margin buffer.
430: */
1.1 kristaps 431:
1.4 kristaps 432: if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
433: vprintf(fmt, ap);
434: va_end(ap);
435: return;
436: }
437:
438: /*
439: * XXX: I assume that the in-margin print won't exceed
440: * PS_BUFSLOP (128 bytes), which is reasonable but still an
441: * assumption that will cause pukeage if it's not the case.
442: */
443:
444: PS_GROWBUF(p, PS_BUFSLOP);
445:
446: pos = (int)p->engine.ps.psmargcur;
447: vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
448: p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.5 kristaps 449:
450: va_end(ap);
1.4 kristaps 451: }
452:
453:
454: static void
455: ps_putchar(struct termp *p, char c)
456: {
457: int pos;
458:
459: /* See ps_printf(). */
460:
461: if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
462: putchar(c);
463: return;
464: }
465:
466: PS_GROWBUF(p, 2);
467:
468: pos = (int)p->engine.ps.psmargcur++;
1.5 kristaps 469: p->engine.ps.psmarg[pos++] = c;
1.4 kristaps 470: p->engine.ps.psmarg[pos] = '\0';
1.2 kristaps 471: }
472:
473:
1.3 kristaps 474: /* ARGSUSED */
1.2 kristaps 475: static void
476: ps_end(struct termp *p)
477: {
478:
1.4 kristaps 479: /*
480: * At the end of the file, do one last showpage. This is the
481: * same behaviour as groff(1) and works for multiple pages as
482: * well as just one.
483: */
484:
1.8 kristaps 485: assert(0 == p->engine.ps.psstate);
486: assert('\0' == p->engine.ps.last);
1.4 kristaps 487: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
488: printf("%s", p->engine.ps.psmarg);
489: printf("showpage\n");
1.2 kristaps 490: printf("%s\n", "%%EOF");
491: }
492:
493:
494: static void
495: ps_begin(struct termp *p)
496: {
1.13 kristaps 497: time_t t;
1.2 kristaps 498:
1.9 kristaps 499: /*
500: * Print margins into margin buffer. Nothing gets output to the
501: * screen yet, so we don't need to initialise the primary state.
1.2 kristaps 502: */
503:
1.4 kristaps 504: if (p->engine.ps.psmarg) {
505: assert(p->engine.ps.psmargsz);
506: p->engine.ps.psmarg[0] = '\0';
507: }
508:
509: p->engine.ps.psmargcur = 0;
1.9 kristaps 510: p->engine.ps.psstate = PS_MARGINS;
1.4 kristaps 511: p->engine.ps.pscol = PS_CHAR_LEFT;
512: p->engine.ps.psrow = PS_CHAR_TOPMARG;
513:
1.9 kristaps 514: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 515:
516: (*p->headf)(p, p->argf);
517: (*p->endline)(p);
518:
519: p->engine.ps.pscol = PS_CHAR_LEFT;
520: p->engine.ps.psrow = PS_CHAR_BOTMARG;
521:
522: (*p->footf)(p, p->argf);
523: (*p->endline)(p);
524:
525: p->engine.ps.psstate &= ~PS_MARGINS;
1.9 kristaps 526:
1.4 kristaps 527: assert(0 == p->engine.ps.psstate);
1.9 kristaps 528: assert(p->engine.ps.psmarg);
529: assert('\0' != p->engine.ps.psmarg[0]);
530:
531: /*
532: * Print header and initialise page state. Following this,
533: * stuff gets printed to the screen, so make sure we're sane.
534: */
1.4 kristaps 535:
1.13 kristaps 536: t = time(NULL);
537:
538: printf("%%!PS\n");
539: printf("%%%%Creator: mandoc-%s\n", VERSION);
540: printf("%%%%CreationDate: %s", ctime(&t));
541: printf("%%%%PageOrder: Ascend\n");
542: printf("%%%%Orientation: Portrait\n");
543: printf("%%%%EndComments\n");
544:
1.9 kristaps 545: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 546: p->engine.ps.pscol = PS_CHAR_LEFT;
547: p->engine.ps.psrow = PS_CHAR_TOP;
1.2 kristaps 548: }
549:
550:
551: static void
1.13 kristaps 552: ps_pletter(struct termp *p, int c)
1.2 kristaps 553: {
1.14 kristaps 554: int f;
1.2 kristaps 555:
1.9 kristaps 556: /*
557: * If we're not in a PostScript "word" context, then open one
558: * now at the current cursor.
559: */
560:
1.2 kristaps 561: if ( ! (PS_INLINE & p->engine.ps.psstate)) {
1.4 kristaps 562: ps_printf(p, "%zu %zu moveto\n(",
1.2 kristaps 563: p->engine.ps.pscol,
564: p->engine.ps.psrow);
565: p->engine.ps.psstate |= PS_INLINE;
566: }
567:
568: /*
569: * We need to escape these characters as per the PostScript
570: * specification. We would also escape non-graphable characters
571: * (like tabs), but none of them would get to this point and
572: * it's superfluous to abort() on them.
573: */
574:
575: switch (c) {
576: case ('('):
577: /* FALLTHROUGH */
578: case (')'):
579: /* FALLTHROUGH */
580: case ('\\'):
1.4 kristaps 581: ps_putchar(p, '\\');
1.2 kristaps 582: break;
583: default:
584: break;
585: }
586:
587: /* Write the character and adjust where we are on the page. */
1.9 kristaps 588:
1.14 kristaps 589: f = (int)p->engine.ps.lastf;
1.13 kristaps 590:
1.14 kristaps 591: if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13 kristaps 592: ps_putchar(p, ' ');
1.14 kristaps 593: p->engine.ps.pscol += (fonts[f].gly[0].wx / 100);
1.13 kristaps 594: return;
595: }
596:
1.4 kristaps 597: ps_putchar(p, c);
1.14 kristaps 598: c -= 32;
599: p->engine.ps.pscol += (fonts[f].gly[c].wx / 100);
1.2 kristaps 600: }
601:
602:
603: static void
1.9 kristaps 604: ps_pclose(struct termp *p)
605: {
606:
607: /*
608: * Spit out that we're exiting a word context (this is a
609: * "partial close" because we don't check the last-char buffer
610: * or anything).
611: */
612:
613: if ( ! (PS_INLINE & p->engine.ps.psstate))
614: return;
615:
616: ps_printf(p, ") show\n");
617: p->engine.ps.psstate &= ~PS_INLINE;
618: }
619:
620:
621: static void
622: ps_fclose(struct termp *p)
623: {
624:
625: /*
626: * Strong closure: if we have a last-char, spit it out after
627: * checking that we're in the right font mode. This will of
628: * course open a new scope, if applicable.
629: *
630: * Following this, close out any scope that's open.
631: */
632:
633: if ('\0' != p->engine.ps.last) {
634: if (p->engine.ps.lastf != TERMFONT_NONE) {
635: ps_pclose(p);
636: ps_setfont(p, TERMFONT_NONE);
637: }
638: ps_pletter(p, p->engine.ps.last);
639: p->engine.ps.last = '\0';
640: }
641:
642: if ( ! (PS_INLINE & p->engine.ps.psstate))
643: return;
644:
645: ps_pclose(p);
646: }
647:
648:
649: static void
1.8 kristaps 650: ps_letter(struct termp *p, char c)
651: {
652: char cc;
1.9 kristaps 653:
654: /*
655: * State machine dictates whether to buffer the last character
656: * or not. Basically, encoded words are detected by checking if
657: * we're an "8" and switching on the buffer. Then we put "8" in
658: * our buffer, and on the next charater, flush both character
659: * and buffer. Thus, "regular" words are detected by having a
660: * regular character and a regular buffer character.
661: */
662:
1.8 kristaps 663: if ('\0' == p->engine.ps.last) {
664: assert(8 != c);
665: p->engine.ps.last = c;
666: return;
667: } else if (8 == p->engine.ps.last) {
668: assert(8 != c);
1.9 kristaps 669: p->engine.ps.last = '\0';
1.8 kristaps 670: } else if (8 == c) {
671: assert(8 != p->engine.ps.last);
1.9 kristaps 672: if ('_' == p->engine.ps.last) {
673: if (p->engine.ps.lastf != TERMFONT_UNDER) {
674: ps_pclose(p);
675: ps_setfont(p, TERMFONT_UNDER);
676: }
677: } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
678: ps_pclose(p);
679: ps_setfont(p, TERMFONT_BOLD);
680: }
1.8 kristaps 681: p->engine.ps.last = c;
682: return;
683: } else {
1.9 kristaps 684: if (p->engine.ps.lastf != TERMFONT_NONE) {
685: ps_pclose(p);
686: ps_setfont(p, TERMFONT_NONE);
687: }
1.8 kristaps 688: cc = p->engine.ps.last;
689: p->engine.ps.last = c;
690: c = cc;
691: }
692:
1.9 kristaps 693: ps_pletter(p, c);
1.8 kristaps 694: }
695:
696:
697: static void
1.2 kristaps 698: ps_advance(struct termp *p, size_t len)
699: {
700:
1.9 kristaps 701: /*
702: * Advance some spaces. This can probably be made smarter,
703: * i.e., to have multiple space-separated words in the same
704: * scope, but this is easier: just close out the current scope
705: * and readjust our column settings.
706: */
1.8 kristaps 707:
1.9 kristaps 708: ps_fclose(p);
1.14 kristaps 709: p->engine.ps.pscol += len;
1.2 kristaps 710: }
711:
712:
713: static void
714: ps_endline(struct termp *p)
715: {
1.8 kristaps 716:
1.9 kristaps 717: /* Close out any scopes we have open: we're at eoln. */
718:
719: ps_fclose(p);
1.2 kristaps 720:
1.9 kristaps 721: /*
722: * If we're in the margin, don't try to recalculate our current
723: * row. XXX: if the column tries to be fancy with multiple
724: * lines, we'll do nasty stuff.
725: */
1.2 kristaps 726:
727: if (PS_MARGINS & p->engine.ps.psstate)
728: return;
729:
1.9 kristaps 730: /*
731: * Put us down a line. If we're at the page bottom, spit out a
732: * showpage and restart our row.
733: */
734:
1.2 kristaps 735: p->engine.ps.pscol = PS_CHAR_LEFT;
736: if (p->engine.ps.psrow >= PS_CHAR_HEIGHT + PS_CHAR_BOT) {
737: p->engine.ps.psrow -= PS_CHAR_HEIGHT;
738: return;
739: }
740:
1.4 kristaps 741: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
742: printf("%s", p->engine.ps.psmarg);
1.2 kristaps 743: printf("showpage\n");
744: p->engine.ps.psrow = PS_CHAR_TOP;
1.1 kristaps 745: }
1.9 kristaps 746:
747:
748: static void
749: ps_setfont(struct termp *p, enum termfont f)
750: {
751:
1.13 kristaps 752: ps_printf(p, "/%s 10 selectfont\n", fonts[(int)f].name);
1.9 kristaps 753: p->engine.ps.lastf = f;
754: }
755:
1.11 kristaps 756:
1.12 kristaps 757: /* ARGSUSED */
1.11 kristaps 758: static size_t
759: ps_width(const struct termp *p, char c)
760: {
761:
1.14 kristaps 762: if (c <= 32 || c - 32 >= MAXCHAR)
763: return(fonts[(int)TERMFONT_NONE].gly[0].wx / 100);
764:
765: c -= 32;
766: return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx / 100);
1.11 kristaps 767: }
CVSweb