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: abnormal word breaking in text #1896

Merged
merged 1 commit into from
Jan 23, 2025
Merged

fix: abnormal word breaking in text #1896

merged 1 commit into from
Jan 23, 2025

Conversation

wang1212
Copy link
Member

@wang1212 wang1212 commented Jan 23, 2025

🤔 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

fixed #1897

💡 Background and solution

When processing text line breaks, in the initial implementation of measuring text width character by character, the line width will be directly obtained from the cache when encountering a word break, because each character is already in the cache; in the improved line break strategy, the width of the entire line of text will be measured near the line break threshold to determine the final more accurate line break character position, which means that the width of each single character may not be in the cache, which causes the exception to be thrown

This can be done by changing the word break judgment of the text to real-time measurement to obtain the line width. (This will not cause a serious performance reduction, because we use the existing cache)

📝 Changelog

Language Changelog
🇺🇸 English fix: abnormal word breaking in text
🇨🇳 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 abnormal word breaking in text rendering. The fix involves changing the word break judgment to real-time measurement to obtain line width, utilizing the existing cache to avoid performance degradation.

Changes

Files Summary
.changeset/config.json Updated configuration to reflect the patch changes.
.changeset/ten-toes-whisper.md Added a changeset file documenting the fix.
tests/demos/bugfix/textWordWrap.ts Added a new test case to verify the fix for abnormal word breaking.
packages/g-lite/src/services/TextService.ts Modified text width calculation to use real-time measurement instead of relying solely on cache.

) {
return text.split('').reduce((sum: number, c) => {
if (!cache[c]) throw Error('cannot count the word without cache');
return sum + cache[c];
return sum + calcWidthWithCache(c);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation threw an error when a character's width was not found in the cache. The new implementation calculates the width in real-time, which resolves the issue of missing cache entries and prevents exceptions.

@hustcc
Copy link
Member

hustcc commented Jan 23, 2025

有相关 issue 吗?

@wang1212
Copy link
Member Author

有相关 issue 吗?

DI S2 那边反馈的,没有提 issue,测试过程中发现的

@hustcc
Copy link
Member

hustcc commented Jan 23, 2025

有相关 issue 吗?

DI S2 那边反馈的,没有提 issue,测试过程中发现的

也可以留一个 issue,有一些脱敏的截图之类,方便知道解决了什么问题。

GitHub 中有两个工具,一个是 issue,是明确有问题的 todo 事项,一个是 discussion,用于讨论、交流、答疑。只不过我们一般把 issue 用作答疑了。

@@ -588,11 +588,10 @@ export class TextService {

private sumTextWidthByCache(
text: string,
cache: { [key in string]: number },
calcWidthWithCache: (txt: string) => number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个会改变缓存命中率,导致部分场景性能降低吗?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

性能应该不会有明显的影响,只是在文本换行阈值附近 1-5 个字符不会命中缓存的差异

@wang1212 wang1212 merged commit 284b6cb into release Jan 23, 2025
2 checks passed
@wang1212 wang1212 deleted the fix-text-wordwrap branch January 23, 2025 07:55
wang1212 added a commit that referenced this pull request Jan 23, 2025
* fix: abnormal word breaking in text (#1896)

* Version Packages (#1898)

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