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

Annotation of mandoc/term_ascii.c, Revision 1.31

1.31    ! schwarze    1: /*     $Id: term_ascii.c,v 1.30 2014/08/14 22:33:10 schwarze Exp $ */
1.1       kristaps    2: /*
1.18      schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.23      schwarze    4:  * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     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.
                     17:  */
                     18: #include "config.h"
                     19:
                     20: #include <sys/types.h>
                     21:
1.31    ! schwarze   22: #if HAVE_WCHAR
1.28      schwarze   23: #include <locale.h>
1.15      kristaps   24: #endif
1.1       kristaps   25: #include <stdint.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
1.2       kristaps   28: #include <unistd.h>
1.31    ! schwarze   29: #if HAVE_WCHAR
1.28      schwarze   30: #include <wchar.h>
1.15      kristaps   31: #endif
1.1       kristaps   32:
1.11      kristaps   33: #include "mandoc.h"
1.22      schwarze   34: #include "mandoc_aux.h"
1.1       kristaps   35: #include "out.h"
                     36: #include "term.h"
                     37: #include "main.h"
                     38:
1.14      kristaps   39: static struct termp     *ascii_init(enum termenc, char *);
1.8       kristaps   40: static double            ascii_hspan(const struct termp *,
1.7       kristaps   41:                                const struct roffsu *);
1.13      kristaps   42: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   43: static void              ascii_advance(struct termp *, size_t);
                     44: static void              ascii_begin(struct termp *);
                     45: static void              ascii_end(struct termp *);
1.1       kristaps   46: static void              ascii_endline(struct termp *);
1.13      kristaps   47: static void              ascii_letter(struct termp *, int);
1.24      schwarze   48: static void              ascii_setwidth(struct termp *, int, size_t);
1.1       kristaps   49:
1.31    ! schwarze   50: #if HAVE_WCHAR
1.15      kristaps   51: static void              locale_advance(struct termp *, size_t);
                     52: static void              locale_endline(struct termp *);
                     53: static void              locale_letter(struct termp *, int);
                     54: static size_t            locale_width(const struct termp *, int);
                     55: #endif
                     56:
1.25      schwarze   57:
1.14      kristaps   58: static struct termp *
                     59: ascii_init(enum termenc enc, char *outopts)
1.1       kristaps   60: {
1.20      schwarze   61:        const char      *toks[4];
1.1       kristaps   62:        char            *v;
1.14      kristaps   63:        struct termp    *p;
1.1       kristaps   64:
1.14      kristaps   65:        p = mandoc_calloc(1, sizeof(struct termp));
1.1       kristaps   66:
1.5       kristaps   67:        p->tabwidth = 5;
1.23      schwarze   68:        p->defrmargin = p->lastrmargin = 78;
1.5       kristaps   69:
1.1       kristaps   70:        p->begin = ascii_begin;
                     71:        p->end = ascii_end;
1.15      kristaps   72:        p->hspan = ascii_hspan;
                     73:        p->type = TERMTYPE_CHAR;
                     74:
                     75:        p->enc = TERMENC_ASCII;
                     76:        p->advance = ascii_advance;
1.1       kristaps   77:        p->endline = ascii_endline;
1.7       kristaps   78:        p->letter = ascii_letter;
1.23      schwarze   79:        p->setwidth = ascii_setwidth;
1.5       kristaps   80:        p->width = ascii_width;
1.1       kristaps   81:
1.31    ! schwarze   82: #if HAVE_WCHAR
1.17      kristaps   83:        if (TERMENC_ASCII != enc) {
                     84:                v = TERMENC_LOCALE == enc ?
1.25      schwarze   85:                    setlocale(LC_ALL, "") :
                     86:                    setlocale(LC_CTYPE, "en_US.UTF-8");
1.17      kristaps   87:                if (NULL != v && MB_CUR_MAX > 1) {
1.15      kristaps   88:                        p->enc = enc;
                     89:                        p->advance = locale_advance;
                     90:                        p->endline = locale_endline;
                     91:                        p->letter = locale_letter;
                     92:                        p->width = locale_width;
                     93:                }
1.17      kristaps   94:        }
1.15      kristaps   95: #endif
                     96:
1.19      schwarze   97:        toks[0] = "indent";
                     98:        toks[1] = "width";
1.20      schwarze   99:        toks[2] = "mdoc";
                    100:        toks[3] = NULL;
1.1       kristaps  101:
                    102:        while (outopts && *outopts)
                    103:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.25      schwarze  104:                case 0:
1.19      schwarze  105:                        p->defindent = (size_t)atoi(v);
                    106:                        break;
1.25      schwarze  107:                case 1:
1.1       kristaps  108:                        p->defrmargin = (size_t)atoi(v);
1.20      schwarze  109:                        break;
1.25      schwarze  110:                case 2:
1.20      schwarze  111:                        /*
                    112:                         * Temporary, undocumented mode
                    113:                         * to imitate mdoc(7) output style.
                    114:                         */
                    115:                        p->mdocstyle = 1;
                    116:                        p->defindent = 5;
1.1       kristaps  117:                        break;
                    118:                default:
                    119:                        break;
                    120:                }
                    121:
                    122:        /* Enforce a lower boundary. */
                    123:        if (p->defrmargin < 58)
                    124:                p->defrmargin = 58;
                    125:
                    126:        return(p);
1.5       kristaps  127: }
                    128:
1.14      kristaps  129: void *
                    130: ascii_alloc(char *outopts)
                    131: {
                    132:
                    133:        return(ascii_init(TERMENC_ASCII, outopts));
                    134: }
1.17      kristaps  135:
                    136: void *
                    137: utf8_alloc(char *outopts)
                    138: {
                    139:
                    140:        return(ascii_init(TERMENC_UTF8, outopts));
                    141: }
                    142:
1.14      kristaps  143: void *
                    144: locale_alloc(char *outopts)
                    145: {
                    146:
                    147:        return(ascii_init(TERMENC_LOCALE, outopts));
1.23      schwarze  148: }
                    149:
                    150: static void
1.24      schwarze  151: ascii_setwidth(struct termp *p, int iop, size_t width)
1.23      schwarze  152: {
                    153:
1.24      schwarze  154:        p->rmargin = p->defrmargin;
                    155:        if (0 < iop)
                    156:                p->defrmargin += width;
                    157:        else if (0 > iop)
                    158:                p->defrmargin -= width;
                    159:        else
                    160:                p->defrmargin = width ? width : p->lastrmargin;
                    161:        p->lastrmargin = p->rmargin;
                    162:        p->rmargin = p->maxrmargin = p->defrmargin;
1.14      kristaps  163: }
1.5       kristaps  164:
                    165: static size_t
1.13      kristaps  166: ascii_width(const struct termp *p, int c)
1.5       kristaps  167: {
                    168:
                    169:        return(1);
1.1       kristaps  170: }
                    171:
                    172: void
                    173: ascii_free(void *arg)
                    174: {
                    175:
                    176:        term_free((struct termp *)arg);
                    177: }
                    178:
                    179: static void
1.13      kristaps  180: ascii_letter(struct termp *p, int c)
1.1       kristaps  181: {
1.25      schwarze  182:
1.1       kristaps  183:        putchar(c);
                    184: }
                    185:
                    186: static void
                    187: ascii_begin(struct termp *p)
                    188: {
                    189:
                    190:        (*p->headf)(p, p->argf);
                    191: }
                    192:
                    193: static void
                    194: ascii_end(struct termp *p)
                    195: {
                    196:
                    197:        (*p->footf)(p, p->argf);
                    198: }
                    199:
                    200: static void
                    201: ascii_endline(struct termp *p)
                    202: {
                    203:
                    204:        putchar('\n');
                    205: }
                    206:
                    207: static void
                    208: ascii_advance(struct termp *p, size_t len)
                    209: {
1.25      schwarze  210:        size_t          i;
1.1       kristaps  211:
                    212:        for (i = 0; i < len; i++)
                    213:                putchar(' ');
                    214: }
1.7       kristaps  215:
1.8       kristaps  216: static double
1.7       kristaps  217: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    218: {
                    219:        double           r;
                    220:
                    221:        /*
1.29      kristaps  222:         * Approximate based on character width.
                    223:         * None of these will be actually correct given that an inch on
                    224:         * the screen depends on character size, terminal, etc., etc.
1.7       kristaps  225:         */
                    226:        switch (su->unit) {
1.29      kristaps  227:        case SCALE_BU:
                    228:                r = su->scale * 10.0 / 240.0;
                    229:                break;
1.25      schwarze  230:        case SCALE_CM:
1.29      kristaps  231:                r = su->scale * 10.0 / 2.54;
                    232:                break;
                    233:        case SCALE_FS:
                    234:                r = su->scale * 2730.666;
1.7       kristaps  235:                break;
1.25      schwarze  236:        case SCALE_IN:
1.27      schwarze  237:                r = su->scale * 10.0;
1.7       kristaps  238:                break;
1.29      kristaps  239:        case SCALE_MM:
                    240:                r = su->scale / 100.0;
                    241:                break;
1.25      schwarze  242:        case SCALE_PC:
1.29      kristaps  243:                r = su->scale * 10.0 / 6.0;
1.7       kristaps  244:                break;
1.25      schwarze  245:        case SCALE_PT:
1.29      kristaps  246:                r = su->scale * 10.0 / 72.0;
1.7       kristaps  247:                break;
1.25      schwarze  248:        case SCALE_VS:
1.27      schwarze  249:                r = su->scale * 2.0 - 1.0;
1.7       kristaps  250:                break;
1.29      kristaps  251:        case SCALE_EN:
1.30      schwarze  252:                /* FALLTHROUGH */
1.29      kristaps  253:        case SCALE_EM:
1.7       kristaps  254:                r = su->scale;
1.29      kristaps  255:                break;
                    256:        case SCALE_MAX:
                    257:                abort();
1.30      schwarze  258:                /* NOTREACHED */
1.7       kristaps  259:        }
                    260:
1.8       kristaps  261:        return(r);
1.7       kristaps  262: }
                    263:
1.31    ! schwarze  264: #if HAVE_WCHAR
1.15      kristaps  265: static size_t
                    266: locale_width(const struct termp *p, int c)
                    267: {
                    268:        int             rc;
                    269:
1.26      schwarze  270:        if (c == ASCII_NBRSP)
                    271:                c = ' ';
                    272:        rc = wcwidth(c);
                    273:        if (rc < 0)
                    274:                rc = 0;
                    275:        return(rc);
1.15      kristaps  276: }
                    277:
                    278: static void
                    279: locale_advance(struct termp *p, size_t len)
                    280: {
1.25      schwarze  281:        size_t          i;
1.15      kristaps  282:
                    283:        for (i = 0; i < len; i++)
                    284:                putwchar(L' ');
                    285: }
                    286:
                    287: static void
                    288: locale_endline(struct termp *p)
                    289: {
                    290:
                    291:        putwchar(L'\n');
                    292: }
                    293:
                    294: static void
                    295: locale_letter(struct termp *p, int c)
                    296: {
1.25      schwarze  297:
1.15      kristaps  298:        putwchar(c);
                    299: }
                    300: #endif

CVSweb