Skip to content

Commit 3b2eaaf

Browse files
authored
FontConverter restore order of font properties (dotnet/runtime#45746)
.NET Framework implementation of `FontConverter` provided a different order of font properties, that can be observed by inspecting an instance of a font in a `PropertyGrid`. Restore the original sort order. The .NET Framework implementation has another sort argument "Weight", which however doesn't appear to be a property of `Font` type. It is possible this property has existed at some point, or the converter may have been expected to work for instances of `IFontDisp` object. Either way presence or absence of "Weight" doesn't appear to make any difference, hence it has not been ported across. Fixes dotnet/runtime#45631 Commit migrated from dotnet/runtime@22328c5
1 parent e63aeac commit 3b2eaaf

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/System.Drawing.Common/src/System/Drawing/FontConverter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ public override PropertyDescriptorCollection GetProperties(
365365
object? value,
366366
Attribute[]? attributes)
367367
{
368-
return value is Font ? TypeDescriptor.GetProperties(value, attributes) : base.GetProperties(context, value, attributes);
368+
if (value is not Font)
369+
return base.GetProperties(context, value, attributes);
370+
371+
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value, attributes);
372+
return props.Sort(new string[] { nameof(Font.Name), nameof(Font.Size), nameof(Font.Unit) });
369373
}
370374

371375
public override bool GetPropertiesSupported(ITypeDescriptorContext context) => true;

src/System.Drawing.Common/tests/System/Drawing/FontConverterTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ public void EmptyStringInput()
8181
Assert.Null(font);
8282
}
8383

84+
[ConditionalFact(Helpers.IsDrawingSupported)]
85+
public void GetFontPropsSorted()
86+
{
87+
// The order provided since .NET Framework
88+
string[] expectedPropNames = new[]
89+
{
90+
nameof(Font.Name),
91+
nameof(Font.Size),
92+
nameof(Font.Unit),
93+
nameof(Font.Bold),
94+
nameof(Font.GdiCharSet),
95+
nameof(Font.GdiVerticalFont),
96+
nameof(Font.Italic),
97+
nameof(Font.Strikeout),
98+
nameof(Font.Underline),
99+
};
100+
101+
FontConverter converter = new FontConverter();
102+
Font font = new($"Courier New", 8.25f, FontStyle.Regular, GraphicsUnit.Point);
103+
104+
PropertyDescriptorCollection props = converter.GetProperties(font);
105+
string[] propNames = new string[props.Count];
106+
107+
int index = 0;
108+
foreach (PropertyDescriptor prop in props)
109+
{
110+
propNames[index++] = prop.DisplayName;
111+
}
112+
113+
Assert.True(propNames.SequenceEqual(expectedPropNames));
114+
}
115+
84116
public static TheoryData<string, string, float, GraphicsUnit, FontStyle> TestConvertFormData()
85117
{
86118
var data = PlatformDetection.IsWindows ?

0 commit comments

Comments
 (0)