From 437b367d3ba9dfd7f9a4c8042ac8d00208c09770 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Fri, 13 Aug 2021 18:57:49 +0800 Subject: [PATCH] feat: correctly break graphemes (#2652) --- src/css/layout/text.ts | 3 ++- src/render/canvas/canvas-renderer.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/css/layout/text.ts b/src/css/layout/text.ts index 9b33e7c..59f0bde 100644 --- a/src/css/layout/text.ts +++ b/src/css/layout/text.ts @@ -1,6 +1,7 @@ import {OVERFLOW_WRAP} from '../property-descriptors/overflow-wrap'; import {CSSParsedDeclaration} from '../index'; import {fromCodePoint, LineBreaker, toCodePoints} from 'css-line-break'; +import {splitGraphemes} from 'text-segmentation'; import {Bounds, parseBounds} from './bounds'; import {FEATURES} from '../../core/features'; import {Context} from '../../core/context'; @@ -86,7 +87,7 @@ const getRangeBounds = (context: Context, node: Text, offset: number, length: nu }; const breakText = (value: string, styles: CSSParsedDeclaration): string[] => { - return styles.letterSpacing !== 0 ? toCodePoints(value).map((i) => fromCodePoint(i)) : breakWords(value, styles); + return styles.letterSpacing !== 0 ? splitGraphemes(value) : breakWords(value, styles); }; // https://drafts.csswg.org/css-text/#word-separator diff --git a/src/render/canvas/canvas-renderer.ts b/src/render/canvas/canvas-renderer.ts index 2c4ded1..d2cefa7 100644 --- a/src/render/canvas/canvas-renderer.ts +++ b/src/render/canvas/canvas-renderer.ts @@ -19,7 +19,6 @@ import { import {calculateBackgroundRendering, getBackgroundValueForIndex} from '../background'; import {isDimensionToken} from '../../css/syntax/parser'; import {TextBounds} from '../../css/layout/text'; -import {fromCodePoint, toCodePoints} from 'css-line-break'; import {ImageElementContainer} from '../../dom/replaced-elements/image-element-container'; import {contentBox} from '../box-sizing'; import {CanvasElementContainer} from '../../dom/replaced-elements/canvas-element-container'; @@ -44,6 +43,7 @@ import {TextShadow} from '../../css/property-descriptors/text-shadow'; import {PAINT_ORDER_LAYER} from '../../css/property-descriptors/paint-order'; import {Renderer} from '../renderer'; import {Context} from '../../core/context'; +import {splitGraphemes} from 'text-segmentation'; export type RenderConfigurations = RenderOptions & { backgroundColor: Color | null; @@ -144,7 +144,7 @@ export class CanvasRenderer extends Renderer { if (letterSpacing === 0) { this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline); } else { - const letters = toCodePoints(text.text).map((i) => fromCodePoint(i)); + const letters = splitGraphemes(text.text); letters.reduce((left, letter) => { this.ctx.fillText(letter, left, text.bounds.top + baseline);