Skip to content

Commit 6425e28

Browse files
committed
[GTK] Consider transformation scale in GC.drawImage(image, x, y) #2919
With e97143c, support was added on Windows to take the `Transform` scaling into consideration when calling `drawImage(image,x,y)`. Especially when used in combination with an SVG-based image, this leads to better results as the best-fitting image is used for painting, rather than relying on interpolation. This change follows a similar logic to what has been done for Windows; The call to `drawImage(image,x,y)` is delegated to `drawImage(x,y,w,h)` when a `Transform` has been set. Within this method, the width and height of the image are used as size after being multiplied by the transformation scale. Contributes to #2919
1 parent f1bf0d3 commit 6425e28

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/GC.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ public GC(Drawable drawable, int style) {
190190
init();
191191
}
192192

193+
private float calculateTransformationScale() {
194+
if (currentTransform == null) {
195+
return 1.0f;
196+
}
197+
// this calculates the effective length in x and y
198+
// direction without being affected by the rotation
199+
// of the transformation
200+
float scaleWidth = (float) Math.hypot(currentTransform[0], currentTransform[2]);
201+
float scaleHeight = (float) Math.hypot(currentTransform[1], currentTransform[3]);
202+
return Math.max(scaleWidth, scaleHeight);
203+
}
204+
193205
/**
194206
* Ensure that the style specified is either LEFT_TO_RIGHT <b>or</b> RIGHT_TO_LEFT.
195207
*
@@ -791,7 +803,12 @@ public void drawImage(Image image, int x, int y) {
791803
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
792804
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
793805
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
794-
drawImage(image, 0, 0, -1, -1, x, y, -1, -1, true);
806+
if (currentTransform != null && !isIdentity(currentTransform)) {
807+
Rectangle imageBounds = image.getBounds();
808+
drawImage(image, x, y, imageBounds.width, imageBounds.height);
809+
} else {
810+
drawImage(image, 0, 0, -1, -1, x, y, -1, -1, true);
811+
}
795812
}
796813

797814
/**
@@ -886,7 +903,10 @@ public void drawImage(Image image, int destX, int destY, int destWidth, int dest
886903
if (image.isDisposed()) {
887904
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
888905
}
889-
image.executeOnImageAtSize(imageAtSize -> drawImage(imageAtSize, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false), destWidth, destHeight);
906+
float transformationScale = calculateTransformationScale();
907+
int scaledWidth = Math.round(destWidth * transformationScale);
908+
int scaledHeight = Math.round(destHeight * transformationScale);
909+
image.executeOnImageAtSize(imageAtSize -> drawImage(imageAtSize, 0, 0, 0, 0, destX, destY, destWidth, destHeight, false), scaledWidth, scaledHeight);
890910
}
891911

892912
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.junit.jupiter.api.AfterEach;
6060
import org.junit.jupiter.api.BeforeEach;
6161
import org.junit.jupiter.api.Test;
62-
import org.junit.jupiter.api.condition.EnabledOnOs;
62+
import org.junit.jupiter.api.condition.DisabledOnOs;
6363
import org.junit.jupiter.api.condition.OS;
6464
import org.junit.jupiter.params.ParameterizedTest;
6565
import org.junit.jupiter.params.provider.ValueSource;
@@ -402,7 +402,7 @@ public void test_drawImageLorg_eclipse_swt_graphics_ImageIIII() {
402402
}
403403

404404
@Test
405-
@EnabledOnOs(OS.WINDOWS)
405+
@DisabledOnOs(OS.MAC)
406406
public void test_drawImageLorg_eclipse_swt_graphics_ImageIIII_withTransform() throws IOException {
407407
Image image = null;
408408
try (InputStream is = getClass().getResourceAsStream("collapseall.svg")) {
@@ -431,6 +431,7 @@ private ImageData getImageDataFromGC(GC gc, int x, int y, int width, int height)
431431
return resultImageData;
432432
}
433433

434+
@Test
434435
public void test_drawImageLorg_eclipse_swt_graphics_ImageIIII_withTransform_zeroTargetSize() throws IOException {
435436
Image image = null;
436437
try (InputStream is = getClass().getResourceAsStream("collapseall.svg")) {

0 commit comments

Comments
 (0)