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

Annotation of mandoc/term.c, Revision 1.235

1.235   ! schwarze    1: /*     $Id: term.c,v 1.234 2014/11/01 04:03:49 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.235   ! schwarze  257:        if (vis > vbl)
1.200     schwarze  258:                vis -= vbl;
1.235   ! schwarze  259:        else
        !           260:                vis = 0;
1.111     kristaps  261:
1.91      kristaps  262:        p->col = 0;
1.129     kristaps  263:        p->overstep = 0;
1.15      kristaps  264:
1.91      kristaps  265:        if ( ! (TERMP_NOBREAK & p->flags)) {
1.139     schwarze  266:                p->viscol = 0;
1.146     kristaps  267:                (*p->endline)(p);
1.15      kristaps  268:                return;
1.71      kristaps  269:        }
1.15      kristaps  270:
1.91      kristaps  271:        if (TERMP_HANG & p->flags) {
1.211     schwarze  272:                p->overstep = (int)(vis - maxvis +
1.222     schwarze  273:                    p->trailspace * (*p->width)(p, ' '));
1.91      kristaps  274:
                    275:                /*
1.92      kristaps  276:                 * If we have overstepped the margin, temporarily move
                    277:                 * it to the right and flag the rest of the line to be
                    278:                 * shorter.
1.212     schwarze  279:                 * If there is a request to keep the columns together,
                    280:                 * allow negative overstep when the column is not full.
1.91      kristaps  281:                 */
1.212     schwarze  282:                if (p->trailspace && p->overstep < 0)
1.129     kristaps  283:                        p->overstep = 0;
1.200     schwarze  284:                return;
1.91      kristaps  285:
                    286:        } else if (TERMP_DANGLE & p->flags)
                    287:                return;
1.15      kristaps  288:
1.200     schwarze  289:        /* If the column was overrun, break the line. */
1.211     schwarze  290:        if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
1.146     kristaps  291:                (*p->endline)(p);
1.200     schwarze  292:                p->viscol = 0;
1.91      kristaps  293:        }
1.15      kristaps  294: }
                    295:
1.222     schwarze  296: /*
1.71      kristaps  297:  * A newline only breaks an existing line; it won't assert vertical
                    298:  * space.  All data in the output buffer is flushed prior to the newline
                    299:  * assertion.
                    300:  */
                    301: void
                    302: term_newln(struct termp *p)
1.15      kristaps  303: {
                    304:
1.71      kristaps  305:        p->flags |= TERMP_NOSPACE;
1.200     schwarze  306:        if (p->col || p->viscol)
                    307:                term_flushln(p);
1.16      kristaps  308: }
                    309:
1.71      kristaps  310: /*
                    311:  * Asserts a vertical space (a full, empty line-break between lines).
                    312:  * Note that if used twice, this will cause two blank spaces and so on.
                    313:  * All data in the output buffer is flushed prior to the newline
                    314:  * assertion.
                    315:  */
                    316: void
                    317: term_vspace(struct termp *p)
1.16      kristaps  318: {
                    319:
1.62      kristaps  320:        term_newln(p);
1.139     schwarze  321:        p->viscol = 0;
1.202     schwarze  322:        if (0 < p->skipvsp)
                    323:                p->skipvsp--;
                    324:        else
                    325:                (*p->endline)(p);
1.16      kristaps  326: }
                    327:
1.125     kristaps  328: void
                    329: term_fontlast(struct termp *p)
                    330: {
                    331:        enum termfont    f;
                    332:
                    333:        f = p->fontl;
                    334:        p->fontl = p->fontq[p->fonti];
                    335:        p->fontq[p->fonti] = f;
                    336: }
                    337:
                    338: void
                    339: term_fontrepl(struct termp *p, enum termfont f)
                    340: {
                    341:
                    342:        p->fontl = p->fontq[p->fonti];
                    343:        p->fontq[p->fonti] = f;
                    344: }
                    345:
                    346: void
                    347: term_fontpush(struct termp *p, enum termfont f)
                    348: {
                    349:
                    350:        assert(p->fonti + 1 < 10);
                    351:        p->fontl = p->fontq[p->fonti];
                    352:        p->fontq[++p->fonti] = f;
                    353: }
                    354:
                    355: const void *
                    356: term_fontq(struct termp *p)
                    357: {
                    358:
                    359:        return(&p->fontq[p->fonti]);
                    360: }
                    361:
                    362: enum termfont
                    363: term_fonttop(struct termp *p)
                    364: {
                    365:
                    366:        return(p->fontq[p->fonti]);
                    367: }
                    368:
                    369: void
                    370: term_fontpopq(struct termp *p, const void *key)
                    371: {
                    372:
1.206     schwarze  373:        while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
1.125     kristaps  374:                p->fonti--;
                    375:        assert(p->fonti >= 0);
                    376: }
