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

Annotation of mandoc/term.c, Revision 1.269

1.269   ! schwarze    1: /*     $Id: term.c,v 1.268 2017/06/08 12:54:58 schwarze Exp $ */
1.1       kristaps    2: /*
1.198     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.259     schwarze    4:  * Copyright (c) 2010-2017 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.246     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.74      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.246     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.74      kristaps   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.266     schwarze   35: static void             adjbuf(struct termp_col *, 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.264     schwarze   39: static void             endline(struct termp *);
1.11      kristaps   40:
1.222     schwarze   41:
1.145     kristaps   42: void
1.269   ! schwarze   43: term_setcol(struct termp *p, size_t maxtcol)
        !            44: {
        !            45:        if (maxtcol > p->maxtcol) {
        !            46:                p->tcols = mandoc_recallocarray(p->tcols,
        !            47:                    p->maxtcol, maxtcol, sizeof(*p->tcols));
        !            48:                p->maxtcol = maxtcol;
        !            49:        }
        !            50:        p->lasttcol = maxtcol - 1;
        !            51:        p->tcol = p->tcols;
        !            52: }
        !            53:
        !            54: void
1.71      kristaps   55: term_free(struct termp *p)
1.14      kristaps   56: {
1.266     schwarze   57:        for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
                     58:                free(p->tcol->buf);
                     59:        free(p->tcols);
1.238     schwarze   60:        free(p->fontq);
1.142     kristaps   61:        free(p);
                     62: }
                     63:
                     64: void
1.222     schwarze   65: term_begin(struct termp *p, term_margin head,
1.246     schwarze   66:                term_margin foot, const struct roff_meta *arg)
1.142     kristaps   67: {
                     68:
                     69:        p->headf = head;
                     70:        p->footf = foot;
                     71:        p->argf = arg;
1.146     kristaps   72:        (*p->begin)(p);
1.142     kristaps   73: }
                     74:
                     75: void
                     76: term_end(struct termp *p)
                     77: {
                     78:
1.146     kristaps   79:        (*p->end)(p);
1.14      kristaps   80: }
                     81:
1.71      kristaps   82: /*
1.221     schwarze   83:  * Flush a chunk of text.  By default, break the output line each time
                     84:  * the right margin is reached, and continue output on the next line
                     85:  * at the same offset as the chunk itself.  By default, also break the
                     86:  * output line at the end of the chunk.
1.130     kristaps   87:  * The following flags may be specified:
1.71      kristaps   88:  *
1.221     schwarze   89:  *  - TERMP_NOBREAK: Do not break the output line at the right margin,
                     90:  *    but only at the max right margin.  Also, do not break the output
                     91:  *    line at the end of the chunk, such that the next call can pad to
                     92:  *    the next column.  However, if less than p->trailspace blanks,
                     93:  *    which can be 0, 1, or 2, remain to the right margin, the line
                     94:  *    will be broken.
1.250     schwarze   95:  *  - TERMP_BRTRSP: Consider trailing whitespace significant
                     96:  *    when deciding whether the chunk fits or not.
1.221     schwarze   97:  *  - TERMP_BRIND: If the chunk does not fit and the output line has
                     98:  *    to be broken, start the next line at the right margin instead
                     99:  *    of at the offset.  Used together with TERMP_NOBREAK for the tags
                    100:  *    in various kinds of tagged lists.
1.263     schwarze  101:  *  - TERMP_HANG: Do not break the output line at the right margin,
1.221     schwarze  102:  *    append the next chunk after it even if this one is too long.
                    103:  *    To be used together with TERMP_NOBREAK.
1.263     schwarze  104:  *  - TERMP_NOPAD: Start writing at the current position,
                    105:  *    do not pad with blank characters up to the offset.
1.71      kristaps  106:  */
                    107: void
                    108: term_flushln(struct termp *p)
