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.Point;
15 import java.awt.Rectangle;
16 import java.awt.Toolkit;
17 import java.awt.geom.Point2D;
18 import java.awt.geom.Rectangle2D;
21 * @author Tuukka Lehtonen
24 public class DPIUtil {
26 private static boolean initialized = false;
27 private static boolean hasZoom;
29 private static float upscaleFactorF;
30 private static double upscaleFactorD;
31 private static float downscaleFactorF;
32 private static double downscaleFactorD;
34 private static void initialize() {
38 double dpi = Toolkit.getDefaultToolkit().getScreenResolution();
40 int zoom = (int) Math.round(100.0 * dpi / baseDpi);
41 hasZoom = zoom != 100;
43 upscaleFactorD = dpi / baseDpi;
44 upscaleFactorF = (float) upscaleFactorD;
45 downscaleFactorD = baseDpi / dpi;
46 downscaleFactorF = (float) downscaleFactorD;
48 // System.out.format("DPIUtil:%n\tswt zoom = %d%n\tdownscale factor = %f%n\tupscale factor = %f%n",
59 private static Rectangle scale(float s, Rectangle r, Rectangle target) {
64 return new Rectangle(r.x, r.y, r.width, r.height);
68 target.width = r.width;
69 target.height = r.height;
77 Math.round(r.width*s),
78 Math.round(r.height*s));
80 target.x = Math.round(r.x*s);
81 target.y = Math.round(r.y*s);
82 target.width = Math.round(r.width*s);
83 target.height = Math.round(r.height*s);
88 private static Rectangle2D scale(double s, Rectangle2D r, Rectangle2D target) {
93 return (Rectangle2D) r.clone();
98 target = (Rectangle2D) r.clone();
99 target.setFrame(r.getX()*s, r.getY()*s, r.getWidth()*s, r.getHeight()*s);
103 private static double downscale0(double x) {
104 return hasZoom ? x * downscaleFactorD : x;
107 private static int downscaleToInteger0(double x) {
108 return (int)(hasZoom ? Math.round((double) x * downscaleFactorD) : x);
111 private static int downscale0(int x) {
112 return hasZoom ? (int) Math.round((double) x * downscaleFactorD) : x;
115 private static double upscale0(double x) {
116 return hasZoom ? x * upscaleFactorD : x;
119 private static int upscaleToInteger0(double x) {
120 return (int)(hasZoom ? Math.round((double) x * upscaleFactorD) : x);
123 private static int upscale0(int x) {
124 return hasZoom ? (int) Math.round((double) x * upscaleFactorD) : x;
129 public static double downscale(double x) {
131 return downscale0(x);
134 public static int downscale(int x) {
136 return downscale0(x);
139 public static Point2D downscale(double x, double y) {
142 return new Point2D.Double(x, y);
143 double s = downscaleFactorD;
144 return new Point2D.Double(x * s, y * s);
147 public static Point downscale(int x, int y) {
149 return new Point(downscale0(x), downscale0(y));
152 public static Point2D downscale(Point2D p) {
153 return downscale(p.getX(), p.getY());
156 public static Point downscaleToInteger(Point2D p) {
158 return new Point(downscaleToInteger0(p.getX()), downscaleToInteger0(p.getY()));
161 public static Rectangle2D downscale(Rectangle2D r, Rectangle2D target) {
163 return scale(downscaleFactorD, r, target);
166 public static Rectangle2D downscale(Rectangle2D r) {
167 return downscale(r, null);
170 public static Rectangle downscale(Rectangle r, Rectangle target) {
172 return scale(downscaleFactorF, r, target);
175 public static Rectangle downscale(Rectangle r) {
176 return downscale(r, null);
179 public static Rectangle downscaleToInteger(Rectangle2D r) {
181 return new Rectangle(
182 downscaleToInteger0(r.getMinX()),
183 downscaleToInteger0(r.getMinY()),
184 downscaleToInteger0(r.getWidth()),
185 downscaleToInteger0(r.getHeight()));
190 public static double upscale(double x) {
195 public static int upscale(int x) {
200 public static Point2D upscale(double x, double y) {
203 return new Point2D.Double(x, y);
204 double s = upscaleFactorD;
205 return new Point2D.Double(x * s, y * s);
208 public static Point upscale(int x, int y) {
210 return new Point(upscale0(x), upscale0(y));
213 public static Point2D upscale(Point2D p) {
215 return (hasZoom && p != null) ? upscale(p.getX(), p.getY()) : p;
218 public static Point upscaleToInteger(Point2D p) {
220 return new Point(upscaleToInteger0(p.getX()), upscaleToInteger0(p.getY()));
223 public static Point upscale(Point p) {
225 return (hasZoom && p != null) ? upscale(p.x, p.y) : p;
228 public static Rectangle2D upscale(Rectangle2D r, Rectangle2D target) {
230 return scale(upscaleFactorD, r, target);
233 public static Rectangle upscale(Rectangle r, Rectangle target) {
235 return scale(upscaleFactorF, r, target);
238 public static Rectangle2D upscale(Rectangle2D r) {
239 return upscale(r, null);
242 public static Rectangle upscale(Rectangle r) {
243 return upscale(r, null);
246 public static Rectangle upscaleToInteger(Rectangle2D r) {
247 return new Rectangle(
248 upscaleToInteger0(r.getMinX()),
249 upscaleToInteger0(r.getMinY()),
250 upscaleToInteger0(r.getWidth()),
251 upscaleToInteger0(r.getHeight()));