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

Annotation of mandoc/term.c, Revision 1.233

1.233   ! schwarze    1: /*     $Id: term.c,v 1.232 2014/10/28 18:49:33 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.216     schwarze    4:  * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.74      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.74      kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.128     kristaps   18: #include "config.h"
                     19:
1.126     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.122     kristaps   23: #include <ctype.h>
1.22      kristaps   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.137     kristaps   28: #include "mandoc.h"
1.218     schwarze   29: #include "mandoc_aux.h"
1.107     kristaps   30: #include "out.h"
1.71      kristaps   31: #include "term.h"
1.105     kristaps   32: #include "main.h"
1.1       kristaps   33:
1.203     schwarze   34: static size_t           cond_width(const struct termp *, int, int *);
1.210     schwarze   35: static void             adjbuf(struct termp *p, size_t);
1.191     kristaps   36: static void             bufferc(struct termp *, char);
                     37: static void             encode(struct termp *, const char *, size_t);
1.194     kristaps   38: static void             encode1(struct termp *, int);
1.11      kristaps   39:
1.222     schwarze   40:
1.145     kristaps   41: void
1.71      kristaps   42: term_free(struct termp *p)
1.14      kristaps   43: {
                     44:
1.231     schwarze   45:        free(p->buf);
1.142     kristaps   46:        free(p);
                     47: }
                     48:
                     49: void
1.222     schwarze   50: term_begin(struct termp *p, term_margin head,
1.142     kristaps   51:                term_margin foot, const void *arg)
                     52: {
                     53:
                     54:        p->headf = head;
                     55:        p->footf = foot;
                     56:        p->argf = arg;
1.146     kristaps   57:        (*p->begin)(p);
1.142     kristaps   58: }
                     59:
                     60: void
                     61: term_end(struct termp *p)
                     62: {
                     63:
1.146     kristaps   64:        (*p->end)(p);
1.14      kristaps   65: }
                     66:
1.71      kristaps   67: /*
1.221     schwarze   68:  * Flush a chunk of text.  By default, break the output line each time
                     69:  * the right margin is reached, and continue output on the next line
                     70:  * at the same offset as the chunk itself.  By default, also break the
                     71:  * output line at the end of the chunk.
1.130     kristaps   72:  * The following flags may be specified:
1.71      kristaps   73:  *
1.221     schwarze   74:  *  - TERMP_NOBREAK: Do not break the output line at the right margin,
                     75:  *    but only at the max right margin.  Also, do not break the output
                     76:  *    line at the end of the chunk, such that the next call can pad to
                     77:  *    the next column.  However, if less than p->trailspace blanks,
                     78:  *    which can be 0, 1, or 2, remain to the right margin, the line
                     79:  *    will be broken.
                     80:  *  - TERMP_BRIND: If the chunk does not fit and the output line has
                     81:  *    to be broken, start the next line at the right margin instead
                     82:  *    of at the offset.  Used together with TERMP_NOBREAK for the tags
                     83:  *    in various kinds of tagged lists.
                     84:  *  - TERMP_DANGLE: Do not break the output line at the right margin,
                     85:  *    append the next chunk after it even if this one is too long.
                     86:  *    To be used together with TERMP_NOBREAK.
                     87:  *  - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
                     88:  *    the next chunk if this column is not full.
1.71      kristaps   89:  */
                     90: void
                     91: term_flushln(struct termp *p)
1.53      kristaps   92: {
1.210     schwarze   93:        size_t           i;     /* current input position in p->buf */
1.205     schwarze   94:        int              ntab;  /* number of tabs to prepend */
1.114     kristaps   95:        size_t           vis;   /* current visual position on output */
                     96:        size_t           vbl;   /* number of blanks to prepend to output */
1.136     schwarze   97:        size_t           vend;  /* end of word visual position on output */
1.114     kristaps   98:        size_t           bp;    /* visual right border position */
1.172     schwarze   99:        size_t           dv;    /* temporary for visual pos calculations */
1.210     schwarze  100:        size_t           j;     /* temporary loop index for p->buf */
                    101:        size_t           jhy;   /* last hyph before overflow w/r/t j */
1.152     kristaps  102:        size_t           maxvis; /* output position of visible boundary */
                    103:        size_t           mmax; /* used in calculating bp */
1.53      kristaps  104:
1.71      kristaps  105:        /*
                    106:         * First, establish the maximum columns of "visible" content.
                    107:         * This is usually the difference between the right-margin and
                    108:         * an indentation, but can be, for tagged lists or columns, a
1.212     schwarze  109:         * small set of values.
                    110:         *
                    111:         * The following unsigned-signed subtractions look strange,
                    112:         * but they are actually correct.  If the int p->overstep
                    113:         * is negative, it gets sign extended.  Subtracting that
                    114:         * very large size_t effectively adds a small number to dv.
1.71      kristaps  115:         */
1.175     kristaps  116:        assert  (p->rmargin >= p->offset);
1.174     schwarze  117:        dv     = p->rmargin - p->offset;
                    118:        maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
                    119:        dv     = p->maxrmargin - p->offset;
                    120:        mmax   = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
1.92      kristaps  121:
1.71      kristaps  122:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  123:
1.136     schwarze  124:        /*
1.200     schwarze  125:         * Calculate the required amount of padding.
1.136     schwarze  126:         */
1.200     schwarze  127:        vbl = p->offset + p->overstep > p->viscol ?
                    128:              p->offset + p->overstep - p->viscol : 0;
1.136     schwarze  129:
1.174     schwarze  130:        vis = vend = 0;
                    131:        i = 0;
1.115     kristaps  132:
1.188     kristaps  133:        while (i < p->col) {
1.71      kristaps  134:                /*
1.154     kristaps  135:                 * Handle literal tab characters: collapse all
                    136:                 * subsequent tabs into a single huge set of spaces.
1.138     schwarze  137:                 */
1.205     schwarze  138:                ntab = 0;
1.188     kristaps  139:                while (i < p->col && '\t' == p->buf[i]) {
1.154     kristaps  140:                        vend = (vis / p->tabwidth + 1) * p->tabwidth;
1.138     schwarze  141:                        vbl += vend - vis;
                    142:                        vis = vend;
1.205     schwarze  143:                        ntab++;
1.169     schwarze  144:                        i++;
1.138     schwarze  145:                }
                    146:
                    147:                /*
1.71      kristaps  148:                 * Count up visible word characters.  Control sequences
                    149:                 * (starting with the CSI) aren't counted.  A space
                    150:                 * generates a non-printing word, which is valid (the
                    151:                 * space is printed according to regular spacing rules).
                    152:                 */
                    153:
1.188     kristaps  154:                for (j = i, jhy = 0; j < p->col; j++) {
1.208     schwarze  155:                        if (' ' == p->buf[j] || '\t' == p->buf[j])
1.71      kristaps  156:                                break;
1.154     kristaps  157:
                    158:                        /* Back over the the last printed character. */
                    159:                        if (8 == p->buf[j]) {
1.153     kristaps  160:                                assert(j);
                    161:                                vend -= (*p->width)(p, p->buf[j - 1]);
1.154     kristaps  162:                                continue;
1.153     kristaps  163:                        }
1.154     kristaps  164:
                    165:                        /* Regular word. */
                    166:                        /* Break at the hyphen point if we overrun. */
1.222     schwarze  167:                        if (vend > vis && vend < bp &&
1.216     schwarze  168:                            (ASCII_HYPH == p->buf[j] ||
                    169:                             ASCII_BREAK == p->buf[j]))
1.154     kristaps  170:                                jhy = j;
                    171:
1.217     schwarze  172:                        /*
                    173:                         * Hyphenation now decided, put back a real
                    174:                         * hyphen such that we get the correct width.
                    175:                         */
                    176:                        if (ASCII_HYPH == p->buf[j])
                    177:                                p->buf[j] = '-';
                    178:
1.154     kristaps  179:                        vend += (*p->width)(p, p->buf[j]);
1.71      kristaps  180:                }
1.53      kristaps  181:
1.71      kristaps  182:                /*
1.81      kristaps  183:                 * Find out whether we would exceed the right margin.
1.136     schwarze  184:                 * If so, break to the next line.
1.81      kristaps  185:                 */
1.140     kristaps  186:                if (vend > bp && 0 == jhy && vis > 0) {
1.136     schwarze  187:                        vend -= vis;
1.146     kristaps  188:                        (*p->endline)(p);
1.201     schwarze  189:                        p->viscol = 0;
1.221     schwarze  190:                        if (TERMP_BRIND & p->flags) {
1.201     schwarze  191:                                vbl = p->rmargin;
1.136     schwarze  192:                                vend += p->rmargin - p->offset;
1.201     schwarze  193:                        } else
1.136     schwarze  194:                                vbl = p->offset;
1.205     schwarze  195:
                    196:                        /* use pending tabs on the new line */
                    197:
                    198:                        if (0 < ntab)
                    199:                                vbl += ntab * p->tabwidth;
1.130     kristaps  200:
1.212     schwarze  201:                        /*
                    202:                         * Remove the p->overstep width.
                    203:                         * Again, if p->overstep is negative,
                    204:                         * sign extension does the right thing.
                    205:                         */
1.130     kristaps  206:
1.174     schwarze  207:                        bp += (size_t)p->overstep;
1.129     kristaps  208:                        p->overstep = 0;
1.71      kristaps  209:                }
1.138     schwarze  210:
1.130     kristaps  211:                /* Write out the [remaining] word. */
1.188     kristaps  212:                for ( ; i < p->col; i++) {
1.140     kristaps  213:                        if (vend > bp && jhy > 0 && i > jhy)
                    214:                                break;
1.138     schwarze  215:                        if ('\t' == p->buf[i])
                    216:                                break;
1.136     schwarze  217:                        if (' ' == p->buf[i]) {
1.164     kristaps  218:                                j = i;
1.228     kristaps  219:                                while (i < p->col && ' ' == p->buf[i])
1.136     schwarze  220:                                        i++;
1.210     schwarze  221:                                dv = (i - j) * (*p->width)(p, ' ');
1.172     schwarze  222:                                vbl += dv;
                    223:                                vend += dv;
1.71      kristaps  224:                                break;
1.136     schwarze  225:                        }
                    226:                        if (ASCII_NBRSP == p->buf[i]) {
1.153     kristaps  227:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  228:                                continue;
                    229:                        }
1.216     schwarze  230:                        if (ASCII_BREAK == p->buf[i])
                    231:                                continue;
1.130     kristaps  232:
1.136     schwarze  233:                        /*
                    234:                         * Now we definitely know there will be
                    235:                         * printable characters to output,
                    236:                         * so write preceding white space now.
                    237:                         */
                    238:                        if (vbl) {
1.146     kristaps  239:                                (*p->advance)(p, vbl);
1.139     schwarze  240:                                p->viscol += vbl;
1.136     schwarze  241:                                vbl = 0;
1.200     schwarze  242:                        }
                    243:
                    244:                        (*p->letter)(p, p->buf[i]);
                    245:                        if (8 == p->buf[i])
                    246:                                p->viscol -= (*p->width)(p, p->buf[i-1]);
1.222     schwarze  247:                        else
1.153     kristaps  248:                                p->viscol += (*p->width)(p, p->buf[i]);
1.136     schwarze  249:                }
                    250:                vis = vend;
1.71      kristaps  251:        }
1.168     schwarze  252:
                    253:        /*
                    254:         * If there was trailing white space, it was not printed;
                    255:         * so reset the cursor position accordingly.
                    256:         */
1.200     schwarze  257:        if (vis)
                    258:                vis -= vbl;
1.111     kristaps  259:
1.91      kristaps  260:        p->col = 0;
1.129     kristaps  261:        p->overstep = 0;
1.15      kristaps  262:
1.91      kristaps  263:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  264:                p->viscol = 0;
1.146     kristaps  265:                (*p->endline)(p);
1.15      kristaps  266:                return;
1.71      kristaps  267:        }
1.15      kristaps  268:
1.91      kristaps  269:        if (TERMP_HANG & p->flags) {
1.211     schwarze  270:                p->overstep = (int)(vis - maxvis +
1.222     schwarze  271:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  272:
                    273:                /*
1.92      kristaps  274:                 * If we have overstepped the margin, temporarily move
                    275:                 * it to the right and flag the rest of the line to be
                    276:                 * shorter.
1.212     schwarze  277:                 * If there is a request to keep the columns together,
                    278:                 * allow negative overstep when the column is not full.
1.91      kristaps  279:                 */
1.212     schwarze  280:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  281:                        p->overstep = 0;
1.200     schwarze  282:                return;
1.91      kristaps  283:
                    284:        } else if (TERMP_DANGLE & p->flags)
                    285:                return;
1.15      kristaps  286:
1.200     schwarze  287:        /* If the column was overrun, break the line. */
1.211     schwarze  288:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  289:                (*p->endline)(p);
1.200     schwarze  290:                p->viscol = 0;
1.91      kristaps  291:        }
1.15      kristaps  292: }
                    293:
1.222     schwarze  294: /*
1.71      kristaps  295:  * A newline only breaks an existing line; it won't assert vertical
                    296:  * space.  All data in the output buffer is flushed prior to the newline
                    297:  * assertion.
                    298:  */
                    299: void
                    300: term_newln(struct termp *p)
1.15      kristaps  301: {
                    302:
1.71      kristaps  303:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  304:        if (p->col || p->viscol)
                    305:                term_flushln(p);
1.16      kristaps  306: }
                    307:
1.71      kristaps  308: /*
                    309:  * Asserts a vertical space (a full, empty line-break between lines).
                    310:  * Note that if used twice, this will cause two blank spaces and so on.
                    311:  * All data in the output buffer is flushed prior to the newline
                    312:  * assertion.
                    313:  */
                    314: void
                    315: term_vspace(struct termp *p)
1.16      kristaps  316: {
                    317:
1.62      kristaps  318:        term_newln(p);
1.139     schwarze  319:        p->viscol = 0;
1.202     schwarze  320:        if (0 < p->skipvsp)
                    321:                p->skipvsp--;
                    322:        else
                    323:                (*p->endline)(p);
1.16      kristaps  324: }
                    325:
1.125     kristaps  326: void
                    327: term_fontlast(struct termp *p)
                    328: {
                    329:        enum termfont    f;
                    330:
                    331:        f = p->fontl;
                    332:        p->fontl = p->fontq[p->fonti];
                    333:        p->fontq[p->fonti] = f;
                    334: }
                    335:
                    336: void
                    337: term_fontrepl(struct termp *p, enum termfont f)
                    338: {
                    339:
                    340:        p->fontl = p->fontq[p->fonti];
                    341:        p->fontq[p->fonti] = f;
                    342: }
                    343:
                    344: void
                    345: term_fontpush(struct termp *p, enum termfont f)
                    346: {
                    347:
                    348:        assert(p->fonti + 1 < 10);
                    349:        p->fontl = p->fontq[p->fonti];
                    350:        p->fontq[++p->fonti] = f;
                    351: }
                    352:
                    353: const void *
                    354: term_fontq(struct termp *p)
                    355: {
                    356:
                    357:        return(&p->fontq[p->fonti]);
                    358: }
                    359:
                    360: enum termfont
                    361: term_fonttop(struct termp *p)
                    362: {
                    363:
                    364:        return(p->fontq[p->fonti]);
                    365: }
                    366:
                    367: void
                    368: term_fontpopq(struct termp *p, const void *key)
                    369: {
                    370:
1.206     schwarze  371:        while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125     kristaps  372:                p->fonti--;
                    373:        assert(p->fonti >= 0);
                    374: }
