Repository navigation
Description
Current behavior 🐛
Severity: Medium - any app that shows an empty RichTextBlock (a placeholder, or content bound later) or puts a blank Paragraph between paragraphs gets a box about 25 % too short, so the layout jumps when the first character arrives.
On Skia, an empty RichTextBlock, or one holding an empty Paragraph, is exactly FontSize tall. WinUI makes it one line of the font tall, the same height as a line with text.
Measured ActualHeight, FontFamily="Segoe UI":
| XAML | WinUI | Uno (Skia Desktop) |
|---|---|---|
<RichTextBlock/> |
18.6 | 14 |
<RichTextBlock><Paragraph/></RichTextBlock> |
18.6 | 14 |
two empty Paragraphs |
37.2 | 28 |
<RichTextBlock FontSize="40"/> |
53.2 | 40 |
<RichTextBlock Padding="5"/> |
28.6 | 24 |
<RichTextBlock LineHeight="50"/> |
50 | 50 |
one Run "x" (reference) |
18.6 | 18.6 |
<TextBlock/> (reference) |
18.6 | 18.7 |
A paragraph that ends in a LineBreak has the same problem, because the empty line after the break also gets FontSize as its height. With the default font at FontSize 14, x + <LineBreak/> is 19.07 + 14 = 33.07 tall instead of two full lines.
Expected behavior 🎯
An empty line gets the line height of the paragraph's default font, as in WinUI. An empty RichTextBlock should be as tall as one with a single line of text, and as tall as an empty TextBlock.
WinUI computes this in LsTextLine::CalculateLineMetrics. When a line has no content, it takes the line spacing from the default typeface's GetTextLineBoundsMetrics, which also respects TextLineBounds:
// Line is empty so text height and text baseline are based on the default typeface.
...
m_textHeight = lineSpacing * pDefaultTextRunProperties->GetFontSize();
m_textBaseline = baseline * pDefaultTextRunProperties->GetFontSize();
How to reproduce it (as minimally and precisely as possible) 🔬
- Put this in a page of an Uno app (Skia Desktop):
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top"> <RichTextBlock x:Name="Empty" FontFamily="Segoe UI" /> <RichTextBlock x:Name="EmptyParagraph" FontFamily="Segoe UI"><Paragraph /></RichTextBlock> <RichTextBlock x:Name="OneLine" FontFamily="Segoe UI"><Paragraph><Run Text="x" /></Paragraph></RichTextBlock> </StackPanel> - After the page loads, compare
Empty.ActualHeight,EmptyParagraph.ActualHeightandOneLine.ActualHeight. - On Uno the first two are
14. On WinUI all three are18.6.
The same check as a runtime test (fails on current master):
[TestMethod]
[DataRow(14d)]
[DataRow(40d)]
public async Task When_Empty_Height_Matches_One_Line(double fontSize)
{
var oneLine = await MeasureHeight(Rtb(fontSize, new Run { Text = "x" }));
var noBlocks = await MeasureHeight(new RichTextBlock { FontSize = fontSize });
var emptyParagraph = await MeasureHeight(Rtb(fontSize));
Assert.AreEqual(oneLine, noBlocks, 0.5);
Assert.AreEqual(oneLine, emptyParagraph, 0.5);
}
// MeasureHeight puts the element in a top-left StackPanel and waits with WaitForLoaded(el, x => x.IsLoaded),
// because an empty RichTextBlock has zero width.
Output: FontSize=14: oneLine=19.07 noBlocks=14 emptyParagraph=14 textBlockEmpty=19.33 trailingLineBreak=33.07 and FontSize=40: oneLine=54.47 noBlocks=40 emptyParagraph=40 textBlockEmpty=54.67 trailingLineBreak=94.47. The test uses the default font, so its numbers differ slightly from the Segoe UI table.
Workaround 🛠️
Set an explicit LineHeight (or MinHeight) on the RichTextBlock.
Affected platforms 📱💻🖥️
Desktop (Windows). The code involved is shared by every Skia head, so the other Skia targets should behave the same, but only Desktop (Windows) was tested.
Uno.Sdk version (and other relevant versions) 📦
Uno Platform master 46ea872727d7ae0480cc229c464d674cc17ee576, net11.0-desktop.
IDE version 🧑💻
n/a (runtime test from the command line)
Anything else we need to know? 💬
Root cause. The block-layout ParagraphTextSource hands ParsedText the font size as the "default line height":
ParagraphTextSource.cs#L249-L250
float ISkiaParagraphSource.DefaultLineHeight
=> (float)((m_pContentOwner as Microsoft.UI.Xaml.Controls.RichTextBlock)?.FontSize ?? 14.0);
ParsedText uses that value as the height of a line with no content: an empty paragraph, or the empty line after a trailing LineBreak (ParsedText.cs#L387-L394, #L421).
Suggested fix. Return the line height of the paragraph's default font, not its font size. That font is already resolved in BlockLayoutHelpers.GetParagraphProperties via FontDetailsCache.GetFont(...).details, and FontDetails.LineHeight gives the value; it is the same metric a line with text uses. Two more things to check: it should use the effective paragraph's font (not always the owner's), and it should respect TextLineBounds the way WinUI's GetTextLineBoundsMetrics does.
What backs what.
- The Uno behaviour was checked with a runtime test on Skia Desktop (Win32), master
46ea872727d. - The WinUI numbers come from a native WinUI 3 (WinAppSDK 2.5.1) run of the same probe.
- The WinUI code path was read in the WinUI sources (link above).
- The trailing-
LineBreakcase was only runtime-tested on Uno. That WinUI gives that line full height is inferred from the sameCalculateLineMetricsempty-line branch.
Activity