1 #include "vterm_internal.h"
2
3 // ### The following from http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
4 // With modifications:
5 // made functions static
6 // moved 'combining' table to file scope, so other functions can see it
7 // ###################################################################
8
9 /*
10 * This is an implementation of wcwidth() and wcswidth() (defined in
11 * IEEE Std 1002.1-2001) for Unicode.
12 *
13 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
14 * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
15 *
16 * In fixed-width output devices, Latin characters all occupy a single
17 * "cell" position of equal width, whereas ideographic CJK characters
18 * occupy two such cells. Interoperability between terminal-line
19 * applications and (teletype-style) character terminals using the
20 * UTF-8 encoding requires agreement on which character should advance
21 * the cursor by how many cell positions. No established formal
22 * standards exist at present on which Unicode character shall occupy
23 * how many cell positions on character terminals. These routines are
24 * a first attempt of defining such behavior based on simple rules
25 * applied to data provided by the Unicode Consortium.
26 *
27 * For some graphical characters, the Unicode standard explicitly
28 * defines a character-cell width via the definition of the East Asian
29 * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
30 * In all these cases, there is no ambiguity about which width a
31 * terminal shall use. For characters in the East Asian Ambiguous (A)
32 * class, the width choice depends purely on a preference of backward
33 * compatibility with either historic CJK or Western practice.
34 * Choosing single-width for these characters is easy to justify as
35 * the appropriate long-term solution, as the CJK practice of
36 * displaying these characters as double-width comes from historic
37 * implementation simplicity (8-bit encoded characters were displayed
38 * single-width and 16-bit ones double-width, even for Greek,
39 * Cyrillic, etc.) and not any typographic considerations.
40 *
41 * Much less clear is the choice of width for the Not East Asian
42 * (Neutral) class. Existing practice does not dictate a width for any
43 * of these characters. It would nevertheless make sense
44 * typographically to allocate two character cells to characters such
45 * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
46 * represented adequately with a single-width glyph. The following
47 * routines at present merely assign a single-cell width to all
48 * neutral characters, in the interest of simplicity. This is not
49 * entirely satisfactory and should be reconsidered before
50 * establishing a formal standard in this area. At the moment, the
51 * decision which Not East Asian (Neutral) characters should be
52 * represented by double-width glyphs cannot yet be answered by
53 * applying a simple rule from the Unicode database content. Setting
54 * up a proper standard for the behavior of UTF-8 character terminals
55 * will require a careful analysis not only of each Unicode character,
56 * but also of each presentation form, something the author of these
57 * routines has avoided to do so far.
58 *
59 * http://www.unicode.org/unicode/reports/tr11/
60 *
61 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
62 *
63 * Permission to use, copy, modify, and distribute this software
64 * for any purpose and without fee is hereby granted. The author
65 * disclaims all warranties with regard to this software.
66 *
67 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
68 */
69
70 struct interval {
71 int first;
72 int last;
73 };
74
75 #if !defined(WCWIDTH_FUNCTION) || !defined(IS_COMBINING_FUNCTION)
76
77 /* sorted list of non-overlapping intervals of non-spacing characters */
78 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
79 // Replaced by the combining table from Vim.
80 static const struct interval combining[] = {
81 {0X0300, 0X036F},
82 {0X0483, 0X0489},
83 {0X0591, 0X05BD},
84 {0X05BF, 0X05BF},
85 {0X05C1, 0X05C2},
86 {0X05C4, 0X05C5},
87 {0X05C7, 0X05C7},
88 {0X0610, 0X061A},
89 {0X064B, 0X065F},
90 {0X0670, 0X0670},
91 {0X06D6, 0X06DC},
92 {0X06DF, 0X06E4},
93 {0X06E7, 0X06E8},
94 {0X06EA, 0X06ED},
95 {0X0711, 0X0711},
96 {0X0730, 0X074A},
97 {0X07A6, 0X07B0},
98 {0X07EB, 0X07F3},
99 {0X07FD, 0X07FD},
100 {0X0816, 0X0819},
101 {0X081B, 0X0823},
102 {0X0825, 0X0827},
103 {0X0829, 0X082D},
104 {0X0859, 0X085B},
105 {0X08D3, 0X08E1},
106 {0X08E3, 0X0903},
107 {0X093A, 0X093C},
108 {0X093E, 0X094F},
109 {0X0951, 0X0957},
110 {0X0962, 0X0963},
111 {0X0981, 0X0983},
112 {0X09BC, 0X09BC},
113 {0X09BE, 0X09C4},
114 {0X09C7, 0X09C8},
115 {0X09CB, 0X09CD},
116 {0X09D7, 0X09D7},
117 {0X09E2, 0X09E3},
118 {0X09FE, 0X09FE},
119 {0X0A01, 0X0A03},
120 {0X0A3C, 0X0A3C},
121 {0X0A3E, 0X0A42},
122 {0X0A47, 0X0A48},
123 {0X0A4B, 0X0A4D},
124 {0X0A51, 0X0A51},
125 {0X0A70, 0X0A71},
126 {0X0A75, 0X0A75},
127 {0X0A81, 0X0A83},
128 {0X0ABC, 0X0ABC},
129 {0X0ABE, 0X0AC5},
130 {0X0AC7, 0X0AC9},
131 {0X0ACB, 0X0ACD},
132 {0X0AE2, 0X0AE3},
133 {0X0AFA, 0X0AFF},
134 {0X0B01, 0X0B03},
135 {0X0B3C, 0X0B3C},
136 {0X0B3E, 0X0B44},
137 {0X0B47, 0X0B48},
138 {0X0B4B, 0X0B4D},
139 {0X0B56, 0X0B57},
140 {0X0B62, 0X0B63},
141 {0X0B82, 0X0B82},
142 {0X0BBE, 0X0BC2},
143 {0X0BC6, 0X0BC8},
144 {0X0BCA, 0X0BCD},
145 {0X0BD7, 0X0BD7},
146 {0X0C00, 0X0C04},
147 {0X0C3E, 0X0C44},
148 {0X0C46, 0X0C48},
149 {0X0C4A, 0X0C4D},
150 {0X0C55, 0X0C56},
151 {0X0C62, 0X0C63},
152 {0X0C81, 0X0C83},
153 {0X0CBC, 0X0CBC},
154 {0X0CBE, 0X0CC4},
155 {0X0CC6, 0X0CC8},
156 {0X0CCA, 0X0CCD},
157 {0X0CD5, 0X0CD6},
158 {0X0CE2, 0X0CE3},
159 {0X0D00, 0X0D03},
160 {0X0D3B, 0X0D3C},
161 {0X0D3E, 0X0D44},
162 {0X0D46, 0X0D48},
163 {0X0D4A, 0X0D4D},
164 {0X0D57, 0X0D57},
165 {0X0D62, 0X0D63},
166 {0X0D82, 0X0D83},
167 {0X0DCA, 0X0DCA},
168 {0X0DCF, 0X0DD4},
169 {0X0DD6, 0X0DD6},
170 {0X0DD8, 0X0DDF},
171 {0X0DF2, 0X0DF3},
172 {0X0E31, 0X0E31},
173 {0X0E34, 0X0E3A},
174 {0X0E47, 0X0E4E},
175 {0X0EB1, 0X0EB1},
176 {0X0EB4, 0X0EBC},
177 {0X0EC8, 0X0ECD},
178 {0X0F18, 0X0F19},
179 {0X0F35, 0X0F35},
180 {0X0F37, 0X0F37},
181 {0X0F39, 0X0F39},
182 {0X0F3E, 0X0F3F},
183 {0X0F71, 0X0F84},
184 {0X0F86, 0X0F87},
185 {0X0F8D, 0X0F97},
186 {0X0F99, 0X0FBC},
187 {0X0FC6, 0X0FC6},
188 {0X102B, 0X103E},
189 {0X1056, 0X1059},
190 {0X105E, 0X1060},
191 {0X1062, 0X1064},
192 {0X1067, 0X106D},
193 {0X1071, 0X1074},
194 {0X1082, 0X108D},
195 {0X108F, 0X108F},
196 {0X109A, 0X109D},
197 {0X135D, 0X135F},
198 {0X1712, 0X1714},
199 {0X1732, 0X1734},
200 {0X1752, 0X1753},
201 {0X1772, 0X1773},
202 {0X17B4, 0X17D3},
203 {0X17DD, 0X17DD},
204 {0X180B, 0X180D},
205 {0X1885, 0X1886},
206 {0X18A9, 0X18A9},
207 {0X1920, 0X192B},
208 {0X1930, 0X193B},
209 {0X1A17, 0X1A1B},
210 {0X1A55, 0X1A5E},
211 {0X1A60, 0X1A7C},
212 {0X1A7F, 0X1A7F},
213 {0X1AB0, 0X1ABE},
214 {0X1B00, 0X1B04},
215 {0X1B34, 0X1B44},
216 {0X1B6B, 0X1B73},
217 {0X1B80, 0X1B82},
218 {0X1BA1, 0X1BAD},
219 {0X1BE6, 0X1BF3},
220 {0X1C24, 0X1C37},
221 {0X1CD0, 0X1CD2},
222 {0X1CD4, 0X1CE8},
223 {0X1CED, 0X1CED},
224 {0X1CF4, 0X1CF4},
225 {0X1CF7, 0X1CF9},
226 {0X1DC0, 0X1DF9},
227 {0X1DFB, 0X1DFF},
228 {0X20D0, 0X20F0},
229 {0X2CEF, 0X2CF1},
230 {0X2D7F, 0X2D7F},
231 {0X2DE0, 0X2DFF},
232 {0X302A, 0X302F},
233 {0X3099, 0X309A},
234 {0XA66F, 0XA672},
235 {0XA674, 0XA67D},
236 {0XA69E, 0XA69F},
237 {0XA6F0, 0XA6F1},
238 {0XA802, 0XA802},
239 {0XA806, 0XA806},
240 {0XA80B, 0XA80B},
241 {0XA823, 0XA827},
242 {0XA880, 0XA881},
243 {0XA8B4, 0XA8C5},
244 {0XA8E0, 0XA8F1},
245 {0XA8FF, 0XA8FF},
246 {0XA926, 0XA92D},
247 {0XA947, 0XA953},
248 {0XA980, 0XA983},
249 {0XA9B3, 0XA9C0},
250 {0XA9E5, 0XA9E5},
251 {0XAA29, 0XAA36},
252 {0XAA43, 0XAA43},
253 {0XAA4C, 0XAA4D},
254 {0XAA7B, 0XAA7D},
255 {0XAAB0, 0XAAB0},
256 {0XAAB2, 0XAAB4},
257 {0XAAB7, 0XAAB8},
258 {0XAABE, 0XAABF},
259 {0XAAC1, 0XAAC1},
260 {0XAAEB, 0XAAEF},
261 {0XAAF5, 0XAAF6},
262 {0XABE3, 0XABEA},
263 {0XABEC, 0XABED},
264 {0XFB1E, 0XFB1E},
265 {0XFE00, 0XFE0F},
266 {0XFE20, 0XFE2F},
267 {0X101FD, 0X101FD},
268 {0X102E0, 0X102E0},
269 {0X10376, 0X1037A},
270 {0X10A01, 0X10A03},
271 {0X10A05, 0X10A06},
272 {0X10A0C, 0X10A0F},
273 {0X10A38, 0X10A3A},
274 {0X10A3F, 0X10A3F},
275 {0X10AE5, 0X10AE6},
276 {0X10D24, 0X10D27},
277 {0X10F46, 0X10F50},
278 {0X11000, 0X11002},
279 {0X11038, 0X11046},
280 {0X1107F, 0X11082},
281 {0X110B0, 0X110BA},
282 {0X11100, 0X11102},
283 {0X11127, 0X11134},
284 {0X11145, 0X11146},
285 {0X11173, 0X11173},
286 {0X11180, 0X11182},
287 {0X111B3, 0X111C0},
288 {0X111C9, 0X111CC},
289 {0X1122C, 0X11237},
290 {0X1123E, 0X1123E},
291 {0X112DF, 0X112EA},
292 {0X11300, 0X11303},
293 {0X1133B, 0X1133C},
294 {0X1133E, 0X11344},
295 {0X11347, 0X11348},
296 {0X1134B, 0X1134D},
297 {0X11357, 0X11357},
298 {0X11362, 0X11363},
299 {0X11366, 0X1136C},
300 {0X11370, 0X11374},
301 {0X11435, 0X11446},
302 {0X1145E, 0X1145E},
303 {0X114B0, 0X114C3},
304 {0X115AF, 0X115B5},
305 {0X115B8, 0X115C0},
306 {0X115DC, 0X115DD},
307 {0X11630, 0X11640},
308 {0X116AB, 0X116B7},
309 {0X1171D, 0X1172B},
310 {0X1182C, 0X1183A},
311 {0X119D1, 0X119D7},
312 {0X119DA, 0X119E0},
313 {0X119E4, 0X119E4},
314 {0X11A01, 0X11A0A},
315 {0X11A33, 0X11A39},
316 {0X11A3B, 0X11A3E},
317 {0X11A47, 0X11A47},
318 {0X11A51, 0X11A5B},
319 {0X11A8A, 0X11A99},
320 {0X11C2F, 0X11C36},
321 {0X11C38, 0X11C3F},
322 {0X11C92, 0X11CA7},
323 {0X11CA9, 0X11CB6},
324 {0X11D31, 0X11D36},
325 {0X11D3A, 0X11D3A},
326 {0X11D3C, 0X11D3D},
327 {0X11D3F, 0X11D45},
328 {0X11D47, 0X11D47},
329 {0X11D8A, 0X11D8E},
330 {0X11D90, 0X11D91},
331 {0X11D93, 0X11D97},
332 {0X11EF3, 0X11EF6},
333 {0X16AF0, 0X16AF4},
334 {0X16B30, 0X16B36},
335 {0X16F4F, 0X16F4F},
336 {0X16F51, 0X16F87},
337 {0X16F8F, 0X16F92},
338 {0X1BC9D, 0X1BC9E},
339 {0X1D165, 0X1D169},
340 {0X1D16D, 0X1D172},
341 {0X1D17B, 0X1D182},
342 {0X1D185, 0X1D18B},
343 {0X1D1AA, 0X1D1AD},
344 {0X1D242, 0X1D244},
345 {0X1DA00, 0X1DA36},
346 {0X1DA3B, 0X1DA6C},
347 {0X1DA75, 0X1DA75},
348 {0X1DA84, 0X1DA84},
349 {0X1DA9B, 0X1DA9F},
350 {0X1DAA1, 0X1DAAF},
351 {0X1E000, 0X1E006},
352 {0X1E008, 0X1E018},
353 {0X1E01B, 0X1E021},
354 {0X1E023, 0X1E024},
355 {0X1E026, 0X1E02A},
356 {0X1E130, 0X1E136},
357 {0X1E2EC, 0X1E2EF},
358 {0X1E8D0, 0X1E8D6},
359 {0X1E944, 0X1E94A},
360 {0XE0100, 0XE01EF}
361 };
362 #endif
363
364 /* auxiliary function for binary search in interval table */
bisearch(uint32_t ucs,const struct interval * table,int max)365 static int bisearch(uint32_t ucs, const struct interval *table, int max) {
366 int min = 0;
367 int mid;
368
369 if ((int)ucs < table[0].first || (int)ucs > table[max].last)
370 return 0;
371 while (max >= min) {
372 mid = (min + max) / 2;
373 if ((int)ucs > table[mid].last)
374 min = mid + 1;
375 else if ((int)ucs < table[mid].first)
376 max = mid - 1;
377 else
378 return 1;
379 }
380
381 return 0;
382 }
383
384
385 /* The following two functions define the column width of an ISO 10646
386 * character as follows:
387 *
388 * - The null character (U+0000) has a column width of 0.
389 *
390 * - Other C0/C1 control characters and DEL will lead to a return
391 * value of -1.
392 *
393 * - Non-spacing and enclosing combining characters (general
394 * category code Mn or Me in the Unicode database) have a
395 * column width of 0.
396 *
397 * - SOFT HYPHEN (U+00AD) has a column width of 1.
398 *
399 * - Other format characters (general category code Cf in the Unicode
400 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
401 *
402 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
403 * have a column width of 0.
404 *
405 * - Spacing characters in the East Asian Wide (W) or East Asian
406 * Full-width (F) category as defined in Unicode Technical
407 * Report #11 have a column width of 2.
408 *
409 * - All remaining characters (including all printable
410 * ISO 8859-1 and WGL4 characters, Unicode control characters,
411 * etc.) have a column width of 1.
412 *
413 * This implementation assumes that uint32_t characters are encoded
414 * in ISO 10646.
415 */
416
417 #ifdef WCWIDTH_FUNCTION
418 // use a provided wcwidth() function
419 int WCWIDTH_FUNCTION(uint32_t ucs);
420 #else
421 # define WCWIDTH_FUNCTION mk_wcwidth
422
mk_wcwidth(uint32_t ucs)423 static int mk_wcwidth(uint32_t ucs)
424 {
425 /* test for 8-bit control characters */
426 if (ucs == 0)
427 return 0;
428 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
429 return -1;
430
431 /* binary search in table of non-spacing characters */
432 if (bisearch(ucs, combining,
433 sizeof(combining) / sizeof(struct interval) - 1))
434 return 0;
435
436 /* if we arrive here, ucs is not a combining or C0/C1 control character */
437
438 return 1 +
439 (ucs >= 0x1100 &&
440 (ucs <= 0x115f || /* Hangul Jamo init. consonants */
441 ucs == 0x2329 || ucs == 0x232a ||
442 (ucs >= 0x2e80 && ucs <= 0xa4cf &&
443 ucs != 0x303f) || /* CJK ... Yi */
444 (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
445 (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
446 (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
447 (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
448 (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
449 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
450 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
451 (ucs >= 0x30000 && ucs <= 0x3fffd)));
452 }
453 #endif
454
455 #if 0 // unused
456 static int mk_wcswidth(const uint32_t *pwcs, size_t n)
457 {
458 int w, width = 0;
459
460 for (;*pwcs && n-- > 0; pwcs++)
461 if ((w = mk_wcwidth(*pwcs)) < 0)
462 return -1;
463 else
464 width += w;
465
466 return width;
467 }
468
469
470 /*
471 * The following functions are the same as mk_wcwidth() and
472 * mk_wcswidth(), except that spacing characters in the East Asian
473 * Ambiguous (A) category as defined in Unicode Technical Report #11
474 * have a column width of 2. This variant might be useful for users of
475 * CJK legacy encodings who want to migrate to UCS without changing
476 * the traditional terminal character-width behaviour. It is not
477 * otherwise recommended for general use.
478 */
479 static int mk_wcwidth_cjk(uint32_t ucs)
480 {
481 #endif
482 /* sorted list of non-overlapping intervals of East Asian Ambiguous
483 * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
484 static const struct interval ambiguous[] = {
485 { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 },
486 { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 },
487 { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 },
488 { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 },
489 { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED },
490 { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA },
491 { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 },
492 { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B },
493 { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 },
494 { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 },
495 { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 },
496 { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE },
497 { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 },
498 { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA },
499 { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 },
500 { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB },
501 { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB },
502 { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 },
503 { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 },
504 { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 },
505 { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 },
506 { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 },
507 { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 },
508 { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 },
509 { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC },
510 { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 },
511 { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 },
512 { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 },
513 { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 },
514 { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 },
515 { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 },
516 { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B },
517 { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 },
518 { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 },
519 { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E },
520 { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 },
521 { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 },
522 { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F },
523 { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 },
524 { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF },
525 { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B },
526 { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 },
527 { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 },
528 { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 },
529 { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 },
530 { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 },
531 { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 },
532 { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 },
533 { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 },
534 { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F },
535 { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF },
536 { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD }
537 };
538 #if 0
539
540 /* binary search in table of non-spacing characters */
541 if (bisearch(ucs, ambiguous,
542 sizeof(ambiguous) / sizeof(struct interval) - 1))
543 return 2;
544
545 return mk_wcwidth(ucs);
546 }
547
548
549 static int mk_wcswidth_cjk(const uint32_t *pwcs, size_t n)
550 {
551 int w, width = 0;
552
553 for (;*pwcs && n-- > 0; pwcs++)
554 if ((w = mk_wcwidth_cjk(*pwcs)) < 0)
555 return -1;
556 else
557 width += w;
558
559 return width;
560 }
561 #endif
562
vterm_unicode_is_ambiguous(uint32_t codepoint)563 INTERNAL int vterm_unicode_is_ambiguous(uint32_t codepoint)
564 {
565 return (bisearch(codepoint, ambiguous,
566 sizeof(ambiguous) / sizeof(struct interval) - 1)) ? 1 : 0;
567 }
568
569 #ifdef IS_COMBINING_FUNCTION
570 // Use a provided is_combining() function.
571 int IS_COMBINING_FUNCTION(uint32_t codepoint);
572 #else
573 # define IS_COMBINING_FUNCTION vterm_is_combining
574 static int
vterm_is_combining(uint32_t codepoint)575 vterm_is_combining(uint32_t codepoint)
576 {
577 return bisearch(codepoint, combining, sizeof(combining) / sizeof(struct interval) - 1);
578 }
579 #endif
580
581 #ifdef GET_SPECIAL_PTY_TYPE_FUNCTION
582 int GET_SPECIAL_PTY_TYPE_FUNCTION(void);
583 #else
584 # define GET_SPECIAL_PTY_TYPE_FUNCTION vterm_get_special_pty_type_placeholder
585 static int
vterm_get_special_pty_type_placeholder(void)586 vterm_get_special_pty_type_placeholder(void)
587 {
588 return 0;
589 }
590 #endif
591
592 // ################################
593 // ### The rest added by Paul Evans
594
595 static const struct interval fullwidth[] = {
596 #include "fullwidth.inc"
597 };
598
vterm_unicode_width(uint32_t codepoint)599 INTERNAL int vterm_unicode_width(uint32_t codepoint)
600 {
601 if(bisearch(codepoint, fullwidth, sizeof(fullwidth) / sizeof(fullwidth[0]) - 1))
602 return 2;
603
604 return WCWIDTH_FUNCTION(codepoint);
605 }
606
vterm_unicode_is_combining(uint32_t codepoint)607 INTERNAL int vterm_unicode_is_combining(uint32_t codepoint)
608 {
609 return IS_COMBINING_FUNCTION(codepoint);
610 }
611
vterm_get_special_pty_type(void)612 INTERNAL int vterm_get_special_pty_type(void)
613 {
614 return GET_SPECIAL_PTY_TYPE_FUNCTION();
615 }
616