1.94      kristaps  375:
1.125     kristaps  376: void
                    377: term_fontpop(struct termp *p)
                    378: {
                    379:
                    380:        assert(p->fonti);
                    381:        p->fonti--;
1.17      kristaps  382: }
                    383:
1.71      kristaps  384: /*
                    385:  * Handle pwords, partial words, which may be either a single word or a
                    386:  * phrase that cannot be broken down (such as a literal string).  This
                    387:  * handles word styling.
                    388:  */
1.86      kristaps  389: void
                    390: term_word(struct termp *p, const char *word)
1.65      kristaps  391: {
1.214     schwarze  392:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  393:        const char      *seq, *cp;
1.194     kristaps  394:        int              sz, uc;
1.124     kristaps  395:        size_t           ssz;
1.184     kristaps  396:        enum mandoc_esc  esc;
1.100     kristaps  397:
1.133     kristaps  398:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  399:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  400:                        bufferc(p, ' ');
1.151     schwarze  401:                        if (TERMP_SENTENCE & p->flags)
                    402:                                bufferc(p, ' ');
                    403:                } else
                    404:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  405:        }
1.207     schwarze  406:        if (TERMP_PREKEEP & p->flags)
                    407:                p->flags |= TERMP_KEEP;
1.65      kristaps  408:
1.71      kristaps  409:        if ( ! (p->flags & TERMP_NONOSPACE))
                    410:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  411:        else
                    412:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  413:
1.213     schwarze  414:        p->flags &= ~TERMP_SENTENCE;
1.65      kristaps  415:
1.184     kristaps  416:        while ('\0' != *word) {
1.203     schwarze  417:                if ('\\' != *word) {
                    418:                        if (TERMP_SKIPCHAR & p->flags) {
                    419:                                p->flags &= ~TERMP_SKIPCHAR;
                    420:                                word++;
                    421:                                continue;
                    422:                        }
1.214     schwarze  423:                        if (TERMP_NBRWORD & p->flags) {
                    424:                                if (' ' == *word) {
                    425:                                        encode(p, nbrsp, 1);
                    426:                                        word++;
                    427:                                        continue;
                    428:                                }
                    429:                                ssz = strcspn(word, "\\ ");
                    430:                        } else
                    431:                                ssz = strcspn(word, "\\");
1.162     kristaps  432:                        encode(p, word, ssz);
1.203     schwarze  433:                        word += (int)ssz;
1.124     kristaps  434:                        continue;
1.203     schwarze  435:                }
1.124     kristaps  436:
1.184     kristaps  437:                word++;
                    438:                esc = mandoc_escape(&word, &seq, &sz);
                    439:                if (ESCAPE_ERROR == esc)
1.224     schwarze  440:                        continue;
1.124     kristaps  441:
1.184     kristaps  442:                switch (esc) {
1.222     schwarze  443:                case ESCAPE_UNICODE:
1.229     schwarze  444:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  445:                        break;
1.222     schwarze  446:                case ESCAPE_NUMBERED:
1.233   ! schwarze  447:                        uc = mchars_num2char(seq, sz);
        !           448:                        if (uc < 0)
        !           449:                                continue;
1.184     kristaps  450:                        break;
1.222     schwarze  451:                case ESCAPE_SPECIAL:
1.229     schwarze  452:                        if (p->enc == TERMENC_ASCII) {
                    453:                                cp = mchars_spec2str(p->symtab,
                    454:                                    seq, sz, &ssz);
1.232     schwarze  455:                                if (cp != NULL)
1.229     schwarze  456:                                        encode(p, cp, ssz);
                    457:                        } else {
                    458:                                uc = mchars_spec2cp(p->symtab, seq, sz);
1.230     schwarze  459:                                if (uc > 0)
                    460:                                        encode1(p, uc);
1.229     schwarze  461:                        }
1.233   ! schwarze  462:                        continue;
1.222     schwarze  463:                case ESCAPE_FONTBOLD:
1.125     kristaps  464:                        term_fontrepl(p, TERMFONT_BOLD);
1.233   ! schwarze  465:                        continue;
1.222     schwarze  466:                case ESCAPE_FONTITALIC:
1.125     kristaps  467:                        term_fontrepl(p, TERMFONT_UNDER);
1.233   ! schwarze  468:                        continue;
1.222     schwarze  469:                case ESCAPE_FONTBI:
1.209     schwarze  470:                        term_fontrepl(p, TERMFONT_BI);
1.233   ! schwarze  471:                        continue;
1.222     schwarze  472:                case ESCAPE_FONT:
1.195     kristaps  473:                        /* FALLTHROUGH */
1.222     schwarze  474:                case ESCAPE_FONTROMAN:
1.125     kristaps  475:                        term_fontrepl(p, TERMFONT_NONE);
1.233   ! schwarze  476:                        continue;
1.222     schwarze  477:                case ESCAPE_FONTPREV:
1.125     kristaps  478:                        term_fontlast(p);
1.233   ! schwarze  479:                        continue;
1.222     schwarze  480:                case ESCAPE_NOSPACE:
1.203     schwarze  481:                        if (TERMP_SKIPCHAR & p->flags)
                    482:                                p->flags &= ~TERMP_SKIPCHAR;
                    483:                        else if ('\0' == *word)
1.184     kristaps  484:                                p->flags |= TERMP_NOSPACE;
1.233   ! schwarze  485:                        continue;
1.222     schwarze  486:                case ESCAPE_SKIPCHAR:
1.203     schwarze  487:                        p->flags |= TERMP_SKIPCHAR;
1.233   ! schwarze  488:                        continue;
1.124     kristaps  489:                default:
1.233   ! schwarze  490:                        continue;
        !           491:                }
        !           492:
        !           493:                /*
        !           494:                 * Common handling for Unicode and numbered
        !           495:                 * character escape sequences.
        !           496:                 */
        !           497:
        !           498:                if (p->enc == TERMENC_ASCII) {
        !           499:                        cp = ascii_uc2str(uc);
        !           500:                        encode(p, cp, strlen(cp));
        !           501:                } else {
        !           502:                        if ((uc < 0x20 && uc != 0x09) ||
        !           503:                            (uc > 0x7E && uc < 0xA0))
        !           504:                                uc = 0xFFFD;
        !           505:                        encode1(p, uc);
1.124     kristaps  506:                }
                    507:        }