1.94      kristaps  377:
1.125     kristaps  378: void
                    379: term_fontpop(struct termp *p)
                    380: {
                    381:
                    382:        assert(p->fonti);
                    383:        p->fonti--;
1.17      kristaps  384: }
                    385:
1.71      kristaps  386: /*
                    387:  * Handle pwords, partial words, which may be either a single word or a
                    388:  * phrase that cannot be broken down (such as a literal string).  This
                    389:  * handles word styling.
                    390:  */
1.86      kristaps  391: void
                    392: term_word(struct termp *p, const char *word)
1.65      kristaps  393: {
1.214     schwarze  394:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  395:        const char      *seq, *cp;
1.194     kristaps  396:        int              sz, uc;
1.124     kristaps  397:        size_t           ssz;
1.184     kristaps  398:        enum mandoc_esc  esc;
1.100     kristaps  399:
1.133     kristaps  400:        if ( ! (TERMP_NOSPACE & p->flags)) {
1.151     schwarze  401:                if ( ! (TERMP_KEEP & p->flags)) {
1.133     kristaps  402:                        bufferc(p, ' ');
1.151     schwarze  403:                        if (TERMP_SENTENCE & p->flags)
                    404:                                bufferc(p, ' ');
                    405:                } else
                    406:                        bufferc(p, ASCII_NBRSP);
1.133     kristaps  407:        }
1.207     schwarze  408:        if (TERMP_PREKEEP & p->flags)
                    409:                p->flags |= TERMP_KEEP;
1.65      kristaps  410:
1.71      kristaps  411:        if ( ! (p->flags & TERMP_NONOSPACE))
                    412:                p->flags &= ~TERMP_NOSPACE;
1.166     kristaps  413:        else
                    414:                p->flags |= TERMP_NOSPACE;
1.133     kristaps  415:
1.213     schwarze  416:        p->flags &= ~TERMP_SENTENCE;
1.65      kristaps  417:
1.184     kristaps  418:        while ('\0' != *word) {
1.203     schwarze  419:                if ('\\' != *word) {
                    420:                        if (TERMP_SKIPCHAR & p->flags) {
                    421:                                p->flags &= ~TERMP_SKIPCHAR;
                    422:                                word++;
                    423:                                continue;
                    424:                        }
1.214     schwarze  425:                        if (TERMP_NBRWORD & p->flags) {
                    426:                                if (' ' == *word) {
                    427:                                        encode(p, nbrsp, 1);
                    428:                                        word++;
                    429:                                        continue;
                    430:                                }
                    431:                                ssz = strcspn(word, "\\ ");
                    432:                        } else
                    433:                                ssz = strcspn(word, "\\");
1.162     kristaps  434:                        encode(p, word, ssz);
1.203     schwarze  435:                        word += (int)ssz;
1.124     kristaps  436:                        continue;
1.203     schwarze  437:                }
1.124     kristaps  438:
1.184     kristaps  439:                word++;
                    440:                esc = mandoc_escape(&word, &seq, &sz);
                    441:                if (ESCAPE_ERROR == esc)
1.224     schwarze  442:                        continue;
1.124     kristaps  443:
1.184     kristaps  444:                switch (esc) {
1.222     schwarze  445:                case ESCAPE_UNICODE:
1.229     schwarze  446:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  447:                        break;
1.222     schwarze  448:                case ESCAPE_NUMBERED:
1.233     schwarze  449:                        uc = mchars_num2char(seq, sz);
                    450:                        if (uc < 0)
                    451:                                continue;
1.184     kristaps  452:                        break;
1.222     schwarze  453:                case ESCAPE_SPECIAL:
1.229     schwarze  454:                        if (p->enc == TERMENC_ASCII) {
                    455:                                cp = mchars_spec2str(p->symtab,
                    456:                                    seq, sz, &ssz);
1.232     schwarze  457:                                if (cp != NULL)
1.229     schwarze  458:                                        encode(p, cp, ssz);
                    459:                        } else {
                    460:                                uc = mchars_spec2cp(p->symtab, seq, sz);
1.230     schwarze  461:                                if (uc > 0)
                    462:                                        encode1(p, uc);
1.229     schwarze  463:                        }
1.233     schwarze  464:                        continue;
1.222     schwarze  465:                case ESCAPE_FONTBOLD:
1.125     kristaps  466:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  467:                        continue;
1.222     schwarze  468:                case ESCAPE_FONTITALIC:
1.125     kristaps  469:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  470:                        continue;
1.222     schwarze  471:                case ESCAPE_FONTBI:
1.209     schwarze  472:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  473:                        continue;
1.222     schwarze  474:                case ESCAPE_FONT:
1.195     kristaps  475:                        /* FALLTHROUGH */
1.222     schwarze  476:                case ESCAPE_FONTROMAN:
1.125     kristaps  477:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  478:                        continue;
1.222     schwarze  479:                case ESCAPE_FONTPREV:
1.125     kristaps  480:                        term_fontlast(p);
1.233     schwarze  481:                        continue;
1.222     schwarze  482:                case ESCAPE_NOSPACE:
1.203     schwarze  483:                        if (TERMP_SKIPCHAR & p->flags)
                    484:                                p->flags &= ~TERMP_SKIPCHAR;
                    485:                        else if ('\0' == *word)
1.184     kristaps  486:                                p->flags |= TERMP_NOSPACE;
1.233     schwarze  487:                        continue;
1.222     schwarze  488:                case ESCAPE_SKIPCHAR:
1.203     schwarze  489:                        p->flags |= TERMP_SKIPCHAR;
1.233     schwarze  490:                        continue;
1.124     kristaps  491:                default:
1.233     schwarze  492:                        continue;
                    493:                }
                    494:
                    495:                /*
                    496:                 * Common handling for Unicode and numbered
                    497:                 * character escape sequences.
                    498:                 */
                    499:
                    500:                if (p->enc == TERMENC_ASCII) {
                    501:                        cp = ascii_uc2str(uc);
                    502:                        encode(p, cp, strlen(cp));
                    503:                } else {
                    504:                        if ((uc < 0x20 && uc != 0x09) ||
                    505:                            (uc > 0x7E && uc < 0xA0))
                    506:                                uc = 0xFFFD;
                    507:                        encode1(p, uc);
1.124     kristaps  508:                }
                    509:        }
1.214     schwarze  510:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  511: }
                    512:
