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

Annotation of mandoc/term.c, Revision 1.191

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

CVSweb