]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.141 2010/06/07 10:52:44 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>
39 static struct termp
*term_alloc(char *, enum termenc
);
40 static void term_free(struct termp
*);
41 static void spec(struct termp
*, const char *, size_t);
42 static void res(struct termp
*, const char *, size_t);
43 static void buffera(struct termp
*, const char *, size_t);
44 static void bufferc(struct termp
*, char);
45 static void adjbuf(struct termp
*p
, size_t);
46 static void encode(struct termp
*, const char *, size_t);
50 ascii_alloc(char *outopts
)
53 return(term_alloc(outopts
, TERMENC_ASCII
));
58 terminal_free(void *arg
)
61 term_free((struct termp
*)arg
);
66 term_free(struct termp
*p
)
72 chars_free(p
->symtab
);
79 term_alloc(char *outopts
, enum termenc enc
)
89 p
= calloc(1, sizeof(struct termp
));
99 while (outopts
&& *outopts
)
100 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
108 /* Enforce some lower boundary. */
111 p
->defrmargin
= width
- 2;
117 * Flush a line of text. A "line" is loosely defined as being something
118 * that should be followed by a newline, regardless of whether it's
119 * broken apart by newlines getting there. A line can also be a
120 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
121 * not have a trailing newline.
123 * The following flags may be specified:
125 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
126 * offset value. This is useful when doing columnar lists where the
127 * prior column has right-padded.
129 * - TERMP_NOBREAK: this is the most important and is used when making
130 * columns. In short: don't print a newline and instead pad to the
131 * right margin. Used in conjunction with TERMP_NOLPAD.
133 * - TERMP_TWOSPACE: when padding, make sure there are at least two
134 * space characters of padding. Otherwise, rather break the line.
136 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
137 * the line is overrun, and don't pad-right if it's underrun.
139 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
140 * overruning, instead save the position and continue at that point
141 * when the next invocation.
143 * In-line line breaking:
145 * If TERMP_NOBREAK is specified and the line overruns the right
146 * margin, it will break and pad-right to the right margin after
147 * writing. If maxrmargin is violated, it will break and continue
148 * writing from the right-margin, which will lead to the above scenario
149 * upon exit. Otherwise, the line will break at the right margin.
152 term_flushln(struct termp
*p
)
154 int i
; /* current input position in p->buf */
155 size_t vis
; /* current visual position on output */
156 size_t vbl
; /* number of blanks to prepend to output */
157 size_t vend
; /* end of word visual position on output */
158 size_t bp
; /* visual right border position */
159 int j
; /* temporary loop index */
160 int jhy
; /* last hyphen before line overflow */
164 * First, establish the maximum columns of "visible" content.
165 * This is usually the difference between the right-margin and
166 * an indentation, but can be, for tagged lists or columns, a
167 * small set of values.
170 assert(p
->offset
< p
->rmargin
);
172 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
174 0 : p
->rmargin
- p
->offset
- p
->overstep
;
175 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
177 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
179 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
182 * Indent the first line of a paragraph.
184 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
187 * FIXME: if bp is zero, we still output the first word before
192 while (i
< (int)p
->col
) {
195 * Handle literal tab characters.
197 for (j
= i
; j
< (int)p
->col
; j
++) {
198 if ('\t' != p
->buf
[j
])
200 vend
= (vis
/p
->tabwidth
+1)*p
->tabwidth
;
206 * Count up visible word characters. Control sequences
207 * (starting with the CSI) aren't counted. A space
208 * generates a non-printing word, which is valid (the
209 * space is printed according to regular spacing rules).
213 for (jhy
= 0; j
< (int)p
->col
; j
++) {
214 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
216 if (8 != p
->buf
[j
]) {
217 if (vend
> vis
&& vend
< bp
&&
218 ASCII_HYPH
== p
->buf
[j
])
226 * Find out whether we would exceed the right margin.
227 * If so, break to the next line.
229 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
232 if (TERMP_NOBREAK
& p
->flags
) {
233 p
->viscol
= p
->rmargin
;
234 for (j
= 0; j
< (int)p
->rmargin
; j
++)
236 vend
+= p
->rmargin
- p
->offset
;
242 /* Remove the p->overstep width. */
244 bp
+= (int)/* LINTED */
250 * Skip leading tabs, they were handled above.
252 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
255 /* Write out the [remaining] word. */
256 for ( ; i
< (int)p
->col
; i
++) {
257 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
259 if ('\t' == p
->buf
[i
])
261 if (' ' == p
->buf
[i
]) {
262 while (' ' == p
->buf
[i
]) {
268 if (ASCII_NBRSP
== p
->buf
[i
]) {
274 * Now we definitely know there will be
275 * printable characters to output,
276 * so write preceding white space now.
279 for (j
= 0; j
< (int)vbl
; j
++)
285 if (ASCII_HYPH
== p
->buf
[i
])
299 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
305 if (TERMP_HANG
& p
->flags
) {
306 /* We need one blank after the tag. */
307 p
->overstep
= /* LINTED */
311 * Behave exactly the same way as groff:
312 * If we have overstepped the margin, temporarily move
313 * it to the right and flag the rest of the line to be
315 * If we landed right at the margin, be happy.
316 * If we are one step before the margin, temporarily
317 * move it one step LEFT and flag the rest of the line
320 if (p
->overstep
>= -1) {
321 assert((int)maxvis
+ p
->overstep
>= 0);
323 maxvis
+= p
->overstep
;
327 } else if (TERMP_DANGLE
& p
->flags
)
331 if (maxvis
> vis
+ /* LINTED */
332 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0)) {
333 p
->viscol
+= maxvis
- vis
;
334 for ( ; vis
< maxvis
; vis
++)
336 } else { /* ...or newline break. */
338 p
->viscol
= p
->rmargin
;
339 for (i
= 0; i
< (int)p
->rmargin
; i
++)
346 * A newline only breaks an existing line; it won't assert vertical
347 * space. All data in the output buffer is flushed prior to the newline
351 term_newln(struct termp
*p
)
354 p
->flags
|= TERMP_NOSPACE
;
355 if (0 == p
->col
&& 0 == p
->viscol
) {
356 p
->flags
&= ~TERMP_NOLPAD
;
360 p
->flags
&= ~TERMP_NOLPAD
;
365 * Asserts a vertical space (a full, empty line-break between lines).
366 * Note that if used twice, this will cause two blank spaces and so on.
367 * All data in the output buffer is flushed prior to the newline
371 term_vspace(struct termp
*p
)
381 spec(struct termp
*p
, const char *word
, size_t len
)
386 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
393 res(struct termp
*p
, const char *word
, size_t len
)
398 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
405 term_fontlast(struct termp
*p
)
410 p
->fontl
= p
->fontq
[p
->fonti
];
411 p
->fontq
[p
->fonti
] = f
;
416 term_fontrepl(struct termp
*p
, enum termfont f
)
419 p
->fontl
= p
->fontq
[p
->fonti
];
420 p
->fontq
[p
->fonti
] = f
;
425 term_fontpush(struct termp
*p
, enum termfont f
)
428 assert(p
->fonti
+ 1 < 10);
429 p
->fontl
= p
->fontq
[p
->fonti
];
430 p
->fontq
[++p
->fonti
] = f
;
435 term_fontq(struct termp
*p
)
438 return(&p
->fontq
[p
->fonti
]);
443 term_fonttop(struct termp
*p
)
446 return(p
->fontq
[p
->fonti
]);
451 term_fontpopq(struct termp
*p
, const void *key
)
454 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
456 assert(p
->fonti
>= 0);
461 term_fontpop(struct termp
*p
)
470 * Handle pwords, partial words, which may be either a single word or a
471 * phrase that cannot be broken down (such as a literal string). This
472 * handles word styling.
475 term_word(struct termp
*p
, const char *word
)
477 const char *sv
, *seq
;
484 if (word
[0] && '\0' == word
[1])
501 if ( ! (TERMP_IGNDELIM
& p
->flags
))
502 p
->flags
|= TERMP_NOSPACE
;
508 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
510 if (TERMP_SENTENCE
& p
->flags
)
514 if ( ! (p
->flags
& TERMP_NONOSPACE
))
515 p
->flags
&= ~TERMP_NOSPACE
;
517 p
->flags
&= ~TERMP_SENTENCE
;
519 /* FIXME: use strcspn. */
529 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
532 case (DECO_RESERVED
):
539 term_fontrepl(p
, TERMFONT_BOLD
);
542 term_fontrepl(p
, TERMFONT_UNDER
);
545 term_fontrepl(p
, TERMFONT_NONE
);
547 case (DECO_PREVIOUS
):
555 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
556 p
->flags
|= TERMP_NOSPACE
;
560 * Note that we don't process the pipe: the parser sees it as
561 * punctuation, but we don't in terms of typography.
563 if (sv
[0] && 0 == sv
[1])
568 p
->flags
|= TERMP_NOSPACE
;
577 adjbuf(struct termp
*p
, size_t sz
)
582 while (sz
>= p
->maxcols
)
585 p
->buf
= realloc(p
->buf
, p
->maxcols
);
586 if (NULL
== p
->buf
) {
594 buffera(struct termp
*p
, const char *word
, size_t sz
)
597 if (p
->col
+ sz
>= p
->maxcols
)
598 adjbuf(p
, p
->col
+ sz
);
600 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
606 bufferc(struct termp
*p
, char c
)
609 if (p
->col
+ 1 >= p
->maxcols
)
610 adjbuf(p
, p
->col
+ 1);
612 p
->buf
[(int)p
->col
++] = c
;
617 encode(struct termp
*p
, const char *word
, size_t sz
)
623 * Encode and buffer a string of characters. If the current
624 * font mode is unset, buffer directly, else encode then buffer
625 * character by character.
628 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
629 buffera(p
, word
, sz
);
633 for (i
= 0; i
< (int)sz
; i
++) {
634 if ( ! isgraph((u_char
)word
[i
])) {
639 if (TERMFONT_UNDER
== f
)
651 term_vspan(const struct roffsu
*su
)
669 r
= su
->scale
/ 1000;
681 return(/* LINTED */(size_t)
687 term_hspan(const struct roffsu
*su
)
691 /* XXX: CM, IN, and PT are approximations. */
698 /* XXX: this is an approximation. */
702 r
= (10 * su
->scale
) / 6;
705 r
= (10 * su
->scale
) / 72;
708 r
= su
->scale
/ 1000; /* FIXME: double-check. */
711 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
720 return((size_t)/* LINTED */