[BACK]Return to term.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/term.c, Revision 1.230

1.230   ! schwarze    1: /*     $Id: term.c,v 1.229 2014/10/26 17:12:03 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.216     schwarze    4:  * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.74      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.74      kristaps   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.
1.1       kristaps   17:  */
1.128     kristaps   18: #include "config.h"
                     19:
1.126     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.122     kristaps   23: #include <ctype.h>
1.22      kristaps   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.137     kristaps   28: #include "mandoc.h"
1.218     schwarze   29: #include "mandoc_aux.h"
1.107     kristaps   30: #include "out.h"
1.71      kristaps   31: #include "term.h"
1.105     kristaps   32: #include "main.h"
1.1       kristaps   33:
1.203     schwarze   34: static size_t           cond_width(const struct termp *, int, int *);
1.210     schwarze   35: static void             adjbuf(struct termp *p, size_t);
1.191     kristaps   36: static void             bufferc(struct termp *, char);
                     37: static void             encode(struct termp *, const char *, size_t);
1.194     kristaps   38: static void             encode1(struct termp *, int);
1.11      kristaps   39:
1.222     schwarze   40:
1.145     kristaps   41: void
1.71      kristaps   42: term_free(struct termp *p)
1.14      kristaps   43: {
                     44:
1.71      kristaps   45:        if (p->buf)
                     46:                free(p->buf);
1.102     kristaps   47:        if (p->symtab)
1.185     kristaps   48:                mchars_free(p->symtab);
1.145     kristaps   49:
1.142     kristaps   50:        free(p);
                     51: }
                     52:
                     53: void
1.222     schwarze   54: term_begin(struct termp *p, term_margin head,
1.142     kristaps   55:                term_margin foot, const void *arg)
                     56: {
                     57:
                     58:        p->headf = head;
                     59:        p->footf = foot;
                     60:        p->argf = arg;
1.146     kristaps   61:        (*p->begin)(p);
1.142     kristaps   62: }
                     63:
                     64: void
                     65: term_end(struct termp *p)
                     66: {
                     67:
1.146     kristaps   68:        (*p->end)(p);
1.14      kristaps   69: }
                     70:
1.71      kristaps   71: /*
1.221     schwarze   72:  * Flush a chunk of text.  By default, break the output line each time
                     73:  * the right margin is reached, and continue output on the next line
                     74:  * at the same offset as the chunk itself.  By default, also break the
                     75:  * output line at the end of the chunk.
1.130     kristaps   76:  * The following flags may be specified:
1.71      kristaps   77:  *
1.221     schwarze   78:  *  - TERMP_NOBREAK: Do not break the output line at the right margin,
                     79:  *    but only at the max right margin.  Also, do not break the output
                     80:  *    line at the end of the chunk, such that the next call can pad to
                     81:  *    the next column.  However, if less than p->trailspace blanks,
                     82:  *    which can be 0, 1, or 2, remain to the right margin, the line
                     83:  *    will be broken.
                     84:  *  - TERMP_BRIND: If the chunk does not fit and the output line has
                     85:  *    to be broken, start the next line at the right margin instead
                     86:  *    of at the offset.  Used together with TERMP_NOBREAK for the tags
                     87:  *    in various kinds of tagged lists.
                     88:  *  - TERMP_DANGLE: Do not break the output line at the right margin,
                     89:  *    append the next chunk after it even if this one is too long.
                     90:  *    To be used together with TERMP_NOBREAK.
                     91:  *  - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
                     92:  *    the next chunk if this column is not full.
1.71      kristaps   93:  */
                     94: void
                     95: term_flushln(struct termp *p)
1.53      kristaps   96: {
1.210     schwarze   97:        size_t           i;     /* current input position in p->buf */
1.205     schwarze   98:        int              ntab;  /* number of tabs to prepend */
1.114     kristaps   99:        size_t           vis;   /* current visual position on output */
                    100:        size_t           vbl;   /* number of blanks to prepend to output */
1.136     schwarze  101:        size_t           vend;  /* end of word visual position on output */
1.114     kristaps  102:        size_t           bp;    /* visual right border position */
1.172     schwarze  103:        size_t           dv;    /* temporary for visual pos calculations */
1.210     schwarze  104:        size_t           j;     /* temporary loop index for p->buf */
                    105:        size_t           jhy;   /* last hyph before overflow w/r/t j */
1.152     kristaps  106:        size_t           maxvis; /* output position of visible boundary */
                    107:        size_t           mmax; /* used in calculating bp */
1.53      kristaps  108:
1.71      kristaps  109:        /*
                    110:         * First, establish the maximum columns of "visible" content.
                    111:         * This is usually the difference between the right-margin and
                    112:         * an indentation, but can be, for tagged lists or columns, a
1.212     schwarze  113:         * small set of values.
                    114:         *
                    115:         * The following unsigned-signed subtractions look strange,
                    116:         * but they are actually correct.  If the int p->overstep
                    117:         * is negative, it gets sign extended.  Subtracting that
                    118:         * very large size_t effectively adds a small number to dv.
1.71      kristaps  119:         */
1.175     kristaps  120:        assert  (p->rmargin >= p->offset);
1.174     schwarze  121:        dv     = p->rmargin - p->offset;
                    122:        maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
                    123:        dv     = p->maxrmargin - p->offset;
                    124:        mmax   = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.92      kristaps  125:
1.71      kristaps  126:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  127:
1.136     schwarze  128:        /*
1.200     schwarze  129:         * Calculate the required amount of padding.
1.136     schwarze  130:         */
1.200     schwarze  131:        vbl = p->offset + p->overstep > p->viscol ?
                    132:              p->offset + p->overstep - p->viscol : 0;
1.136     schwarze  133:
1.174     schwarze  134:        vis = vend = 0;
                    135:        i = 0;
1.115     kristaps  136:
1.188     kristaps  137:        while (i < p->col) {
1.71      kristaps  138:                /*
1.154     kristaps  139:                 * Handle literal tab characters: collapse all
                    140:                 * subsequent tabs into a single huge set of spaces.
1.138     schwarze  141:                 */
1.205     schwarze  142:                ntab = 0;
1.188     kristaps  143:                while (i < p->col && '\t' == p->buf[i]) {
1.154     kristaps  144:                        vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.138     schwarze  145:                        vbl += vend - vis;
                    146:                        vis = vend;
1.205     schwarze  147:                        ntab++;
1.169     schwarze  148:                        i++;
1.138     schwarze  149:                }
                    150:
                    151:                /*
1.71      kristaps  152:                 * Count up visible word characters.  Control sequences
                    153:                 * (starting with the CSI) aren't counted.  A space
                    154:                 * generates a non-printing word, which is valid (the
                    155:                 * space is printed according to regular spacing rules).
                    156:                 */
                    157:
1.188     kristaps  158:                for (j = i, jhy = 0; j < p->col; j++) {
1.208     schwarze  159:                        if (' ' == p->buf[j] || '\t' == p->buf[j])
1.71      kristaps  160:                                break;
1.154     kristaps  161:
                    162:                        /* Back over the the last printed character. */
                    163:                        if (8 == p->buf[j]) {
1.153     kristaps  164:                                assert(j);
                    165:                                vend -= (*p->width)(p, p->buf[j - 1]);
1.154     kristaps  166:                                continue;
1.153     kristaps  167:                        }
1.154     kristaps  168:
                    169:                        /* Regular word. */
                    170:                        /* Break at the hyphen point if we overrun. */
1.222     schwarze  171:                        if (vend > vis && vend < bp &&
1.216     schwarze  172:                            (ASCII_HYPH == p->buf[j] ||
                    173:                             ASCII_BREAK == p->buf[j]))
1.154     kristaps  174:                                jhy = j;
                    175:
1.217     schwarze  176:                        /*
                    177:                         * Hyphenation now decided, put back a real
                    178:                         * hyphen such that we get the correct width.
                    179:                         */
                    180:                        if (ASCII_HYPH == p->buf[j])
                    181:                                p->buf[j] = '-';
                    182:
1.154     kristaps  183:                        vend += (*p->width)(p, p->buf[j]);
1.71      kristaps  184:                }
1.53      kristaps  185:
1.71      kristaps  186:                /*
1.81      kristaps  187:                 * Find out whether we would exceed the right margin.
1.136     schwarze  188:                 * If so, break to the next line.
1.81      kristaps  189:                 */
1.140     kristaps  190:                if (vend > bp && 0 == jhy && vis > 0) {
1.136     schwarze  191:                        vend -= vis;
1.146     kristaps  192:                        (*p->endline)(p);
1.201     schwarze  193:                        p->viscol = 0;
1.221     schwarze  194:                        if (TERMP_BRIND & p->flags) {
1.201     schwarze  195:                                vbl = p->rmargin;
1.136     schwarze  196:                                vend += p->rmargin - p->offset;
1.201     schwarze  197:                        } else
1.136     schwarze  198:                                vbl = p->offset;
1.205     schwarze  199:
                    200:                        /* use pending tabs on the new line */
                    201:
                    202:                        if (0 < ntab)
                    203:                                vbl += ntab * p->tabwidth;
1.130     kristaps  204:
1.212     schwarze  205:                        /*
                    206:                         * Remove the p->overstep width.
                    207:                         * Again, if p->overstep is negative,
                    208:                         * sign extension does the right thing.
                    209:                         */
1.130     kristaps  210:
1.174     schwarze  211:                        bp += (size_t)p->overstep;
1.129     kristaps  212:                        p->overstep = 0;
1.71      kristaps  213:                }
1.138     schwarze  214:
1.130     kristaps  215:                /* Write out the [remaining] word. */
1.188     kristaps  216:                for ( ; i < p->col; i++) {
1.140     kristaps  217:                        if (vend > bp && jhy > 0 && i > jhy)
                    218:                                break;
1.138     schwarze  219:                        if ('\t' == p->buf[i])
                    220:                                break;
1.136     schwarze  221:                        if (' ' == p->buf[i]) {
1.164     kristaps  222:                                j = i;
1.228     kristaps  223:                                while (i < p->col && ' ' == p->buf[i])
1.136     schwarze  224:                                        i++;
1.210     schwarze  225:                                dv = (i - j) * (*p->width)(p, ' ');
1.172     schwarze  226:                                vbl += dv;
                    227:                                vend += dv;
1.71      kristaps  228:                                break;
1.136     schwarze  229:                        }
                    230:                        if (ASCII_NBRSP == p->buf[i]) {
1.153     kristaps  231:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  232:                                continue;
                    233:                        }
1.216     schwarze  234:                        if (ASCII_BREAK == p->buf[i])
                    235:                                continue;
1.130     kristaps  236:
1.136     schwarze  237:                        /*
                    238:                         * Now we definitely know there will be
                    239:                         * printable characters to output,
                    240:                         * so write preceding white space now.
                    241:                         */
                    242:                        if (vbl) {
1.146     kristaps  243:                                (*p->advance)(p, vbl);
1.139     schwarze  244:                                p->viscol += vbl;
1.136     schwarze  245:                                vbl = 0;
1.200     schwarze  246:                        }
                    247:
                    248:                        (*p->letter)(p, p->buf[i]);
                    249:                        if (8 == p->buf[i])
                    250:                                p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222     schwarze  251:                        else
1.153     kristaps  252:                                p->viscol += (*p->width)(p, p->buf[i]);
1.136     schwarze  253:                }
                    254:                vis = vend;
1.71      kristaps  255:        }
1.168     schwarze  256:
                    257:        /*
                    258:         * If there was trailing white space, it was not printed;
                    259:         * so reset the cursor position accordingly.
                    260:         */
1.200     schwarze  261:        if (vis)
                    262:                vis -= vbl;
1.111     kristaps  263:
1.91      kristaps  264:        p->col = 0;
1.129     kristaps  265:        p->overstep = 0;
1.15      kristaps  266:
1.91      kristaps  267:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  268:                p->viscol = 0;
1.146     kristaps  269:                (*p->endline)(p);
1.15      kristaps  270:                return;
1.71      kristaps  271:        }
1.15      kristaps  272:
1.91      kristaps  273:        if (TERMP_HANG & p->flags) {
1.211     schwarze  274:                p->overstep = (int)(vis - maxvis +
1.222     schwarze  275:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  276:
                    277:                /*
1.92      kristaps  278:                 * If we have overstepped the margin, temporarily move
                    279:                 * it to the right and flag the rest of the line to be
                    280:                 * shorter.
1.212     schwarze  281:                 * If there is a request to keep the columns together,
                    282:                 * allow negative overstep when the column is not full.
1.91      kristaps  283:                 */
1.212     schwarze  284:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  285:                        p->overstep = 0;
1.200     schwarze  286:                return;
1.91      kristaps  287:
                    288:        } else if (TERMP_DANGLE & p->flags)
                    289:                return;
1.15      kristaps  290:
1.200     schwarze  291:        /* If the column was overrun, break the line. */
1.211     schwarze  292:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  293:                (*p->endline)(p);
1.200     schwarze  294:                p->viscol = 0;
1.91      kristaps  295:        }
1.15      kristaps  296: }
                    297:
