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

Annotation of mandoc/term_ascii.c, Revision 1.17

1.17    ! kristaps    1: /*     $Id: term_ascii.c,v 1.16 2011/05/19 15:48:58 kristaps Exp $ */
1.1       kristaps    2: /*
1.9       schwarze    3:  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/types.h>
                     22:
                     23: #include <assert.h>
1.15      kristaps   24: #ifdef USE_WCHAR
                     25: # include <locale.h>
                     26: #endif
1.1       kristaps   27: #include <stdint.h>
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
1.2       kristaps   30: #include <unistd.h>
1.15      kristaps   31: #ifdef USE_WCHAR
                     32: # include <wchar.h>
                     33: #endif
1.1       kristaps   34:
1.11      kristaps   35: #include "mandoc.h"
1.1       kristaps   36: #include "out.h"
                     37: #include "term.h"
                     38: #include "main.h"
                     39:
1.16      kristaps   40: /*
                     41:  * Sadly, this doesn't seem to be defined on systems even when they
                     42:  * support it.  For the time being, remove it and let those compiling
                     43:  * the software decide for themselves what to use.
                     44:  */
                     45: #if 0
1.15      kristaps   46: #if ! defined(__STDC_ISO_10646__)
                     47: # undef USE_WCHAR
1.16      kristaps   48: #endif
1.15      kristaps   49: #endif
                     50:
1.14      kristaps   51: static struct termp     *ascii_init(enum termenc, char *);
1.8       kristaps   52: static double            ascii_hspan(const struct termp *,
1.7       kristaps   53:                                const struct roffsu *);
1.13      kristaps   54: static size_t            ascii_width(const struct termp *, int);
1.7       kristaps   55: static void              ascii_advance(struct termp *, size_t);
                     56: static void              ascii_begin(struct termp *);
                     57: static void              ascii_end(struct termp *);
1.1       kristaps   58: static void              ascii_endline(struct termp *);
1.13      kristaps   59: static void              ascii_letter(struct termp *, int);
1.1       kristaps   60:
1.15      kristaps   61: #ifdef USE_WCHAR
                     62: static void              locale_advance(struct termp *, size_t);
                     63: static void              locale_endline(struct termp *);
                     64: static void              locale_letter(struct termp *, int);
                     65: static size_t            locale_width(const struct termp *, int);
                     66: #endif
                     67:
1.14      kristaps   68: static struct termp *
                     69: ascii_init(enum termenc enc, char *outopts)
1.1       kristaps   70: {
                     71:        const char      *toks[2];
                     72:        char            *v;
1.14      kristaps   73:        struct termp    *p;
1.1       kristaps   74:
1.14      kristaps   75:        p = mandoc_calloc(1, sizeof(struct termp));
                     76:        p->enc = enc;
1.1       kristaps   77:
1.5       kristaps   78:        p->tabwidth = 5;
                     79:        p->defrmargin = 78;
                     80:
1.1       kristaps   81:        p->begin = ascii_begin;
                     82:        p->end = ascii_end;
1.15      kristaps   83:        p->hspan = ascii_hspan;
                     84:        p->type = TERMTYPE_CHAR;
                     85:
                     86:        p->enc = TERMENC_ASCII;
                     87:        p->advance = ascii_advance;
1.1       kristaps   88:        p->endline = ascii_endline;
1.7       kristaps   89:        p->letter = ascii_letter;
1.5       kristaps   90:        p->width = ascii_width;
1.1       kristaps   91:
1.17    ! kristaps   92: #ifdef USE_WCHAR
        !            93:        if (TERMENC_ASCII != enc) {
        !            94:                v = TERMENC_LOCALE == enc ?
        !            95:                        setlocale(LC_ALL, "") :
        !            96:                        setlocale(LC_CTYPE, "UTF-8");
        !            97:                if (NULL != v && MB_CUR_MAX > 1) {
1.15      kristaps   98:                        p->enc = enc;
                     99:                        p->advance = locale_advance;
                    100:                        p->endline = locale_endline;
                    101:                        p->letter = locale_letter;
                    102:                        p->width = locale_width;
                    103:                }
1.17    ! kristaps  104:        }
1.15      kristaps  105: #endif
                    106:
1.1       kristaps  107:        toks[0] = "width";
                    108:        toks[1] = NULL;
                    109:
                    110:        while (outopts && *outopts)
                    111:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    112:                case (0):
                    113:                        p->defrmargin = (size_t)atoi(v);
                    114:                        break;
                    115:                default:
                    116:                        break;
                    117:                }
                    118:
                    119:        /* Enforce a lower boundary. */
                    120:        if (p->defrmargin < 58)
                    121:                p->defrmargin = 58;
                    122:
                    123:        return(p);
