1 /* $Id: term.c,v 1.127 2009/11/12 08:21:06 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.
17 #include <sys/types.h>
33 static struct termp *term_alloc(enum termenc);
34 static void term_free(struct termp *);
35 static void spec(struct termp *, const char *, size_t);
36 static void res(struct termp *, const char *, size_t);
37 static void buffera(struct termp *, const char *, size_t);
38 static void bufferc(struct termp *, char);
39 static void adjbuf(struct termp *p, size_t);
40 static void encode(struct termp *, const char *, size_t);
47 return(term_alloc(TERMENC_ASCII));
52 terminal_free(void *arg)
55 term_free((struct termp *)arg);
60 term_free(struct termp *p)
66 chars_free(p->symtab);
73 term_alloc(enum termenc enc)
77 p = calloc(1, sizeof(struct termp));
89 * Flush a line of text. A "line" is loosely defined as being something
90 * that should be followed by a newline, regardless of whether it's
91 * broken apart by newlines getting there. A line can also be a
92 * fragment of a columnar list.
94 * Specifically, a line is whatever's in p->buf of length p->col, which
95 * is zeroed after this function returns.
97 * The usage of termp:flags is as follows:
99 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
100 * offset value. This is useful when doing columnar lists where the
101 * prior column has right-padded.
103 * - TERMP_NOBREAK: this is the most important and is used when making
104 * columns. In short: don't print a newline and instead pad to the
105 * right margin. Used in conjunction with TERMP_NOLPAD.
107 * - TERMP_TWOSPACE: when padding, make sure there are at least two
108 * space characters of padding. Otherwise, rather break the line.
110 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
111 * the line is overrun, and don't pad-right if it's underrun.
113 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
114 * overruning, instead save the position and continue at that point
115 * when the next invocation.
117 * In-line line breaking:
119 * If TERMP_NOBREAK is specified and the line overruns the right
120 * margin, it will break and pad-right to the right margin after
121 * writing. If maxrmargin is violated, it will break and continue
122 * writing from the right-margin, which will lead to the above scenario
123 * upon exit. Otherwise, the line will break at the right margin.
126 term_flushln(struct termp *p)
128 int i; /* current input position in p->buf */
129 size_t vis; /* current visual position on output */
130 size_t vbl; /* number of blanks to prepend to output */
131 size_t vsz; /* visual characters to write to output */
132 size_t bp; /* visual right border position */
133 int j; /* temporary loop index */
135 static int overstep = 0;
138 * First, establish the maximum columns of "visible" content.
139 * This is usually the difference between the right-margin and
140 * an indentation, but can be, for tagged lists or columns, a
141 * small set of values.
144 assert(p->offset < p->rmargin);
146 maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
148 0 : p->rmargin - p->offset - overstep;
149 mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
151 0 : p->maxrmargin - p->offset - overstep;
153 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
156 * FIXME: if bp is zero, we still output the first word before
163 * If in the standard case (left-justified), then begin with our
164 * indentation, otherwise (columns, etc.) just start spitting
168 if ( ! (p->flags & TERMP_NOLPAD))
170 for (j = 0; j < (int)p->offset; j++)
173 for (i = 0; i < (int)p->col; i++) {
175 * Count up visible word characters. Control sequences
176 * (starting with the CSI) aren't counted. A space
177 * generates a non-printing word, which is valid (the
178 * space is printed according to regular spacing rules).
182 for (j = i, vsz = 0; j < (int)p->col; j++) {
183 if (j && ' ' == p->buf[j])
185 else if (8 == p->buf[j])
192 * Choose the number of blanks to prepend: no blank at the
193 * beginning of a line, one between words -- but do not
194 * actually write them yet.
196 vbl = (size_t)(0 == vis ? 0 : 1);
199 * Find out whether we would exceed the right margin.
200 * If so, break to the next line. (TODO: hyphenate)
201 * Otherwise, write the chosen number of blanks now.
203 if (vis && vis + vbl + vsz > bp) {
205 if (TERMP_NOBREAK & p->flags) {
206 for (j = 0; j < (int)p->rmargin; j++)
208 vis = p->rmargin - p->offset;
210 for (j = 0; j < (int)p->offset; j++)
214 /* Remove the overstep width. */
215 bp += (int)/* LINTED */
219 for (j = 0; j < (int)vbl; j++)
225 * Finally, write out the word.
227 for ( ; i < (int)p->col; i++) {
228 if (' ' == p->buf[i])
231 /* The unit sep. is a non-breaking space. */
243 if ( ! (TERMP_NOBREAK & p->flags)) {
248 if (TERMP_HANG & p->flags) {
249 /* We need one blank after the tag. */
250 overstep = /* LINTED */
254 * Behave exactly the same way as groff:
255 * If we have overstepped the margin, temporarily move
256 * it to the right and flag the rest of the line to be
258 * If we landed right at the margin, be happy.
259 * If we are one step before the margin, temporarily
260 * move it one step LEFT and flag the rest of the line
263 if (overstep >= -1) {
264 assert((int)maxvis + overstep >= 0);
270 } else if (TERMP_DANGLE & p->flags)
274 if (maxvis > vis + /* LINTED */
275 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
276 for ( ; vis < maxvis; vis++)
278 else { /* ...or newline break. */
280 for (i = 0; i < (int)p->rmargin; i++)
287 * A newline only breaks an existing line; it won't assert vertical
288 * space. All data in the output buffer is flushed prior to the newline
292 term_newln(struct termp *p)
295 p->flags |= TERMP_NOSPACE;
297 p->flags &= ~TERMP_NOLPAD;
301 p->flags &= ~TERMP_NOLPAD;
306 * Asserts a vertical space (a full, empty line-break between lines).
307 * Note that if used twice, this will cause two blank spaces and so on.
308 * All data in the output buffer is flushed prior to the newline
312 term_vspace(struct termp *p)
321 spec(struct termp *p, const char *word, size_t len)
326 rhs = chars_a2ascii(p->symtab, word, len, &sz);
333 res(struct termp *p, const char *word, size_t len)
338 rhs = chars_a2res(p->symtab, word, len, &sz);
345 term_fontlast(struct termp *p)
350 p->fontl = p->fontq[p->fonti];
351 p->fontq[p->fonti] = f;
356 term_fontrepl(struct termp *p, enum termfont f)
359 p->fontl = p->fontq[p->fonti];
360 p->fontq[p->fonti] = f;
365 term_fontpush(struct termp *p, enum termfont f)
368 assert(p->fonti + 1 < 10);
369 p->fontl = p->fontq[p->fonti];
370 p->fontq[++p->fonti] = f;
375 term_fontq(struct termp *p)
378 return(&p->fontq[p->fonti]);
383 term_fonttop(struct termp *p)
386 return(p->fontq[p->fonti]);
391 term_fontpopq(struct termp *p, const void *key)
394 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
396 assert(p->fonti >= 0);
401 term_fontpop(struct termp *p)
410 * Handle pwords, partial words, which may be either a single word or a
411 * phrase that cannot be broken down (such as a literal string). This
412 * handles word styling.
415 term_word(struct termp *p, const char *word)
417 const char *sv, *seq;
424 if (word[0] && '\0' == word[1])
443 if ( ! (TERMP_IGNDELIM & p->flags))
444 p->flags |= TERMP_NOSPACE;
450 if ( ! (TERMP_NOSPACE & p->flags))
453 if ( ! (p->flags & TERMP_NONOSPACE))
454 p->flags &= ~TERMP_NOSPACE;
456 /* FIXME: use strcspn. */
466 sz = a2roffdeco(&deco, &seq, &ssz);
469 case (DECO_RESERVED):
476 term_fontrepl(p, TERMFONT_BOLD);
479 term_fontrepl(p, TERMFONT_UNDER);
482 term_fontrepl(p, TERMFONT_NONE);
484 case (DECO_PREVIOUS):
492 if (DECO_NOSPACE == deco && '\0' == *word)
493 p->flags |= TERMP_NOSPACE;
496 if (sv[0] && 0 == sv[1])
503 p->flags |= TERMP_NOSPACE;
512 adjbuf(struct termp *p, size_t sz)
517 while (sz >= p->maxcols)
520 p->buf = realloc(p->buf, p->maxcols);
521 if (NULL == p->buf) {
529 buffera(struct termp *p, const char *word, size_t sz)
532 if (p->col + sz >= p->maxcols)
533 adjbuf(p, p->col + sz);
535 memcpy(&p->buf[(int)p->col], word, sz);
541 bufferc(struct termp *p, char c)
544 if (p->col + 1 >= p->maxcols)
545 adjbuf(p, p->col + 1);
547 p->buf[(int)p->col++] = c;
552 encode(struct termp *p, const char *word, size_t sz)
558 * Encode and buffer a string of characters. If the current
559 * font mode is unset, buffer directly, else encode then buffer
560 * character by character.
563 if (TERMFONT_NONE == (f = term_fonttop(p))) {
564 buffera(p, word, sz);
568 for (i = 0; i < (int)sz; i++) {
569 if ( ! isgraph((u_char)word[i])) {
574 if (TERMFONT_UNDER == f)
586 term_vspan(const struct roffsu *su)
604 r = su->scale / 1000;
616 return(/* LINTED */(size_t)
622 term_hspan(const struct roffsu *su)
626 /* XXX: CM, IN, and PT are approximations. */
633 /* XXX: this is an approximation. */
637 r = (10 * su->scale) / 6;
640 r = (10 * su->scale) / 72;
643 r = su->scale / 1000; /* FIXME: double-check. */
646 r = su->scale * 2 - 1; /* FIXME: double-check. */
655 return((size_t)/* LINTED */