1.222     schwarze  298: /*
1.71      kristaps  299:  * A newline only breaks an existing line; it won't assert vertical
                    300:  * space.  All data in the output buffer is flushed prior to the newline
                    301:  * assertion.
                    302:  */
                    303: void
                    304: term_newln(struct termp *p)
1.15      kristaps  305: {
                    306:
1.71      kristaps  307:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  308:        if (p->col || p->viscol)
                    309:                term_flushln(p);
1.16      kristaps  310: }
                    311:
1.71      kristaps  312: /*
                    313:  * Asserts a vertical space (a full, empty line-break between lines).
                    314:  * Note that if used twice, this will cause two blank spaces and so on.
                    315:  * All data in the output buffer is flushed prior to the newline
                    316:  * assertion.
                    317:  */
                    318: void
                    319: term_vspace(struct termp *p)
1.16      kristaps  320: {
                    321:
1.62      kristaps  322:        term_newln(p);
1.139     schwarze  323:        p->viscol = 0;
1.202     schwarze  324:        if (0 < p->skipvsp)
                    325:                p->skipvsp--;
                    326:        else
                    327:                (*p->endline)(p);
1.16      kristaps  328: }
                    329:
1.125     kristaps  330: void
                    331: term_fontlast(struct termp *p)
                    332: {
                    333:        enum termfont    f;
                    334:
                    335:        f = p->fontl;
                    336:        p->fontl = p->fontq[p->fonti];
                    337:        p->fontq[p->fonti] = f;
                    338: }
                    339:
                    340: void
                    341: term_fontrepl(struct termp *p, enum termfont f)
                    342: {
                    343:
                    344:        p->fontl = p->fontq[p->fonti];
                    345:        p->fontq[p->fonti] = f;
                    346: }
                    347:
                    348: void
                    349: term_fontpush(struct termp *p, enum termfont f)
                    350: {
                    351:
                    352:        assert(p->fonti + 1 < 10);
                    353:        p->fontl = p->fontq[p->fonti];
                    354:        p->fontq[++p->fonti] = f;
                    355: }
                    356:
                    357: const void *
                    358: term_fontq(struct termp *p)
                    359: {
                    360:
                    361:        return(&p->fontq[p->fonti]);
                    362: }
                    363:
                    364: enum termfont
                    365: term_fonttop(struct termp *p)
                    366: {
                    367:
                    368:        return(p->fontq[p->fonti]);
                    369: }
                    370:
                    371: void
                    372: term_fontpopq(struct termp *p, const void *key)
                    373: {
                    374:
1.206     schwarze  375:        while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125     kristaps  376:                p->fonti--;
                    377:        assert(p->fonti >= 0);
                    378: }
