]>
git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
1 /* $Id: term_ascii.c,v 1.32 2014/08/17 22:10:29 schwarze Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
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.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.
20 #include <sys/types.h>
34 #include "mandoc_aux.h"
39 static struct termp
*ascii_init(enum termenc
, char *);
40 static double ascii_hspan(const struct termp
*,
41 const struct roffsu
*);
42 static size_t ascii_width(const struct termp
*, int);
43 static void ascii_advance(struct termp
*, size_t);
44 static void ascii_begin(struct termp
*);
45 static void ascii_end(struct termp
*);
46 static void ascii_endline(struct termp
*);
47 static void ascii_letter(struct termp
*, int);
48 static void ascii_setwidth(struct termp
*, int, size_t);
51 static void locale_advance(struct termp
*, size_t);
52 static void locale_endline(struct termp
*);
53 static void locale_letter(struct termp
*, int);
54 static size_t locale_width(const struct termp
*, int);
59 ascii_init(enum termenc enc
, char *outopts
)
65 p
= mandoc_calloc(1, sizeof(struct termp
));
68 p
->defrmargin
= p
->lastrmargin
= 78;
70 p
->begin
= ascii_begin
;
72 p
->hspan
= ascii_hspan
;
73 p
->type
= TERMTYPE_CHAR
;
75 p
->enc
= TERMENC_ASCII
;
76 p
->advance
= ascii_advance
;
77 p
->endline
= ascii_endline
;
78 p
->letter
= ascii_letter
;
79 p
->setwidth
= ascii_setwidth
;
80 p
->width
= ascii_width
;
83 if (TERMENC_ASCII
!= enc
) {
84 v
= TERMENC_LOCALE
== enc
?
85 setlocale(LC_ALL
, "") :
86 setlocale(LC_CTYPE
, "en_US.UTF-8");
87 if (NULL
!= v
&& MB_CUR_MAX
> 1) {
89 p
->advance
= locale_advance
;
90 p
->endline
= locale_endline
;
91 p
->letter
= locale_letter
;
92 p
->width
= locale_width
;
102 while (outopts
&& *outopts
)
103 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
105 p
->defindent
= (size_t)atoi(v
);
108 p
->defrmargin
= (size_t)atoi(v
);
112 * Temporary, undocumented mode
113 * to imitate mdoc(7) output style.
122 /* Enforce a lower boundary. */
123 if (p
->defrmargin
< 58)
130 ascii_alloc(char *outopts
)
133 return(ascii_init(TERMENC_ASCII
, outopts
));
137 utf8_alloc(char *outopts
)
140 return(ascii_init(TERMENC_UTF8
, outopts
));
144 locale_alloc(char *outopts
)
147 return(ascii_init(TERMENC_LOCALE
, outopts
));
151 ascii_setwidth(struct termp
*p
, int iop
, size_t width
)
154 p
->rmargin
= p
->defrmargin
;
156 p
->defrmargin
+= width
;
158 p
->defrmargin
-= width
;
160 p
->defrmargin
= width
? width
: p
->lastrmargin
;
161 p
->lastrmargin
= p
->rmargin
;
162 p
->rmargin
= p
->maxrmargin
= p
->defrmargin
;
166 ascii_width(const struct termp
*p
, int c
)
173 ascii_free(void *arg
)
176 term_free((struct termp
*)arg
);
180 ascii_letter(struct termp
*p
, int c
)
187 ascii_begin(struct termp
*p
)
190 (*p
->headf
)(p
, p
->argf
);
194 ascii_end(struct termp
*p
)
197 (*p
->footf
)(p
, p
->argf
);
201 ascii_endline(struct termp
*p
)
208 ascii_advance(struct termp
*p
, size_t len
)
212 for (i
= 0; i
< len
; i
++)
217 ascii_hspan(const struct termp
*p
, const struct roffsu
*su
)
222 * Approximate based on character width.
223 * None of these will be actually correct given that an inch on
224 * the screen depends on character size, terminal, etc., etc.
228 r
= su
->scale
* 10.0 / 240.0;
231 r
= su
->scale
* 10.0 / 2.54;
234 r
= su
->scale
* 2730.666;
237 r
= su
->scale
* 10.0;
240 r
= su
->scale
/ 100.0;
243 r
= su
->scale
* 10.0 / 6.0;
246 r
= su
->scale
* 10.0 / 72.0;
249 r
= su
->scale
* 2.0 - 1.0;
266 locale_width(const struct termp
*p
, int c
)
270 if (c
== ASCII_NBRSP
)
279 locale_advance(struct termp
*p
, size_t len
)
283 for (i
= 0; i
< len
; i
++)
288 locale_endline(struct termp
*p
)
295 locale_letter(struct termp
*p
, int c
)