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

Annotation of mandoc/term.c, Revision 1.216

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

CVSweb