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

Annotation of mandoc/term.c, Revision 1.151

1.151   ! schwarze    1: /*     $Id: term.c,v 1.150 2010/06/26 15:36:37 kristaps 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 */
                    136:        int              j;     /* temporary loop index */
1.140     kristaps  137:        int              jhy;   /* last hyphen before line overflow */
1.114     kristaps  138:        size_t           maxvis, mmax;
1.53      kristaps  139:
1.71      kristaps  140:        /*
                    141:         * First, establish the maximum columns of "visible" content.
                    142:         * This is usually the difference between the right-margin and
                    143:         * an indentation, but can be, for tagged lists or columns, a
1.115     kristaps  144:         * small set of values.
1.71      kristaps  145:         */
1.53      kristaps  146:
1.71      kristaps  147:        assert(p->offset < p->rmargin);
1.92      kristaps  148:
1.129     kristaps  149:        maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
1.119     kristaps  150:                /* LINTED */
1.129     kristaps  151:                0 : p->rmargin - p->offset - p->overstep;
                    152:        mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
1.119     kristaps  153:                /* LINTED */
1.129     kristaps  154:                0 : p->maxrmargin - p->offset - p->overstep;
1.92      kristaps  155:
1.71      kristaps  156:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  157:
1.136     schwarze  158:        /*
                    159:         * Indent the first line of a paragraph.
                    160:         */
                    161:        vbl = p->flags & TERMP_NOLPAD ? 0 : p->offset;
                    162:
1.115     kristaps  163:        /*
                    164:         * FIXME: if bp is zero, we still output the first word before
                    165:         * breaking the line.
                    166:         */
                    167:
1.136     schwarze  168:        vis = vend = i = 0;
                    169:        while (i < (int)p->col) {
1.71      kristaps  170:
                    171:                /*
1.138     schwarze  172:                 * Handle literal tab characters.
                    173:                 */
                    174:                for (j = i; j < (int)p->col; j++) {
                    175:                        if ('\t' != p->buf[j])
                    176:                                break;
                    177:                        vend = (vis/p->tabwidth+1)*p->tabwidth;
                    178:                        vbl += vend - vis;
                    179:                        vis = vend;
                    180:                }
                    181:
                    182:                /*
1.71      kristaps  183:                 * Count up visible word characters.  Control sequences
                    184:                 * (starting with the CSI) aren't counted.  A space
                    185:                 * generates a non-printing word, which is valid (the
                    186:                 * space is printed according to regular spacing rules).
                    187:                 */
                    188:
                    189:                /* LINTED */
1.140     kristaps  190:                for (jhy = 0; j < (int)p->col; j++) {
1.138     schwarze  191:                        if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
1.71      kristaps  192:                                break;
1.140     kristaps  193:                        if (8 != p->buf[j]) {
                    194:                                if (vend > vis && vend < bp &&
                    195:                                    ASCII_HYPH == p->buf[j])
                    196:                                        jhy = j;
                    197:                                vend++;
                    198:                        } else
1.136     schwarze  199:                                vend--;
1.71      kristaps  200:                }
1.53      kristaps  201:
1.71      kristaps  202:                /*
1.81      kristaps  203:                 * Find out whether we would exceed the right margin.
1.136     schwarze  204:                 * If so, break to the next line.
1.81      kristaps  205:                 */
1.140     kristaps  206:                if (vend > bp && 0 == jhy && vis > 0) {
1.136     schwarze  207:                        vend -= vis;
1.146     kristaps  208:                        (*p->endline)(p);
1.81      kristaps  209:                        if (TERMP_NOBREAK & p->flags) {
1.139     schwarze  210:                                p->viscol = p->rmargin;
1.146     kristaps  211:                                (*p->advance)(p, p->rmargin);
1.136     schwarze  212:                                vend += p->rmargin - p->offset;
1.81      kristaps  213:                        } else {
1.139     schwarze  214:                                p->viscol = 0;
1.136     schwarze  215:                                vbl = p->offset;
1.81      kristaps  216:                        }
1.130     kristaps  217:
1.129     kristaps  218:                        /* Remove the p->overstep width. */
1.130     kristaps  219:
1.112     kristaps  220:                        bp += (int)/* LINTED */
1.129     kristaps  221:                                p->overstep;
                    222:                        p->overstep = 0;
1.71      kristaps  223:                }
1.53      kristaps  224:
1.138     schwarze  225:                /*
                    226:                 * Skip leading tabs, they were handled above.
                    227:                 */
                    228:                while (i < (int)p->col && '\t' == p->buf[i])
                    229:                        i++;
                    230:
1.130     kristaps  231:                /* Write out the [remaining] word. */
1.136     schwarze  232:                for ( ; i < (int)p->col; i++) {
1.140     kristaps  233:                        if (vend > bp && jhy > 0 && i > jhy)
                    234:                                break;
1.138     schwarze  235:                        if ('\t' == p->buf[i])
                    236:                                break;
1.136     schwarze  237:                        if (' ' == p->buf[i]) {
                    238:                                while (' ' == p->buf[i]) {
                    239:                                        vbl++;
                    240:                                        i++;
                    241:                                }
1.71      kristaps  242:                                break;
1.136     schwarze  243:                        }
                    244:                        if (ASCII_NBRSP == p->buf[i]) {
                    245:                                vbl++;
                    246:                                continue;
                    247:                        }
1.130     kristaps  248:
1.136     schwarze  249:                        /*
                    250:                         * Now we definitely know there will be
                    251:                         * printable characters to output,
                    252:                         * so write preceding white space now.
                    253:                         */
                    254:                        if (vbl) {
1.146     kristaps  255:                                (*p->advance)(p, vbl);
1.139     schwarze  256:                                p->viscol += vbl;
1.136     schwarze  257:                                vbl = 0;
                    258:                        }
1.140     kristaps  259:
                    260:                        if (ASCII_HYPH == p->buf[i])
1.146     kristaps  261:                                (*p->letter)(p, '-');
1.140     kristaps  262:                        else
1.146     kristaps  263:                                (*p->letter)(p, p->buf[i]);
1.140     kristaps  264:
1.139     schwarze  265:                        p->viscol += 1;
1.136     schwarze  266:                }
                    267:                vend += vbl;
                    268:                vis = vend;
1.71      kristaps  269:        }
1.111     kristaps  270:
1.91      kristaps  271:        p->col = 0;
1.129     kristaps  272:        p->overstep = 0;
1.15      kristaps  273:
1.91      kristaps  274:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  275:                p->viscol = 0;
1.146     kristaps  276:                (*p->endline)(p);
1.15      kristaps  277:                return;
1.71      kristaps  278:        }
1.15      kristaps  279:
1.91      kristaps  280:        if (TERMP_HANG & p->flags) {
                    281:                /* We need one blank after the tag. */
1.129     kristaps  282:                p->overstep = /* LINTED */
1.92      kristaps  283:                        vis - maxvis + 1;
1.91      kristaps  284:
                    285:                /*
                    286:                 * Behave exactly the same way as groff:
1.92      kristaps  287:                 * If we have overstepped the margin, temporarily move
                    288:                 * it to the right and flag the rest of the line to be
                    289:                 * shorter.
1.91      kristaps  290:                 * If we landed right at the margin, be happy.
1.92      kristaps  291:                 * If we are one step before the margin, temporarily
                    292:                 * move it one step LEFT and flag the rest of the line
                    293:                 * to be longer.
1.91      kristaps  294:                 */
1.129     kristaps  295:                if (p->overstep >= -1) {
                    296:                        assert((int)maxvis + p->overstep >= 0);
1.92      kristaps  297:                        /* LINTED */
1.129     kristaps  298:                        maxvis += p->overstep;
1.92      kristaps  299:                } else
1.129     kristaps  300:                        p->overstep = 0;
1.91      kristaps  301:
                    302:        } else if (TERMP_DANGLE & p->flags)
                    303:                return;
1.15      kristaps  304:
1.92      kristaps  305:        /* Right-pad. */
                    306:        if (maxvis > vis + /* LINTED */
1.139     schwarze  307:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
                    308:                p->viscol += maxvis - vis;
1.146     kristaps  309:                (*p->advance)(p, maxvis - vis);
1.142     kristaps  310:                vis += (maxvis - vis);
1.139     schwarze  311:        } else {        /* ...or newline break. */
1.146     kristaps  312:                (*p->endline)(p);
1.139     schwarze  313:                p->viscol = p->rmargin;
1.146     kristaps  314:                (*p->advance)(p, p->rmargin);
1.91      kristaps  315:        }
1.15      kristaps  316: }
                    317:
                    318:
1.71      kristaps  319: /*
                    320:  * A newline only breaks an existing line; it won't assert vertical
                    321:  * space.  All data in the output buffer is flushed prior to the newline
                    322:  * assertion.
                    323:  */
                    324: void
                    325: term_newln(struct termp *p)
1.15      kristaps  326: {
                    327:
1.71      kristaps  328:        p->flags |= TERMP_NOSPACE;
1.139     schwarze  329:        if (0 == p->col && 0 == p->viscol) {
1.71      kristaps  330:                p->flags &= ~TERMP_NOLPAD;
1.15      kristaps  331:                return;
1.16      kristaps  332:        }
1.71      kristaps  333:        term_flushln(p);
                    334:        p->flags &= ~TERMP_NOLPAD;
1.16      kristaps  335: }
                    336:
                    337:
1.71      kristaps  338: /*
                    339:  * Asserts a vertical space (a full, empty line-break between lines).
                    340:  * Note that if used twice, this will cause two blank spaces and so on.
                    341:  * All data in the output buffer is flushed prior to the newline
                    342:  * assertion.
                    343:  */
                    344: void
                    345: term_vspace(struct termp *p)
1.16      kristaps  346: {
                    347:
1.62      kristaps  348:        term_newln(p);
1.139     schwarze  349:        p->viscol = 0;
1.146     kristaps  350:        (*p->endline)(p);
1.16      kristaps  351: }
                    352:
                    353:
1.71      kristaps  354: static void
1.125     kristaps  355: spec(struct termp *p, const char *word, size_t len)
1.17      kristaps  356: {
1.71      kristaps  357:        const char      *rhs;
                    358:        size_t           sz;
1.17      kristaps  359:
1.101     kristaps  360:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.125     kristaps  361:        if (rhs)
                    362:                encode(p, rhs, sz);
1.94      kristaps  363: }
                    364:
                    365:
                    366: static void
1.125     kristaps  367: res(struct termp *p, const char *word, size_t len)
1.94      kristaps  368: {
                    369:        const char      *rhs;
                    370:        size_t           sz;
                    371:
1.101     kristaps  372:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.125     kristaps  373:        if (rhs)
                    374:                encode(p, rhs, sz);
                    375: }
                    376:
                    377:
                    378: void
                    379: term_fontlast(struct termp *p)
                    380: {
                    381:        enum termfont    f;
                    382:
                    383:        f = p->fontl;
                    384:        p->fontl = p->fontq[p->fonti];
                    385:        p->fontq[p->fonti] = f;
                    386: }
                    387:
                    388:
                    389: void
                    390: term_fontrepl(struct termp *p, enum termfont f)
                    391: {
                    392:
                    393:        p->fontl = p->fontq[p->fonti];
                    394:        p->fontq[p->fonti] = f;
                    395: }
                    396:
                    397:
                    398: void
                    399: term_fontpush(struct termp *p, enum termfont f)
                    400: {
                    401:
                    402:        assert(p->fonti + 1 < 10);
                    403:        p->fontl = p->fontq[p->fonti];
                    404:        p->fontq[++p->fonti] = f;
                    405: }
                    406:
                    407:
                    408: const void *
                    409: term_fontq(struct termp *p)
                    410: {
                    411:
                    412:        return(&p->fontq[p->fonti]);
                    413: }
                    414:
                    415:
                    416: enum termfont
                    417: term_fonttop(struct termp *p)
                    418: {
                    419:
                    420:        return(p->fontq[p->fonti]);
                    421: }
                    422:
                    423:
                    424: void
                    425: term_fontpopq(struct termp *p, const void *key)
                    426: {
                    427:
                    428:        while (p->fonti >= 0 && key != &p->fontq[p->fonti])
                    429:                p->fonti--;
                    430:        assert(p->fonti >= 0);
                    431: }
