term.c revision 371584c2eae4cf827fd406ba26c14f021adaaa70
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: term.c,v 1.257 2016/04/12 15:30:00 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorestatic size_t cond_width(const struct termp *, int, int *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void encode(struct termp *, const char *, size_t);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Flush a chunk of text. By default, break the output line each time
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the right margin is reached, and continue output on the next line
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * at the same offset as the chunk itself. By default, also break the
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * output line at the end of the chunk.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * The following flags may be specified:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_NOBREAK: Do not break the output line at the right margin,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * but only at the max right margin. Also, do not break the output
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * line at the end of the chunk, such that the next call can pad to
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the next column. However, if less than p->trailspace blanks,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * which can be 0, 1, or 2, remain to the right margin, the line
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * will be broken.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * - TERMP_BRTRSP: Consider trailing whitespace significant
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * when deciding whether the chunk fits or not.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_BRIND: If the chunk does not fit and the output line has
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * to be broken, start the next line at the right margin instead
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * of at the offset. Used together with TERMP_NOBREAK for the tags
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * in various kinds of tagged lists.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_DANGLE: Do not break the output line at the right margin,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * append the next chunk after it even if this one is too long.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * To be used together with TERMP_NOBREAK.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the next chunk if this column is not full.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t i; /* current input position in p->buf */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vis; /* current visual position on output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vbl; /* number of blanks to prepend to output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vend; /* end of word visual position on output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t bp; /* visual right border position */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t dv; /* temporary for visual pos calculations */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t j; /* temporary loop index for p->buf */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t jhy; /* last hyph before overflow w/r/t j */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t maxvis; /* output position of visible boundary */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * First, establish the maximum columns of "visible" content.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * This is usually the difference between the right-margin and
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * an indentation, but can be, for tagged lists or columns, a
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * small set of values.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * The following unsigned-signed subtractions look strange,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * but they are actually correct. If the int p->overstep
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * is negative, it gets sign extended. Subtracting that
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * very large size_t effectively adds a small number to dv.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov dv = p->rmargin > p->offset ? p->rmargin - p->offset : 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Calculate the required amount of padding.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (i < p->col) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Handle literal tab characters: collapse all
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * subsequent tabs into a single huge set of spaces.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend = (vis / p->tabwidth + 1) * p->tabwidth;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Count up visible word characters. Control sequences
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * (starting with the CSI) aren't counted. A space
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * generates a non-printing word, which is valid (the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * space is printed according to regular spacing rules).
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Back over the last printed character. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Regular word. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Break at the hyphen point if we overrun. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Hyphenation now decided, put back a real
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * hyphen such that we get the correct width.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Find out whether we would exceed the right margin.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If so, break to the next line.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore /* use pending tabs on the new line */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Remove the p->overstep width.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Again, if p->overstep is negative,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * sign extension does the right thing.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Write out the [remaining] word. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for ( ; i < p->col; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Now we definitely know there will be
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * printable characters to output,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * so write preceding white space now.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If there was trailing white space, it was not printed;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * so reset the cursor position accordingly.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->overstep += (int)(p->offset + vis - p->rmargin +
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we have overstepped the margin, temporarily move
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * it to the right and flag the rest of the line to be
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * If there is a request to keep the columns together,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * allow negative overstep when the column is not full.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Trailing whitespace is significant in some columns. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* If the column was overrun, break the line. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * A newline only breaks an existing line; it won't assert vertical
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * space. All data in the output buffer is flushed prior to the newline
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Asserts a vertical space (a full, empty line-break between lines).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Note that if used twice, this will cause two blank spaces and so on.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * All data in the output buffer is flushed prior to the newline
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Swap current and previous font; for \fP and .ft P */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Set font, save current, discard previous; for \f, .ft, .B etc. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontrepl(struct termp *p, enum termfont f)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Set font, save previous. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontpush(struct termp *p, enum termfont f)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Flush to make the saved pointer current again. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Pop one font off the stack. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Handle pwords, partial words, which may be either a single word or a
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * phrase that cannot be broken down (such as a literal string). This
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * handles word styling.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Trim trailing backspace/blank pair. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for Unicode and numbered
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * character escape sequences.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * See encode().
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Do this for a single (probably unicode) value.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Does not check for non-decorated glyphs.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreencode(struct termp *p, const char *word, size_t sz)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for (i = 0; i < sz; i++) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorecond_width(const struct termp *p, int c, int *skip)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return (*p->width)(p, c);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_strlen(const struct termp *p, const char *cp)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Account for escaped sequences within string length
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * calculations. This follows the logic in term_word() as we
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * must calculate the width of produced strings.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < rsz; i++)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for Unicode and numbered
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * character escape sequences.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for all escape sequences
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * printing more than one character.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < rsz; i++)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_vspan(const struct termp *p, const struct roffsu *su)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * Convert a scaling width to basic units, rounding down.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_hspan(const struct termp *p, const struct roffsu *su)