1 /* $Id: term.c,v 1.226 2014/08/01 19:38:29 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
31 #include "mandoc_aux.h"
36 static size_t cond_width(const struct termp *, int, int *);
37 static void adjbuf(struct termp *p, size_t);
38 static void bufferc(struct termp *, char);
39 static void encode(struct termp *, const char *, size_t);
40 static void encode1(struct termp *, int);
44 term_free(struct termp *p)
50 mchars_free(p->symtab);
56 term_begin(struct termp *p, term_margin head,
57 term_margin foot, const void *arg)
67 term_end(struct termp *p)
74 * Flush a chunk of text. By default, break the output line each time
75 * the right margin is reached, and continue output on the next line
76 * at the same offset as the chunk itself. By default, also break the
77 * output line at the end of the chunk.
78 * The following flags may be specified:
80 * - TERMP_NOBREAK: Do not break the output line at the right margin,
81 * but only at the max right margin. Also, do not break the output
82 * line at the end of the chunk, such that the next call can pad to
83 * the next column. However, if less than p->trailspace blanks,
84 * which can be 0, 1, or 2, remain to the right margin, the line
86 * - TERMP_BRIND: If the chunk does not fit and the output line has
87 * to be broken, start the next line at the right margin instead
88 * of at the offset. Used together with TERMP_NOBREAK for the tags
89 * in various kinds of tagged lists.
90 * - TERMP_DANGLE: Do not break the output line at the right margin,
91 * append the next chunk after it even if this one is too long.
92 * To be used together with TERMP_NOBREAK.
93 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
94 * the next chunk if this column is not full.
97 term_flushln(struct termp *p)
99 size_t i; /* current input position in p->buf */
100 int ntab; /* number of tabs to prepend */
101 size_t vis; /* current visual position on output */
102 size_t vbl; /* number of blanks to prepend to output */
103 size_t vend; /* end of word visual position on output */
104 size_t bp; /* visual right border position */
105 size_t dv; /* temporary for visual pos calculations */
106 size_t j; /* temporary loop index for p->buf */
107 size_t jhy; /* last hyph before overflow w/r/t j */
108 size_t maxvis; /* output position of visible boundary */
109 size_t mmax; /* used in calculating bp */
112 * First, establish the maximum columns of "visible" content.
113 * This is usually the difference between the right-margin and
114 * an indentation, but can be, for tagged lists or columns, a
115 * small set of values.
117 * The following unsigned-signed subtractions look strange,
118 * but they are actually correct. If the int p->overstep
119 * is negative, it gets sign extended. Subtracting that
120 * very large size_t effectively adds a small number to dv.
122 assert (p->rmargin >= p->offset);
123 dv = p->rmargin - p->offset;
124 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
125 dv = p->maxrmargin - p->offset;
126 mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
128 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
131 * Calculate the required amount of padding.
133 vbl = p->offset + p->overstep > p->viscol ?
134 p->offset + p->overstep - p->viscol : 0;
141 * Handle literal tab characters: collapse all
142 * subsequent tabs into a single huge set of spaces.
145 while (i < p->col && '\t' == p->buf[i]) {
146 vend = (vis / p->tabwidth + 1) * p->tabwidth;
154 * Count up visible word characters. Control sequences
155 * (starting with the CSI) aren't counted. A space
156 * generates a non-printing word, which is valid (the
157 * space is printed according to regular spacing rules).
160 for (j = i, jhy = 0; j < p->col; j++) {
161 if (' ' == p->buf[j] || '\t' == p->buf[j])
164 /* Back over the the last printed character. */
165 if (8 == p->buf[j]) {
167 vend -= (*p->width)(p, p->buf[j - 1]);
172 /* Break at the hyphen point if we overrun. */
173 if (vend > vis && vend < bp &&
174 (ASCII_HYPH == p->buf[j] ||
175 ASCII_BREAK == p->buf[j]))
179 * Hyphenation now decided, put back a real
180 * hyphen such that we get the correct width.
182 if (ASCII_HYPH == p->buf[j])
185 vend += (*p->width)(p, p->buf[j]);
189 * Find out whether we would exceed the right margin.
190 * If so, break to the next line.
192 if (vend > bp && 0 == jhy && vis > 0) {
196 if (TERMP_BRIND & p->flags) {
198 vend += p->rmargin - p->offset;
202 /* use pending tabs on the new line */
205 vbl += ntab * p->tabwidth;
208 * Remove the p->overstep width.
209 * Again, if p->overstep is negative,
210 * sign extension does the right thing.
213 bp += (size_t)p->overstep;
217 /* Write out the [remaining] word. */
218 for ( ; i < p->col; i++) {
219 if (vend > bp && jhy > 0 && i > jhy)
221 if ('\t' == p->buf[i])
223 if (' ' == p->buf[i]) {
225 while (' ' == p->buf[i])
227 dv = (i - j) * (*p->width)(p, ' ');
232 if (ASCII_NBRSP == p->buf[i]) {
233 vbl += (*p->width)(p, ' ');
236 if (ASCII_BREAK == p->buf[i])
240 * Now we definitely know there will be
241 * printable characters to output,
242 * so write preceding white space now.
245 (*p->advance)(p, vbl);
250 (*p->letter)(p, p->buf[i]);
252 p->viscol -= (*p->width)(p, p->buf[i-1]);
254 p->viscol += (*p->width)(p, p->buf[i]);
260 * If there was trailing white space, it was not printed;
261 * so reset the cursor position accordingly.
269 if ( ! (TERMP_NOBREAK & p->flags)) {
275 if (TERMP_HANG & p->flags) {
276 p->overstep = (int)(vis - maxvis +
277 p->trailspace * (*p->width)(p, ' '));
280 * If we have overstepped the margin, temporarily move
281 * it to the right and flag the rest of the line to be
283 * If there is a request to keep the columns together,
284 * allow negative overstep when the column is not full.
286 if (p->trailspace && p->overstep < 0)
290 } else if (TERMP_DANGLE & p->flags)
293 /* If the column was overrun, break the line. */
294 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
301 * A newline only breaks an existing line; it won't assert vertical
302 * space. All data in the output buffer is flushed prior to the newline
306 term_newln(struct termp *p)
309 p->flags |= TERMP_NOSPACE;
310 if (p->col || p->viscol)
315 * Asserts a vertical space (a full, empty line-break between lines).
316 * Note that if used twice, this will cause two blank spaces and so on.
317 * All data in the output buffer is flushed prior to the newline
321 term_vspace(struct termp *p)
333 term_fontlast(struct termp *p)
338 p->fontl = p->fontq[p->fonti];
339 p->fontq[p->fonti] = f;
343 term_fontrepl(struct termp *p, enum termfont f)
346 p->fontl = p->fontq[p->fonti];
347 p->fontq[p->fonti] = f;
351 term_fontpush(struct termp *p, enum termfont f)
354 assert(p->fonti + 1 < 10);
355 p->fontl = p->fontq[p->fonti];
356 p->fontq[++p->fonti] = f;
360 term_fontq(struct termp *p)
363 return(&p->fontq[p->fonti]);
367 term_fonttop(struct termp *p)
370 return(p->fontq[p->fonti]);
374 term_fontpopq(struct termp *p, const void *key)
377 while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
379 assert(p->fonti >= 0);
383 term_fontpop(struct termp *p)
391 * Handle pwords, partial words, which may be either a single word or a
392 * phrase that cannot be broken down (such as a literal string). This
393 * handles word styling.
396 term_word(struct termp *p, const char *word)
398 const char nbrsp[2] = { ASCII_NBRSP, 0 };
399 const char *seq, *cp;
405 if ( ! (TERMP_NOSPACE & p->flags)) {
406 if ( ! (TERMP_KEEP & p->flags)) {
408 if (TERMP_SENTENCE & p->flags)
411 bufferc(p, ASCII_NBRSP);
413 if (TERMP_PREKEEP & p->flags)
414 p->flags |= TERMP_KEEP;
416 if ( ! (p->flags & TERMP_NONOSPACE))
417 p->flags &= ~TERMP_NOSPACE;
419 p->flags |= TERMP_NOSPACE;
421 p->flags &= ~TERMP_SENTENCE;
423 while ('\0' != *word) {
425 if (TERMP_SKIPCHAR & p->flags) {
426 p->flags &= ~TERMP_SKIPCHAR;
430 if (TERMP_NBRWORD & p->flags) {
436 ssz = strcspn(word, "\\ ");
438 ssz = strcspn(word, "\\");
439 encode(p, word, ssz);
445 esc = mandoc_escape(&word, &seq, &sz);
446 if (ESCAPE_ERROR == esc)
449 if (TERMENC_ASCII != p->enc)
452 uc = mchars_num2uc(seq + 1, sz - 1);
458 uc = mchars_spec2cp(p->symtab, seq, sz);
471 case ESCAPE_NUMBERED:
472 c = mchars_num2char(seq, sz);
477 cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
483 case ESCAPE_FONTBOLD:
484 term_fontrepl(p, TERMFONT_BOLD);
486 case ESCAPE_FONTITALIC:
487 term_fontrepl(p, TERMFONT_UNDER);
490 term_fontrepl(p, TERMFONT_BI);
494 case ESCAPE_FONTROMAN:
495 term_fontrepl(p, TERMFONT_NONE);
497 case ESCAPE_FONTPREV:
501 if (TERMP_SKIPCHAR & p->flags)
502 p->flags &= ~TERMP_SKIPCHAR;
503 else if ('\0' == *word)
504 p->flags |= TERMP_NOSPACE;
506 case ESCAPE_SKIPCHAR:
507 p->flags |= TERMP_SKIPCHAR;
513 p->flags &= ~TERMP_NBRWORD;
517 adjbuf(struct termp *p, size_t sz)
522 while (sz >= p->maxcols)
525 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
529 bufferc(struct termp *p, char c)
532 if (p->col + 1 >= p->maxcols)
533 adjbuf(p, p->col + 1);
535 p->buf[p->col++] = c;
540 * Do this for a single (probably unicode) value.
541 * Does not check for non-decorated glyphs.
544 encode1(struct termp *p, int c)
548 if (TERMP_SKIPCHAR & p->flags) {
549 p->flags &= ~TERMP_SKIPCHAR;
553 if (p->col + 6 >= p->maxcols)
554 adjbuf(p, p->col + 6);
558 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
559 p->buf[p->col++] = '_';
560 p->buf[p->col++] = 8;
562 if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
564 p->buf[p->col++] = '-';
566 p->buf[p->col++] = c;
567 p->buf[p->col++] = 8;
569 p->buf[p->col++] = c;
573 encode(struct termp *p, const char *word, size_t sz)
577 if (TERMP_SKIPCHAR & p->flags) {
578 p->flags &= ~TERMP_SKIPCHAR;
583 * Encode and buffer a string of characters. If the current
584 * font mode is unset, buffer directly, else encode then buffer
585 * character by character.
588 if (TERMFONT_NONE == term_fonttop(p)) {
589 if (p->col + sz >= p->maxcols)
590 adjbuf(p, p->col + sz);
591 for (i = 0; i < sz; i++)
592 p->buf[p->col++] = word[i];
596 /* Pre-buffer, assuming worst-case. */
598 if (p->col + 1 + (sz * 5) >= p->maxcols)
599 adjbuf(p, p->col + 1 + (sz * 5));
601 for (i = 0; i < sz; i++) {
602 if (ASCII_HYPH == word[i] ||
603 isgraph((unsigned char)word[i]))
606 p->buf[p->col++] = word[i];
611 term_setwidth(struct termp *p, const char *wstr)
632 if (a2roffsu(wstr, &su, SCALE_MAX))
633 width = term_hspan(p, &su);
637 (*p->setwidth)(p, iop, width);
641 term_len(const struct termp *p, size_t sz)
644 return((*p->width)(p, ' ') * sz);
648 cond_width(const struct termp *p, int c, int *skip)
655 return((*p->width)(p, c));
659 term_strlen(const struct termp *p, const char *cp)
663 const char *seq, *rhs;
665 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
669 * Account for escaped sequences within string length
670 * calculations. This follows the logic in term_word() as we
671 * must calculate the width of produced strings.
676 while ('\0' != *cp) {
677 rsz = strcspn(cp, rej);
678 for (i = 0; i < rsz; i++)
679 sz += cond_width(p, *cp++, &skip);
684 esc = mandoc_escape(&cp, &seq, &ssz);
685 if (ESCAPE_ERROR == esc)
688 if (TERMENC_ASCII != p->enc)
691 c = mchars_num2uc(seq + 1,
695 sz += cond_width(p, c, &skip);
698 c = mchars_spec2cp(p->symtab,
702 sz += cond_width(p, c, &skip);
712 sz += cond_width(p, '?', &skip);
714 case ESCAPE_NUMBERED:
715 c = mchars_num2char(seq, ssz);
717 sz += cond_width(p, c, &skip);
720 rhs = mchars_spec2str(p->symtab,
729 case ESCAPE_SKIPCHAR:
744 for (i = 0; i < rsz; i++)
745 sz += (*p->width)(p, *rhs++);
748 sz += cond_width(p, ' ', &skip);
752 sz += cond_width(p, '-', &skip);
766 term_vspan(const struct termp *p, const struct roffsu *su)
784 r = su->scale / 1000.0;
796 return((size_t)(r + 0.0005));
800 term_hspan(const struct termp *p, const struct roffsu *su)
804 v = (*p->hspan)(p, su);
807 return((size_t)(v + 0.0005));