Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
package org.eclipse.swt.widgets;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;

/**
* Automated Tests for class org.eclipse.swt.widgets.Control for Windows
Expand Down Expand Up @@ -107,6 +110,28 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
}

@ParameterizedTest
@CsvSource({ "0.5, 100, true", "1.0, 200, true", "2.0, 200, true", "2.0, quarter, true", "0.5, 100, false",
"1.0, 200, false", "2.0, 200, false", "2.0, quarter, false", })
public void testAutoScaleImageData(float scaleFactor, String autoScale, boolean monitorSpecificScaling) {
DPIUtil.setMonitorSpecificScaling(monitorSpecificScaling);
DPIUtil.runWithAutoScaleValue(autoScale, () -> {
Display display = new Display();
try {
ImageData imageData = new ImageData(100, 100, 1, new PaletteData(new RGB(0, 0, 0)));
int width = imageData.width;
int height = imageData.height;
int scaledWidth = Math.round(width * scaleFactor);
int scaledHeight = Math.round(height * scaleFactor);
ImageData scaledImageData = DPIUtil.autoScaleImageData(display, imageData, scaleFactor);
assertEquals(scaledWidth, scaledImageData.width);
assertEquals(scaledHeight, scaledImageData.height);
} finally {
display.dispose();
}
});
}

record FontComparison(int originalFontHeight, int currentFontHeight) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1811,22 +1811,24 @@ public String toString () {
* API for Image. It is marked public only so that it
* can be shared within the packages provided by SWT.
*
* Draws a scaled image using the GC by another image.
* Draws a scaled image using the GC for a given imageData.
*
* @param gc the GC to draw on the resulting image
* @param original the image which is supposed to be scaled and drawn on the resulting image
* @param imageData the imageData which is used to draw the scaled Image
* @param width the width of the original image
* @param height the height of the original image
* @param scaleFactor the factor with which the image is supposed to be scaled
*
* @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, CocoaDPIUtil.pixelToPoint (width), CocoaDPIUtil.pixelToPoint (height),
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 0, 0, CocoaDPIUtil.pixelToPoint (width), CocoaDPIUtil.pixelToPoint (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 (CocoaDPIUtil.pixelToPoint (width * scaleFactor)), Math.round (CocoaDPIUtil.pixelToPoint (height * scaleFactor)));
imageToDraw.dispose();
}

private final class CocoaDPIUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ public static ImageData autoScaleImageData (Device device, final ImageData image
int defaultZoomLevel = 100;
boolean useSmoothScaling = isSmoothScalingEnabled() && imageData.getTransparencyType() != SWT.TRANSPARENCY_MASK;
if (useSmoothScaling) {
Image original = new Image(device, (ImageDataProvider) zoom -> (zoom == defaultZoomLevel) ? imageData : null);
ImageGcDrawer drawer = new ImageGcDrawer() {
@Override
public void drawOn(GC gc, int imageWidth, int imageHeight) {
gc.setAntialias (SWT.ON);
Image.drawScaled(gc, original, width, height, scaleFactor);
Image.drawScaled(gc, imageData, width, height, scaleFactor);
};

@Override
Expand All @@ -168,7 +167,6 @@ public int getGcStyle() {
};
Image resultImage = new Image (device, drawer, scaledWidth, scaledHeight);
ImageData result = resultImage.getImageData (defaultZoomLevel);
original.dispose ();
resultImage.dispose ();
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,22 +1572,24 @@ public String toString () {
* API for Image. It is marked public only so that it
* can be shared within the packages provided by SWT.
*
* Draws a scaled image using the GC by another image.
* Draws a scaled image using the GC for a given imageData.
*
* @param gc the GC to draw on the resulting image
* @param original the image which is supposed to be scaled and drawn on the resulting image
* @param imageData the imageData which is used to draw the scaled Image
* @param width the width of the original image
* @param height the height of the original image
* @param scaleFactor the factor with which the image is supposed to be scaled
*
* @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, width, height,
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 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 (width * scaleFactor), Math.round (height * scaleFactor));
imageToDraw.dispose();
}

private final class GtkDPIUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,19 +839,24 @@ public static long win32_getHandle (Image image, int zoom) {
* API for Image. It is marked public only so that it
* can be shared within the packages provided by SWT.
*
* Draws a scaled image using the GC by another image.
* Draws a scaled image using the GC for a given imageData.
*
* @param gc the GC to draw on the resulting image
* @param original the image which is supposed to be scaled and drawn on the resulting image
* @param imageData the imageData which is used to draw the scaled Image
* @param width the width of the original image
* @param height the height of the original image
* @param scaleFactor the factor with which the image is supposed to be scaled
*
* @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, width, height,
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
boolean originalStrictChecks = Device.strictChecks;
Device.strictChecks = false;
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 0, 0, width, height,
0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor), false);
Device.strictChecks = originalStrictChecks;
imageToDraw.dispose();
}

long [] createGdipImage(Integer zoom) {
Expand Down
Loading