Annotation of mandoc/term_ps.c, Revision 1.14
1.14 ! kristaps 1: /* $Id: term_ps.c,v 1.13 2010/06/28 13:45:28 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.13 kristaps 34: struct glyph {
35: int wx; /* WX in AFM */
36: };
37:
38: #define MAXCHAR 95
39:
40: struct font {
41: const char *name; /* FontName in AFM */
42: struct glyph gly[MAXCHAR]; /* glyph metrics */
43: };
44:
45: /*
46: * We define, for the time being, three fonts: bold, oblique/italic, and
47: * normal (roman). The following table hard-codes the font metrics for
48: * ASCII, i.e., 32--127.
49: */
50:
51: static const struct font fonts[3] = {
52: { "Courier", {
53: { 600 },
54: { 600 },
55: { 600 },
56: { 600 },
57: { 600 },
58: { 600 },
59: { 600 },
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: } },
149: { "Courier-Bold", {
150: { 600 },
151: { 600 },
152: { 600 },
153: { 600 },
154: { 600 },
155: { 600 },
156: { 600 },
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: } },
246: { "Courier-Oblique", {
247: { 600 },
248: { 600 },
249: { 600 },
250: { 600 },
251: { 600 },
252: { 600 },
253: { 600 },
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: } },
343: };
344:
1.2 kristaps 345: #define PS_CHAR_HEIGHT 12
346: #define PS_CHAR_TOPMARG (792 - 24)
347: #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
348: #define PS_CHAR_LEFT 36
349: #define PS_CHAR_BOTMARG 24
350: #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
351:
1.4 kristaps 352: #define PS_BUFSLOP 128
353: #define PS_GROWBUF(p, sz) \
354: do if ((p)->engine.ps.psmargcur + (sz) > \
355: (p)->engine.ps.psmargsz) { \
356: (p)->engine.ps.psmargsz += /* CONSTCOND */ \
357: MAX(PS_BUFSLOP, (sz)); \
358: (p)->engine.ps.psmarg = realloc \
359: ((p)->engine.ps.psmarg, \
360: (p)->engine.ps.psmargsz); \
361: if (NULL == (p)->engine.ps.psmarg) { \
362: perror(NULL); \
363: exit(EXIT_FAILURE); \
364: } \
365: } while (/* CONSTCOND */ 0)
366:
1.9 kristaps 367:
1.2 kristaps 368: static void ps_letter(struct termp *, char);
369: static void ps_begin(struct termp *);
370: static void ps_end(struct termp *);
371: static void ps_advance(struct termp *, size_t);
372: static void ps_endline(struct termp *);
1.9 kristaps 373: static void ps_fclose(struct termp *);
1.11 kristaps 374: static size_t ps_width(const struct termp *, char);
1.9 kristaps 375: static void ps_pclose(struct termp *);
1.13 kristaps 376: static void ps_pletter(struct termp *, int);
1.4 kristaps 377: static void ps_printf(struct termp *, const char *, ...);
378: static void ps_putchar(struct termp *, char);
1.9 kristaps 379: static void ps_setfont(struct termp *, enum termfont);
1.2 kristaps 380:
381:
1.1 kristaps 382: void *
383: ps_alloc(void)
384: {
385: struct termp *p;
386:
387: if (NULL == (p = term_alloc(TERMENC_ASCII)))
388: return(NULL);
389:
1.14 ! kristaps 390: p->defrmargin = 612 - (PS_CHAR_LEFT * 2);
1.1 kristaps 391: p->type = TERMTYPE_PS;
1.2 kristaps 392: p->letter = ps_letter;
393: p->begin = ps_begin;
394: p->end = ps_end;
395: p->advance = ps_advance;
396: p->endline = ps_endline;
1.11 kristaps 397: p->width = ps_width;
1.1 kristaps 398: return(p);
399: }
400:
401:
402: void
403: ps_free(void *arg)
404: {
1.4 kristaps 405: struct termp *p;
406:
407: p = (struct termp *)arg;
408:
409: if (p->engine.ps.psmarg)
410: free(p->engine.ps.psmarg);
411:
412: term_free(p);
413: }
414:
415:
416: static void
417: ps_printf(struct termp *p, const char *fmt, ...)
418: {
419: va_list ap;
420: int pos;
421:
422: va_start(ap, fmt);
423:
424: /*
425: * If we're running in regular mode, then pipe directly into
426: * vprintf(). If we're processing margins, then push the data
427: * into our growable margin buffer.
428: */
1.1 kristaps 429:
1.4 kristaps 430: if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
431: vprintf(fmt, ap);
432: va_end(ap);
433: return;
434: }
435:
436: /*
437: * XXX: I assume that the in-margin print won't exceed
438: * PS_BUFSLOP (128 bytes), which is reasonable but still an
439: * assumption that will cause pukeage if it's not the case.
440: */
441:
442: PS_GROWBUF(p, PS_BUFSLOP);
443:
444: pos = (int)p->engine.ps.psmargcur;
445: vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
446: p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.5 kristaps 447:
448: va_end(ap);
1.4 kristaps 449: }
450:
451:
452: static void
453: ps_putchar(struct termp *p, char c)
454: {
455: int pos;
456:
457: /* See ps_printf(). */
458:
459: if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
460: putchar(c);
461: return;
462: }
463:
464: PS_GROWBUF(p, 2);
465:
466: pos = (int)p->engine.ps.psmargcur++;
1.5 kristaps 467: p->engine.ps.psmarg[pos++] = c;
1.4 kristaps 468: p->engine.ps.psmarg[pos] = '\0';
1.2 kristaps 469: }
470:
471:
1.3 kristaps 472: /* ARGSUSED */
1.2 kristaps 473: static void
474: ps_end(struct termp *p)
475: {
476:
1.4 kristaps 477: /*
478: * At the end of the file, do one last showpage. This is the
479: * same behaviour as groff(1) and works for multiple pages as
480: * well as just one.
481: */
482:
1.8 kristaps 483: assert(0 == p->engine.ps.psstate);
484: assert('\0' == p->engine.ps.last);
1.4 kristaps 485: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
486: printf("%s", p->engine.ps.psmarg);
487: printf("showpage\n");
1.2 kristaps 488: printf("%s\n", "%%EOF");
489: }
490:
491:
492: static void
493: ps_begin(struct termp *p)
494: {
1.13 kristaps 495: time_t t;
1.2 kristaps 496:
1.9 kristaps 497: /*
498: * Print margins into margin buffer. Nothing gets output to the
499: * screen yet, so we don't need to initialise the primary state.
1.2 kristaps 500: */
501:
1.4 kristaps 502: if (p->engine.ps.psmarg) {
503: assert(p->engine.ps.psmargsz);
504: p->engine.ps.psmarg[0] = '\0';
505: }
506:
507: p->engine.ps.psmargcur = 0;
1.9 kristaps 508: p->engine.ps.psstate = PS_MARGINS;
1.4 kristaps 509: p->engine.ps.pscol = PS_CHAR_LEFT;
510: p->engine.ps.psrow = PS_CHAR_TOPMARG;
511:
1.9 kristaps 512: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 513:
514: (*p->headf)(p, p->argf);
515: (*p->endline)(p);
516:
517: p->engine.ps.pscol = PS_CHAR_LEFT;
518: p->engine.ps.psrow = PS_CHAR_BOTMARG;
519:
520: (*p->footf)(p, p->argf);
521: (*p->endline)(p);
522:
523: p->engine.ps.psstate &= ~PS_MARGINS;
1.9 kristaps 524:
1.4 kristaps 525: assert(0 == p->engine.ps.psstate);
1.9 kristaps 526: assert(p->engine.ps.psmarg);
527: assert('\0' != p->engine.ps.psmarg[0]);
528:
529: /*
530: * Print header and initialise page state. Following this,
531: * stuff gets printed to the screen, so make sure we're sane.
532: */
1.4 kristaps 533:
1.13 kristaps 534: t = time(NULL);
535:
536: printf("%%!PS\n");
537: printf("%%%%Creator: mandoc-%s\n", VERSION);
538: printf("%%%%CreationDate: %s", ctime(&t));
539: printf("%%%%PageOrder: Ascend\n");
540: printf("%%%%Orientation: Portrait\n");
541: printf("%%%%EndComments\n");
542:
1.9 kristaps 543: ps_setfont(p, TERMFONT_NONE);
1.4 kristaps 544: p->engine.ps.pscol = PS_CHAR_LEFT;
545: p->engine.ps.psrow = PS_CHAR_TOP;
1.2 kristaps 546: }
547:
548:
549: static void
1.13 kristaps 550: ps_pletter(struct termp *p, int c)
1.2 kristaps 551: {
1.14 ! kristaps 552: int f;
1.2 kristaps 553:
1.9 kristaps 554: /*
555: * If we're not in a PostScript "word" context, then open one
556: * now at the current cursor.
557: */
558:
1.2 kristaps 559: if ( ! (PS_INLINE & p->engine.ps.psstate)) {
1.4 kristaps 560: ps_printf(p, "%zu %zu moveto\n(",
1.2 kristaps 561: p->engine.ps.pscol,
562: p->engine.ps.psrow);
563: p->engine.ps.psstate |= PS_INLINE;
564: }
565:
566: /*
567: * We need to escape these characters as per the PostScript
568: * specification. We would also escape non-graphable characters
569: * (like tabs), but none of them would get to this point and
570: * it's superfluous to abort() on them.
571: */
572:
573: switch (c) {
574: case ('('):
575: /* FALLTHROUGH */
576: case (')'):
577: /* FALLTHROUGH */
578: case ('\\'):
1.4 kristaps 579: ps_putchar(p, '\\');
1.2 kristaps 580: break;
581: default:
582: break;
583: }
584:
585: /* Write the character and adjust where we are on the page. */
1.9 kristaps 586:
1.14 ! kristaps 587: f = (int)p->engine.ps.lastf;
1.13 kristaps 588:
1.14 ! kristaps 589: if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13 kristaps 590: ps_putchar(p, ' ');
1.14 ! kristaps 591: p->engine.ps.pscol += (fonts[f].gly[0].wx / 100);
1.13 kristaps 592: return;
593: }
594:
1.4 kristaps 595: ps_putchar(p, c);
1.14 ! kristaps 596: c -= 32;
! 597: p->engine.ps.pscol += (fonts[f].gly[c].wx / 100);
1.2 kristaps 598: }
599:
600:
601: static void
1.9 kristaps 602: ps_pclose(struct termp *p)
603: {
604:
605: /*
606: * Spit out that we're exiting a word context (this is a
607: * "partial close" because we don't check the last-char buffer
608: * or anything).
609: */
610:
611: if ( ! (PS_INLINE & p->engine.ps.psstate))
612: return;
613:
614: ps_printf(p, ") show\n");
615: p->engine.ps.psstate &= ~PS_INLINE;
616: }
617:
618:
619: static void
620: ps_fclose(struct termp *p)
621: {
622:
623: /*
624: * Strong closure: if we have a last-char, spit it out after
625: * checking that we're in the right font mode. This will of
626: * course open a new scope, if applicable.
627: *
628: * Following this, close out any scope that's open.
629: */
630:
631: if ('\0' != p->engine.ps.last) {
632: if (p->engine.ps.lastf != TERMFONT_NONE) {
633: ps_pclose(p);
634: ps_setfont(p, TERMFONT_NONE);
635: }
636: ps_pletter(p, p->engine.ps.last);
637: p->engine.ps.last = '\0';
638: }
639:
640: if ( ! (PS_INLINE & p->engine.ps.psstate))
641: return;
642:
643: ps_pclose(p);
644: }
645:
646:
647: static void
1.8 kristaps 648: ps_letter(struct termp *p, char c)
649: {
650: char cc;
1.9 kristaps 651:
652: /*
653: * State machine dictates whether to buffer the last character
654: * or not. Basically, encoded words are detected by checking if
655: * we're an "8" and switching on the buffer. Then we put "8" in
656: * our buffer, and on the next charater, flush both character
657: * and buffer. Thus, "regular" words are detected by having a
658: * regular character and a regular buffer character.
659: */
660:
1.8 kristaps 661: if ('\0' == p->engine.ps.last) {
662: assert(8 != c);
663: p->engine.ps.last = c;
664: return;
665: } else if (8 == p->engine.ps.last) {
666: assert(8 != c);
1.9 kristaps 667: p->engine.ps.last = '\0';
1.8 kristaps 668: } else if (8 == c) {
669: assert(8 != p->engine.ps.last);
1.9 kristaps 670: if ('_' == p->engine.ps.last) {
671: if (p->engine.ps.lastf != TERMFONT_UNDER) {
672: ps_pclose(p);
673: ps_setfont(p, TERMFONT_UNDER);
674: }
675: } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
676: ps_pclose(p);
677: ps_setfont(p, TERMFONT_BOLD);
678: }
1.8 kristaps 679: p->engine.ps.last = c;
680: return;
681: } else {
1.9 kristaps 682: if (p->engine.ps.lastf != TERMFONT_NONE) {
683: ps_pclose(p);
684: ps_setfont(p, TERMFONT_NONE);
685: }
1.8 kristaps 686: cc = p->engine.ps.last;
687: p->engine.ps.last = c;
688: c = cc;
689: }
690:
1.9 kristaps 691: ps_pletter(p, c);
1.8 kristaps 692: }
693:
694:
695: static void
1.2 kristaps 696: ps_advance(struct termp *p, size_t len)
697: {
698:
1.9 kristaps 699: /*
700: * Advance some spaces. This can probably be made smarter,
701: * i.e., to have multiple space-separated words in the same
702: * scope, but this is easier: just close out the current scope
703: * and readjust our column settings.
704: */
1.8 kristaps 705:
1.9 kristaps 706: ps_fclose(p);
1.14 ! kristaps 707: p->engine.ps.pscol += len;
1.2 kristaps 708: }
709:
710:
711: static void
712: ps_endline(struct termp *p)
713: {
1.8 kristaps 714:
1.9 kristaps 715: /* Close out any scopes we have open: we're at eoln. */
716:
717: ps_fclose(p);
1.2 kristaps 718:
1.9 kristaps 719: /*
720: * If we're in the margin, don't try to recalculate our current
721: * row. XXX: if the column tries to be fancy with multiple
722: * lines, we'll do nasty stuff.
723: */
1.2 kristaps 724:
725: if (PS_MARGINS & p->engine.ps.psstate)
726: return;
727:
1.9 kristaps 728: /*
729: * Put us down a line. If we're at the page bottom, spit out a
730: * showpage and restart our row.
731: */
732:
1.2 kristaps 733: p->engine.ps.pscol = PS_CHAR_LEFT;
734: if (p->engine.ps.psrow >= PS_CHAR_HEIGHT + PS_CHAR_BOT) {
735: p->engine.ps.psrow -= PS_CHAR_HEIGHT;
736: return;
737: }
738:
1.4 kristaps 739: assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
740: printf("%s", p->engine.ps.psmarg);
1.2 kristaps 741: printf("showpage\n");
742: p->engine.ps.psrow = PS_CHAR_TOP;
1.1 kristaps 743: }
1.9 kristaps 744:
745:
746: static void
747: ps_setfont(struct termp *p, enum termfont f)
748: {
749:
1.13 kristaps 750: ps_printf(p, "/%s 10 selectfont\n", fonts[(int)f].name);
1.9 kristaps 751: p->engine.ps.lastf = f;
752: }
753:
1.11 kristaps 754:
1.12 kristaps 755: /* ARGSUSED */
1.11 kristaps 756: static size_t
757: ps_width(const struct termp *p, char c)
758: {
759:
1.14 ! kristaps 760: if (c <= 32 || c - 32 >= MAXCHAR)
! 761: return(fonts[(int)TERMFONT_NONE].gly[0].wx / 100);
! 762:
! 763: c -= 32;
! 764: return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx / 100);
1.11 kristaps 765: }
CVSweb