Skip to content

feat(text): Let UnicodeText resume, trim, host objects and draw by line - #26727

Open
mikernet wants to merge 8 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/unicodetext-line-options
Open

mikernet wants to merge 8 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/unicodetext-line-options

Conversation

@mikernet

@mikernet mikernet commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: part of #24339 (epic: #26728)

PR Type:

✨ Feature

What changed? 🚀

This gives UnicodeText, the Skia text engine of TextBlock, TextBox, PasswordBox and RichEditBox, what RichTextBlock needs to lay out each paragraph with it: resuming a paragraph on an overflow, trimming at another width, hosting InlineUIContainers, reading one line at a time and drawing each line at its own position. It is the fourth step of moving RichTextBlock to UnicodeText (#24339). It is stacked on #26726, so the first three commits in the diff are that PR's.

Nothing uses the new options yet; RichTextBlock starts using them in the next PR. Each one is tested directly on the engine here. TextBlock, TextBox, PasswordBox and RichEditBox don't set any of them, so their layout and drawing are unchanged.

All the inputs go in one new trailing constructor parameter, in UnicodeTextOptions options = default. Its default changes nothing, so no call site changes.

Commit by commit:

  1. Resume (ResumeIndex). An overflow continues a paragraph where the previous page broke it, at that page's width. The resumed layout still resolves bidi and shapes the whole paragraph, so the resumed lines keep the paragraph's direction and the joining across the break (an Arabic word broken in the middle keeps its joined forms). The text before the index goes on lines that never wrap and end at the index; FirstResumedLine is the first line after them. Those lines go through the same bidi index mapping as every other line, including the copy with embedding characters that fix(text): Embed only differing runs and honor bidi controls #26726 adds. An index where no line can start (inside a cluster, such as a surrogate pair, or between the two halves of a CRLF) throws.
  2. Trimming (TrimmingWidth, ForcedEllipsisLine). A line can be trimmed at a width other than the one it wraps at, and a line that fits can get the paragraph's ellipsis because text after it won't be shown. Lines before the resume index are never trimmed, and a forced line before the first resumed line throws.
  3. Inline objects (InlineContainers). When the caller sets it, each InlineUIContainer takes one U+FFFC character, sized by what the caller measures, as RichEditBox images already do. U+FFFC breaks on both sides. A container before the resume index gets an empty placeholder and isn't measured, since the previous page laid it out.
  4. Line-scoped reads in UnicodeText.Lines.cs: a line's range, metrics and content edge, the character hit at an x on it, the caret x of an index on it, the caret stops around an index, the spans a range covers on it, and its inline objects. A line reports its line break length and the whitespace before it as TextLine does; on a line that ends with an ellipsis, the trailing whitespace is 0.
  5. DrawLines, which draws the given lines, each moved by its own translation from where the layout puts it (zero draws it where Draw does), and with its own band, given where the line is drawn, for its fills and caret. Everything the engine places per line moves with its line: glyphs, decorations, character backgrounds, tab leaders, highlight and selection fills, high-contrast backplates, spell-check squiggles, the composition underline, inline images, list markers and the caret on the ending line. Draw is now DrawLines over every line at the layout's own position.

Cost for existing callers

I measured UnicodeText construction and Draw against #26726's branch, for a short ASCII label, an Arabic paragraph and a long wrapped paragraph, alternating the two builds over nine rounds (Skia Desktop, Release, the same benchmark source in both builds):

Case Construction, #26726 Construction, this PR Draw, #26726 Draw, this PR Allocated per call
ASCII label, "Hello world" 1.19–1.27 µs 1.23–1.26 µs 5.4 µs 4.8 µs same, to the byte (4,152 B; 1,728 B)
Arabic paragraph, right to left, 400 px 46.0–46.1 µs 46.3–46.5 µs 11.9 µs 12.1 µs same (78,552 B; 11,448 B)
Long paragraph, 1,500 characters, wrapped at 400 px 122.8–125.3 µs 121.4–128.3 µs 85.4 µs 86.2 µs same (384,768 B; 72,704 B)

Each number is the fastest of 9 batches of 300 to 20,000 calls, with GC.GetAllocatedBytesForCurrentThread around each batch. Construction compares the last two alternating rounds of each build; Draw compares the fastest of nine rounds per build, because a busy PC moved its medians by up to 20% in both builds alike.

The options record adds no allocation when it's left unset.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable)
    • Given_UnicodeTextOptions (Skia only: it builds UnicodeText directly): resumed lines equal the full layout's line by line (clusters, glyph IDs, direction, x) for left-to-right, right-to-left, mixed text, a run whose direction differs from the control's, emergency breaks in Latin and Arabic, a right-to-left paragraph resumed at a Latin line, line breaks and tabs before and after the resume index, NoWrap, and a resume at the end of the text; every index of a text with a surrogate pair, a combining mark and a CRLF, which resumes where a line can start and throws elsewhere; trimming at another width and the forced ellipsis, also in a resumed layout; inline object placeholders; the line reads, round-tripped over every cluster, and the trailing whitespace and line break lengths; DrawLines with a different offset for every line against each line drawn alone under a translated session, with and without high contrast; a band taller than the line, which the fills and the caret cover; and DrawLines at the layout's own positions against Draw for a TextBlock, pixel for pixel.
    • Every test fails when I break the code it covers (for DrawLines, one mutation per element placed per line).
    • Skia Desktop (Win32): the new tests pass three times in a row. The RichTextBlock group (207 cases) and the TextBlock/TextBox/RichEditBox/PasswordBox group (1,599 cases) give the same results as on fix(text): Embed only differing runs and honor bidi controls #26726's branch.
    • Skia WASM: the new tests pass (23 cases), except 4 methods (6 cases) that need a Hebrew or Arabic fallback font, which run on Skia Desktop (Win32) only.
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features): internal engine API, nothing user-facing.
  • 🖼️ Validated PR Screenshots Compare Test Run results.
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

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.
Add UnicodeTextOptions, an optional trailing parameter of the UnicodeText
constructor whose default changes nothing, with ResumeIndex: where a
previous layout of the same paragraph broke at its own width.