1.53      kristaps  109: {
1.114     kristaps  110:        size_t           vis;   /* current visual position on output */
                    111:        size_t           vbl;   /* number of blanks to prepend to output */
1.136     schwarze  112:        size_t           vend;  /* end of word visual position on output */
1.114     kristaps  113:        size_t           bp;    /* visual right border position */
1.172     schwarze  114:        size_t           dv;    /* temporary for visual pos calculations */
1.266     schwarze  115:        size_t           j;     /* temporary loop index for p->tcol->buf */
1.210     schwarze  116:        size_t           jhy;   /* last hyph before overflow w/r/t j */
1.152     kristaps  117:        size_t           maxvis; /* output position of visible boundary */
1.266     schwarze  118:        int              ntab;  /* number of tabs to prepend */
1.53      kristaps  119:
1.266     schwarze  120:        vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ?
                    121:            0 : p->tcol->offset - p->viscol;
1.263     schwarze  122:        if (p->minbl && vbl < p->minbl)
                    123:                vbl = p->minbl;
1.266     schwarze  124:        maxvis = p->tcol->rmargin > p->viscol + vbl ?
                    125:            p->tcol->rmargin - p->viscol - vbl : 0;
1.263     schwarze  126:        bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
                    127:            p->maxrmargin > p->viscol + vbl ?
                    128:            p->maxrmargin - p->viscol - vbl : 0;
1.174     schwarze  129:        vis = vend = 0;
1.115     kristaps  130:
1.269   ! schwarze  131:        if ((p->flags && TERMP_MULTICOL) == 0)
1.267     schwarze  132:                p->tcol->col = 0;
1.269   ! schwarze  133:        while (p->tcol->col < p->tcol->lastcol) {
1.267     schwarze  134:
1.71      kristaps  135:                /*
1.154     kristaps  136:                 * Handle literal tab characters: collapse all
                    137:                 * subsequent tabs into a single huge set of spaces.
1.138     schwarze  138:                 */
1.267     schwarze  139:
1.205     schwarze  140:                ntab = 0;
1.269   ! schwarze  141:                while (p->tcol->col < p->tcol->lastcol &&
1.267     schwarze  142:                    p->tcol->buf[p->tcol->col] == '\t') {
1.260     schwarze  143:                        vend = term_tab_next(vis);
1.138     schwarze  144:                        vbl += vend - vis;
                    145:                        vis = vend;
1.205     schwarze  146:                        ntab++;
1.267     schwarze  147:                        p->tcol->col++;
1.138     schwarze  148:                }
                    149:
                    150:                /*
1.71      kristaps  151:                 * Count up visible word characters.  Control sequences
                    152:                 * (starting with the CSI) aren't counted.  A space
                    153:                 * generates a non-printing word, which is valid (the
                    154:                 * space is printed according to regular spacing rules).
                    155:                 */
                    156:
1.267     schwarze  157:                jhy = 0;
1.269   ! schwarze  158:                for (j = p->tcol->col; j < p->tcol->lastcol; j++) {
1.266     schwarze  159:                        if (p->tcol->buf[j] == ' ' || p->tcol->buf[j] == '\t')
1.71      kristaps  160:                                break;
1.154     kristaps  161:
1.257     schwarze  162:                        /* Back over the last printed character. */
1.266     schwarze  163:                        if (p->tcol->buf[j] == '\b') {
1.153     kristaps  164:                                assert(j);
1.266     schwarze  165:                                vend -= (*p->width)(p, p->tcol->buf[j - 1]);
1.154     kristaps  166:                                continue;
1.153     kristaps  167:                        }
1.154     kristaps  168:
                    169:                        /* Regular word. */
                    170:                        /* Break at the hyphen point if we overrun. */
1.222     schwarze  171:                        if (vend > vis && vend < bp &&
1.266     schwarze  172:                            (p->tcol->buf[j] == ASCII_HYPH||
                    173:                             p->tcol->buf[j] == ASCII_BREAK))
1.154     kristaps  174:                                jhy = j;
                    175:
1.217     schwarze  176:                        /*
                    177:                         * Hyphenation now decided, put back a real
                    178:                         * hyphen such that we get the correct width.
                    179:                         */
1.266     schwarze  180:                        if (p->tcol->buf[j] == ASCII_HYPH)
                    181:                                p->tcol->buf[j] = '-';
1.217     schwarze  182:
1.266     schwarze  183:                        vend += (*p->width)(p, p->tcol->buf[j]);
1.71      kristaps  184:                }
1.53      kristaps  185:
1.71      kristaps  186:                /*
1.81      kristaps  187:                 * Find out whether we would exceed the right margin.
1.136     schwarze  188:                 * If so, break to the next line.
1.81      kristaps  189:                 */
1.267     schwarze  190:
                    191:                if (vend > bp && jhy == 0 && vis > 0 &&
1.264     schwarze  192:                    (p->flags & TERMP_BRNEVER) == 0) {
1.269   ! schwarze  193:                        if (p->flags & TERMP_MULTICOL)
1.267     schwarze  194:                                return;
                    195:
                    196:                        endline(p);
1.136     schwarze  197:                        vend -= vis;
1.205     schwarze  198:
1.260     schwarze  199:                        /* Use pending tabs on the new line. */
                    200:
                    201:                        vbl = 0;
                    202:                        while (ntab--)
                    203:                                vbl = term_tab_next(vbl);
                    204:
                    205:                        /* Re-establish indentation. */
1.205     schwarze  206:
1.263     schwarze  207:                        if (p->flags & TERMP_BRIND)
1.266     schwarze  208:                                vbl += p->tcol->rmargin;
1.263     schwarze  209:                        else
1.266     schwarze  210:                                vbl += p->tcol->offset;
                    211:                        maxvis = p->tcol->rmargin > vbl ?
                    212:                            p->tcol->rmargin - vbl : 0;
1.263     schwarze  213:                        bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
                    214:                            p->maxrmargin > vbl ?  p->maxrmargin - vbl : 0;
1.71      kristaps  215:                }
1.138     schwarze  216:
1.267     schwarze  217:                /*
                    218:                 * Write out the rest of the word.
                    219:                 */
                    220:
1.269   ! schwarze  221:                for ( ; p->tcol->col < p->tcol->lastcol; p->tcol->col++) {
1.267     schwarze  222:                        if (vend > bp && jhy > 0 && p->tcol->col > jhy)
1.140     kristaps  223:                                break;
1.267     schwarze  224:                        if (p->tcol->buf[p->tcol->col] == '\t')
1.138     schwarze  225:                                break;
1.267     schwarze  226:                        if (p->tcol->buf[p->tcol->col] == ' ') {
                    227:                                j = p->tcol->col;
1.269   ! schwarze  228:                                while (p->tcol->col < p->tcol->lastcol &&
1.267     schwarze  229:                                    p->tcol->buf[p->tcol->col] == ' ')
                    230:                                        p->tcol->col++;
                    231:                                dv = (p->tcol->col - j) * (*p->width)(p, ' ');
1.172     schwarze  232:                                vbl += dv;
                    233:                                vend += dv;
1.71      kristaps  234:                                break;
1.136     schwarze  235:                        }
1.267     schwarze  236:                        if (p->tcol->buf[p->tcol->col] == ASCII_NBRSP) {
1.153     kristaps  237:                                vbl += (*p->width)(p, ' ');
1.136     schwarze  238:                                continue;
                    239:                        }
1.267     schwarze  240:                        if (p->tcol->buf[p->tcol->col] == ASCII_BREAK)
1.216     schwarze  241:                                continue;
1.130     kristaps  242:
1.136     schwarze  243:                        /*
                    244:                         * Now we definitely know there will be
                    245:                         * printable characters to output,
                    246:                         * so write preceding white space now.
                    247:                         */
                    248:                        if (vbl) {
1.146     kristaps  249:                                (*p->advance)(p, vbl);
1.139     schwarze  250:                                p->viscol += vbl;
1.136     schwarze  251:                                vbl = 0;
1.200     schwarze  252:                        }
                    253:
1.267     schwarze  254:                        (*p->letter)(p, p->tcol->buf[p->tcol->col]);
                    255:                        if (p->tcol->buf[p->tcol->col] == '\b')
                    256:                                p->viscol -= (*p->width)(p,
                    257:                                    p->tcol->buf[p->tcol->col - 1]);
1.222     schwarze  258:                        else
1.267     schwarze  259:                                p->viscol += (*p->width)(p,
                    260:                                    p->tcol->buf[p->tcol->col]);
1.136     schwarze  261:                }
                    262:                vis = vend;
1.71      kristaps  263:        }
1.168     schwarze  264:
                    265:        /*
                    266:         * If there was trailing white space, it was not printed;
                    267:         * so reset the cursor position accordingly.
                    268:         */
1.267     schwarze  269:
1.235     schwarze  270:        if (vis > vbl)
1.200     schwarze  271:                vis -= vbl;
1.235     schwarze  272:        else
                    273:                vis = 0;
1.111     kristaps  274:
1.269   ! schwarze  275:        p->col = p->tcol->col = p->tcol->lastcol = 0;
1.264     schwarze  276:        p->minbl = p->trailspace;
1.263     schwarze  277:        p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
1.15      kristaps  278:
1.269   ! schwarze  279:        if (p->flags & TERMP_MULTICOL)
        !           280:                return;
        !           281:
1.250     schwarze  282:        /* Trailing whitespace is significant in some columns. */
1.267     schwarze  283:
1.250     schwarze  284:        if (vis && vbl && (TERMP_BRTRSP & p->flags))
                    285:                vis += vbl;
1.15      kristaps  286:
1.200     schwarze  287:        /* If the column was overrun, break the line. */
1.264     schwarze  288:        if ((p->flags & TERMP_NOBREAK) == 0 ||
                    289:            ((p->flags & TERMP_HANG) == 0 &&
                    290:             vis + p->trailspace * (*p->width)(p, ' ') > maxvis))
                    291:                endline(p);
                    292: }
                    293:
                    294: static void
                    295: endline(struct termp *p)
                    296: {
                    297:        if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
                    298:                p->mc = NULL;
                    299:                p->flags &= ~TERMP_ENDMC;
                    300:        }
                    301:        if (p->mc != NULL) {
                    302:                if (p->viscol && p->maxrmargin >= p->viscol)
                    303:                        (*p->advance)(p, p->maxrmargin - p->viscol + 1);
                    304:                p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
                    305:                term_word(p, p->mc);
                    306:                p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
                    307:        }
                    308:        p->viscol = 0;
                    309:        p->minbl = 0;
                    310:        (*p->endline)(p);