1.94      kristaps  432:
1.125     kristaps  433:
                    434: void
                    435: term_fontpop(struct termp *p)
                    436: {
                    437:
                    438:        assert(p->fonti);
                    439:        p->fonti--;
1.17      kristaps  440: }
                    441:
                    442:
1.71      kristaps  443: /*
                    444:  * Handle pwords, partial words, which may be either a single word or a
                    445:  * phrase that cannot be broken down (such as a literal string).  This
                    446:  * handles word styling.
                    447:  */
1.86      kristaps  448: void
                    449: term_word(struct termp *p, const char *word)
1.65      kristaps  450: {
1.124     kristaps  451:        const char      *sv, *seq;
1.125     kristaps  452:        int              sz;
1.124     kristaps  453:        size_t           ssz;
                    454:        enum roffdeco    deco;
1.71      kristaps  455:
1.100     kristaps  456:        sv = word;
                    457:
1.123     kristaps  458:        if (word[0] && '\0' == word[1])
1.100     kristaps  459:                switch (word[0]) {
                    460:                case('.'):
                    461:                        /* FALLTHROUGH */
                    462:                case(','):
                    463:                        /* FALLTHROUGH */
                    464:                case(';'):
                    465:                        /* FALLTHROUGH */
                    466:                case(':'):
                    467:                        /* FALLTHROUGH */
                    468:                case('?'):
                    469:                        /* FALLTHROUGH */
                    470:                case('!'):
                    471:                        /* FALLTHROUGH */
                    472:                case(')'):
                    473:                        /* FALLTHROUGH */
                    474:                case(']'):
                    475:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    476:                                p->flags |= TERMP_NOSPACE;
                    477:                        break;
                    478:                default:
                    479:                        break;
                    480:                }
1.65      kristaps  481:
1.133     kristaps  482:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151   ! schwarze  483:                if ( ! (TERMP_KEEP & p->flags)) {
        !           484:                        if (TERMP_PREKEEP & p->flags)
        !           485:                                p->flags |= TERMP_KEEP;
1.133     kristaps  486:                        bufferc(p, ' ');
1.151   ! schwarze  487:                        if (TERMP_SENTENCE & p->flags)
        !           488:                                bufferc(p, ' ');
        !           489:                } else
        !           490:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  491:        }
1.65      kristaps  492:
1.71      kristaps  493:        if ( ! (p->flags & TERMP_NONOSPACE))
                    494:                p->flags &= ~TERMP_NOSPACE;
1.133     kristaps  495:
                    496:        p->flags &= ~TERMP_SENTENCE;
