1 /* $Id: term_ascii.c,v 1.20 2011/12/04 23:10:52 schwarze Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/types.h>
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.
46 #if ! defined(__STDC_ISO_10646__)
51 static struct termp *ascii_init(enum termenc, char *);
52 static double ascii_hspan(const struct termp *,
53 const struct roffsu *);
54 static size_t ascii_width(const struct termp *, int);
55 static void ascii_advance(struct termp *, size_t);
56 static void ascii_begin(struct termp *);
57 static void ascii_end(struct termp *);
58 static void ascii_endline(struct termp *);
59 static void ascii_letter(struct termp *, int);
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);
69 ascii_init(enum termenc enc, char *outopts)
75 p = mandoc_calloc(1, sizeof(struct termp));
81 p->begin = ascii_begin;
83 p->hspan = ascii_hspan;
84 p->type = TERMTYPE_CHAR;
86 p->enc = TERMENC_ASCII;
87 p->advance = ascii_advance;
88 p->endline = ascii_endline;
89 p->letter = ascii_letter;
90 p->width = ascii_width;
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) {
99 p->advance = locale_advance;
100 p->endline = locale_endline;
101 p->letter = locale_letter;
102 p->width = locale_width;
112 while (outopts && *outopts)
113 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
115 p->defindent = (size_t)atoi(v);
118 p->defrmargin = (size_t)atoi(v);
122 * Temporary, undocumented mode
123 * to imitate mdoc(7) output style.
132 /* Enforce a lower boundary. */
133 if (p->defrmargin < 58)
140 ascii_alloc(char *outopts)
143 return(ascii_init(TERMENC_ASCII, outopts));
147 utf8_alloc(char *outopts)
150 return(ascii_init(TERMENC_UTF8, outopts));
155 locale_alloc(char *outopts)
158 return(ascii_init(TERMENC_LOCALE, outopts));
163 ascii_width(const struct termp *p, int c)
170 ascii_free(void *arg)
173 term_free((struct termp *)arg);
178 ascii_letter(struct termp *p, int c)
185 ascii_begin(struct termp *p)
188 (*p->headf)(p, p->argf);
192 ascii_end(struct termp *p)
195 (*p->footf)(p, p->argf);
200 ascii_endline(struct termp *p)
208 ascii_advance(struct termp *p, size_t len)
212 for (i = 0; i < len; i++)
218 ascii_hspan(const struct termp *p, const struct roffsu *su)
223 * Approximate based on character width. These are generated
224 * entirely by eyeballing the screen, but appear to be correct.
235 r = (10 * su->scale) / 6;
238 r = (10 * su->scale) / 72;
241 r = su->scale / 1000;
244 r = su->scale * 2 - 1;
257 locale_width(const struct termp *p, int c)
261 return((rc = wcwidth(c)) < 0 ? 0 : rc);
266 locale_advance(struct termp *p, size_t len)
270 for (i = 0; i < len; i++)
276 locale_endline(struct termp *p)
284 locale_letter(struct termp *p, int c)