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

Conversation

jiayihu
Copy link
Contributor

@jiayihu jiayihu commented Feb 2, 2024

Use graphemeSplit to correctly split chars into graphemes correctly for textStyles conversion.

This bug doesn't cause actually any issue to fabric, which I why I could not write any test. Fabric uses the textStyles utils only for toObject and fromObject, but since the bug is present in both directions fromObject(toObject(textStyles)) === textStyles, but it's technically wrong. If you use a text like 👨‍👩‍👦‍👦, it will be looped through as 4 distinct chars, but it's a single grapheme.

I know we should use object.graphemeSplit to actually use the overriden implementation from the consumer, but there's a comment that says there should be a way to provide the graphemeSplit implementation directly to fabric. So for now I think this is enough as reminder that textStyles should do grapheme splitting as well. If needed, we can change the functions to accept textLines: string[][] as array of grapheme-split text lines.

Also nobody reported this issue. I knew about it only because at work we use the TextStyleArray format both for serialization and for the React UI state, e.g. to show the text formatting buttons for the selected text.

Copy link

codesandbox bot commented Feb 2, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@ShaMan123
Copy link
Contributor

ShaMan123 commented Feb 4, 2024

I have an easier solution for all this mess but we decided that Text is going to be rewritten.
I think this is a good PR and agree that graphemeSplit should be the instance method.
You can't override the fabric impl anymore, that was v5 architecture.
Regarding a test, I think the char you provided is a prefect test for the PR.

@ShaMan123
Copy link
Contributor

Your other suggestion is even better IMO, passing down all the context.

@asturur
Copy link
Member

asturur commented Feb 4, 2024

grapheme split should still be overridable in some way, probably we should make it overridable with some configuration.

To be noted that if you change grapheme split implementation during lifetime of an app, or simply if new emoji get released and you update your code to support them with different splitting, then at that point style will be different, but so will be the text displayed, so i would consider that an edge case.

Regarding text, yes i m not going flip text over since is an highly used feature that requires stability.
If we really want to improve text we need to write a new one, starting to define what better means.

@asturur asturur merged commit 81d387d into fabricjs:master Feb 4, 2024
20 of 22 checks passed
@jiayihu
Copy link
Contributor Author

jiayihu commented Feb 5, 2024

grapheme split should still be overridable in some way, probably we should make it overridable with some configuration.

Yes I agree, setting some configuration seems the most logical way

Thanks for writing the test 👍

@jiayihu jiayihu deleted the fix/grapheme-split branch February 5, 2024 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants