Skip to content

Fix text bounding rect#1134

Merged
100pah merged 3 commits intomasterfrom
fix-text-bounding-rect
Jul 19, 2025
Merged

Fix text bounding rect#1134
100pah merged 3 commits intomasterfrom
fix-text-bounding-rect

Conversation

@100pah
Copy link
Copy Markdown
Contributor

@100pah 100pah commented Jul 14, 2025

text bounding rect consistency

Previously, plain text bounding rect is inconsistent (contain stroke width or not) according to style.width & style.overflow (even if no wrapping or truncation happen and text is visually the same). This commit makes it consistent.

text bounding rect include stroke

Previously, the bounding rect varies and unpredictable according to the internal auto-stroke application:

  • If auto stroke is applied, there are 1px extra stroke, otherwise not. It's discernible and matters in compact layout of multiple labels.
  • plain text: if style.width & style.overflow are specified, exclude stroke, otherwise include stroke.
  • rich text: always exclude stroke

This commit exclude auto-stroke width uniformly, since auto-stroke using the same color as background to make the border invisible.

Should text bounding rect includes text stroke width?

  • Pros:
    • Intuitively, and by convention, bounding rect of Path always includes stroke width.
  • Cons:
    • It's unpredictable for users whether "auto stroke" is applied. If stroke width is included and multiple texts are laid out based on its bounding rect, the position of texts may vary and is unpredictable - especially in limited space (e.g., see echarts pie label cases).
    • "auto stroke" attempts to use the same color as the background to make the border to be invisible in most cases, thus it might be more reasonable to be excluded from bounding rect.
  • Conclusion:
    • If users specifies style.stroke, it will be included into the bounding rect as normal. Otherwise, keep the stroke width as 0 in this case to guarantee consistency of bounding rect based layout, regardless of whether "auto stroke" is applied.

Deprecate src/contain/text.ts getBoundingRect

Use (new Text(...)).getBoundingRect() or (new TSpan(...)).getBoundingRect() instead.
It behave differently from Text#getBoundingRect, and unexpectedly when text contains "\n".
The current effect:
zr_getBoundingRect_previous
The red border is the return of contain/text.ts getBoundingRect, but the blue border is the actual boundingRect if rendering by ZRText.


PENDING

Should text bounding rect includes style.padding, style.width, style.height when NO background and border displayed? It depends on how to define that. HTML getBoundingClientRect contain padding in that case. But currently ZRText does not.
If implement that, an extra invisible Rect may need to be added as the placeholder for the bounding rect computation, considering animation of padding. But will it degrades performance for the most commonly used plain texts cases?

100pah added 2 commits July 14, 2025 16:05
…ct is inconsistent (contain stroke width or not) according to style.width & style.overflow (even if no wrapping or truncation happen and text is visually the same). This commit makes it consistent. (2) Previously, the bounding rect varies and unpredictable according to the internal auto-stroke application. This commit exclude auto-stroke width uniformly, since auto-stroke using the same color as background to make the border invisible. (3) deprecated `src/contain/text.ts` `getBoundingRect`, because it behave differently from Text#getBoundingRect, and unexpectedly when text contains "\n".
@Jancheng-z
Copy link
Copy Markdown

Jancheng-z commented Jul 14, 2025 via email

100pah added a commit to apache/echarts that referenced this pull request Jul 15, 2025
…the inconsistent behavior in pie label layout - (2.1) Previously calling `getBoundingRect` before textConfig applied (causing the result diff). (2.2) Previously adding extra margin for "auto text stroke" even if no auto text stroke added. Currently use ecomfe/zrender#1134 to uniform the behavior.
@100pah 100pah merged commit 70f25b7 into master Jul 19, 2025
3 checks passed
@100pah 100pah deleted the fix-text-bounding-rect branch July 19, 2025 04:33
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.

4 participants