Skip to content

Lay out and select right-to-left text - #40

Merged
servercimen merged 16 commits into
feature/accessibilityfrom
feature/right-to-left
Nov 24, 2026
Merged

servercimen merged 16 commits into
feature/accessibilityfrom
feature/right-to-left

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 10 of a stack of 13 pull requests. The base is #39 (feature/accessibility). #31 (part 1) tells how to merge the stack.

On this stacked branch, swift test passes (370 tests), and the iOS build and build-for-testing pass.

Summary

Text in right-to-left scripts, such as Arabic and Hebrew, must start at the right edge, and a selection must follow the glyphs. Before this change, every block started at the left, and right-to-left selections were empty or wrong. Text with no right-to-left character uses the same code as before.

  • Each block takes its base direction from its first strong character, by the bidirectional classes of Unicode 16.0. A streamed block is not scanned again.
  • A right-to-left block starts at the right edge. Its list markers, checkboxes, quote bars and indents are on the right. TranscriptStyle.textAlignment counts from the leading edge. Code blocks stay left to right.
  • A right-to-left table puts its first column on the right. A column with a colon on one side (:--- or ---:) keeps its text on that side of the screen.
  • A selection has one rect for each run of glyphs in one direction. Each character of a ligature can be selected. Links in right-to-left text open.
  • UITextInput gives the direction of each paragraph and each selection rect. Left and right caret moves go to the next caret on that side of the screen.
  • Custom tag shapes, padding and images follow the direction of the tag. The new TranscriptFragment.isRightToLeft (false by default) tells a shape of your own.
  • The README has a new section, "Right-to-left text".

TranscriptFragment.isRightToLeft is the only new public API. DocumentLayout.selectionRects(for:) can now give more than one rect for a line.

Tests

  • swift test on macOS: 218 tests pass. New suites test the direction scan, the layout and the selection. New pixel tests compare the streamed tail and the fades of right-to-left text with a full drawing.
  • iOS build: xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' (Xcode 27.2) succeeds with no warnings.

Checked on an iPhone 12 mini

A check screen in a local test app measured in-app pictures. Each check ran on main first and showed the problem, except the left-to-right control, which must not change. The branch ran 3 times with the same results.

  • Lines: main puts Hebrew lines at the left. The branch ends them 0.7 to 1.0 pt from the right edge.
  • Markers, bars and tables: main puts the bullet, the quote bar and the first table column at the left. The branch puts them on the right, and hit tests find the correct cell.
  • Selection: for a Hebrew word, main gives no rect. The branch gives one right-to-left rect. On a mixed line, main gives one rect that also covers the next word. The branch gives one rect for each direction.
  • Caret moves: on a mixed line, main makes 8 steps in the wrong direction each way. The branch makes 0.
  • Ligature and tags: main gives no rect for lam-alef, and draws a wrapped pill over the wrong words. The branch gives two rects that tile the glyph, and closes each part of the pill on the correct side.
  • Streaming: when the first Hebrew letter of a quote arrives, the branch moves the quote to the right and leaves no ink at the left.
  • Control: a left-to-right screen is pixel-identical on main and on the branch.

Stand-ins: the check reads the handle positions from UITextSelectionDisplayInteraction after select(_:), not from a finger. closestPosition(to:) takes the place of a drag, and position(from:in:offset:) takes the place of the arrow keys. Fades are pixel comparisons at set times, not a screen recording.

Not checked

  • A finger drag of the handles across a line break and across a change of direction.
  • A screen recording of a streamed quote, and flicker in a fade-in of Hebrew and Arabic text.
  • VoiceOver, the edit menu, Look Up, Translate, Writing Tools and hardware keyboard arrows on right-to-left text.
  • Bold Text and Increase Contrast with right-to-left text, tags and tables.

Known limits

  • A code block or a thematic break inside a right-to-left list or quote keeps its indent and quote bar on the left.
  • A block with no strong character, such as only digits or emoji, is left to right.

🤖 Generated with Claude Code

servercimen and others added 15 commits November 23, 2026 17:08
DirectionScan finds the direction of the first strong character, as the
Unicode bidirectional algorithm does. It also records if the text has a
right-to-left character. The scan continues from where it stopped, so
streamed text is not read again.

The styled text of a block can now carry a base direction for Core Text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each block gets the base direction of its first strong character. A
block that has right-to-left characters is typeset with that direction,
so a line typeset from the middle of the block keeps it. A block with no
right-to-left character is typeset as before. Code stays left to right.

In a right-to-left block:
- Lines start at the right edge. The text alignment counts from there.
- The indents of lists and quotes, the list markers and the quote bars
  are on the right. A number marker shows its period on the side of the
  text.
- A table puts its first column on the right.
- A truncated last line keeps the full width.

A streamed block does not scan its text again. It drops its earlier
lines when its direction changes, and the gap above it is drawn again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A line with right-to-left text gets one selection rect for each stretch
of glyphs that go the same way. Thus a selection in text of mixed
direction can have several rects on one line. Each part records its
direction and if it holds the start or the end of the selection.

A link tap in a right-to-left run finds the character under the point
from the glyphs.

