Skip to content

Commit

Permalink
Extend DPIUtil to provide scale up and down methods with target zoom …
Browse files Browse the repository at this point in the history
…as parameter

This contribution adds additional methods to scale different datatypes up or down by passing the target zoom level as parameter. All existing methods will delegate to the added methods and pass DPIUtil::deviceZoom as zoom.

Contributes to eclipse-platform#62
and eclipse-platform#131
  • Loading branch information
akoch-yatta committed Apr 24, 2024
1 parent 979d3f1 commit 3cda284
Show file tree
Hide file tree
Showing 3 changed files with 446 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Daniel Kruegler - #420 - [High DPI] "swt.autoScale" should add new "half" option
* Yatta Solutions - #131 - Additional methods to specify target zoom directly
*******************************************************************************/
package org.eclipse.swt.internal;

Expand Down Expand Up @@ -124,9 +125,13 @@ public static int[] autoScaleDown(Drawable drawable, int[] pointArray) {
/**
* Auto-scale down float array dimensions.
*/
public static float[] autoScaleDown (float size[]) {
if (deviceZoom == 100 || size == null) return size;
float scaleFactor = getScalingFactor ();
public static float[] autoScaleDown(float size[]) {
return autoScaleDown(size, deviceZoom);
}

public static float[] autoScaleDown(float size[], int zoom) {
if (zoom == 100 || size == null) return size;
float scaleFactor = getScalingFactor (zoom);
float scaledSize[] = new float[size.length];
for (int i = 0; i < scaledSize.length; i++) {
scaledSize[i] = size[i] / scaleFactor;
Expand All @@ -137,50 +142,75 @@ public static float[] autoScaleDown (float size[]) {
/**
* Auto-scale down float array dimensions if enabled for Drawable class.
*/
public static float[] autoScaleDown (Drawable drawable, float size[]) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
public static float[] autoScaleDown(Drawable drawable, float size[]) {
return autoScaleDown(drawable, size, deviceZoom);
}

public static float[] autoScaleDown(Drawable drawable, float size[], int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return autoScaleDown(size, zoom);
}

/**
* Auto-scale down int dimensions.
*/
public static int autoScaleDown (int size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
public static int autoScaleDown(int size) {
return autoScaleDown(size, deviceZoom);
}

public static int autoScaleDown(int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (zoom);
return Math.round (size / scaleFactor);
}

/**
* Auto-scale down int dimensions if enabled for Drawable class.
*/
public static int autoScaleDown (Drawable drawable, int size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
public static int autoScaleDown(Drawable drawable, int size) {
return autoScaleDown(drawable, size, deviceZoom);
}

public static int autoScaleDown(Drawable drawable, int size, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return autoScaleDown (size, zoom);
}

/**
* Auto-scale down float dimensions.
*/
public static float autoScaleDown (float size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
public static float autoScaleDown(float size) {
return autoScaleDown(size, deviceZoom);
}

public static float autoScaleDown(float size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (zoom);
return (size / scaleFactor);
}

/**
* Auto-scale down float dimensions if enabled for Drawable class.
*/
public static float autoScaleDown (Drawable drawable, float size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
public static float autoScaleDown(Drawable drawable, float size) {
return autoScaleDown (drawable, size, deviceZoom);
}

public static float autoScaleDown(Drawable drawable, float size, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return autoScaleDown (size, zoom);
}

/**
* Returns a new scaled down Point.
*/
public static Point autoScaleDown (Point point) {
if (deviceZoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor ();
public static Point autoScaleDown(Point point) {
return autoScaleDown(point, deviceZoom);
}

public static Point autoScaleDown(Point point, int zoom) {
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor(zoom);
Point scaledPoint = new Point (0,0);
scaledPoint.x = Math.round (point.x / scaleFactor);
scaledPoint.y = Math.round (point.y / scaleFactor);
Expand All @@ -190,19 +220,27 @@ public static Point autoScaleDown (Point point) {
/**
* Returns a new scaled down Point if enabled for Drawable class.
*/
public static Point autoScaleDown (Drawable drawable, Point point) {
if (drawable != null && !drawable.isAutoScalable ()) return point;
return autoScaleDown (point);
public static Point autoScaleDown(Drawable drawable, Point point) {
return autoScaleDown(drawable, point, deviceZoom);
}

public static Point autoScaleDown(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return autoScaleDown (point, zoom);
}

/**
* Returns a new scaled down Rectangle.
*/
public static Rectangle autoScaleDown (Rectangle rect) {
if (deviceZoom == 100 || rect == null) return rect;
public static Rectangle autoScaleDown(Rectangle rect) {
return autoScaleDown(rect, deviceZoom);
}

public static Rectangle autoScaleDown(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle (0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleDown (new Point (rect.x, rect.y));
Point scaledBottomRight = DPIUtil.autoScaleDown (new Point (rect.x + rect.width, rect.y + rect.height));
Point scaledTopLeft = DPIUtil.autoScaleDown(new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = DPIUtil.autoScaleDown(new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand All @@ -213,9 +251,14 @@ public static Rectangle autoScaleDown (Rectangle rect) {
/**
* Returns a new scaled down Rectangle if enabled for Drawable class.
*/
public static Rectangle autoScaleDown (Drawable drawable, Rectangle rect) {
if (drawable != null && !drawable.isAutoScalable ()) return rect;
return autoScaleDown (rect);
public static Rectangle autoScaleDown(Drawable drawable, Rectangle rect) {
if (drawable != null && !drawable.isAutoScalable()) return rect;
return autoScaleDown(rect, deviceZoom);
}

public static Rectangle autoScaleDown(Drawable drawable, Rectangle rect, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return rect;
return autoScaleDown (rect, zoom);
}

/**
Expand Down Expand Up @@ -288,26 +331,40 @@ public static ImageData autoScaleUp (Device device, final ImageData imageData) {
}

public static int[] autoScaleUp(int[] pointArray) {
if (deviceZoom == 100 || pointArray == null) return pointArray;
float scaleFactor = getScalingFactor ();
int [] returnArray = new int[pointArray.length];
return autoScaleUp(pointArray, deviceZoom);
}

public static int[] autoScaleUp(int[] pointArray, int zoom) {
if (zoom == 100 || pointArray == null) return pointArray;
float scaleFactor = getScalingFactor(zoom);
int[] returnArray = new int[pointArray.length];
for (int i = 0; i < pointArray.length; i++) {
returnArray [i] = Math.round (pointArray [i] * scaleFactor);
}
return returnArray;
}

public static int[] autoScaleUp(Drawable drawable, int[] pointArray) {
if (drawable != null && !drawable.isAutoScalable ()) return pointArray;
return autoScaleUp (pointArray);
return autoScaleUp(drawable, pointArray, deviceZoom);
}

public static int[] autoScaleUp(Drawable drawable, int[] pointArray, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return pointArray;
return autoScaleUp (pointArray, zoom);
}
/**
* Auto-scale up int dimensions.
*/
public static int autoScaleUp (int size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
public static int autoScaleUp(int size) {
return autoScaleUp(size, deviceZoom);
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int autoScaleUp(int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor(zoom);
return Math.round (size * scaleFactor);
}

Expand All @@ -323,29 +380,45 @@ public static int autoScaleUpUsingNativeDPI (int size) {
/**
* Auto-scale up int dimensions if enabled for Drawable class.
*/
public static int autoScaleUp (Drawable drawable, int size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleUp (size);
public static int autoScaleUp(Drawable drawable, int size) {
return autoScaleUp(drawable, size, deviceZoom);
}

public static int autoScaleUp(Drawable drawable, int size, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return autoScaleUp (size, zoom);
}

public static float autoScaleUp(float size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return autoScaleUp(size, deviceZoom);
}

public static float autoScaleUp(float size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor(zoom);
return (size * scaleFactor);
}

public static float autoScaleUp(Drawable drawable, float size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleUp (size);
return autoScaleUp(drawable, size, deviceZoom);
}

public static float autoScaleUp(Drawable drawable, float size, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return autoScaleUp (size, zoom);
}

/**
* Returns a new scaled up Point.
*/
public static Point autoScaleUp (Point point) {
if (deviceZoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor ();
Point scaledPoint = new Point (0,0);
public static Point autoScaleUp(Point point) {
return autoScaleUp(point, deviceZoom);
}

public static Point autoScaleUp(Point point, int zoom) {
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor(zoom);
Point scaledPoint = new Point(0,0);
scaledPoint.x = Math.round (point.x * scaleFactor);
scaledPoint.y = Math.round (point.y * scaleFactor);
return scaledPoint;
Expand All @@ -354,19 +427,27 @@ public static Point autoScaleUp (Point point) {
/**
* Returns a new scaled up Point if enabled for Drawable class.
*/
public static Point autoScaleUp (Drawable drawable, Point point) {
if (drawable != null && !drawable.isAutoScalable ()) return point;
return autoScaleUp (point);
public static Point autoScaleUp(Drawable drawable, Point point) {
return autoScaleUp (drawable, point, deviceZoom);
}

public static Point autoScaleUp(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return autoScaleUp (point, zoom);
}

/**
* Returns a new scaled up Rectangle.
*/
public static Rectangle autoScaleUp (Rectangle rect) {
if (deviceZoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle (0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point (rect.x, rect.y));
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point (rect.x + rect.width, rect.y + rect.height));
public static Rectangle autoScaleUp(Rectangle rect) {
return autoScaleUp(rect, deviceZoom);
}

public static Rectangle autoScaleUp(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle(0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point(rect.x, rect.y), zoom);
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point(rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand All @@ -378,20 +459,31 @@ public static Rectangle autoScaleUp (Rectangle rect) {
/**
* Returns a new scaled up Rectangle if enabled for Drawable class.
*/
public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect) {
if (drawable != null && !drawable.isAutoScalable ()) return rect;
return autoScaleUp (rect);
public static Rectangle autoScaleUp(Drawable drawable, Rectangle rect) {
return autoScaleUp(drawable, rect, deviceZoom);
}

public static Rectangle autoScaleUp(Drawable drawable, Rectangle rect, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return rect;
return autoScaleUp (rect, zoom);
}

/**
* Returns Scaling factor from the display
* @return float scaling factor
*/
private static float getScalingFactor () {
return getScalingFactor(deviceZoom);
}

private static float getScalingFactor(int zoom) {
if (useCairoAutoScale) {
return 1;
}
return deviceZoom / 100f;
if (zoom <= 0) {
zoom = deviceZoom;
}
return zoom / 100f;
}

/**
Expand Down Expand Up @@ -552,4 +644,4 @@ public ImageData getImageData(int zoom) {
return DPIUtil.autoScaleImageData(device, imageData, zoom, currentZoom);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
Test_org_eclipse_swt_accessibility_Accessible.class,
Test_org_eclipse_swt_accessibility_AccessibleControlEvent.class,
Test_org_eclipse_swt_accessibility_AccessibleEvent.class,
Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class })
Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class,
DPIUtilTests.class})
public class AllNonBrowserTests {
private static List<Error> leakedResources;

Expand Down
Loading

0 comments on commit 3cda284

Please sign in to comment.