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

Annotation of mandoc/term.c, Revision 1.202

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

CVSweb