[Text] Fixes double reorder of cached bidi runs - #21351
Merged
Merged
Conversation
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
|
You can test this PR using the following package version. |
3 tasks
MrJul
added this pull request to the merge queue
May 14, 2026
3 tasks
MrJul
pushed a commit
to MrJul/Avalonia
that referenced
this pull request
May 28, 2026
* Fix double reorder with cached runs * Revert change * Add covering unit tests * Remove worktrees * Update API baseline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does the pull request do?
Fix double reorder with cached bidi runs
When
TextRunCachewas enabled and a paragraph contained RTL text, recreating aTextLayoutfrom the same cache would render RTL runs incorrectly.The root cause was in how shaped runs were shared between the cache and the formatter:
The cache stored a
ShapedTextRunreferencing aShapedBufferbacked by a rentedGlyphInfo[]array.The formatter created "non-owning" copies for use in the line by passing the same
ArraySlice<GlyphInfo>to a newShapedBufferconstructor; both the cached run and the line's run therefore pointed at the same backing array.BidiReorderer.Reverse()mutated glyph order in-place on the line's copy, which also reversed the cached run's glyphs.On the next layout from cache, the retrieved run had IsReversed = false (fresh copy) but an already-reversed buffer.
BidiReordererreversed it a second time, putting RTL glyphs back into logical (ascending-cluster) order — causing wrong visual rendering.What is the current behavior?
What is the updated/expected behavior with this PR?
How was the solution implemented (if it's not obvious)?
ShapedTextRunis now ref-counted (AddRef/Dispose). The shaper produces one reference; callingAddRef()yields an additional owned reference rather than a shallow buffer copy.BidiReordererno longer calls Reverse() on individual runs. RTLShapedBuffers are produced by the shaper already in visual (descending-cluster) order; the reorderer only shuffles the run sequence.TextFormatterImpl: when adding runs to the cache,AddRefis called so both the cache and the current line hold independent, correctly-counted references. When reading from cache (FormatLineFromCache), each run's ref-count is incremented rather than copying the buffer.TextRunCache: properly disposes the old entry (releasing its reference) when a key is overwritten, preventing leaks.Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues