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

Annotation of mandoc/term.c, Revision 1.180

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

CVSweb