1 /* $Id: term.c,v 1.120 2009/10/31 06:10:58 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.
30 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
31 /* FIXME: accomodate non-breaking, collapsing white-space. */
33 static struct termp *term_alloc(enum termenc);
34 static void term_free(struct termp *);
36 static void do_escaped(struct termp *, const char **);
37 static void do_special(struct termp *,
38 const char *, size_t);
39 static void do_reserved(struct termp *,
40 const char *, size_t);
41 static void buffer(struct termp *, char);
42 static void encode(struct termp *, char);
49 return(term_alloc(TERMENC_ASCII));
54 terminal_free(void *arg)
57 term_free((struct termp *)arg);
62 term_free(struct termp *p)
68 chars_free(p->symtab);
75 term_alloc(enum termenc enc)
79 p = calloc(1, sizeof(struct termp));
91 * Flush a line of text. A "line" is loosely defined as being something
92 * that should be followed by a newline, regardless of whether it's
93 * broken apart by newlines getting there. A line can also be a
94 * fragment of a columnar list.
96 * Specifically, a line is whatever's in p->buf of length p->col, which
97 * is zeroed after this function returns.
99 * The usage of termp:flags is as follows:
101 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
102 * offset value. This is useful when doing columnar lists where the
103 * prior column has right-padded.
105 * - TERMP_NOBREAK: this is the most important and is used when making
106 * columns. In short: don't print a newline and instead pad to the
107 * right margin. Used in conjunction with TERMP_NOLPAD.
109 * - TERMP_TWOSPACE: when padding, make sure there are at least two
110 * space characters of padding. Otherwise, rather break the line.
112 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
113 * the line is overrun, and don't pad-right if it's underrun.
115 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
116 * overruning, instead save the position and continue at that point
117 * when the next invocation.
119 * In-line line breaking:
121 * If TERMP_NOBREAK is specified and the line overruns the right
122 * margin, it will break and pad-right to the right margin after
123 * writing. If maxrmargin is violated, it will break and continue
124 * writing from the right-margin, which will lead to the above scenario
125 * upon exit. Otherwise, the line will break at the right margin.
128 term_flushln(struct termp *p)
130 int i; /* current input position in p->buf */
131 size_t vis; /* current visual position on output */
132 size_t vbl; /* number of blanks to prepend to output */
133 size_t vsz; /* visual characters to write to output */
134 size_t bp; /* visual right border position */
135 int j; /* temporary loop index */
137 static int overstep = 0;
140 * First, establish the maximum columns of "visible" content.
141 * This is usually the difference between the right-margin and
142 * an indentation, but can be, for tagged lists or columns, a
143 * small set of values.
146 assert(p->offset < p->rmargin);
148 maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
150 0 : p->rmargin - p->offset - overstep;
151 mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
153 0 : p->maxrmargin - p->offset - overstep;
155 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
158 * FIXME: if bp is zero, we still output the first word before
165 * If in the standard case (left-justified), then begin with our
166 * indentation, otherwise (columns, etc.) just start spitting
170 if ( ! (p->flags & TERMP_NOLPAD))
172 for (j = 0; j < (int)p->offset; j++)
175 for (i = 0; i < (int)p->col; i++) {
177 * Count up visible word characters. Control sequences
178 * (starting with the CSI) aren't counted. A space
179 * generates a non-printing word, which is valid (the
180 * space is printed according to regular spacing rules).
184 for (j = i, vsz = 0; j < (int)p->col; j++) {
185 if (j && ' ' == p->buf[j])
187 else if (8 == p->buf[j])
194 * Choose the number of blanks to prepend: no blank at the
195 * beginning of a line, one between words -- but do not
196 * actually write them yet.
198 vbl = (size_t)(0 == vis ? 0 : 1);
201 * Find out whether we would exceed the right margin.
202 * If so, break to the next line. (TODO: hyphenate)
203 * Otherwise, write the chosen number of blanks now.
205 if (vis && vis + vbl + vsz > bp) {
207 if (TERMP_NOBREAK & p->flags) {
208 for (j = 0; j < (int)p->rmargin; j++)
210 vis = p->rmargin - p->offset;
212 for (j = 0; j < (int)p->offset; j++)
216 /* Remove the overstep width. */
217 bp += (int)/* LINTED */
221 for (j = 0; j < (int)vbl; j++)
227 * Finally, write out the word.
229 for ( ; i < (int)p->col; i++) {
230 if (' ' == p->buf[i])
240 if ( ! (TERMP_NOBREAK & p->flags)) {
245 if (TERMP_HANG & p->flags) {
246 /* We need one blank after the tag. */
247 overstep = /* LINTED */
251 * Behave exactly the same way as groff:
252 * If we have overstepped the margin, temporarily move
253 * it to the right and flag the rest of the line to be
255 * If we landed right at the margin, be happy.
256 * If we are one step before the margin, temporarily
257 * move it one step LEFT and flag the rest of the line
260 if (overstep >= -1) {
261 assert((int)maxvis + overstep >= 0);
267 } else if (TERMP_DANGLE & p->flags)
271 if (maxvis > vis + /* LINTED */
272 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
273 for ( ; vis < maxvis; vis++)
275 else { /* ...or newline break. */
277 for (i = 0; i < (int)p->rmargin; i++)
284 * A newline only breaks an existing line; it won't assert vertical
285 * space. All data in the output buffer is flushed prior to the newline
289 term_newln(struct termp *p)
292 p->flags |= TERMP_NOSPACE;
294 p->flags &= ~TERMP_NOLPAD;
298 p->flags &= ~TERMP_NOLPAD;
303 * Asserts a vertical space (a full, empty line-break between lines).
304 * Note that if used twice, this will cause two blank spaces and so on.
305 * All data in the output buffer is flushed prior to the newline
309 term_vspace(struct termp *p)
318 do_special(struct termp *p, const char *word, size_t len)
324 rhs = chars_a2ascii(p->symtab, word, len, &sz);
328 fputs("Unknown special character: ", stderr);
329 for (i = 0; i < (int)len; i++)
330 fputc(word[i], stderr);
335 for (i = 0; i < (int)sz; i++)
341 do_reserved(struct termp *p, const char *word, size_t len)
347 rhs = chars_a2res(p->symtab, word, len, &sz);
351 fputs("Unknown reserved word: ", stderr);
352 for (i = 0; i < (int)len; i++)
353 fputc(word[i], stderr);
358 for (i = 0; i < (int)sz; i++)
364 * Handle an escape sequence: determine its length and pass it to the
365 * escape-symbol look table. Note that we assume mdoc(3) has validated
366 * the escape sequence (we assert upon badly-formed escape sequences).
369 do_escaped(struct termp *p, const char **word)
384 if (0 == *wp || 0 == *(wp + 1)) {
385 *word = 0 == *wp ? wp : wp + 1;
389 do_special(p, wp, 2);
393 } else if ('*' == *wp) {
402 if (0 == *wp || 0 == *(wp + 1)) {
403 *word = 0 == *wp ? wp : wp + 1;
407 do_reserved(p, wp, 2);
414 do_reserved(p, wp, 1);
419 } else if ('f' == *wp) {
435 p->bold = p->under = 0;
444 } else if ('[' != *wp) {
445 do_special(p, wp, 1);
451 for (j = 0; *wp && ']' != *wp; wp++, j++)
460 do_special(p, wp - j, (size_t)j);
462 do_reserved(p, wp - j, (size_t)j);
468 * Handle pwords, partial words, which may be either a single word or a
469 * phrase that cannot be broken down (such as a literal string). This
470 * handles word styling.
473 term_word(struct termp *p, const char *word)
479 if (word[0] && 0 == word[1])
498 if ( ! (TERMP_IGNDELIM & p->flags))
499 p->flags |= TERMP_NOSPACE;
505 if ( ! (TERMP_NOSPACE & p->flags))
508 if ( ! (p->flags & TERMP_NONOSPACE))
509 p->flags &= ~TERMP_NOSPACE;
511 for ( ; *word; word++)
515 do_escaped(p, &word);
517 if (sv[0] && 0 == sv[1])
524 p->flags |= TERMP_NOSPACE;
533 * Insert a single character into the line-buffer. If the buffer's
534 * space is exceeded, then allocate more space by doubling the buffer
538 buffer(struct termp *p, char c)
542 if (p->col + 1 >= p->maxcols) {
546 p->buf = realloc(p->buf, s);
547 if (NULL == p->buf) {
553 p->buf[(int)(p->col)++] = c;
558 encode(struct termp *p, char c)
576 term_vspan(const struct roffsu *su)
594 r = su->scale / 1000;
606 return(/* LINTED */(size_t)
612 term_hspan(const struct roffsu *su)
616 /* XXX: CM, IN, and PT are approximations. */
623 /* XXX: this is an approximation. */
627 r = (10 * su->scale) / 6;
630 r = (10 * su->scale) / 72;
633 r = su->scale / 1000; /* FIXME: double-check. */
636 r = su->scale * 2 - 1; /* FIXME: double-check. */
645 return((size_t)/* LINTED */