1.94      kristaps  379:
1.125     kristaps  380: void
                    381: term_fontpop(struct termp *p)
                    382: {
                    383:
                    384:        assert(p->fonti);
                    385:        p->fonti--;
1.17      kristaps  386: }
                    387:
1.71      kristaps  388: /*
                    389:  * Handle pwords, partial words, which may be either a single word or a
                    390:  * phrase that cannot be broken down (such as a literal string).  This
                    391:  * handles word styling.
                    392:  */
1.86      kristaps  393: void
                    394: term_word(struct termp *p, const char *word)
1.65      kristaps  395: {
1.214     schwarze  396:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  397:        const char      *seq, *cp;
                    398:        char             c;
1.194     kristaps  399:        int              sz, uc;
1.124     kristaps  400:        size_t           ssz;
1.184     kristaps  401:        enum mandoc_esc  esc;
1.100     kristaps  402:
1.133     kristaps  403:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  404:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  405:                        bufferc(p, ' ');
1.151     schwarze  406:                        if (TERMP_SENTENCE & p->flags)
                    407:                                bufferc(p, ' ');
                    408:                } else
                    409:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  410:        }
1.207     schwarze  411:        if (TERMP_PREKEEP & p->flags)
                    412:                p->flags |= TERMP_KEEP;
