xref: /openbsd/lib/libcurses/widechar/lib_wacs.c (revision c7ef0cfc)
1 /* $OpenBSD: lib_wacs.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2018,2020 Thomas E. Dickey                                     *
5  * Copyright 2002-2015,2016 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Thomas Dickey 2002                                              *
34  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 MODULE_ID("$Id: lib_wacs.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
39 
40 NCURSES_EXPORT_VAR(cchar_t) * _nc_wacs = 0;
41 
42 NCURSES_EXPORT(void)
_nc_init_wacs(void)43 _nc_init_wacs(void)
44 {
45     /* *INDENT-OFF* */
46     static const struct {
47 	unsigned map;
48 	int	value[2];
49     } table[] = {
50 	/* VT100 symbols */
51 	{ 'l',	{ '+',	0x250c }},	/* upper left corner */
52 	{ 'm',	{ '+',	0x2514 }},	/* lower left corner */
53 	{ 'k',	{ '+',	0x2510 }},	/* upper right corner */
54 	{ 'j',	{ '+',	0x2518 }},	/* lower right corner */
55 	{ 't',	{ '+',	0x251c }},	/* tee pointing left */
56 	{ 'u',	{ '+',	0x2524 }},	/* tee pointing right */
57 	{ 'v',	{ '+',	0x2534 }},	/* tee pointing up */
58 	{ 'w',	{ '+',	0x252c }},	/* tee pointing down */
59 	{ 'q',	{ '-',	0x2500 }},	/* horizontal line */
60 	{ 'x',	{ '|',	0x2502 }},	/* vertical line */
61 	{ 'n',	{ '+',	0x253c }},	/* large plus or crossover */
62 	{ 'o',	{ '~',	0x23ba }},	/* scan line 1 */
63 	{ 's',	{ '_',	0x23bd }},	/* scan line 9 */
64 	{ '`',	{ '+',	0x25c6 }},	/* diamond */
65 	{ 'a',	{ ':',	0x2592 }},	/* checker board (stipple) */
66 	{ 'f',	{ '\'',	0x00b0 }},	/* degree symbol */
67 	{ 'g',	{ '#',	0x00b1 }},	/* plus/minus */
68 	{ '~',	{ 'o',	0x00b7 }},	/* bullet */
69 	/* Teletype 5410v1 symbols */
70 	{ ',',	{ '<',	0x2190 }},	/* arrow pointing left */
71 	{ '+',	{ '>',	0x2192 }},	/* arrow pointing right */
72 	{ '.',	{ 'v',	0x2193 }},	/* arrow pointing down */
73 	{ '-',	{ '^',	0x2191 }},	/* arrow pointing up */
74 	{ 'h',	{ '#',	0x2592 }},	/* board of squares */
75 	{ 'i',	{ '#',	0x2603 }},	/* lantern symbol */
76 	{ '0',	{ '#',	0x25ae }},	/* solid square block */
77 	/* these defaults were invented for ncurses */
78 	{ 'p',	{ '-',	0x23bb }},	/* scan line 3 */
79 	{ 'r',	{ '-',	0x23bc }},	/* scan line 7 */
80 	{ 'y',	{ '<',	0x2264 }},	/* less-than-or-equal-to */
81 	{ 'z',	{ '>',	0x2265 }},	/* greater-than-or-equal-to */
82 	{ '{',	{ '*',	0x03c0 }},	/* greek pi */
83 	{ '|',	{ '!',	0x2260 }},	/* not-equal */
84 	{ '}',	{ 'f',	0x00a3 }},	/* pound-sterling symbol */
85 	/* thick-line-drawing */
86 	{ 'L',	{ '+',	0x250f }},	/* upper left corner */
87 	{ 'M',	{ '+',	0x2517 }},	/* lower left corner */
88 	{ 'K',	{ '+',	0x2513 }},	/* upper right corner */
89 	{ 'J',	{ '+',	0x251b }},	/* lower right corner */
90 	{ 'T',	{ '+',	0x2523 }},	/* tee pointing left */
91 	{ 'U',	{ '+',	0x252b }},	/* tee pointing right */
92 	{ 'V',	{ '+',	0x253b }},	/* tee pointing up */
93 	{ 'W',	{ '+',	0x2533 }},	/* tee pointing down */
94 	{ 'Q',	{ '-',	0x2501 }},	/* horizontal line */
95 	{ 'X',	{ '|',	0x2503 }},	/* vertical line */
96 	{ 'N',	{ '+',	0x254b }},	/* large plus or crossover */
97 	/* double-line-drawing */
98 	{ 'C',	{ '+',	0x2554 }},	/* upper left corner */
99 	{ 'D',	{ '+',	0x255a }},	/* lower left corner */
100 	{ 'B',	{ '+',	0x2557 }},	/* upper right corner */
101 	{ 'A',	{ '+',	0x255d }},	/* lower right corner */
102 	{ 'G',	{ '+',	0x2563 }},	/* tee pointing left */
103 	{ 'F',	{ '+',	0x2560 }},	/* tee pointing right */
104 	{ 'H',	{ '+',	0x2569 }},	/* tee pointing up */
105 	{ 'I',	{ '+',	0x2566 }},	/* tee pointing down */
106 	{ 'R',	{ '-',	0x2550 }},	/* horizontal line */
107 	{ 'Y',	{ '|',	0x2551 }},	/* vertical line */
108 	{ 'E',	{ '+',	0x256c }},	/* large plus or crossover */
109     };
110     /* *INDENT-ON* */
111 
112     int active = _nc_unicode_locale();
113 
114     /*
115      * If we're running in a UTF-8 locale, will use the Unicode equivalents
116      * rather than the terminfo information.  Actually the terminfo should
117      * be the rule, but there are people who are offended by the notion that
118      * a Unicode-capable terminal would have something resembling a mode.
119      * So the smacs/rmacs may be disabled -- sometime.
120      */
121     T(("initializing WIDE-ACS map (Unicode is%s active)",
122        active ? "" : " not"));
123 
124     if ((_nc_wacs = typeCalloc(cchar_t, ACS_LEN)) != 0) {
125 	unsigned n;
126 
127 	for (n = 0; n < SIZEOF(table); ++n) {
128 	    unsigned m;
129 #if NCURSES_WCWIDTH_GRAPHICS
130 	    int wide = wcwidth((wchar_t) table[n].value[active]);
131 #else
132 	    int wide = 1;
133 #endif
134 
135 	    m = table[n].map;
136 	    if (active && (wide == 1)) {
137 		SetChar(_nc_wacs[m], table[n].value[1], A_NORMAL);
138 	    } else if (acs_map[m] & A_ALTCHARSET) {
139 		SetChar(_nc_wacs[m], m, A_ALTCHARSET);
140 	    } else {
141 		SetChar(_nc_wacs[m], table[n].value[0], A_NORMAL);
142 	    }
143 
144 	    T(("#%d, width:%d SetChar(%c, %s) = %s",
145 	       n, wide, m,
146 	       _tracechar(table[n].value[active]),
147 	       _tracecchar_t(&_nc_wacs[m])));
148 	}
149     }
150 }
151