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

Annotation of mandoc/term_ascii.c, Revision 1.10

1.10    ! kristaps    1: /*     $Id: term_ascii.c,v 1.9 2010/07/13 23:53:20 schwarze 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>
                     24: #include <stdint.h>
                     25: #include <stdio.h>
                     26: #include <stdlib.h>
1.2       kristaps   27: #include <unistd.h>
1.1       kristaps   28:
                     29: #include "out.h"
                     30: #include "term.h"
                     31: #include "main.h"
                     32:
1.8       kristaps   33: static double            ascii_hspan(const struct termp *,
1.7       kristaps   34:                                const struct roffsu *);
                     35: static size_t            ascii_width(const struct termp *, char);
                     36: static void              ascii_advance(struct termp *, size_t);
                     37: static void              ascii_begin(struct termp *);
                     38: static void              ascii_end(struct termp *);
1.1       kristaps   39: static void              ascii_endline(struct termp *);
                     40: static void              ascii_letter(struct termp *, char);
                     41:
                     42:
                     43: void *
                     44: ascii_alloc(char *outopts)
                     45: {
                     46:        struct termp    *p;
                     47:        const char      *toks[2];
                     48:        char            *v;
                     49:
                     50:        if (NULL == (p = term_alloc(TERMENC_ASCII)))
                     51:                return(NULL);
                     52:
1.5       kristaps   53:        p->tabwidth = 5;
                     54:        p->defrmargin = 78;
                     55:
1.7       kristaps   56:        p->advance = ascii_advance;
1.1       kristaps   57:        p->begin = ascii_begin;
                     58:        p->end = ascii_end;
                     59:        p->endline = ascii_endline;
1.7       kristaps   60:        p->hspan = ascii_hspan;
                     61:        p->letter = ascii_letter;
                     62:        p->type = TERMTYPE_CHAR;
1.5       kristaps   63:        p->width = ascii_width;
1.1       kristaps   64:
                     65:        toks[0] = "width";
                     66:        toks[1] = NULL;
                     67:
                     68:        while (outopts && *outopts)
                     69:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                     70:                case (0):
                     71:                        p->defrmargin = (size_t)atoi(v);
                     72:                        break;
                     73:                default:
                     74:                        break;
                     75:                }
                     76:
                     77:        /* Enforce a lower boundary. */
                     78:        if (p->defrmargin < 58)
                     79:                p->defrmargin = 58;
                     80:
                     81:        return(p);
1.5       kristaps   82: }
                     83:
                     84:
1.6       kristaps   85: /* ARGSUSED */
1.5       kristaps   86: static size_t
                     87: ascii_width(const struct termp *p, char c)
                     88: {
                     89:
                     90:        return(1);
1.1       kristaps   91: }
                     92:
                     93:
                     94: void
                     95: ascii_free(void *arg)
                     96: {
                     97:
                     98:        term_free((struct termp *)arg);
                     99: }
                    100:
                    101:
                    102: /* ARGSUSED */
                    103: static void
                    104: ascii_letter(struct termp *p, char c)
                    105: {
                    106:
1.10    ! kristaps  107:        /* LINTED */
1.1       kristaps  108:        putchar(c);
                    109: }
                    110:
                    111:
                    112: static void
                    113: ascii_begin(struct termp *p)
                    114: {
                    115:
                    116:        (*p->headf)(p, p->argf);
                    117: }
                    118:
                    119:
                    120: static void
                    121: ascii_end(struct termp *p)
                    122: {
                    123:
                    124:        (*p->footf)(p, p->argf);
                    125: }
                    126:
                    127:
                    128: /* ARGSUSED */
                    129: static void
                    130: ascii_endline(struct termp *p)
                    131: {
                    132:
                    133:        putchar('\n');
                    134: }
                    135:
                    136:
                    137: /* ARGSUSED */
                    138: static void
                    139: ascii_advance(struct termp *p, size_t len)
                    140: {
                    141:        size_t          i;
                    142:
                    143:        /* Just print whitespace on the terminal. */
                    144:        for (i = 0; i < len; i++)
                    145:                putchar(' ');
                    146: }
1.7       kristaps  147:
                    148:
                    149: /* ARGSUSED */
1.8       kristaps  150: static double
1.7       kristaps  151: ascii_hspan(const struct termp *p, const struct roffsu *su)
                    152: {
                    153:        double           r;
                    154:
                    155:        /*
                    156:         * Approximate based on character width.  These are generated
                    157:         * entirely by eyeballing the screen, but appear to be correct.
                    158:         */
                    159:
                    160:        switch (su->unit) {
                    161:        case (SCALE_CM):
                    162:                r = 4 * su->scale;
                    163:                break;
                    164:        case (SCALE_IN):
                    165:                r = 10 * su->scale;
                    166:                break;
                    167:        case (SCALE_PC):
                    168:                r = (10 * su->scale) / 6;
                    169:                break;
                    170:        case (SCALE_PT):
                    171:                r = (10 * su->scale) / 72;
                    172:                break;
                    173:        case (SCALE_MM):
                    174:                r = su->scale / 1000;
                    175:                break;
                    176:        case (SCALE_VS):
                    177:                r = su->scale * 2 - 1;
                    178:                break;
                    179:        default:
                    180:                r = su->scale;
                    181:                break;
                    182:        }
                    183:
1.8       kristaps  184:        return(r);
1.7       kristaps  185: }
                    186:

CVSweb