1.65      kristaps  413:
1.71      kristaps  414:        if ( ! (p->flags & TERMP_NONOSPACE))
                    415:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  416:        else
                    417:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  418:
1.213     schwarze  419:        p->flags &= ~TERMP_SENTENCE;
1.65      kristaps  420:
1.184     kristaps  421:        while ('\0' != *word) {
1.203     schwarze  422:                if ('\\' != *word) {
                    423:                        if (TERMP_SKIPCHAR & p->flags) {
                    424:                                p->flags &= ~TERMP_SKIPCHAR;
                    425:                                word++;
                    426:                                continue;
                    427:                        }
1.214     schwarze  428:                        if (TERMP_NBRWORD & p->flags) {
                    429:                                if (' ' == *word) {
                    430:                                        encode(p, nbrsp, 1);
                    431:                                        word++;
                    432:                                        continue;
                    433:                                }
                    434:                                ssz = strcspn(word, "\\ ");
                    435:                        } else
                    436:                                ssz = strcspn(word, "\\");
1.162     kristaps  437:                        encode(p, word, ssz);
1.203     schwarze  438:                        word += (int)ssz;
1.124     kristaps  439:                        continue;
1.203     schwarze  440:                }
1.124     kristaps  441:
1.184     kristaps  442:                word++;
                    443:                esc = mandoc_escape(&word, &seq, &sz);
                    444:                if (ESCAPE_ERROR == esc)
1.224     schwarze  445:                        continue;
1.124     kristaps  446:
1.184     kristaps  447:                switch (esc) {
1.222     schwarze  448:                case ESCAPE_UNICODE:
1.229     schwarze  449:                        uc = mchars_num2uc(seq + 1, sz - 1);
                    450:                        if (p->enc == TERMENC_ASCII) {
                    451:                                cp = ascii_uc2str(uc);
                    452:                                encode(p, cp, strlen(cp));
                    453:                        } else
                    454:                                encode1(p, uc);
1.192     kristaps  455:                        break;
1.222     schwarze  456:                case ESCAPE_NUMBERED:
1.196     kristaps  457:                        c = mchars_num2char(seq, sz);
                    458:                        if ('\0' != c)
1.191     kristaps  459:                                encode(p, &c, 1);
1.184     kristaps  460:                        break;
1.222     schwarze  461:                case ESCAPE_SPECIAL:
1.229     schwarze  462:                        if (p->enc == TERMENC_ASCII) {
                    463:                                cp = mchars_spec2str(p->symtab,
                    464:                                    seq, sz, &ssz);
                    465:                                if (cp == NULL)
                    466:                                        encode(p, "<?>", 3);
                    467:                                else
                    468:                                        encode(p, cp, ssz);
                    469:                        } else {
                    470:                                uc = mchars_spec2cp(p->symtab, seq, sz);
1.230   ! schwarze  471:                                if (uc > 0)
        !           472:                                        encode1(p, uc);
1.229     schwarze  473:                        }
1.124     kristaps  474:                        break;
1.222     schwarze  475:                case ESCAPE_FONTBOLD:
1.125     kristaps  476:                        term_fontrepl(p, TERMFONT_BOLD);
1.124     kristaps  477:                        break;
1.222     schwarze  478:                case ESCAPE_FONTITALIC:
1.125     kristaps  479:                        term_fontrepl(p, TERMFONT_UNDER);
1.124     kristaps  480:                        break;
1.222     schwarze  481:                case ESCAPE_FONTBI:
1.209     schwarze  482:                        term_fontrepl(p, TERMFONT_BI);
                    483:                        break;
1.222     schwarze  484:                case ESCAPE_FONT:
1.195     kristaps  485:                        /* FALLTHROUGH */
1.222     schwarze  486:                case ESCAPE_FONTROMAN:
1.125     kristaps  487:                        term_fontrepl(p, TERMFONT_NONE);
1.124     kristaps  488:                        break;
1.222     schwarze  489:                case ESCAPE_FONTPREV:
1.125     kristaps  490:                        term_fontlast(p);
1.124     kristaps  491:                        break;
1.222     schwarze  492:                case ESCAPE_NOSPACE:
1.203     schwarze  493:                        if (TERMP_SKIPCHAR & p->flags)
                    494:                                p->flags &= ~TERMP_SKIPCHAR;
                    495:                        else if ('\0' == *word)
1.184     kristaps  496:                                p->flags |= TERMP_NOSPACE;
                    497:                        break;
1.222     schwarze  498:                case ESCAPE_SKIPCHAR:
1.203     schwarze  499:                        p->flags |= TERMP_SKIPCHAR;
                    500:                        break;
1.124     kristaps  501:                default:
                    502:                        break;
                    503:                }
                    504:        }
