Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split text into graphemes correctly #9646

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(textStyles): Split text into graphemes correctly [#9646](https://github.com/fabricjs/fabric.js/pull/9646)
- fix(ActiveSelection): static default inheritance [#9635](https://github.com/fabricjs/fabric.js/pull/9635)
- fix(StaticCanvas): StaticCanvas setDimensions typings [#9618](https://github.com/fabricjs/fabric.js/pull/9618)
- refactor(): Align gradient with class registry usage, part of #9144 [#9627](https://github.com/fabricjs/fabric.js/pull/9627)
Expand Down
22 changes: 22 additions & 0 deletions src/shapes/Text/StyledText.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FabricText } from './Text';
import { graphemeSplit } from '../../util/lang_string';

describe('setSelectionStyles', () => {
test('will set properties at the correct position', () => {
Expand Down Expand Up @@ -35,3 +36,24 @@ describe('setSelectionStyles', () => {
});
});
});

describe('toObject', () => {
it('Will serialize text with graphemes in mind', () => {
const text = new FabricText('🤩🤩\nHello', {
styles: {
1: {
0: {
fontSize: 40,
},
},
},
});
const serializedStyles = text.toObject().styles;
expect(serializedStyles).toEqual([
{ start: 2, end: 3, style: { fontSize: 40 } },
]);
expect(serializedStyles[0].start).toEqual(
graphemeSplit(text.textLines[0]).length
);
});
});
10 changes: 7 additions & 3 deletions src/util/misc/textStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
TextStyleDeclaration,
} from '../../shapes/Text/StyledText';
import { cloneDeep } from '../internals/cloneDeep';
import { graphemeSplit } from '../lang_string';

export type TextStyleArray = {
start: number;
Expand Down Expand Up @@ -57,14 +58,15 @@ export const stylesToArray = (

//loop through each textLine
for (let i = 0; i < textLines.length; i++) {
const chars = graphemeSplit(textLines[i]);
if (!styles[i]) {
//no styles exist for this line, so add the line's length to the charIndex total and reset prevStyle
charIndex += textLines[i].length;
charIndex += chars.length;
prevStyle = {};
continue;
}
//loop through each character of the current line
for (let c = 0; c < textLines[i].length; c++) {
for (let c = 0; c < chars.length; c++) {
charIndex++;
const thisStyle = styles[i][c];
//check if style exists for this character
Expand Down Expand Up @@ -108,8 +110,10 @@ export const stylesFromArray = (
styleIndex = 0;
//loop through each textLine
for (let i = 0; i < textLines.length; i++) {
const chars = graphemeSplit(textLines[i]);

//loop through each character of the current line
for (let c = 0; c < textLines[i].length; c++) {
for (let c = 0; c < chars.length; c++) {
charIndex++;
//check if there's a style collection that includes the current character
if (
Expand Down
Loading