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

Annotation of mandoc/term.c, Revision 1.193

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

CVSweb