1 /* $Id: term.c,v 1.197 2011/05/24 21:31:23 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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>
36 static void adjbuf(struct termp *p, int);
37 static void bufferc(struct termp *, char);
38 static void encode(struct termp *, const char *, size_t);
39 static void encode1(struct termp *, int);
42 term_free(struct termp *p)
48 mchars_free(p->symtab);
55 term_begin(struct termp *p, term_margin head,
56 term_margin foot, const void *arg)
67 term_end(struct termp *p)
74 * Flush a line of text. A "line" is loosely defined as being something
75 * that should be followed by a newline, regardless of whether it's
76 * broken apart by newlines getting there. A line can also be a
77 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
78 * not have a trailing newline.
80 * The following flags may be specified:
82 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
83 * offset value. This is useful when doing columnar lists where the
84 * prior column has right-padded.
86 * - TERMP_NOBREAK: this is the most important and is used when making
87 * columns. In short: don't print a newline and instead pad to the
88 * right margin. Used in conjunction with TERMP_NOLPAD.
90 * - TERMP_TWOSPACE: when padding, make sure there are at least two
91 * space characters of padding. Otherwise, rather break the line.
93 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
94 * the line is overrun, and don't pad-right if it's underrun.
96 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
97 * overruning, instead save the position and continue at that point
98 * when the next invocation.
100 * In-line line breaking:
102 * If TERMP_NOBREAK is specified and the line overruns the right
103 * margin, it will break and pad-right to the right margin after
104 * writing. If maxrmargin is violated, it will break and continue
105 * writing from the right-margin, which will lead to the above scenario
106 * upon exit. Otherwise, the line will break at the right margin.
109 term_flushln(struct termp *p)
111 int i; /* current input position in p->buf */
112 size_t vis; /* current visual position on output */
113 size_t vbl; /* number of blanks to prepend to output */
114 size_t vend; /* end of word visual position on output */
115 size_t bp; /* visual right border position */
116 size_t dv; /* temporary for visual pos calculations */
117 int j; /* temporary loop index for p->buf */
118 int jhy; /* last hyph before overflow w/r/t j */
119 size_t maxvis; /* output position of visible boundary */
120 size_t mmax; /* used in calculating bp */
123 * First, establish the maximum columns of "visible" content.
124 * This is usually the difference between the right-margin and
125 * an indentation, but can be, for tagged lists or columns, a
126 * small set of values.
128 assert (p->rmargin >= p->offset);
129 dv = p->rmargin - p->offset;
130 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
131 dv = p->maxrmargin - p->offset;
132 mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
134 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
137 * Indent the first line of a paragraph.
139 vbl = p->flags & TERMP_NOLPAD ? (size_t)0 : p->offset;
146 * Handle literal tab characters: collapse all
147 * subsequent tabs into a single huge set of spaces.
149 while (i < p->col && '\t' == p->buf[i]) {
150 vend = (vis / p->tabwidth + 1) * p->tabwidth;
157 * Count up visible word characters. Control sequences
158 * (starting with the CSI) aren't counted. A space
159 * generates a non-printing word, which is valid (the
160 * space is printed according to regular spacing rules).
163 for (j = i, jhy = 0; j < p->col; j++) {
164 if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
167 /* Back over the the last printed character. */
168 if (8 == p->buf[j]) {
170 vend -= (*p->width)(p, p->buf[j - 1]);
175 /* Break at the hyphen point if we overrun. */
176 if (vend > vis && vend < bp &&
177 ASCII_HYPH == p->buf[j])
180 vend += (*p->width)(p, p->buf[j]);
184 * Find out whether we would exceed the right margin.
185 * If so, break to the next line.
187 if (vend > bp && 0 == jhy && vis > 0) {
190 if (TERMP_NOBREAK & p->flags) {
191 p->viscol = p->rmargin;
192 (*p->advance)(p, p->rmargin);
193 vend += p->rmargin - p->offset;
199 /* Remove the p->overstep width. */
201 bp += (size_t)p->overstep;
205 /* Write out the [remaining] word. */
206 for ( ; i < p->col; i++) {
207 if (vend > bp && jhy > 0 && i > jhy)
209 if ('\t' == p->buf[i])
211 if (' ' == p->buf[i]) {
213 while (' ' == p->buf[i])
215 dv = (size_t)(i - j) * (*p->width)(p, ' ');
220 if (ASCII_NBRSP == p->buf[i]) {
221 vbl += (*p->width)(p, ' ');
226 * Now we definitely know there will be
227 * printable characters to output,
228 * so write preceding white space now.
231 (*p->advance)(p, vbl);
236 if (ASCII_HYPH == p->buf[i]) {
237 (*p->letter)(p, '-');
238 p->viscol += (*p->width)(p, '-');
240 (*p->letter)(p, p->buf[i]);
241 p->viscol += (*p->width)(p, p->buf[i]);
248 * If there was trailing white space, it was not printed;
249 * so reset the cursor position accordingly.
256 if ( ! (TERMP_NOBREAK & p->flags)) {
262 if (TERMP_HANG & p->flags) {
263 /* We need one blank after the tag. */
264 p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
267 * Behave exactly the same way as groff:
268 * If we have overstepped the margin, temporarily move
269 * it to the right and flag the rest of the line to be
271 * If we landed right at the margin, be happy.
272 * If we are one step before the margin, temporarily
273 * move it one step LEFT and flag the rest of the line
276 if (p->overstep >= -1) {
277 assert((int)maxvis + p->overstep >= 0);
278 maxvis += (size_t)p->overstep;
282 } else if (TERMP_DANGLE & p->flags)
287 ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
288 p->viscol += maxvis - vis;
289 (*p->advance)(p, maxvis - vis);
290 vis += (maxvis - vis);
291 } else { /* ...or newline break. */
293 p->viscol = p->rmargin;
294 (*p->advance)(p, p->rmargin);
300 * A newline only breaks an existing line; it won't assert vertical
301 * space. All data in the output buffer is flushed prior to the newline
305 term_newln(struct termp *p)
308 p->flags |= TERMP_NOSPACE;
309 if (0 == p->col && 0 == p->viscol) {
310 p->flags &= ~TERMP_NOLPAD;
314 p->flags &= ~TERMP_NOLPAD;
319 * Asserts a vertical space (a full, empty line-break between lines).
320 * Note that if used twice, this will cause two blank spaces and so on.
321 * All data in the output buffer is flushed prior to the newline
325 term_vspace(struct termp *p)
334 term_fontlast(struct termp *p)
339 p->fontl = p->fontq[p->fonti];
340 p->fontq[p->fonti] = f;
345 term_fontrepl(struct termp *p, enum termfont f)
348 p->fontl = p->fontq[p->fonti];
349 p->fontq[p->fonti] = f;
354 term_fontpush(struct termp *p, enum termfont f)
357 assert(p->fonti + 1 < 10);
358 p->fontl = p->fontq[p->fonti];
359 p->fontq[++p->fonti] = f;
364 term_fontq(struct termp *p)
367 return(&p->fontq[p->fonti]);
372 term_fonttop(struct termp *p)
375 return(p->fontq[p->fonti]);
380 term_fontpopq(struct termp *p, const void *key)
383 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
385 assert(p->fonti >= 0);
390 term_fontpop(struct termp *p)
398 * Handle pwords, partial words, which may be either a single word or a
399 * phrase that cannot be broken down (such as a literal string). This
400 * handles word styling.
403 term_word(struct termp *p, const char *word)
405 const char *seq, *cp;
411 if ( ! (TERMP_NOSPACE & p->flags)) {
412 if ( ! (TERMP_KEEP & p->flags)) {
413 if (TERMP_PREKEEP & p->flags)
414 p->flags |= TERMP_KEEP;
416 if (TERMP_SENTENCE & p->flags)
419 bufferc(p, ASCII_NBRSP);
422 if ( ! (p->flags & TERMP_NONOSPACE))
423 p->flags &= ~TERMP_NOSPACE;
425 p->flags |= TERMP_NOSPACE;
427 p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM);
429 while ('\0' != *word) {
430 if ((ssz = strcspn(word, "\\")) > 0)
431 encode(p, word, ssz);
438 esc = mandoc_escape(&word, &seq, &sz);
439 if (ESCAPE_ERROR == esc)
442 if (TERMENC_ASCII != p->enc)
444 case (ESCAPE_UNICODE):
445 uc = mchars_num2uc(seq + 1, sz - 1);
450 case (ESCAPE_SPECIAL):
451 uc = mchars_spec2cp(p->symtab, seq, sz);
461 case (ESCAPE_UNICODE):
464 case (ESCAPE_NUMBERED):
465 c = mchars_num2char(seq, sz);
469 case (ESCAPE_SPECIAL):
470 cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
476 case (ESCAPE_FONTBOLD):
477 term_fontrepl(p, TERMFONT_BOLD);
479 case (ESCAPE_FONTITALIC):
480 term_fontrepl(p, TERMFONT_UNDER);
484 case (ESCAPE_FONTROMAN):
485 term_fontrepl(p, TERMFONT_NONE);
487 case (ESCAPE_FONTPREV):
490 case (ESCAPE_NOSPACE):
492 p->flags |= TERMP_NOSPACE;
501 adjbuf(struct termp *p, int sz)
506 while (sz >= p->maxcols)
509 p->buf = mandoc_realloc
510 (p->buf, sizeof(int) * (size_t)p->maxcols);
514 bufferc(struct termp *p, char c)
517 if (p->col + 1 >= p->maxcols)
518 adjbuf(p, p->col + 1);
520 p->buf[p->col++] = c;
525 * Do this for a single (probably unicode) value.
526 * Does not check for non-decorated glyphs.
529 encode1(struct termp *p, int c)
533 if (p->col + 4 >= p->maxcols)
534 adjbuf(p, p->col + 4);
538 if (TERMFONT_NONE == f) {
539 p->buf[p->col++] = c;
541 } else if (TERMFONT_UNDER == f) {
542 p->buf[p->col++] = '_';
544 p->buf[p->col++] = c;
546 p->buf[p->col++] = 8;
547 p->buf[p->col++] = c;
551 encode(struct termp *p, const char *word, size_t sz)
560 * Encode and buffer a string of characters. If the current
561 * font mode is unset, buffer directly, else encode then buffer
562 * character by character.
565 if (TERMFONT_NONE == (f = term_fonttop(p))) {
566 if (p->col + len >= p->maxcols)
567 adjbuf(p, p->col + len);
568 for (i = 0; i < len; i++)
569 p->buf[p->col++] = word[i];
573 /* Pre-buffer, assuming worst-case. */
575 if (p->col + 1 + (len * 3) >= p->maxcols)
576 adjbuf(p, p->col + 1 + (len * 3));
578 for (i = 0; i < len; i++) {
579 if ( ! isgraph((unsigned char)word[i])) {
580 p->buf[p->col++] = word[i];
584 if (TERMFONT_UNDER == f)
585 p->buf[p->col++] = '_';
587 p->buf[p->col++] = word[i];
589 p->buf[p->col++] = 8;
590 p->buf[p->col++] = word[i];
595 term_len(const struct termp *p, size_t sz)
598 return((*p->width)(p, ' ') * sz);
603 term_strlen(const struct termp *p, const char *cp)
607 const char *seq, *rhs;
609 static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
612 * Account for escaped sequences within string length
613 * calculations. This follows the logic in term_word() as we
614 * must calculate the width of produced strings.
618 while ('\0' != *cp) {
619 rsz = strcspn(cp, rej);
620 for (i = 0; i < rsz; i++)
621 sz += (*p->width)(p, *cp++);
627 esc = mandoc_escape(&cp, &seq, &ssz);
628 if (ESCAPE_ERROR == esc)
631 if (TERMENC_ASCII != p->enc)
633 case (ESCAPE_UNICODE):
638 sz += (*p->width)(p, c);
640 case (ESCAPE_SPECIAL):
642 (p->symtab, seq, ssz);
645 sz += (*p->width)(p, c);
654 case (ESCAPE_UNICODE):
655 sz += (*p->width)(p, '?');
657 case (ESCAPE_NUMBERED):
658 c = mchars_num2char(seq, ssz);
660 sz += (*p->width)(p, c);
662 case (ESCAPE_SPECIAL):
663 rhs = mchars_spec2str
664 (p->symtab, seq, ssz, &rsz);
679 for (i = 0; i < rsz; i++)
680 sz += (*p->width)(p, *rhs++);
683 sz += (*p->width)(p, ' ');
687 sz += (*p->width)(p, '-');
700 term_vspan(const struct termp *p, const struct roffsu *su)
718 r = su->scale / 1000;
730 return(/* LINTED */(size_t)
735 term_hspan(const struct termp *p, const struct roffsu *su)
739 v = ((*p->hspan)(p, su));
742 return((size_t) /* LINTED */