JAL-3833 cap the scaling used by HiDPISetting for incorrect DPI values or incorrect...
[jalview.git] / src / jalview / bin / HiDPISetting.java
index 497900f..7a22607 100644 (file)
@@ -1,7 +1,6 @@
 package jalview.bin;
 
 import java.awt.HeadlessException;
-import java.awt.Toolkit;
 
 public class HiDPISetting
 {
@@ -11,7 +10,7 @@ public class HiDPISetting
 
   private static final int bigScreenThreshold = 1400;
 
-  private static final String scalePropertyName = "sun.java2d.uiScale";
+  public static final String scalePropertyName = "sun.java2d.uiScale";
 
   private static final boolean isLinux;
 
@@ -35,10 +34,14 @@ public class HiDPISetting
 
   public static int scale = 0;
 
+  public final static int MAX_SCALE = 8;
+
   private static boolean doneInit = false;
 
   private static boolean allowScalePropertyArg = false;
 
+  private static ScreenInfo screenInfo = new ScreenInfo();
+
   static
   {
     String system = System.getProperty("os.name") == null ? null
@@ -113,9 +116,10 @@ public class HiDPISetting
     // and screen resolution (more trustworthy)
 
     // get screen dpi
+    screenInfo = getScreenInfo();
     try
     {
-      dpi = Toolkit.getDefaultToolkit().getScreenResolution();
+      dpi = screenInfo.getScreenResolution();
     } catch (HeadlessException e)
     {
       System.err.println("Cannot get screen resolution: " + e.getMessage());
@@ -124,8 +128,8 @@ public class HiDPISetting
     // try and get screen size height and width
     try
     {
-      int height = Toolkit.getDefaultToolkit().getScreenSize().height;
-      int width = Toolkit.getDefaultToolkit().getScreenSize().width;
+      int height = screenInfo.getScreenHeight();
+      int width = screenInfo.getScreenWidth();
       // using mindimension in case of portrait screens
       mindimension = Math.min(height, width);
     } catch (HeadlessException e)
@@ -147,6 +151,14 @@ public class HiDPISetting
 
     int dimensionScale = 1 + (mindimension / bigScreenThreshold);
 
+    // reject outrageous values -- dpiScale in particular could be mistaken
+    if (dpiScale > MAX_SCALE) {
+      dpiScale = 1;
+    }
+    if (dimensionScale > MAX_SCALE) {
+      dimensionScale = 1;
+    }
+
     // choose larger of dimensionScale or dpiScale (most likely dimensionScale
     // as dpiScale often misreported)
     int autoScale = Math.max(dpiScale, dimensionScale);
@@ -170,11 +182,41 @@ public class HiDPISetting
     doneInit = true;
   }
 
+  public static String getScalePropertyArg(int s)
+  {
+    return "-D" + scalePropertyName + "=" + String.valueOf(s);
+  }
+
   public static String getScalePropertyArg()
   {
     init();
     // HiDPI setting. Just looking at Linux to start with. Test with Windows.
-    return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale
-            : null;
+    return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
+  }
+
+  public static void clear()
+  {
+    setHiDPI = false;
+    setHiDPIScale = 0;
+    dpi = 0;
+    mindimension = 0;
+    width = 0;
+    scale = 0;
+    doneInit = false;
+    allowScalePropertyArg = false;
+  }
+
+  public static void setScreenInfo(ScreenInfo si)
+  {
+    screenInfo = si;
+  }
+
+  public static ScreenInfo getScreenInfo()
+  {
+    if (screenInfo == null)
+    {
+      screenInfo = new ScreenInfo();
+    }
+    return screenInfo;
   }
 }