The bidi resolution and the shaping still cover the whole text, so the
resumed lines keep the paragraph's level and the joining across the
break. The text before the index goes on lines that never wrap and end
at the index, and FirstResumedLine is the first line after them.
Add TrimmingWidth, the width a line must fit for trimming when it
differs from the width the text wraps at, and ForcedEllipsisLine, a
line that gets an ellipsis even if it fits and ends the layout, as a
paragraph ellipsis does.

Lines before the resume index are never trimmed.
Add InlineContainers: when a caller sets it, each InlineUIContainer
among the inlines takes one U+FFFC character, sized by what the caller
measures, as RichEditBox images already do. U+FFFC breaks on both
sides, so an object never splits a line inside it. A container before
the resume index belongs to a previous layout and gets an empty
placeholder without being measured.
Add line-scoped reads for callers that position each line themselves:
a line's range, metrics and content edge (GetLineInfo), the character
hit at an x on it (HitTestLine), the caret x of an index on it
(GetCaretX), the caret stops around an index (GetCaretStop), the spans
a range covers on it (GetRangeBoundsInLine) and its inline objects
(GetInlineObjectsInLine).
Add DrawLines, which draws the given lines of a layout, each with its
own offset and its own band for the fills and the caret, for callers
that place each line themselves. Every element placed per line moves
with its line: glyphs, decorations, character backgrounds, tab
leaders, highlight and selection fills, high-contrast backplates,
spell-check squiggles, the composition underline, inline images, list
markers and the caret on the ending line.

Draw is DrawLines over every line at the layout's own position.
@github-actions github-actions Bot added area/skia ✏️ Categorizes an issue or PR as relevant to Skia kind/documentation labels Nov 25, 2026
@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

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia kind/documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant