Repository navigation
Description
Current behavior 🐛
With FlowDirection="RightToLeft", TextBlock and RichTextBlock apply Padding.Left on the visual left and Padding.Right on the visual right, as if the element were left-to-right. Hit-testing subtracts Padding.Left from the physical X in the same way.
Measured on Skia Desktop (Win32), text in a 300px-wide white Border, FontSize 24. The numbers are the left and right edges of the glyph ink in the Border:
| Control | Text | Padding | Uno (Skia) | WinUI 3 |
|---|---|---|---|---|
| RichTextBlock | abc |
0 |
262 - 300 | 264 - 300 |
| RichTextBlock | abc |
80,0,0,0 |
262 - 300 (no inset) | 184 - 220 |
| RichTextBlock | abc |
0,0,80,0 |
182 - 220 | 264 - 300 (no inset) |
| RichTextBlock | كلام |
80,0,0,0 |
261 - 298 (no inset) | 181 - 218 |
| TextBlock | abc |
0 |
2 - 39 | 264 - 300 |
| TextBlock | abc |
80,0,0,0 |
82 - 119 (inset from the left) | 184 - 220 |
| TextBlock | abc |
0,0,80,0 |
2 - 39 | 264 - 300 |
| TextBlock | كلام |
80,0,0,0 |
81 - 118 | 181 - 218 |
On RichTextBlock the two padding sides are swapped. On TextBlock the padding is on the visual left too. (The TextBlock text also starts on the left even with no padding. That is a separate alignment problem, see the end of this issue.)
Expected behavior 🎯
As in WinUI, Padding is logical under right-to-left: Padding.Left insets the text from the visual right edge and Padding.Right from the visual left. With Padding="80,0,0,0", right-aligned RTL text should end 80px short of the right edge.
Checked on native WinUI 3 (WinAppSDK SamplesApp head, same runtime test; results in the table above) and in the WinUI sources:
CTextBlock::GetContentRenderingOffset/GetContentRenderTransformkeep the element mirrored, un-mirror only the glyphs, and offset the content by-padding.leftunder RTL so the padding stays logical: TextBlock.cpp#L3079-L3119RichTextBlock:PageNode::GetContentRenderingOffsetoffsets bypadding.leftinside the mirrored element space, which lands on the visual right: PageNode.cpp#L321-L334
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"> <TextBlock FlowDirection="RightToLeft" Padding="80,0,0,0" Foreground="Black" FontSize="24" Text="abc" /> </Border> <Border Width="300" Background="White"> <RichTextBlock FlowDirection="RightToLeft" Padding="80,0,0,0" Foreground="Black" FontSize="24"> <Paragraph>abc</Paragraph> </RichTextBlock> </Border> </StackPanel>
- Run it on any Skia head (for example Desktop).
- Observe: the
RichTextBlocktext touches the right edge of its white box, and theTextBlocktext is inset 80px from the left edge. - Run the same XAML on WinUI 3: both texts end 80px short of the right edge.
Runtime test used for the table (drops into Uno.UI.RuntimeTests; it reports the ink bounds instead of asserting):
[TestClass]
[RunsOnUIThread]
public class Given_TextBlock_RtlPadding
{
[TestMethod]
[DataRow(false, 80d, 0d, "abc")]
[DataRow(true, 80d, 0d, "abc")]
public async Task When_RightToLeft_Padding(bool useRichTextBlock, double padLeft, double padRight, string content)
{
FrameworkElement text = useRichTextBlock
? new RichTextBlock
{
FlowDirection = FlowDirection.RightToLeft,
Padding = new Thickness(padLeft, 0, padRight, 0),
Foreground = new SolidColorBrush(Colors.Black),
FontSize = 24,
Blocks = { new Paragraph { Inlines = { new Run { Text = content } } } },
}
: new TextBlock
{
FlowDirection = FlowDirection.RightToLeft,
Padding = new Thickness(padLeft, 0, padRight, 0),
Foreground = new SolidColorBrush(Colors.Black),
FontSize = 24,
Text = content,
};
var host = new Border { Width = 300, Background = new SolidColorBrush(Colors.White), Child = text };
await UITestHelper.Load(host);
var bitmap = await UITestHelper.ScreenShot(host);
var scale = bitmap.Width / 300d;
int minX = int.MaxValue, maxX = int.MinValue;
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 < 128 && c.G < 128 && c.B < 128)
{
minX = Math.Min(minX, x);
maxX = Math.Max(maxX, x);
}
}
}
var inkRight = (maxX + 1) / scale;
// WinUI: Padding.Left is the logical start, i.e. the visual right under RTL.
Assert.IsLessThanOrEqualTo(300 - padLeft + 2, inkRight, $"ink=[{minX / scale},{inkRight}]");
}
}Workaround 🛠️
Use symmetric horizontal padding, or swap Left and Right in the Padding of RTL text elements (only on Uno), or wrap the text in a Border and put the padding on the Border.
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 ffcff32 (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, RichTextBlock and RichTextBlockOverflow (the text engine already emits glyphs in visual order), so these elements paint in unmirrored space:
UIElement.cs#L108-L122
The draw and hit-test code still offsets by Padding.Left as if it were the visual left:
TextBlock.Draw: TextBlock.cs#L1638, hit-test: TextBlock.cs#L2052RichTextBlockdraw: RichTextBlock.mux.cs#L386, hit-test: #L675 and #L704RichTextBlockOverflowdraw: RichTextBlockOverflow.mux.cs#L451, hit-test: RichTextBlockOverflow.Input.cs#L139
The per-block Block.Margin.Left translation right after the padding (RichTextBlock.mux.cs#L392, RichTextBlockOverflow.mux.cs#L456) has the same shape and is likely affected too (not measured).
Suggested fix. Under RTL, use the visual-left inset Padding.Right (and Margin.Right for blocks) in these translations and in the hit-test offsets, or restore WinUI's model: keep the element mirrored and un-mirror only the glyph drawing, as CTextBlock::GetContentRenderTransform does.
Related, separate problem. A right-to-left TextBlock with the default TextAlignment draws its text on the visual left (2 - 39 in the table, WinUI: 264 - 300). TextBlock.ParseText passes TextAlignment.Left to UnicodeText, which aligns it visually without the RTL swap that ParsedText applies. RichTextBlock is not affected by that one.
See also #24748 (RTL \RichTextBlock\ hit-testing), which touches the same unmirrored coordinate space.
Activity