1.15      kristaps  311: }
                    312:
1.222     schwarze  313: /*
1.71      kristaps  314:  * A newline only breaks an existing line; it won't assert vertical
                    315:  * space.  All data in the output buffer is flushed prior to the newline
                    316:  * assertion.
                    317:  */
                    318: void
                    319: term_newln(struct termp *p)
1.15      kristaps  320: {
                    321:
1.71      kristaps  322:        p->flags |= TERMP_NOSPACE;
1.269   ! schwarze  323:        if (p->tcol->lastcol || p->viscol)
1.200     schwarze  324:                term_flushln(p);
1.16      kristaps  325: }
                    326:
1.71      kristaps  327: /*
                    328:  * Asserts a vertical space (a full, empty line-break between lines).
                    329:  * Note that if used twice, this will cause two blank spaces and so on.
                    330:  * All data in the output buffer is flushed prior to the newline
                    331:  * assertion.
                    332:  */
                    333: void
                    334: term_vspace(struct termp *p)
1.16      kristaps  335: {
                    336:
1.62      kristaps  337:        term_newln(p);
1.139     schwarze  338:        p->viscol = 0;
1.264     schwarze  339:        p->minbl = 0;
1.202     schwarze  340:        if (0 < p->skipvsp)
                    341:                p->skipvsp--;
                    342:        else
                    343:                (*p->endline)(p);
1.16      kristaps  344: }
                    345:
1.238     schwarze  346: /* Swap current and previous font; for \fP and .ft P */
1.125     kristaps  347: void
                    348: term_fontlast(struct termp *p)
                    349: {
                    350:        enum termfont    f;
                    351:
                    352:        f = p->fontl;
                    353:        p->fontl = p->fontq[p->fonti];
                    354:        p->fontq[p->fonti] = f;
                    355: }
                    356:
1.238     schwarze  357: /* Set font, save current, discard previous; for \f, .ft, .B etc. */
1.125     kristaps  358: void
                    359: term_fontrepl(struct termp *p, enum termfont f)
                    360: {
                    361:
                    362:        p->fontl = p->fontq[p->fonti];
                    363:        p->fontq[p->fonti] = f;
                    364: }
                    365:
1.238     schwarze  366: /* Set font, save previous. */
1.125     kristaps  367: void
                    368: term_fontpush(struct termp *p, enum termfont f)
                    369: {
                    370:
                    371:        p->fontl = p->fontq[p->fonti];
1.238     schwarze  372:        if (++p->fonti == p->fontsz) {
                    373:                p->fontsz += 8;
                    374:                p->fontq = mandoc_reallocarray(p->fontq,
1.256     schwarze  375:                    p->fontsz, sizeof(*p->fontq));
1.238     schwarze  376:        }
                    377:        p->fontq[p->fonti] = f;
1.125     kristaps  378: }
                    379:
1.238     schwarze  380: /* Flush to make the saved pointer current again. */
1.125     kristaps  381: void
1.244     schwarze  382: term_fontpopq(struct termp *p, int i)
1.125     kristaps  383: {
                    384:
1.244     schwarze  385:        assert(i >= 0);
                    386:        if (p->fonti > i)
                    387:                p->fonti = i;
1.125     kristaps  388: }
1.94      kristaps  389:
1.238     schwarze  390: /* Pop one font off the stack. */
1.125     kristaps  391: void
                    392: term_fontpop(struct termp *p)
                    393: {
                    394:
                    395:        assert(p->fonti);
                    396:        p->fonti--;
1.17      kristaps  397: }
                    398:
1.71      kristaps  399: /*
                    400:  * Handle pwords, partial words, which may be either a single word or a
                    401:  * phrase that cannot be broken down (such as a literal string).  This
                    402:  * handles word styling.
                    403:  */
1.86      kristaps  404: void
                    405: term_word(struct termp *p, const char *word)