1.65      kristaps  497:
1.125     kristaps  498:        /* FIXME: use strcspn. */
1.124     kristaps  499:
                    500:        while (*word) {
                    501:                if ('\\' != *word) {
1.125     kristaps  502:                        encode(p, word, 1);
1.124     kristaps  503:                        word++;
                    504:                        continue;
                    505:                }
                    506:
                    507:                seq = ++word;
                    508:                sz = a2roffdeco(&deco, &seq, &ssz);
                    509:
                    510:                switch (deco) {
                    511:                case (DECO_RESERVED):
1.125     kristaps  512:                        res(p, seq, ssz);
1.124     kristaps  513:                        break;
                    514:                case (DECO_SPECIAL):
1.125     kristaps  515:                        spec(p, seq, ssz);
1.124     kristaps  516:                        break;
                    517:                case (DECO_BOLD):
1.125     kristaps  518:                        term_fontrepl(p, TERMFONT_BOLD);
1.124     kristaps  519:                        break;
                    520:                case (DECO_ITALIC):
1.125     kristaps  521:                        term_fontrepl(p, TERMFONT_UNDER);
1.124     kristaps  522:                        break;
                    523:                case (DECO_ROMAN):
1.125     kristaps  524:                        term_fontrepl(p, TERMFONT_NONE);
1.124     kristaps  525:                        break;
                    526:                case (DECO_PREVIOUS):
1.125     kristaps  527:                        term_fontlast(p);
1.124     kristaps  528:                        break;
                    529:                default:
                    530:                        break;
                    531:                }
1.127     kristaps  532:
1.124     kristaps  533:                word += sz;
1.127     kristaps  534:                if (DECO_NOSPACE == deco && '\0' == *word)
                    535:                        p->flags |= TERMP_NOSPACE;
1.124     kristaps  536:        }
1.65      kristaps  537:
1.131     kristaps  538:        /*
                    539:         * Note that we don't process the pipe: the parser sees it as
                    540:         * punctuation, but we don't in terms of typography.
                    541:         */
1.100     kristaps  542:        if (sv[0] && 0 == sv[1])
                    543:                switch (sv[0]) {
                    544:                case('('):
                    545:                        /* FALLTHROUGH */
                    546:                case('['):
                    547:                        p->flags |= TERMP_NOSPACE;
                    548:                        break;
                    549:                default:
                    550:                        break;
                    551:                }
1.65      kristaps  552: }
                    553:
                    554:
1.71      kristaps  555: static void
1.125     kristaps  556: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  557: {
                    558:
1.125     kristaps  559:        if (0 == p->maxcols)
                    560:                p->maxcols = 1024;
                    561:        while (sz >= p->maxcols)
                    562:                p->maxcols <<= 2;
                    563:
                    564:        p->buf = realloc(p->buf, p->maxcols);
                    565:        if (NULL == p->buf) {
                    566:                perror(NULL);
                    567:                exit(EXIT_FAILURE);
1.71      kristaps  568:        }
1.51      kristaps  569: }
                    570:
1.79      kristaps  571:
                    572: static void
1.125     kristaps  573: buffera(struct termp *p, const char *word, size_t sz)
1.79      kristaps  574: {
1.125     kristaps  575:
                    576:        if (p->col + sz >= p->maxcols)
                    577:                adjbuf(p, p->col + sz);
                    578:
1.126     kristaps  579:        memcpy(&p->buf[(int)p->col], word, sz);
1.125     kristaps  580:        p->col += sz;
                    581: }
                    582:
                    583:
                    584: static void
                    585: bufferc(struct termp *p, char c)
                    586: {
                    587:
                    588:        if (p->col + 1 >= p->maxcols)
                    589:                adjbuf(p, p->col + 1);
                    590:
1.126     kristaps  591:        p->buf[(int)p->col++] = c;
1.125     kristaps  592: }
                    593:
                    594:
                    595: static void
                    596: encode(struct termp *p, const char *word, size_t sz)
                    597: {
                    598:        enum termfont     f;
                    599:        int               i;
                    600:
                    601:        /*
                    602:         * Encode and buffer a string of characters.  If the current
                    603:         * font mode is unset, buffer directly, else encode then buffer
                    604:         * character by character.
                    605:         */
                    606:
1.147     kristaps  607:        if (TERMFONT_NONE == (f = term_fonttop(p))) {
1.125     kristaps  608:                buffera(p, word, sz);
                    609:                return;
                    610:        }
                    611:
                    612:        for (i = 0; i < (int)sz; i++) {
                    613:                if ( ! isgraph((u_char)word[i])) {
                    614:                        bufferc(p, word[i]);
                    615:                        continue;
1.79      kristaps  616:                }
1.125     kristaps  617:
                    618:                if (TERMFONT_UNDER == f)
                    619:                        bufferc(p, '_');
                    620:                else
                    621:                        bufferc(p, word[i]);
                    622:
                    623:                bufferc(p, 8);
                    624:                bufferc(p, word[i]);
1.79      kristaps  625:        }
                    626: }
