3 import java.awt.HeadlessException;
5 public class HiDPISetting
7 private static final int hidpiThreshold = 160;
9 private static final int hidpiMultiThreshold = 240;
11 private static final int bigScreenThreshold = 1400;
13 public static final String scalePropertyName = "sun.java2d.uiScale";
15 private static final boolean isLinux;
17 // private static final boolean isAMac;
19 // private static final boolean isWindows;
21 public static final String setHiDPIPropertyName = "setHiDPI";
23 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
25 private static boolean setHiDPI = false;
27 private static int setHiDPIScale = 0;
29 public static int dpi = 0;
31 public static int mindimension = 0;
33 public static int width = 0;
35 public static int scale = 0;
37 public final static int MAX_SCALE = 8;
39 private static boolean doneInit = false;
41 private static boolean allowScalePropertyArg = false;
43 private static ScreenInfo screenInfo = new ScreenInfo();
47 String system = System.getProperty("os.name") == null ? null
48 : System.getProperty("os.name").toLowerCase();
51 isLinux = system.indexOf("linux") > -1;
52 // isAMac = system.indexOf("mac") > -1;
53 // isWindows = system.indexOf("windows") > -1;
58 // isAMac = isWindows = false;
62 private static void init()
69 // get and use command line property values first
70 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
71 boolean setHiDPIPropertyBool = Boolean.parseBoolean(setHiDPIProperty);
73 // allow -DsetHiDPI=false to turn off HiDPI scaling
74 if (setHiDPIProperty != null && !setHiDPIPropertyBool)
81 setHiDPI = setHiDPIProperty != null && setHiDPIPropertyBool;
83 String setHiDPIScaleProperty = System
84 .getProperty(setHiDPIScalePropertyName);
85 if (setHiDPIScaleProperty != null)
89 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
90 // if setHiDPIScale property is validly set and setHiDPI property wasn't
91 // attempted to be set we assume setHiDPIScale to be true
92 if (setHiDPIProperty == null)
96 } catch (NumberFormatException e)
98 System.err.println(setHiDPIScalePropertyName + " property give ("
99 + setHiDPIScaleProperty + ") but not parseable as integer");
102 if (setHiDPI && setHiDPIScale > 0)
104 setHiDPIScale(setHiDPIScale);
108 // check to see if the scale property has already been set by something else
110 String existingProperty = System.getProperty(scalePropertyName);
111 if (existingProperty != null)
115 int existingPropertyVal = Integer.parseInt(existingProperty);
116 System.out.println("Existing " + scalePropertyName + " is "
117 + existingPropertyVal);
118 if (existingPropertyVal > 1)
120 setHiDPIScale(existingPropertyVal);
123 } catch (NumberFormatException e)
125 System.out.println("Could not convert property " + scalePropertyName
126 + " vale '" + existingProperty + "' to number");
130 // Try and auto guess a good scale based on reported DPI (not trustworthy)
131 // and screen resolution (more trustworthy)
134 screenInfo = getScreenInfo();
137 dpi = screenInfo.getScreenResolution();
138 } catch (HeadlessException e)
140 System.err.println("Cannot get screen resolution: " + e.getMessage());
143 // try and get screen size height and width
146 int height = screenInfo.getScreenHeight();
147 int width = screenInfo.getScreenWidth();
148 // using mindimension in case of portrait screens
149 mindimension = Math.min(height, width);
150 } catch (HeadlessException e)
153 "Cannot get screen size height and width:" + e.getMessage());
156 // attempt at a formula for scaling based on screen dpi and mindimension.
157 // scale will be an integer >=1. This formula is based on some testing and
160 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
161 // of hidpiMultiThreshold (else scale=1)
162 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
163 int dpiScale = (dpi - hidpiThreshold > 0)
164 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
167 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
169 // reject outrageous values -- dpiScale in particular could be mistaken
170 if (dpiScale > MAX_SCALE) {
173 if (dimensionScale > MAX_SCALE) {
177 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
178 // as dpiScale often misreported)
179 int autoScale = Math.max(dpiScale, dimensionScale);
181 // only make an automatic change if scale is changed and other conditions
182 // (OS is linux) apply, or if setHiDPI has been specified
183 if ((autoScale > 1 && isLinux) || setHiDPI)
185 setHiDPIScale(autoScale);
189 // looks like we're not doing any scaling
193 public static void setHiDPIScale(int s)
196 allowScalePropertyArg = true;
200 public static String getScalePropertyArg(int s)
202 return "-D" + scalePropertyName + "=" + String.valueOf(s);
205 public static String getScalePropertyArg()
208 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
209 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
212 public static void clear()
221 allowScalePropertyArg = false;
224 public static void setScreenInfo(ScreenInfo si)
229 public static ScreenInfo getScreenInfo()
231 if (screenInfo == null)
233 screenInfo = new ScreenInfo();