]>
git.cameronkatri.com Git - mandoc.git/blob - term_ascii.c
1 /* $Id: term_ascii.c,v 1.28 2014/08/10 23:54:41 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"
40 * Sadly, this doesn't seem to be defined on systems even when they
41 * support it. For the time being, remove it and let those compiling
42 * the software decide for themselves what to use.
45 #if ! defined(__STDC_ISO_10646__)
50 static struct termp
*ascii_init(enum termenc
, char *);
51 static double ascii_hspan(const struct termp
*,
52 const struct roffsu
*);
53 static size_t ascii_width(const struct termp
*, int);
54 static void ascii_advance(struct termp
*, size_t);
55 static void ascii_begin(struct termp
*);
56 static void ascii_end(struct termp
*);
57 static void ascii_endline(struct termp
*);
58 static void ascii_letter(struct termp
*, int);
59 static void ascii_setwidth(struct termp
*, int, size_t);
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);
70 ascii_init(enum termenc enc
, char *outopts
)
76 p
= mandoc_calloc(1, sizeof(struct termp
));
79 p
->defrmargin
= p
->lastrmargin
= 78;
81 p
->begin
= ascii_begin
;
83 p
->hspan
= ascii_hspan
;
84 p
->type
= TERMTYPE_CHAR
;
86 p
->enc
= TERMENC_ASCII
;
87 p
->advance
= ascii_advance
;
88 p
->endline
= ascii_endline
;
89 p
->letter
= ascii_letter
;
90 p
->setwidth
= ascii_setwidth
;
91 p
->width
= ascii_width
;
94 if (TERMENC_ASCII
!= enc
) {
95 v
= TERMENC_LOCALE
== enc
?
96 setlocale(LC_ALL
, "") :
97 setlocale(LC_CTYPE
, "en_US.UTF-8");
98 if (NULL
!= v
&& MB_CUR_MAX
> 1) {
100 p
->advance
= locale_advance
;
101 p
->endline
= locale_endline
;
102 p
->letter
= locale_letter
;
103 p
->width
= locale_width
;
113 while (outopts
&& *outopts
)
114 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
116 p
->defindent
= (size_t)atoi(v
);
119 p
->defrmargin
= (size_t)atoi(v
);
123 * Temporary, undocumented mode
124 * to imitate mdoc(7) output style.
133 /* Enforce a lower boundary. */
134 if (p
->defrmargin
< 58)
141 ascii_alloc(char *outopts
)
144 return(ascii_init(TERMENC_ASCII
, outopts
));
148 utf8_alloc(char *outopts
)
151 return(ascii_init(TERMENC_UTF8
, outopts
));
155 locale_alloc(char *outopts
)
158 return(ascii_init(TERMENC_LOCALE
, outopts
));
162 ascii_setwidth(struct termp
*p
, int iop
, size_t width
)
165 p
->rmargin
= p
->defrmargin
;
167 p
->defrmargin
+= width
;
169 p
->defrmargin
-= width
;
171 p
->defrmargin
= width
? width
: p
->lastrmargin
;
172 p
->lastrmargin
= p
->rmargin
;
173 p
->rmargin
= p
->maxrmargin
= p
->defrmargin
;
177 ascii_width(const struct termp
*p
, int c
)
184 ascii_free(void *arg
)
187 term_free((struct termp
*)arg
);
191 ascii_letter(struct termp
*p
, int c
)
198 ascii_begin(struct termp
*p
)
201 (*p
->headf
)(p
, p
->argf
);
205 ascii_end(struct termp
*p
)
208 (*p
->footf
)(p
, p
->argf
);
212 ascii_endline(struct termp
*p
)
219 ascii_advance(struct termp
*p
, size_t len
)
223 for (i
= 0; i
< len
; i
++)
228 ascii_hspan(const struct termp
*p
, const struct roffsu
*su
)
233 * Approximate based on character width. These are generated
234 * entirely by eyeballing the screen, but appear to be correct.
242 r
= su
->scale
* 10.0;
245 r
= (su
->scale
* 10.0) / 6.0;
248 r
= (su
->scale
* 10.0) / 72.0;
251 r
= su
->scale
/ 1000.0;
254 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
)