1.214     schwarze  505:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  506: }
                    507:
1.71      kristaps  508: static void
1.210     schwarze  509: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  510: {
                    511:
1.125     kristaps  512:        if (0 == p->maxcols)
                    513:                p->maxcols = 1024;
                    514:        while (sz >= p->maxcols)
                    515:                p->maxcols <<= 2;
                    516:
1.223     schwarze  517:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  518: }
                    519:
1.79      kristaps  520: static void
1.125     kristaps  521: bufferc(struct termp *p, char c)
                    522: {
                    523:
                    524:        if (p->col + 1 >= p->maxcols)
                    525:                adjbuf(p, p->col + 1);
                    526:
1.188     kristaps  527:        p->buf[p->col++] = c;
1.125     kristaps  528: }
                    529:
1.194     kristaps  530: /*
                    531:  * See encode().
                    532:  * Do this for a single (probably unicode) value.
                    533:  * Does not check for non-decorated glyphs.
                    534:  */
                    535: static void
                    536: encode1(struct termp *p, int c)
                    537: {
                    538:        enum termfont     f;
                    539:
1.203     schwarze  540:        if (TERMP_SKIPCHAR & p->flags) {
                    541:                p->flags &= ~TERMP_SKIPCHAR;
                    542:                return;
                    543:        }
                    544:
1.209     schwarze  545:        if (p->col + 6 >= p->maxcols)
                    546:                adjbuf(p, p->col + 6);
1.194     kristaps  547:
                    548:        f = term_fonttop(p);
                    549:
1.209     schwarze  550:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  551:                p->buf[p->col++] = '_';
1.209     schwarze  552:                p->buf[p->col++] = 8;
                    553:        }
                    554:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    555:                if (ASCII_HYPH == c)
                    556:                        p->buf[p->col++] = '-';
                    557:                else
                    558:                        p->buf[p->col++] = c;
                    559:                p->buf[p->col++] = 8;
                    560:        }
1.194     kristaps  561:        p->buf[p->col++] = c;
                    562: }
                    563:
