diff --git a/bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java b/bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java index 20dfbd5f2a..68541e6326 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java +++ b/bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java @@ -286,7 +286,6 @@ public static Shell new_Shell (final Display display, final Canvas parent) { SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e); } if (handle == 0) SWT.error (SWT.ERROR_INVALID_ARGUMENT, null, " [peer not created]"); - final Shell shell = Shell.win32_new (display, handle); final ComponentListener listener = new ComponentAdapter () { @Override @@ -294,7 +293,7 @@ public void componentResized (ComponentEvent e) { display.syncExec (() -> { if (shell.isDisposed()) return; Dimension dim = parent.getSize (); - shell.setSize(DPIUtil.autoScaleDown(new Point(dim.width, dim.height))); // To Points + shell.setSize(DPIUtil.scaleDown(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points }); } }; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java index 840d258570..eb91911b6b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java @@ -1880,7 +1880,7 @@ void handleDOMEvent (OleEvent e) { int screenY = pVarResult.getInt(); pVarResult.dispose(); - Point position = DPIUtil.autoScaleDown(new Point(screenX, screenY)); // To Points + Point position = DPIUtil.scaleDown(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points position = browser.getDisplay().map(null, browser, position); newEvent.x = position.x; newEvent.y = position.y; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/WebSite.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/WebSite.java index 75574d434a..b88a7ff346 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/WebSite.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/WebSite.java @@ -320,8 +320,8 @@ int ShowContextMenu(int dwID, long ppt, long pcmdtReserved, long pdispReserved) Event event = new Event(); POINT pt = new POINT(); OS.MoveMemory(pt, ppt, POINT.sizeof); - pt.x = DPIUtil.autoScaleDown(pt.x); // To Points - pt.y = DPIUtil.autoScaleDown(pt.y); // To Points + pt.x = DPIUtil.scaleDown(pt.x, DPIUtil.getDeviceZoom()); // To Points + pt.y = DPIUtil.scaleDown(pt.y, DPIUtil.getDeviceZoom()); // To Points event.x = pt.x; event.y = pt.y; browser.notifyListeners(SWT.MenuDetect, event); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java index d1e95565b4..4b011c93b5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java @@ -1822,11 +1822,11 @@ public String toString () { * @noreference This method is not intended to be referenced by clients. */ public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) { - gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height), + gc.drawImage (original, 0, 0, CocoaDPIUtil.autoScaleDown (width), CocoaDPIUtil.autoScaleDown (height), /* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors. * Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..). */ - 0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor))); + 0, 0, Math.round (CocoaDPIUtil.autoScaleDown (width * scaleFactor)), Math.round (CocoaDPIUtil.autoScaleDown (height * scaleFactor))); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/CocoaDPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/CocoaDPIUtil.java new file mode 100644 index 0000000000..7e0e0aaba4 --- /dev/null +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/CocoaDPIUtil.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2022 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * 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; + +/** + * This class hold common constants and utility functions w.r.t. to SWT high DPI + * functionality. + *

+ * The {@code autoScaleUp(..)} methods convert from API coordinates (in SWT + * points) to internal high DPI coordinates (in pixels) that interface with + * native widgets. + *

+ *

+ * The {@code autoScaleDown(..)} convert from high DPI pixels to API coordinates + * (in SWT points). + *

+ * + * @since 3.105 + */ +public class CocoaDPIUtil { + + /** + * Auto-scale down int dimensions. + */ + public static int autoScaleDown(int size) { + return DPIUtil.scaleDown(size, DPIUtil.deviceZoom); + } + + /** + * Auto-scale down float dimensions. + */ + public static float autoScaleDown(float size) { + return DPIUtil.scaleDown(size, DPIUtil.deviceZoom); + } +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java index c5515b3fe7..4333190bb4 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java @@ -2613,31 +2613,31 @@ static void buildDitheredGradientChannel(int from, int to, int steps, static void fillGradientRectangle(GC gc, Device device, int x, int y, int width, int height, boolean vertical, RGB fromRGB, RGB toRGB, - int redBits, int greenBits, int blueBits) { + int redBits, int greenBits, int blueBits, int zoom) { /* Create the bitmap and tile it */ ImageData band = createGradientBand(width, height, vertical, fromRGB, toRGB, redBits, greenBits, blueBits); Image image = new Image(device, band); if ((band.width == 1) || (band.height == 1)) { - gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(band.width), DPIUtil.autoScaleDown(band.height), - DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(y), DPIUtil.autoScaleDown(width), - DPIUtil.autoScaleDown(height)); + gc.drawImage(image, 0, 0, DPIUtil.scaleDown(band.width, zoom), DPIUtil.scaleDown(band.height, zoom), + DPIUtil.scaleDown(x, zoom), DPIUtil.scaleDown(y, zoom), DPIUtil.scaleDown(width, zoom), + DPIUtil.scaleDown(height, zoom)); } else { if (vertical) { for (int dx = 0; dx < width; dx += band.width) { int blitWidth = width - dx; if (blitWidth > band.width) blitWidth = band.width; - gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(blitWidth), DPIUtil.autoScaleDown(band.height), - DPIUtil.autoScaleDown(dx + x), DPIUtil.autoScaleDown(y), DPIUtil.autoScaleDown(blitWidth), - DPIUtil.autoScaleDown(band.height)); + gc.drawImage(image, 0, 0, DPIUtil.scaleDown(blitWidth, zoom), DPIUtil.scaleDown(band.height, zoom), + DPIUtil.scaleDown(dx + x, zoom), DPIUtil.scaleDown(y, zoom), DPIUtil.scaleDown(blitWidth, zoom), + DPIUtil.scaleDown(band.height, zoom)); } } else { for (int dy = 0; dy < height; dy += band.height) { int blitHeight = height - dy; if (blitHeight > band.height) blitHeight = band.height; - gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(band.width), DPIUtil.autoScaleDown(blitHeight), - DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(dy + y), DPIUtil.autoScaleDown(band.width), - DPIUtil.autoScaleDown(blitHeight)); + gc.drawImage(image, 0, 0, DPIUtil.scaleDown(band.width, zoom), DPIUtil.scaleDown(blitHeight, zoom), + DPIUtil.scaleDown(x, zoom), DPIUtil.scaleDown(dy + y, zoom), DPIUtil.scaleDown(band.width, zoom), + DPIUtil.scaleDown(blitHeight, zoom)); } } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index a15f4683ae..36dfe1b48f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -40,7 +40,7 @@ public class DPIUtil { private static final int DPI_ZOOM_100 = 96; - private static int deviceZoom = 100; + public static int deviceZoom = 100; private static int nativeDeviceZoom = 100; private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH; @@ -59,7 +59,6 @@ public static Optional forString(String s) { private static AutoScaleMethod autoScaleMethod; private static String autoScaleValue; - private static final boolean USE_CAIRO_AUTOSCALE = SWT.getPlatform().equals("gtk"); /** * System property that controls the autoScale functionality. @@ -116,37 +115,6 @@ public static Optional forString(String s) { autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST; } -/** - * Auto-scale down ImageData - */ -public static ImageData autoScaleDown (Device device, final ImageData imageData) { - if (deviceZoom == 100 || imageData == null || (device != null && !device.isAutoScalable())) return imageData; - float scaleFactor = 1.0f / getScalingFactor (deviceZoom); - return autoScaleImageData(device, imageData, scaleFactor); -} - -public static int[] autoScaleDown(int[] pointArray) { - if (deviceZoom == 100 || pointArray == null) return pointArray; - float scaleFactor = getScalingFactor (deviceZoom); - 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[] autoScaleDown(Drawable drawable, int[] pointArray) { - if (drawable != null && !drawable.isAutoScalable ()) return pointArray; - return autoScaleDown (pointArray); -} - -/** - * Auto-scale down float array dimensions. - */ -public static float[] autoScaleDown(float size[]) { - return scaleDown(size, deviceZoom); -} - public static float[] scaleDown(float size[], int zoom) { if (zoom == 100 || size == null) return size; float scaleFactor = getScalingFactor (zoom); @@ -157,75 +125,33 @@ public static float[] scaleDown(float size[], int zoom) { return scaledSize; } -/** - * Auto-scale down float array dimensions if enabled for Drawable class. - */ -public static float[] autoScaleDown(Drawable drawable, float size[]) { - return scaleDown(drawable, size, deviceZoom); -} - public static float[] scaleDown(Drawable drawable, float size[], int zoom) { if (drawable != null && !drawable.isAutoScalable()) return size; return scaleDown(size, zoom); } -/** - * Auto-scale down int dimensions. - */ -public static int autoScaleDown(int size) { - return scaleDown(size, deviceZoom); -} - public static int scaleDown(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) { - return scaleDown(drawable, size, deviceZoom); -} - public static int scaleDown(Drawable drawable, int size, int zoom) { if (drawable != null && !drawable.isAutoScalable()) return size; return scaleDown (size, zoom); } -/** - * Auto-scale down float dimensions. - */ -public static float autoScaleDown(float size) { - return scaleDown(size, deviceZoom); -} - public static float scaleDown(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) { - return scaleDown (drawable, size, deviceZoom); -} - public static float scaleDown(Drawable drawable, float size, int zoom) { if (drawable != null && !drawable.isAutoScalable()) return size; return scaleDown (size, zoom); } -/** - * Returns a new scaled down Point. - */ -public static Point autoScaleDown(Point point) { - return scaleDown(point, deviceZoom); -} - public static Point scaleDown(Point point, int zoom) { if (zoom == 100 || point == null) return point; float scaleFactor = getScalingFactor(zoom); @@ -235,25 +161,11 @@ public static Point scaleDown(Point point, int zoom) { return scaledPoint; } -/** - * Returns a new scaled down Point if enabled for Drawable class. - */ -public static Point autoScaleDown(Drawable drawable, Point point) { - return scaleDown(drawable, point, deviceZoom); -} - public static Point scaleDown(Drawable drawable, Point point, int zoom) { if (drawable != null && !drawable.isAutoScalable()) return point; return scaleDown (point, zoom); } -/** - * Returns a new scaled down Rectangle. - */ -public static Rectangle autoScaleDown(Rectangle rect) { - return scaleDown(rect, deviceZoom); -} - public static Rectangle scaleDown(Rectangle rect, int zoom) { if (zoom == 100 || rect == null) return rect; Rectangle scaledRect = new Rectangle (0,0,0,0); @@ -266,14 +178,6 @@ public static Rectangle scaleDown(Rectangle rect, int zoom) { scaledRect.height = scaledBottomRight.y - scaledTopLeft.y; return scaledRect; } -/** - * 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 scaleDown(rect, deviceZoom); -} - public static Rectangle scaleDown(Drawable drawable, Rectangle rect, int zoom) { if (drawable != null && !drawable.isAutoScalable()) return rect; return scaleDown (rect, zoom); @@ -293,7 +197,7 @@ public static ImageData scaleImageData (Device device, final ElementAtZoom */ public void drawText(String string, int x, int y, boolean isTransparent) { - Point loc = DPIUtil.autoScaleUp(drawable, new Point (x, y)); + Point loc = new Point (x, y); drawTextInPixels(string, loc.x, loc.y, isTransparent); } void drawTextInPixels(String string, int x, int y, boolean isTransparent) { @@ -1444,7 +1444,7 @@ void drawTextInPixels(String string, int x, int y, boolean isTransparent) { * */ public void drawText (String string, int x, int y, int flags) { - Point loc = DPIUtil.autoScaleUp(drawable, new Point (x, y)); + Point loc = new Point (x, y); drawTextInPixels(string, loc.x, loc.y, flags); } void drawTextInPixels (String string, int x, int y, int flags) { @@ -1530,7 +1530,7 @@ public boolean equals(Object object) { */ public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - Rectangle rect = DPIUtil.autoScaleUp(drawable, new Rectangle(x, y, width, height)); + Rectangle rect = new Rectangle(x, y, width, height); fillArcInPixels(rect.x, rect.y, rect.width, rect.height, startAngle, arcAngle); } void fillArcInPixels(int x, int y, int width, int height, int startAngle, int arcAngle) { @@ -1589,7 +1589,7 @@ void fillArcInPixels(int x, int y, int width, int height, int startAngle, int ar */ public void fillGradientRectangle(int x, int y, int width, int height, boolean vertical) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - Rectangle rect = DPIUtil.autoScaleUp(drawable, new Rectangle(x, y, width, height)); + Rectangle rect = new Rectangle(x, y, width, height); fillGradientRectangleInPixels(rect.x, rect.y, rect.width, rect.height, vertical); } @@ -1673,7 +1673,7 @@ void fillGradientRectangleInPixels(int x, int y, int width, int height, boolean */ public void fillOval(int x, int y, int width, int height) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - Rectangle rect = DPIUtil.autoScaleUp(drawable, new Rectangle(x, y, width, height)); + Rectangle rect = new Rectangle(x, y, width, height); fillOvalInPixels(rect.x, rect.y, rect.width, rect.height); } void fillOvalInPixels(int x, int y, int width, int height) { @@ -1758,7 +1758,7 @@ public void fillPath (Path path) { public void fillPolygon(int[] pointArray) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - int [] scaledPointArray = DPIUtil.autoScaleUp(drawable, pointArray); + int [] scaledPointArray = pointArray; fillPolygonInPixels(scaledPointArray); } void fillPolygonInPixels(int[] pointArray) { @@ -1824,7 +1824,7 @@ void fillRectangleInPixels(int x, int y, int width, int height) { public void fillRectangle(Rectangle rect) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - fillRectangleInPixels(DPIUtil.autoScaleUp(drawable, rect)); + fillRectangleInPixels(rect); } void fillRectangleInPixels(Rectangle rect) { fillRectangleInPixels(rect.x, rect.y, rect.width, rect.height); @@ -1849,8 +1849,8 @@ void fillRectangleInPixels(Rectangle rect) { */ public void fillRoundRectangle(int x, int y, int width, int height, int arcWidth, int arcHeight) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - Rectangle rect = DPIUtil.autoScaleUp(drawable, new Rectangle(x, y, width, height)); - Point arcSize = DPIUtil.autoScaleUp(drawable, new Point(arcWidth, arcHeight)); + Rectangle rect = new Rectangle(x, y, width, height); + Point arcSize = new Point(arcWidth, arcHeight); fillRoundRectangleInPixels(rect.x, rect.y, rect.width, rect.height, arcSize.x, arcSize.y); } void fillRoundRectangleInPixels(int x, int y, int width, int height, int arcWidth, int arcHeight) { @@ -2074,7 +2074,7 @@ public int getCharWidth(char ch) { */ public Rectangle getClipping() { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - return DPIUtil.autoScaleDown(drawable, getClippingInPixels()); + return getClippingInPixels(); } Rectangle getClippingInPixels() { /* Calculate visible bounds in device space */ @@ -2251,11 +2251,11 @@ public FontMetrics getFontMetrics() { FontMetrics fm = new FontMetrics(); int ascent = OS.pango_font_metrics_get_ascent(metrics); int descent = OS.pango_font_metrics_get_descent(metrics); - int ascentInPoints = DPIUtil.autoScaleDown(drawable, OS.PANGO_PIXELS(ascent)); + int ascentInPoints = OS.PANGO_PIXELS(ascent); fm.ascentInPoints = ascentInPoints; - int heightInPoints = DPIUtil.autoScaleDown(drawable, OS.PANGO_PIXELS(ascent + descent)); + int heightInPoints = OS.PANGO_PIXELS(ascent + descent); fm.descentInPoints = heightInPoints - ascentInPoints; - fm.averageCharWidthInPoints = DPIUtil.autoScaleDown(drawable, OS.PANGO_PIXELS(OS.pango_font_metrics_get_approximate_char_width(metrics))); + fm.averageCharWidthInPoints = OS.PANGO_PIXELS(OS.pango_font_metrics_get_approximate_char_width(metrics)); OS.pango_font_metrics_unref(metrics); return fm; } @@ -2352,7 +2352,7 @@ public int getInterpolation() { public LineAttributes getLineAttributes() { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); LineAttributes attributes = getLineAttributesInPixels(); - attributes.width = DPIUtil.autoScaleDown(drawable, attributes.width); + attributes.width = attributes.width; return attributes; } LineAttributes getLineAttributesInPixels() { @@ -2453,7 +2453,7 @@ public int getLineStyle() { */ public int getLineWidth() { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - return (int)DPIUtil.autoScaleDown(drawable, data.lineWidth); + return getLineWidthInPixels(); } int getLineWidthInPixels() { return (int)data.lineWidth; @@ -3112,7 +3112,7 @@ void setClipping(long clipRgn) { */ public void setClipping(int x, int y, int width, int height) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - setClippingInPixels(DPIUtil.autoScaleUp(drawable, x), DPIUtil.autoScaleUp(drawable, y), DPIUtil.autoScaleUp(drawable, width), DPIUtil.autoScaleUp(drawable, height)); + setClippingInPixels(x, y, width, height); } void setClippingInPixels(int x, int y, int width, int height) { if (width < 0) { @@ -3190,7 +3190,7 @@ public void setClipping(Path path) { */ public void setClipping(Rectangle rect) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - setClippingInPixels(DPIUtil.autoScaleUp(drawable, rect)); + setClippingInPixels(rect); } void setClippingInPixels(Rectangle rect) { if (rect == null) { @@ -3415,7 +3415,7 @@ public void setInterpolation(int interpolation) { public void setLineAttributes(LineAttributes attributes) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (attributes == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - attributes.width = DPIUtil.autoScaleUp(drawable, attributes.width); + attributes.width = attributes.width; setLineAttributesInPixels(attributes); } void setLineAttributesInPixels(LineAttributes attributes) { @@ -3669,7 +3669,7 @@ public void setLineStyle(int lineStyle) { */ public void setLineWidth(int lineWidth) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - setLineWidthInPixels(DPIUtil.autoScaleUp(drawable, lineWidth)); + setLineWidthInPixels(lineWidth); } void setLineWidthInPixels(int lineWidth) { if (data.lineWidth == lineWidth) return; @@ -3853,7 +3853,7 @@ public void setXORMode(boolean xor) { * */ public Point stringExtent(String string) { - return DPIUtil.autoScaleDown(drawable, stringExtentInPixels(string)); + return stringExtentInPixels(string); } Point stringExtentInPixels(String string) { return textExtentInPixels(string, 0); @@ -3879,7 +3879,7 @@ Point stringExtentInPixels(String string) { * */ public Point textExtent(String string) { - return DPIUtil.autoScaleDown(drawable, textExtentInPixels(string)); + return textExtentInPixels(string); } Point textExtentInPixels(String string) { @@ -3920,7 +3920,7 @@ Point textExtentInPixels(String string) { public Point textExtent(String string, int flags) { if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (string == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - return DPIUtil.autoScaleDown(drawable, textExtentInPixels(string, flags)); + return textExtentInPixels(string, flags); } Point textExtentInPixels(String string, int flags) { setString(string, flags); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java index 74158802a5..57c160c623 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java @@ -214,7 +214,7 @@ public final class Image extends Resource implements Drawable { */ public Image(Device device, int width, int height) { super(device); - Point size = DPIUtil.autoScaleUp(new Point(width, height)); + Point size = new Point(width, height); currentDeviceZoom = DPIUtil.getDeviceZoom(); init(size.x, size.y); init(); @@ -411,8 +411,7 @@ public Image(Device device, Rectangle bounds) { super(device); if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); currentDeviceZoom = DPIUtil.getDeviceZoom(); - Rectangle bounds1 = DPIUtil.autoScaleUp (bounds); - init(bounds1.width, bounds1.height); + init(bounds.width, bounds.height); init(); } @@ -440,7 +439,7 @@ public Image(Device device, Rectangle bounds) { * @see #dispose() */ public Image(Device device, ImageData data) { - this(device, DPIUtil.autoScaleUp(device, data), DPIUtil.getDeviceZoom()); + this(device, GtkDPIUtil.autoScaleUp(device, data), DPIUtil.getDeviceZoom()); } private Image(Device device, ImageData data, int zoom) { @@ -489,8 +488,8 @@ public Image(Device device, ImageData source, ImageData mask) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } currentDeviceZoom = DPIUtil.getDeviceZoom(); - source = DPIUtil.autoScaleUp (device, source); - mask = DPIUtil.autoScaleUp (device, mask); + source = GtkDPIUtil.autoScaleUp (device, source); + mask = GtkDPIUtil.autoScaleUp (device, mask); mask = ImageData.convertMask (mask); ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data); image.maskPad = mask.scanlinePad; @@ -997,7 +996,7 @@ public Color getBackground() { */ public Rectangle getBounds() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - return DPIUtil.autoScaleDown(getBoundsInPixels()); + return getBoundsInPixels(); } /** @@ -1586,11 +1585,11 @@ public String toString () { * @noreference This method is not intended to be referenced by clients. */ public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) { - gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height), + gc.drawImage (original, 0, 0, width, height, /* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors. * Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..). */ - 0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor))); + 0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor)); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/TextLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/TextLayout.java index f7a8cbf6f5..afa6253e08 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/TextLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/TextLayout.java @@ -107,9 +107,9 @@ public FontMetrics getFixedLineMetrics(Device device) { } FontMetrics result = new FontMetrics(); - result.ascentInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.ascentInPoints); - result.descentInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.descentInPoints); - result.averageCharWidthInPoints = DPIUtil.autoScaleDown(device, lineMetricsInPixels.averageCharWidthInPoints); + result.ascentInPoints = lineMetricsInPixels.ascentInPoints; + result.descentInPoints = lineMetricsInPixels.descentInPoints; + result.averageCharWidthInPoints =lineMetricsInPixels.averageCharWidthInPoints; return result; } @@ -121,9 +121,9 @@ public void setFixedLineMetrics(Device device, FontMetrics metrics) { } FontMetrics result = new FontMetrics(); - result.ascentInPoints = DPIUtil.autoScaleUp(device, metrics.ascentInPoints); - result.descentInPoints = DPIUtil.autoScaleUp(device, metrics.descentInPoints); - result.averageCharWidthInPoints = DPIUtil.autoScaleUp(device, metrics.averageCharWidthInPoints); + result.ascentInPoints = metrics.ascentInPoints; + result.descentInPoints = metrics.descentInPoints; + result.averageCharWidthInPoints = metrics.averageCharWidthInPoints; lineMetricsInPixels = result; } @@ -296,8 +296,8 @@ void computeRuns () { boolean useMinAscentDescent = !metricsAdapter.isFixedMetrics() && (ascentInPoints != -1 || descentInPoints != -1); if (useMinAscentDescent && segementsLength > 0) { PangoRectangle rect = new PangoRectangle(); - if (ascentInPoints != -1) rect.y = -(DPIUtil.autoScaleUp(getDevice(), ascentInPoints) * OS.PANGO_SCALE); - rect.height = DPIUtil.autoScaleUp(getDevice(), (Math.max(0, ascentInPoints) + Math.max(0, descentInPoints))) * OS.PANGO_SCALE; + if (ascentInPoints != -1) rect.y = -(ascentInPoints * OS.PANGO_SCALE); + rect.height = (Math.max(0, ascentInPoints) + Math.max(0, descentInPoints)) * OS.PANGO_SCALE; int lineCount = OS.pango_layout_get_line_count(layout); chars = new char[segementsLength + lineCount * 2]; lineOffsets = new int [lineCount]; @@ -481,9 +481,9 @@ void computeRuns () { GlyphMetrics metrics = style.metrics; if (metrics != null) { PangoRectangle rect = new PangoRectangle(); - rect.y = -(DPIUtil.autoScaleUp(getDevice(), metrics.ascent) * OS.PANGO_SCALE); - rect.height = DPIUtil.autoScaleUp(getDevice(), (metrics.ascent + metrics.descent)) * OS.PANGO_SCALE; - rect.width = DPIUtil.autoScaleUp(getDevice(), metrics.width) * OS.PANGO_SCALE; + rect.y = -(metrics.ascent * OS.PANGO_SCALE); + rect.height = (metrics.ascent + metrics.descent) * OS.PANGO_SCALE; + rect.width = metrics.width * OS.PANGO_SCALE; long attr = OS.pango_attr_shape_new (rect, rect); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; @@ -494,7 +494,7 @@ void computeRuns () { } int rise = style.rise; if (rise != 0) { - long attr = OS.pango_attr_rise_new (DPIUtil.autoScaleUp(getDevice(), rise) * OS.PANGO_SCALE); + long attr = OS.pango_attr_rise_new (rise * OS.PANGO_SCALE); OS.memmove (attribute, attr, PangoAttribute.sizeof); attribute.start_index = byteStart; attribute.end_index = byteEnd; @@ -559,8 +559,6 @@ void destroy() { * */ public void draw(GC gc, int x, int y) { - x = DPIUtil.autoScaleUp(getDevice(), x); - y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y); } @@ -589,8 +587,6 @@ void drawInPixels(GC gc, int x, int y) { */ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { checkLayout (); - x = DPIUtil.autoScaleUp(getDevice(), x); - y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground); } void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { @@ -626,8 +622,6 @@ void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Col */ public void draw(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) { checkLayout (); - x = DPIUtil.autoScaleUp(getDevice(), x); - y = DPIUtil.autoScaleUp(getDevice(), y); drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, flags); } void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) { @@ -685,7 +679,7 @@ void drawInPixels(GC gc, int x, int y, int selectionStart, int selectionEnd, Col int lineY = y + OS.PANGO_PIXELS(rect.y); int height = OS.PANGO_PIXELS(rect.height); if (ascentInPoints != -1 && descentInPoints != -1) { - height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); + height = Math.max (height, ascentInPoints + descentInPoints); } height += getSpacingInPixels(); int width = (flags & SWT.FULL_SELECTION) != 0 ? 0x7fff : height / 3; @@ -918,7 +912,7 @@ public int getAscent () { */ public Rectangle getBounds() { int spacingInPixels = getSpacingInPixels(); - return DPIUtil.autoScaleDown(getDevice(), getBoundsInPixels(spacingInPixels)); + return getBoundsInPixels(spacingInPixels); } Rectangle getBoundsInPixels(int spacingInPixels) { @@ -931,7 +925,7 @@ Rectangle getBoundsInPixels(int spacingInPixels) { int width = OS.PANGO_PIXELS(w[0]); int height = OS.PANGO_PIXELS(h[0]); if (ascentInPoints != -1 && descentInPoints != -1) { - height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); + height = Math.max (height, ascentInPoints + descentInPoints); } height += spacingInPixels; return new Rectangle(0, 0, width, height + getScaledVerticalIndent()); @@ -953,7 +947,7 @@ Rectangle getBoundsInPixels(int spacingInPixels) { */ public Rectangle getBounds(int start, int end) { checkLayout(); - return DPIUtil.autoScaleDown(getDevice(), getBoundsInPixels(start, end)); + return getBoundsInPixels(start, end); } Rectangle getBoundsInPixels(int start, int end) { @@ -1059,7 +1053,7 @@ public Font getFont () { */ public int getIndent () { checkLayout(); - return DPIUtil.autoScaleDown(getDevice(), getIndentInPixels()); + return getIndentInPixels(); } int getIndentInPixels () { @@ -1141,7 +1135,7 @@ public int getLevel(int offset) { */ public Rectangle getLineBounds(int lineIndex) { checkLayout(); - return DPIUtil.autoScaleDown(getDevice(), getLineBoundsInPixels(lineIndex)); + return getLineBoundsInPixels(lineIndex); } Rectangle getLineBoundsInPixels(int lineIndex) { @@ -1166,7 +1160,7 @@ private Rectangle getLineBoundsInPixels(int lineIndex, long iter) { int width = OS.PANGO_PIXELS(rect.width); int height = OS.PANGO_PIXELS(rect.height); if (ascentInPoints != -1 && descentInPoints != -1) { - height = Math.max (height, DPIUtil.autoScaleUp(getDevice(), ascentInPoints + descentInPoints)); + height = Math.max (height, ascentInPoints + descentInPoints); } x += Math.min (indent, wrapIndent); return new Rectangle(x, y, width, height); @@ -1255,14 +1249,14 @@ public FontMetrics getLineMetrics (int lineIndex) { long metrics = OS.pango_context_get_metrics(context, font, lang); int ascent = OS.pango_font_metrics_get_ascent(metrics); int descent = OS.pango_font_metrics_get_descent(metrics); - ascentInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(ascent)); - heightInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(ascent + descent)); + ascentInPoints = OS.PANGO_PIXELS(ascent); + heightInPoints = OS.PANGO_PIXELS(ascent + descent); OS.pango_font_metrics_unref(metrics); } else { PangoRectangle rect = new PangoRectangle(); metricsAdapter.pango_layout_line_get_extents(OS.pango_layout_get_line(layout, lineIndex), null, rect); - ascentInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(-rect.y)); - heightInPoints = DPIUtil.autoScaleDown(getDevice(), OS.PANGO_PIXELS(rect.height)); + ascentInPoints = OS.PANGO_PIXELS(-rect.y); + heightInPoints = OS.PANGO_PIXELS(rect.height); } heightInPoints = Math.max(this.ascentInPoints + this.descentInPoints, heightInPoints); ascentInPoints = Math.max(this.ascentInPoints, ascentInPoints); @@ -1320,7 +1314,7 @@ public int[] getLineOffsets() { */ public Point getLocation(int offset, boolean trailing) { checkLayout(); - return DPIUtil.autoScaleDown(getDevice(), getLocationInPixels(offset, trailing)); + return getLocationInPixels(offset, trailing); } Point getLocationInPixels(int offset, boolean trailing) { @@ -1459,7 +1453,7 @@ int _getOffset (int offset, int movement, boolean forward) { */ public int getOffset(Point point, int[] trailing) { checkLayout(); - return getOffsetInPixels(DPIUtil.autoScaleUp(getDevice(), point), trailing); + return getOffsetInPixels(point, trailing); } int getOffsetInPixels(Point point, int[] trailing) { @@ -1685,7 +1679,7 @@ String getSegmentsText() { */ public int getSpacing () { checkLayout(); - return DPIUtil.autoScaleDown(getDevice(), getSpacingInPixels()); + return getSpacingInPixels(); } int getSpacingInPixels () { @@ -1717,7 +1711,7 @@ private int getScaledVerticalIndent() { if (verticalIndentInPoints == 0) { return verticalIndentInPoints; } - return DPIUtil.autoScaleUp(getDevice(), verticalIndentInPoints); + return verticalIndentInPoints; } /** @@ -1787,7 +1781,7 @@ public TextStyle[] getStyles () { */ public int[] getTabs() { checkLayout(); - return DPIUtil.autoScaleDown (getDevice(), getTabsInPixels ()); + return getTabsInPixels (); } int[] getTabsInPixels () { @@ -1834,7 +1828,7 @@ public int getTextDirection () { */ public int getWidth () { checkLayout (); - return DPIUtil.autoScaleDown(getDevice(), getWidthInPixels()); + return getWidthInPixels(); } int getWidthInPixels () { @@ -1854,7 +1848,7 @@ int getWidthInPixels () { */ public int getWrapIndent () { checkLayout (); - return DPIUtil.autoScaleDown(getDevice(), getWrapIndentInPixels()); + return getWrapIndentInPixels(); } int getWrapIndentInPixels () { return wrapIndent; @@ -2042,7 +2036,7 @@ public void setFont (Font font) { */ public void setIndent (int indent) { checkLayout (); - setIndentInPixels(DPIUtil.autoScaleUp(getDevice(), indent)); + setIndentInPixels(indent); } void setIndentInPixels (int indent) { @@ -2115,7 +2109,7 @@ public void setOrientation(int orientation) { public void setSpacing (int spacing) { checkLayout(); if (spacing < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - setSpacingInPixels(DPIUtil.autoScaleUp(getDevice(), spacing)); + setSpacingInPixels(spacing); } void setSpacingInPixels (int spacing) { @@ -2338,7 +2332,7 @@ public void setStyle (TextStyle style, int start, int end) { public void setTabs(int[] tabs) { checkLayout(); if (this.tabs == null && tabs == null) return; - setTabsInPixels (DPIUtil.autoScaleUp (getDevice(), tabs)); + setTabsInPixels (tabs); } void setTabsInPixels (int[] tabs) { @@ -2434,7 +2428,7 @@ public void setTextDirection (int textDirection) { public void setWidth (int width) { checkLayout (); if (width < -1 || width == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - setWidthInPixels(DPIUtil.autoScaleUp(getDevice(), width)); + setWidthInPixels(width); } void setWidthInPixels (int width) { @@ -2472,7 +2466,7 @@ void setWidth () { public void setWrapIndent (int wrapIndent) { checkLayout(); if (wrapIndent < 0) return; - setWrapIndentInPixels(DPIUtil.autoScaleUp(getDevice(), wrapIndent)); + setWrapIndentInPixels(wrapIndent); } void setWrapIndentInPixels (int wrapIndent) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/GtkDPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/GtkDPIUtil.java new file mode 100644 index 0000000000..663e119abc --- /dev/null +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/GtkDPIUtil.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2022 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * 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; + +import org.eclipse.swt.graphics.*; + +/** + * This class hold common constants and utility functions w.r.t. to SWT high DPI + * functionality. + *

+ * The {@code autoScaleUp(..)} methods convert from API coordinates (in + * SWT points) to internal high DPI coordinates (in pixels) that interface with + * native widgets. + *

+ *

+ * The {@code autoScaleDown(..)} convert from high DPI pixels to API coordinates + * (in SWT points). + *

+ * + * @since 3.105 + */ +public class GtkDPIUtil { + + /** + * Auto-scale up ImageData to device zoom that is at 100%. + */ + public static ImageData autoScaleUp (Device device, final ImageData imageData) { + return autoScaleImageData(device, imageData, 100); + } + + /** + * Auto-scale ImageData to device zoom that are at given zoom factor. + */ + public static ImageData autoScaleImageData (Device device, final ImageData imageData, int imageDataZoomFactor) { + if (DPIUtil.deviceZoom == imageDataZoomFactor || imageData == null || (device != null && !device.isAutoScalable())) return imageData; + float scaleFactor = (float) DPIUtil.deviceZoom / imageDataZoomFactor; + return DPIUtil.autoScaleImageData(device, imageData, scaleFactor); + } + +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java index 6199377681..cf3ab4b48a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java @@ -2807,10 +2807,10 @@ void fillArcInPixels (int x, int y, int width, int height, int startAngle, int a */ public void fillGradientRectangle (int x, int y, int width, int height, boolean vertical) { Rectangle rect = DPIUtil.scaleUp(drawable, new Rectangle(x, y, width, height), getZoom()); - fillGradientRectangleInPixels(rect.x, rect.y, rect.width, rect.height, vertical); + fillGradientRectangleInPixels(rect.x, rect.y, rect.width, rect.height, vertical, getZoom()); } -void fillGradientRectangleInPixels(int x, int y, int width, int height, boolean vertical) { +void fillGradientRectangleInPixels(int x, int y, int width, int height, boolean vertical, int zoom) { checkNonDisposed(); if (width == 0 || height == 0) return; @@ -2901,7 +2901,7 @@ void fillGradientRectangleInPixels(int x, int y, int width, int height, boolean final int bitResolution = (depth >= 24) ? 8 : (depth >= 15) ? 5 : 0; ImageData.fillGradientRectangle(this, data.device, x, y, width, height, vertical, fromRGB, toRGB, - bitResolution, bitResolution, bitResolution); + bitResolution, bitResolution, bitResolution, zoom); } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index 511fcdfec4..77523bf7be 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -1592,7 +1592,7 @@ public Menu getMenuBar () { @Override public Rectangle getBounds() { checkDevice (); - return DPIUtil.autoScaleDown(getBoundsInPixels()); + return DPIUtil.scaleDown(getBoundsInPixels(), DPIUtil.getDeviceZoom()); } Rectangle getBoundsInPixels () { @@ -1665,7 +1665,7 @@ int getClickCount (int type, int button, long hwnd, long lParam) { @Override public Rectangle getClientArea () { checkDevice (); - return DPIUtil.autoScaleDown(getClientAreaInPixels()); + return DPIUtil.scaleDown(getClientAreaInPixels(), DPIUtil.getDeviceZoom()); } Rectangle getClientAreaInPixels () { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java index e925359299..4e23cbb4e6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java @@ -72,7 +72,7 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig @Override public Rectangle mapMonitorBounds(Rectangle rect, int zoom) { - return DPIUtil.autoScaleDown(rect); + return DPIUtil.scaleDown(rect, zoom); } @Override @@ -97,13 +97,15 @@ public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { @Override public Point getCursorLocation() { + int zoom = DPIUtil.getDeviceZoom(); Point cursorLocationInPixels = display.getCursorLocationInPixels(); - return DPIUtil.autoScaleDown(cursorLocationInPixels); + return DPIUtil.scaleDown(cursorLocationInPixels, zoom); } @Override public void setCursorLocation(int x, int y) { - display.setCursorLocationInPixels(DPIUtil.autoScaleUp(x), DPIUtil.autoScaleUp(y)); + int zoom = DPIUtil.getDeviceZoom(); + display.setCursorLocationInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)); } @Override diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java index d501c4d0a6..802f3ef794 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java @@ -14,7 +14,6 @@ package org.eclipse.swt.tests.junit; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; @@ -50,97 +49,16 @@ public void tearDown() { DPIUtil.setDeviceZoom(deviceZoom); } - @Test - public void scaleDownFloatArray() { - float[] valueAt200 = new float[] { 2, 3, 4 }; - float[] valueAt150 = new float[] { 1.5f, 2.25f, 3 }; - float[] valueAt100 = new float[] { 1, 1.5f, 2 }; - - float[] scaledValue = DPIUtil.autoScaleDown(valueAt200); - assertArrayEquals(valueAt100, scaledValue, .001f, String.format("Scaling down float array from device zoom (%d) to 100 failed", - ZOOM_200)); - scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200); - assertArrayEquals(valueAt100, scaledValue, .001f, String.format("Scaling down float array from device zoom (%d) to 100 with device failed", - ZOOM_200)); - - scaledValue = DPIUtil.scaleDown(valueAt200, 200); - assertArrayEquals(valueAt100, scaledValue, .001f, "Scaling down float array from 200 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt200, 200); - assertArrayEquals(valueAt100, scaledValue, .001f, "Scaling down float array from 200 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt150, 150); - assertArrayEquals(valueAt100, scaledValue, .001f, "Scaling down float array from 150 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt150, 150); - assertArrayEquals(valueAt100, scaledValue, .001f, "Scaling down float array from 150 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling down float array without zoom change failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling down float array without zoom change with device failed"); - } - - @Test - public void scaleDownInteger() { - int valueAt200 = 10; - int valueAt150 = 7; - int valueAt100 = 5; - - int scaledValue = DPIUtil.autoScaleDown(valueAt200); - assertEquals( - valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200); - assertEquals( - valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 with device failed", ZOOM_200)); - - scaledValue = DPIUtil.scaleDown(valueAt200, 200); - assertEquals(valueAt100, scaledValue, "Scaling down integer from 200 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt200, 200); - assertEquals(valueAt100, scaledValue, "Scaling down integer from 200 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt150, 150); - assertEquals(valueAt100, scaledValue, "Scaling down integer from 150 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt150, 150); - assertEquals(valueAt100, scaledValue, "Scaling down integer from 150 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling down integer without zoom change failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling down integer without zoom change with device failed"); - } - - @Test - public void scaleDownFloat() { - float valueAt200 = 10f; - float valueAt150 = 7.5f; - float valueAt100 = 5f; - - float scaledValue = DPIUtil.autoScaleDown(valueAt200); - assertEquals(valueAt100, scaledValue, .001f, - String.format("Scaling down float from device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200); - assertEquals(valueAt100, scaledValue, .001f, String - .format("Scaling down float from device zoom (%d) to 100 with device failed", ZOOM_200)); - - scaledValue = DPIUtil.scaleDown(valueAt200, 200); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float from 200 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt200, 200); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float from 200 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt150, 150); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float from 150 failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt150, 150); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float from 150 with device failed"); - scaledValue = DPIUtil.scaleDown(valueAt100, 100); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float without zoom change failed"); - scaledValue = DPIUtil.scaleDown((Device) null, valueAt100, 100); - assertEquals(valueAt100, scaledValue, .001f, "Scaling down float without zoom change with device failed"); - } - @Test public void scaleDownPoint() { Point valueAt200 = new Point(10, 14); Point valueAt150 = new Point(7, 10); Point valueAt100 = new Point(5, 7); - Point scaledValue = DPIUtil.autoScaleDown(valueAt200); + Point scaledValue = DPIUtil.scaleDown(valueAt200, 100); assertEquals(valueAt100, scaledValue, String.format("Scaling down Point from device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200); + scaledValue = DPIUtil.scaleDown((Device) null, valueAt200, 100); assertEquals(valueAt100, scaledValue, String .format("Scaling down Point from device zoom (%d) to 100 with device failed", ZOOM_200)); @@ -164,10 +82,10 @@ public void scaleDownRectangle() { Rectangle valueAt150 = new Rectangle(75, 113, 7, 10); Rectangle valueAt100 = new Rectangle(50, 75, 5, 7); - Rectangle scaledValue = DPIUtil.autoScaleDown(valueAt200); + Rectangle scaledValue = DPIUtil.scaleDown(valueAt200, 100); assertEquals(valueAt100, scaledValue, String.format("Scaling down Rectangle from device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200); + scaledValue = DPIUtil.scaleDown((Device) null, valueAt200, 100); assertEquals(valueAt100, scaledValue, String.format( "Scaling down Rectangle from device zoom (%d) to 100 with device failed", ZOOM_200)); @@ -185,43 +103,16 @@ public void scaleDownRectangle() { assertSame(valueAt100, scaledValue, "Scaling down Rectangle without zoom change with device failed"); } - @Test - public void scaleUpIntArray() { - int[] valueAt200 = new int[] { 10, 12, 14 }; - int[] valueAt150 = new int[] { 8, 9, 11 }; - int[] valueAt100 = new int[] { 5, 6, 7 }; - - int[] scaledValue = DPIUtil.autoScaleUp(valueAt100); - assertArrayEquals(valueAt200, scaledValue, - String.format("Scaling up int array to device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100); - assertArrayEquals(valueAt200, scaledValue, String - .format("Scaling up int array to device zoom (%d) to 100 with device failed", ZOOM_200)); - - scaledValue = DPIUtil.scaleUp(valueAt100, 200); - assertArrayEquals(valueAt200, scaledValue, "Scaling up int array to 200 failed"); - scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 200); - assertArrayEquals(valueAt200, scaledValue, "Scaling up int array to 200 with device failed"); - scaledValue = DPIUtil.scaleUp(valueAt100, 150); - assertArrayEquals(valueAt150, scaledValue, "Scaling up int array to 150 failed"); - scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 150); - assertArrayEquals(valueAt150, scaledValue, "Scaling up int array to 150 with device failed"); - scaledValue = DPIUtil.scaleUp(valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling up int array without zoom change failed"); - scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); - assertSame(valueAt100, scaledValue, "Scaling up int array without zoom change with device failed"); - } - @Test public void scaleUpInteger() { int valueAt200 = 10; int valueAt150 = 8; int valueAt100 = 5; - int scaledValue = DPIUtil.autoScaleUp(valueAt100); + int scaledValue = DPIUtil.scaleUp(valueAt100, 100); assertEquals(valueAt200, scaledValue, String.format("Scaling up integer to device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100); + scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertEquals(valueAt200, scaledValue, String .format("Scaling up integer to device zoom (%d) to 100 with device failed", ZOOM_200)); @@ -245,10 +136,10 @@ public void scaleUpFloat() { float valueAt150 = 7.5f; float valueAt100 = 5; - float scaledValue = DPIUtil.autoScaleUp(valueAt100); + float scaledValue = DPIUtil.scaleUp(valueAt100, 100); assertEquals(valueAt200, scaledValue, 0.001f, String.format("Scaling up integer to device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100); + scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertEquals(valueAt200, scaledValue, 0.001f, String .format("Scaling up integer to device zoom (%d) to 100 with device failed", ZOOM_200)); @@ -272,10 +163,10 @@ public void scaleUpPoint() { Point valueAt150 = new Point(8, 11); Point valueAt100 = new Point(5, 7); - Point scaledValue = DPIUtil.autoScaleUp(valueAt100); + Point scaledValue = DPIUtil.scaleUp(valueAt100, 100); assertEquals(valueAt200, scaledValue, String.format("Scaling up Point to device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100); + scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertEquals(valueAt200, scaledValue, String .format("Scaling up Point to device zoom (%d) to 100 with device failed", ZOOM_200)); @@ -299,10 +190,10 @@ public void scaleUpRectangle() { Rectangle valueAt150 = new Rectangle(75, 113, 8, 10); Rectangle valueAt100 = new Rectangle(50, 75, 5, 7); - Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100); + Rectangle scaledValue = DPIUtil.scaleUp(valueAt100, 100); assertEquals(valueAt200, scaledValue, String.format("Scaling up Rectangle to device zoom (%d) to 100 failed", ZOOM_200)); - scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100); + scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertEquals(valueAt200, scaledValue, String .format("Scaling up Rectangle to device zoom (%d) to 100 with device failed", ZOOM_200)); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java index eceef1c959..dc7cb2f84e 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java @@ -598,7 +598,7 @@ public void test_getBoundsInPixels() { Rectangle bounds = image.getBounds(); image.dispose(); assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds); - assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels); + assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.scaleUp(initialBounds, 100), boundsInPixels); // create icon image ImageData imageData = new ImageData(initialBounds.width, initialBounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)})); @@ -607,21 +607,21 @@ public void test_getBoundsInPixels() { bounds = image.getBounds(); image.dispose(); assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds); - assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels); + assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.scaleUp(initialBounds, 100), boundsInPixels); // create image with FileNameProvider image = new Image(display, imageFileNameProvider); boundsInPixels = image.getBoundsInPixels(); bounds = image.getBounds(); image.dispose(); - assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(bounds), boundsInPixels); + assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.scaleUp(bounds, 100), boundsInPixels); // create image with ImageDataProvider image = new Image(display, imageDataProvider); boundsInPixels = image.getBoundsInPixels(); bounds = image.getBounds(); image.dispose(); - assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.autoScaleUp(bounds), boundsInPixels); + assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values.", DPIUtil.scaleUp(bounds, 100), boundsInPixels); // create image with ImageGcDrawer image = new Image(display, imageGcDrawer, initialBounds.width, initialBounds.height); @@ -629,7 +629,7 @@ public void test_getBoundsInPixels() { bounds = image.getBounds(); image.dispose(); assertEquals("Image.getBounds method doesn't return original bounds.", initialBounds, bounds); - assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values for ImageGcDrawer.", DPIUtil.autoScaleUp(initialBounds), boundsInPixels); + assertEquals("Image.getBoundsInPixels method doesn't return bounds in Pixel values for ImageGcDrawer.", DPIUtil.scaleUp(initialBounds, 100), boundsInPixels); } @SuppressWarnings("deprecation") @@ -647,7 +647,7 @@ public void test_getImageDataCurrentZoom() { ImageData imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom(); image.dispose(); Rectangle boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height); - assertEquals(":a: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds)); + assertEquals(":a: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.scaleUp(bounds, 100)); // create icon image and compare size of imageData ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)})); @@ -655,7 +655,7 @@ public void test_getImageDataCurrentZoom() { imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom(); image.dispose(); boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height); - assertEquals(":b: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds)); + assertEquals(":b: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.scaleUp(bounds, 100)); // create image with FileNameProvider image = new Image(display, imageFileNameProvider); @@ -663,7 +663,7 @@ public void test_getImageDataCurrentZoom() { boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height); bounds = image.getBounds(); image.dispose(); - assertEquals(":c: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds)); + assertEquals(":c: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.scaleUp(bounds, 100)); // create image with ImageDataProvider image = new Image(display, imageDataProvider); @@ -671,7 +671,7 @@ public void test_getImageDataCurrentZoom() { boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height); bounds = image.getBounds(); image.dispose(); - assertEquals(":d: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds)); + assertEquals(":d: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.scaleUp(bounds, 100)); } @Test