1 /* Copyright (c) 2003 Corinna Vinschen <corinna@vinschen.de> */
2 /*
3 FUNCTION
4 	<<wcwidth>>---number of column positions of a wide-character code
5 
6 INDEX
7 	wcwidth
8 
9 SYNOPSIS
10 	#include <wchar.h>
11 	int wcwidth(const wchar_t <[wc]>);
12 
13 DESCRIPTION
14 	The <<wcwidth>> function shall determine the number of column
15 	positions required for the wide character <[wc]>. The application
16 	shall ensure that the value of <[wc]> is a character representable
17 	as a wint_t (combining Unicode surrogate pairs into single 21-bit
18 	Unicode code points), and is a wide-character code corresponding to a
19 	valid character in the current locale.
20 
21 RETURNS
22 	The <<wcwidth>> function shall either return 0 (if <[wc]> is a null
23 	wide-character code), or return the number of column positions to
24 	be occupied by the wide-character code <[wc]>, or return -1 (if <[wc]>
25 	does not correspond to a printable wide-character code).
26 
27 PORTABILITY
28 <<wcwidth>> has been introduced in the Single UNIX Specification Volume 2.
29 <<wcwidth>> has been marked as an extension in the Single UNIX Specification Volume 3.
30 */
31 
32 /*
33  * This is an implementation of wcwidth() (defined in
34  * IEEE Std 1002.1-2001) for Unicode.
35  *
36  * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
37  *
38  * In fixed-width output devices, Latin characters all occupy a single
39  * "cell" position of equal width, whereas ideographic CJK characters
40  * occupy two such cells. Interoperability between terminal-line
41  * applications and (teletype-style) character terminals using the
42  * UTF-8 encoding requires agreement on which character should advance
43  * the cursor by how many cell positions. No established formal
44  * standards exist at present on which Unicode character shall occupy
45  * how many cell positions on character terminals. These routines are
46  * a first attempt of defining such behavior based on simple rules
47  * applied to data provided by the Unicode Consortium.
48  *
49  * For some graphical characters, the Unicode standard explicitly
50  * defines a character-cell width via the definition of the East Asian
51  * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
52  * In all these cases, there is no ambiguity about which width a
53  * terminal shall use. For characters in the East Asian Ambiguous (A)
54  * class, the width choice depends purely on a preference of backward
55  * compatibility with either historic CJK or Western practice.
56  * Choosing single-width for these characters is easy to justify as
57  * the appropriate long-term solution, as the CJK practice of
58  * displaying these characters as double-width comes from historic
59  * implementation simplicity (8-bit encoded characters were displayed
60  * single-width and 16-bit ones double-width, even for Greek,
61  * Cyrillic, etc.) and not any typographic considerations.
62  *
63  * Much less clear is the choice of width for the Not East Asian
64  * (Neutral) class. Existing practice does not dictate a width for any
65  * of these characters. It would nevertheless make sense
66  * typographically to allocate two character cells to characters such
67  * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
68  * represented adequately with a single-width glyph. The following
69  * routines at present merely assign a single-cell width to all
70  * neutral characters, in the interest of simplicity. This is not
71  * entirely satisfactory and should be reconsidered before
72  * establishing a formal standard in this area. At the moment, the
73  * decision which Not East Asian (Neutral) characters should be
74  * represented by double-width glyphs cannot yet be answered by
75  * applying a simple rule from the Unicode database content. Setting
76  * up a proper standard for the behavior of UTF-8 character terminals
77  * will require a careful analysis not only of each Unicode character,
78  * but also of each presentation form, something the author of these
79  * routines has avoided to do so far.
80  *
81  * http://www.unicode.org/unicode/reports/tr11/
82  *
83  * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
84  *
85  * Permission to use, copy, modify, and distribute this software
86  * for any purpose and without fee is hereby granted. The author
87  * disclaims all warranties with regard to this software.
88  *
89  * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
90  */
91 
92 #include <_ansi.h>
93 #include <wchar.h>
94 #include <stdint.h>
95 #ifndef _MB_CAPABLE
96 #include <wctype.h> /* iswprint, iswcntrl */
97 #endif
98 #include "local.h"
99 
100 #ifdef _MB_CAPABLE
101 struct interval
102 {
103   uint32_t first;
104   uint32_t last;
105 };
106 
107 /* auxiliary function for binary search in interval table */
108 static int
bisearch(uint32_t ucs,const struct interval * table,int max)109 bisearch(uint32_t ucs, const struct interval *table, int max)
110 {
111   int min = 0;
112   int mid;
113 
114   if (ucs < table[0].first || ucs > table[max].last)
115     return 0;
116   while (max >= min)
117     {
118       mid = (min + max) / 2;
119       if (ucs > table[mid].last)
120 	min = mid + 1;
121       else if (ucs < table[mid].first)
122 	max = mid - 1;
123       else
124 	return 1;
125     }
126 
127   return 0;
128 }
129 #endif /* _MB_CAPABLE */
130 
131 /* The following function defines the column width of an ISO 10646
132  * character as follows:
133  *
134  *    - The null character (U+0000) has a column width of 0.
135  *
136  *    - Other C0/C1 control characters and DEL will lead to a return
137  *      value of -1.
138  *
139  *    - If the current language is recognized as a language usually using
140  *      CJK fonts, spacing characters in the East Asian Ambiguous (A)
141  *      category as defined in Unicode Technical Report #11 have a column
142  *      width of 2.
143  *
144  *    - Non-spacing and enclosing combining characters (general
145  *      category code Mn or Me in the Unicode database) have a
146  *      column width of 0.
147  *
148  *    - SOFT HYPHEN (U+00AD) has a column width of 1.
149  *
150  *    - Other format characters (general category code Cf in the Unicode
151  *      database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
152  *
153  *    - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
154  *      have a column width of 0.
155  *
156  *    - Spacing characters in the East Asian Wide (W) or East Asian
157  *      Full-width (F) category as defined in Unicode Technical
158  *      Report #11 have a column width of 2.
159  *
160  *    - All remaining characters (including all printable
161  *      ISO 8859-1 and WGL4 characters, Unicode control characters,
162  *      etc.) have a column width of 1.
163  *
164  * This implementation assumes that wint_t characters are encoded
165  * in ISO 10646.
166  */
167 
168 int
__wcwidth(const wint_t _ucs)169 __wcwidth (const wint_t _ucs)
170 {
171   uint32_t ucs = (uint32_t) _ucs;
172 #ifdef _MB_CAPABLE
173   /* sorted list of non-overlapping intervals of East Asian Ambiguous chars */
174   static const struct interval ambiguous[] =
175 #include "ambiguous.t"
176 
177   /* sorted list of non-overlapping intervals of non-spacing characters */
178   static const struct interval combining[] =
179 #include "combining.t"
180 
181   /* sorted list of non-overlapping intervals of wide characters,
182      ranges extended to Blocks where possible
183    */
184   static const struct interval wide[] =
185 #include "wide.t"
186 
187   /* Test for NUL character */
188   if (ucs == 0)
189     return 0;
190 
191   /* Test for printable ASCII characters */
192   if (ucs >= 0x20 && ucs < 0x7f)
193     return 1;
194 
195   /* Test for control characters */
196   if (ucs < 0xa0)
197     return -1;
198 
199   /* Test for surrogate pair values. */
200   if (ucs >= (uint32_t) 0xd800 && ucs <= (uint32_t) 0xdfff)
201     return -1;
202 
203   /* check CJK width mode (1: ambiguous-wide, 0: normal, -1: disabled) */
204   int cjk_lang = __locale_cjk_lang ();
205 
206   /* binary search in table of ambiguous characters */
207   if (cjk_lang > 0
208       && bisearch(ucs, ambiguous,
209 		  sizeof(ambiguous) / sizeof(struct interval) - 1))
210     return 2;
211 
212   /* binary search in table of non-spacing characters */
213   if (bisearch(ucs, combining,
214 	       sizeof(combining) / sizeof(struct interval) - 1))
215     return 0;
216 
217   /* if we arrive here, ucs is not a combining or C0/C1 control character */
218 
219   /* binary search in table of wide character codes */
220   if (cjk_lang >= 0
221       && bisearch(ucs, wide,
222 	       sizeof(wide) / sizeof(struct interval) - 1))
223     return 2;
224   else
225     return 1;
226 #else /* !_MB_CAPABLE */
227   if (iswprint (ucs))
228     return 1;
229   if (iswcntrl (ucs) || ucs == L'\0')
230     return 0;
231   return -1;
232 #endif /* _MB_CAPABLE */
233 }
234 
235 int
wcwidth(const wchar_t wc)236 wcwidth (const wchar_t wc)
237 {
238   wint_t wi = wc;
239 
240 #ifdef _MB_CAPABLE
241   wi = _jp2uc (wi);
242 #endif /* _MB_CAPABLE */
243   return __wcwidth (wi);
244 }
245