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

Annotation of mandoc/term.c, Revision 1.200

1.200   ! schwarze    1: /*     $Id: term.c,v 1.199 2011/09/18 21:18:19 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.177     schwarze    4:  * Copyright (c) 2010, 2011 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.81      kristaps  187:                        if (TERMP_NOBREAK & p->flags) {
1.139     schwarze  188:                                p->viscol = p->rmargin;
1.146     kristaps  189:                                (*p->advance)(p, p->rmargin);
1.136     schwarze  190:                                vend += p->rmargin - p->offset;
1.81      kristaps  191:                        } else {
1.139     schwarze  192:                                p->viscol = 0;
1.136     schwarze  193:                                vbl = p->offset;
1.81      kristaps  194:                        }
1.130     kristaps  195:
1.129     kristaps  196:                        /* Remove the p->overstep width. */
1.130     kristaps  197:
1.174     schwarze  198:                        bp += (size_t)p->overstep;
1.129     kristaps  199:                        p->overstep = 0;
1.71      kristaps  200:                }
1.138     schwarze  201:
1.130     kristaps  202:                /* Write out the [remaining] word. */
1.188     kristaps  203:                for ( ; i < p->col; i++) {
1.140     kristaps  204:                        if (vend > bp && jhy > 0 && i > jhy)
                    205:                                break;
1.138     schwarze  206:                        if ('\t' == p->buf[i])
                    207:                                break;
1.136     schwarze  208:                        if (' ' == p->buf[i]) {
1.164     kristaps  209:                                j = i;
                    210:                                while (' ' == p->buf[i])
1.136     schwarze  211:                                        i++;
1.174     schwarze  212:                                dv = (size_t)(i - j) * (*p->width)(p, ' ');
1.172     schwarze  213:                                vbl += dv;
                    214:                                vend += dv;
1.71      kristaps  215:                                break;
1.136     schwarze  216:                        }
                    217:                        if (ASCII_NBRSP == p->buf[i]) {
1.153     kristaps  218:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  219:                                continue;
                    220:                        }
1.130     kristaps  221:
1.136     schwarze  222:                        /*
                    223:                         * Now we definitely know there will be
                    224:                         * printable characters to output,
                    225:                         * so write preceding white space now.
                    226:                         */
                    227:                        if (vbl) {
1.146     kristaps  228:                                (*p->advance)(p, vbl);
1.139     schwarze  229:                                p->viscol += vbl;
1.136     schwarze  230:                                vbl = 0;
                    231:                        }
1.140     kristaps  232:
1.153     kristaps  233:                        if (ASCII_HYPH == p->buf[i]) {
1.146     kristaps  234:                                (*p->letter)(p, '-');
1.153     kristaps  235:                                p->viscol += (*p->width)(p, '-');
1.200   ! schwarze  236:                                continue;
        !           237:                        }
        !           238:
        !           239:                        (*p->letter)(p, p->buf[i]);
        !           240:                        if (8 == p->buf[i])
        !           241:                                p->viscol -= (*p->width)(p, p->buf[i-1]);
        !           242:                        else
1.153     kristaps  243:                                p->viscol += (*p->width)(p, p->buf[i]);
1.136     schwarze  244:                }
                    245:                vis = vend;
1.71      kristaps  246:        }
1.168     schwarze  247:
                    248:        /*
                    249:         * If there was trailing white space, it was not printed;
                    250:         * so reset the cursor position accordingly.
                    251:         */
1.200   ! schwarze  252:        if (vis)
        !           253:                vis -= vbl;
1.111     kristaps  254:
1.91      kristaps  255:        p->col = 0;
1.129     kristaps  256:        p->overstep = 0;
1.15      kristaps  257:
1.91      kristaps  258:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  259:                p->viscol = 0;
1.146     kristaps  260:                (*p->endline)(p);
1.15      kristaps  261:                return;
1.71      kristaps  262:        }
1.15      kristaps  263:
1.91      kristaps  264:        if (TERMP_HANG & p->flags) {
                    265:                /* We need one blank after the tag. */
1.174     schwarze  266:                p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
1.91      kristaps  267:
                    268:                /*
                    269:                 * Behave exactly the same way as groff:
1.92      kristaps  270:                 * If we have overstepped the margin, temporarily move
                    271:                 * it to the right and flag the rest of the line to be
                    272:                 * shorter.
1.91      kristaps  273:                 * If we landed right at the margin, be happy.
1.92      kristaps  274:                 * If we are one step before the margin, temporarily
                    275:                 * move it one step LEFT and flag the rest of the line
                    276:                 * to be longer.
1.91      kristaps  277:                 */
1.200   ! schwarze  278:                if (p->overstep < -1)
1.129     kristaps  279:                        p->overstep = 0;
1.200   ! schwarze  280:                return;
1.91      kristaps  281:
                    282:        } else if (TERMP_DANGLE & p->flags)
                    283:                return;
1.15      kristaps  284:
1.200   ! schwarze  285:        /* If the column was overrun, break the line. */
        !           286:        if (maxvis <= vis +
1.174     schwarze  287:            ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
1.146     kristaps  288:                (*p->endline)(p);
1.200   ! schwarze  289:                p->viscol = 0;
1.91      kristaps  290:        }
1.15      kristaps  291: }
                    292:
                    293:
1.71      kristaps  294: /*
                    295:  * A newline only breaks an existing line; it won't assert vertical
                    296:  * space.  All data in the output buffer is flushed prior to the newline
                    297:  * assertion.
                    298:  */
                    299: void
                    300: term_newln(struct termp *p)
1.15      kristaps  301: {
                    302:
1.71      kristaps  303:        p->flags |= TERMP_NOSPACE;
1.200   ! schwarze  304:        if (p->col || p->viscol)
        !           305:                term_flushln(p);
1.16      kristaps  306: }
                    307:
                    308:
1.71      kristaps  309: /*
                    310:  * Asserts a vertical space (a full, empty line-break between lines).
                    311:  * Note that if used twice, this will cause two blank spaces and so on.
                    312:  * All data in the output buffer is flushed prior to the newline
                    313:  * assertion.
                    314:  */
                    315: void
                    316: term_vspace(struct termp *p)
1.16      kristaps  317: {
                    318:
1.62      kristaps  319:        term_newln(p);
1.139     schwarze  320:        p->viscol = 0;
1.146     kristaps  321:        (*p->endline)(p);
1.16      kristaps  322: }
                    323:
1.125     kristaps  324: void
                    325: term_fontlast(struct termp *p)
                    326: {
                    327:        enum termfont    f;
                    328:
                    329:        f = p->fontl;
                    330:        p->fontl = p->fontq[p->fonti];
                    331:        p->fontq[p->fonti] = f;
                    332: }
                    333:
                    334:
                    335: void
                    336: term_fontrepl(struct termp *p, enum termfont f)
                    337: {
                    338:
                    339:        p->fontl = p->fontq[p->fonti];
                    340:        p->fontq[p->fonti] = f;
                    341: }
                    342:
                    343:
                    344: void
                    345: term_fontpush(struct termp *p, enum termfont f)
                    346: {
                    347:
                    348:        assert(p->fonti + 1 < 10);
                    349:        p->fontl = p->fontq[p->fonti];
                    350:        p->fontq[++p->fonti] = f;
                    351: }
                    352:
                    353:
                    354: const void *
                    355: term_fontq(struct termp *p)
                    356: {
                    357:
                    358:        return(&p->fontq[p->fonti]);
                    359: }
                    360:
                    361:
                    362: enum termfont
                    363: term_fonttop(struct termp *p)
                    364: {
                    365:
                    366:        return(p->fontq[p->fonti]);
                    367: }
                    368:
                    369:
                    370: void
                    371: term_fontpopq(struct termp *p, const void *key)
                    372: {
                    373:
                    374:        while (p->fonti >= 0 && key != &p->fontq[p->fonti])
                    375:                p->fonti--;
                    376:        assert(p->fonti >= 0);
                    377: }
1.94      kristaps  378:
1.125     kristaps  379:
                    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.191     kristaps  396:        const char      *seq, *cp;
                    397:        char             c;
1.194     kristaps  398:        int              sz, uc;
1.124     kristaps  399:        size_t           ssz;
1.184     kristaps  400:        enum mandoc_esc  esc;
1.100     kristaps  401:
1.133     kristaps  402:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  403:                if ( ! (TERMP_KEEP & p->flags)) {
                    404:                        if (TERMP_PREKEEP & p->flags)
                    405:                                p->flags |= TERMP_KEEP;
1.133     kristaps  406:                        bufferc(p, ' ');
1.151     schwarze  407:                        if (TERMP_SENTENCE & p->flags)
                    408:                                bufferc(p, ' ');
                    409:                } else
                    410:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  411:        }
1.65      kristaps  412:
1.71      kristaps  413:        if ( ! (p->flags & TERMP_NONOSPACE))
                    414:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  415:        else
                    416:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  417:
1.173     schwarze  418:        p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM);
1.65      kristaps  419:
1.184     kristaps  420:        while ('\0' != *word) {
1.162     kristaps  421:                if ((ssz = strcspn(word, "\\")) > 0)
                    422:                        encode(p, word, ssz);
1.124     kristaps  423:
1.178     kristaps  424:                word += (int)ssz;
1.162     kristaps  425:                if ('\\' != *word)
1.124     kristaps  426:                        continue;
                    427:
1.184     kristaps  428:                word++;
                    429:                esc = mandoc_escape(&word, &seq, &sz);
                    430:                if (ESCAPE_ERROR == esc)
                    431:                        break;
1.124     kristaps  432:
1.196     kristaps  433:                if (TERMENC_ASCII != p->enc)
                    434:                        switch (esc) {
                    435:                        case (ESCAPE_UNICODE):
                    436:                                uc = mchars_num2uc(seq + 1, sz - 1);
                    437:                                if ('\0' == uc)
                    438:                                        break;
                    439:                                encode1(p, uc);
                    440:                                continue;
                    441:                        case (ESCAPE_SPECIAL):
                    442:                                uc = mchars_spec2cp(p->symtab, seq, sz);
                    443:                                if (uc <= 0)
                    444:                                        break;
                    445:                                encode1(p, uc);
                    446:                                continue;
                    447:                        default:
                    448:                                break;
                    449:                        }
                    450:
1.184     kristaps  451:                switch (esc) {
1.192     kristaps  452:                case (ESCAPE_UNICODE):
1.196     kristaps  453:                        encode1(p, '?');
1.192     kristaps  454:                        break;
1.184     kristaps  455:                case (ESCAPE_NUMBERED):
1.196     kristaps  456:                        c = mchars_num2char(seq, sz);
                    457:                        if ('\0' != c)
1.191     kristaps  458:                                encode(p, &c, 1);
1.184     kristaps  459:                        break;
                    460:                case (ESCAPE_SPECIAL):
1.191     kristaps  461:                        cp = mchars_spec2str(p->symtab, seq, sz, &ssz);
                    462:                        if (NULL != cp)
                    463:                                encode(p, cp, ssz);
                    464:                        else if (1 == ssz)
                    465:                                encode(p, seq, sz);
1.124     kristaps  466:                        break;
1.184     kristaps  467:                case (ESCAPE_FONTBOLD):
1.125     kristaps  468:                        term_fontrepl(p, TERMFONT_BOLD);
1.124     kristaps  469:                        break;
1.184     kristaps  470:                case (ESCAPE_FONTITALIC):
1.125     kristaps  471:                        term_fontrepl(p, TERMFONT_UNDER);
1.124     kristaps  472:                        break;
1.195     kristaps  473:                case (ESCAPE_FONT):
                    474:                        /* FALLTHROUGH */
1.184     kristaps  475:                case (ESCAPE_FONTROMAN):
1.125     kristaps  476:                        term_fontrepl(p, TERMFONT_NONE);
1.124     kristaps  477:                        break;
1.184     kristaps  478:                case (ESCAPE_FONTPREV):
1.125     kristaps  479:                        term_fontlast(p);
1.124     kristaps  480:                        break;
1.184     kristaps  481:                case (ESCAPE_NOSPACE):
                    482:                        if ('\0' == *word)
                    483:                                p->flags |= TERMP_NOSPACE;
                    484:                        break;
1.124     kristaps  485:                default:
                    486:                        break;
                    487:                }
                    488:        }
1.65      kristaps  489: }
                    490:
1.71      kristaps  491: static void
1.188     kristaps  492: adjbuf(struct termp *p, int sz)
1.51      kristaps  493: {
                    494:
1.125     kristaps  495:        if (0 == p->maxcols)
                    496:                p->maxcols = 1024;
                    497:        while (sz >= p->maxcols)
                    498:                p->maxcols <<= 2;
                    499:
1.188     kristaps  500:        p->buf = mandoc_realloc
                    501:                (p->buf, sizeof(int) * (size_t)p->maxcols);
1.51      kristaps  502: }
                    503:
1.79      kristaps  504: static void
1.125     kristaps  505: bufferc(struct termp *p, char c)
                    506: {
                    507:
                    508:        if (p->col + 1 >= p->maxcols)
                    509:                adjbuf(p, p->col + 1);
                    510:
1.188     kristaps  511:        p->buf[p->col++] = c;
1.125     kristaps  512: }
                    513:
