From 1adc3807dea544d29512510dc9546ec65b72e389 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 17 Sep 2018 15:34:42 +0300 Subject: [PATCH] Small fix for SWTDPIUtil to prevent uninitialized use gitlab #119 Change-Id: I27779078dca87927b9700a1acc3b26aed7cca308 --- .../org/simantics/utils/ui/SWTDPIUtil.java | 75 +++++++++++++------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java index 962276432..43d19efb2 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTDPIUtil.java @@ -105,17 +105,42 @@ public class SWTDPIUtil { return target; } + private static double downscaleSwt0(double x) { + return hasSwtScale ? x * fromSwtInternalScalingFactorD : x; + } + + private static int downscaleToIntegerSwt0(double x) { + return (int)(hasSwtScale ? Math.round((double) x * fromSwtInternalScalingFactorD) : x); + } + + private static int downscaleSwt0(int x) { + return hasSwtScale ? (int) Math.round((double) x * fromSwtInternalScalingFactorD) : x; + } + + private static double upscaleSwt0(double x) { + return hasSwtScale ? x * toSwtInternalScalingFactorD : x; + } + + private static int upscaleToIntegerSwt0(double x) { + return (int)(hasSwtScale ? Math.round((double) x * toSwtInternalScalingFactorD) : x); + } + + private static int upscaleSwt0(int x) { + return hasSwtScale ? (int) Math.round((double) x * toSwtInternalScalingFactorD) : x; + } + // SWT API Coordinates <-> pixels + // Downscaling + public static double downscaleSwt(double x) { initialize(); - return hasSwtScale ? x * fromSwtInternalScalingFactorD : x; + return downscaleSwt0(x); } public static int downscaleSwt(int x) { - if (!hasSwtScale) - return x; - return (int) Math.round(downscaleSwt((double) x)); + initialize(); + return downscaleSwt0(x); } public static Point2D downscaleSwt(double x, double y) { @@ -127,7 +152,8 @@ public class SWTDPIUtil { } public static Point downscaleSwt(int x, int y) { - return new Point(downscaleSwt(x), downscaleSwt(y)); + initialize(); + return new Point(downscaleSwt0(x), downscaleSwt0(y)); } public static Point2D downscaleSwt(Point2D p) { @@ -135,9 +161,8 @@ public class SWTDPIUtil { } public static Point downscaleSwtToInteger(Point2D p) { - return new Point( - (int) Math.round(downscaleSwt(p.getX())), - (int) Math.round(downscaleSwt(p.getY()))); + initialize(); + return new Point(downscaleToIntegerSwt0(p.getX()), downscaleToIntegerSwt0(p.getY())); } public static Rectangle2D downscaleSwt(Rectangle2D r, Rectangle2D target) { @@ -159,26 +184,28 @@ public class SWTDPIUtil { } public static Rectangle downscaleSwtToInteger(Rectangle2D r) { + initialize(); return new Rectangle( - (int) Math.round(downscaleSwt(r.getMinX())), - (int) Math.round(downscaleSwt(r.getMinY())), - (int) Math.round(downscaleSwt(r.getWidth())), - (int) Math.round(downscaleSwt(r.getHeight()))); + downscaleToIntegerSwt0(r.getMinX()), + downscaleToIntegerSwt0(r.getMinY()), + downscaleToIntegerSwt0(r.getWidth()), + downscaleToIntegerSwt0(r.getHeight())); } + // Upscaling + public static double upscaleSwt(double x) { initialize(); - return hasSwtScale ? x * toSwtInternalScalingFactorD : x; + return upscaleSwt0(x); } public static int upscaleSwt(int x) { initialize(); - if (!hasSwtScale) - return x; - return (int) Math.round((double) x * toSwtInternalScalingFactorD); + return upscaleSwt0(x); } public static Point2D upscaleSwt(double x, double y) { + initialize(); if (!hasSwtScale) return new Point2D.Double(x, y); double s = toSwtInternalScalingFactorD; @@ -186,7 +213,8 @@ public class SWTDPIUtil { } public static Point upscaleSwt(int x, int y) { - return new Point(upscaleSwt(x), upscaleSwt(y)); + initialize(); + return new Point(upscaleSwt0(x), upscaleSwt0(y)); } public static Point2D upscaleSwt(Point2D p) { @@ -194,9 +222,8 @@ public class SWTDPIUtil { } public static Point upscaleSwtToInteger(Point2D p) { - return new Point( - (int) Math.round(upscaleSwt(p.getX())), - (int) Math.round(upscaleSwt(p.getY()))); + initialize(); + return new Point(upscaleToIntegerSwt0(p.getX()), upscaleToIntegerSwt0(p.getY())); } public static Point upscaleSwt(Point p) { @@ -223,10 +250,10 @@ public class SWTDPIUtil { public static Rectangle upscaleSwtToInteger(Rectangle2D r) { return new Rectangle( - (int) Math.round(upscaleSwt(r.getMinX())), - (int) Math.round(upscaleSwt(r.getMinY())), - (int) Math.round(upscaleSwt(r.getWidth())), - (int) Math.round(upscaleSwt(r.getHeight()))); + upscaleToIntegerSwt0(r.getMinX()), + upscaleToIntegerSwt0(r.getMinY()), + upscaleToIntegerSwt0(r.getWidth()), + upscaleToIntegerSwt0(r.getHeight())); } } -- 2.43.2