3 <<wcwidth>>---number of column positions of a wide-character code
10 int wcwidth(const wint_t <[wc]>);
13 The <<wcwidth>> function shall determine the number of column
14 positions required for the wide character <[wc]>. The application
15 shall ensure that the value of <[wc]> is a character representable
16 as a wint_t (combining Unicode surrogate pairs into single 21-bit
17 Unicode code points), and is a wide-character code corresponding to a
18 valid character in the current locale.
21 The <<wcwidth>> function shall either return 0 (if <[wc]> is a null
22 wide-character code), or return the number of column positions to
23 be occupied by the wide-character code <[wc]>, or return -1 (if <[wc]>
24 does not correspond to a printable wide-character code).
27 <<wcwidth>> has been introduced in the Single UNIX Specification Volume 2.
28 <<wcwidth>> has been marked as an extension in the Single UNIX Specification Volume 3.
32 * This is an implementation of wcwidth() (defined in
33 * IEEE Std 1002.1-2001) for Unicode.
35 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
37 * In fixed-width output devices, Latin characters all occupy a single
38 * "cell" position of equal width, whereas ideographic CJK characters
39 * occupy two such cells. Interoperability between terminal-line
40 * applications and (teletype-style) character terminals using the
41 * UTF-8 encoding requires agreement on which character should advance
42 * the cursor by how many cell positions. No established formal
43 * standards exist at present on which Unicode character shall occupy
44 * how many cell positions on character terminals. These routines are
45 * a first attempt of defining such behavior based on simple rules
46 * applied to data provided by the Unicode Consortium.
48 * For some graphical characters, the Unicode standard explicitly
49 * defines a character-cell width via the definition of the East Asian
50 * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
51 * In all these cases, there is no ambiguity about which width a
52 * terminal shall use. For characters in the East Asian Ambiguous (A)
53 * class, the width choice depends purely on a preference of backward
54 * compatibility with either historic CJK or Western practice.
55 * Choosing single-width for these characters is easy to justify as
56 * the appropriate long-term solution, as the CJK practice of
57 * displaying these characters as double-width comes from historic
58 * implementation simplicity (8-bit encoded characters were displayed
59 * single-width and 16-bit ones double-width, even for Greek,
60 * Cyrillic, etc.) and not any typographic considerations.
62 * Much less clear is the choice of width for the Not East Asian
63 * (Neutral) class. Existing practice does not dictate a width for any
64 * of these characters. It would nevertheless make sense
65 * typographically to allocate two character cells to characters such
66 * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
67 * represented adequately with a single-width glyph. The following
68 * routines at present merely assign a single-cell width to all
69 * neutral characters, in the interest of simplicity. This is not
70 * entirely satisfactory and should be reconsidered before
71 * establishing a formal standard in this area. At the moment, the
72 * decision which Not East Asian (Neutral) characters should be
73 * represented by double-width glyphs cannot yet be answered by
74 * applying a simple rule from the Unicode database content. Setting
75 * up a proper standard for the behavior of UTF-8 character terminals
76 * will require a careful analysis not only of each Unicode character,
77 * but also of each presentation form, something the author of these
78 * routines has avoided to do so far.
80 * http://www.unicode.org/unicode/reports/tr11/
82 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
84 * Permission to use, copy, modify, and distribute this software
85 * for any purpose and without fee is hereby granted. The author
86 * disclaims all warranties with regard to this software.
88 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
94 #include <wctype.h> /* iswprint, iswcntrl */
105 /* auxiliary function for binary search in interval table */
107 bisearch(wint_t ucs
, const struct interval
*table
, int max
)
112 if (ucs
< table
[0].first
|| ucs
> table
[max
].last
)
116 mid
= (min
+ max
) / 2;
117 if (ucs
> table
[mid
].last
)
119 else if (ucs
< table
[mid
].first
)
127 #endif /* _MB_CAPABLE */
129 /* The following function defines the column width of an ISO 10646
130 * character as follows:
132 * - The null character (U+0000) has a column width of 0.
134 * - Other C0/C1 control characters and DEL will lead to a return
137 * - If the current language is recognized as a language usually using
138 * CJK fonts, spacing characters in the East Asian Ambiguous (A)
139 * category as defined in Unicode Technical Report #11 have a column
142 * - Non-spacing and enclosing combining characters (general
143 * category code Mn or Me in the Unicode database) have a
146 * - SOFT HYPHEN (U+00AD) has a column width of 1.
148 * - Other format characters (general category code Cf in the Unicode
149 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
151 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
152 * have a column width of 0.
154 * - Spacing characters in the East Asian Wide (W) or East Asian
155 * Full-width (F) category as defined in Unicode Technical
156 * Report #11 have a column width of 2.
158 * - All remaining characters (including all printable
159 * ISO 8859-1 and WGL4 characters, Unicode control characters,
160 * etc.) have a column width of 1.
162 * This implementation assumes that wint_t characters are encoded
167 __wcwidth (const wint_t ucs
)
170 /* sorted list of non-overlapping intervals of East Asian Ambiguous chars */
171 static const struct interval ambiguous
[] =
172 #include "ambiguous.t"
174 /* sorted list of non-overlapping intervals of non-spacing characters */
175 static const struct interval combining
[] =
176 #include "combining.t"
178 /* sorted list of non-overlapping intervals of wide characters,
179 ranges extended to Blocks where possible
181 static const struct interval wide
[] =
184 /* Test for NUL character */
188 /* Test for printable ASCII characters */
189 if (ucs
>= 0x20 && ucs
< 0x7f)
192 /* Test for control characters */
196 /* Test for surrogate pair values. */
197 if (ucs
>= 0xd800 && ucs
<= 0xdfff)
200 /* check CJK width mode (1: ambiguous-wide, 0: normal, -1: disabled) */
201 int cjk_lang
= __locale_cjk_lang ();
203 /* binary search in table of ambiguous characters */
205 && bisearch(ucs
, ambiguous
,
206 sizeof(ambiguous
) / sizeof(struct interval
) - 1))
209 /* binary search in table of non-spacing characters */
210 if (bisearch(ucs
, combining
,
211 sizeof(combining
) / sizeof(struct interval
) - 1))
214 /* if we arrive here, ucs is not a combining or C0/C1 control character */
216 /* binary search in table of wide character codes */
218 && bisearch(ucs
, wide
,
219 sizeof(wide
) / sizeof(struct interval
) - 1))
223 #else /* !_MB_CAPABLE */
226 if (iswcntrl (ucs
) || ucs
== L
'\0')
229 #endif /* _MB_CAPABLE */
233 wcwidth (const wint_t wc
)
239 #endif /* _MB_CAPABLE */
240 return __wcwidth (wi
);