1 /* $Id: term_ascii.c,v 1.11 2011/01/02 12:21:07 kristaps Exp $ */
3 * Copyright (c) 2010 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>
34 static double ascii_hspan(const struct termp *,
35 const struct roffsu *);
36 static size_t ascii_width(const struct termp *, char);
37 static void ascii_advance(struct termp *, size_t);
38 static void ascii_begin(struct termp *);
39 static void ascii_end(struct termp *);
40 static void ascii_endline(struct termp *);
41 static void ascii_letter(struct termp *, char);
45 ascii_alloc(char *outopts)
51 if (NULL == (p = term_alloc(TERMENC_ASCII)))
57 p->advance = ascii_advance;
58 p->begin = ascii_begin;
60 p->endline = ascii_endline;
61 p->hspan = ascii_hspan;
62 p->letter = ascii_letter;
63 p->type = TERMTYPE_CHAR;
64 p->width = ascii_width;
69 while (outopts && *outopts)
70 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
72 p->defrmargin = (size_t)atoi(v);
78 /* Enforce a lower boundary. */
79 if (p->defrmargin < 58)
88 ascii_width(const struct termp *p, char c)
99 term_free((struct termp *)arg);
105 ascii_letter(struct termp *p, char c)
114 ascii_begin(struct termp *p)
117 (*p->headf)(p, p->argf);
122 ascii_end(struct termp *p)
125 (*p->footf)(p, p->argf);
131 ascii_endline(struct termp *p)
140 ascii_advance(struct termp *p, size_t len)
144 /* Just print whitespace on the terminal. */
145 for (i = 0; i < len; i++)
152 ascii_hspan(const struct termp *p, const struct roffsu *su)
157 * Approximate based on character width. These are generated
158 * entirely by eyeballing the screen, but appear to be correct.
169 r = (10 * su->scale) / 6;
172 r = (10 * su->scale) / 72;
175 r = su->scale / 1000;
178 r = su->scale * 2 - 1;