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

Annotation of mandoc/term.c, Revision 1.121

1.121   ! kristaps    1: /*     $Id: term.c,v 1.120 2009/10/31 06:10:58 kristaps Exp $ */
1.1       kristaps    2: /*
1.75      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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:  */
                     17: #include <assert.h>
1.22      kristaps   18: #include <stdio.h>
1.1       kristaps   19: #include <stdlib.h>
                     20: #include <string.h>
1.113     kristaps   21: #include <time.h>
1.1       kristaps   22:
1.101     kristaps   23: #include "chars.h"
1.107     kristaps   24: #include "out.h"
1.71      kristaps   25: #include "term.h"
                     26: #include "man.h"
                     27: #include "mdoc.h"
1.105     kristaps   28: #include "main.h"
1.1       kristaps   29:
1.103     kristaps   30: /* FIXME: accomodate non-breaking, non-collapsing white-space. */
                     31: /* FIXME: accomodate non-breaking, collapsing white-space. */
                     32:
1.71      kristaps   33: static struct termp     *term_alloc(enum termenc);
                     34: static void              term_free(struct termp *);
1.95      kristaps   35:
                     36: static void              do_escaped(struct termp *, const char **);
                     37: static void              do_special(struct termp *,
1.71      kristaps   38:                                const char *, size_t);
1.95      kristaps   39: static void              do_reserved(struct termp *,
1.94      kristaps   40:                                const char *, size_t);
1.95      kristaps   41: static void              buffer(struct termp *, char);
                     42: static void              encode(struct termp *, char);
1.1       kristaps   43:
                     44:
1.71      kristaps   45: void *
                     46: ascii_alloc(void)
1.10      kristaps   47: {
1.1       kristaps   48:
1.71      kristaps   49:        return(term_alloc(TERMENC_ASCII));
1.1       kristaps   50: }
                     51:
                     52:
1.99      kristaps   53: void
1.71      kristaps   54: terminal_free(void *arg)
1.11      kristaps   55: {
                     56:
1.71      kristaps   57:        term_free((struct termp *)arg);
1.11      kristaps   58: }
                     59:
                     60:
1.71      kristaps   61: static void
                     62: term_free(struct termp *p)
1.14      kristaps   63: {
                     64:
1.71      kristaps   65:        if (p->buf)
                     66:                free(p->buf);
1.102     kristaps   67:        if (p->symtab)
1.101     kristaps   68:                chars_free(p->symtab);
1.14      kristaps   69:
1.71      kristaps   70:        free(p);
1.14      kristaps   71: }
                     72:
                     73:
1.71      kristaps   74: static struct termp *
                     75: term_alloc(enum termenc enc)
1.14      kristaps   76: {
1.71      kristaps   77:        struct termp *p;
1.14      kristaps   78:
1.117     kristaps   79:        p = calloc(1, sizeof(struct termp));
                     80:        if (NULL == p) {
1.120     kristaps   81:                perror(NULL);
1.117     kristaps   82:                exit(EXIT_FAILURE);
                     83:        }
1.80      kristaps   84:        p->maxrmargin = 78;
1.71      kristaps   85:        p->enc = enc;
                     86:        return(p);
1.14      kristaps   87: }
                     88:
                     89:
1.71      kristaps   90: /*
                     91:  * Flush a line of text.  A "line" is loosely defined as being something
                     92:  * that should be followed by a newline, regardless of whether it's
                     93:  * broken apart by newlines getting there.  A line can also be a
                     94:  * fragment of a columnar list.
                     95:  *
                     96:  * Specifically, a line is whatever's in p->buf of length p->col, which
                     97:  * is zeroed after this function returns.
                     98:  *
1.84      kristaps   99:  * The usage of termp:flags is as follows:
1.71      kristaps  100:  *
                    101:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    102:  *    offset value.  This is useful when doing columnar lists where the
                    103:  *    prior column has right-padded.
                    104:  *
                    105:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    106:  *    columns.  In short: don't print a newline and instead pad to the
                    107:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    108:  *
1.91      kristaps  109:  *  - TERMP_TWOSPACE: when padding, make sure there are at least two
                    110:  *    space characters of padding.  Otherwise, rather break the line.
                    111:  *
1.84      kristaps  112:  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
                    113:  *    the line is overrun, and don't pad-right if it's underrun.
                    114:  *
                    115:  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
                    116:  *    overruning, instead save the position and continue at that point
                    117:  *    when the next invocation.
1.71      kristaps  118:  *
                    119:  *  In-line line breaking:
                    120:  *
                    121:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    122:  *  margin, it will break and pad-right to the right margin after
                    123:  *  writing.  If maxrmargin is violated, it will break and continue
1.114     kristaps  124:  *  writing from the right-margin, which will lead to the above scenario
                    125:  *  upon exit.  Otherwise, the line will break at the right margin.
1.71      kristaps  126:  */
                    127: void
                    128: term_flushln(struct termp *p)
