Skip to content

Commit 62054db

Browse files
authored
Make sure inlines properly inherit text run properties from their parent (#19750)
* Make sure inlines properly inherit text run properties from their parent * Adjust comment
1 parent aadcd01 commit 62054db

File tree

5 files changed

+125
-31
lines changed

5 files changed

+125
-31
lines changed

src/Avalonia.Controls/Documents/Bold.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ static Bold()
1313
{
1414
FontWeightProperty.OverrideDefaultValue<Bold>(FontWeight.Bold);
1515
}
16+
17+
public Bold()
18+
{
19+
SetCurrentValue(FontWeightProperty, FontWeight.Bold);
20+
}
1621
}
1722
}

src/Avalonia.Controls/Documents/Inline.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,44 +72,43 @@ public static void SetTextDecorations(Control control, TextDecorationCollection?
7272

7373
protected TextRunProperties CreateTextRunProperties()
7474
{
75-
var textDecorations = TextDecorations;
76-
var background = Background;
75+
var parentOrSelfBackground = Background ?? FindParentBackground();
7776

78-
if(Parent is Inline inline)
79-
{
80-
if(textDecorations == null)
81-
{
82-
textDecorations = inline.TextDecorations;
83-
}
84-
85-
if(background == null)
86-
{
87-
background = inline.Background;
88-
}
89-
}
90-
91-
var fontStyle = FontStyle;
77+
var typeface = new Typeface(
78+
FontFamily,
79+
FontStyle,
80+
FontWeight,
81+
FontStretch);
9282

93-
if(Parent is Italic)
94-
{
95-
fontStyle = FontStyle.Italic;
96-
}
83+
return new GenericTextRunProperties(
84+
typeface,
85+
FontFeatures,
86+
FontSize,
87+
TextDecorations,
88+
Foreground,
89+
parentOrSelfBackground,
90+
BaselineAlignment);
91+
}
9792

98-
var fontWeight = FontWeight;
93+
/// <summary>
94+
/// Searches for the next parent inline element with a non-null Background and returns its Background brush.
95+
/// </summary>
96+
/// <returns>The first non-null Background brush found in parent inline elements, or null if none is found.</returns>
97+
private IBrush? FindParentBackground()
98+
{
99+
var parent = Parent;
99100

100-
if(Parent is Bold)
101+
while (parent is Inline inline)
101102
{
102-
fontWeight = FontWeight.Bold;
103+
if (inline.Background != null)
104+
{
105+
return inline.Background;
106+
}
107+
108+
parent = inline.Parent;
103109
}
104110

105-
return new GenericTextRunProperties(
106-
new Typeface(FontFamily, fontStyle, fontWeight),
107-
FontFeatures,
108-
FontSize,
109-
textDecorations,
110-
Foreground,
111-
background,
112-
BaselineAlignment);
111+
return null;
113112
}
114113

115114
/// <inheritdoc />

src/Avalonia.Controls/Documents/Italic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ static Italic()
1313
{
1414
FontStyleProperty.OverrideDefaultValue<Italic>(FontStyle.Italic);
1515
}
16+
17+
public Italic()
18+
{
19+
SetCurrentValue(FontStyleProperty, FontStyle.Italic);
20+
}
1621
}
1722
}

src/Avalonia.Controls/Documents/Underline.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ static Underline()
1111
{
1212
TextDecorationsProperty.OverrideDefaultValue<Underline>(Media.TextDecorations.Underline);
1313
}
14+
15+
public Underline()
16+
{
17+
SetCurrentValue(TextDecorationsProperty, Media.TextDecorations.Underline);
18+
}
1419
}
1520
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Avalonia.Media;
2+
using Avalonia.UnitTests;
3+
using Avalonia.Media.TextFormatting;
4+
using Avalonia.Controls.Documents;
5+
using Xunit;
6+
using System.Collections.Generic;
7+
8+
namespace Avalonia.Controls.UnitTests
9+
{
10+
public class InlineTests : ScopedTestBase
11+
{
12+
[Fact]
13+
public void Should_Inherit_FontWeight_In_Nested_Inlines()
14+
{
15+
var bold = new Bold();
16+
var span = new Span();
17+
var run = new Run("Test");
18+
span.Inlines.Add(run);
19+
bold.Inlines.Add(span);
20+
21+
var textRuns = new List<TextRun>();
22+
bold.BuildTextRun(textRuns, default);
23+
24+
var runProperties = textRuns[0].Properties;
25+
Assert.Equal(FontWeight.Bold, runProperties.Typeface.Weight);
26+
}
27+
28+
[Fact]
29+
public void Should_Inherit_FontStyle_In_Nested_Inlines()
30+
{
31+
var italic = new Italic();
32+
var span = new Span();
33+
var run = new Run("Test");
34+
span.Inlines.Add(run);
35+
italic.Inlines.Add(span);
36+
37+
var textRuns = new List<TextRun>();
38+
italic.BuildTextRun(textRuns, default);
39+
40+
var runProperties = textRuns[0].Properties;
41+
Assert.Equal(FontStyle.Italic, runProperties.Typeface.Style);
42+
}
43+
44+
[Fact]
45+
public void Should_Inherit_FontStretch_In_Nested_Inlines()
46+
{
47+
var span = new Span();
48+
var innerSpan = new Span();
49+
var run = new Run("Test");
50+
span.FontStretch = FontStretch.Condensed;
51+
innerSpan.Inlines.Add(run);
52+
span.Inlines.Add(innerSpan);
53+
54+
var textRuns = new List<TextRun>();
55+
span.BuildTextRun(textRuns, default);
56+
57+
var runProperties = textRuns[0].Properties;
58+
Assert.Equal(FontStretch.Condensed, runProperties.Typeface.Stretch);
59+
}
60+
61+
[Fact]
62+
public void Should_Inherit_Background_In_Nested_Inlines()
63+
{
64+
var backgroundBrush = Brushes.Red;
65+
var span = new Span();
66+
var innerSpan = new Span();
67+
var run = new Run("Test");
68+
69+
span.Background = backgroundBrush;
70+
innerSpan.Inlines.Add(run);
71+
span.Inlines.Add(innerSpan);
72+
73+
var textRuns = new List<TextRun>();
74+
span.BuildTextRun(textRuns, default);
75+
76+
var runProperties = textRuns[0].Properties;
77+
Assert.Equal(backgroundBrush, runProperties.BackgroundBrush);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)