Repository navigation
Lay out and select right-to-left text - #40
Merged
Merged
Conversation
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>
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.
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 testpasses (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.
TranscriptStyle.textAlignmentcounts from the leading edge. Code blocks stay left to right.:---or---:) keeps its text on that side of the screen.UITextInputgives 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.TranscriptFragment.isRightToLeft(false by default) tells a shape of your own.TranscriptFragment.isRightToLeftis the only new public API.DocumentLayout.selectionRects(for:)can now give more than one rect for a line.Tests
swift teston 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.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.
Stand-ins: the check reads the handle positions from
UITextSelectionDisplayInteractionafterselect(_:), not from a finger.closestPosition(to:)takes the place of a drag, andposition(from:in:offset:)takes the place of the arrow keys. Fades are pixel comparisons at set times, not a screen recording.Not checked
Known limits
🤖 Generated with Claude Code