1.71      kristaps  513: static void
1.210     schwarze  514: adjbuf(struct termp *p, size_t sz)
1.51      kristaps  515: {
                    516:
1.125     kristaps  517:        if (0 == p->maxcols)
                    518:                p->maxcols = 1024;
                    519:        while (sz >= p->maxcols)
                    520:                p->maxcols <<= 2;
                    521:
1.223     schwarze  522:        p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
1.51      kristaps  523: }
                    524:
1.79      kristaps  525: static void
1.125     kristaps  526: bufferc(struct termp *p, char c)
                    527: {
                    528:
                    529:        if (p->col + 1 >= p->maxcols)
                    530:                adjbuf(p, p->col + 1);
                    531:
1.188     kristaps  532:        p->buf[p->col++] = c;
1.125     kristaps  533: }
                    534:
1.194     kristaps  535: /*
                    536:  * See encode().
                    537:  * Do this for a single (probably unicode) value.
                    538:  * Does not check for non-decorated glyphs.
                    539:  */
                    540: static void
                    541: encode1(struct termp *p, int c)
                    542: {
                    543:        enum termfont     f;
                    544:
1.203     schwarze  545:        if (TERMP_SKIPCHAR & p->flags) {
                    546:                p->flags &= ~TERMP_SKIPCHAR;
                    547:                return;
                    548:        }
                    549:
1.209     schwarze  550:        if (p->col + 6 >= p->maxcols)
                    551:                adjbuf(p, p->col + 6);
1.194     kristaps  552:
                    553:        f = term_fonttop(p);
                    554:
1.209     schwarze  555:        if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
1.194     kristaps  556:                p->buf[p->col++] = '_';
1.209     schwarze  557:                p->buf[p->col++] = 8;
                    558:        }
                    559:        if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
                    560:                if (ASCII_HYPH == c)
                    561:                        p->buf[p->col++] = '-';
                    562:                else
                    563:                        p->buf[p->col++] = c;
                    564:                p->buf[p->col++] = 8;
                    565:        }
1.194     kristaps  566:        p->buf[p->col++] = c;
                    567: }
                    568:
1.125     kristaps  569: static void
                    570: encode(struct termp *p, const char *word, size_t sz)
                    571: {
1.210     schwarze  572:        size_t            i;
1.188     kristaps  573:
1.203     schwarze  574:        if (TERMP_SKIPCHAR & p->flags) {
                    575:                p->flags &= ~TERMP_SKIPCHAR;
                    576:                return;
                    577:        }
                    578:
1.125     kristaps  579:        /*
                    580:         * Encode and buffer a string of characters.  If the current
                    581:         * font mode is unset, buffer directly, else encode then buffer
                    582:         * character by character.
                    583:         */
                    584:
1.209     schwarze  585:        if (TERMFONT_NONE == term_fonttop(p)) {
1.222     schwarze  586:                if (p->col + sz >= p->maxcols)
1.210     schwarze  587:                        adjbuf(p, p->col + sz);
                    588:                for (i = 0; i < sz; i++)
1.188     kristaps  589:                        p->buf[p->col++] = word[i];
1.125     kristaps  590:                return;
                    591:        }
                    592:
1.165     kristaps  593:        /* Pre-buffer, assuming worst-case. */
                    594:
1.210     schwarze  595:        if (p->col + 1 + (sz * 5) >= p->maxcols)
                    596:                adjbuf(p, p->col + 1 + (sz * 5));
1.165     kristaps  597:
1.210     schwarze  598:        for (i = 0; i < sz; i++) {
1.209     schwarze  599:                if (ASCII_HYPH == word[i] ||
                    600:                    isgraph((unsigned char)word[i]))
                    601:                        encode1(p, word[i]);
1.125     kristaps  602:                else
1.188     kristaps  603:                        p->buf[p->col++] = word[i];
1.79      kristaps  604:        }
1.219     schwarze  605: }
                    606:
                    607: void
                    608: term_setwidth(struct termp *p, const char *wstr)
                    609: {
                    610:        struct roffsu    su;
                    611:        size_t           width;
                    612:        int              iop;
                    613:
1.220     schwarze  614:        iop = 0;
                    615:        width = 0;
1.219     schwarze  616:        if (NULL != wstr) {
                    617:                switch (*wstr) {
1.222     schwarze  618:                case '+':
1.219     schwarze  619:                        iop = 1;
                    620:                        wstr++;
                    621:                        break;
1.222     schwarze  622:                case '-':
1.219     schwarze  623:                        iop = -1;
                    624:                        wstr++;
                    625:                        break;
                    626:                default:
                    627:                        break;
                    628:                }
1.220     schwarze  629:                if (a2roffsu(wstr, &su, SCALE_MAX))
                    630:                        width = term_hspan(p, &su);
                    631:                else
1.219     schwarze  632:                        iop = 0;
                    633:        }
                    634:        (*p->setwidth)(p, iop, width);
1.79      kristaps  635: }
1.106     kristaps  636:
1.107     kristaps  637: size_t
1.149     kristaps  638: term_len(const struct termp *p, size_t sz)
                    639: {
                    640:
                    641:        return((*p->width)(p, ' ') * sz);
                    642: }
                    643:
1.203     schwarze  644: static size_t
                    645: cond_width(const struct termp *p, int c, int *skip)
                    646: {
                    647:
                    648:        if (*skip) {
                    649:                (*skip) = 0;
                    650:                return(0);
                    651:        } else
                    652:                return((*p->width)(p, c));
                    653: }
1.149     kristaps  654:
                    655: size_t
                    656: term_strlen(const struct termp *p, const char *cp)
                    657: {
1.184     kristaps  658:        size_t           sz, rsz, i;
1.233     schwarze  659:        int              ssz, skip, uc;
1.171     kristaps  660:        const char      *seq, *rhs;
1.196     kristaps  661:        enum mandoc_esc  esc;
1.216     schwarze  662:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    663:                        ASCII_BREAK, '\0' };
1.171     kristaps  664:
1.184     kristaps  665:        /*
                    666:         * Account for escaped sequences within string length
                    667:         * calculations.  This follows the logic in term_word() as we
                    668:         * must calculate the width of produced strings.
                    669:         */
                    670:
                    671:        sz = 0;
