]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.122 2009/11/05 08:40:16 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.
31 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
32 /* FIXME: accomodate non-breaking, collapsing white-space. */
34 static struct termp
*term_alloc(enum termenc
);
35 static void term_free(struct termp
*);
37 static void do_escaped(struct termp
*, const char **);
38 static void do_special(struct termp
*,
39 const char *, size_t);
40 static void do_reserved(struct termp
*,
41 const char *, size_t);
42 static void buffer(struct termp
*, char);
43 static void encode(struct termp
*, char);
50 return(term_alloc(TERMENC_ASCII
));
55 terminal_free(void *arg
)
58 term_free((struct termp
*)arg
);
63 term_free(struct termp
*p
)
69 chars_free(p
->symtab
);
76 term_alloc(enum termenc enc
)
80 p
= calloc(1, sizeof(struct termp
));
92 * Flush a line of text. A "line" is loosely defined as being something
93 * that should be followed by a newline, regardless of whether it's
94 * broken apart by newlines getting there. A line can also be a
95 * fragment of a columnar list.
97 * Specifically, a line is whatever's in p->buf of length p->col, which
98 * is zeroed after this function returns.
100 * The usage of termp:flags is as follows:
102 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
103 * offset value. This is useful when doing columnar lists where the
104 * prior column has right-padded.
106 * - TERMP_NOBREAK: this is the most important and is used when making
107 * columns. In short: don't print a newline and instead pad to the
108 * right margin. Used in conjunction with TERMP_NOLPAD.
110 * - TERMP_TWOSPACE: when padding, make sure there are at least two
111 * space characters of padding. Otherwise, rather break the line.
113 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
114 * the line is overrun, and don't pad-right if it's underrun.
116 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
117 * overruning, instead save the position and continue at that point
118 * when the next invocation.
120 * In-line line breaking:
122 * If TERMP_NOBREAK is specified and the line overruns the right
123 * margin, it will break and pad-right to the right margin after
124 * writing. If maxrmargin is violated, it will break and continue
125 * writing from the right-margin, which will lead to the above scenario
126 * upon exit. Otherwise, the line will break at the right margin.
129 term_flushln(struct termp
*p
)
131 int i
; /* current input position in p->buf */
132 size_t vis
; /* current visual position on output */
133 size_t vbl
; /* number of blanks to prepend to output */
134 size_t vsz
; /* visual characters to write to output */
135 size_t bp
; /* visual right border position */
136 int j
; /* temporary loop index */
138 static int overstep
= 0;
141 * First, establish the maximum columns of "visible" content.
142 * This is usually the difference between the right-margin and
143 * an indentation, but can be, for tagged lists or columns, a
144 * small set of values.
147 assert(p
->offset
< p
->rmargin
);
149 maxvis
= (int)(p
->rmargin
- p
->offset
) - overstep
< 0 ?
151 0 : p
->rmargin
- p
->offset
- overstep
;
152 mmax
= (int)(p
->maxrmargin
- p
->offset
) - overstep
< 0 ?
154 0 : p
->maxrmargin
- p
->offset
- overstep
;
156 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
159 * FIXME: if bp is zero, we still output the first word before
166 * If in the standard case (left-justified), then begin with our
167 * indentation, otherwise (columns, etc.) just start spitting
171 if ( ! (p
->flags
& TERMP_NOLPAD
))
173 for (j
= 0; j
< (int)p
->offset
; j
++)
176 for (i
= 0; i
< (int)p
->col
; i
++) {
178 * Count up visible word characters. Control sequences
179 * (starting with the CSI) aren't counted. A space
180 * generates a non-printing word, which is valid (the
181 * space is printed according to regular spacing rules).
185 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
186 if (j
&& ' ' == p
->buf
[j
])
188 else if (8 == p
->buf
[j
])
195 * Choose the number of blanks to prepend: no blank at the
196 * beginning of a line, one between words -- but do not
197 * actually write them yet.
199 vbl
= (size_t)(0 == vis
? 0 : 1);
202 * Find out whether we would exceed the right margin.
203 * If so, break to the next line. (TODO: hyphenate)
204 * Otherwise, write the chosen number of blanks now.
206 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
208 if (TERMP_NOBREAK
& p
->flags
) {
209 for (j
= 0; j
< (int)p
->rmargin
; j
++)
211 vis
= p
->rmargin
- p
->offset
;
213 for (j
= 0; j
< (int)p
->offset
; j
++)
217 /* Remove the overstep width. */
218 bp
+= (int)/* LINTED */
222 for (j
= 0; j
< (int)vbl
; j
++)
228 * Finally, write out the word.
230 for ( ; i
< (int)p
->col
; i
++) {
231 if (' ' == p
->buf
[i
])
234 /* The unit sep. is a non-breaking space. */
246 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
251 if (TERMP_HANG
& p
->flags
) {
252 /* We need one blank after the tag. */
253 overstep
= /* LINTED */
257 * Behave exactly the same way as groff:
258 * If we have overstepped the margin, temporarily move
259 * it to the right and flag the rest of the line to be
261 * If we landed right at the margin, be happy.
262 * If we are one step before the margin, temporarily
263 * move it one step LEFT and flag the rest of the line
266 if (overstep
>= -1) {
267 assert((int)maxvis
+ overstep
>= 0);
273 } else if (TERMP_DANGLE
& p
->flags
)
277 if (maxvis
> vis
+ /* LINTED */
278 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
279 for ( ; vis
< maxvis
; vis
++)
281 else { /* ...or newline break. */
283 for (i
= 0; i
< (int)p
->rmargin
; i
++)
290 * A newline only breaks an existing line; it won't assert vertical
291 * space. All data in the output buffer is flushed prior to the newline
295 term_newln(struct termp
*p
)
298 p
->flags
|= TERMP_NOSPACE
;
300 p
->flags
&= ~TERMP_NOLPAD
;
304 p
->flags
&= ~TERMP_NOLPAD
;
309 * Asserts a vertical space (a full, empty line-break between lines).
310 * Note that if used twice, this will cause two blank spaces and so on.
311 * All data in the output buffer is flushed prior to the newline
315 term_vspace(struct termp
*p
)
324 do_special(struct termp
*p
, const char *word
, size_t len
)
330 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
334 fputs("Unknown special character: ", stderr
);
335 for (i
= 0; i
< (int)len
; i
++)
336 fputc(word
[i
], stderr
);
341 for (i
= 0; i
< (int)sz
; i
++)
347 do_reserved(struct termp
*p
, const char *word
, size_t len
)
353 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
357 fputs("Unknown reserved word: ", stderr
);
358 for (i
= 0; i
< (int)len
; i
++)
359 fputc(word
[i
], stderr
);
364 for (i
= 0; i
< (int)sz
; i
++)
370 * Handle an escape sequence: determine its length and pass it to the
371 * escape-symbol look table. Note that we assume mdoc(3) has validated
372 * the escape sequence (we assert upon badly-formed escape sequences).
375 do_escaped(struct termp
*p
, const char **word
)
390 if (0 == *wp
|| 0 == *(wp
+ 1)) {
391 *word
= 0 == *wp
? wp
: wp
+ 1;
395 do_special(p
, wp
, 2);
399 } else if ('*' == *wp
) {
408 if (0 == *wp
|| 0 == *(wp
+ 1)) {
409 *word
= 0 == *wp
? wp
: wp
+ 1;
413 do_reserved(p
, wp
, 2);
420 do_reserved(p
, wp
, 1);
425 } else if ('f' == *wp
) {
435 p
->metamask
= p
->metafont
;
436 p
->metafont
|= METAF_BOLD
;
441 p
->metamask
= p
->metafont
;
442 p
->metafont
|= METAF_UNDER
;
446 p
->metamask
= p
->metafont
;
452 p
->metamask
= p
->metafont
;
453 p
->metafont
&= ~METAF_UNDER
;
454 p
->metafont
&= ~METAF_BOLD
;
463 } else if ('[' != *wp
) {
464 do_special(p
, wp
, 1);
470 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
479 do_special(p
, wp
- j
, (size_t)j
);
481 do_reserved(p
, wp
- j
, (size_t)j
);
487 * Handle pwords, partial words, which may be either a single word or a
488 * phrase that cannot be broken down (such as a literal string). This
489 * handles word styling.
492 term_word(struct termp
*p
, const char *word
)
498 if (word
[0] && 0 == word
[1])
517 if ( ! (TERMP_IGNDELIM
& p
->flags
))
518 p
->flags
|= TERMP_NOSPACE
;
524 if ( ! (TERMP_NOSPACE
& p
->flags
))
527 if ( ! (p
->flags
& TERMP_NONOSPACE
))
528 p
->flags
&= ~TERMP_NOSPACE
;
530 for ( ; *word
; word
++)
534 do_escaped(p
, &word
);
536 if (sv
[0] && 0 == sv
[1])
543 p
->flags
|= TERMP_NOSPACE
;
552 * Insert a single character into the line-buffer. If the buffer's
553 * space is exceeded, then allocate more space by doubling the buffer
557 buffer(struct termp
*p
, char c
)
561 if (p
->col
+ 1 >= p
->maxcols
) {
565 p
->buf
= realloc(p
->buf
, s
);
566 if (NULL
== p
->buf
) {
572 p
->buf
[(int)(p
->col
)++] = c
;
577 encode(struct termp
*p
, char c
)
580 if (isgraph((u_char
)c
)) {
581 if (p
->under
|| METAF_UNDER
& p
->metafont
) {
585 if (p
->bold
|| METAF_BOLD
& p
->metafont
) {
595 term_vspan(const struct roffsu
*su
)
613 r
= su
->scale
/ 1000;
625 return(/* LINTED */(size_t)
631 term_hspan(const struct roffsu
*su
)
635 /* XXX: CM, IN, and PT are approximations. */
642 /* XXX: this is an approximation. */
646 r
= (10 * su
->scale
) / 6;
649 r
= (10 * su
->scale
) / 72;
652 r
= su
->scale
/ 1000; /* FIXME: double-check. */
655 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
664 return((size_t)/* LINTED */