]> git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
3b4ca241196dc1582f038cd8f2cc662775e89a28
[mandoc.git] / term_ascii.c
1 /* $Id: term_ascii.c,v 1.16 2011/05/19 15:48:58 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 /*
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[2];
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 #if defined (USE_WCHAR)
93 if (TERMENC_LOCALE == enc)
94 if (setlocale(LC_ALL, "") && MB_CUR_MAX > 1) {
95 p->enc = enc;
96 p->advance = locale_advance;
97 p->endline = locale_endline;
98 p->letter = locale_letter;
99 p->width = locale_width;
100 }
101 #endif
102
103 toks[0] = "width";
104 toks[1] = NULL;
105
106 while (outopts && *outopts)
107 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
108 case (0):
109 p->defrmargin = (size_t)atoi(v);
110 break;
111 default:
112 break;
113 }
114
115 /* Enforce a lower boundary. */
116 if (p->defrmargin < 58)
117 p->defrmargin = 58;
118
119 return(p);
120 }
121
122 void *
123 ascii_alloc(char *outopts)
124 {
125
126 return(ascii_init(TERMENC_ASCII, outopts));
127 }
128
129 void *
130 locale_alloc(char *outopts)
131 {
132
133 return(ascii_init(TERMENC_LOCALE, outopts));
134 }
135
136 /* ARGSUSED */
137 static size_t
138 ascii_width(const struct termp *p, int c)
139 {
140
141 return(1);
142 }
143
144 void
145 ascii_free(void *arg)
146 {
147
148 term_free((struct termp *)arg);
149 }
150
151 /* ARGSUSED */
152 static void
153 ascii_letter(struct termp *p, int c)
154 {
155
156 putchar(c);
157 }
158
159 static void
160 ascii_begin(struct termp *p)
161 {
162
163 (*p->headf)(p, p->argf);
164 }
165
166 static void
167 ascii_end(struct termp *p)
168 {
169
170 (*p->footf)(p, p->argf);
171 }
172
173 /* ARGSUSED */
174 static void
175 ascii_endline(struct termp *p)
176 {
177
178 putchar('\n');
179 }
180
181 /* ARGSUSED */
182 static void
183 ascii_advance(struct termp *p, size_t len)
184 {
185 size_t i;
186
187 for (i = 0; i < len; i++)
188 putchar(' ');
189 }
190
191 /* ARGSUSED */
192 static double
193 ascii_hspan(const struct termp *p, const struct roffsu *su)
194 {
195 double r;
196
197 /*
198 * Approximate based on character width. These are generated
199 * entirely by eyeballing the screen, but appear to be correct.
200 */
201
202 switch (su->unit) {
203 case (SCALE_CM):
204 r = 4 * su->scale;
205 break;
206 case (SCALE_IN):
207 r = 10 * su->scale;
208 break;
209 case (SCALE_PC):
210 r = (10 * su->scale) / 6;
211 break;
212 case (SCALE_PT):
213 r = (10 * su->scale) / 72;
214 break;
215 case (SCALE_MM):
216 r = su->scale / 1000;
217 break;
218 case (SCALE_VS):
219 r = su->scale * 2 - 1;
220 break;
221 default:
222 r = su->scale;
223 break;
224 }
225
226 return(r);
227 }
228
229 #ifdef USE_WCHAR
230 /* ARGSUSED */
231 static size_t
232 locale_width(const struct termp *p, int c)
233 {
234 int rc;
235
236 return((rc = wcwidth(c)) < 0 ? 0 : rc);
237 }
238
239 /* ARGSUSED */
240 static void
241 locale_advance(struct termp *p, size_t len)
242 {
243 size_t i;
244
245 for (i = 0; i < len; i++)
246 putwchar(L' ');
247 }
248
249 /* ARGSUSED */
250 static void
251 locale_endline(struct termp *p)
252 {
253
254 putwchar(L'\n');
255 }
256
257 /* ARGSUSED */
258 static void
259 locale_letter(struct termp *p, int c)
260 {
261
262 putwchar(c);
263 }
264 #endif