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

fix: the text wrapping position is not accurate #1876

Merged
merged 1 commit into from
Dec 26, 2024
Merged

fix: the text wrapping position is not accurate #1876

merged 1 commit into from
Dec 26, 2024

Conversation

wang1212
Copy link
Member

@wang1212 wang1212 commented Dec 26, 2024

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / Document optimization
  • TypeScript definition update
  • Refactoring
  • Performance improvement
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

#1833

💡 Background and solution

Measuring text is an expensive operation, so G internally calculates the overall text width on a character-by-character basis and caches it when calculating line breaks. Due to the complexity of text rendering, this results in a larger result than measuring the entire text at once, resulting in less precise line break locations.

However, we cannot simply adjust the character-by-character strategy to measure the entire text, which will cause serious performance problems in scenarios with a long text.

image In the case of long text, the performance difference between the two different strategies is two orders of magnitude

On the other hand, we can combine the two strategies, use the character-by-character and cache strategies to calculate a rough line break position, and then use the measurement of the entire text near the threshold to get the precise result. Usually, the difference in the position calculated by the two different strategies is within 1-3 characters, so the performance consumption is acceptable, and more importantly, it brings better visual effects.

Before

image image

After

image image

📝 Changelog

Language Changelog
🇺🇸 English fix: the text wrapping position is not accurate
🇨🇳 Chinese fix: 文本换行位置不准确

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

Copy link

Walkthrough

This pull request addresses a bug related to inaccurate text wrapping positions. The solution involves a hybrid approach that combines character-by-character and whole-text measurement strategies to improve precision without significant performance loss. This change enhances the visual appearance of text rendering.

Changes

Files Summary
.changeset/silver-tigers-roll.md Added a changeset for the patch fixing text wrapping position.
tests/demos/bugfix/textWordWrap.ts Introduced a benchmark test to compare text measurement strategies.
tests/demos/event/hierarchy.ts Updated imports to include Canvas in the hierarchy demo.
packages/g-lite/src/services/TextService.ts Refactored text measurement logic to improve line break precision.

@wang1212 wang1212 merged commit 1d13497 into release Dec 26, 2024
2 checks passed
@wang1212 wang1212 deleted the fix-1833 branch December 26, 2024 08:18
wang1212 added a commit that referenced this pull request Dec 26, 2024
* fix: the text wrapping position is not accurate (#1876)

* Version Packages (#1877)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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.

2 participants