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

Annotation of mandoc/term.c, Revision 1.203

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

CVSweb