Allow parsing all fractional sizes in wxFont descriptions

Remove the check that the size representation was the same as float and
as double, which was supposed to catch various edge cases (NaNs, huge
numbers etc) but actually caught plenty of perfectly valid font sizes
such as 13.8 that simply lost precision when converting from double to
float.

Just check that the size is positive and less than FLT_MAX to avoid
using values that really don't make sense as font sizes.

Also add a unit test checking that using fractional font sizes in
description string works as expected.

Closes #18590.

Closes https://github.com/wxWidgets/wxWidgets/pull/1707
This commit is contained in:
Vadim Zeitlin
2020-01-16 00:07:06 +01:00
parent 28098259a8
commit a73194f6b4
4 changed files with 37 additions and 6 deletions

View File

@@ -449,4 +449,28 @@ TEST_CASE("wxFont::NativeFontInfoUserDesc", "[font][fontinfo]")
CHECK( test.GetEncoding() == temp2.GetEncoding() );
#endif
}
// Test for a bug with handling fractional font sizes in description
// strings (see #18590).
wxFont font(*wxNORMAL_FONT);
static const float sizes[] = { 12.0f, 10.5f, 13.8f, 10.123f, 11.1f };
for ( unsigned n = 0; n < WXSIZEOF(sizes); n++ )
{
font.SetFractionalPointSize(sizes[n]);
// Just setting the font can slightly change it because of rounding
// errors, so don't expect the actual size to be exactly equal to what
// we used -- but close enough.
const float sizeUsed = font.GetFractionalPointSize();
CHECK( sizeUsed == Approx(sizes[n]).epsilon(0.001) );
const wxString& desc = font.GetNativeFontInfoDesc();
INFO("Font description: " << desc);
CHECK( font.SetNativeFontInfo(desc) );
// Notice that here we use the exact comparison, there is no reason for
// a differently rounded size to be used.
CHECK( font.GetFractionalPointSize() == sizeUsed );
}
}