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

Annotation of mandoc/term.c, Revision 1.177

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

CVSweb