Lines that are all left to right keep the code that they used before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- baseWritingDirection(for:in:) gives the direction of the paragraph at
  the position.
- Each selection rect gives the direction of its glyphs. The start and
  end flags go to the rects that hold the first and the last selected
  character.
- firstRect(for:) is the union of the rects on the first line.
- In a right-to-left paragraph, left and right are swapped for caret
  moves and for ranges that extend a position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The README tells how a block gets its direction, and what moves to the
right in a right-to-left block. The doc comments of the text alignment,
the list indent and the quote indent tell about the right side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A table cell is typeset on its own, as before. Core Text gives it the
direction of its own text, so an Arabic cell in an English table keeps
its punctuation in the correct place. The direction of the table sets
only the order and the side of the columns.

A test streams a right-to-left table, so the rows that it keeps use the
mirrored columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tail frames and the fade pieces must give the same pixels as a
drawing from scratch. The text has right-to-left paragraphs, a list, a
table, and a quote that turns right to left when its first letter
arrives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No test shows it yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A glyph now stands for its character and for the characters after it
up to the next glyph of the run. A selection takes the part of the
glyph that its characters have, so one character of a ligature gets
a rect. A tap on a ligature finds the character under the point.

Lines of a block without a right-to-left character skip the check of
their glyph runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In a line with right-to-left text, a move to the right now goes to
the nearest caret on the right of the screen, and a move to the left
to the nearest caret on the left. This is also correct in a
left-to-right run inside a right-to-left paragraph, and the reverse.
At the end of the line, the caret goes on to the next or the previous
line. Lines with no right-to-left text move by one character, as
before.

The far end of a range on one line is found from its carets. The
first rect of a range is the union of the rects of its first line,
not of all the rects at the same height, so a range across table
cells gives the first cell only.

The UITextInput methods call DocumentLayout helpers that the macOS
tests reach. New tests cover the moves, the first rect and copy of
right-to-left and mixed text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The general category does not give the bidirectional class of every
character. Roman numerals, regional indicators, private use characters
and the digits of Indic scripts are left to right, and some modifier
letters are neutral. A table of these exceptions, from Unicode 16.0,
now corrects the category rule. A block that starts with such a
character and then holds Hebrew is left to right, as Core Text finds.

U+FEFF (zero width no-break space) no longer counts as a right-to-left
character.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The delimiter cells ":---" and "---:" align a column to the left and
to the right of the screen. A right-to-left table mirrored them, so
":---" showed at the right. The parser now records the columns with a
colon on one side only. A right-to-left table keeps their alignment
on that side. A column with no colon still starts at the leading
edge. Left-to-right tables and copy do not change.

The README describes table columns and the arrow keys in right-to-left
text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In right-to-left text, a tag fragment had its start on the left of
its end, so layout skipped it, and tag pills and underlines were
missing. A tag in a block with right-to-left text now takes the
direction of its first strong character:

- The text rect of a right-to-left tag comes from the left and right
  edges of its glyphs.
- Core Text adds the extra advance of a character on its right, in
  both directions. So the room on the right of a tag goes on its
  rightmost character, and the room on its left goes on the character
  to its left.
- The padding before a right-to-left tag and its image are on the
  right.
- TranscriptFragment has a new isRightToLeft property, false by
  default. The built-in pill and marker shapes use it to close the
  correct side of a tag that wraps.

Blocks without a right-to-left character do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first and the last character of a tag with text in both
directions can be anywhere in it on the screen. Then the edges of its
glyphs give its fragment, so its shapes are not dropped. A new test
also checks that a streamed right-to-left block with tags is laid out
as a block made at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BlockLayout: the block keeps the sideways scroll values and the direction values.
BlockLayout.layOutText: the function takes both wraps and direction, and returns the right edge again.
BlockLayout.layOutText: the reuse check keeps the right edge, and the check that the block grew stays in the initializer.
BlockLayout.layOutText: a line goes through place(), and its right edge is measured after place() moves it.
BlockLayout.makeMarker: list markers keep the number format, weight and colors, and get the right-to-left paragraph style.
BlockLayout.offset(for:width:room:): the doc says that the line moves from the leading edge.
TagDecorations: a tag records both isRightToLeft and keepsTogether.
TranscriptStyle.attributedString: the function takes both weight and direction, and keeps the quote text color.
DocumentLayout: the view keeps leavesTextOut and documentRange, and quoteBars(at:) uses the quote bar width of the style.
DocumentLayout.draw: the quote bars come from quoteBars(at:) and use the quote bar color of the style.
DocumentLayout: selection segments and the first line rect take a sideways scroll, and a block that scrolls cuts them to its box.
TranscriptTextView: the UITextInput methods stay nonisolated on the main thread with the side scroll.
TranscriptTextView: left and right moves, the farthest position and the extended range use the right-to-left logic.
TranscriptTextView: the first rect, the selection rects and the base writing direction use the right-to-left logic.
TranscriptSelectionRect: the stored values are nonisolated, and the rect keeps its writing direction.
DecorationShapeTests: both groups of tests are kept.
AccessibilityContent: a cell of a right-to-left table gets the frame of its own column, with a new test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit eae1b13 into main Nov 24, 2026
0 of 3 checks passed
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.

1 participant