1.194     kristaps  514: /*
                    515:  * See encode().
                    516:  * Do this for a single (probably unicode) value.
                    517:  * Does not check for non-decorated glyphs.
                    518:  */
                    519: static void
                    520: encode1(struct termp *p, int c)
                    521: {
                    522:        enum termfont     f;
                    523:
                    524:        if (p->col + 4 >= p->maxcols)
                    525:                adjbuf(p, p->col + 4);
                    526:
                    527:        f = term_fonttop(p);
                    528:
                    529:        if (TERMFONT_NONE == f) {
                    530:                p->buf[p->col++] = c;
                    531:                return;
                    532:        } else if (TERMFONT_UNDER == f) {
                    533:                p->buf[p->col++] = '_';
                    534:        } else
                    535:                p->buf[p->col++] = c;
                    536:
                    537:        p->buf[p->col++] = 8;
                    538:        p->buf[p->col++] = c;
                    539: }
                    540:
1.125     kristaps  541: static void
                    542: encode(struct termp *p, const char *word, size_t sz)
                    543: {
                    544:        enum termfont     f;
1.188     kristaps  545:        int               i, len;
                    546:
                    547:        /* LINTED */
                    548:        len = sz;
1.125     kristaps  549:
                    550:        /*
                    551:         * Encode and buffer a string of characters.  If the current
                    552:         * font mode is unset, buffer directly, else encode then buffer
                    553:         * character by character.
                    554:         */
                    555:
1.147     kristaps  556:        if (TERMFONT_NONE == (f = term_fonttop(p))) {
1.188     kristaps  557:                if (p->col + len >= p->maxcols)
                    558:                        adjbuf(p, p->col + len);
                    559:                for (i = 0; i < len; i++)
                    560:                        p->buf[p->col++] = word[i];
1.125     kristaps  561:                return;
                    562:        }
                    563:
1.165     kristaps  564:        /* Pre-buffer, assuming worst-case. */
                    565:
1.188     kristaps  566:        if (p->col + 1 + (len * 3) >= p->maxcols)
                    567:                adjbuf(p, p->col + 1 + (len * 3));
1.165     kristaps  568:
1.188     kristaps  569:        for (i = 0; i < len; i++) {
1.199     schwarze  570:                if (ASCII_HYPH != word[i] &&
                    571:                    ! isgraph((unsigned char)word[i])) {
1.188     kristaps  572:                        p->buf[p->col++] = word[i];
1.125     kristaps  573:                        continue;
1.79      kristaps  574:                }
1.125     kristaps  575:
                    576:                if (TERMFONT_UNDER == f)
1.188     kristaps  577:                        p->buf[p->col++] = '_';
1.199     schwarze  578:                else if (ASCII_HYPH == word[i])
                    579:                        p->buf[p->col++] = '-';
1.125     kristaps  580:                else
1.188     kristaps  581:                        p->buf[p->col++] = word[i];
1.125     kristaps  582:
1.188     kristaps  583:                p->buf[p->col++] = 8;
                    584:                p->buf[p->col++] = word[i];
1.79      kristaps  585:        }
                    586: }
