Annotation of mandoc/term_ascii.c, Revision 1.46
1.46 ! schwarze 1: /* $Id: term_ascii.c,v 1.45 2015/04/04 17:47:18 schwarze Exp $ */
1.1 kristaps 2: /*
1.18 schwarze 3: * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.44 schwarze 4: * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
1.44 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.44 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 kristaps 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include "config.h"
19:
20: #include <sys/types.h>
21:
1.39 schwarze 22: #include <assert.h>
1.31 schwarze 23: #if HAVE_WCHAR
1.28 schwarze 24: #include <locale.h>
1.15 kristaps 25: #endif
1.1 kristaps 26: #include <stdint.h>
27: #include <stdio.h>
28: #include <stdlib.h>
1.2 kristaps 29: #include <unistd.h>
1.31 schwarze 30: #if HAVE_WCHAR
1.28 schwarze 31: #include <wchar.h>
1.15 kristaps 32: #endif
1.1 kristaps 33:
1.11 kristaps 34: #include "mandoc.h"
1.22 schwarze 35: #include "mandoc_aux.h"
1.1 kristaps 36: #include "out.h"
37: #include "term.h"
1.44 schwarze 38: #include "manconf.h"
1.1 kristaps 39: #include "main.h"
40:
1.44 schwarze 41: static struct termp *ascii_init(enum termenc, const struct mchars *,
42: const struct manoutput *);
1.45 schwarze 43: static int ascii_hspan(const struct termp *,
1.7 kristaps 44: const struct roffsu *);
1.13 kristaps 45: static size_t ascii_width(const struct termp *, int);
1.7 kristaps 46: static void ascii_advance(struct termp *, size_t);
47: static void ascii_begin(struct termp *);
48: static void ascii_end(struct termp *);
1.1 kristaps 49: static void ascii_endline(struct termp *);
1.13 kristaps 50: static void ascii_letter(struct termp *, int);
1.45 schwarze 51: static void ascii_setwidth(struct termp *, int, int);
1.1 kristaps 52:
1.31 schwarze 53: #if HAVE_WCHAR
1.15 kristaps 54: static void locale_advance(struct termp *, size_t);
55: static void locale_endline(struct termp *);
56: static void locale_letter(struct termp *, int);
57: static size_t locale_width(const struct termp *, int);
58: #endif
59:
1.25 schwarze 60:
1.14 kristaps 61: static struct termp *
1.44 schwarze 62: ascii_init(enum termenc enc, const struct mchars *mchars,
63: const struct manoutput *outopts)
1.1 kristaps 64: {
1.46 ! schwarze 65: #if HAVE_WCHAR
1.1 kristaps 66: char *v;
1.46 ! schwarze 67: #endif
1.14 kristaps 68: struct termp *p;
1.1 kristaps 69:
1.14 kristaps 70: p = mandoc_calloc(1, sizeof(struct termp));
1.1 kristaps 71:
1.38 schwarze 72: p->symtab = mchars;
1.5 kristaps 73: p->tabwidth = 5;
1.23 schwarze 74: p->defrmargin = p->lastrmargin = 78;
1.41 schwarze 75: p->fontq = mandoc_reallocarray(NULL,
76: (p->fontsz = 8), sizeof(enum termfont));
77: p->fontq[0] = p->fontl = TERMFONT_NONE;
1.5 kristaps 78:
1.1 kristaps 79: p->begin = ascii_begin;
80: p->end = ascii_end;
1.15 kristaps 81: p->hspan = ascii_hspan;
82: p->type = TERMTYPE_CHAR;
83:
84: p->enc = TERMENC_ASCII;
85: p->advance = ascii_advance;
1.1 kristaps 86: p->endline = ascii_endline;
1.7 kristaps 87: p->letter = ascii_letter;
1.23 schwarze 88: p->setwidth = ascii_setwidth;
1.5 kristaps 89: p->width = ascii_width;
1.1 kristaps 90:
1.31 schwarze 91: #if HAVE_WCHAR
1.17 kristaps 92: if (TERMENC_ASCII != enc) {
93: v = TERMENC_LOCALE == enc ?
1.25 schwarze 94: setlocale(LC_ALL, "") :
95: setlocale(LC_CTYPE, "en_US.UTF-8");
1.17 kristaps 96: if (NULL != v && MB_CUR_MAX > 1) {
1.15 kristaps 97: p->enc = enc;
98: p->advance = locale_advance;
99: p->endline = locale_endline;
100: p->letter = locale_letter;
101: p->width = locale_width;
102: }
1.17 kristaps 103: }
1.15 kristaps 104: #endif
105:
1.44 schwarze 106: if (outopts->mdoc) {
107: p->mdocstyle = 1;
108: p->defindent = 5;
109: }
110: if (outopts->indent)
111: p->defindent = outopts->indent;
112: if (outopts->width)
113: p->defrmargin = outopts->width;
114: if (outopts->synopsisonly)
115: p->synopsisonly = 1;
1.1 kristaps 116:
117: return(p);
1.5 kristaps 118: }
119:
1.14 kristaps 120: void *
1.44 schwarze 121: ascii_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.14 kristaps 122: {
123:
1.38 schwarze 124: return(ascii_init(TERMENC_ASCII, mchars, outopts));
1.14 kristaps 125: }
1.17 kristaps 126:
127: void *
1.44 schwarze 128: utf8_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.17 kristaps 129: {
130:
1.38 schwarze 131: return(ascii_init(TERMENC_UTF8, mchars, outopts));
1.17 kristaps 132: }
133:
1.14 kristaps 134: void *
1.44 schwarze 135: locale_alloc(const struct mchars *mchars, const struct manoutput *outopts)
1.14 kristaps 136: {
137:
1.38 schwarze 138: return(ascii_init(TERMENC_LOCALE, mchars, outopts));
1.23 schwarze 139: }
140:
141: static void
1.45 schwarze 142: ascii_setwidth(struct termp *p, int iop, int width)
1.23 schwarze 143: {
144:
1.45 schwarze 145: width /= 24;
1.24 schwarze 146: p->rmargin = p->defrmargin;
1.40 schwarze 147: if (iop > 0)
1.24 schwarze 148: p->defrmargin += width;
1.40 schwarze 149: else if (iop == 0)
1.45 schwarze 150: p->defrmargin = width ? (size_t)width : p->lastrmargin;
151: else if (p->defrmargin > (size_t)width)
1.24 schwarze 152: p->defrmargin -= width;
153: else
1.40 schwarze 154: p->defrmargin = 0;
1.24 schwarze 155: p->lastrmargin = p->rmargin;
156: p->rmargin = p->maxrmargin = p->defrmargin;
1.42 schwarze 157: }
158:
159: void
160: ascii_sepline(void *arg)
161: {
162: struct termp *p;
163: size_t i;
164:
165: p = (struct termp *)arg;
166: putchar('\n');
167: for (i = 0; i < p->defrmargin; i++)
168: putchar('-');
169: putchar('\n');
170: putchar('\n');
1.34 schwarze 171: }
172:
1.5 kristaps 173: static size_t
1.13 kristaps 174: ascii_width(const struct termp *p, int c)
1.5 kristaps 175: {
176:
177: return(1);
1.1 kristaps 178: }
179:
180: void
181: ascii_free(void *arg)
182: {
183:
184: term_free((struct termp *)arg);
185: }
186:
187: static void
1.13 kristaps 188: ascii_letter(struct termp *p, int c)
1.1 kristaps 189: {
1.25 schwarze 190:
1.1 kristaps 191: putchar(c);
192: }
193:
194: static void
195: ascii_begin(struct termp *p)
196: {
197:
198: (*p->headf)(p, p->argf);
199: }
200:
201: static void
202: ascii_end(struct termp *p)
203: {
204:
205: (*p->footf)(p, p->argf);
206: }
207:
208: static void
209: ascii_endline(struct termp *p)
210: {
211:
212: putchar('\n');
213: }
214:
215: static void
216: ascii_advance(struct termp *p, size_t len)
217: {
1.25 schwarze 218: size_t i;
1.1 kristaps 219:
220: for (i = 0; i < len; i++)
221: putchar(' ');
222: }
1.7 kristaps 223:
1.45 schwarze 224: static int
1.7 kristaps 225: ascii_hspan(const struct termp *p, const struct roffsu *su)
226: {
227: double r;
228:
229: switch (su->unit) {
1.29 kristaps 230: case SCALE_BU:
1.45 schwarze 231: r = su->scale;
1.29 kristaps 232: break;
1.25 schwarze 233: case SCALE_CM:
1.45 schwarze 234: r = su->scale * 240.0 / 2.54;
1.29 kristaps 235: break;
236: case SCALE_FS:
1.45 schwarze 237: r = su->scale * 65536.0;
1.7 kristaps 238: break;
1.25 schwarze 239: case SCALE_IN:
1.45 schwarze 240: r = su->scale * 240.0;
1.7 kristaps 241: break;
1.29 kristaps 242: case SCALE_MM:
1.45 schwarze 243: r = su->scale * 0.24;
1.29 kristaps 244: break;
1.45 schwarze 245: case SCALE_VS:
246: /* FALLTHROUGH */
1.25 schwarze 247: case SCALE_PC:
1.45 schwarze 248: r = su->scale * 40.0;
1.7 kristaps 249: break;
1.25 schwarze 250: case SCALE_PT:
1.45 schwarze 251: r = su->scale * 10.0 / 3.0;
1.7 kristaps 252: break;
1.29 kristaps 253: case SCALE_EN:
1.30 schwarze 254: /* FALLTHROUGH */
1.29 kristaps 255: case SCALE_EM:
1.45 schwarze 256: r = su->scale * 24.0;
1.29 kristaps 257: break;
1.32 schwarze 258: default:
1.29 kristaps 259: abort();
1.30 schwarze 260: /* NOTREACHED */
1.7 kristaps 261: }
1.45 schwarze 262: return(r > 0.0 ? r + 0.01 : r - 0.01);
1.36 schwarze 263: }
264:
265: const char *
266: ascii_uc2str(int uc)
267: {
268: static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
269: static const char *tab[] = {
270: "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
271: "<BS>", "\t", "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
272: "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
273: "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
274: " ", "!", "\"", "#", "$", "%", "&", "'",
275: "(", ")", "*", "+", ",", "-", ".", "/",
276: "0", "1", "2", "3", "4", "5", "6", "7",
277: "8", "9", ":", ";", "<", "=", ">", "?",
278: "@", "A", "B", "C", "D", "E", "F", "G",
279: "H", "I", "J", "K", "L", "M", "N", "O",
280: "P", "Q", "R", "S", "T", "U", "V", "W",
281: "X", "Y", "Z", "[", "\\", "]", "^", "_",
282: "`", "a", "b", "c", "d", "e", "f", "g",
283: "h", "i", "j", "k", "l", "m", "n", "o",
284: "p", "q", "r", "s", "t", "u", "v", "w",
285: "x", "y", "z", "{", "|", "}", "~", "<DEL>",
286: "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
287: "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
288: "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
289: "<99>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
1.37 schwarze 290: nbrsp, "!", "/\bc", "GBP", "o\bx", "=\bY", "|", "<sec>",
291: "\"", "(C)", "_\ba", "<<", "~", "", "(R)", "-",
292: "<deg>","+-", "2", "3", "'", ",\bu", "<par>",".",
293: ",", "1", "_\bo", ">>", "1/4", "1/2", "3/4", "?",
294: "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE", ",\bC",
295: "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
296: "-\bD", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
297: "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th", "ss",
298: "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae", ",\bc",
299: "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
300: "d", "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","-:-",
301: "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th", "\"\by",
302: "A", "a", "A", "a", "A", "a", "'\bC", "'\bc",
303: "^\bC", "^\bc", "C", "c", "C", "c", "D", "d",
304: "/\bD", "/\bd", "E", "e", "E", "e", "E", "e",
305: "E", "e", "E", "e", "^\bG", "^\bg", "G", "g",
306: "G", "g", ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
307: "~\bI", "~\bi", "I", "i", "I", "i", "I", "i",
308: "I", "i", "IJ", "ij", "^\bJ", "^\bj", ",\bK", ",\bk",
309: "q", "'\bL", "'\bl", ",\bL", ",\bl", "L", "l", "L",
310: "l", "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
1.36 schwarze 311: "n", "'n", "Ng", "ng", "O", "o", "O", "o",
1.37 schwarze 312: "O", "o", "OE", "oe", "'\bR", "'\br", ",\bR", ",\br",
313: "R", "r", "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
314: "S", "s", ",\bT", ",\bt", "T", "t", "/\bT", "/\bt",
315: "~\bU", "~\bu", "U", "u", "U", "u", "U", "u",
316: "U", "u", "U", "u", "^\bW", "^\bw", "^\bY", "^\by",
317: "\"\bY","'\bZ", "'\bz", "Z", "z", "Z", "z", "s",
1.36 schwarze 318: "b", "B", "B", "b", "6", "6", "O", "C",
319: "c", "D", "D", "D", "d", "d", "3", "@",
1.37 schwarze 320: "E", "F", ",\bf", "G", "G", "hv", "I", "/\bI",
321: "K", "k", "/\bl", "l", "W", "N", "n", "~\bO",
1.36 schwarze 322: "O", "o", "OI", "oi", "P", "p", "YR", "2",
323: "2", "SH", "sh", "t", "T", "t", "T", "U",
1.37 schwarze 324: "u", "Y", "V", "Y", "y", "/\bZ", "/\bz", "ZH",
325: "ZH", "zh", "zh", "/\b2", "5", "5", "ts", "w",
1.36 schwarze 326: "|", "||", "|=", "!", "DZ", "Dz", "dz", "LJ",
327: "Lj", "lj", "NJ", "Nj", "nj", "A", "a", "I",
328: "i", "O", "o", "U", "u", "U", "u", "U",
329: "u", "U", "u", "U", "u", "@", "A", "a",
1.37 schwarze 330: "A", "a", "AE", "ae", "/\bG", "/\bg", "G", "g",
1.36 schwarze 331: "K", "k", "O", "o", "O", "o", "ZH", "zh",
1.37 schwarze 332: "j", "DZ", "Dz", "dz", "'\bG", "'\bg", "HV", "W",
333: "`\bN", "`\bn", "A", "a", "'\bAE","'\bae","O", "o"};
1.36 schwarze 334:
1.39 schwarze 335: assert(uc >= 0);
1.36 schwarze 336: if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
337: return(tab[uc]);
338: return(mchars_uc2str(uc));
1.7 kristaps 339: }
340:
1.31 schwarze 341: #if HAVE_WCHAR
1.15 kristaps 342: static size_t
343: locale_width(const struct termp *p, int c)
344: {
345: int rc;
346:
1.26 schwarze 347: if (c == ASCII_NBRSP)
348: c = ' ';
349: rc = wcwidth(c);
350: if (rc < 0)
351: rc = 0;
352: return(rc);
1.15 kristaps 353: }
354:
355: static void
356: locale_advance(struct termp *p, size_t len)
357: {
1.25 schwarze 358: size_t i;
1.15 kristaps 359:
360: for (i = 0; i < len; i++)
361: putwchar(L' ');
362: }
363:
364: static void
365: locale_endline(struct termp *p)
366: {
367:
368: putwchar(L'\n');
369: }
370:
371: static void
372: locale_letter(struct termp *p, int c)
373: {
1.25 schwarze 374:
1.15 kristaps 375: putwchar(c);
376: }
377: #endif
CVSweb