1.203     schwarze  672:        skip = 0;
1.189     kristaps  673:        while ('\0' != *cp) {
                    674:                rsz = strcspn(cp, rej);
                    675:                for (i = 0; i < rsz; i++)
1.203     schwarze  676:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  677:
1.184     kristaps  678:                switch (*cp) {
1.222     schwarze  679:                case '\\':
1.189     kristaps  680:                        cp++;
1.196     kristaps  681:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    682:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  683:                                continue;
1.196     kristaps  684:
                    685:                        rhs = NULL;
                    686:
                    687:                        switch (esc) {
1.222     schwarze  688:                        case ESCAPE_UNICODE:
1.234     schwarze  689:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  690:                                break;
1.222     schwarze  691:                        case ESCAPE_NUMBERED:
1.233     schwarze  692:                                uc = mchars_num2char(seq, ssz);
                    693:                                if (uc < 0)
                    694:                                        continue;
1.171     kristaps  695:                                break;
1.222     schwarze  696:                        case ESCAPE_SPECIAL:
1.233     schwarze  697:                                if (p->enc == TERMENC_ASCII) {
1.229     schwarze  698:                                        rhs = mchars_spec2str(p->symtab,
                    699:                                            seq, ssz, &rsz);
1.233     schwarze  700:                                        if (rhs != NULL)
                    701:                                                break;
                    702:                                } else {
                    703:                                        uc = mchars_spec2cp(p->symtab,
1.229     schwarze  704:                                            seq, ssz);
1.233     schwarze  705:                                        if (uc > 0)
                    706:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  707:                                }
1.233     schwarze  708:                                continue;
1.222     schwarze  709:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  710:                                skip = 1;
1.233     schwarze  711:                                continue;
1.171     kristaps  712:                        default:
1.233     schwarze  713:                                continue;
1.171     kristaps  714:                        }
1.149     kristaps  715:
1.233     schwarze  716:                        /*
                    717:                         * Common handling for Unicode and numbered
                    718:                         * character escape sequences.
                    719:                         */
                    720:
                    721:                        if (rhs == NULL) {
                    722:                                if (p->enc == TERMENC_ASCII) {
                    723:                                        rhs = ascii_uc2str(uc);
                    724:                                        rsz = strlen(rhs);
                    725:                                } else {
                    726:                                        if ((uc < 0x20 && uc != 0x09) ||
                    727:                                            (uc > 0x7E && uc < 0xA0))
                    728:                                                uc = 0xFFFD;
                    729:                                        sz += cond_width(p, uc, &skip);
                    730:                                        continue;
                    731:                                }
                    732:                        }
1.184     kristaps  733:
1.203     schwarze  734:                        if (skip) {
                    735:                                skip = 0;
                    736:                                break;
                    737:                        }
1.233     schwarze  738:
                    739:                        /*
                    740:                         * Common handling for all escape sequences
                    741:                         * printing more than one character.
                    742:                         */
1.203     schwarze  743:
1.184     kristaps  744:                        for (i = 0; i < rsz; i++)
                    745:                                sz += (*p->width)(p, *rhs++);
                    746:                        break;
1.222     schwarze  747:                case ASCII_NBRSP:
1.203     schwarze  748:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  749:                        cp++;
1.184     kristaps  750:                        break;
1.222     schwarze  751:                case ASCII_HYPH:
1.203     schwarze  752:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  753:                        cp++;
1.216     schwarze  754:                        /* FALLTHROUGH */
1.222     schwarze  755:                case ASCII_BREAK:
1.184     kristaps  756:                        break;
                    757:                default:
                    758:                        break;
                    759:                }
1.189     kristaps  760:        }
1.149     kristaps  761:
                    762:        return(sz);
                    763: }
                    764:
                    765: size_t
                    766: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  767: {
                    768:        double           r;
                    769:
1.107     kristaps  770:        switch (su->unit) {
1.222     schwarze  771:        case SCALE_CM:
1.225     schwarze  772:                r = su->scale * 2.0;
1.106     kristaps  773:                break;
1.222     schwarze  774:        case SCALE_IN:
1.225     schwarze  775:                r = su->scale * 6.0;
1.106     kristaps  776:                break;
1.222     schwarze  777:        case SCALE_PC:
1.107     kristaps  778:                r = su->scale;
1.106     kristaps  779:                break;
1.222     schwarze  780:        case SCALE_PT:
1.225     schwarze  781:                r = su->scale / 8.0;
1.106     kristaps  782:                break;
1.222     schwarze  783:        case SCALE_MM:
1.225     schwarze  784:                r = su->scale / 1000.0;
1.106     kristaps  785:                break;
1.222     schwarze  786:        case SCALE_VS:
1.107     kristaps  787:                r = su->scale;
1.106     kristaps  788:                break;
                    789:        default:
1.225     schwarze  790:                r = su->scale - 1.0;
1.106     kristaps  791:                break;
                    792:        }
                    793:
                    794:        if (r < 0.0)
                    795:                r = 0.0;
1.226     schwarze  796:        return((size_t)(r + 0.0005));
1.106     kristaps  797: }
                    798:
1.107     kristaps  799: size_t
1.149     kristaps  800: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  801: {
1.156     kristaps  802:        double           v;
1.108     kristaps  803:
1.225     schwarze  804:        v = (*p->hspan)(p, su);
1.156     kristaps  805:        if (v < 0.0)
                    806:                v = 0.0;
1.226     schwarze  807:        return((size_t)(v + 0.0005));
1.106     kristaps  808: }

CVSweb