From 6445acc7832ccd1f2fc5211a68c90f4426ab4346 Mon Sep 17 00:00:00 2001
From: Julian Smart <julian@anthemion.co.uk>
Date: Sun, 29 Oct 2006 17:01:34 +0000
Subject: [PATCH] Applied patch 1586499: wxCoordRound function Centralises
 rounding and adds nearest-value rounding to prntbase.cpp. by Robert J. Lang

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42624 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/function.tex    | 16 +++++++++
 include/wx/defs.h             | 12 +++++++
 include/wx/dfb/dc.h           | 44 +++++-------------------
 include/wx/gtk/gnome/gprint.h | 44 +++++-------------------
 include/wx/gtk1/dc.h          | 44 +++++-------------------
 include/wx/mac/carbon/dc.h    | 63 ++++++-----------------------------
 include/wx/mac/classic/dc.h   | 58 +++++++-------------------------
 include/wx/mgl/dc.h           | 44 +++++-------------------
 include/wx/x11/dc.h           | 44 +++++-------------------
 src/common/prntbase.cpp       | 32 +++++++++---------
 src/mac/carbon/dccg.cpp       | 44 +++++-------------------
 11 files changed, 114 insertions(+), 331 deletions(-)

diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 96b383f984..47d46b1b40 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -58,6 +58,7 @@ the corresponding topic.
 \helpref{wxCONCAT}{wxconcat}\\
 \helpref{wxConcatFiles}{wxconcatfiles}\\
 \helpref{wxConstCast}{wxconstcast}\\
+\helpref{wxCoordRound}{wxcoordround}\\
 \helpref{wxCopyFile}{wxcopyfile}\\
 \helpref{wxCreateDynamicObject}{wxcreatedynamicobject}\\
 \helpref{wxCreateFileTipProvider}{wxcreatefiletipprovider}\\
@@ -2846,6 +2847,21 @@ code which might have to be compiled with an old compiler without support for
 this language feature but still take advantage of it when it is available.
 
 
+
+\membersection{::wxCoordRound}\label{wxcoordround}
+
+\func{wxCoord}{wxCoordRound}{\param{const float\& }{f}}
+
+\func{wxCoord}{wxCoordRound}{\param{const double\& }{f}}
+
+Convert \em{f} to a wxCoord, using round-to-nearest. This is commonly used
+in scaling calculations.
+
+\wxheading{Include files}
+
+<wx/defs.h>
+
+
 \membersection{::wxGetKeyState}\label{wxgetkeystate}
 
 \func{bool}{wxGetKeyState}{\param{wxKeyCode }{key}}
diff --git a/include/wx/defs.h b/include/wx/defs.h
index 8082fe9d2a..831f870c5e 100644
--- a/include/wx/defs.h
+++ b/include/wx/defs.h
@@ -658,6 +658,18 @@ typedef int wxCoord;
 
 enum {  wxDefaultCoord = -1 };
 
+/*  round-to-nearest (used in scaling) */
+#ifdef __cplusplus
+inline wxCoord wxCoordRound(const float& f)
+{
+    return (f > 0) ? (wxCoord)(f + 0.5) : (f < 0) ? (wxCoord)(f - 0.5) : 0;
+}
+inline wxCoord wxCoordRound(const double& f)
+{
+    return (f > 0) ? (wxCoord)(f + 0.5) : (f < 0) ? (wxCoord)(f - 0.5) : 0;
+}
+#endif
+
 /*  ---------------------------------------------------------------------------- */
 /*  define fixed length types */
 /*  ---------------------------------------------------------------------------- */
diff --git a/include/wx/dfb/dc.h b/include/wx/dfb/dc.h
index 92abb84e39..0f189d4eeb 100644
--- a/include/wx/dfb/dc.h
+++ b/include/wx/dfb/dc.h
@@ -80,63 +80,35 @@ public:
 
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        wxCoord new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        wxCoord new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        wxCoord new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        wxCoord new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 
     // Returns the surface (and increases its ref count)
diff --git a/include/wx/gtk/gnome/gprint.h b/include/wx/gtk/gnome/gprint.h
index 4cdb6d999c..8490d504c2 100644
--- a/include/wx/gtk/gnome/gprint.h
+++ b/include/wx/gtk/gnome/gprint.h
@@ -296,63 +296,35 @@ private:
 private:
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        wxCoord new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        wxCoord new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        wxCoord new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        wxCoord new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 private:
     DECLARE_DYNAMIC_CLASS(wxGnomePrintDC)
diff --git a/include/wx/gtk1/dc.h b/include/wx/gtk1/dc.h
index 82f2ae49e7..fe00bef38f 100644
--- a/include/wx/gtk1/dc.h
+++ b/include/wx/gtk1/dc.h
@@ -70,63 +70,35 @@ public:
 
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        wxCoord new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        wxCoord new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        wxCoord new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        wxCoord new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 
 protected:
diff --git a/include/wx/mac/carbon/dc.h b/include/wx/mac/carbon/dc.h
index 2b40a0daa0..6b10ee2093 100644
--- a/include/wx/mac/carbon/dc.h
+++ b/include/wx/mac/carbon/dc.h
@@ -158,88 +158,45 @@ public:
 public:
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        long new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)new_x / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)new_x / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
-
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)x / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)x / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
-
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        long new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)new_y / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)new_y / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
-
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)y / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)y / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
-
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        long new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)new_x * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)new_x * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
-
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)x * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)x * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
-
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        long new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)new_y * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)new_y * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
-
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)y * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)y * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 
     wxCoord XLOG2DEVMAC(wxCoord x) const
     {
-        long new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)new_x * m_scaleX + 0.5) * m_signX + m_deviceOriginX + m_macLocalOrigin.x;
-        else
-            return (wxCoord)((double)new_x * m_scaleX - 0.5) * m_signX + m_deviceOriginX + m_macLocalOrigin.x;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX + m_macLocalOrigin.x;
     }
 
     wxCoord YLOG2DEVMAC(wxCoord y) const
     {
-        long new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)new_y * m_scaleY + 0.5) * m_signY + m_deviceOriginY + m_macLocalOrigin.y;
-        else
-            return (wxCoord)((double)new_y * m_scaleY - 0.5) * m_signY + m_deviceOriginY + m_macLocalOrigin.y;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY + m_macLocalOrigin.y;
     }
 
 #if wxMAC_USE_CORE_GRAPHICS
diff --git a/include/wx/mac/classic/dc.h b/include/wx/mac/classic/dc.h
index 88b7dacfb3..392b4f7a19 100644
--- a/include/wx/mac/classic/dc.h
+++ b/include/wx/mac/classic/dc.h
@@ -105,79 +105,45 @@ class WXDLLEXPORT wxDC: public wxDCBase
 
     wxCoord XDEV2LOG(wxCoord x) const
     {
-      long new_x = x - m_deviceOriginX ;
-      if (new_x > 0)
-        return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-      else
-        return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-      if (x > 0)
-        return (wxCoord)((double)(x) / m_scaleX + 0.5);
-      else
-        return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-      long new_y = y - m_deviceOriginY ;
-      if (new_y > 0)
-        return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-      else
-        return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-      if (y > 0)
-        return (wxCoord)((double)(y) / m_scaleY + 0.5);
-      else
-        return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-      long new_x = x - m_logicalOriginX;
-      if (new_x > 0)
-        return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX  ;
-      else
-        return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX ;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-      if (x > 0)
-        return (wxCoord)((double)(x) * m_scaleX + 0.5);
-      else
-        return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-      long new_y = y - m_logicalOriginY ;
-      if (new_y > 0)
-        return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY ;
-      else
-        return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY ;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-      if (y > 0)
-        return (wxCoord)((double)(y) * m_scaleY + 0.5);
-      else
-        return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
+
     wxCoord XLOG2DEVMAC(wxCoord x) const
     {
-      long new_x = x - m_logicalOriginX;
-      if (new_x > 0)
-        return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX + m_macLocalOrigin.x ;
-      else
-        return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX + m_macLocalOrigin.x ;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX + m_macLocalOrigin.x;
     }
+
     wxCoord YLOG2DEVMAC(wxCoord y) const
     {
-      long new_y = y - m_logicalOriginY ;
-      if (new_y > 0)
-        return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY + m_macLocalOrigin.y ;
-      else
-        return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY + m_macLocalOrigin.y ;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY + m_macLocalOrigin.y;
     }
 
     WXHRGN MacGetCurrentClipRgn() { return m_macCurrentClipRgn ; }
diff --git a/include/wx/mgl/dc.h b/include/wx/mgl/dc.h
index 192a729417..a838fcd724 100644
--- a/include/wx/mgl/dc.h
+++ b/include/wx/mgl/dc.h
@@ -101,63 +101,35 @@ public:
 
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        wxCoord new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        wxCoord new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        wxCoord new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        wxCoord new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 
     MGLDevCtx *GetMGLDC() const { return m_MGLDC; }
diff --git a/include/wx/x11/dc.h b/include/wx/x11/dc.h
index 7c80e92e19..99ba4178b9 100644
--- a/include/wx/x11/dc.h
+++ b/include/wx/x11/dc.h
@@ -65,63 +65,35 @@ public:
 
     wxCoord XDEV2LOG(wxCoord x) const
     {
-        wxCoord new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)(new_x) / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+        return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
     }
     wxCoord XDEV2LOGREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) / m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) / m_scaleX - 0.5);