1.214     schwarze  508:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  509: }
                    510:
1.71      kristaps  511: static void
1.210     schwarze  512: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  513: {
                    514:
1.125     kristaps  515:        if (0 == p->maxcols)
                    516:                p->maxcols = 1024;
                    517:        while (sz >= p->maxcols)
                    518:                p->maxcols <<= 2;
                    519:
1.223     schwarze  520:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  521: }
                    522:
1.79      kristaps  523: static void
1.125     kristaps  524: bufferc(struct termp *p, char c)
                    525: {
                    526:
                    527:        if (p->col + 1 >= p->maxcols)
                    528:                adjbuf(p, p->col + 1);
                    529:
1.188     kristaps  530:        p->buf[p->col++] = c;
1.125     kristaps  531: }
                    532:
1.194     kristaps  533: /*
                    534:  * See encode().
                    535:  * Do this for a single (probably unicode) value.
                    536:  * Does not check for non-decorated glyphs.
                    537:  */
                    538: static void
                    539: encode1(struct termp *p, int c)
                    540: {
                    541:        enum termfont     f;
                    542:
1.203     schwarze  543:        if (TERMP_SKIPCHAR & p->flags) {
                    544:                p->flags &= ~TERMP_SKIPCHAR;
                    545:                return;
                    546:        }
                    547:
1.209     schwarze  548:        if (p->col + 6 >= p->maxcols)
                    549:                adjbuf(p, p->col + 6);
1.194     kristaps  550:
                    551:        f = term_fonttop(p);
                    552:
1.209     schwarze  553:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  554:                p->buf[p->col++] = '_';
1.209     schwarze  555:                p->buf[p->col++] = 8;
                    556:        }
                    557:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    558:                if (ASCII_HYPH == c)
                    559:                        p->buf[p->col++] = '-';
                    560:                else
                    561:                        p->buf[p->col++] = c;
                    562:                p->buf[p->col++] = 8;
                    563:        }
1.194     kristaps  564:        p->buf[p->col++] = c;
                    565: }
                    566:
1.125     kristaps  567: static void
                    568: encode(struct termp *p, const char *word, size_t sz)
                    569: {
1.210     schwarze  570:        size_t            i;
1.188     kristaps  571:
1.203     schwarze  572:        if (TERMP_SKIPCHAR & p->flags) {
                    573:                p->flags &= ~TERMP_SKIPCHAR;
                    574:                return;
                    575:        }
                    576:
1.125     kristaps  577:        /*
                    578:         * Encode and buffer a string of characters.  If the current
                    579:         * font mode is unset, buffer directly, else encode then buffer
                    580:         * character by character.
                    581:         */
                    582:
1.209     schwarze  583:        if (TERMFONT_NONE == term_fonttop(p)) {
1.222     schwarze  584:                if (p->col + sz >= p->maxcols)
1.210     schwarze  585:                        adjbuf(p, p->col + sz);
                    586:                for (i = 0; i < sz; i++)
1.188     kristaps  587:                        p->buf[p->col++] = word[i];
1.125     kristaps  588:                return;
                    589:        }
                    590:
1.165     kristaps  591:        /* Pre-buffer, assuming worst-case. */
                    592:
1.210     schwarze  593:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    594:                adjbuf(p, p->col + 1 + (sz * 5));
1.165     kristaps  595:
1.210     schwarze  596:        for (i = 0; i < sz; i++) {
1.209     schwarze  597:                if (ASCII_HYPH == word[i] ||
                    598:                    isgraph((unsigned char)word[i]))
                    599:                        encode1(p, word[i]);
1.125     kristaps  600:                else
1.188     kristaps  601:                        p->buf[p->col++] = word[i];
1.79      kristaps  602:        }
1.219     schwarze  603: }
                    604:
                    605: void
                    606: term_setwidth(struct termp *p, const char *wstr)
                    607: {
                    608:        struct roffsu    su;
                    609:        size_t           width;
                    610:        int              iop;
                    611:
1.220     schwarze  612:        iop = 0;
                    613:        width = 0;
1.219     schwarze  614:        if (NULL != wstr) {
                    615:                switch (*wstr) {
1.222     schwarze  616:                case '+':
1.219     schwarze  617:                        iop = 1;
                    618:                        wstr++;
                    619:                        break;
1.222     schwarze  620:                case '-':
1.219     schwarze  621:                        iop = -1;
                    622:                        wstr++;
                    623:                        break;
                    624:                default:
                    625:                        break;
                    626:                }
1.220     schwarze  627:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    628:                        width = term_hspan(p, &su);
                    629:                else
1.219     schwarze  630:                        iop = 0;
                    631:        }
                    632:        (*p->setwidth)(p, iop, width);
1.79      kristaps  633: }
1.106     kristaps  634:
1.107     kristaps  635: size_t
1.149     kristaps  636: term_len(const struct termp *p, size_t sz)
                    637: {
                    638:
                    639:        return((*p->width)(p, ' ') * sz);
                    640: }
                    641:
1.203     schwarze  642: static size_t
                    643: cond_width(const struct termp *p, int c, int *skip)
                    644: {
                    645:
                    646:        if (*skip) {
                    647:                (*skip) = 0;
                    648:                return(0);
                    649:        } else
                    650:                return((*p->width)(p, c));
                    651: }
1.149     kristaps  652:
                    653: size_t
                    654: term_strlen(const struct termp *p, const char *cp)
                    655: {
1.184     kristaps  656:        size_t           sz, rsz, i;
1.233   ! schwarze  657:        int              ssz, skip, uc;
1.171     kristaps  658:        const char      *seq, *rhs;
1.196     kristaps  659:        enum mandoc_esc  esc;
1.216     schwarze  660:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    661:                        ASCII_BREAK, '\0' };
1.171     kristaps  662:
1.184     kristaps  663:        /*
                    664:         * Account for escaped sequences within string length
                    665:         * calculations.  This follows the logic in term_word() as we
                    666:         * must calculate the width of produced strings.
                    667:         */
                    668:
                    669:        sz = 0;
