]> git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
Correctly handle constructs like
[mandoc.git] / term_ascii.c
1 /* $Id: term_ascii.c,v 1.19 2011/11/13 13:15:14 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2011 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 /*
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
46 #if ! defined(__STDC_ISO_10646__)
47 # undef USE_WCHAR
48 #endif
49 #endif
50
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);
60
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
68 static struct termp *
69 ascii_init(enum termenc enc, char *outopts)
70 {
71 const char *toks[3];
72 char *v;
73 struct termp *p;
74
75 p = mandoc_calloc(1, sizeof(struct termp));
76 p->enc = enc;
77
78 p->tabwidth = 5;
79 p->defrmargin = 78;
80
81 p->begin = ascii_begin;
82 p->end = ascii_end;
83 p->hspan = ascii_hspan;
84 p->type = TERMTYPE_CHAR;
85
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;
91
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) {
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 }
104 }
105 #endif
106
107 toks[0] = "indent";
108 toks[1] = "width";
109 toks[2] = NULL;
110
111 while (outopts && *outopts)
112 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
113 case (0):
114 p->defindent = (size_t)atoi(v);
115 break;
116 case (1):
117 p->defrmargin = (size_t)atoi(v);
118 break;
119 default:
120 break;
121 }
122
123 /* Enforce a lower boundary. */
124 if (p->defrmargin < 58)
125 p->defrmargin = 58;
126
127 return(p);
128 }
129
130 void *
131 ascii_alloc(char *outopts)
132 {
133
134 return(ascii_init(TERMENC_ASCII, outopts));
135 }
136
137 void *
138 utf8_alloc(char *outopts)
139 {
140
141 return(ascii_init(TERMENC_UTF8, outopts));
142 }
143
144
145 void *
146 locale_alloc(char *outopts)
147 {
148
149 return(ascii_init(TERMENC_LOCALE, outopts));
150 }
151
152 /* ARGSUSED */
153 static size_t
154 ascii_width(const struct termp *p, int c)
155 {
156
157 return(1);
158 }
159
160 void
161 ascii_free(void *arg)
162 {
163
164 term_free((struct termp *)arg);
165 }
166
167 /* ARGSUSED */
168 static void
169 ascii_letter(struct termp *p, int c)
170 {
171
172 putchar(c);
173 }
174
175 static void
176 ascii_begin(struct termp *p)
177 {
178
179 (*p->headf)(p, p->argf);
180 }
181
182 static void
183 ascii_end(struct termp *p)
184 {
185
186 (*p->footf)(p, p->argf);
187 }
188
189 /* ARGSUSED */
190 static void
191 ascii_endline(struct termp *p)
192 {
193
194 putchar('\n');
195 }
196
197 /* ARGSUSED */
198 static void
199 ascii_advance(struct termp *p, size_t len)
200 {
201 size_t i;
202
203 for (i = 0; i < len; i++)
204 putchar(' ');
205 }
206
207 /* ARGSUSED */
208 static double
209 ascii_hspan(const struct termp *p, const struct roffsu *su)
210 {
211 double r;
212
213 /*
214 * Approximate based on character width. These are generated
215 * entirely by eyeballing the screen, but appear to be correct.
216 */
217
218 switch (su->unit) {
219 case (SCALE_CM):
220 r = 4 * su->scale;
221 break;
222 case (SCALE_IN):
223 r = 10 * su->scale;
224 break;
225 case (SCALE_PC):
226 r = (10 * su->scale) / 6;
227 break;
228 case (SCALE_PT):
229 r = (10 * su->scale) / 72;
230 break;
231 case (SCALE_MM):
232 r = su->scale / 1000;
233 break;
234 case (SCALE_VS):
235 r = su->scale * 2 - 1;
236 break;
237 default:
238 r = su->scale;
239 break;
240 }
241
242 return(r);
243 }
244
245 #ifdef USE_WCHAR
246 /* ARGSUSED */
247 static size_t
248 locale_width(const struct termp *p, int c)
249 {
250 int rc;
251
252 return((rc = wcwidth(c)) < 0 ? 0 : rc);
253 }
254
255 /* ARGSUSED */
256 static void
257 locale_advance(struct termp *p, size_t len)
258 {
259 size_t i;
260
261 for (i = 0; i < len; i++)
262 putwchar(L' ');
263 }
264
265 /* ARGSUSED */
266 static void
267 locale_endline(struct termp *p)
268 {
269
270 putwchar(L'\n');
271 }
272
273 /* ARGSUSED */
274 static void
275 locale_letter(struct termp *p, int c)
276 {
277
278 putwchar(c);
279 }
280 #endif