mirror of
https://github.com/TextPlace/CoreTextPlace.git
synced 2025-04-30 12:41:10 +00:00
15 lines
441 B
TypeScript
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);
|
|
}
|