]> git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
7619907ed14479d348d35cbbdebe4d6a9d9d48e0
[mandoc.git] / term_ascii.c
1 /* $Id: term_ascii.c,v 1.15 2011/05/17 22:32:45 kristaps Exp $ */
2 /*
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 #ifdef USE_WCHAR
25 # include <locale.h>
26 #endif
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #ifdef USE_WCHAR
32 # include <wchar.h>
33 #endif
34
35 #include "mandoc.h"
36 #include "out.h"
37 #include "term.h"
38 #include "main.h"
39
40 #if ! defined(__STDC_ISO_10646__)
41 # undef USE_WCHAR
42 #endif
43
44 static struct termp *ascii_init(enum termenc, char *);
45 static double ascii_hspan(const struct termp *,
46 const struct roffsu *);
47 static size_t ascii_width(const struct termp *, int);
48 static void ascii_advance(struct termp *, size_t);
49 static void ascii_begin(struct termp *);
50 static void ascii_end(struct termp *);
51 static void ascii_endline(struct termp *);
52 static void ascii_letter(struct termp *, int);
53
54 #ifdef USE_WCHAR
55 static void locale_advance(struct termp *, size_t);
56 static void locale_endline(struct termp *);
57 static void locale_letter(struct termp *, int);
58 static size_t locale_width(const struct termp *, int);
59 #endif
60
61 static struct termp *
62 ascii_init(enum termenc enc, char *outopts)
63 {
64 const char *toks[2];
65 char *v;
66 struct termp *p;
67
68 p = mandoc_calloc(1, sizeof(struct termp));
69 p->enc = enc;
70
71 p->tabwidth = 5;
72 p->defrmargin = 78;
73
74 p->begin = ascii_begin;
75 p->end = ascii_end;
76 p->hspan = ascii_hspan;
77 p->type = TERMTYPE_CHAR;
78
79 p->enc = TERMENC_ASCII;
80 p->advance = ascii_advance;
81 p->endline = ascii_endline;
82 p->letter = ascii_letter;
83 p->width = ascii_width;
84
85 #if defined (USE_WCHAR)
86 if (TERMENC_LOCALE == enc)
87 if (setlocale(LC_ALL, "") && MB_CUR_MAX > 1) {
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 }
94 #endif
95
96 toks[0] = "width";
97 toks[1] = NULL;
98
99 while (outopts && *outopts)
100 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
101 case (0):
102 p->defrmargin = (size_t)atoi(v);
103 break;
104 default:
105 break;
106 }
107
108 /* Enforce a lower boundary. */
109 if (p->defrmargin < 58)
110 p->defrmargin = 58;
111
112 return(p);
113 }
114
115 void *
116 ascii_alloc(char *outopts)
117 {
118
119 return(ascii_init(TERMENC_ASCII, outopts));
120 }
121
122 void *
123 locale_alloc(char *outopts)
124 {
125
126 return(ascii_init(TERMENC_LOCALE, outopts));
127 }
128
129 /* ARGSUSED */
130 static size_t
131 ascii_width(const struct termp *p, int c)
132 {
133
134 return(1);
135 }
136
137 void
138 ascii_free(void *arg)
139 {
140
141 term_free((struct termp *)arg);
142 }
143
144 /* ARGSUSED */
145 static void
146 ascii_letter(struct termp *p, int c)
147 {
148
149 putchar(c);
150 }
151
152 static void
153 ascii_begin(struct termp *p)
154 {
155
156 (*p->headf)(p, p->argf);
157 }
158
159 static void
160 ascii_end(struct termp *p)
161 {
162
163 (*p->footf)(p, p->argf);
164 }
165
166 /* ARGSUSED */
167 static void
168 ascii_endline(struct termp *p)
169 {
170
171 putchar('\n');
172 }
173
174 /* ARGSUSED */
175 static void
176 ascii_advance(struct termp *p, size_t len)
177 {
178 size_t i;
179
180 for (i = 0; i < len; i++)
181 putchar(' ');
182 }
183
184 /* ARGSUSED */
185 static double
186 ascii_hspan(const struct termp *p, const struct roffsu *su)
187 {
188 double r;
189
190 /*
191 * Approximate based on character width. These are generated
192 * entirely by eyeballing the screen, but appear to be correct.
193 */
194
195 switch (su->unit) {
196 case (SCALE_CM):
197 r = 4 * su->scale;
198 break;
199 case (SCALE_IN):
200 r = 10 * su->scale;
201 break;
202 case (SCALE_PC):
203 r = (10 * su->scale) / 6;
204 break;
205 case (SCALE_PT):
206 r = (10 * su->scale) / 72;
207 break;
208 case (SCALE_MM):
209 r = su->scale / 1000;
210 break;
211 case (SCALE_VS):
212 r = su->scale * 2 - 1;
213 break;
214 default:
215 r = su->scale;
216 break;
217 }
218
219 return(r);
220 }
221
222 #ifdef USE_WCHAR
223 /* ARGSUSED */
224 static size_t
225 locale_width(const struct termp *p, int c)
226 {
227 int rc;
228
229 return((rc = wcwidth(c)) < 0 ? 0 : rc);
230 }
231
232 /* ARGSUSED */
233 static void
234 locale_advance(struct termp *p, size_t len)
235 {
236 size_t i;
237
238 for (i = 0; i < len; i++)
239 putwchar(L' ');
240 }
241
242 /* ARGSUSED */
243 static void
244 locale_endline(struct termp *p)
245 {
246
247 putwchar(L'\n');
248 }
249
250 /* ARGSUSED */
251 static void
252 locale_letter(struct termp *p, int c)
253 {
254
255 putwchar(c);
256 }
257 #endif