1.106     kristaps  627:
                    628:
1.107     kristaps  629: size_t
1.149     kristaps  630: term_len(const struct termp *p, size_t sz)
                    631: {
                    632:
                    633:        return((*p->width)(p, ' ') * sz);
                    634: }
                    635:
                    636:
                    637: size_t
                    638: term_strlen(const struct termp *p, const char *cp)
                    639: {
                    640:        size_t           sz;
                    641:
                    642:        for (sz = 0; *cp; cp++)
                    643:                sz += (*p->width)(p, *cp);
                    644:
                    645:        return(sz);
                    646: }
                    647:
                    648:
                    649: size_t
                    650: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  651: {
                    652:        double           r;
                    653:
1.107     kristaps  654:        switch (su->unit) {
1.106     kristaps  655:        case (SCALE_CM):
1.107     kristaps  656:                r = su->scale * 2;
1.106     kristaps  657:                break;
                    658:        case (SCALE_IN):
1.107     kristaps  659:                r = su->scale * 6;
1.106     kristaps  660:                break;
                    661:        case (SCALE_PC):
1.107     kristaps  662:                r = su->scale;
1.106     kristaps  663:                break;
                    664:        case (SCALE_PT):
1.107     kristaps  665:                r = su->scale / 8;
1.106     kristaps  666:                break;
                    667:        case (SCALE_MM):
1.107     kristaps  668:                r = su->scale / 1000;
1.106     kristaps  669:                break;
                    670:        case (SCALE_VS):
1.107     kristaps  671:                r = su->scale;
1.106     kristaps  672:                break;
                    673:        default:
1.107     kristaps  674:                r = su->scale - 1;
1.106     kristaps  675:                break;
                    676:        }
                    677:
                    678:        if (r < 0.0)
                    679:                r = 0.0;
1.107     kristaps  680:        return(/* LINTED */(size_t)
1.106     kristaps  681:                        r);
                    682: }
                    683:
                    684:
1.107     kristaps  685: size_t
1.149     kristaps  686: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  687: {
                    688:        double           r;
                    689:
1.108     kristaps  690:        /* XXX: CM, IN, and PT are approximations. */
                    691:
1.107     kristaps  692:        switch (su->unit) {
1.106     kristaps  693:        case (SCALE_CM):
1.108     kristaps  694:                r = 4 * su->scale;
1.106     kristaps  695:                break;
                    696:        case (SCALE_IN):
1.108     kristaps  697:                /* XXX: this is an approximation. */
                    698:                r = 10 * su->scale;
1.106     kristaps  699:                break;
                    700:        case (SCALE_PC):
1.108     kristaps  701:                r = (10 * su->scale) / 6;
1.106     kristaps  702:                break;
                    703:        case (SCALE_PT):
1.108     kristaps  704:                r = (10 * su->scale) / 72;
1.106     kristaps  705:                break;
                    706:        case (SCALE_MM):
1.107     kristaps  707:                r = su->scale / 1000; /* FIXME: double-check. */
1.106     kristaps  708:                break;
                    709:        case (SCALE_VS):
1.107     kristaps  710:                r = su->scale * 2 - 1; /* FIXME: double-check. */
1.106     kristaps  711:                break;
                    712:        default:
1.107     kristaps  713:                r = su->scale;
1.106     kristaps  714:                break;
                    715:        }
                    716:
                    717:        if (r < 0.0)
                    718:                r = 0.0;
1.107     kristaps  719:        return((size_t)/* LINTED */
1.106     kristaps  720:                        r);
                    721: }
                    722:
                    723:

CVSweb