1.125     kristaps  564: static void
                    565: encode(struct termp *p, const char *word, size_t sz)
                    566: {
1.210     schwarze  567:        size_t            i;
1.188     kristaps  568:
1.203     schwarze  569:        if (TERMP_SKIPCHAR & p->flags) {
                    570:                p->flags &= ~TERMP_SKIPCHAR;
                    571:                return;
                    572:        }
                    573:
1.125     kristaps  574:        /*
                    575:         * Encode and buffer a string of characters.  If the current
                    576:         * font mode is unset, buffer directly, else encode then buffer
                    577:         * character by character.
                    578:         */
                    579:
1.209     schwarze  580:        if (TERMFONT_NONE == term_fonttop(p)) {
1.222     schwarze  581:                if (p->col + sz >= p->maxcols)
1.210     schwarze  582:                        adjbuf(p, p->col + sz);
                    583:                for (i = 0; i < sz; i++)
1.188     kristaps  584:                        p->buf[p->col++] = word[i];
1.125     kristaps  585:                return;
                    586:        }
                    587:
1.165     kristaps  588:        /* Pre-buffer, assuming worst-case. */
                    589:
1.210     schwarze  590:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    591:                adjbuf(p, p->col + 1 + (sz * 5));
1.165     kristaps  592:
1.210     schwarze  593:        for (i = 0; i < sz; i++) {
1.209     schwarze  594:                if (ASCII_HYPH == word[i] ||
                    595:                    isgraph((unsigned char)word[i]))
                    596:                        encode1(p, word[i]);
1.125     kristaps  597:                else
1.188     kristaps  598:                        p->buf[p->col++] = word[i];
1.79      kristaps  599:        }
1.219     schwarze  600: }
                    601:
                    602: void
                    603: term_setwidth(struct termp *p, const char *wstr)
                    604: {
                    605:        struct roffsu    su;
                    606:        size_t           width;
                    607:        int              iop;
                    608:
1.220     schwarze  609:        iop = 0;
                    610:        width = 0;
1.219     schwarze  611:        if (NULL != wstr) {
                    612:                switch (*wstr) {
1.222     schwarze  613:                case '+':
1.219     schwarze  614:                        iop = 1;
                    615:                        wstr++;
                    616:                        break;
1.222     schwarze  617:                case '-':
1.219     schwarze  618:                        iop = -1;
                    619:                        wstr++;
                    620:                        break;
                    621:                default:
                    622:                        break;
                    623:                }
1.220     schwarze  624:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    625:                        width = term_hspan(p, &su);
                    626:                else
1.219     schwarze  627:                        iop = 0;
                    628:        }
                    629:        (*p->setwidth)(p, iop, width);
1.79      kristaps  630: }
1.106     kristaps  631:
1.107     kristaps  632: size_t
1.149     kristaps  633: term_len(const struct termp *p, size_t sz)
                    634: {
                    635:
                    636:        return((*p->width)(p, ' ') * sz);
                    637: }
                    638:
1.203     schwarze  639: static size_t
                    640: cond_width(const struct termp *p, int c, int *skip)
                    641: {
                    642:
                    643:        if (*skip) {
                    644:                (*skip) = 0;
                    645:                return(0);
                    646:        } else
                    647:                return((*p->width)(p, c));
                    648: }
1.149     kristaps  649:
                    650: size_t
                    651: term_strlen(const struct termp *p, const char *cp)
                    652: {
1.184     kristaps  653:        size_t           sz, rsz, i;
1.203     schwarze  654:        int              ssz, skip, c;
1.171     kristaps  655:        const char      *seq, *rhs;
1.196     kristaps  656:        enum mandoc_esc  esc;
1.216     schwarze  657:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    658:                        ASCII_BREAK, '\0' };
1.171     kristaps  659:
1.184     kristaps  660:        /*
                    661:         * Account for escaped sequences within string length
                    662:         * calculations.  This follows the logic in term_word() as we
                    663:         * must calculate the width of produced strings.
                    664:         */
                    665:
                    666:        sz = 0;
