[BACK]Return to term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/term.c, Revision 1.57

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