1.65      kristaps  406: {
1.261     schwarze  407:        struct roffsu    su;
1.214     schwarze  408:        const char       nbrsp[2] = { ASCII_NBRSP, 0 };
1.191     kristaps  409:        const char      *seq, *cp;
1.194     kristaps  410:        int              sz, uc;
1.262     schwarze  411:        size_t           csz, lsz, ssz;
1.184     kristaps  412:        enum mandoc_esc  esc;
1.100     kristaps  413:
1.264     schwarze  414:        if ((p->flags & TERMP_NOBUF) == 0) {
                    415:                if ((p->flags & TERMP_NOSPACE) == 0) {
                    416:                        if ((p->flags & TERMP_KEEP) == 0) {
1.151     schwarze  417:                                bufferc(p, ' ');
1.264     schwarze  418:                                if (p->flags & TERMP_SENTENCE)
                    419:                                        bufferc(p, ' ');
                    420:                        } else
                    421:                                bufferc(p, ASCII_NBRSP);
                    422:                }
                    423:                if (p->flags & TERMP_PREKEEP)
                    424:                        p->flags |= TERMP_KEEP;
                    425:                if (p->flags & TERMP_NONOSPACE)
                    426:                        p->flags |= TERMP_NOSPACE;
                    427:                else
                    428:                        p->flags &= ~TERMP_NOSPACE;
                    429:                p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
                    430:                p->skipvsp = 0;
1.133     kristaps  431:        }
1.65      kristaps  432:
1.184     kristaps  433:        while ('\0' != *word) {
1.203     schwarze  434:                if ('\\' != *word) {
1.214     schwarze  435:                        if (TERMP_NBRWORD & p->flags) {
                    436:                                if (' ' == *word) {
                    437:                                        encode(p, nbrsp, 1);
                    438:                                        word++;
                    439:                                        continue;
                    440:                                }
                    441:                                ssz = strcspn(word, "\\ ");
                    442:                        } else
                    443:                                ssz = strcspn(word, "\\");
1.162     kristaps  444:                        encode(p, word, ssz);
1.203     schwarze  445:                        word += (int)ssz;
1.124     kristaps  446:                        continue;
1.203     schwarze  447:                }
1.124     kristaps  448:
1.184     kristaps  449:                word++;
                    450:                esc = mandoc_escape(&word, &seq, &sz);
                    451:                if (ESCAPE_ERROR == esc)
1.224     schwarze  452:                        continue;
1.124     kristaps  453:
1.184     kristaps  454:                switch (esc) {
1.222     schwarze  455:                case ESCAPE_UNICODE:
1.229     schwarze  456:                        uc = mchars_num2uc(seq + 1, sz - 1);
1.192     kristaps  457:                        break;
1.222     schwarze  458:                case ESCAPE_NUMBERED:
1.233     schwarze  459:                        uc = mchars_num2char(seq, sz);
                    460:                        if (uc < 0)
                    461:                                continue;
1.184     kristaps  462:                        break;
1.222     schwarze  463:                case ESCAPE_SPECIAL:
1.229     schwarze  464:                        if (p->enc == TERMENC_ASCII) {
1.254     schwarze  465:                                cp = mchars_spec2str(seq, sz, &ssz);
1.232     schwarze  466:                                if (cp != NULL)
1.229     schwarze  467:                                        encode(p, cp, ssz);
                    468:                        } else {
1.254     schwarze  469:                                uc = mchars_spec2cp(seq, sz);
1.230     schwarze  470:                                if (uc > 0)
                    471:                                        encode1(p, uc);
1.229     schwarze  472:                        }
1.233     schwarze  473:                        continue;
1.222     schwarze  474:                case ESCAPE_FONTBOLD:
1.125     kristaps  475:                        term_fontrepl(p, TERMFONT_BOLD);
1.233     schwarze  476:                        continue;
1.222     schwarze  477:                case ESCAPE_FONTITALIC:
1.125     kristaps  478:                        term_fontrepl(p, TERMFONT_UNDER);
1.233     schwarze  479:                        continue;
1.222     schwarze  480:                case ESCAPE_FONTBI:
1.209     schwarze  481:                        term_fontrepl(p, TERMFONT_BI);
1.233     schwarze  482:                        continue;
1.222     schwarze  483:                case ESCAPE_FONT:
                    484:                case ESCAPE_FONTROMAN:
1.125     kristaps  485:                        term_fontrepl(p, TERMFONT_NONE);
1.233     schwarze  486:                        continue;
1.222     schwarze  487:                case ESCAPE_FONTPREV:
1.125     kristaps  488:                        term_fontlast(p);
1.233     schwarze  489:                        continue;
1.222     schwarze  490:                case ESCAPE_NOSPACE:
1.248     schwarze  491:                        if (p->flags & TERMP_BACKAFTER)
                    492:                                p->flags &= ~TERMP_BACKAFTER;
                    493:                        else if (*word == '\0')
1.237     schwarze  494:                                p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
1.261     schwarze  495:                        continue;
                    496:                case ESCAPE_HORIZ:
1.268     schwarze  497:                        if (a2roffsu(seq, &su, SCALE_EM) == NULL)
1.261     schwarze  498:                                continue;
                    499:                        uc = term_hspan(p, &su) / 24;
                    500:                        if (uc > 0)
                    501:                                while (uc-- > 0)
                    502:                                        bufferc(p, ASCII_NBRSP);
                    503:                        else if (p->col > (size_t)(-uc))
                    504:                                p->col += uc;
                    505:                        else {
                    506:                                uc += p->col;
                    507:                                p->col = 0;
1.266     schwarze  508:                                if (p->tcol->offset > (size_t)(-uc)) {
1.261     schwarze  509:                                        p->ti += uc;
1.266     schwarze  510:                                        p->tcol->offset += uc;
1.261     schwarze  511:                                } else {
1.266     schwarze  512:                                        p->ti -= p->tcol->offset;
                    513:                                        p->tcol->offset = 0;
1.261     schwarze  514:                                }
1.262     schwarze  515:                        }
                    516:                        continue;
                    517:                case ESCAPE_HLINE:
1.268     schwarze  518:                        if ((seq = a2roffsu(seq, &su, SCALE_EM)) == NULL)
1.262     schwarze  519:                                continue;
                    520:                        uc = term_hspan(p, &su) / 24;
                    521:                        if (uc <= 0) {
1.266     schwarze  522:                                if (p->tcol->rmargin <= p->tcol->offset)
1.262     schwarze  523:                                        continue;
1.266     schwarze  524:                                lsz = p->tcol->rmargin - p->tcol->offset;
1.262     schwarze  525:                        } else
                    526:                                lsz = uc;
1.268     schwarze  527:                        if (*seq == '\0')
1.262     schwarze  528:                                uc = -1;
                    529:                        else if (*seq == '\\') {
                    530:                                seq++;
                    531:                                esc = mandoc_escape(&seq, &cp, &sz);
                    532:                                switch (esc) {
                    533:                                case ESCAPE_UNICODE:
                    534:                                        uc = mchars_num2uc(cp + 1, sz - 1);
                    535:                                        break;
                    536:                                case ESCAPE_NUMBERED:
                    537:                                        uc = mchars_num2char(cp, sz);
                    538:                                        break;
                    539:                                case ESCAPE_SPECIAL:
                    540:                                        uc = mchars_spec2cp(cp, sz);
                    541:                                        break;
                    542:                                default:
                    543:                                        uc = -1;
                    544:                                        break;
                    545:                                }
                    546:                        } else
                    547:                                uc = *seq;
                    548:                        if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
                    549:                                uc = '_';
                    550:                        if (p->enc == TERMENC_ASCII) {
                    551:                                cp = ascii_uc2str(uc);
                    552:                                csz = term_strlen(p, cp);
                    553:                                ssz = strlen(cp);
                    554:                        } else
                    555:                                csz = (*p->width)(p, uc);
                    556:                        while (lsz >= csz) {
                    557:                                if (p->enc == TERMENC_ASCII)
                    558:                                        encode(p, cp, ssz);
                    559:                                else
                    560:                                        encode1(p, uc);
                    561:                                lsz -= csz;
1.261     schwarze  562:                        }
1.233     schwarze  563:                        continue;
1.222     schwarze  564:                case ESCAPE_SKIPCHAR:
1.248     schwarze  565:                        p->flags |= TERMP_BACKAFTER;
1.233     schwarze  566:                        continue;
1.243     schwarze  567:                case ESCAPE_OVERSTRIKE:
                    568:                        cp = seq + sz;
                    569:                        while (seq < cp) {
                    570:                                if (*seq == '\\') {
                    571:                                        mandoc_escape(&seq, NULL, NULL);
                    572:                                        continue;
                    573:                                }
                    574:                                encode1(p, *seq++);
1.248     schwarze  575:                                if (seq < cp) {
                    576:                                        if (p->flags & TERMP_BACKBEFORE)
                    577:                                                p->flags |= TERMP_BACKAFTER;
                    578:                                        else
                    579:                                                p->flags |= TERMP_BACKBEFORE;
                    580:                                }
1.243     schwarze  581:                        }
1.249     schwarze  582:                        /* Trim trailing backspace/blank pair. */
1.269   ! schwarze  583:                        if (p->tcol->lastcol > 2 &&
        !           584:                            (p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
        !           585:                             p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
        !           586:                                p->tcol->lastcol -= 2;
        !           587:                        if (p->col > p->tcol->lastcol)
        !           588:                                p->col = p->tcol->lastcol;
1.248     schwarze  589:                        continue;
1.124     kristaps  590:                default:
1.233     schwarze  591:                        continue;
                    592:                }
                    593:
                    594:                /*
                    595:                 * Common handling for Unicode and numbered
                    596:                 * character escape sequences.
                    597:                 */
                    598:
                    599:                if (p->enc == TERMENC_ASCII) {
                    600:                        cp = ascii_uc2str(uc);
                    601:                        encode(p, cp, strlen(cp));
                    602:                } else {
                    603:                        if ((uc < 0x20 && uc != 0x09) ||
                    604:                            (uc > 0x7E && uc < 0xA0))
                    605:                                uc = 0xFFFD;
                    606:                        encode1(p, uc);
1.124     kristaps  607:                }
                    608:        }
1.214     schwarze  609:        p->flags &= ~TERMP_NBRWORD;
1.65      kristaps  610: }
                    611:
1.71      kristaps  612: static void
1.266     schwarze  613: adjbuf(struct termp_col *c, size_t sz)
1.51      kristaps  614: {
1.266     schwarze  615:        if (c->maxcols == 0)
                    616:                c->maxcols = 1024;
                    617:        while (c->maxcols <= sz)
                    618:                c->maxcols <<= 2;
                    619:        c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
1.51      kristaps  620: }
                    621:
1.79      kristaps  622: static void
1.125     kristaps  623: bufferc(struct termp *p, char c)
                    624: {
1.264     schwarze  625:        if (p->flags & TERMP_NOBUF) {
                    626:                (*p->letter)(p, c);
                    627:                return;
                    628:        }
1.266     schwarze  629:        if (p->col + 1 >= p->tcol->maxcols)
                    630:                adjbuf(p->tcol, p->col + 1);
1.269   ! schwarze  631:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.266     schwarze  632:                p->tcol->buf[p->col] = c;
1.269   ! schwarze  633:        if (p->tcol->lastcol < ++p->col)
        !           634:                p->tcol->lastcol = p->col;
1.125     kristaps  635: }
                    636:
1.194     kristaps  637: /*
                    638:  * See encode().
                    639:  * Do this for a single (probably unicode) value.
                    640:  * Does not check for non-decorated glyphs.
                    641:  */
                    642: static void
                    643: encode1(struct termp *p, int c)
                    644: {
                    645:        enum termfont     f;
                    646:
1.264     schwarze  647:        if (p->flags & TERMP_NOBUF) {
                    648:                (*p->letter)(p, c);
                    649:                return;
                    650:        }
                    651:
1.266     schwarze  652:        if (p->col + 7 >= p->tcol->maxcols)
                    653:                adjbuf(p->tcol, p->col + 7);
1.194     kristaps  654:
1.255     schwarze  655:        f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
1.248     schwarze  656:            p->fontq[p->fonti] : TERMFONT_NONE;
1.194     kristaps  657:
1.248     schwarze  658:        if (p->flags & TERMP_BACKBEFORE) {
1.266     schwarze  659:                if (p->tcol->buf[p->col - 1] == ' ' ||
                    660:                    p->tcol->buf[p->col - 1] == '\t')
1.249     schwarze  661:                        p->col--;
                    662:                else
1.266     schwarze  663:                        p->tcol->buf[p->col++] = '\b';
1.248     schwarze  664:                p->flags &= ~TERMP_BACKBEFORE;
                    665:        }
1.266     schwarze  666:        if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
                    667:                p->tcol->buf[p->col++] = '_';
                    668:                p->tcol->buf[p->col++] = '\b';
                    669:        }
                    670:        if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
                    671:                if (c == ASCII_HYPH)
                    672:                        p->tcol->buf[p->col++] = '-';
1.209     schwarze  673:                else
1.266     schwarze  674:                        p->tcol->buf[p->col++] = c;
                    675:                p->tcol->buf[p->col++] = '\b';
1.209     schwarze  676:        }
1.269   ! schwarze  677:        if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
1.266     schwarze  678:                p->tcol->buf[p->col] = c;
1.269   ! schwarze  679:        if (p->tcol->lastcol < ++p->col)
        !           680:                p->tcol->lastcol = p->col;
1.248     schwarze  681:        if (p->flags & TERMP_BACKAFTER) {
                    682:                p->flags |= TERMP_BACKBEFORE;
                    683:                p->flags &= ~TERMP_BACKAFTER;
                    684:        }
1.194     kristaps  685: }
                    686:
1.125     kristaps  687: static void
                    688: encode(struct termp *p, const char *word, size_t sz)
                    689: {
1.210     schwarze  690:        size_t            i;
1.264     schwarze  691:
                    692:        if (p->flags & TERMP_NOBUF) {
                    693:                for (i = 0; i < sz; i++)
                    694:                        (*p->letter)(p, word[i]);
                    695:                return;
                    696:        }
1.188     kristaps  697:
1.266     schwarze  698:        if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
                    699:                adjbuf(p->tcol, p->col + 2 + (sz * 5));
1.165     kristaps  700:
1.210     schwarze  701:        for (i = 0; i < sz; i++) {
1.209     schwarze  702:                if (ASCII_HYPH == word[i] ||
                    703:                    isgraph((unsigned char)word[i]))
                    704:                        encode1(p, word[i]);
1.259     schwarze  705:                else {
1.269   ! schwarze  706:                        if (p->tcol->lastcol <= p->col ||
1.265     schwarze  707:                            (word[i] != ' ' && word[i] != ASCII_NBRSP))
1.266     schwarze  708:                                p->tcol->buf[p->col] = word[i];
1.265     schwarze  709:                        p->col++;
1.259     schwarze  710:
                    711:                        /*
                    712:                         * Postpone the effect of \z while handling
                    713:                         * an overstrike sequence from ascii_uc2str().
                    714:                         */
                    715:
                    716:                        if (word[i] == '\b' &&
                    717:                            (p->flags & TERMP_BACKBEFORE)) {
                    718:                                p->flags &= ~TERMP_BACKBEFORE;
                    719:                                p->flags |= TERMP_BACKAFTER;
                    720:                        }
                    721:                }
1.79      kristaps  722:        }
1.269   ! schwarze  723:        if (p->tcol->lastcol < p->col)
        !           724:                p->tcol->lastcol = p->col;
1.219     schwarze  725: }
                    726:
                    727: void
                    728: term_setwidth(struct termp *p, const char *wstr)
                    729: {
                    730:        struct roffsu    su;
1.247     schwarze  731:        int              iop, width;
1.219     schwarze  732:
1.220     schwarze  733:        iop = 0;
                    734:        width = 0;
1.219     schwarze  735:        if (NULL != wstr) {
                    736:                switch (*wstr) {
1.222     schwarze  737:                case '+':
1.219     schwarze  738:                        iop = 1;
                    739:                        wstr++;
                    740:                        break;
1.222     schwarze  741:                case '-':
1.219     schwarze  742:                        iop = -1;
                    743:                        wstr++;
                    744:                        break;
                    745:                default:
                    746:                        break;
                    747:                }
1.268     schwarze  748:                if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
1.220     schwarze  749:                        width = term_hspan(p, &su);
                    750:                else
1.219     schwarze  751:                        iop = 0;
                    752:        }
                    753:        (*p->setwidth)(p, iop, width);
1.79      kristaps  754: }
1.106     kristaps  755:
1.107     kristaps  756: size_t
1.149     kristaps  757: term_len(const struct termp *p, size_t sz)
                    758: {
                    759:
1.252     schwarze  760:        return (*p->width)(p, ' ') * sz;
1.149     kristaps  761: }
                    762:
