-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
CodepointWidthDetector.cpp
449 lines (432 loc) · 18 KB
/
CodepointWidthDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "inc/CodepointWidthDetector.hpp"
namespace
{
// used to store range data in CodepointWidthDetector's internal map
struct UnicodeRange final
{
char32_t lowerBound;
char32_t upperBound : 31;
char32_t isAmbiguous : 1;
};
static bool operator<(const UnicodeRange& range, const unsigned int searchTerm) noexcept
{
return range.upperBound < searchTerm;
}
// Generated by Generate-CodepointWidthsFromUCD.ps1 -Pack:True -Full: -NoOverrides:False
// on 2022-11-15 19:54:23Z from Unicode 15.0.0.
// 321149 (0x4E67D) codepoints covered.
// 240 (0xF0) codepoints overridden.
// Override path: .\src\types\unicode_width_overrides.xml
static constexpr std::array<UnicodeRange, 297> s_wideAndAmbiguousTable{
UnicodeRange{ 0xa1, 0xa1, 1 },
UnicodeRange{ 0xa4, 0xa4, 1 },
UnicodeRange{ 0xa7, 0xa8, 1 },
UnicodeRange{ 0xaa, 0xaa, 1 },
UnicodeRange{ 0xad, 0xae, 1 },
UnicodeRange{ 0xb0, 0xb4, 1 },
UnicodeRange{ 0xb6, 0xba, 1 },
UnicodeRange{ 0xbc, 0xbf, 1 },
UnicodeRange{ 0xc6, 0xc6, 1 },
UnicodeRange{ 0xd0, 0xd0, 1 },
UnicodeRange{ 0xd7, 0xd8, 1 },
UnicodeRange{ 0xde, 0xe1, 1 },
UnicodeRange{ 0xe6, 0xe6, 1 },
UnicodeRange{ 0xe8, 0xea, 1 },
UnicodeRange{ 0xec, 0xed, 1 },
UnicodeRange{ 0xf0, 0xf0, 1 },
UnicodeRange{ 0xf2, 0xf3, 1 },
UnicodeRange{ 0xf7, 0xfa, 1 },
UnicodeRange{ 0xfc, 0xfc, 1 },
UnicodeRange{ 0xfe, 0xfe, 1 },
UnicodeRange{ 0x101, 0x101, 1 },
UnicodeRange{ 0x111, 0x111, 1 },
UnicodeRange{ 0x113, 0x113, 1 },
UnicodeRange{ 0x11b, 0x11b, 1 },
UnicodeRange{ 0x126, 0x127, 1 },
UnicodeRange{ 0x12b, 0x12b, 1 },
UnicodeRange{ 0x131, 0x133, 1 },
UnicodeRange{ 0x138, 0x138, 1 },
UnicodeRange{ 0x13f, 0x142, 1 },
UnicodeRange{ 0x144, 0x144, 1 },
UnicodeRange{ 0x148, 0x14b, 1 },
UnicodeRange{ 0x14d, 0x14d, 1 },
UnicodeRange{ 0x152, 0x153, 1 },
UnicodeRange{ 0x166, 0x167, 1 },
UnicodeRange{ 0x16b, 0x16b, 1 },
UnicodeRange{ 0x1ce, 0x1ce, 1 },
UnicodeRange{ 0x1d0, 0x1d0, 1 },
UnicodeRange{ 0x1d2, 0x1d2, 1 },
UnicodeRange{ 0x1d4, 0x1d4, 1 },
UnicodeRange{ 0x1d6, 0x1d6, 1 },
UnicodeRange{ 0x1d8, 0x1d8, 1 },
UnicodeRange{ 0x1da, 0x1da, 1 },
UnicodeRange{ 0x1dc, 0x1dc, 1 },
UnicodeRange{ 0x251, 0x251, 1 },
UnicodeRange{ 0x261, 0x261, 1 },
UnicodeRange{ 0x2c4, 0x2c4, 1 },
UnicodeRange{ 0x2c7, 0x2c7, 1 },
UnicodeRange{ 0x2c9, 0x2cb, 1 },
UnicodeRange{ 0x2cd, 0x2cd, 1 },
UnicodeRange{ 0x2d0, 0x2d0, 1 },
UnicodeRange{ 0x2d8, 0x2db, 1 },
UnicodeRange{ 0x2dd, 0x2dd, 1 },
UnicodeRange{ 0x2df, 0x2df, 1 },
UnicodeRange{ 0x300, 0x36f, 1 },
UnicodeRange{ 0x391, 0x3a1, 1 },
UnicodeRange{ 0x3a3, 0x3a9, 1 },
UnicodeRange{ 0x3b1, 0x3c1, 1 },
UnicodeRange{ 0x3c3, 0x3c9, 1 },
UnicodeRange{ 0x401, 0x401, 1 },
UnicodeRange{ 0x410, 0x44f, 1 },
UnicodeRange{ 0x451, 0x451, 1 },
UnicodeRange{ 0x1100, 0x115f, 0 },
UnicodeRange{ 0x2010, 0x2010, 1 },
UnicodeRange{ 0x2013, 0x2016, 1 },
UnicodeRange{ 0x2018, 0x2019, 1 },
UnicodeRange{ 0x201c, 0x201d, 1 },
UnicodeRange{ 0x2020, 0x2022, 1 },
UnicodeRange{ 0x2024, 0x2027, 1 },
UnicodeRange{ 0x2030, 0x2030, 1 },
UnicodeRange{ 0x2032, 0x2033, 1 },
UnicodeRange{ 0x2035, 0x2035, 1 },
UnicodeRange{ 0x203b, 0x203b, 1 },
UnicodeRange{ 0x203e, 0x203e, 1 },
UnicodeRange{ 0x2074, 0x2074, 1 },
UnicodeRange{ 0x207f, 0x207f, 1 },
UnicodeRange{ 0x2081, 0x2084, 1 },
UnicodeRange{ 0x20ac, 0x20ac, 1 },
UnicodeRange{ 0x2103, 0x2103, 1 },
UnicodeRange{ 0x2105, 0x2105, 1 },
UnicodeRange{ 0x2109, 0x2109, 1 },
UnicodeRange{ 0x2113, 0x2113, 1 },
UnicodeRange{ 0x2116, 0x2116, 1 },
UnicodeRange{ 0x2121, 0x2122, 1 },
UnicodeRange{ 0x2126, 0x2126, 1 },
UnicodeRange{ 0x212b, 0x212b, 1 },
UnicodeRange{ 0x2153, 0x2154, 1 },
UnicodeRange{ 0x215b, 0x215e, 1 },
UnicodeRange{ 0x2160, 0x216b, 1 },
UnicodeRange{ 0x2170, 0x2179, 1 },
UnicodeRange{ 0x2189, 0x2189, 1 },
UnicodeRange{ 0x2190, 0x2199, 1 },
UnicodeRange{ 0x21b8, 0x21b9, 1 },
UnicodeRange{ 0x21d2, 0x21d2, 1 },
UnicodeRange{ 0x21d4, 0x21d4, 1 },
UnicodeRange{ 0x21e7, 0x21e7, 1 },
UnicodeRange{ 0x2200, 0x2200, 1 },
UnicodeRange{ 0x2202, 0x2203, 1 },
UnicodeRange{ 0x2207, 0x2208, 1 },
UnicodeRange{ 0x220b, 0x220b, 1 },
UnicodeRange{ 0x220f, 0x220f, 1 },
UnicodeRange{ 0x2211, 0x2211, 1 },
UnicodeRange{ 0x2215, 0x2215, 1 },
UnicodeRange{ 0x221a, 0x221a, 1 },
UnicodeRange{ 0x221d, 0x2220, 1 },
UnicodeRange{ 0x2223, 0x2223, 1 },
UnicodeRange{ 0x2225, 0x2225, 1 },
UnicodeRange{ 0x2227, 0x222c, 1 },
UnicodeRange{ 0x222e, 0x222e, 1 },
UnicodeRange{ 0x2234, 0x2237, 1 },
UnicodeRange{ 0x223c, 0x223d, 1 },
UnicodeRange{ 0x2248, 0x2248, 1 },
UnicodeRange{ 0x224c, 0x224c, 1 },
UnicodeRange{ 0x2252, 0x2252, 1 },
UnicodeRange{ 0x2260, 0x2261, 1 },
UnicodeRange{ 0x2264, 0x2267, 1 },
UnicodeRange{ 0x226a, 0x226b, 1 },
UnicodeRange{ 0x226e, 0x226f, 1 },
UnicodeRange{ 0x2282, 0x2283, 1 },
UnicodeRange{ 0x2286, 0x2287, 1 },
UnicodeRange{ 0x2295, 0x2295, 1 },
UnicodeRange{ 0x2299, 0x2299, 1 },
UnicodeRange{ 0x22a5, 0x22a5, 1 },
UnicodeRange{ 0x22bf, 0x22bf, 1 },
UnicodeRange{ 0x2312, 0x2312, 1 },
UnicodeRange{ 0x231a, 0x231b, 0 },
UnicodeRange{ 0x2329, 0x232a, 0 },
UnicodeRange{ 0x23e9, 0x23ec, 0 },
UnicodeRange{ 0x23f0, 0x23f0, 0 },
UnicodeRange{ 0x23f3, 0x23f3, 0 },
UnicodeRange{ 0x2460, 0x24e9, 1 },
UnicodeRange{ 0x24eb, 0x24ff, 1 },
UnicodeRange{ 0x25a0, 0x25a1, 1 },
UnicodeRange{ 0x25a3, 0x25a9, 1 },
UnicodeRange{ 0x25b2, 0x25b3, 1 },
UnicodeRange{ 0x25b6, 0x25b7, 1 },
UnicodeRange{ 0x25bc, 0x25bd, 1 },
UnicodeRange{ 0x25c0, 0x25c1, 1 },
UnicodeRange{ 0x25c6, 0x25c8, 1 },
UnicodeRange{ 0x25cb, 0x25cb, 1 },
UnicodeRange{ 0x25ce, 0x25d1, 1 },
UnicodeRange{ 0x25e2, 0x25e5, 1 },
UnicodeRange{ 0x25ef, 0x25ef, 1 },
UnicodeRange{ 0x25fd, 0x25fe, 0 },
UnicodeRange{ 0x2605, 0x2606, 1 },
UnicodeRange{ 0x2609, 0x2609, 1 },
UnicodeRange{ 0x260e, 0x260f, 1 },
UnicodeRange{ 0x2614, 0x2615, 0 },
UnicodeRange{ 0x261c, 0x261c, 1 },
UnicodeRange{ 0x261e, 0x261e, 1 },
UnicodeRange{ 0x2640, 0x2640, 1 },
UnicodeRange{ 0x2642, 0x2642, 1 },
UnicodeRange{ 0x2648, 0x2653, 0 },
UnicodeRange{ 0x2660, 0x2661, 1 },
UnicodeRange{ 0x2663, 0x2665, 1 },
UnicodeRange{ 0x2667, 0x266a, 1 },
UnicodeRange{ 0x266c, 0x266d, 1 },
UnicodeRange{ 0x266f, 0x266f, 1 },
UnicodeRange{ 0x267f, 0x267f, 0 },
UnicodeRange{ 0x2693, 0x2693, 0 },
UnicodeRange{ 0x269e, 0x269f, 1 },
UnicodeRange{ 0x26a1, 0x26a1, 0 },
UnicodeRange{ 0x26aa, 0x26ab, 0 },
UnicodeRange{ 0x26bd, 0x26be, 0 },
UnicodeRange{ 0x26bf, 0x26bf, 1 },
UnicodeRange{ 0x26c4, 0x26c5, 0 },
UnicodeRange{ 0x26c6, 0x26cd, 1 },
UnicodeRange{ 0x26ce, 0x26ce, 0 },
UnicodeRange{ 0x26cf, 0x26d3, 1 },
UnicodeRange{ 0x26d4, 0x26d4, 0 },
UnicodeRange{ 0x26d5, 0x26e1, 1 },
UnicodeRange{ 0x26e3, 0x26e3, 1 },
UnicodeRange{ 0x26e8, 0x26e9, 1 },
UnicodeRange{ 0x26ea, 0x26ea, 0 },
UnicodeRange{ 0x26eb, 0x26f1, 1 },
UnicodeRange{ 0x26f2, 0x26f3, 0 },
UnicodeRange{ 0x26f4, 0x26f4, 1 },
UnicodeRange{ 0x26f5, 0x26f5, 0 },
UnicodeRange{ 0x26f6, 0x26f9, 1 },
UnicodeRange{ 0x26fa, 0x26fa, 0 },
UnicodeRange{ 0x26fb, 0x26fc, 1 },
UnicodeRange{ 0x26fd, 0x26fd, 0 },
UnicodeRange{ 0x26fe, 0x26ff, 1 },
UnicodeRange{ 0x2705, 0x2705, 0 },
UnicodeRange{ 0x270a, 0x270b, 0 },
UnicodeRange{ 0x2728, 0x2728, 0 },
UnicodeRange{ 0x273d, 0x273d, 1 },
UnicodeRange{ 0x274c, 0x274c, 0 },
UnicodeRange{ 0x274e, 0x274e, 0 },
UnicodeRange{ 0x2753, 0x2755, 0 },
UnicodeRange{ 0x2757, 0x2757, 0 },
UnicodeRange{ 0x2776, 0x277f, 1 },
UnicodeRange{ 0x2795, 0x2797, 0 },
UnicodeRange{ 0x27b0, 0x27b0, 0 },
UnicodeRange{ 0x27bf, 0x27bf, 0 },
UnicodeRange{ 0x2b1b, 0x2b1c, 0 },
UnicodeRange{ 0x2b50, 0x2b50, 0 },
UnicodeRange{ 0x2b55, 0x2b55, 0 },
UnicodeRange{ 0x2b56, 0x2b59, 1 },
UnicodeRange{ 0x2e80, 0x2e99, 0 },
UnicodeRange{ 0x2e9b, 0x2ef3, 0 },
UnicodeRange{ 0x2f00, 0x2fd5, 0 },
UnicodeRange{ 0x2ff0, 0x2ffb, 0 },
UnicodeRange{ 0x3000, 0x303e, 0 },
UnicodeRange{ 0x3041, 0x3096, 0 },
UnicodeRange{ 0x3099, 0x30ff, 0 },
UnicodeRange{ 0x3105, 0x312f, 0 },
UnicodeRange{ 0x3131, 0x318e, 0 },
UnicodeRange{ 0x3190, 0x31e3, 0 },
UnicodeRange{ 0x31f0, 0x321e, 0 },
UnicodeRange{ 0x3220, 0x3247, 0 },
UnicodeRange{ 0x3248, 0x324f, 1 },
UnicodeRange{ 0x3250, 0x4dbf, 0 },
UnicodeRange{ 0x4e00, 0xa48c, 0 },
UnicodeRange{ 0xa490, 0xa4c6, 0 },
UnicodeRange{ 0xa960, 0xa97c, 0 },
UnicodeRange{ 0xac00, 0xd7a3, 0 },
UnicodeRange{ 0xe000, 0xf8ff, 1 },
UnicodeRange{ 0xf900, 0xfaff, 0 },
UnicodeRange{ 0xfe00, 0xfe0f, 1 },
UnicodeRange{ 0xfe10, 0xfe19, 0 },
UnicodeRange{ 0xfe30, 0xfe52, 0 },
UnicodeRange{ 0xfe54, 0xfe66, 0 },
UnicodeRange{ 0xfe68, 0xfe6b, 0 },
UnicodeRange{ 0xff01, 0xff60, 0 },
UnicodeRange{ 0xffe0, 0xffe6, 0 },
UnicodeRange{ 0xfffd, 0xfffd, 1 },
UnicodeRange{ 0x16fe0, 0x16fe4, 0 },
UnicodeRange{ 0x16ff0, 0x16ff1, 0 },
UnicodeRange{ 0x17000, 0x187f7, 0 },
UnicodeRange{ 0x18800, 0x18cd5, 0 },
UnicodeRange{ 0x18d00, 0x18d08, 0 },
UnicodeRange{ 0x1aff0, 0x1aff3, 0 },
UnicodeRange{ 0x1aff5, 0x1affb, 0 },
UnicodeRange{ 0x1affd, 0x1affe, 0 },
UnicodeRange{ 0x1b000, 0x1b122, 0 },
UnicodeRange{ 0x1b132, 0x1b132, 0 },
UnicodeRange{ 0x1b150, 0x1b152, 0 },
UnicodeRange{ 0x1b155, 0x1b155, 0 },
UnicodeRange{ 0x1b164, 0x1b167, 0 },
UnicodeRange{ 0x1b170, 0x1b2fb, 0 },
UnicodeRange{ 0x1f004, 0x1f004, 0 },
UnicodeRange{ 0x1f0cf, 0x1f0cf, 0 },
UnicodeRange{ 0x1f100, 0x1f10a, 1 },
UnicodeRange{ 0x1f110, 0x1f12d, 1 },
UnicodeRange{ 0x1f130, 0x1f169, 1 },
UnicodeRange{ 0x1f170, 0x1f18d, 1 },
UnicodeRange{ 0x1f18e, 0x1f18e, 0 },
UnicodeRange{ 0x1f18f, 0x1f190, 1 },
UnicodeRange{ 0x1f191, 0x1f19a, 0 },
UnicodeRange{ 0x1f19b, 0x1f1ac, 1 },
UnicodeRange{ 0x1f1e6, 0x1f202, 0 },
UnicodeRange{ 0x1f210, 0x1f23b, 0 },
UnicodeRange{ 0x1f240, 0x1f248, 0 },
UnicodeRange{ 0x1f250, 0x1f251, 0 },
UnicodeRange{ 0x1f260, 0x1f265, 0 },
UnicodeRange{ 0x1f300, 0x1f320, 0 },
UnicodeRange{ 0x1f32d, 0x1f335, 0 },
UnicodeRange{ 0x1f337, 0x1f37c, 0 },
UnicodeRange{ 0x1f37e, 0x1f393, 0 },
UnicodeRange{ 0x1f3a0, 0x1f3ca, 0 },
UnicodeRange{ 0x1f3cf, 0x1f3d3, 0 },
UnicodeRange{ 0x1f3e0, 0x1f3f0, 0 },
UnicodeRange{ 0x1f3f4, 0x1f3f4, 0 },
UnicodeRange{ 0x1f3f8, 0x1f43e, 0 },
UnicodeRange{ 0x1f440, 0x1f440, 0 },
UnicodeRange{ 0x1f442, 0x1f4fc, 0 },
UnicodeRange{ 0x1f4ff, 0x1f53d, 0 },
UnicodeRange{ 0x1f54b, 0x1f54e, 0 },
UnicodeRange{ 0x1f550, 0x1f567, 0 },
UnicodeRange{ 0x1f57a, 0x1f57a, 0 },
UnicodeRange{ 0x1f595, 0x1f596, 0 },
UnicodeRange{ 0x1f5a4, 0x1f5a4, 0 },
UnicodeRange{ 0x1f5fb, 0x1f64f, 0 },
UnicodeRange{ 0x1f680, 0x1f6c5, 0 },
UnicodeRange{ 0x1f6cc, 0x1f6cc, 0 },
UnicodeRange{ 0x1f6d0, 0x1f6d2, 0 },
UnicodeRange{ 0x1f6d5, 0x1f6d7, 0 },
UnicodeRange{ 0x1f6dc, 0x1f6df, 0 },
UnicodeRange{ 0x1f6eb, 0x1f6ec, 0 },
UnicodeRange{ 0x1f6f4, 0x1f6fc, 0 },
UnicodeRange{ 0x1f7e0, 0x1f7eb, 0 },
UnicodeRange{ 0x1f7f0, 0x1f7f0, 0 },
UnicodeRange{ 0x1f90c, 0x1f93a, 0 },
UnicodeRange{ 0x1f93c, 0x1f945, 0 },
UnicodeRange{ 0x1f947, 0x1f9ff, 0 },
UnicodeRange{ 0x1fa70, 0x1fa7c, 0 },
UnicodeRange{ 0x1fa80, 0x1fa88, 0 },
UnicodeRange{ 0x1fa90, 0x1fabd, 0 },
UnicodeRange{ 0x1fabf, 0x1fac5, 0 },
UnicodeRange{ 0x1face, 0x1fadb, 0 },
UnicodeRange{ 0x1fae0, 0x1fae8, 0 },
UnicodeRange{ 0x1faf0, 0x1faf8, 0 },
UnicodeRange{ 0x20000, 0x2fffd, 0 },
UnicodeRange{ 0x30000, 0x3fffd, 0 },
UnicodeRange{ 0xe0100, 0xe01ef, 1 },
UnicodeRange{ 0xf0000, 0xffffd, 1 },
UnicodeRange{ 0x100000, 0x10fffd, 1 },
};
}
// Routine Description:
// - returns the width type of codepoint as fast as we can by using quick lookup table and fallback cache.
// Arguments:
// - glyph - the utf16 encoded codepoint to search for
// Return Value:
// - the width type of the codepoint
CodepointWidth CodepointWidthDetector::GetWidth(const std::wstring_view& glyph) noexcept
{
char32_t codepoint = 0;
switch (glyph.size())
{
case 1:
codepoint = til::at(glyph, 0);
break;
case 2:
codepoint = (til::at(glyph, 0) & 0x3FF) << 10;
codepoint |= til::at(glyph, 1) & 0x3FF;
codepoint += 0x10000;
break;
default:
codepoint = 0;
break;
}
if (codepoint < 0x80)
{
return CodepointWidth::Narrow;
}
// The return value of _lookupGlyphWidth coincides with the enum value of CodepointWidth
// on purpose to allow for this easy conversion to happen. Optimally, we should probably
// remove CodepointWidth altogether to allow for zero-width joiners and other characters.
static_assert(WI_EnumValue(CodepointWidth::Narrow) == 1);
static_assert(WI_EnumValue(CodepointWidth::Wide) == 2);
return static_cast<CodepointWidth>(_lookupGlyphWidth(codepoint, glyph));
}
// Routine Description:
// - checks if codepoint is wide. will attempt to fallback as much possible until an answer is determined
// Arguments:
// - glyph - the utf16 encoded codepoint to check width of
// Return Value:
// - true if codepoint is wide
bool CodepointWidthDetector::IsWide(const std::wstring_view& glyph) noexcept
{
return GetWidth(glyph) == CodepointWidth::Wide;
}
// GetWidth's slow-path for non-ASCII characters. Returns the number of columns the codepoint takes up in the terminal.
uint8_t CodepointWidthDetector::_lookupGlyphWidth(const char32_t codepoint, const std::wstring_view& glyph) noexcept
{
#pragma warning(suppress : 26447) // The function is declared 'noexcept' but calls function 'lower_bound<...>()' which may throw exceptions (f.6).
const auto it = std::lower_bound(s_wideAndAmbiguousTable.begin(), s_wideAndAmbiguousTable.end(), codepoint);
uint8_t width = 1;
if (it != s_wideAndAmbiguousTable.end() && codepoint >= it->lowerBound && codepoint <= it->upperBound)
{
width = 2;
if (it->isAmbiguous)
{
width = _checkFallbackViaCache(codepoint, glyph);
}
}
return width;
}
// Call the function specified via SetFallbackMethod() to turn CodepointWidth::Ambiguous into Narrow/Wide.
// Caches the results in _fallbackCache. This is _lookupGlyphWidth's even-slower-path.
uint8_t CodepointWidthDetector::_checkFallbackViaCache(const char32_t codepoint, const std::wstring_view& glyph) noexcept
try
{
// Ambiguous glyphs are considered narrow by default. See microsoft/terminal#2066 for more info.
if (!_pfnFallbackMethod)
{
return 1;
}
if (const auto it = _fallbackCache.find(codepoint); it != _fallbackCache.end())
{
return it->second;
}
const uint8_t width = _pfnFallbackMethod(glyph) ? 2 : 1;
_fallbackCache.insert_or_assign(codepoint, width);
return width;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return 1;
}
// Method Description:
// - Sets a function that should be used as the fallback mechanism for
// determining a particular glyph's width, should the glyph be an ambiguous
// width.
// A Terminal could hook in a Renderer's IsGlyphWideByFont method as the
// fallback to ask the renderer for the glyph's width (for example).
// Arguments:
// - pfnFallback - the function to use as the fallback method.
// Return Value:
// - <none>
void CodepointWidthDetector::SetFallbackMethod(std::function<bool(const std::wstring_view&)> pfnFallback) noexcept
{
_pfnFallbackMethod = std::move(pfnFallback);
}
// Method Description:
// - Resets the internal ambiguous character width cache mechanism
// since it will be different when the font changes and we should
// re-query the new font for that information.
// Arguments:
// - <none>
// Return Value:
// - <none>
void CodepointWidthDetector::NotifyFontChanged() noexcept
{
#pragma warning(suppress : 26447) // The function is declared 'noexcept' but calls function 'clear()' which may throw exceptions (f.6).
_fallbackCache.clear();
}