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

Annotation of mandoc/term.c, Revision 1.239

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

CVSweb