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

Annotation of mandoc/term.c, Revision 1.201

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

CVSweb