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

Annotation of mandoc/term.c, Revision 1.268

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

CVSweb