]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
1 /* $Id: term_ps.c,v 1.31 2010/07/04 20:06:59 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 /* Convert PostScript point "x" to an AFM unit. */
37 #define PNT2AFM(p, x) /* LINTED */ \
38 (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
40 /* Convert an AFM unit "x" to a PostScript points */
41 #define AFM2PNT(p, x) /* LINTED */ \
42 (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
45 size_t wx
; /* WX in AFM */
49 const char *name
; /* FontName in AFM */
50 #define MAXCHAR 95 /* total characters we can handle */
51 struct glyph gly
[MAXCHAR
]; /* glyph metrics */
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.
60 static const struct font fonts
[TERMFONT__MAX
] = {
354 /* These work the buffer used by the header and footer. */
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) { \
366 exit(EXIT_FAILURE); \
368 } while (/* CONSTCOND */ 0)
371 static double ps_hspan(const struct termp
*,
372 const struct roffsu
*);
373 static size_t ps_width(const struct termp
*, char);
374 static void ps_advance(struct termp
*, size_t);
375 static void ps_begin(struct termp
*);
376 static void ps_end(struct termp
*);
377 static void ps_endline(struct termp
*);
378 static void ps_fclose(struct termp
*);
379 static void ps_letter(struct termp
*, char);
380 static void ps_pclose(struct termp
*);
381 static void ps_pletter(struct termp
*, int);
382 static void ps_printf(struct termp
*, const char *, ...);
383 static void ps_putchar(struct termp
*, char);
384 static void ps_setfont(struct termp
*, enum termfont
);
388 ps_alloc(char *outopts
)
391 size_t pagex
, pagey
, marginx
, marginy
, lineheight
;
396 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
399 p
->advance
= ps_advance
;
402 p
->endline
= ps_endline
;
404 p
->letter
= ps_letter
;
405 p
->type
= TERMTYPE_PS
;
413 while (outopts
&& *outopts
)
414 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
422 /* Default to US letter (millimetres). */
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.
434 if (pp
&& strcasecmp(pp
, "letter")) {
435 if (0 == strcasecmp(pp
, "a3")) {
438 } else if (0 == strcasecmp(pp
, "a4")) {
441 } else if (0 == strcasecmp(pp
, "a5")) {
444 } else if (0 == strcasecmp(pp
, "legal")) {
447 } else if (2 != sscanf(pp
, "%zux%zu", &pagex
, &pagey
))
448 fprintf(stderr
, "%s: Unknown paper\n", pp
);
449 } else if (NULL
== pp
)
453 * This MUST be defined before any PNT2AFM or AFM2PNT
454 * calculations occur.
457 p
->engine
.ps
.scale
= 11;
459 /* Remember millimetres -> AFM units. */
461 pagex
= PNT2AFM(p
, ((double)pagex
* 2.834));
462 pagey
= PNT2AFM(p
, ((double)pagey
* 2.834));
464 /* Margins are 1/9 the page x and y. */
466 marginx
= /* LINTED */
467 (size_t)((double)pagex
/ 9.0);
468 marginy
= /* LINTED */
469 (size_t)((double)pagey
/ 9.0);
471 /* Line-height is 1.4em. */
473 lineheight
= PNT2AFM(p
, ((double)p
->engine
.ps
.scale
* 1.4));
475 p
->engine
.ps
.width
= pagex
;
476 p
->engine
.ps
.height
= pagey
;
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
;
482 p
->engine
.ps
.lineheight
= lineheight
;
484 p
->defrmargin
= pagex
- (marginx
* 2);
494 p
= (struct termp
*)arg
;
496 if (p
->engine
.ps
.psmarg
)
497 free(p
->engine
.ps
.psmarg
);
504 ps_printf(struct termp
*p
, const char *fmt
, ...)
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.
517 if ( ! (PS_MARGINS
& p
->engine
.ps
.flags
)) {
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.
529 PS_GROWBUF(p
, PS_BUFSLOP
);
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
);
540 ps_putchar(struct termp
*p
, char c
)
544 /* See ps_printf(). */
546 if ( ! (PS_MARGINS
& p
->engine
.ps
.flags
)) {
553 pos
= (int)p
->engine
.ps
.psmargcur
++;
554 p
->engine
.ps
.psmarg
[pos
++] = c
;
555 p
->engine
.ps
.psmarg
[pos
] = '\0';
561 ps_end(struct termp
*p
)
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
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");
579 printf("%%%%Trailer\n");
580 printf("%%%%Pages: %zu\n", p
->engine
.ps
.pages
);
586 ps_begin(struct termp
*p
)
592 * Print margins into margin buffer. Nothing gets output to the
593 * screen yet, so we don't need to initialise the primary state.
596 if (p
->engine
.ps
.psmarg
) {
597 assert(p
->engine
.ps
.psmargsz
);
598 p
->engine
.ps
.psmarg
[0] = '\0';
601 p
->engine
.ps
.psmargcur
= 0;
602 p
->engine
.ps
.flags
= PS_MARGINS
;
603 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
604 p
->engine
.ps
.psrow
= p
->engine
.ps
.header
;
606 ps_setfont(p
, TERMFONT_NONE
);
608 (*p
->headf
)(p
, p
->argf
);
611 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
612 p
->engine
.ps
.psrow
= p
->engine
.ps
.footer
;
614 (*p
->footf
)(p
, p
->argf
);
617 p
->engine
.ps
.flags
&= ~PS_MARGINS
;
619 assert(0 == p
->engine
.ps
.flags
);
620 assert(p
->engine
.ps
.psmarg
);
621 assert('\0' != p
->engine
.ps
.psmarg
[0]);
624 * Print header and initialise page state. Following this,
625 * stuff gets printed to the screen, so make sure we're sane.
630 printf("%%!PS-Adobe-3.0\n");
631 printf("%%%%Creator: mandoc-%s\n", VERSION
);
632 printf("%%%%CreationDate: %s", ctime(&t
));
633 printf("%%%%DocumentData: Clean7Bit\n");
634 printf("%%%%Orientation: Portrait\n");
635 printf("%%%%Pages: (atend)\n");
636 printf("%%%%PageOrder: Ascend\n");
637 printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
638 AFM2PNT(p
, p
->engine
.ps
.width
),
639 AFM2PNT(p
, p
->engine
.ps
.height
));
640 printf("%%%%DocumentNeededResources: font");
641 for (i
= 0; i
< (int)TERMFONT__MAX
; i
++)
642 printf(" %s", fonts
[i
].name
);
643 printf("\n%%%%EndComments\n");
645 printf("%%%%Page: %zu %zu\n",
646 p
->engine
.ps
.pages
+ 1,
647 p
->engine
.ps
.pages
+ 1);
649 ps_setfont(p
, TERMFONT_NONE
);
650 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
651 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
652 p
->engine
.ps
.flags
|= PS_NEWPAGE
;
657 ps_pletter(struct termp
*p
, int c
)
662 * If we're not in a PostScript "word" context, then open one
663 * now at the current cursor.
666 if (PS_NEWPAGE
& p
->engine
.ps
.flags
)
667 printf("%%%%Page: %zu %zu\n",
668 p
->engine
.ps
.pages
+ 1,
669 p
->engine
.ps
.pages
+ 1);
671 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
)) {
672 ps_printf(p
, "%zu %zu moveto\n(",
673 AFM2PNT(p
, p
->engine
.ps
.pscol
),
674 AFM2PNT(p
, p
->engine
.ps
.psrow
));
675 p
->engine
.ps
.flags
|= PS_INLINE
;
676 p
->engine
.ps
.flags
&= ~PS_NEWPAGE
;
679 assert( ! (PS_NEWPAGE
& p
->engine
.ps
.flags
));
682 * We need to escape these characters as per the PostScript
683 * specification. We would also escape non-graphable characters
684 * (like tabs), but none of them would get to this point and
685 * it's superfluous to abort() on them.
700 /* Write the character and adjust where we are on the page. */
702 f
= (int)p
->engine
.ps
.lastf
;
704 if (c
<= 32 || (c
- 32 > MAXCHAR
)) {
706 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[0].wx
;
710 ps_putchar(p
, (char)c
);
712 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[c
].wx
;
717 ps_pclose(struct termp
*p
)
721 * Spit out that we're exiting a word context (this is a
722 * "partial close" because we don't check the last-char buffer
726 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
))
729 ps_printf(p
, ") show\n");
730 p
->engine
.ps
.flags
&= ~PS_INLINE
;
735 ps_fclose(struct termp
*p
)
739 * Strong closure: if we have a last-char, spit it out after
740 * checking that we're in the right font mode. This will of
741 * course open a new scope, if applicable.
743 * Following this, close out any scope that's open.
746 if ('\0' != p
->engine
.ps
.last
) {
747 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
749 ps_setfont(p
, TERMFONT_NONE
);
751 ps_pletter(p
, p
->engine
.ps
.last
);
752 p
->engine
.ps
.last
= '\0';
755 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
))
763 ps_letter(struct termp
*p
, char c
)
768 * State machine dictates whether to buffer the last character
769 * or not. Basically, encoded words are detected by checking if
770 * we're an "8" and switching on the buffer. Then we put "8" in
771 * our buffer, and on the next charater, flush both character
772 * and buffer. Thus, "regular" words are detected by having a
773 * regular character and a regular buffer character.
776 if ('\0' == p
->engine
.ps
.last
) {
778 p
->engine
.ps
.last
= c
;
780 } else if (8 == p
->engine
.ps
.last
) {
782 p
->engine
.ps
.last
= '\0';
784 assert(8 != p
->engine
.ps
.last
);
785 if ('_' == p
->engine
.ps
.last
) {
786 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
788 ps_setfont(p
, TERMFONT_UNDER
);
790 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
792 ps_setfont(p
, TERMFONT_BOLD
);
794 p
->engine
.ps
.last
= c
;
797 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
799 ps_setfont(p
, TERMFONT_NONE
);
801 cc
= p
->engine
.ps
.last
;
802 p
->engine
.ps
.last
= c
;
811 ps_advance(struct termp
*p
, size_t len
)
815 * Advance some spaces. This can probably be made smarter,
816 * i.e., to have multiple space-separated words in the same
817 * scope, but this is easier: just close out the current scope
818 * and readjust our column settings.
822 p
->engine
.ps
.pscol
+= len
;
827 ps_endline(struct termp
*p
)
830 /* Close out any scopes we have open: we're at eoln. */
835 * If we're in the margin, don't try to recalculate our current
836 * row. XXX: if the column tries to be fancy with multiple
837 * lines, we'll do nasty stuff.
840 if (PS_MARGINS
& p
->engine
.ps
.flags
)
845 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
847 /* If we haven't printed anything, return. */
849 if (PS_NEWPAGE
& p
->engine
.ps
.flags
)
853 * Put us down a line. If we're at the page bottom, spit out a
854 * showpage and restart our row.
857 if (p
->engine
.ps
.psrow
>= p
->engine
.ps
.lineheight
+
858 p
->engine
.ps
.bottom
) {
859 p
->engine
.ps
.psrow
-= p
->engine
.ps
.lineheight
;
863 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
864 printf("%s", p
->engine
.ps
.psmarg
);
865 printf("showpage\n");
866 p
->engine
.ps
.pages
++;
867 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
868 assert( ! (PS_NEWPAGE
& p
->engine
.ps
.flags
));
869 p
->engine
.ps
.flags
|= PS_NEWPAGE
;
874 ps_setfont(struct termp
*p
, enum termfont f
)
877 assert(f
< TERMFONT__MAX
);
878 ps_printf(p
, "/%s %zu selectfont\n",
879 fonts
[(int)f
].name
, p
->engine
.ps
.scale
);
880 p
->engine
.ps
.lastf
= f
;
886 ps_width(const struct termp
*p
, char c
)
889 if (c
<= 32 || c
- 32 >= MAXCHAR
)
890 return(fonts
[(int)TERMFONT_NONE
].gly
[0].wx
);
893 return(fonts
[(int)TERMFONT_NONE
].gly
[(int)c
].wx
);
898 ps_hspan(const struct termp
*p
, const struct roffsu
*su
)
903 * All of these measurements are derived by converting from the
904 * native measurement to AFM units.
909 r
= PNT2AFM(p
, su
->scale
* 28.34);
912 r
= PNT2AFM(p
, su
->scale
* 72);
915 r
= PNT2AFM(p
, su
->scale
* 12);
918 r
= PNT2AFM(p
, su
->scale
* 100);
922 fonts
[(int)TERMFONT_NONE
].gly
[109 - 32].wx
;
925 r
= PNT2AFM(p
, su
->scale
* 2.834);
929 fonts
[(int)TERMFONT_NONE
].gly
[110 - 32].wx
;
932 r
= su
->scale
* p
->engine
.ps
.lineheight
;