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

Annotation of mandoc/term.c, Revision 1.188

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

CVSweb