CoreTextPlace/logic/character.ts
2025-01-28 17:21:59 +08:00

15 lines
441 B
TypeScript

import { unicodeWidth } from "@std/cli/unicode-width";
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
export function getCharacterWidth(ch: string): number {
const segments = [...segmenter.segment(ch)];
if (segments.length !== 1) {
throw new Error(
`Expected exactly one grapheme cluster, got ${segments.length}.`,
);
}
// TODO: Properly fix this.
return Math.min(unicodeWidth(ch), 2);
}