1.106     kristaps  587:
1.107     kristaps  588: size_t
1.149     kristaps  589: term_len(const struct termp *p, size_t sz)
                    590: {
                    591:
                    592:        return((*p->width)(p, ' ') * sz);
                    593: }
                    594:
                    595:
                    596: size_t
                    597: term_strlen(const struct termp *p, const char *cp)
                    598: {
1.184     kristaps  599:        size_t           sz, rsz, i;
1.190     kristaps  600:        int              ssz, c;
1.171     kristaps  601:        const char      *seq, *rhs;
1.196     kristaps  602:        enum mandoc_esc  esc;
1.189     kristaps  603:        static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' };
1.171     kristaps  604:
1.184     kristaps  605:        /*
                    606:         * Account for escaped sequences within string length
                    607:         * calculations.  This follows the logic in term_word() as we
                    608:         * must calculate the width of produced strings.
                    609:         */
                    610:
                    611:        sz = 0;
1.189     kristaps  612:        while ('\0' != *cp) {
                    613:                rsz = strcspn(cp, rej);
                    614:                for (i = 0; i < rsz; i++)
                    615:                        sz += (*p->width)(p, *cp++);
                    616:
1.192     kristaps  617:                c = 0;
1.184     kristaps  618:                switch (*cp) {
                    619:                case ('\\'):
1.189     kristaps  620:                        cp++;
1.196     kristaps  621:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    622:                        if (ESCAPE_ERROR == esc)
1.184     kristaps  623:                                return(sz);
1.196     kristaps  624:
                    625:                        if (TERMENC_ASCII != p->enc)
                    626:                                switch (esc) {
                    627:                                case (ESCAPE_UNICODE):
                    628:                                        c = mchars_num2uc
                    629:                                                (seq + 1, ssz - 1);
                    630:                                        if ('\0' == c)
                    631:                                                break;
                    632:                                        sz += (*p->width)(p, c);
                    633:                                        continue;
                    634:                                case (ESCAPE_SPECIAL):
                    635:                                        c = mchars_spec2cp
                    636:                                                (p->symtab, seq, ssz);
                    637:                                        if (c <= 0)
                    638:                                                break;
                    639:                                        sz += (*p->width)(p, c);
                    640:                                        continue;
                    641:                                default:
1.194     kristaps  642:                                        break;
                    643:                                }
1.196     kristaps  644:
                    645:                        rhs = NULL;
                    646:
                    647:                        switch (esc) {
                    648:                        case (ESCAPE_UNICODE):
                    649:                                sz += (*p->width)(p, '?');
1.194     kristaps  650:                                break;
1.190     kristaps  651:                        case (ESCAPE_NUMBERED):
1.194     kristaps  652:                                c = mchars_num2char(seq, ssz);
1.190     kristaps  653:                                if ('\0' != c)
                    654:                                        sz += (*p->width)(p, c);
1.171     kristaps  655:                                break;
1.184     kristaps  656:                        case (ESCAPE_SPECIAL):
1.185     kristaps  657:                                rhs = mchars_spec2str
1.171     kristaps  658:                                        (p->symtab, seq, ssz, &rsz);
                    659:
1.184     kristaps  660:                                if (ssz != 1 || rhs)
1.171     kristaps  661:                                        break;
                    662:
                    663:                                rhs = seq;
                    664:                                rsz = ssz;
                    665:                                break;
                    666:                        default:
                    667:                                break;
                    668:                        }
1.149     kristaps  669:
1.184     kristaps  670:                        if (NULL == rhs)
                    671:                                break;
                    672:
                    673:                        for (i = 0; i < rsz; i++)
                    674:                                sz += (*p->width)(p, *rhs++);
                    675:                        break;
                    676:                case (ASCII_NBRSP):
1.176     kristaps  677:                        sz += (*p->width)(p, ' ');
                    678:                        cp++;
1.184     kristaps  679:                        break;
                    680:                case (ASCII_HYPH):
1.176     kristaps  681:                        sz += (*p->width)(p, '-');
                    682:                        cp++;
1.184     kristaps  683:                        break;
                    684:                default:
                    685:                        break;
                    686:                }
1.189     kristaps  687:        }
1.149     kristaps  688:
                    689:        return(sz);
                    690: }
                    691:
1.157     kristaps  692: /* ARGSUSED */
1.149     kristaps  693: size_t
                    694: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  695: {
                    696:        double           r;
                    697:
1.107     kristaps  698:        switch (su->unit) {
1.106     kristaps  699:        case (SCALE_CM):
1.107     kristaps  700:                r = su->scale * 2;
1.106     kristaps  701:                break;
                    702:        case (SCALE_IN):
1.107     kristaps  703:                r = su->scale * 6;
1.106     kristaps  704:                break;
                    705:        case (SCALE_PC):
1.107     kristaps  706:                r = su->scale;
1.106     kristaps  707:                break;
                    708:        case (SCALE_PT):
1.107     kristaps  709:                r = su->scale / 8;
1.106     kristaps  710:                break;
                    711:        case (SCALE_MM):
1.107     kristaps  712:                r = su->scale / 1000;
1.106     kristaps  713:                break;
                    714:        case (SCALE_VS):
1.107     kristaps  715:                r = su->scale;
1.106     kristaps  716:                break;
                    717:        default:
1.107     kristaps  718:                r = su->scale - 1;
1.106     kristaps  719:                break;
                    720:        }
                    721:
                    722:        if (r < 0.0)
                    723:                r = 0.0;
1.107     kristaps  724:        return(/* LINTED */(size_t)
1.106     kristaps  725:                        r);
                    726: }
                    727:
1.107     kristaps  728: size_t
1.149     kristaps  729: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  730: {
1.156     kristaps  731:        double           v;
1.108     kristaps  732:
1.156     kristaps  733:        v = ((*p->hspan)(p, su));
                    734:        if (v < 0.0)
                    735:                v = 0.0;
                    736:        return((size_t) /* LINTED */
                    737:                        v);
1.106     kristaps  738: }

CVSweb