New unicode-graphemes addon.

Handle grapheme cluster lookup as well as wcwidth.
This is based on Unicode 15, and could replace the unicode11 addon.
This commit is contained in:
Per Bothner
2023-05-16 18:31:31 -07:00
parent 7a7f4f0add
commit 2f7fe1189c
28 changed files with 1055 additions and 49 deletions
+20 -4
View File
@@ -3,8 +3,8 @@
* @license MIT
*/
import { IUnicodeVersionProvider } from 'common/services/Services';
type CharWidth = 0 | 1 | 2;
import { UnicodeCharProperties, UnicodeCharWidth } from 'common/services/Services';
import { UnicodeService } from 'common/services/UnicodeService';
const BMP_COMBINING = [
[0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
@@ -121,12 +121,28 @@ export class UnicodeV6 implements IUnicodeVersionProvider {
}
}
public wcwidth(num: number): CharWidth {
public wcwidth(num: number): UnicodeCharWidth {
if (num < 32) return 0;
if (num < 127) return 1;
if (num < 65536) return table[num] as CharWidth;
if (num < 65536) return table[num] as UnicodeCharWidth;
if (bisearch(num, HIGH_COMBINING)) return 0;
if ((num >= 0x20000 && num <= 0x2fffd) || (num >= 0x30000 && num <= 0x3fffd)) return 2;
return 1;
}
charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties {
let width = this.wcwidth(codepoint);
let shouldJoin = width === 0;
if (shouldJoin) {
let oldWidth = preceding === 0 ? 0
: UnicodeService.extractWidth(preceding);
if (oldWidth === 0) {
width = 1;
shouldJoin = false;
} else if (oldWidth > width) {
width = oldWidth;
}
}
return UnicodeService.createPropertyValue(0, width, shouldJoin);
}
}