Annotation of mandoc/term_ascii.c, Revision 1.15
1.15 ! kristaps 1: /* $Id: term_ascii.c,v 1.14 2011/05/17 14:38:34 kristaps Exp $ */
1.1 kristaps 2: /*
1.9 schwarze 3: * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 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>
1.15 ! kristaps 24: #ifdef USE_WCHAR
! 25: # include <locale.h>
! 26: #endif
1.1 kristaps 27: #include <stdint.h>
28: #include <stdio.h>
29: #include <stdlib.h>
1.2 kristaps 30: #include <unistd.h>
1.15 ! kristaps 31: #ifdef USE_WCHAR
! 32: # include <wchar.h>
! 33: #endif
1.1 kristaps 34:
1.11 kristaps 35: #include "mandoc.h"
1.1 kristaps 36: #include "out.h"
37: #include "term.h"
38: #include "main.h"
39:
1.15 ! kristaps 40: #if ! defined(__STDC_ISO_10646__)
! 41: # undef USE_WCHAR
! 42: #endif
! 43:
1.14 kristaps 44: static struct termp *ascii_init(enum termenc, char *);
1.8 kristaps 45: static double ascii_hspan(const struct termp *,
1.7 kristaps 46: const struct roffsu *);
1.13 kristaps 47: static size_t ascii_width(const struct termp *, int);
1.7 kristaps 48: static void ascii_advance(struct termp *, size_t);
49: static void ascii_begin(struct termp *);
50: static void ascii_end(struct termp *);
1.1 kristaps 51: static void ascii_endline(struct termp *);
1.13 kristaps 52: static void ascii_letter(struct termp *, int);
1.1 kristaps 53:
1.15 ! kristaps 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:
1.14 kristaps 61: static struct termp *
62: ascii_init(enum termenc enc, char *outopts)
1.1 kristaps 63: {
64: const char *toks[2];
65: char *v;
1.14 kristaps 66: struct termp *p;
1.1 kristaps 67:
1.14 kristaps 68: p = mandoc_calloc(1, sizeof(struct termp));
69: p->enc = enc;
1.1 kristaps 70:
1.5 kristaps 71: p->tabwidth = 5;
72: p->defrmargin = 78;
73:
1.1 kristaps 74: p->begin = ascii_begin;
75: p->end = ascii_end;
1.15 ! kristaps 76: p->hspan = ascii_hspan;
! 77: p->type = TERMTYPE_CHAR;
! 78:
! 79: p->enc = TERMENC_ASCII;
! 80: p->advance = ascii_advance;
1.1 kristaps 81: p->endline = ascii_endline;
1.7 kristaps 82: p->letter = ascii_letter;
1.5 kristaps 83: p->width = ascii_width;
1.1 kristaps 84:
1.15 ! kristaps 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:
1.1 kristaps 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);
1.5 kristaps 113: }
114:
1.14 kristaps 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: }
1.5 kristaps 128:
1.6 kristaps 129: /* ARGSUSED */
1.5 kristaps 130: static size_t
1.13 kristaps 131: ascii_width(const struct termp *p, int c)
1.5 kristaps 132: {
133:
134: return(1);
1.1 kristaps 135: }
136:
137: void
138: ascii_free(void *arg)
139: {
140:
141: term_free((struct termp *)arg);
142: }
143:
144: /* ARGSUSED */
145: static void
1.13 kristaps 146: ascii_letter(struct termp *p, int c)
1.1 kristaps 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: }
1.7 kristaps 183:
184: /* ARGSUSED */
1.8 kristaps 185: static double
1.7 kristaps 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:
1.8 kristaps 219: return(r);
1.7 kristaps 220: }
221:
1.15 ! kristaps 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
CVSweb