]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
1 /* $Id: term_ps.c,v 1.17 2010/06/29 14:53:14 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/param.h>
36 int wx
; /* WX in AFM */
40 const char *name
; /* FontName in AFM */
41 #define MAXCHAR 95 /* total characters we can handle */
42 struct glyph gly
[MAXCHAR
]; /* glyph metrics */
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.
51 static const struct font fonts
[TERMFONT__MAX
] = {
246 { "Courier-Oblique", {
345 /* These work the buffer used by the header and footer. */
346 #define PS_BUFSLOP 128
347 #define PS_GROWBUF(p, sz) \
348 do if ((p)->engine.ps.psmargcur + (sz) > \
349 (p)->engine.ps.psmargsz) { \
350 (p)->engine.ps.psmargsz += /* CONSTCOND */ \
351 MAX(PS_BUFSLOP, (sz)); \
352 (p)->engine.ps.psmarg = realloc \
353 ((p)->engine.ps.psmarg, \
354 (p)->engine.ps.psmargsz); \
355 if (NULL == (p)->engine.ps.psmarg) { \
357 exit(EXIT_FAILURE); \
359 } while (/* CONSTCOND */ 0)
362 static void ps_letter(struct termp
*, char);
363 static void ps_begin(struct termp
*);
364 static void ps_end(struct termp
*);
365 static void ps_advance(struct termp
*, size_t);
366 static void ps_endline(struct termp
*);
367 static void ps_fclose(struct termp
*);
368 static size_t ps_width(const struct termp
*, char);
369 static void ps_pclose(struct termp
*);
370 static void ps_pletter(struct termp
*, int);
371 static void ps_printf(struct termp
*, const char *, ...);
372 static void ps_putchar(struct termp
*, char);
373 static void ps_setfont(struct termp
*, enum termfont
);
377 ps_alloc(char *outopts
)
380 size_t pagex
, pagey
, margin
;
384 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
387 /* Default is USA letter. */
392 p
->type
= TERMTYPE_PS
;
393 p
->letter
= ps_letter
;
396 p
->advance
= ps_advance
;
397 p
->endline
= ps_endline
;
403 while (outopts
&& *outopts
)
404 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
406 if (0 == strcasecmp(v
, "a4")) {
409 } else if (0 == strcasecmp(v
, "letter")) {
418 assert(margin
* 2 < pagex
);
419 assert(margin
* 2 < pagey
);
421 p
->engine
.ps
.width
= pagex
;
422 p
->engine
.ps
.height
= pagey
;
423 p
->engine
.ps
.header
= pagey
- (margin
/ 2);
424 p
->engine
.ps
.top
= pagey
- margin
;
425 p
->engine
.ps
.footer
= (margin
/ 2);
426 p
->engine
.ps
.bottom
= margin
;
427 p
->engine
.ps
.left
= margin
;
428 p
->engine
.ps
.lineheight
= 12;
430 p
->defrmargin
= pagex
- (margin
* 2);
440 p
= (struct termp
*)arg
;
442 if (p
->engine
.ps
.psmarg
)
443 free(p
->engine
.ps
.psmarg
);
450 ps_printf(struct termp
*p
, const char *fmt
, ...)
458 * If we're running in regular mode, then pipe directly into
459 * vprintf(). If we're processing margins, then push the data
460 * into our growable margin buffer.
463 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
470 * XXX: I assume that the in-margin print won't exceed
471 * PS_BUFSLOP (128 bytes), which is reasonable but still an
472 * assumption that will cause pukeage if it's not the case.
475 PS_GROWBUF(p
, PS_BUFSLOP
);
477 pos
= (int)p
->engine
.ps
.psmargcur
;
478 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
479 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
486 ps_putchar(struct termp
*p
, char c
)
490 /* See ps_printf(). */
492 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
499 pos
= (int)p
->engine
.ps
.psmargcur
++;
500 p
->engine
.ps
.psmarg
[pos
++] = c
;
501 p
->engine
.ps
.psmarg
[pos
] = '\0';
507 ps_end(struct termp
*p
)
511 * At the end of the file, do one last showpage. This is the
512 * same behaviour as groff(1) and works for multiple pages as
516 assert(0 == p
->engine
.ps
.psstate
);
517 assert('\0' == p
->engine
.ps
.last
);
518 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
519 printf("%s", p
->engine
.ps
.psmarg
);
520 p
->engine
.ps
.pages
++;
521 printf("showpage\n");
523 printf("%%%%Trailer\n");
524 printf("%%%%Pages: %zu\n", p
->engine
.ps
.pages
);
530 ps_begin(struct termp
*p
)
536 * Print margins into margin buffer. Nothing gets output to the
537 * screen yet, so we don't need to initialise the primary state.
540 if (p
->engine
.ps
.psmarg
) {
541 assert(p
->engine
.ps
.psmargsz
);
542 p
->engine
.ps
.psmarg
[0] = '\0';
545 p
->engine
.ps
.psmargcur
= 0;
546 p
->engine
.ps
.psstate
= PS_MARGINS
;
547 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
548 p
->engine
.ps
.psrow
= p
->engine
.ps
.header
;
550 ps_setfont(p
, TERMFONT_NONE
);
552 (*p
->headf
)(p
, p
->argf
);
555 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
556 p
->engine
.ps
.psrow
= p
->engine
.ps
.footer
;
558 (*p
->footf
)(p
, p
->argf
);
561 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
563 assert(0 == p
->engine
.ps
.psstate
);
564 assert(p
->engine
.ps
.psmarg
);
565 assert('\0' != p
->engine
.ps
.psmarg
[0]);
568 * Print header and initialise page state. Following this,
569 * stuff gets printed to the screen, so make sure we're sane.
574 printf("%%!PS-Adobe-3.0\n");
575 printf("%%%%Creator: mandoc-%s\n", VERSION
);
576 printf("%%%%CreationDate: %s", ctime(&t
));
577 printf("%%%%DocumentData: Clean7Bit\n");
578 printf("%%%%Orientation: Portrait\n");
579 printf("%%%%Pages: (atend)\n");
580 printf("%%%%PageOrder: Ascend\n");
581 printf("%%%%Orientation: Portrait\n");
582 printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
584 p
->engine
.ps
.height
);
585 printf("%%%%DocumentNeededResources: font");
586 for (i
= 0; i
< (int)TERMFONT__MAX
; i
++)
587 printf(" %s", fonts
[i
].name
);
588 printf("\n%%%%EndComments\n");
590 printf("%%%%Page: %zu %zu\n",
591 p
->engine
.ps
.pages
+ 1,
592 p
->engine
.ps
.pages
+ 1);
594 ps_setfont(p
, TERMFONT_NONE
);
595 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
596 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
601 ps_pletter(struct termp
*p
, int c
)
606 * If we're not in a PostScript "word" context, then open one
607 * now at the current cursor.
610 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
)) {
611 ps_printf(p
, "%zu %zu moveto\n(",
614 p
->engine
.ps
.psstate
|= PS_INLINE
;
618 * We need to escape these characters as per the PostScript
619 * specification. We would also escape non-graphable characters
620 * (like tabs), but none of them would get to this point and
621 * it's superfluous to abort() on them.
636 /* Write the character and adjust where we are on the page. */
638 f
= (int)p
->engine
.ps
.lastf
;
640 if (c
<= 32 || (c
- 32 > MAXCHAR
)) {
642 p
->engine
.ps
.pscol
+= (fonts
[f
].gly
[0].wx
/ 100);
648 p
->engine
.ps
.pscol
+= (fonts
[f
].gly
[c
].wx
/ 100);
653 ps_pclose(struct termp
*p
)
657 * Spit out that we're exiting a word context (this is a
658 * "partial close" because we don't check the last-char buffer
662 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
665 ps_printf(p
, ") show\n");
666 p
->engine
.ps
.psstate
&= ~PS_INLINE
;
671 ps_fclose(struct termp
*p
)
675 * Strong closure: if we have a last-char, spit it out after
676 * checking that we're in the right font mode. This will of
677 * course open a new scope, if applicable.
679 * Following this, close out any scope that's open.
682 if ('\0' != p
->engine
.ps
.last
) {
683 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
685 ps_setfont(p
, TERMFONT_NONE
);
687 ps_pletter(p
, p
->engine
.ps
.last
);
688 p
->engine
.ps
.last
= '\0';
691 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
699 ps_letter(struct termp
*p
, char c
)
704 * State machine dictates whether to buffer the last character
705 * or not. Basically, encoded words are detected by checking if
706 * we're an "8" and switching on the buffer. Then we put "8" in
707 * our buffer, and on the next charater, flush both character
708 * and buffer. Thus, "regular" words are detected by having a
709 * regular character and a regular buffer character.
712 if ('\0' == p
->engine
.ps
.last
) {
714 p
->engine
.ps
.last
= c
;
716 } else if (8 == p
->engine
.ps
.last
) {
718 p
->engine
.ps
.last
= '\0';
720 assert(8 != p
->engine
.ps
.last
);
721 if ('_' == p
->engine
.ps
.last
) {
722 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
724 ps_setfont(p
, TERMFONT_UNDER
);
726 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
728 ps_setfont(p
, TERMFONT_BOLD
);
730 p
->engine
.ps
.last
= c
;
733 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
735 ps_setfont(p
, TERMFONT_NONE
);
737 cc
= p
->engine
.ps
.last
;
738 p
->engine
.ps
.last
= c
;
747 ps_advance(struct termp
*p
, size_t len
)
751 * Advance some spaces. This can probably be made smarter,
752 * i.e., to have multiple space-separated words in the same
753 * scope, but this is easier: just close out the current scope
754 * and readjust our column settings.
758 p
->engine
.ps
.pscol
+= len
;
763 ps_endline(struct termp
*p
)
766 /* Close out any scopes we have open: we're at eoln. */
771 * If we're in the margin, don't try to recalculate our current
772 * row. XXX: if the column tries to be fancy with multiple
773 * lines, we'll do nasty stuff.
776 if (PS_MARGINS
& p
->engine
.ps
.psstate
)
780 * Put us down a line. If we're at the page bottom, spit out a
781 * showpage and restart our row.
784 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
785 if (p
->engine
.ps
.psrow
>= p
->engine
.ps
.lineheight
+
786 p
->engine
.ps
.bottom
) {
787 p
->engine
.ps
.psrow
-= p
->engine
.ps
.lineheight
;
791 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
792 printf("%s", p
->engine
.ps
.psmarg
);
793 printf("%%%%Page: %zu %zu\n",
794 p
->engine
.ps
.pages
+ 1,
795 p
->engine
.ps
.pages
+ 1);
796 printf("showpage\n");
797 p
->engine
.ps
.pages
++;
798 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
803 ps_setfont(struct termp
*p
, enum termfont f
)
806 assert(f
< TERMFONT__MAX
);
807 ps_printf(p
, "/%s 10 selectfont\n", fonts
[(int)f
].name
);
808 p
->engine
.ps
.lastf
= f
;
814 ps_width(const struct termp
*p
, char c
)
817 if (c
<= 32 || c
- 32 >= MAXCHAR
)
818 return(fonts
[(int)TERMFONT_NONE
].gly
[0].wx
/ 100);
821 return(fonts
[(int)TERMFONT_NONE
].gly
[(int)c
].wx
/ 100);