1.5       kristaps  124: }
                    125:
1.14      kristaps  126: void *
                    127: ascii_alloc(char *outopts)
                    128: {
                    129:
                    130:        return(ascii_init(TERMENC_ASCII, outopts));
                    131: }
1.17    ! kristaps  132:
        !           133: void *
        !           134: utf8_alloc(char *outopts)
        !           135: {
        !           136:
        !           137:        return(ascii_init(TERMENC_UTF8, outopts));
        !           138: }
        !           139:
1.14      kristaps  140:
                    141: void *
                    142: locale_alloc(char *outopts)
                    143: {
                    144:
                    145:        return(ascii_init(TERMENC_LOCALE, outopts));
                    146: }
1.5       kristaps  147:
1.6       kristaps  148: /* ARGSUSED */
1.5       kristaps  149: static size_t
1.13      kristaps  150: ascii_width(const struct termp *p, int c)
1.5       kristaps  151: {
                    152:
                    153:        return(1);
1.1       kristaps  154: }
                    155:
                    156: void
                    157: ascii_free(void *arg)
                    158: {
                    159:
                    160:        term_free((struct termp *)arg);
                    161: }
                    162:
                    163: /* ARGSUSED */
                    164: static void
1.13      kristaps  165: ascii_letter(struct termp *p, int c)
1.1       kristaps  166: {
                    167:
                    168:        putchar(c);
                    169: }
                    170:
                    171: static void
                    172: ascii_begin(struct termp *p)
                    173: {
                    174:
                    175:        (*p->headf)(p, p->argf);
                    176: }
                    177:
                    178: static void
                    179: ascii_end(struct termp *p)
                    180: {
                    181:
                    182:        (*p->footf)(p, p->argf);
                    183: }
                    184:
                    185: /* ARGSUSED */
                    186: static void
                    187: ascii_endline(struct termp *p)
                    188: {
                    189:
                    190:        putchar('\n');
                    191: }
                    192:
                    193: /* ARGSUSED */
                    194: static void
                    195: ascii_advance(struct termp *p, size_t len)
                    196: {
                    197:        size_t          i;
                    198:
                    199:        for (i = 0; i < len; i++)
                    200:                putchar(' ');
                    201: }
1.7       kristaps  202:
                    203: /* ARGSUSED */
1.8       kristaps  204: static double
1.7       kristaps  205: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    206: {
                    207:        double           r;
                    208:
                    209:        /*
                    210:         * Approximate based on character width.  These are generated
                    211:         * entirely by eyeballing the screen, but appear to be correct.
                    212:         */
                    213:
                    214:        switch (su->unit) {
                    215:        case (SCALE_CM):
                    216:                r = 4 * su->scale;
                    217:                break;
                    218:        case (SCALE_IN):
                    219:                r = 10 * su->scale;
                    220:                break;
                    221:        case (SCALE_PC):
                    222:                r = (10 * su->scale) / 6;
                    223:                break;
                    224:        case (SCALE_PT):
                    225:                r = (10 * su->scale) / 72;
                    226:                break;
                    227:        case (SCALE_MM):
                    228:                r = su->scale / 1000;
                    229:                break;
                    230:        case (SCALE_VS):
                    231:                r = su->scale * 2 - 1;
                    232:                break;
                    233:        default:
                    234:                r = su->scale;
                    235:                break;
                    236:        }
                    237:
1.8       kristaps  238:        return(r);
1.7       kristaps  239: }
                    240:
1.15      kristaps  241: #ifdef USE_WCHAR
                    242: /* ARGSUSED */
                    243: static size_t
                    244: locale_width(const struct termp *p, int c)
                    245: {
                    246:        int             rc;
                    247:
                    248:        return((rc = wcwidth(c)) < 0 ? 0 : rc);
                    249: }
                    250:
                    251: /* ARGSUSED */
                    252: static void
                    253: locale_advance(struct termp *p, size_t len)
                    254: {
                    255:        size_t          i;
                    256:
                    257:        for (i = 0; i < len; i++)
                    258:                putwchar(L' ');
                    259: }
                    260:
                    261: /* ARGSUSED */
                    262: static void
                    263: locale_endline(struct termp *p)
                    264: {
                    265:
                    266:        putwchar(L'\n');
                    267: }
                    268:
                    269: /* ARGSUSED */
                    270: static void
                    271: locale_letter(struct termp *p, int c)
                    272: {
                    273:
                    274:        putwchar(c);
                    275: }
                    276: #endif

CVSweb