+        return wxCoordRound((double)(x) / m_scaleX);
     }
     wxCoord YDEV2LOG(wxCoord y) const
     {
-        wxCoord new_y = y - m_deviceOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-        else
-            return (wxCoord)((double)(new_y) / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+        return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
     }
     wxCoord YDEV2LOGREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) / m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) / m_scaleY - 0.5);
+        return wCoordRound((double)(y) / m_scaleY);
     }
     wxCoord XLOG2DEV(wxCoord x) const
     {
-        wxCoord new_x = x - m_logicalOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)(new_x) * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-        else
-            return (wxCoord)((double)(new_x) * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+        return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
     }
     wxCoord XLOG2DEVREL(wxCoord x) const
     {
-        if (x > 0)
-            return (wxCoord)((double)(x) * m_scaleX + 0.5);
-        else
-            return (wxCoord)((double)(x) * m_scaleX - 0.5);
+        return wxCoordRound((double)(x) * m_scaleX);
     }
     wxCoord YLOG2DEV(wxCoord y) const
     {
-        wxCoord new_y = y - m_logicalOriginY;
-        if (new_y > 0)
-            return (wxCoord)((double)(new_y) * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-        else
-            return (wxCoord)((double)(new_y) * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+        return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
     }
     wxCoord YLOG2DEVREL(wxCoord y) const
     {
-        if (y > 0)
-            return (wxCoord)((double)(y) * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)(y) * m_scaleY - 0.5);
+        return wxCoordRound((double)(y) * m_scaleY);
     }
 
 public:
diff --git a/src/common/prntbase.cpp b/src/common/prntbase.cpp
index 0620e82bdf..94b3de2a39 100644
--- a/src/common/prntbase.cpp
+++ b/src/common/prntbase.cpp
@@ -617,10 +617,10 @@ void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageS
     GetPageSizeMM(&mw, &mh);
     float mmToDeviceX = float(pw) / mw;
     float mmToDeviceY = float(ph) / mh;
-    wxRect pageMarginsRect(paperRect.x + wxCoord(mmToDeviceX * topLeft.x),
-        paperRect.y + wxCoord(mmToDeviceY * topLeft.y),
-        paperRect.width - wxCoord(mmToDeviceX * (topLeft.x + bottomRight.x)),
-        paperRect.height - wxCoord(mmToDeviceY * (topLeft.y + bottomRight.y)));
+    wxRect pageMarginsRect(paperRect.x + wxCoordRound(mmToDeviceX * topLeft.x),
+        paperRect.y + wxCoordRound(mmToDeviceY * topLeft.y),
+        paperRect.width - wxCoordRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
+        paperRect.height - wxCoordRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
     wxCoord w, h;
     m_printoutDC->GetSize(&w, &h);
     float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
@@ -708,10 +708,10 @@ wxRect wxPrintout::GetLogicalPaperRect() const
     // This DC doesn't match the printed page, so we have to scale.
     float scaleX = float(w) / pw;
     float scaleY = float(h) / ph;
-    return wxRect(m_printoutDC->DeviceToLogicalX(wxCoord(paperRect.x * scaleX)), 
-        m_printoutDC->DeviceToLogicalY(wxCoord(paperRect.y * scaleY)), 
-        m_printoutDC->DeviceToLogicalXRel(wxCoord(paperRect.width * scaleX)), 
-        m_printoutDC->DeviceToLogicalYRel(wxCoord(paperRect.height * scaleY)));
+    return wxRect(m_printoutDC->DeviceToLogicalX(wxCoordRound(paperRect.x * scaleX)), 
+        m_printoutDC->DeviceToLogicalY(wxCoordRound(paperRect.y * scaleY)), 
+        m_printoutDC->DeviceToLogicalXRel(wxCoordRound(paperRect.width * scaleX)), 
+        m_printoutDC->DeviceToLogicalYRel(wxCoordRound(paperRect.height * scaleY)));
 }
 
 wxRect wxPrintout::GetLogicalPageRect() const
@@ -740,10 +740,10 @@ wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSe
     GetPageSizeMM(&mw, &mh);
     float mmToDeviceX = float(pw) / mw;
     float mmToDeviceY = float(ph) / mh;
-    wxRect pageMarginsRect(paperRect.x + wxCoord(mmToDeviceX * topLeft.x),
-        paperRect.y + wxCoord(mmToDeviceY * topLeft.y),
-        paperRect.width - wxCoord(mmToDeviceX * (topLeft.x + bottomRight.x)),
-        paperRect.height - wxCoord(mmToDeviceY * (topLeft.y + bottomRight.y)));
+    wxRect pageMarginsRect(paperRect.x + wxCoordRound(mmToDeviceX * topLeft.x),
+        paperRect.y + wxCoordRound(mmToDeviceY * topLeft.y),
+        paperRect.width - wxCoordRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
+        paperRect.height - wxCoordRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
     wxCoord w, h;
     m_printoutDC->GetSize(&w, &h);
     if (w == pw && h == ph) {
@@ -756,10 +756,10 @@ wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSe
     // This DC doesn't match the printed page, so we have to scale.
     float scaleX = float(w) / pw;
     float scaleY = float(h) / ph;
-    return wxRect(m_printoutDC->DeviceToLogicalX(wxCoord(pageMarginsRect.x * scaleX)), 
-        m_printoutDC->DeviceToLogicalY(wxCoord(pageMarginsRect.y * scaleY)), 
-        m_printoutDC->DeviceToLogicalXRel(wxCoord(pageMarginsRect.width * scaleX)), 
-        m_printoutDC->DeviceToLogicalYRel(wxCoord(pageMarginsRect.height * scaleY)));
+    return wxRect(m_printoutDC->DeviceToLogicalX(wxCoordRound(pageMarginsRect.x * scaleX)), 
+        m_printoutDC->DeviceToLogicalY(wxCoordRound(pageMarginsRect.y * scaleY)), 
+        m_printoutDC->DeviceToLogicalXRel(wxCoordRound(pageMarginsRect.width * scaleX)), 
+        m_printoutDC->DeviceToLogicalYRel(wxCoordRound(pageMarginsRect.height * scaleY)));
 }
 
 void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
diff --git a/src/mac/carbon/dccg.cpp b/src/mac/carbon/dccg.cpp
index 01514f90f2..2d2f2e27a4 100755
--- a/src/mac/carbon/dccg.cpp
+++ b/src/mac/carbon/dccg.cpp
@@ -2286,70 +2286,42 @@ void wxDC::Clear(void)
 */ // TODO
 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
 {
-        long new_x = x - m_deviceOriginX;
-        if (new_x > 0)
-            return (wxCoord)((double)new_x / m_scaleX + 0.5) * m_signX + m_logicalOriginX;
-        else
-            return (wxCoord)((double)new_x / m_scaleX - 0.5) * m_signX + m_logicalOriginX;
+    return wxCoordRound((double)(x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
 }
 
 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
 {
-	long new_y = y - m_deviceOriginY;
-	if (new_y > 0)
-		return (wxCoord)((double)new_y / m_scaleY + 0.5) * m_signY + m_logicalOriginY;
-	else
-		return (wxCoord)((double)new_y / m_scaleY - 0.5) * m_signY + m_logicalOriginY;
+    return wxCoordRound((double)(y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
 }
 
 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
 {
-	if (x > 0)
-		return (wxCoord)((double)x / m_scaleX + 0.5);
-	else
-		return (wxCoord)((double)x / m_scaleX - 0.5);
+    return wxCoordRound((double)(x) / m_scaleX);
 }
 
 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
 {
-	if (y > 0)
-		return (wxCoord)((double)y / m_scaleY + 0.5);
-	else
-		return (wxCoord)((double)y / m_scaleY - 0.5);
+    return wxCoordRound((double)(y) / m_scaleY);
 }
 
 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
 {
-	long new_x = x - m_logicalOriginX;
-	if (new_x > 0)
-		return (wxCoord)((double)new_x * m_scaleX + 0.5) * m_signX + m_deviceOriginX;
-	else
-		return (wxCoord)((double)new_x * m_scaleX - 0.5) * m_signX + m_deviceOriginX;
+    return wxCoordRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
 }
 
 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
 {
-	long new_y = y - m_logicalOriginY;
-	if (new_y > 0)
-		return (wxCoord)((double)new_y * m_scaleY + 0.5) * m_signY + m_deviceOriginY;
-	else
-		return (wxCoord)((double)new_y * m_scaleY - 0.5) * m_signY + m_deviceOriginY;
+    return wxCoordRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
 }
 
 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
 {
-	if (x > 0)
-		return (wxCoord)((double)x * m_scaleX + 0.5);
-	else
-		return (wxCoord)((double)x * m_scaleX - 0.5);
+    return wxCoordRound((double)(x) * m_scaleX);
 }
 
 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
 {
-        if (y > 0)
-            return (wxCoord)((double)y * m_scaleY + 0.5);
-        else
-            return (wxCoord)((double)y * m_scaleY - 0.5);
+    return wxCoordRound((double)(y) * m_scaleY);
 }
 
 #endif // wxMAC_USE_CORE_GRAPHICS
-- 
2.50.0

