Repository navigation
Description
Current behavior 🐛
A TextBlock with FlowDirection="RightToLeft" and the default TextAlignment draws its text at the visual left edge, as if it were left-to-right. WinUI draws it at the visual right edge (the start edge of a right-to-left paragraph). It happens with Latin and Arabic content alike.
Measured on Skia Desktop (Win32): TextBlock with Width="300", FontSize="24", red text, inside a white Border. The numbers are the left and right edges of the glyph ink:
| FlowDirection | TextAlignment | Text | Uno (Skia) | WinUI 3 |
|---|---|---|---|---|
| LeftToRight | default | abc |
2 - 38 | left edge |
| RightToLeft | default | abc |
2 - 38 | 264 - 300 |
| RightToLeft | default | Link (red Hyperlink) |
3 - 45 | 260 - 299 |
| RightToLeft | default | رابط (red Hyperlink) |
1 - 39 | 258 - 297 |
| RightToLeft | Left |
abc |
2 - 38 | right edge (same as default) |
| RightToLeft | Right |
abc |
262 - 299 | left edge (not measured; follows from the mirroring below) |
The Hyperlink rows were measured on an earlier master (ffcff32) with the same scan; the abc rows on the commit listed below. RichTextBlock is not affected: with the same content it draws at the right edge, like WinUI.
Expected behavior 🎯
As in WinUI: under FlowDirection="RightToLeft", TextAlignment.Left (the default) means the logical start, so a short line is drawn against the visual right edge, and TextAlignment.Right puts it against the visual left.
Backed by a native WinUI 3 run (WinAppSDK, the same ink-scanning runtime test; numbers in the table above) and the WinUI sources: CTextBlock keeps the element mirrored under RTL and only un-mirrors the glyph runs, so the line alignment computed in mirrored space lands on the visual right: TextBlock.cpp#L3079-L3119.
How to reproduce it (as minimally and precisely as possible) 🔬
- Create a new Uno app (Skia renderer) and put this in
MainPage.xaml:<StackPanel Spacing="8" Padding="20"> <Border Width="300" Background="White" HorizontalAlignment="Left"> <TextBlock FlowDirection="RightToLeft" FontSize="24" Foreground="Red" Text="abc" /> </Border> <Border Width="300" Background="White" HorizontalAlignment="Left"> <TextBlock FlowDirection="RightToLeft" FontSize="24" Foreground="Red" Text="مرحبا" /> </Border> </StackPanel>
- Run it on any Skia head (for example Desktop).
- Observe: both texts are drawn at the left edge of their white boxes.
- Run the same XAML on WinUI 3: both texts are drawn at the right edge.
Runtime test used for the Uno column (drops into Uno.UI.RuntimeTests; it fails on the RTL rows on current master):
[TestClass]
[RunsOnUIThread]
public class Given_TextBlock_RtlDefaultAlignment
{
[TestMethod]
[DataRow(FlowDirection.LeftToRight, null)]
[DataRow(FlowDirection.RightToLeft, null)]
[DataRow(FlowDirection.RightToLeft, TextAlignment.Left)]
[DataRow(FlowDirection.RightToLeft, TextAlignment.Right)]
public async Task When_Short_Text_Ink_Position(FlowDirection flow, TextAlignment? alignment)
{
var sut = new TextBlock
{
Text = "abc",
Width = 300,
FontSize = 24,
FlowDirection = flow,
Foreground = new SolidColorBrush(Colors.Red),
};
if (alignment is { } a)
{
sut.TextAlignment = a;
}
var host = new Border
{
Background = new SolidColorBrush(Colors.White),
Child = sut,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
};
await UITestHelper.Load(host);
var bitmap = await UITestHelper.ScreenShot(host);
int minX = int.MaxValue, maxX = -1;
for (var y = 0; y < bitmap.Height; y++)
{
for (var x = 0; x < bitmap.Width; x++)
{
var c = bitmap.GetPixel(x, y);
if (c.R > 180 && c.G < 120 && c.B < 120)
{
minX = Math.Min(minX, x);
maxX = Math.Max(maxX, x);
}
}
}
var msg = $"{flow} TextAlignment={(alignment?.ToString() ?? "default")}: ink x=[{minX},{maxX}] of width {bitmap.Width}";
// WinUI: RTL with default/Left alignment = start edge = visual right; Right = visual left.
var expectRightEdge = flow == FlowDirection.RightToLeft && alignment != TextAlignment.Right;
if (expectRightEdge)
{
Assert.IsTrue(minX > bitmap.Width / 2, "Expected ink at the right edge. " + msg);
}
else
{
Assert.IsTrue(maxX < bitmap.Width / 2, "Expected ink at the left edge. " + msg);
}
}
}Workaround 🛠️
Set TextAlignment="Right" on right-to-left TextBlocks, but only on Uno (on WinUI that moves the text to the left), or use a RichTextBlock.
Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
All Skia targets (the code is shared). Measured on Skia Desktop (Win32).
Uno.Sdk version (and other relevant versions) 📦
master at 1fd0494 (7.0 dev).
IDE version 🧑💻
N/A (runtime tests run from the command line)
Anything else we need to know? 💬
Root cause. UIElement.GetFlowDirectionTransform suppresses the element mirror for a right-to-left TextBlock (the text engine emits glyphs in visual order), so the TextBlock paints in unmirrored space:
UIElement.cs#L108-L122
TextBlock.ParseText passes its TextAlignment (which is Left by default, never null for a plain TextBlock) to UnicodeText:
TextBlock.cs#L1501-L1534
UnicodeText.GetAlignmentOffsetForLine then treats Left/Right as physical sides even in its right-to-left branch: Left gives offset 0 (visual left), Right gives the visual right:
UnicodeText.cs#L1664-L1693
The RTL-aware fallback textAlignment ?? (rtl ? Right : Left) only applies when the alignment is null, which only happens for a TextBox-owned TextBlock without an explicit TextAlignment:
UnicodeText.cs#L264
Because draw, caret and hit-testing all go through GetAlignmentOffsetForLine, hyperlinks and selection still match the (wrong) drawn position.
Suggested fix. In the right-to-left branch of GetAlignmentOffsetForLine, swap Left and Right so they mean start/end (Left → totalWidth - lineWidth, Right → 0) for unmirrored text surfaces, and check that the TextBox path (which currently relies on the null fallback mapping to Right) keeps its behavior. TextAlignment.Justify and DetectFromContent should be checked too.
Related, different root causes: #24749 (RTL Padding applied on the wrong side), #24748 (RTL RichTextBlock hit-testing ignores the alignment flip). All three come from the same unmirrored coordinate space but need separate fixes.
Activity
fix(textblock): Align RTL text relative to FlowDirection