]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/DPIUtil.java
Add check for headless installations in DPIUtil
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / DPIUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.utils;
13
14 import java.awt.GraphicsEnvironment;
15 import java.awt.Point;
16 import java.awt.Rectangle;
17 import java.awt.Toolkit;
18 import java.awt.geom.Point2D;
19 import java.awt.geom.Rectangle2D;
20
21 /**
22  * @author Tuukka Lehtonen
23  * @since 1.36.0
24  */
25 public class DPIUtil {
26
27         private static boolean initialized = false;
28         private static boolean hasZoom;
29
30         private static float upscaleFactorF;
31         private static double upscaleFactorD;
32         private static float downscaleFactorF;
33         private static double downscaleFactorD;
34
35         private static void initialize() {
36                 if (initialized)
37                         return;
38
39                 double dpi;
40                 if (!GraphicsEnvironment.isHeadless())
41                         dpi = Toolkit.getDefaultToolkit().getScreenResolution();
42                 else
43                         dpi = 96;
44                 double baseDpi = 96;
45                 int zoom = (int) Math.round(100.0 * dpi / baseDpi);
46                 hasZoom = zoom != 100;
47
48                 upscaleFactorD   = dpi / baseDpi;
49                 upscaleFactorF   = (float) upscaleFactorD;
50                 downscaleFactorD = baseDpi / dpi;
51                 downscaleFactorF   = (float) downscaleFactorD;
52
53 //              System.out.format("DPIUtil:%n\tswt zoom = %d%n\tdownscale factor = %f%n\tupscale factor = %f%n",
54 //                              zoom,
55 //                              downscaleFactorD,
56 //                              upscaleFactorD
57 //                              );
58
59                 initialized = true;
60         }
61
62         // Internals
63
64         private static Rectangle scale(float s, Rectangle r, Rectangle target) {
65                 if (s == 1.0f) {
66                         if (r == target)
67                                 return r;
68                         if (target == null) {
69                                 return new Rectangle(r.x, r.y, r.width, r.height);
70                         } else {
71                                 target.x = r.x;
72                                 target.y = r.y;
73                                 target.width = r.width;
74                                 target.height = r.height;
75                                 return target;
76                         }
77                 }
78                 if (target == null) {
79                         return new Rectangle(
80                                         Math.round(r.x*s),
81                                         Math.round(r.y*s),
82                                         Math.round(r.width*s),
83                                         Math.round(r.height*s));
84                 } else {
85                         target.x = Math.round(r.x*s);
86                         target.y = Math.round(r.y*s);
87                         target.width = Math.round(r.width*s);
88                         target.height = Math.round(r.height*s);
89                         return target;
90                 }
91         }
92
93         private static Rectangle2D scale(double s, Rectangle2D r, Rectangle2D target) {
94                 if (s == 1.0) {
95                         if (r == target)
96                                 return r;
97                         if (target == null)
98                                 return (Rectangle2D) r.clone();
99                         target.setFrame(r);
100                         return target;
101                 }
102                 if (target == null)
103                         target = (Rectangle2D) r.clone();
104                 target.setFrame(r.getX()*s, r.getY()*s, r.getWidth()*s, r.getHeight()*s);
105                 return target;
106         }
107
108         private static double downscale0(double x) {
109                 return hasZoom ? x * downscaleFactorD : x;
110         }
111
112         private static int downscaleToInteger0(double x) {
113                 return (int)(hasZoom ? Math.round((double) x * downscaleFactorD) : x);
114         }
115
116         private static int downscale0(int x) {
117                 return hasZoom ? (int) Math.round((double) x * downscaleFactorD) : x;
118         }
119
120         private static double upscale0(double x) {
121                 return hasZoom ? x * upscaleFactorD : x;
122         }
123
124         private static int upscaleToInteger0(double x) {
125                 return (int)(hasZoom ? Math.round((double) x * upscaleFactorD) : x);
126         }
127
128         private static int upscale0(int x) {
129                 return hasZoom ? (int) Math.round((double) x * upscaleFactorD) : x;
130         }
131
132         // Downscaling
133
134         public static double downscale(double x) {
135                 initialize();
136                 return downscale0(x);
137         }
138
139         public static int downscale(int x) {
140                 initialize();
141                 return downscale0(x);
142         }
143
144         public static Point2D downscale(double x, double y) {
145                 initialize();
146                 if (!hasZoom)
147                         return new Point2D.Double(x, y);
148                 double s = downscaleFactorD;
149                 return new Point2D.Double(x * s, y * s);
150         }
151
152         public static Point downscale(int x, int y) {
153                 initialize();
154                 return new Point(downscale0(x), downscale0(y));
155         }
156
157         public static Point2D downscale(Point2D p) {
158                 return downscale(p.getX(), p.getY());
159         }
160
161         public static Point downscaleToInteger(Point2D p) {
162                 initialize();
163                 return new Point(downscaleToInteger0(p.getX()), downscaleToInteger0(p.getY()));
164         }
165
166         public static Rectangle2D downscale(Rectangle2D r, Rectangle2D target) {
167                 initialize();
168                 return scale(downscaleFactorD, r, target);
169         }
170
171         public static Rectangle2D downscale(Rectangle2D r) {
172                 return downscale(r, null);
173         }
174
175         public static Rectangle downscale(Rectangle r, Rectangle target) {
176                 initialize();
177                 return scale(downscaleFactorF, r, target);
178         }
179
180         public static Rectangle downscale(Rectangle r) {
181                 return downscale(r, null);
182         }
183
184         public static Rectangle downscaleToInteger(Rectangle2D r) {
185                 initialize();
186                 return new Rectangle( 
187                                 downscaleToInteger0(r.getMinX()),
188                                 downscaleToInteger0(r.getMinY()),
189                                 downscaleToInteger0(r.getWidth()),
190                                 downscaleToInteger0(r.getHeight()));
191         }
192
193         // Upscaling
194
195         public static double upscale(double x) {
196                 initialize();
197                 return upscale0(x);
198         }
199
200         public static int upscale(int x) {
201                 initialize();
202                 return upscale0(x);
203         }
204
205         public static Point2D upscale(double x, double y) {
206                 initialize();
207                 if (!hasZoom)
208                         return new Point2D.Double(x, y);
209                 double s = upscaleFactorD;
210                 return new Point2D.Double(x * s, y * s);
211         }
212
213         public static Point upscale(int x, int y) {
214                 initialize();
215                 return new Point(upscale0(x), upscale0(y));
216         }
217
218         public static Point2D upscale(Point2D p) {
219                 initialize();
220                 return (hasZoom && p != null) ? upscale(p.getX(), p.getY()) : p;
221         }
222
223         public static Point upscaleToInteger(Point2D p) {
224                 initialize();
225                 return new Point(upscaleToInteger0(p.getX()), upscaleToInteger0(p.getY()));
226         }
227
228         public static Point upscale(Point p) {
229                 initialize();
230                 return (hasZoom && p != null) ? upscale(p.x, p.y) : p;
231         }
232
233         public static Rectangle2D upscale(Rectangle2D r, Rectangle2D target) {
234                 initialize();
235                 return scale(upscaleFactorD, r, target);
236         }
237
238         public static Rectangle upscale(Rectangle r, Rectangle target) {
239                 initialize();
240                 return scale(upscaleFactorF, r, target);
241         }
242
243         public static Rectangle2D upscale(Rectangle2D r) {
244                 return upscale(r, null);
245         }
246
247         public static Rectangle upscale(Rectangle r) {
248                 return upscale(r, null);
249         }
250
251         public static Rectangle upscaleToInteger(Rectangle2D r) {
252                 return new Rectangle( 
253                                 upscaleToInteger0(r.getMinX()),
254                                 upscaleToInteger0(r.getMinY()),
255                                 upscaleToInteger0(r.getWidth()),
256                                 upscaleToInteger0(r.getHeight()));
257         }
258
259 }