CoreTextPlace/logic/character.ts
2025-01-28 14:31:14 +08:00

14 lines
398 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}.`,
);
}
return unicodeWidth(ch);
}