]>
git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
7619907ed14479d348d35cbbdebe4d6a9d9d48e0
1 /* $Id: term_ascii.c,v 1.15 2011/05/17 22:32:45 kristaps Exp $ */
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/types.h>
40 #if ! defined(__STDC_ISO_10646__)
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);
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);
62 ascii_init(enum termenc enc
, char *outopts
)
68 p
= mandoc_calloc(1, sizeof(struct termp
));
74 p
->begin
= ascii_begin
;
76 p
->hspan
= ascii_hspan
;
77 p
->type
= TERMTYPE_CHAR
;
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
;
85 #if defined (USE_WCHAR)
86 if (TERMENC_LOCALE
== enc
)
87 if (setlocale(LC_ALL
, "") && MB_CUR_MAX
> 1) {
89 p
->advance
= locale_advance
;
90 p
->endline
= locale_endline
;
91 p
->letter
= locale_letter
;
92 p
->width
= locale_width
;
99 while (outopts
&& *outopts
)
100 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
102 p
->defrmargin
= (size_t)atoi(v
);
108 /* Enforce a lower boundary. */
109 if (p
->defrmargin
< 58)
116 ascii_alloc(char *outopts
)
119 return(ascii_init(TERMENC_ASCII
, outopts
));
123 locale_alloc(char *outopts
)
126 return(ascii_init(TERMENC_LOCALE
, outopts
));
131 ascii_width(const struct termp
*p
, int c
)
138 ascii_free(void *arg
)
141 term_free((struct termp
*)arg
);
146 ascii_letter(struct termp
*p
, int c
)
153 ascii_begin(struct termp
*p
)
156 (*p
->headf
)(p
, p
->argf
);
160 ascii_end(struct termp
*p
)
163 (*p
->footf
)(p
, p
->argf
);
168 ascii_endline(struct termp
*p
)
176 ascii_advance(struct termp
*p
, size_t len
)
180 for (i
= 0; i
< len
; i
++)
186 ascii_hspan(const struct termp
*p
, const struct roffsu
*su
)
191 * Approximate based on character width. These are generated
192 * entirely by eyeballing the screen, but appear to be correct.
203 r
= (10 * su
->scale
) / 6;
206 r
= (10 * su
->scale
) / 72;
209 r
= su
->scale
/ 1000;
212 r
= su
->scale
* 2 - 1;
225 locale_width(const struct termp
*p
, int c
)
229 return((rc
= wcwidth(c
)) < 0 ? 0 : rc
);
234 locale_advance(struct termp
*p
, size_t len
)
238 for (i
= 0; i
< len
; i
++)
244 locale_endline(struct termp
*p
)
252 locale_letter(struct termp
*p
, int c
)