1.203     schwarze  670:        skip = 0;
1.189     kristaps  671:        while ('\0' != *cp) {
                    672:                rsz = strcspn(cp, rej);
                    673:                for (i = 0; i < rsz; i++)
1.203     schwarze  674:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  675:
1.184     kristaps  676:                switch (*cp) {
1.222     schwarze  677:                case '\\':
1.189     kristaps  678:                        cp++;
1.196     kristaps  679:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    680:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  681:                                continue;
1.196     kristaps  682:
                    683:                        rhs = NULL;
                    684:
                    685:                        switch (esc) {
1.222     schwarze  686:                        case ESCAPE_UNICODE:
1.233   ! schwarze  687:                                uc = mchars_num2uc(seq + 1, sz - 1);
1.194     kristaps  688:                                break;
1.222     schwarze  689:                        case ESCAPE_NUMBERED:
1.233   ! schwarze  690:                                uc = mchars_num2char(seq, ssz);
        !           691:                                if (uc < 0)
        !           692:                                        continue;
1.171     kristaps  693:                                break;
1.222     schwarze  694:                        case ESCAPE_SPECIAL:
1.233   ! schwarze  695:                                if (p->enc == TERMENC_ASCII) {
1.229     schwarze  696:                                        rhs = mchars_spec2str(p->symtab,
                    697:                                            seq, ssz, &rsz);
1.233   ! schwarze  698:                                        if (rhs != NULL)
        !           699:                                                break;
        !           700:                                } else {
        !           701:                                        uc = mchars_spec2cp(p->symtab,
1.229     schwarze  702:                                            seq, ssz);
1.233   ! schwarze  703:                                        if (uc > 0)
        !           704:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  705:                                }
1.233   ! schwarze  706:                                continue;
1.222     schwarze  707:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  708:                                skip = 1;
1.233   ! schwarze  709:                                continue;
1.171     kristaps  710:                        default:
1.233   ! schwarze  711:                                continue;
1.171     kristaps  712:                        }
1.149     kristaps  713:
1.233   ! schwarze  714:                        /*
        !           715:                         * Common handling for Unicode and numbered
        !           716:                         * character escape sequences.
        !           717:                         */
        !           718:
        !           719:                        if (rhs == NULL) {
        !           720:                                if (p->enc == TERMENC_ASCII) {
        !           721:                                        rhs = ascii_uc2str(uc);
        !           722:                                        rsz = strlen(rhs);
        !           723:                                } else {
        !           724:                                        if ((uc < 0x20 && uc != 0x09) ||
        !           725:                                            (uc > 0x7E && uc < 0xA0))
        !           726:                                                uc = 0xFFFD;
        !           727:                                        sz += cond_width(p, uc, &skip);
        !           728:                                        continue;
        !           729:                                }
        !           730:                        }
1.184     kristaps  731:
1.203     schwarze  732:                        if (skip) {
                    733:                                skip = 0;
                    734:                                break;
                    735:                        }
1.233   ! schwarze  736:
        !           737:                        /*
        !           738:                         * Common handling for all escape sequences
        !           739:                         * printing more than one character.
        !           740:                         */
1.203     schwarze  741:
1.184     kristaps  742:                        for (i = 0; i < rsz; i++)
                    743:                                sz += (*p->width)(p, *rhs++);
                    744:                        break;
1.222     schwarze  745:                case ASCII_NBRSP:
1.203     schwarze  746:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  747:                        cp++;
1.184     kristaps  748:                        break;
1.222     schwarze  749:                case ASCII_HYPH:
1.203     schwarze  750:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  751:                        cp++;
1.216     schwarze  752:                        /* FALLTHROUGH */
1.222     schwarze  753:                case ASCII_BREAK:
1.184     kristaps  754:                        break;
                    755:                default:
                    756:                        break;
                    757:                }
1.189     kristaps  758:        }
1.149     kristaps  759:
                    760:        return(sz);
                    761: }
                    762:
                    763: size_t
                    764: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  765: {
                    766:        double           r;
                    767:
1.107     kristaps  768:        switch (su->unit) {
1.222     schwarze  769:        case SCALE_CM:
1.225     schwarze  770:                r = su->scale * 2.0;
1.106     kristaps  771:                break;
1.222     schwarze  772:        case SCALE_IN:
1.225     schwarze  773:                r = su->scale * 6.0;
1.106     kristaps  774:                break;
1.222     schwarze  775:        case SCALE_PC:
1.107     kristaps  776:                r = su->scale;
1.106     kristaps  777:                break;
1.222     schwarze  778:        case SCALE_PT:
1.225     schwarze  779:                r = su->scale / 8.0;
1.106     kristaps  780:                break;
1.222     schwarze  781:        case SCALE_MM:
1.225     schwarze  782:                r = su->scale / 1000.0;
1.106     kristaps  783:                break;
1.222     schwarze  784:        case SCALE_VS:
1.107     kristaps  785:                r = su->scale;
1.106     kristaps  786:                break;
                    787:        default:
1.225     schwarze  788:                r = su->scale - 1.0;
1.106     kristaps  789:                break;
                    790:        }
                    791:
                    792:        if (r < 0.0)
                    793:                r = 0.0;
1.226     schwarze  794:        return((size_t)(r + 0.0005));
1.106     kristaps  795: }
                    796:
1.107     kristaps  797: size_t
1.149     kristaps  798: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  799: {
1.156     kristaps  800:        double           v;
1.108     kristaps  801:
1.225     schwarze  802:        v = (*p->hspan)(p, su);
1.156     kristaps  803:        if (v < 0.0)
                    804:                v = 0.0;
1.226     schwarze  805:        return((size_t)(v + 0.0005));
1.106     kristaps  806: }

CVSweb