1.203     schwarze  763: static size_t
                    764: cond_width(const struct termp *p, int c, int *skip)
                    765: {
                    766:
                    767:        if (*skip) {
                    768:                (*skip) = 0;
1.252     schwarze  769:                return 0;
1.203     schwarze  770:        } else
1.252     schwarze  771:                return (*p->width)(p, c);
1.203     schwarze  772: }
1.149     kristaps  773:
                    774: size_t
                    775: term_strlen(const struct termp *p, const char *cp)
                    776: {
1.184     kristaps  777:        size_t           sz, rsz, i;
1.233     schwarze  778:        int              ssz, skip, uc;
1.171     kristaps  779:        const char      *seq, *rhs;
1.196     kristaps  780:        enum mandoc_esc  esc;
1.216     schwarze  781:        static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
                    782:                        ASCII_BREAK, '\0' };
1.171     kristaps  783:
1.184     kristaps  784:        /*
                    785:         * Account for escaped sequences within string length
                    786:         * calculations.  This follows the logic in term_word() as we
                    787:         * must calculate the width of produced strings.
                    788:         */
                    789:
                    790:        sz = 0;
1.203     schwarze  791:        skip = 0;
1.189     kristaps  792:        while ('\0' != *cp) {
                    793:                rsz = strcspn(cp, rej);
                    794:                for (i = 0; i < rsz; i++)
1.203     schwarze  795:                        sz += cond_width(p, *cp++, &skip);
1.189     kristaps  796:
1.184     kristaps  797:                switch (*cp) {
1.222     schwarze  798:                case '\\':
1.189     kristaps  799:                        cp++;
1.196     kristaps  800:                        esc = mandoc_escape(&cp, &seq, &ssz);
                    801:                        if (ESCAPE_ERROR == esc)
1.224     schwarze  802:                                continue;
1.196     kristaps  803:
                    804:                        rhs = NULL;
                    805:
                    806:                        switch (esc) {
1.222     schwarze  807:                        case ESCAPE_UNICODE:
1.234     schwarze  808:                                uc = mchars_num2uc(seq + 1, ssz - 1);
1.194     kristaps  809:                                break;
1.222     schwarze  810:                        case ESCAPE_NUMBERED:
1.233     schwarze  811:                                uc = mchars_num2char(seq, ssz);
                    812:                                if (uc < 0)
                    813:                                        continue;
1.171     kristaps  814:                                break;
1.222     schwarze  815:                        case ESCAPE_SPECIAL:
1.233     schwarze  816:                                if (p->enc == TERMENC_ASCII) {
1.254     schwarze  817:                                        rhs = mchars_spec2str(seq, ssz, &rsz);
1.233     schwarze  818:                                        if (rhs != NULL)
                    819:                                                break;
                    820:                                } else {
1.254     schwarze  821:                                        uc = mchars_spec2cp(seq, ssz);
1.233     schwarze  822:                                        if (uc > 0)
                    823:                                                sz += cond_width(p, uc, &skip);
1.229     schwarze  824:                                }
1.233     schwarze  825:                                continue;
1.222     schwarze  826:                        case ESCAPE_SKIPCHAR:
1.203     schwarze  827:                                skip = 1;
1.243     schwarze  828:                                continue;
                    829:                        case ESCAPE_OVERSTRIKE:
                    830:                                rsz = 0;
                    831:                                rhs = seq + ssz;
                    832:                                while (seq < rhs) {
                    833:                                        if (*seq == '\\') {
                    834:                                                mandoc_escape(&seq, NULL, NULL);
                    835:                                                continue;
                    836:                                        }
                    837:                                        i = (*p->width)(p, *seq++);
                    838:                                        if (rsz < i)
                    839:                                                rsz = i;
                    840:                                }
                    841:                                sz += rsz;
1.233     schwarze  842:                                continue;
1.171     kristaps  843:                        default:
1.233     schwarze  844:                                continue;
1.171     kristaps  845:                        }
1.149     kristaps  846:
1.233     schwarze  847:                        /*
                    848:                         * Common handling for Unicode and numbered
                    849:                         * character escape sequences.
                    850:                         */
                    851:
                    852:                        if (rhs == NULL) {
                    853:                                if (p->enc == TERMENC_ASCII) {
                    854:                                        rhs = ascii_uc2str(uc);
                    855:                                        rsz = strlen(rhs);
                    856:                                } else {
                    857:                                        if ((uc < 0x20 && uc != 0x09) ||
                    858:                                            (uc > 0x7E && uc < 0xA0))
                    859:                                                uc = 0xFFFD;
                    860:                                        sz += cond_width(p, uc, &skip);
                    861:                                        continue;
                    862:                                }
                    863:                        }
1.184     kristaps  864:
1.203     schwarze  865:                        if (skip) {
                    866:                                skip = 0;
                    867:                                break;
                    868:                        }
1.233     schwarze  869:
                    870:                        /*
                    871:                         * Common handling for all escape sequences
                    872:                         * printing more than one character.
                    873:                         */
1.203     schwarze  874:
1.184     kristaps  875:                        for (i = 0; i < rsz; i++)
                    876:                                sz += (*p->width)(p, *rhs++);
                    877:                        break;
1.222     schwarze  878:                case ASCII_NBRSP:
1.203     schwarze  879:                        sz += cond_width(p, ' ', &skip);
1.176     kristaps  880:                        cp++;
1.184     kristaps  881:                        break;
1.222     schwarze  882:                case ASCII_HYPH:
1.203     schwarze  883:                        sz += cond_width(p, '-', &skip);
1.176     kristaps  884:                        cp++;
1.184     kristaps  885:                        break;
                    886:                default:
                    887:                        break;
                    888:                }
1.189     kristaps  889:        }
1.149     kristaps  890:
1.252     schwarze  891:        return sz;
1.149     kristaps  892: }
                    893:
1.240     schwarze  894: int
1.149     kristaps  895: term_vspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  896: {
                    897:        double           r;
1.241     schwarze  898:        int              ri;
1.106     kristaps  899:
1.107     kristaps  900:        switch (su->unit) {
1.239     schwarze  901:        case SCALE_BU:
                    902:                r = su->scale / 40.0;
                    903:                break;
1.222     schwarze  904:        case SCALE_CM:
1.239     schwarze  905:                r = su->scale * 6.0 / 2.54;
                    906:                break;
                    907:        case SCALE_FS:
                    908:                r = su->scale * 65536.0 / 40.0;
1.106     kristaps  909:                break;
1.222     schwarze  910:        case SCALE_IN:
1.225     schwarze  911:                r = su->scale * 6.0;
1.106     kristaps  912:                break;
1.239     schwarze  913:        case SCALE_MM:
                    914:                r = su->scale * 0.006;
                    915:                break;
1.222     schwarze  916:        case SCALE_PC:
1.107     kristaps  917:                r = su->scale;
1.106     kristaps  918:                break;
1.222     schwarze  919:        case SCALE_PT:
1.239     schwarze  920:                r = su->scale / 12.0;
1.106     kristaps  921:                break;
1.239     schwarze  922:        case SCALE_EN:
                    923:        case SCALE_EM:
                    924:                r = su->scale * 0.6;
1.106     kristaps  925:                break;
1.222     schwarze  926:        case SCALE_VS:
1.107     kristaps  927:                r = su->scale;
1.106     kristaps  928:                break;
                    929:        default:
1.239     schwarze  930:                abort();
1.106     kristaps  931:        }
1.241     schwarze  932:        ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
1.252     schwarze  933:        return ri < 66 ? ri : 1;
1.106     kristaps  934: }
                    935:
1.247     schwarze  936: /*
                    937:  * Convert a scaling width to basic units, rounding down.
                    938:  */
1.240     schwarze  939: int
1.149     kristaps  940: term_hspan(const struct termp *p, const struct roffsu *su)
1.106     kristaps  941: {
1.108     kristaps  942:
1.252     schwarze  943:        return (*p->hspan)(p, su);
1.106     kristaps  944: }

CVSweb