]>
git.cameronkatri.com Git - mandoc.git/blob - term_ps.c
1 /* $Id: term_ps.c,v 1.23 2010/06/30 15:05:02 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>
35 /* Convert PostScript point "x" to an AFM unit. */
36 #define PNT2AFM(p, x) \
37 (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
39 /* Convert an AFM unit "x" to a PostScript points */
40 #define AFM2PNT(p, x) \
41 (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
44 int wx
; /* WX in AFM */
48 const char *name
; /* FontName in AFM */
49 #define MAXCHAR 95 /* total characters we can handle */
50 struct glyph gly
[MAXCHAR
]; /* glyph metrics */
54 * We define, for the time being, three fonts: bold, oblique/italic, and
55 * normal (roman). The following table hard-codes the font metrics for
56 * ASCII, i.e., 32--127.
59 static const struct font fonts
[TERMFONT__MAX
] = {
60 { "CharterBT-Roman", {
157 { "CharterBT-Bold", {
254 { "CharterBT-Italic", {
353 /* These work the buffer used by the header and footer. */
354 #define PS_BUFSLOP 128
355 #define PS_GROWBUF(p, sz) \
356 do if ((p)->engine.ps.psmargcur + (sz) > \
357 (p)->engine.ps.psmargsz) { \
358 (p)->engine.ps.psmargsz += /* CONSTCOND */ \
359 MAX(PS_BUFSLOP, (sz)); \
360 (p)->engine.ps.psmarg = realloc \
361 ((p)->engine.ps.psmarg, \
362 (p)->engine.ps.psmargsz); \
363 if (NULL == (p)->engine.ps.psmarg) { \
365 exit(EXIT_FAILURE); \
367 } while (/* CONSTCOND */ 0)
370 static double ps_hspan(const struct termp
*,
371 const struct roffsu
*);
372 static size_t ps_width(const struct termp
*, char);
373 static void ps_advance(struct termp
*, size_t);
374 static void ps_begin(struct termp
*);
375 static void ps_end(struct termp
*);
376 static void ps_endline(struct termp
*);
377 static void ps_fclose(struct termp
*);
378 static void ps_letter(struct termp
*, char);
379 static void ps_pclose(struct termp
*);
380 static void ps_pletter(struct termp
*, int);
381 static void ps_printf(struct termp
*, const char *, ...);
382 static void ps_putchar(struct termp
*, char);
383 static void ps_setfont(struct termp
*, enum termfont
);
387 ps_alloc(char *outopts
)
390 size_t pagex
, pagey
, margin
, lineheight
;
395 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
398 p
->advance
= ps_advance
;
401 p
->endline
= ps_endline
;
403 p
->letter
= ps_letter
;
404 p
->type
= TERMTYPE_PS
;
407 p
->engine
.ps
.scale
= 10;
414 while (outopts
&& *outopts
)
415 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
423 margin
= PNT2AFM(p
, 72);
424 lineheight
= PNT2AFM(p
, 12);
426 if (0 == strcasecmp(paper
, "a4")) {
427 pagex
= PNT2AFM(p
, 595);
428 pagey
= PNT2AFM(p
, 842);
430 pagex
= PNT2AFM(p
, 612);
431 pagey
= PNT2AFM(p
, 792);
434 assert(margin
* 2 < pagex
);
435 assert(margin
* 2 < pagey
);
437 p
->engine
.ps
.width
= pagex
;
438 p
->engine
.ps
.height
= pagey
;
439 p
->engine
.ps
.header
= pagey
- (margin
/ 2);
440 p
->engine
.ps
.top
= pagey
- margin
;
441 p
->engine
.ps
.footer
= (margin
/ 2);
442 p
->engine
.ps
.bottom
= margin
;
443 p
->engine
.ps
.left
= margin
;
444 p
->engine
.ps
.lineheight
= lineheight
;
446 p
->defrmargin
= pagex
- (margin
* 2);
456 p
= (struct termp
*)arg
;
458 if (p
->engine
.ps
.psmarg
)
459 free(p
->engine
.ps
.psmarg
);
466 ps_printf(struct termp
*p
, const char *fmt
, ...)
474 * If we're running in regular mode, then pipe directly into
475 * vprintf(). If we're processing margins, then push the data
476 * into our growable margin buffer.
479 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
486 * XXX: I assume that the in-margin print won't exceed
487 * PS_BUFSLOP (128 bytes), which is reasonable but still an
488 * assumption that will cause pukeage if it's not the case.
491 PS_GROWBUF(p
, PS_BUFSLOP
);
493 pos
= (int)p
->engine
.ps
.psmargcur
;
494 vsnprintf(&p
->engine
.ps
.psmarg
[pos
], PS_BUFSLOP
, fmt
, ap
);
495 p
->engine
.ps
.psmargcur
= strlen(p
->engine
.ps
.psmarg
);
502 ps_putchar(struct termp
*p
, char c
)
506 /* See ps_printf(). */
508 if ( ! (PS_MARGINS
& p
->engine
.ps
.psstate
)) {
515 pos
= (int)p
->engine
.ps
.psmargcur
++;
516 p
->engine
.ps
.psmarg
[pos
++] = c
;
517 p
->engine
.ps
.psmarg
[pos
] = '\0';
523 ps_end(struct termp
*p
)
527 * At the end of the file, do one last showpage. This is the
528 * same behaviour as groff(1) and works for multiple pages as
532 assert(0 == p
->engine
.ps
.psstate
);
533 assert('\0' == p
->engine
.ps
.last
);
534 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
535 printf("%s", p
->engine
.ps
.psmarg
);
536 p
->engine
.ps
.pages
++;
537 printf("showpage\n");
539 printf("%%%%Trailer\n");
540 printf("%%%%Pages: %zu\n", p
->engine
.ps
.pages
);
546 ps_begin(struct termp
*p
)
552 * Print margins into margin buffer. Nothing gets output to the
553 * screen yet, so we don't need to initialise the primary state.
556 if (p
->engine
.ps
.psmarg
) {
557 assert(p
->engine
.ps
.psmargsz
);
558 p
->engine
.ps
.psmarg
[0] = '\0';
561 p
->engine
.ps
.psmargcur
= 0;
562 p
->engine
.ps
.psstate
= PS_MARGINS
;
563 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
564 p
->engine
.ps
.psrow
= p
->engine
.ps
.header
;
566 ps_setfont(p
, TERMFONT_NONE
);
568 (*p
->headf
)(p
, p
->argf
);
571 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
572 p
->engine
.ps
.psrow
= p
->engine
.ps
.footer
;
574 (*p
->footf
)(p
, p
->argf
);
577 p
->engine
.ps
.psstate
&= ~PS_MARGINS
;
579 assert(0 == p
->engine
.ps
.psstate
);
580 assert(p
->engine
.ps
.psmarg
);
581 assert('\0' != p
->engine
.ps
.psmarg
[0]);
584 * Print header and initialise page state. Following this,
585 * stuff gets printed to the screen, so make sure we're sane.
590 printf("%%!PS-Adobe-3.0\n");
591 printf("%%%%Creator: mandoc-%s\n", VERSION
);
592 printf("%%%%CreationDate: %s", ctime(&t
));
593 printf("%%%%DocumentData: Clean7Bit\n");
594 printf("%%%%Orientation: Portrait\n");
595 printf("%%%%Pages: (atend)\n");
596 printf("%%%%PageOrder: Ascend\n");
597 printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
598 AFM2PNT(p
, p
->engine
.ps
.width
),
599 AFM2PNT(p
, p
->engine
.ps
.height
));
600 printf("%%%%DocumentNeededResources: font");
601 for (i
= 0; i
< (int)TERMFONT__MAX
; i
++)
602 printf(" %s", fonts
[i
].name
);
603 printf("\n%%%%EndComments\n");
605 printf("%%%%Page: %zu %zu\n",
606 p
->engine
.ps
.pages
+ 1,
607 p
->engine
.ps
.pages
+ 1);
609 ps_setfont(p
, TERMFONT_NONE
);
610 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
611 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
616 ps_pletter(struct termp
*p
, int c
)
621 * If we're not in a PostScript "word" context, then open one
622 * now at the current cursor.
625 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
)) {
626 ps_printf(p
, "%zu %zu moveto\n(",
627 AFM2PNT(p
, p
->engine
.ps
.pscol
),
628 AFM2PNT(p
, p
->engine
.ps
.psrow
));
629 p
->engine
.ps
.psstate
|= PS_INLINE
;
633 * We need to escape these characters as per the PostScript
634 * specification. We would also escape non-graphable characters
635 * (like tabs), but none of them would get to this point and
636 * it's superfluous to abort() on them.
651 /* Write the character and adjust where we are on the page. */
653 f
= (int)p
->engine
.ps
.lastf
;
655 if (c
<= 32 || (c
- 32 > MAXCHAR
)) {
657 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[0].wx
;
663 p
->engine
.ps
.pscol
+= fonts
[f
].gly
[c
].wx
;
668 ps_pclose(struct termp
*p
)
672 * Spit out that we're exiting a word context (this is a
673 * "partial close" because we don't check the last-char buffer
677 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
680 ps_printf(p
, ") show\n");
681 p
->engine
.ps
.psstate
&= ~PS_INLINE
;
686 ps_fclose(struct termp
*p
)
690 * Strong closure: if we have a last-char, spit it out after
691 * checking that we're in the right font mode. This will of
692 * course open a new scope, if applicable.
694 * Following this, close out any scope that's open.
697 if ('\0' != p
->engine
.ps
.last
) {
698 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
700 ps_setfont(p
, TERMFONT_NONE
);
702 ps_pletter(p
, p
->engine
.ps
.last
);
703 p
->engine
.ps
.last
= '\0';
706 if ( ! (PS_INLINE
& p
->engine
.ps
.psstate
))
714 ps_letter(struct termp
*p
, char c
)
719 * State machine dictates whether to buffer the last character
720 * or not. Basically, encoded words are detected by checking if
721 * we're an "8" and switching on the buffer. Then we put "8" in
722 * our buffer, and on the next charater, flush both character
723 * and buffer. Thus, "regular" words are detected by having a
724 * regular character and a regular buffer character.
727 if ('\0' == p
->engine
.ps
.last
) {
729 p
->engine
.ps
.last
= c
;
731 } else if (8 == p
->engine
.ps
.last
) {
733 p
->engine
.ps
.last
= '\0';
735 assert(8 != p
->engine
.ps
.last
);
736 if ('_' == p
->engine
.ps
.last
) {
737 if (p
->engine
.ps
.lastf
!= TERMFONT_UNDER
) {
739 ps_setfont(p
, TERMFONT_UNDER
);
741 } else if (p
->engine
.ps
.lastf
!= TERMFONT_BOLD
) {
743 ps_setfont(p
, TERMFONT_BOLD
);
745 p
->engine
.ps
.last
= c
;
748 if (p
->engine
.ps
.lastf
!= TERMFONT_NONE
) {
750 ps_setfont(p
, TERMFONT_NONE
);
752 cc
= p
->engine
.ps
.last
;
753 p
->engine
.ps
.last
= c
;
762 ps_advance(struct termp
*p
, size_t len
)
766 * Advance some spaces. This can probably be made smarter,
767 * i.e., to have multiple space-separated words in the same
768 * scope, but this is easier: just close out the current scope
769 * and readjust our column settings.
773 p
->engine
.ps
.pscol
+= len
;
778 ps_endline(struct termp
*p
)
781 /* Close out any scopes we have open: we're at eoln. */
786 * If we're in the margin, don't try to recalculate our current
787 * row. XXX: if the column tries to be fancy with multiple
788 * lines, we'll do nasty stuff.
791 if (PS_MARGINS
& p
->engine
.ps
.psstate
)
795 * Put us down a line. If we're at the page bottom, spit out a
796 * showpage and restart our row.
799 p
->engine
.ps
.pscol
= p
->engine
.ps
.left
;
800 if (p
->engine
.ps
.psrow
>= p
->engine
.ps
.lineheight
+
801 p
->engine
.ps
.bottom
) {
802 p
->engine
.ps
.psrow
-= p
->engine
.ps
.lineheight
;
806 assert(p
->engine
.ps
.psmarg
&& p
->engine
.ps
.psmarg
[0]);
807 printf("%s", p
->engine
.ps
.psmarg
);
808 printf("showpage\n");
809 p
->engine
.ps
.pages
++;
810 printf("%%%%Page: %zu %zu\n",
811 p
->engine
.ps
.pages
+ 1,
812 p
->engine
.ps
.pages
+ 1);
813 p
->engine
.ps
.psrow
= p
->engine
.ps
.top
;
818 ps_setfont(struct termp
*p
, enum termfont f
)
821 assert(f
< TERMFONT__MAX
);
822 ps_printf(p
, "/%s %zu selectfont\n",
823 fonts
[(int)f
].name
, p
->engine
.ps
.scale
);
824 p
->engine
.ps
.lastf
= f
;
830 ps_width(const struct termp
*p
, char c
)
833 if (c
<= 32 || c
- 32 >= MAXCHAR
)
834 return(fonts
[(int)TERMFONT_NONE
].gly
[0].wx
);
837 return(fonts
[(int)TERMFONT_NONE
].gly
[(int)c
].wx
);
842 ps_hspan(const struct termp
*p
, const struct roffsu
*su
)
847 * All of these measurements are derived by converting from the
848 * native measurement to AFM units.
853 r
= PNT2AFM(p
, su
->scale
* 28.34);
856 r
= PNT2AFM(p
, su
->scale
* 72);
859 r
= PNT2AFM(p
, su
->scale
* 12);
862 r
= PNT2AFM(p
, su
->scale
* 100);
866 fonts
[(int)TERMFONT_NONE
].gly
[109 - 32].wx
;
869 r
= PNT2AFM(p
, su
->scale
* 2.834);
873 fonts
[(int)TERMFONT_NONE
].gly
[110 - 32].wx
;
876 r
= su
->scale
* p
->engine
.ps
.lineheight
;