Skip to content

fix(text): Embed only differing runs and honor bidi controls - #26726

Open
mikernet wants to merge 3 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/text-bidi-embedding
Open

mikernet wants to merge 3 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/text-bidi-embedding

Conversation

@mikernet

@mikernet mikernet commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25291, closes #26722, closes #26723; part of #24339 (epic: #26728)

PR Type:

🐞 Bugfix

What changed? 🚀

This changes how UnicodeText sets up bidi. UnicodeText is the Skia text engine of TextBlock, TextBox, PasswordBox (when it shows its password) and RichEditBox. RichTextBlock doesn't use it yet, so it isn't affected. This is the second step of moving RichTextBlock to UnicodeText (#24339).

After this PR:

  • ICU gets a copy of the text in native memory and no embedding-levels array. The copy lives until ubidi_close.
    • Before, ubidi_setPara got a level array that was rented from a pool and returned before the line loop read it again. ICU writes its resolved levels into that array and keeps reading it, so anything that rented the array in between changed the drawn order. I reproduced that with a test hook. A forced compacting GC alone didn't reproduce it.
    • The text was also pinned only for the duration of the call. ICU keeps that pointer too, but nothing Uno calls reads the text again, so that half was latent.
  • Only a run whose FlowDirection differs from the control's is embedded, as on WinUI. In a right-to-left control, every right-to-left run used to get an explicit level, so all of the text became a right-to-left embedding. "Hello world." was drawn ".Hello world", and brackets were mirrored (Latin text in a FlowDirection=RightToLeft TextBlock is laid out as RTL on Skia: punctuation moves and brackets flip (WinUI keeps LTR order) #25291). Text now takes its direction from its own content.
  • Bidi control characters in the text are honored, as on WinUI. ICU ignores LRE, RLE, LRO, RLO, PDF and the isolates when it gets a level array, which UnicodeText passed for every non-trivial layout. Now:
    • when no run differs from the control, ICU reads the text as it is;
    • when one does, ICU reads a copy with an LRE or RLE before that run and a PDF after it, which is how WinUI embeds such an element. The run's embedding and the text's own controls then nest by the usual rules.
    • A paragraph separator ends every embedding, so the run's embedding is opened again after each separator inside the run.
    • Indices are mapped between the text and the copy, through a pooled map, for the bidi runs and the line reordering.

Commit by commit:

  1. The buffer lifetime. Here ICU still gets a level array, copied into native memory with the text.
  2. The embedding rule, as ICU's wildcard level 0 for runs that have the control's direction.
  3. The bidi controls. This commit replaces the level array with the copy described above.

Behavior change, per control

Control and content Before After WinUI Source
TextBlock, FlowDirection="RightToLeft", "Hello world." ".Hello world" "Hello world." "Hello world." measured
TextBox, FlowDirection="RightToLeft", "Hello world......" dots on the left dots on the right, still left-aligned same as after measured
RichEditBox, right-to-left control, right-to-left paragraph, "Hello world......" dots on the left dots on the right dots on the right; WinUI also right-aligns it, Uno doesn't before or after measured
RichEditBox, right-to-left control, default paragraphs: Latin; Hebrew with a number; a second paragraph unchanged unchanged same measured
Text containing RLO, or RLE…PDF (TextBlock) the controls were ignored applied applied measured
Same in TextBox, PasswordBox and RichEditBox ignored applied applied from code: same engine
Left-to-right TextBlock, paragraph starting with a right-to-left character, then an explicit right-to-left Run: right-to-left mark + "abc ", Run "def", " gh" "abc def gh" "gh def abc" "gh def abc" measured
Mirror case: right-to-left TextBlock, Latin-first paragraph, explicit left-to-right Run unchanged unchanged same measured
A differing run that opens an RLE or RLO and doesn't close it, then more text the text after the run kept its order the run's embedding continues to the end of the paragraph same as after measured
Same with an LRI, RLI or FSI left open unchanged unchanged same measured
Default-aligned right-to-left TextBox whose text starts with a neutral, such as "(Hello)" right-aligned left-aligned, like Latin text left-aligned from code: the default alignment follows the first run's direction

The "measured" rows were checked on native WinUI, and in none of them does Uno now differ from WinUI where it didn't before.

Unchanged differences from WinUI:

  • WinUI lays out every RichEditBox paragraph on a left-to-right base. So a right-to-left paragraph in a left-to-right control, and a Hebrew-first paragraph, read differently on Uno.
  • WinUI right-aligns right-to-left RichEditBox paragraphs; Uno doesn't.

I left both for a follow-up.

Also unchanged:

Since the controls are honored in TextBox input too, an override in typed or pasted text can now reorder what is displayed, as on WinUI: "invoice", U+202E and "txt.exe" display as "invoiceexe.txt".

Validation

New tests read the visual order from the colour of each run's ink rather than from character rects.

Given_TextBlock_Bidi:

  • When_Bidi_Buffers_Outlive_SetPara: Skia only, uses an internal hook. It overwrites the pooled level and map arrays a buffer returned too early would come back as.
  • When_Rtl_TextBlock_Latin_Keeps_Period_At_End.
  • When_Run_FlowDirection_Differs_From_Control: 2 rows.
  • When_Bidi_Controls_In_Text: 5 rows.
    • RLO;
    • RLO with a differing run;
    • RLE with a trailing neutral;
    • an RLE…PDF of the text straddling a differing run's start;
    • nesting at the deepest level, where an LTR letter inside 125 nested embeddings resolves to level 126.
  • When_Differing_Run_Leaves_Control_Open: RLE, RLO, LRI, RLI, FSI.
  • When_Differing_Run_In_Paragraph_Of_Its_Own_Direction: both directions.
  • When_Differing_Run_Wraps: both directions, wrapped and trimmed. It checks the order of each line where lines break, or the line is trimmed, inside a differing run.
  • When_Differing_Run_Ends_With_Spaces: Skia only, see above.
  • When_Differing_Run_Spans_Paragraph_Separator.

Other classes:

  • Given_TextBox.When_Rtl_TextBox_Latin_Keeps_Period_At_End.
  • Given_RichEditBox.When_Rtl_RichEditBox_Latin_Keeps_Dots_At_End: 2 rows.
  • Given_RichEditBox.When_Rtl_RichEditBox_Hebrew_Keeps_Ltr_Base: 2 rows. It runs on Skia Win32 and WinUI only, since it needs a font with Hebrew.

Runtime, Skia Desktop (Win32):

  • The 27 new cases pass, 3 runs out of 3.
  • Each test fails before the change it covers, or under a mutation:
    • the buffer test fails with the hook on unmodified code, and with the map returned to the pool before the line loop;
    • on commit 1: the period, TextBox, RichEditBox right-to-left paragraph and right-to-left-control run rows;
    • on commit 2: the bidi-control rows, the unclosed RLE and RLO rows, and the left-to-right row of When_Differing_Run_In_Paragraph_Of_Its_Own_Direction;
    • with the re-opening after paragraph separators disabled: When_Differing_Run_Spans_Paragraph_Separator;
    • with the line range not mapped into the copy: the 4 When_Differing_Run_Wraps rows.
  • These rows pass before and guard what could regress:
    • When_Run_FlowDirection_Differs_From_Control(LeftToRight);
    • the unclosed-isolate rows;
    • the mirror row;
    • When_Differing_Run_Ends_With_Spaces;
    • the RichEditBox default-paragraph rows.
  • Given_RichTextBlock|Given_SkiaTextFormatter|Given_BlockLayoutEngine|Given_HyperlinkAutomation: 207 of 207, the same as master.
  • Given_TextBlock|Given_TextBox|Given_RichEditBox|Given_PasswordBox: compared with a master run, no case went from pass to fail, and the 27 new cases pass. The 2 failures and 1 inconclusive case are the same as on master: input timing, a CRLF hit-test, and an accessibility client running.

Runtime, native WinUI (WinAppSDK): the 25 cases that run there pass.

Runtime, Skia WebAssembly: the 25 cases that run there pass.

Cost. I timed UnicodeText construction with a temporary runtime test, alternating master and this branch, while the PC was in use. Time is the minimum per batch, best of the rounds. Allocations are measured with GC.GetAllocatedBytesForCurrentThread.

Text Time, master Time, this PR Allocated per construction, master Allocated, this PR
Short ASCII label, right-to-left control 2.36 µs 2.17 µs 4,568 B 4,536 B
Arabic paragraph, right-to-left control 49.7 µs 46.5 µs 78,728 B 78,552 B
RichEditBox-like paragraph, 238 characters, every run differing 29.6 µs 29.7 µs 65,520 B 65,296 B
RichEditBox-like document, 2,380 characters in 10 paragraphs, every run differing 302.5 µs 300.0 µs 710,480 B 708,096 B
  • The per-round medians of the last row were about 6% higher on the branch, within the spread I measured between identical builds.
  • The left-to-right ASCII label takes the trivial path, which this PR doesn't change.

Compile: Uno.UI and the runtime tests build with analyzers and code style on, with no warning or error.

PR Checklist ✅

Behavior change:

  • Under FlowDirection="RightToLeft", left-to-right text in a TextBlock, TextBox or revealed PasswordBox keeps its own reading order instead of being laid out as a right-to-left embedding. So does a right-to-left paragraph in a right-to-left RichEditBox.
  • A Run whose FlowDirection differs from the control's is embedded even in a paragraph that already has its direction.
  • Bidi control characters in the text are applied.

Each of these matches WinUI where measured, as listed in the table above.

ubidi_setPara keeps pointers to the text and the embedding levels it is
given, writes the resolved levels back into that array, and
ubidi_setLine and ubidi_getLevels read them again for every line.
UnicodeText pinned both only for the setPara call and returned the
level array to the shared pool before the line loop, so anything that
rented the array in between changed the visual order of the text.

Copy both into native memory owned by the bidi object and free it
after ubidi_close.
In a right-to-left TextBlock, TextBox or RichEditBox, UnicodeText gave
every right-to-left run the explicit level 1, which made all of the
text a right-to-left embedding: "Hello world." was drawn as
".Hello world".

WinUI embeds only an element whose direction differs from its parent's.
A run with the control's direction now gets ICU's wildcard level 0,
which takes its paragraph's level, detected from the first strong
character, as left-to-right controls already did. A run whose direction
differs keeps its explicit level.
UnicodeText passed ICU an embedding-levels array for every non-trivial
layout, and ICU ignores LRE, RLE, LRO, RLO, PDF and the isolates when it
gets one. WinUI honors them.

Pass no array. When no run's direction differs from the control's, ICU
reads the text as it is. When one does, ICU reads a copy with an LRE or
RLE before that run and a PDF after it, the way WinUI embeds such an
element, so the run's embedding and the text's own controls nest by the
usual rules. A paragraph separator ends every embedding, so the run's
embedding is opened again after each one inside the run. Indices are
mapped between the text and the copy, through a pooled map, for the runs
and the line reordering.

As on WinUI, a differing run is now embedded even in a paragraph that
already has its direction, and a control the run leaves open is closed
by the run's PDF in place of the run's own embedding.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment