]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.145 2010/06/08 13:22:37 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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/types.h>
40 #define PS_CHAR_WIDTH 6
41 #define PS_CHAR_HEIGHT 12
42 #define PS_CHAR_TOPMARG (792 - 24)
43 #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
44 #define PS_CHAR_LEFT 36
45 #define PS_CHAR_BOTMARG 24
46 #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
48 static void spec(struct termp
*, const char *, size_t);
49 static void res(struct termp
*, const char *, size_t);
50 static void buffera(struct termp
*, const char *, size_t);
51 static void bufferc(struct termp
*, char);
52 static void adjbuf(struct termp
*p
, size_t);
53 static void encode(struct termp
*, const char *, size_t);
54 static void advance(struct termp
*, size_t);
55 static void endline(struct termp
*);
56 static void letter(struct termp
*, char);
57 static void pageopen(struct termp
*);
61 ascii_alloc(char *outopts
)
67 if (NULL
== (p
= term_alloc(TERMENC_ASCII
)))
70 p
->type
= TERMTYPE_CHAR
;
75 while (outopts
&& *outopts
)
76 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
78 p
->defrmargin
= (size_t)atoi(v
);
84 /* Enforce a lower boundary. */
85 if (p
->defrmargin
< 58)
96 term_free((struct termp
*)arg
);
101 term_free(struct termp
*p
)
107 chars_free(p
->symtab
);
114 * Push a single letter into our output engine.
117 letter(struct termp
*p
, char c
)
120 if (TERMTYPE_CHAR
== p
->type
) {
122 * If using the terminal device, just push the letter
123 * out into the screen.
129 if ( ! (PS_INLINE
& p
->psstate
)) {
131 * If we're not in a PostScript "word" context, then
132 * open one now at the current cursor.
134 printf("%zu %zu moveto\n", p
->pscol
, p
->psrow
);
136 p
->psstate
|= PS_INLINE
;
140 * We need to escape these characters as per the PostScript
141 * specification. We would also escape non-graphable characters
142 * (like tabs), but none of them would get to this point and
143 * it's superfluous to abort() on them.
158 /* Write the character and adjust where we are on the page. */
160 p
->pscol
+= PS_CHAR_WIDTH
;
165 * Begin a "terminal" context. Since terminal encompasses PostScript,
166 * the actual terminal, etc., there are a few things we can do here.
169 term_begin(struct termp
*p
, term_margin head
,
170 term_margin foot
, const void *arg
)
177 if (TERMTYPE_CHAR
== p
->type
) {
178 /* Emit the header and be done. */
179 (*p
->headf
)(p
, p
->argf
);
184 * Emit the standard PostScript prologue, set our initial page
185 * position, then run pageopen() on the initial page.
188 printf("%s\n", "%!PS");
189 printf("%s\n", "/Courier");
190 printf("%s\n", "10 selectfont");
199 * Open a page. This is only used for -Tps at the moment. It opens a
200 * page context, printing the header and the footer. THE OUTPUT BUFFER
201 * MUST BE EMPTY. If it is not, output will ghost on the next line and
202 * we'll be all gross and out of state.
205 pageopen(struct termp
*p
)
208 assert(TERMTYPE_PS
== p
->type
);
209 assert(0 == p
->psstate
);
211 p
->pscol
= PS_CHAR_LEFT
;
212 p
->psrow
= PS_CHAR_TOPMARG
;
213 p
->psstate
|= PS_MARGINS
;
215 (*p
->headf
)(p
, p
->argf
);
218 p
->psstate
&= ~PS_MARGINS
;
219 assert(0 == p
->psstate
);
221 p
->pscol
= PS_CHAR_LEFT
;
222 p
->psrow
= PS_CHAR_BOTMARG
;
223 p
->psstate
|= PS_MARGINS
;
225 (*p
->footf
)(p
, p
->argf
);
228 p
->psstate
&= ~PS_MARGINS
;
229 assert(0 == p
->psstate
);
231 p
->pscol
= PS_CHAR_LEFT
;
232 p
->psrow
= PS_CHAR_TOP
;
238 term_end(struct termp
*p
)
241 if (TERMTYPE_CHAR
== p
->type
) {
242 (*p
->footf
)(p
, p
->argf
);
246 printf("%s\n", "%%END");
251 endline(struct termp
*p
)
254 if (TERMTYPE_CHAR
== p
->type
) {
259 if (PS_INLINE
& p
->psstate
) {
261 p
->psstate
&= ~PS_INLINE
;
264 if (PS_MARGINS
& p
->psstate
)
267 p
->pscol
= PS_CHAR_LEFT
;
268 if (p
->psrow
>= PS_CHAR_HEIGHT
+ PS_CHAR_BOT
) {
269 p
->psrow
-= PS_CHAR_HEIGHT
;
274 * XXX: can't run pageopen() until we're certain a flushln() has
275 * occured, else the buf will reopen in an awkward state on the
278 printf("showpage\n");
279 p
->psrow
= PS_CHAR_TOP
;
284 * Advance the output engine by a certain amount of whitespace.
287 advance(struct termp
*p
, size_t len
)
291 if (TERMTYPE_CHAR
== p
->type
) {
292 /* Just print whitespace on the terminal. */
293 for (i
= 0; i
< len
; i
++)
298 if (PS_INLINE
& p
->psstate
) {
299 /* Dump out any existing line scope. */
301 p
->psstate
&= ~PS_INLINE
;
304 p
->pscol
+= len
? len
* PS_CHAR_WIDTH
: 0;
309 term_alloc(enum termenc enc
)
313 p
= calloc(1, sizeof(struct termp
));
327 * Flush a line of text. A "line" is loosely defined as being something
328 * that should be followed by a newline, regardless of whether it's
329 * broken apart by newlines getting there. A line can also be a
330 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
331 * not have a trailing newline.
333 * The following flags may be specified:
335 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
336 * offset value. This is useful when doing columnar lists where the
337 * prior column has right-padded.
339 * - TERMP_NOBREAK: this is the most important and is used when making
340 * columns. In short: don't print a newline and instead pad to the
341 * right margin. Used in conjunction with TERMP_NOLPAD.
343 * - TERMP_TWOSPACE: when padding, make sure there are at least two
344 * space characters of padding. Otherwise, rather break the line.
346 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
347 * the line is overrun, and don't pad-right if it's underrun.
349 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
350 * overruning, instead save the position and continue at that point
351 * when the next invocation.
353 * In-line line breaking:
355 * If TERMP_NOBREAK is specified and the line overruns the right
356 * margin, it will break and pad-right to the right margin after
357 * writing. If maxrmargin is violated, it will break and continue
358 * writing from the right-margin, which will lead to the above scenario
359 * upon exit. Otherwise, the line will break at the right margin.
362 term_flushln(struct termp
*p
)
364 int i
; /* current input position in p->buf */
365 size_t vis
; /* current visual position on output */
366 size_t vbl
; /* number of blanks to prepend to output */
367 size_t vend
; /* end of word visual position on output */
368 size_t bp
; /* visual right border position */
369 int j
; /* temporary loop index */
370 int jhy
; /* last hyphen before line overflow */
374 * First, establish the maximum columns of "visible" content.
375 * This is usually the difference between the right-margin and
376 * an indentation, but can be, for tagged lists or columns, a
377 * small set of values.
380 assert(p
->offset
< p
->rmargin
);
382 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
384 0 : p
->rmargin
- p
->offset
- p
->overstep
;
385 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
387 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
389 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
392 * Indent the first line of a paragraph.
394 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
397 * FIXME: if bp is zero, we still output the first word before
402 while (i
< (int)p
->col
) {
405 * Handle literal tab characters.
407 for (j
= i
; j
< (int)p
->col
; j
++) {
408 if ('\t' != p
->buf
[j
])
410 vend
= (vis
/p
->tabwidth
+1)*p
->tabwidth
;
416 * Count up visible word characters. Control sequences
417 * (starting with the CSI) aren't counted. A space
418 * generates a non-printing word, which is valid (the
419 * space is printed according to regular spacing rules).
423 for (jhy
= 0; j
< (int)p
->col
; j
++) {
424 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
426 if (8 != p
->buf
[j
]) {
427 if (vend
> vis
&& vend
< bp
&&
428 ASCII_HYPH
== p
->buf
[j
])
436 * Find out whether we would exceed the right margin.
437 * If so, break to the next line.
439 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
442 if (TERMP_NOBREAK
& p
->flags
) {
443 p
->viscol
= p
->rmargin
;
444 advance(p
, p
->rmargin
);
445 vend
+= p
->rmargin
- p
->offset
;
451 /* Remove the p->overstep width. */
453 bp
+= (int)/* LINTED */
459 * Skip leading tabs, they were handled above.
461 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
464 /* Write out the [remaining] word. */
465 for ( ; i
< (int)p
->col
; i
++) {
466 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
468 if ('\t' == p
->buf
[i
])
470 if (' ' == p
->buf
[i
]) {
471 while (' ' == p
->buf
[i
]) {
477 if (ASCII_NBRSP
== p
->buf
[i
]) {
483 * Now we definitely know there will be
484 * printable characters to output,
485 * so write preceding white space now.
493 if (ASCII_HYPH
== p
->buf
[i
])
496 letter(p
, p
->buf
[i
]);
507 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
513 if (TERMP_HANG
& p
->flags
) {
514 /* We need one blank after the tag. */
515 p
->overstep
= /* LINTED */
519 * Behave exactly the same way as groff:
520 * If we have overstepped the margin, temporarily move
521 * it to the right and flag the rest of the line to be
523 * If we landed right at the margin, be happy.
524 * If we are one step before the margin, temporarily
525 * move it one step LEFT and flag the rest of the line
528 if (p
->overstep
>= -1) {
529 assert((int)maxvis
+ p
->overstep
>= 0);
531 maxvis
+= p
->overstep
;
535 } else if (TERMP_DANGLE
& p
->flags
)
539 if (maxvis
> vis
+ /* LINTED */
540 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0)) {
541 p
->viscol
+= maxvis
- vis
;
542 advance(p
, maxvis
- vis
);
543 vis
+= (maxvis
- vis
);
544 } else { /* ...or newline break. */
546 p
->viscol
= p
->rmargin
;
547 advance(p
, p
->rmargin
);
553 * A newline only breaks an existing line; it won't assert vertical
554 * space. All data in the output buffer is flushed prior to the newline
558 term_newln(struct termp
*p
)
561 p
->flags
|= TERMP_NOSPACE
;
562 if (0 == p
->col
&& 0 == p
->viscol
) {
563 p
->flags
&= ~TERMP_NOLPAD
;
567 p
->flags
&= ~TERMP_NOLPAD
;
572 * Asserts a vertical space (a full, empty line-break between lines).
573 * Note that if used twice, this will cause two blank spaces and so on.
574 * All data in the output buffer is flushed prior to the newline
578 term_vspace(struct termp
*p
)
588 spec(struct termp
*p
, const char *word
, size_t len
)
593 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
600 res(struct termp
*p
, const char *word
, size_t len
)
605 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
612 term_fontlast(struct termp
*p
)
617 p
->fontl
= p
->fontq
[p
->fonti
];
618 p
->fontq
[p
->fonti
] = f
;
623 term_fontrepl(struct termp
*p
, enum termfont f
)
626 p
->fontl
= p
->fontq
[p
->fonti
];
627 p
->fontq
[p
->fonti
] = f
;
632 term_fontpush(struct termp
*p
, enum termfont f
)
635 assert(p
->fonti
+ 1 < 10);
636 p
->fontl
= p
->fontq
[p
->fonti
];
637 p
->fontq
[++p
->fonti
] = f
;
642 term_fontq(struct termp
*p
)
645 return(&p
->fontq
[p
->fonti
]);
650 term_fonttop(struct termp
*p
)
653 return(p
->fontq
[p
->fonti
]);
658 term_fontpopq(struct termp
*p
, const void *key
)
661 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
663 assert(p
->fonti
>= 0);
668 term_fontpop(struct termp
*p
)
677 * Handle pwords, partial words, which may be either a single word or a
678 * phrase that cannot be broken down (such as a literal string). This
679 * handles word styling.
682 term_word(struct termp
*p
, const char *word
)
684 const char *sv
, *seq
;
691 if (word
[0] && '\0' == word
[1])
708 if ( ! (TERMP_IGNDELIM
& p
->flags
))
709 p
->flags
|= TERMP_NOSPACE
;
715 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
717 if (TERMP_SENTENCE
& p
->flags
)
721 if ( ! (p
->flags
& TERMP_NONOSPACE
))
722 p
->flags
&= ~TERMP_NOSPACE
;
724 p
->flags
&= ~TERMP_SENTENCE
;
726 /* FIXME: use strcspn. */
736 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
739 case (DECO_RESERVED
):
746 term_fontrepl(p
, TERMFONT_BOLD
);
749 term_fontrepl(p
, TERMFONT_UNDER
);
752 term_fontrepl(p
, TERMFONT_NONE
);
754 case (DECO_PREVIOUS
):
762 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
763 p
->flags
|= TERMP_NOSPACE
;
767 * Note that we don't process the pipe: the parser sees it as
768 * punctuation, but we don't in terms of typography.
770 if (sv
[0] && 0 == sv
[1])
775 p
->flags
|= TERMP_NOSPACE
;
784 adjbuf(struct termp
*p
, size_t sz
)
789 while (sz
>= p
->maxcols
)
792 p
->buf
= realloc(p
->buf
, p
->maxcols
);
793 if (NULL
== p
->buf
) {
801 buffera(struct termp
*p
, const char *word
, size_t sz
)
804 if (p
->col
+ sz
>= p
->maxcols
)
805 adjbuf(p
, p
->col
+ sz
);
807 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
813 bufferc(struct termp
*p
, char c
)
816 if (p
->col
+ 1 >= p
->maxcols
)
817 adjbuf(p
, p
->col
+ 1);
819 p
->buf
[(int)p
->col
++] = c
;
824 encode(struct termp
*p
, const char *word
, size_t sz
)
830 * Encode and buffer a string of characters. If the current
831 * font mode is unset, buffer directly, else encode then buffer
832 * character by character.
835 if (TERMTYPE_PS
== p
->type
) {
836 buffera(p
, word
, sz
);
838 } else if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
839 buffera(p
, word
, sz
);
843 for (i
= 0; i
< (int)sz
; i
++) {
844 if ( ! isgraph((u_char
)word
[i
])) {
849 if (TERMFONT_UNDER
== f
)
861 term_vspan(const struct roffsu
*su
)
879 r
= su
->scale
/ 1000;
891 return(/* LINTED */(size_t)
897 term_hspan(const struct roffsu
*su
)
901 /* XXX: CM, IN, and PT are approximations. */
908 /* XXX: this is an approximation. */
912 r
= (10 * su
->scale
) / 6;
915 r
= (10 * su
->scale
) / 72;
918 r
= su
->scale
/ 1000; /* FIXME: double-check. */
921 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
930 return((size_t)/* LINTED */