1.203     schwarze  667:        skip = 0;
1.189     kristaps  668:        while ('\0' != *cp) {
                    669:                rsz = strcspn(cp, rej);
                    670:                for (i = 0; i < rsz; i++)
1.203     schwarze  671:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  672:
1.184     kristaps  673:                switch (*cp) {
1.222     schwarze  674:                case '\\':
1.189     kristaps  675:                        cp++;
1.196     kristaps  676:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    677:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  678:                                continue;
1.196     kristaps  679:
                    680:                        rhs = NULL;
                    681:
                    682:                        switch (esc) {
1.222     schwarze  683:                        case ESCAPE_UNICODE:
1.229     schwarze  684:                                c = mchars_num2uc(seq + 1, sz - 1);
                    685:                                if (p->enc == TERMENC_ASCII) {
                    686:                                        rhs = ascii_uc2str(c);
                    687:                                        rsz = strlen(rhs);
                    688:                                } else
                    689:                                        sz += cond_width(p, c, &skip);
1.194     kristaps  690:                                break;
1.222     schwarze  691:                        case ESCAPE_NUMBERED:
1.194     kristaps  692:                                c = mchars_num2char(seq, ssz);
1.190     kristaps  693:                                if ('\0' != c)
1.203     schwarze  694:                                        sz += cond_width(p, c, &skip);
1.171     kristaps  695:                                break;
1.222     schwarze  696:                        case ESCAPE_SPECIAL:
1.229     schwarze  697:                                if (p->enc == TERMENC_ASCII) {
                    698:                                        rhs = mchars_spec2str(p->symtab,
                    699:                                            seq, ssz, &rsz);
                    700:                                        if (rhs == NULL) {
                    701:                                                rhs = "<?>";
                    702:                                                rsz = 3;
                    703:                                        }
                    704:                                } else {
                    705:                                        c = mchars_spec2cp(p->symtab,
                    706:                                            seq, ssz);
1.230   ! schwarze  707:                                        if (c > 0)
        !           708:                                                sz += cond_width(p, c, &skip);
1.229     schwarze  709:                                }
1.171     kristaps  710:                                break;
1.222     schwarze  711:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  712:                                skip = 1;
                    713:                                break;
1.171     kristaps  714:                        default:
                    715:                                break;
                    716:                        }
1.149     kristaps  717:
1.184     kristaps  718:                        if (NULL == rhs)
                    719:                                break;
                    720:
1.203     schwarze  721:                        if (skip) {
                    722:                                skip = 0;
                    723:                                break;
                    724:                        }
                    725:
1.184     kristaps  726:                        for (i = 0; i < rsz; i++)
                    727:                                sz += (*p->width)(p, *rhs++);
                    728:                        break;
1.222     schwarze  729:                case ASCII_NBRSP:
1.203     schwarze  730:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  731:                        cp++;
1.184     kristaps  732:                        break;
1.222     schwarze  733:                case ASCII_HYPH:
1.203     schwarze  734:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  735:                        cp++;
1.216     schwarze  736:                        /* FALLTHROUGH */
1.222     schwarze  737:                case ASCII_BREAK:
1.184     kristaps  738:                        break;
                    739:                default:
                    740:                        break;
                    741:                }
1.189     kristaps  742:        }
1.149     kristaps  743:
                    744:        return(sz);
                    745: }
                    746:
                    747: size_t
                    748: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  749: {
                    750:        double           r;
                    751:
1.107     kristaps  752:        switch (su->unit) {
1.222     schwarze  753:        case SCALE_CM:
1.225     schwarze  754:                r = su->scale * 2.0;
1.106     kristaps  755:                break;
1.222     schwarze  756:        case SCALE_IN:
1.225     schwarze  757:                r = su->scale * 6.0;
1.106     kristaps  758:                break;
1.222     schwarze  759:        case SCALE_PC:
1.107     kristaps  760:                r = su->scale;
1.106     kristaps  761:                break;
1.222     schwarze  762:        case SCALE_PT:
1.225     schwarze  763:                r = su->scale / 8.0;
1.106     kristaps  764:                break;
1.222     schwarze  765:        case SCALE_MM:
1.225     schwarze  766:                r = su->scale / 1000.0;
1.106     kristaps  767:                break;
1.222     schwarze  768:        case SCALE_VS:
1.107     kristaps  769:                r = su->scale;
1.106     kristaps  770:                break;
                    771:        default:
1.225     schwarze  772:                r = su->scale - 1.0;
1.106     kristaps  773:                break;
                    774:        }
                    775:
                    776:        if (r < 0.0)
                    777:                r = 0.0;
1.226     schwarze  778:        return((size_t)(r + 0.0005));
1.106     kristaps  779: }
                    780:
1.107     kristaps  781: size_t
1.149     kristaps  782: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  783: {
1.156     kristaps  784:        double           v;
1.108     kristaps  785:
1.225     schwarze  786:        v = (*p->hspan)(p, su);
1.156     kristaps  787:        if (v < 0.0)
                    788:                v = 0.0;
1.226     schwarze  789:        return((size_t)(v + 0.0005));
1.106     kristaps  790: }

CVSweb