]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
1 /* $Id: term_ps.c,v 1.29 2010/07/04 19:42:25 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 #define MINMARGIN_MM 20 /* Minimum 2cm margins. */
37 #define MINMARGIN_PNT 56.68
38 #define DEFPAGEX_MM 216 /* Default page size is US-letter. */
39 #define DEFPAGEY_MM 279
41 /* Convert PostScript point "x" to an AFM unit. */
42 #define PNT2AFM(p, x) /* LINTED */ \
43 (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
45 /* Convert an AFM unit "x" to a PostScript points */
46 #define AFM2PNT(p, x) /* LINTED */ \
47 (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
50 size_t wx
; /* WX in AFM */
54 const char *name
; /* FontName in AFM */
55 #define MAXCHAR 95 /* total characters we can handle */
56 struct glyph gly
[MAXCHAR
]; /* glyph metrics */
60 * We define, for the time being, three fonts: bold, oblique/italic, and
61 * normal (roman). The following table hard-codes the font metrics for
62 * ASCII, i.e., 32--127.
65 static const struct font fonts
[TERMFONT__MAX
] = {
359 /* These work the buffer used by the header and footer. */
360 #define PS_BUFSLOP 128
361 #define PS_GROWBUF(p, sz) \
362 do if ((p)->engine.ps.psmargcur + (sz) > \
363 (p)->engine.ps.psmargsz) { \
364 (p)->engine.ps.psmargsz += /* CONSTCOND */ \
365 MAX(PS_BUFSLOP, (sz)); \
366 (p)->engine.ps.psmarg = realloc \
367 ((p)->engine.ps.psmarg, \
368 (p)->engine.ps.psmargsz); \
369 if (NULL == (p)->engine.ps.psmarg) { \
371 exit(EXIT_FAILURE); \
373 } while (/* CONSTCOND */ 0)
376 static double ps_hspan(const struct termp
*,
377 const struct roffsu
*);
378 static size_t ps_width(const struct termp
*, char);
379 static void ps_advance(struct termp
*, size_t);
380 static void ps_begin(struct termp
*);
381 static void ps_end(struct termp
*);
382 static void ps_endline(struct termp
*);
383 static void ps_fclose(struct termp
*);
384 static void ps_letter(struct termp
*, char);
385 static void ps_pclose(struct termp
*);
386 static void ps_pletter(struct termp
*, int);
387 static void ps_printf(struct termp
*, const char *, ...);
388 static void ps_putchar(struct termp
*, char);
389 static void ps_setfont(struct termp
*, enum termfont
);
393 ps_alloc(char *outopts
)
396 size_t pagex
, pagey
, marginx
, marginy
, lineheight
;
401 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
404 p
->advance
= ps_advance
;
407 p
->endline
= ps_endline
;
409 p
->letter
= ps_letter
;
410 p
->type
= TERMTYPE_PS
;
418 while (outopts
&& *outopts
)
419 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
427 /* Default to US letter (millimetres). */
433 * The ISO-269 paper sizes can be calculated automatically, but
434 * it would require bringing in -lm for pow() and I'd rather not
435 * do that. So just do it the easy way for now. Since this
436 * only happens once, I'm not terribly concerned.
439 if (pp
&& strcasecmp(pp
, "letter")) {
440 if (0 == strcasecmp(pp
, "a3")) {
443 } else if (0 == strcasecmp(pp
, "a4")) {
446 } else if (0 == strcasecmp(pp
, "a5")) {
449 } else if (0 == strcasecmp(pp
, "legal")) {
452 } else if (2 != sscanf(pp
, "%zux%zu", &pagex
, &pagey
))
453 fprintf(stderr
, "%s: Unknown paper\n", pp
);
454 } else if (NULL
== pp
)
457 /* Enforce minimum page size >= (2 times) min-margin. */
459 if ((2 * MINMARGIN_MM
) >= pagex
) {
460 fprintf(stderr
, "%s: Insufficient page width\n", pp
);
462 } else if ((2 * MINMARGIN_MM
>= pagey
)) {
463 fprintf(stderr
, "%s: Insufficient page length\n", pp
);
468 * This MUST be defined before any PNT2AFM or AFM2PNT
469 * calculations occur.
472 p
->engine
.ps
.scale
= 11;
474 /* Remember millimetres -> AFM units. */
476 pagex
= PNT2AFM(p
, ((double)pagex
* 2.834));
477 pagey
= PNT2AFM(p
, ((double)pagey
* 2.834));
479 /* Margins are 1/9 the page x and y. */
481 marginx
= /* LINTED */
482 (size_t)((double)pagex
/ 9.0);
483 marginy
= /* LINTED */
484 (size_t)((double)pagey
/ 9.0);
486 lineheight
= PNT2AFM(p
, 16);
488 p
->engine
.ps
.width
= pagex
;
489 p
->engine
.ps
.height
= pagey
;
490 p
->engine
.ps
.header
= pagey
- (marginy
/ 2) - (lineheight
/ 2);
491 p
->engine
.ps
.top
= pagey
- marginy
;
492 p
->engine
.ps
.footer
= (marginy
/ 2) - (lineheight
/ 2);
493 p
->engine
.ps
.bottom
= marginy
;
494 p
->engine
.ps
.left
= marginx
;
495 p
->engine
.ps
.lineheight
= lineheight
;
497 p
->defrmargin
= pagex
- (marginx
* 2);
507 p
= (struct termp
*)arg
;
509 if (p
->engine
.ps
.psmarg
)
510 free(p
->engine
.ps
.psmarg
);
517 ps_printf(struct termp
*p
, const char *fmt
, ...)
525 * If we're running in regular mode, then pipe directly into
526 * vprintf(). If we're processing margins, then push the data
527 * into our growable margin buffer.
530 if ( ! (PS_MARGINS
& p
->engine
.ps
.flags
)) {
537 * XXX: I assume that the in-margin print won't exceed
538 * PS_BUFSLOP (128 bytes), which is reasonable but still an
539 * assumption that will cause pukeage if it's not the case.
542 PS_GROWBUF(p
, PS_BUFSLOP
);
544 pos
= (int)p
->engine
.ps
.psmargcur
;
545 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
546 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
553 ps_putchar(struct termp
*p
, char c
)
557 /* See ps_printf(). */
559 if ( ! (PS_MARGINS
& p
->engine
.ps
.flags
)) {
566 pos
= (int)p
->engine
.ps
.psmargcur
++;
567 p
->engine
.ps
.psmarg
[pos
++] = c
;
568 p
->engine
.ps
.psmarg
[pos
] = '\0';
574 ps_end(struct termp
*p
)
578 * At the end of the file, do one last showpage. This is the
579 * same behaviour as groff(1) and works for multiple pages as
583 assert(0 == p
->engine
.ps
.flags
);
584 assert('\0' == p
->engine
.ps
.last
);
585 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
586 printf("%s", p
->engine
.ps
.psmarg
);
587 p
->engine
.ps
.pages
++;
588 printf("showpage\n");
590 printf("%%%%Trailer\n");
591 printf("%%%%Pages: %zu\n", p
->engine
.ps
.pages
);
597 ps_begin(struct termp
*p
)
603 * Print margins into margin buffer. Nothing gets output to the
604 * screen yet, so we don't need to initialise the primary state.
607 if (p
->engine
.ps
.psmarg
) {
608 assert(p
->engine
.ps
.psmargsz
);
609 p
->engine
.ps
.psmarg
[0] = '\0';
612 p
->engine
.ps
.psmargcur
= 0;
613 p
->engine
.ps
.flags
= PS_MARGINS
;
614 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
615 p
->engine
.ps
.psrow
= p
->engine
.ps
.header
;
617 ps_setfont(p
, TERMFONT_NONE
);
619 (*p
->headf
)(p
, p
->argf
);
622 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
623 p
->engine
.ps
.psrow
= p
->engine
.ps
.footer
;
625 (*p
->footf
)(p
, p
->argf
);
628 p
->engine
.ps
.flags
&= ~PS_MARGINS
;
630 assert(0 == p
->engine
.ps
.flags
);
631 assert(p
->engine
.ps
.psmarg
);
632 assert('\0' != p
->engine
.ps
.psmarg
[0]);
635 * Print header and initialise page state. Following this,
636 * stuff gets printed to the screen, so make sure we're sane.
641 printf("%%!PS-Adobe-3.0\n");
642 printf("%%%%Creator: mandoc-%s\n", VERSION
);
643 printf("%%%%CreationDate: %s", ctime(&t
));
644 printf("%%%%DocumentData: Clean7Bit\n");
645 printf("%%%%Orientation: Portrait\n");
646 printf("%%%%Pages: (atend)\n");
647 printf("%%%%PageOrder: Ascend\n");
648 printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
649 AFM2PNT(p
, p
->engine
.ps
.width
),
650 AFM2PNT(p
, p
->engine
.ps
.height
));
651 printf("%%%%DocumentNeededResources: font");
652 for (i
= 0; i
< (int)TERMFONT__MAX
; i
++)
653 printf(" %s", fonts
[i
].name
);
654 printf("\n%%%%EndComments\n");
656 printf("%%%%Page: %zu %zu\n",
657 p
->engine
.ps
.pages
+ 1,
658 p
->engine
.ps
.pages
+ 1);
660 ps_setfont(p
, TERMFONT_NONE
);
661 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
662 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
663 p
->engine
.ps
.flags
|= PS_NEWPAGE
;
668 ps_pletter(struct termp
*p
, int c
)
673 * If we're not in a PostScript "word" context, then open one
674 * now at the current cursor.
677 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
)) {
678 ps_printf(p
, "%zu %zu moveto\n(",
679 AFM2PNT(p
, p
->engine
.ps
.pscol
),
680 AFM2PNT(p
, p
->engine
.ps
.psrow
));
681 p
->engine
.ps
.flags
|= PS_INLINE
;
682 p
->engine
.ps
.flags
&= ~PS_NEWPAGE
;
685 assert( ! (PS_NEWPAGE
& p
->engine
.ps
.flags
));
688 * We need to escape these characters as per the PostScript
689 * specification. We would also escape non-graphable characters
690 * (like tabs), but none of them would get to this point and
691 * it's superfluous to abort() on them.
706 /* Write the character and adjust where we are on the page. */
708 f
= (int)p
->engine
.ps
.lastf
;
710 if (c
<= 32 || (c
- 32 > MAXCHAR
)) {
712 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[0].wx
;
716 ps_putchar(p
, (char)c
);
718 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[c
].wx
;
723 ps_pclose(struct termp
*p
)
727 * Spit out that we're exiting a word context (this is a
728 * "partial close" because we don't check the last-char buffer
732 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
))
735 ps_printf(p
, ") show\n");
736 p
->engine
.ps
.flags
&= ~PS_INLINE
;
741 ps_fclose(struct termp
*p
)
745 * Strong closure: if we have a last-char, spit it out after
746 * checking that we're in the right font mode. This will of
747 * course open a new scope, if applicable.
749 * Following this, close out any scope that's open.
752 if ('\0' != p
->engine
.ps
.last
) {
753 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
755 ps_setfont(p
, TERMFONT_NONE
);
757 ps_pletter(p
, p
->engine
.ps
.last
);
758 p
->engine
.ps
.last
= '\0';
761 if ( ! (PS_INLINE
& p
->engine
.ps
.flags
))
769 ps_letter(struct termp
*p
, char c
)
774 * State machine dictates whether to buffer the last character
775 * or not. Basically, encoded words are detected by checking if
776 * we're an "8" and switching on the buffer. Then we put "8" in
777 * our buffer, and on the next charater, flush both character
778 * and buffer. Thus, "regular" words are detected by having a
779 * regular character and a regular buffer character.
782 if ('\0' == p
->engine
.ps
.last
) {
784 p
->engine
.ps
.last
= c
;
786 } else if (8 == p
->engine
.ps
.last
) {
788 p
->engine
.ps
.last
= '\0';
790 assert(8 != p
->engine
.ps
.last
);
791 if ('_' == p
->engine
.ps
.last
) {
792 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
794 ps_setfont(p
, TERMFONT_UNDER
);
796 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
798 ps_setfont(p
, TERMFONT_BOLD
);
800 p
->engine
.ps
.last
= c
;
803 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
805 ps_setfont(p
, TERMFONT_NONE
);
807 cc
= p
->engine
.ps
.last
;
808 p
->engine
.ps
.last
= c
;
817 ps_advance(struct termp
*p
, size_t len
)
821 * Advance some spaces. This can probably be made smarter,
822 * i.e., to have multiple space-separated words in the same
823 * scope, but this is easier: just close out the current scope
824 * and readjust our column settings.
828 p
->engine
.ps
.pscol
+= len
;
833 ps_endline(struct termp
*p
)
836 /* Close out any scopes we have open: we're at eoln. */
841 * If we're in the margin, don't try to recalculate our current
842 * row. XXX: if the column tries to be fancy with multiple
843 * lines, we'll do nasty stuff.
846 if (PS_MARGINS
& p
->engine
.ps
.flags
)
851 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
853 /* If we haven't printed anything, return. */
855 if (PS_NEWPAGE
& p
->engine
.ps
.flags
)
859 * Put us down a line. If we're at the page bottom, spit out a
860 * showpage and restart our row.
863 if (p
->engine
.ps
.psrow
>= p
->engine
.ps
.lineheight
+
864 p
->engine
.ps
.bottom
) {
865 p
->engine
.ps
.psrow
-= p
->engine
.ps
.lineheight
;
869 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
870 printf("%s", p
->engine
.ps
.psmarg
);
871 printf("showpage\n");
872 p
->engine
.ps
.pages
++;
873 printf("%%%%Page: %zu %zu\n",
874 p
->engine
.ps
.pages
+ 1,
875 p
->engine
.ps
.pages
+ 1);
876 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
877 assert( ! (PS_NEWPAGE
& p
->engine
.ps
.flags
));
878 p
->engine
.ps
.flags
|= PS_NEWPAGE
;
883 ps_setfont(struct termp
*p
, enum termfont f
)
886 assert(f
< TERMFONT__MAX
);
887 ps_printf(p
, "/%s %zu selectfont\n",
888 fonts
[(int)f
].name
, p
->engine
.ps
.scale
);
889 p
->engine
.ps
.lastf
= f
;
895 ps_width(const struct termp
*p
, char c
)
898 if (c
<= 32 || c
- 32 >= MAXCHAR
)
899 return(fonts
[(int)TERMFONT_NONE
].gly
[0].wx
);
902 return(fonts
[(int)TERMFONT_NONE
].gly
[(int)c
].wx
);
907 ps_hspan(const struct termp
*p
, const struct roffsu
*su
)
912 * All of these measurements are derived by converting from the
913 * native measurement to AFM units.
918 r
= PNT2AFM(p
, su
->scale
* 28.34);
921 r
= PNT2AFM(p
, su
->scale
* 72);
924 r
= PNT2AFM(p
, su
->scale
* 12);
927 r
= PNT2AFM(p
, su
->scale
* 100);
931 fonts
[(int)TERMFONT_NONE
].gly
[109 - 32].wx
;
934 r
= PNT2AFM(p
, su
->scale
* 2.834);
938 fonts
[(int)TERMFONT_NONE
].gly
[110 - 32].wx
;
941 r
= su
->scale
* p
->engine
.ps
.lineheight
;