From b0b70ab476d2e26a843ade48427736bdfa887d5b Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Mon, 11 Aug 2025 13:45:45 +0200 Subject: [PATCH] [win32] Foward native zoom to draw bitmap in GC This commit adapts the logic when a bitmap or icon is drawn with the GC in the windows implementation. As of now, the autoscale zoom was used to identify the image handle to use. If the image is itself drawn with a ImageGCDrawer, information about the original native zoom was lost, which led to inconsistencies in fonts depending on the auto scale mode. This commit fowards the native zoom in this case to enable the Image to initialize the GC accordingly. fixes https://github.com/eclipse-platform/eclipse.platform.swt/issues/2385 fixes https://github.com/eclipse-platform/eclipse.platform.swt/issues/2311 --- .../win32/org/eclipse/swt/graphics/GC.java | 4 +-- .../org/eclipse/swt/graphics/GCData.java | 2 ++ .../win32/org/eclipse/swt/graphics/Image.java | 29 +++++++++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) 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 1e48a77a51d..e2d8c831cc1 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 @@ -1253,7 +1253,7 @@ private void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int src } return; } - long imageHandle = Image.win32_getHandle(srcImage, imageZoom); + long imageHandle = srcImage.getHandle(imageZoom, data.nativeZoom); switch (srcImage.type) { case SWT.BITMAP: drawBitmap(srcImage, imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple); @@ -4384,7 +4384,7 @@ private void init(Drawable drawable, GCData data, long hDC) { } Image image = data.image; if (image != null) { - data.hNullBitmap = OS.SelectObject(hDC, Image.win32_getHandle(image, data.nativeZoom)); + data.hNullBitmap = OS.SelectObject(hDC, image.getHandle(data.imageZoom, data.nativeZoom)); image.memGC = this; } int layout = data.layout; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GCData.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GCData.java index 0f313b30559..9e22aab2a42 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GCData.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GCData.java @@ -48,6 +48,7 @@ public final class GCData { public float lineMiterLimit = 10; public int alpha = 0xFF; public int nativeZoom; + int imageZoom; public Image image; public PAINTSTRUCT ps; @@ -67,6 +68,7 @@ void copyTo(GCData originalData) { originalData.font = font; originalData.nativeZoom = nativeZoom; originalData.image = image; + originalData.imageZoom = imageZoom; originalData.ps = ps; originalData.layout = layout; originalData.hwnd = hwnd; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index ccec341525b..34cbb56f840 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -809,10 +809,15 @@ private ImageHandle getImageMetadata(ZoomContext zoomContext) { * @noreference This method is not intended to be referenced by clients. */ public static long win32_getHandle (Image image, int zoom) { - if (image.isDisposed()) { + return image.getHandle(zoom, zoom); +} + +long getHandle (int targetZoom, int nativeZoom) { + if (isDisposed()) { return 0L; } - return image.getImageMetadata(new ZoomContext(zoom)).handle; + ZoomContext zoomContext = imageProvider.getFittingZoomContext(targetZoom, nativeZoom); + return getImageMetadata(zoomContext).handle; } /** @@ -1752,6 +1757,7 @@ private long configureGC(GCData data, ZoomContext zoomContext) { } data.device = device; data.nativeZoom = zoomContext.nativeZoom(); + data.imageZoom = zoomContext.targetZoom(); data.image = this; data.font = SWTFontProvider.getSystemFont(device, zoomContext.nativeZoom()); } @@ -1913,6 +1919,10 @@ private abstract class AbstractImageProviderWrapper { protected abstract Rectangle getBounds(int zoom); + protected ZoomContext getFittingZoomContext(int targetZoom, int nativeZoom) { + return new ZoomContext(targetZoom); + } + protected long configureGCData(GCData data) { return configureGC(data, new ZoomContext(100)); } @@ -2138,6 +2148,14 @@ private class PlainImageProviderWrapper extends AbstractImageProviderWrapper { type = SWT.BITMAP; } + @Override + protected ZoomContext getFittingZoomContext(int targetZoom, int nativeZoom) { + if (memGC != null) { + return new ZoomContext(targetZoom, nativeZoom); + } + return super.getFittingZoomContext(targetZoom, nativeZoom); + } + @Override public Collection getPreservedZoomLevels() { return Collections.singleton(baseZoom); @@ -2145,7 +2163,7 @@ public Collection getPreservedZoomLevels() { @Override protected long configureGCData(GCData data) { - return configureGC(data, new ZoomContext(DPIUtil.getDeviceZoom())); + return configureGC(data, new ZoomContext(DPIUtil.getDeviceZoom(), DPIUtil.getNativeDeviceZoom())); } @Override @@ -2577,6 +2595,11 @@ private class ImageGcDrawerWrapper extends DynamicImageProviderWrapper { this.height = height; } + @Override + protected ZoomContext getFittingZoomContext(int targetZoom, int nativeZoom) { + return new ZoomContext(targetZoom, nativeZoom); + } + @Override protected Rectangle getBounds(int zoom) { Rectangle rectangle = new Rectangle(0, 0, width, height);