1.53      kristaps  129: {
1.114     kristaps  130:        int              i;     /* current input position in p->buf */
                    131:        size_t           vis;   /* current visual position on output */
                    132:        size_t           vbl;   /* number of blanks to prepend to output */
                    133:        size_t           vsz;   /* visual characters to write to output */
                    134:        size_t           bp;    /* visual right border position */
                    135:        int              j;     /* temporary loop index */
                    136:        size_t           maxvis, mmax;
1.91      kristaps  137:        static int       overstep = 0;
1.53      kristaps  138:
1.71      kristaps  139:        /*
                    140:         * First, establish the maximum columns of "visible" content.
                    141:         * This is usually the difference between the right-margin and
                    142:         * an indentation, but can be, for tagged lists or columns, a
1.115     kristaps  143:         * small set of values.
1.71      kristaps  144:         */
1.53      kristaps  145:
1.71      kristaps  146:        assert(p->offset < p->rmargin);
1.92      kristaps  147:
1.114     kristaps  148:        maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
1.119     kristaps  149:                /* LINTED */
                    150:                0 : p->rmargin - p->offset - overstep;
1.114     kristaps  151:        mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
1.119     kristaps  152:                /* LINTED */
                    153:                0 : p->maxrmargin - p->offset - overstep;
1.92      kristaps  154:
1.71      kristaps  155:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  156:
                    157:        /*
                    158:         * FIXME: if bp is zero, we still output the first word before
                    159:         * breaking the line.
                    160:         */
                    161:
1.71      kristaps  162:        vis = 0;
1.84      kristaps  163:
1.71      kristaps  164:        /*
                    165:         * If in the standard case (left-justified), then begin with our
                    166:         * indentation, otherwise (columns, etc.) just start spitting
                    167:         * out text.
                    168:         */
1.53      kristaps  169:
1.71      kristaps  170:        if ( ! (p->flags & TERMP_NOLPAD))
                    171:                /* LINTED */
                    172:                for (j = 0; j < (int)p->offset; j++)
                    173:                        putchar(' ');
                    174:
                    175:        for (i = 0; i < (int)p->col; i++) {
                    176:                /*
                    177:                 * Count up visible word characters.  Control sequences
                    178:                 * (starting with the CSI) aren't counted.  A space
                    179:                 * generates a non-printing word, which is valid (the
                    180:                 * space is printed according to regular spacing rules).
                    181:                 */
                    182:
                    183:                /* LINTED */
                    184:                for (j = i, vsz = 0; j < (int)p->col; j++) {
1.93      kristaps  185:                        if (j && ' ' == p->buf[j])
1.71      kristaps  186:                                break;
                    187:                        else if (8 == p->buf[j])
1.89      kristaps  188:                                vsz--;
1.71      kristaps  189:                        else
                    190:                                vsz++;
                    191:                }
1.53      kristaps  192:
1.71      kristaps  193:                /*
1.81      kristaps  194:                 * Choose the number of blanks to prepend: no blank at the
                    195:                 * beginning of a line, one between words -- but do not
                    196:                 * actually write them yet.
1.71      kristaps  197:                 */
1.81      kristaps  198:                vbl = (size_t)(0 == vis ? 0 : 1);
1.71      kristaps  199:
1.81      kristaps  200:                /*
                    201:                 * Find out whether we would exceed the right margin.
                    202:                 * If so, break to the next line.  (TODO: hyphenate)
                    203:                 * Otherwise, write the chosen number of blanks now.
                    204:                 */
                    205:                if (vis && vis + vbl + vsz > bp) {
                    206:                        putchar('\n');
                    207:                        if (TERMP_NOBREAK & p->flags) {
                    208:                                for (j = 0; j < (int)p->rmargin; j++)
                    209:                                        putchar(' ');
                    210:                                vis = p->rmargin - p->offset;
                    211:                        } else {
1.71      kristaps  212:                                for (j = 0; j < (int)p->offset; j++)
                    213:                                        putchar(' ');
                    214:                                vis = 0;
1.81      kristaps  215:                        }
1.104     kristaps  216:                        /* Remove the overstep width. */
1.112     kristaps  217:                        bp += (int)/* LINTED */
                    218:                                overstep;
1.110     kristaps  219:                        overstep = 0;
1.81      kristaps  220:                } else {
                    221:                        for (j = 0; j < (int)vbl; j++)
1.71      kristaps  222:                                putchar(' ');
1.81      kristaps  223:                        vis += vbl;
1.71      kristaps  224:                }
1.53      kristaps  225:
1.78      kristaps  226:                /*
1.81      kristaps  227:                 * Finally, write out the word.
1.71      kristaps  228:                 */
                    229:                for ( ; i < (int)p->col; i++) {
                    230:                        if (' ' == p->buf[i])
                    231:                                break;
1.121   ! kristaps  232:
        !           233:                        /* The unit sep. is a non-breaking space. */
        !           234:                        if (31 == p->buf[i])
        !           235:                                putchar(' ');
        !           236:                        else
        !           237:                                putchar(p->buf[i]);
1.71      kristaps  238:                }
                    239:                vis += vsz;
                    240:        }
1.111     kristaps  241:
1.91      kristaps  242:        p->col = 0;
1.111     kristaps  243:        overstep = 0;
1.15      kristaps  244:
1.91      kristaps  245:        if ( ! (TERMP_NOBREAK & p->flags)) {
                    246:                putchar('\n');
1.15      kristaps  247:                return;
1.71      kristaps  248:        }
1.15      kristaps  249:
1.91      kristaps  250:        if (TERMP_HANG & p->flags) {
                    251:                /* We need one blank after the tag. */
1.92      kristaps  252:                overstep = /* LINTED */
                    253:                        vis - maxvis + 1;
1.91      kristaps  254:
                    255:                /*
                    256:                 * Behave exactly the same way as groff:
1.92      kristaps  257:                 * If we have overstepped the margin, temporarily move
                    258:                 * it to the right and flag the rest of the line to be
                    259:                 * shorter.
1.91      kristaps  260:                 * If we landed right at the margin, be happy.
1.92      kristaps  261:                 * If we are one step before the margin, temporarily
                    262:                 * move it one step LEFT and flag the rest of the line
                    263:                 * to be longer.
1.91      kristaps  264:                 */
1.92      kristaps  265:                if (overstep >= -1) {
                    266:                        assert((int)maxvis + overstep >= 0);
                    267:                        /* LINTED */
1.91      kristaps  268:                        maxvis += overstep;
1.92      kristaps  269:                } else
1.91      kristaps  270:                        overstep = 0;
                    271:
                    272:        } else if (TERMP_DANGLE & p->flags)
                    273:                return;
1.15      kristaps  274:
1.92      kristaps  275:        /* Right-pad. */
                    276:        if (maxvis > vis + /* LINTED */
                    277:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
1.91      kristaps  278:                for ( ; vis < maxvis; vis++)
                    279:                        putchar(' ');
1.92      kristaps  280:        else {  /* ...or newline break. */
1.71      kristaps  281:                putchar('\n');
1.91      kristaps  282:                for (i = 0; i < (int)p->rmargin; i++)
                    283:                        putchar(' ');
                    284:        }
1.15      kristaps  285: }
                    286:
                    287:
1.71      kristaps  288: /*
                    289:  * A newline only breaks an existing line; it won't assert vertical
                    290:  * space.  All data in the output buffer is flushed prior to the newline
                    291:  * assertion.
                    292:  */
                    293: void
                    294: term_newln(struct termp *p)
1.15      kristaps  295: {
                    296:
1.71      kristaps  297:        p->flags |= TERMP_NOSPACE;
                    298:        if (0 == p->col) {
                    299:                p->flags &= ~TERMP_NOLPAD;
1.15      kristaps  300:                return;
1.16      kristaps  301:        }
1.71      kristaps  302:        term_flushln(p);
                    303:        p->flags &= ~TERMP_NOLPAD;
1.16      kristaps  304: }
                    305:
                    306:
1.71      kristaps  307: /*
                    308:  * Asserts a vertical space (a full, empty line-break between lines).
                    309:  * Note that if used twice, this will cause two blank spaces and so on.
                    310:  * All data in the output buffer is flushed prior to the newline
                    311:  * assertion.
                    312:  */
                    313: void
                    314: term_vspace(struct termp *p)
1.16      kristaps  315: {
                    316:
1.62      kristaps  317:        term_newln(p);
1.71      kristaps  318:        putchar('\n');
1.16      kristaps  319: }
                    320:
                    321:
1.71      kristaps  322: static void
1.95      kristaps  323: do_special(struct termp *p, const char *word, size_t len)
1.17      kristaps  324: {
1.71      kristaps  325:        const char      *rhs;
                    326:        size_t           sz;
1.79      kristaps  327:        int              i;
1.17      kristaps  328:
1.101     kristaps  329:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.86      kristaps  330:
1.96      kristaps  331:        if (NULL == rhs) {
1.97      kristaps  332: #if 0
1.96      kristaps  333:                fputs("Unknown special character: ", stderr);
                    334:                for (i = 0; i < (int)len; i++)
                    335:                        fputc(word[i], stderr);
                    336:                fputc('\n', stderr);
                    337: #endif
1.94      kristaps  338:                return;
1.96      kristaps  339:        }
1.94      kristaps  340:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  341:                encode(p, rhs[i]);
1.94      kristaps  342: }
                    343:
                    344:
                    345: static void
1.95      kristaps  346: do_reserved(struct termp *p, const char *word, size_t len)
1.94      kristaps  347: {
                    348:        const char      *rhs;
                    349:        size_t           sz;
                    350:        int              i;
                    351:
1.101     kristaps  352:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.94      kristaps  353:
1.96      kristaps  354:        if (NULL == rhs) {
                    355: #if 0
                    356:                fputs("Unknown reserved word: ", stderr);
                    357:                for (i = 0; i < (int)len; i++)
                    358:                        fputc(word[i], stderr);
                    359:                fputc('\n', stderr);
                    360: #endif
1.94      kristaps  361:                return;
1.96      kristaps  362:        }
1.94      kristaps  363:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  364:                encode(p, rhs[i]);
1.17      kristaps  365: }
                    366:
                    367:
1.71      kristaps  368: /*
                    369:  * Handle an escape sequence: determine its length and pass it to the
                    370:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    371:  * the escape sequence (we assert upon badly-formed escape sequences).
                    372:  */
                    373: static void
1.95      kristaps  374: do_escaped(struct termp *p, const char **word)
1.17      kristaps  375: {
1.97      kristaps  376:        int              j, type;
1.86      kristaps  377:        const char      *wp;
                    378:
                    379:        wp = *word;
1.97      kristaps  380:        type = 1;
1.17      kristaps  381:
1.86      kristaps  382:        if (0 == *(++wp)) {
                    383:                *word = wp;
1.71      kristaps  384:                return;
1.86      kristaps  385:        }
1.17      kristaps  386:
1.86      kristaps  387:        if ('(' == *wp) {
                    388:                wp++;
                    389:                if (0 == *wp || 0 == *(wp + 1)) {
                    390:                        *word = 0 == *wp ? wp : wp + 1;
1.71      kristaps  391:                        return;
1.86      kristaps  392:                }
1.22      kristaps  393:
1.95      kristaps  394:                do_special(p, wp, 2);
1.86      kristaps  395:                *word = ++wp;
1.71      kristaps  396:                return;
1.22      kristaps  397:
1.86      kristaps  398:        } else if ('*' == *wp) {
                    399:                if (0 == *(++wp)) {
                    400:                        *word = wp;
1.71      kristaps  401:                        return;
1.86      kristaps  402:                }
1.22      kristaps  403:
1.86      kristaps  404:                switch (*wp) {
1.71      kristaps  405:                case ('('):
1.86      kristaps  406:                        wp++;
                    407:                        if (0 == *wp || 0 == *(wp + 1)) {
                    408:                                *word = 0 == *wp ? wp : wp + 1;
1.71      kristaps  409:                                return;
1.86      kristaps  410:                        }
1.65      kristaps  411:
1.95      kristaps  412:                        do_reserved(p, wp, 2);
1.86      kristaps  413:                        *word = ++wp;
1.71      kristaps  414:                        return;
                    415:                case ('['):
1.97      kristaps  416:                        type = 0;
1.71      kristaps  417:                        break;
                    418:                default:
1.95      kristaps  419:                        do_reserved(p, wp, 1);
1.86      kristaps  420:                        *word = wp;
1.71      kristaps  421:                        return;
                    422:                }
                    423:
1.86      kristaps  424:        } else if ('f' == *wp) {
                    425:                if (0 == *(++wp)) {
                    426:                        *word = wp;
1.71      kristaps  427:                        return;
1.86      kristaps  428:                }
                    429:
                    430:                switch (*wp) {
1.71      kristaps  431:                case ('B'):
1.98      kristaps  432:                        p->bold++;
1.71      kristaps  433:                        break;
                    434:                case ('I'):
1.98      kristaps  435:                        p->under++;
1.71      kristaps  436:                        break;
                    437:                case ('P'):
                    438:                        /* FALLTHROUGH */
                    439:                case ('R'):
1.98      kristaps  440:                        p->bold = p->under = 0;
1.71      kristaps  441:                        break;
                    442:                default:
                    443:                        break;
                    444:                }
1.86      kristaps  445:
                    446:                *word = wp;
1.71      kristaps  447:                return;
1.22      kristaps  448:
1.86      kristaps  449:        } else if ('[' != *wp) {
1.95      kristaps  450:                do_special(p, wp, 1);
1.86      kristaps  451:                *word = wp;
1.71      kristaps  452:                return;
                    453:        }
1.28      kristaps  454:
1.86      kristaps  455:        wp++;
                    456:        for (j = 0; *wp && ']' != *wp; wp++, j++)
1.71      kristaps  457:                /* Loop... */ ;
1.28      kristaps  458:
1.86      kristaps  459:        if (0 == *wp) {
                    460:                *word = wp;
1.71      kristaps  461:                return;
1.86      kristaps  462:        }
1.48      kristaps  463:
1.97      kristaps  464:        if (type)
                    465:                do_special(p, wp - j, (size_t)j);
                    466:        else
                    467:                do_reserved(p, wp - j, (size_t)j);
1.86      kristaps  468:        *word = wp;
1.48      kristaps  469: }
                    470:
                    471:
1.71      kristaps  472: /*
                    473:  * Handle pwords, partial words, which may be either a single word or a
                    474:  * phrase that cannot be broken down (such as a literal string).  This
                    475:  * handles word styling.
                    476:  */
1.86      kristaps  477: void
                    478: term_word(struct termp *p, const char *word)
1.65      kristaps  479: {
1.88      kristaps  480:        const char       *sv;
1.71      kristaps  481:
1.100     kristaps  482:        sv = word;
                    483:
                    484:        if (word[0] && 0 == word[1])
                    485:                switch (word[0]) {
                    486:                case('.'):
                    487:                        /* FALLTHROUGH */
                    488:                case(','):
                    489:                        /* FALLTHROUGH */
                    490:                case(';'):
                    491:                        /* FALLTHROUGH */
                    492:                case(':'):
                    493:                        /* FALLTHROUGH */
                    494:                case('?'):
                    495:                        /* FALLTHROUGH */
                    496:                case('!'):
                    497:                        /* FALLTHROUGH */
                    498:                case(')'):
                    499:                        /* FALLTHROUGH */
                    500:                case(']'):
                    501:                        /* FALLTHROUGH */
                    502:                case('}'):
                    503:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    504:                                p->flags |= TERMP_NOSPACE;
                    505:                        break;
                    506:                default:
                    507:                        break;
                    508:                }
1.65      kristaps  509:
1.71      kristaps  510:        if ( ! (TERMP_NOSPACE & p->flags))
1.95      kristaps  511:                buffer(p, ' ');
1.65      kristaps  512:
1.71      kristaps  513:        if ( ! (p->flags & TERMP_NONOSPACE))
                    514:                p->flags &= ~TERMP_NOSPACE;
1.65      kristaps  515:
1.100     kristaps  516:        for ( ; *word; word++)
1.86      kristaps  517:                if ('\\' != *word)
1.95      kristaps  518:                        encode(p, *word);
1.79      kristaps  519:                else
1.95      kristaps  520:                        do_escaped(p, &word);
1.65      kristaps  521:
1.100     kristaps  522:        if (sv[0] && 0 == sv[1])
                    523:                switch (sv[0]) {
                    524:                case('('):
                    525:                        /* FALLTHROUGH */
                    526:                case('['):
                    527:                        /* FALLTHROUGH */
                    528:                case('{'):
                    529:                        p->flags |= TERMP_NOSPACE;
                    530:                        break;
                    531:                default:
                    532:                        break;
                    533:                }
1.65      kristaps  534: }
                    535:
                    536:
1.71      kristaps  537: /*
                    538:  * Insert a single character into the line-buffer.  If the buffer's
                    539:  * space is exceeded, then allocate more space by doubling the buffer
                    540:  * size.
                    541:  */
                    542: static void
1.95      kristaps  543: buffer(struct termp *p, char c)
1.51      kristaps  544: {
1.71      kristaps  545:        size_t           s;
1.51      kristaps  546:
1.71      kristaps  547:        if (p->col + 1 >= p->maxcols) {
                    548:                if (0 == p->maxcols)
                    549:                        p->maxcols = 256;
                    550:                s = p->maxcols * 2;
                    551:                p->buf = realloc(p->buf, s);
1.118     kristaps  552:                if (NULL == p->buf) {
1.120     kristaps  553:                        perror(NULL);
1.118     kristaps  554:                        exit(EXIT_FAILURE);
                    555:                }
1.71      kristaps  556:                p->maxcols = s;
                    557:        }
                    558:        p->buf[(int)(p->col)++] = c;
1.51      kristaps  559: }
                    560:
1.79      kristaps  561:
                    562: static void
1.95      kristaps  563: encode(struct termp *p, char c)
1.79      kristaps  564: {
1.89      kristaps  565:
1.98      kristaps  566:        if (' ' != c) {
1.109     kristaps  567:                if (p->under) {
                    568:                        buffer(p, '_');
                    569:                        buffer(p, 8);
                    570:                }
1.98      kristaps  571:                if (p->bold) {
1.95      kristaps  572:                        buffer(p, c);
                    573:                        buffer(p, 8);
1.79      kristaps  574:                }
                    575:        }
1.95      kristaps  576:        buffer(p, c);
1.79      kristaps  577: }
1.106     kristaps  578:
                    579:
1.107     kristaps  580: size_t
                    581: term_vspan(const struct roffsu *su)
1.106     kristaps  582: {
                    583:        double           r;
                    584:
1.107     kristaps  585:        switch (su->unit) {
1.106     kristaps  586:        case (SCALE_CM):
1.107     kristaps  587:                r = su->scale * 2;
1.106     kristaps  588:                break;
                    589:        case (SCALE_IN):
1.107     kristaps  590:                r = su->scale * 6;
1.106     kristaps  591:                break;
                    592:        case (SCALE_PC):
1.107     kristaps  593:                r = su->scale;
1.106     kristaps  594:                break;
                    595:        case (SCALE_PT):
1.107     kristaps  596:                r = su->scale / 8;
1.106     kristaps  597:                break;
                    598:        case (SCALE_MM):
1.107     kristaps  599:                r = su->scale / 1000;
1.106     kristaps  600:                break;
                    601:        case (SCALE_VS):
1.107     kristaps  602:                r = su->scale;
1.106     kristaps  603:                break;
                    604:        default:
1.107     kristaps  605:                r = su->scale - 1;
1.106     kristaps  606:                break;
                    607:        }
                    608:
                    609:        if (r < 0.0)
                    610:                r = 0.0;
1.107     kristaps  611:        return(/* LINTED */(size_t)
1.106     kristaps  612:                        r);
                    613: }
                    614:
                    615:
1.107     kristaps  616: size_t
                    617: term_hspan(const struct roffsu *su)
1.106     kristaps  618: {
                    619:        double           r;
                    620:
1.108     kristaps  621:        /* XXX: CM, IN, and PT are approximations. */
                    622:
1.107     kristaps  623:        switch (su->unit) {
1.106     kristaps  624:        case (SCALE_CM):
1.108     kristaps  625:                r = 4 * su->scale;
1.106     kristaps  626:                break;
                    627:        case (SCALE_IN):
1.108     kristaps  628:                /* XXX: this is an approximation. */
                    629:                r = 10 * su->scale;
1.106     kristaps  630:                break;
                    631:        case (SCALE_PC):
1.108     kristaps  632:                r = (10 * su->scale) / 6;
1.106     kristaps  633:                break;
                    634:        case (SCALE_PT):
1.108     kristaps  635:                r = (10 * su->scale) / 72;
1.106     kristaps  636:                break;
                    637:        case (SCALE_MM):
1.107     kristaps  638:                r = su->scale / 1000; /* FIXME: double-check. */
1.106     kristaps  639:                break;
                    640:        case (SCALE_VS):
1.107     kristaps  641:                r = su->scale * 2 - 1; /* FIXME: double-check. */
1.106     kristaps  642:                break;
                    643:        default:
1.107     kristaps  644:                r = su->scale;
1.106     kristaps  645:                break;
                    646:        }
                    647:
                    648:        if (r < 0.0)
                    649:                r = 0.0;
1.107     kristaps  650:        return((size_t)/* LINTED */
1.106     kristaps  651:                        r);
                    652: }
                    653:
                    654:

CVSweb