Use rounding in both wxWindow::FromPhys() and ToPhys()

It seems better to round, rather than truncate, in ToPhys(), for the
same reasons as in wxBitmap::CreateScaled() (see parent commit), and
then ceil() mustn't be used in FromPhys() neither, as this would break
round-tripping via both functions.

So, finally, keep the behaviour simple and, hopefully, the least
surprising, by just rounding the result in both functions.
This commit is contained in:
Vadim Zeitlin
2022-01-22 21:40:36 +00:00
parent 51dc85c72d
commit dad828da38
2 changed files with 10 additions and 14 deletions

View File

@@ -1217,11 +1217,12 @@ public:
factor under the other platforms. factor under the other platforms.
Note that dividing an integer value by scale factor doesn't always Note that dividing an integer value by scale factor doesn't always
yield an integer value. This function always round the resulting value yield an integer value. This function rounds the resulting value to
up, e.g. 15 physical pixels are translated to 8, not 7, logical pixels the closest integer, e.g. 15 physical pixels are translated to 8, not
in 200% DPI scaling. This ensures that a physical bitmap of size 15 is 7, logical pixels in 200% DPI scaling. This ensures that a physical
not truncated if the result of this function is used to create a window bitmap of size 15 is not truncated if the result of this function is
to show it, but it does mean that there will be one extra pixel left. used to create a window to show it, but it does mean that there will be
one extra pixel, not covered by this bitmap, left.
@see FromDIP(), ToPhys() @see FromDIP(), ToPhys()

View File

@@ -2905,15 +2905,10 @@ wxSize wxWindowBase::FromPhys(wxSize sz, const wxWindowBase* w)
if ( scale != 1.0 ) if ( scale != 1.0 )
{ {
// We prefer to round up the size so that conversion from physical
// pixels to logical and back doesn't result in smaller value, as this
// would e.g. truncate the bitmap of odd size when drawing it at 200%
// scaling. Leaving an extra pixel in this case seems like a lesser
// evil, even if not ideal.
if ( sz.x != wxDefaultCoord ) if ( sz.x != wxDefaultCoord )
sz.x = ceil(sz.x) / scale; sz.x = wxRound(sz.x / scale);
if ( sz.y != wxDefaultCoord ) if ( sz.y != wxDefaultCoord )
sz.y = ceil(sz.y) / scale; sz.y = wxRound(sz.y / scale);
} }
return sz; return sz;
@@ -2927,9 +2922,9 @@ wxSize wxWindowBase::ToPhys(wxSize sz, const wxWindowBase* w)
if ( scale != 1.0 ) if ( scale != 1.0 )
{ {
if ( sz.x != wxDefaultCoord ) if ( sz.x != wxDefaultCoord )
sz.x *= scale; sz.x = wxRound(sz.x * scale);
if ( sz.y != wxDefaultCoord ) if ( sz.y != wxDefaultCoord )
sz.y *= scale; sz.y = wxRound(sz.y * scale);
} }
return sz; return sz;