1 /*******************************************************************************
2 * Copyright (c) 2018 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.utils;
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;
22 * @author Tuukka Lehtonen
25 public class DPIUtil {
27 private static boolean initialized = false;
28 private static boolean hasZoom;
30 private static float upscaleFactorF;
31 private static double upscaleFactorD;
32 private static float downscaleFactorF;
33 private static double downscaleFactorD;
35 private static void initialize() {
40 if (!GraphicsEnvironment.isHeadless())
41 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
45 int zoom = (int) Math.round(100.0 * dpi / baseDpi);
46 hasZoom = zoom != 100;
48 upscaleFactorD = dpi / baseDpi;
49 upscaleFactorF = (float) upscaleFactorD;
50 downscaleFactorD = baseDpi / dpi;
51 downscaleFactorF = (float) downscaleFactorD;
53 // System.out.format("DPIUtil:%n\tswt zoom = %d%n\tdownscale factor = %f%n\tupscale factor = %f%n",
64 private static Rectangle scale(float s, Rectangle r, Rectangle target) {
69 return new Rectangle(r.x, r.y, r.width, r.height);
73 target.width = r.width;
74 target.height = r.height;
82 Math.round(r.width*s),
83 Math.round(r.height*s));
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);
93 private static Rectangle2D scale(double s, Rectangle2D r, Rectangle2D target) {
98 return (Rectangle2D) r.clone();
103 target = (Rectangle2D) r.clone();
104 target.setFrame(r.getX()*s, r.getY()*s, r.getWidth()*s, r.getHeight()*s);
108 private static double downscale0(double x) {
109 return hasZoom ? x * downscaleFactorD : x;
112 private static int downscaleToInteger0(double x) {
113 return (int)(hasZoom ? Math.round((double) x * downscaleFactorD) : x);
116 private static int downscale0(int x) {
117 return hasZoom ? (int) Math.round((double) x * downscaleFactorD) : x;
120 private static double upscale0(double x) {
121 return hasZoom ? x * upscaleFactorD : x;
124 private static int upscaleToInteger0(double x) {
125 return (int)(hasZoom ? Math.round((double) x * upscaleFactorD) : x);
128 private static int upscale0(int x) {
129 return hasZoom ? (int) Math.round((double) x * upscaleFactorD) : x;
134 public static double downscale(double x) {
136 return downscale0(x);
139 public static int downscale(int x) {
141 return downscale0(x);
144 public static Point2D downscale(double x, double y) {
147 return new Point2D.Double(x, y);
148 double s = downscaleFactorD;
149 return new Point2D.Double(x * s, y * s);
152 public static Point downscale(int x, int y) {
154 return new Point(downscale0(x), downscale0(y));
157 public static Point2D downscale(Point2D p) {
158 return downscale(p.getX(), p.getY());
161 public static Point downscaleToInteger(Point2D p) {
163 return new Point(downscaleToInteger0(p.getX()), downscaleToInteger0(p.getY()));
166 public static Rectangle2D downscale(Rectangle2D r, Rectangle2D target) {
168 return scale(downscaleFactorD, r, target);
171 public static Rectangle2D downscale(Rectangle2D r) {
172 return downscale(r, null);
175 public static Rectangle downscale(Rectangle r, Rectangle target) {
177 return scale(downscaleFactorF, r, target);
180 public static Rectangle downscale(Rectangle r) {
181 return downscale(r, null);
184 public static Rectangle downscaleToInteger(Rectangle2D r) {
186 return new Rectangle(
187 downscaleToInteger0(r.getMinX()),
188 downscaleToInteger0(r.getMinY()),
189 downscaleToInteger0(r.getWidth()),
190 downscaleToInteger0(r.getHeight()));
195 public static double upscale(double x) {
200 public static int upscale(int x) {
205 public static Point2D upscale(double x, double y) {
208 return new Point2D.Double(x, y);
209 double s = upscaleFactorD;
210 return new Point2D.Double(x * s, y * s);
213 public static Point upscale(int x, int y) {
215 return new Point(upscale0(x), upscale0(y));
218 public static Point2D upscale(Point2D p) {
220 return (hasZoom && p != null) ? upscale(p.getX(), p.getY()) : p;
223 public static Point upscaleToInteger(Point2D p) {
225 return new Point(upscaleToInteger0(p.getX()), upscaleToInteger0(p.getY()));
228 public static Point upscale(Point p) {
230 return (hasZoom && p != null) ? upscale(p.x, p.y) : p;
233 public static Rectangle2D upscale(Rectangle2D r, Rectangle2D target) {
235 return scale(upscaleFactorD, r, target);
238 public static Rectangle upscale(Rectangle r, Rectangle target) {
240 return scale(upscaleFactorF, r, target);
243 public static Rectangle2D upscale(Rectangle2D r) {
244 return upscale(r, null);
247 public static Rectangle upscale(Rectangle r) {
248 return upscale(r, null);
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()));