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

Annotation of mandoc/term.c, Revision 1.152

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

CVSweb