From 0ebc80b230ff42725de9470bf60051345ad437b4 Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Tue, 11 Feb 2025 12:31:20 +0100 Subject: [PATCH 1/2] [win32] fix to use zoom dpi for fonts via display With commit cd8bb13 we adapted the calcuation how to scale up fonts to fix the calculation for fonts used by the Printer. This introduced an issue if monitor-specific scaling is active and the primary monitor zoom is changed, because two different calculation were mixed now. This commit fixes this inconsistency. --- .../win32/org/eclipse/swt/graphics/Device.java | 15 ++++++++++----- .../win32/org/eclipse/swt/graphics/Font.java | 9 +-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java index 429bbc71473..05f4157b6b2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java @@ -260,9 +260,9 @@ void checkGDIP() { protected void create (DeviceData data) { } -int computePixels(float height) { +int computePixels(float height, int zoom) { long hDC = internal_new_GC (null); - int pixels = -(int)(0.5f + (height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72f)); + int pixels = -(int)(0.5f + (height / calculateFontConversionFactor(hDC, zoom))); internal_dispose_GC (hDC, null); return pixels; } @@ -271,9 +271,7 @@ float computePoints(LOGFONT logFont, long hFont) { return computePoints(logFont, hFont, SWT.DEFAULT); } -float computePoints(LOGFONT logFont, long hFont, int zoom) { - long hDC = internal_new_GC (null); - +private float calculateFontConversionFactor(long hDC, int zoom) { float conversionFactor = 72f; if (isAutoScalable() && zoom != SWT.DEFAULT) { // For auto scalable devices we need to use a dynamic @@ -282,6 +280,13 @@ float computePoints(LOGFONT logFont, long hFont, int zoom) { } else { conversionFactor /= OS.GetDeviceCaps(hDC, OS.LOGPIXELSY); } + return conversionFactor; +} + +float computePoints(LOGFONT logFont, long hFont, int zoom) { + long hDC = internal_new_GC (null); + + float conversionFactor = calculateFontConversionFactor(hDC, zoom); int pixels = 0; if (logFont.lfHeight > 0) { /* diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java index 12cdecdf77c..cd4056de17a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java @@ -236,14 +236,7 @@ void init (FontData fd) { if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); LOGFONT logFont = fd.data; int lfHeight = logFont.lfHeight; - logFont.lfHeight = device.computePixels(fd.height); - - int primaryZoom = extractZoom(device); - if (zoom != primaryZoom) { - float scaleFactor = 1f * zoom / primaryZoom; - logFont.lfHeight *= scaleFactor; - } - + logFont.lfHeight = device.computePixels(fd.height, zoom); handle = OS.CreateFontIndirect(logFont); logFont.lfHeight = lfHeight; if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); From 4f96f762a0522a76dc23e3bcbfc4bed39a9d84f2 Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Tue, 11 Feb 2025 14:10:26 +0100 Subject: [PATCH 2/2] [win32] fix for rounding issues when scaling fonts This commit fixes the calculation of the height when scaling a font for a different zoom when monitor-specific scaling is active --- .../win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java index 1625996f465..06c7f49e106 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java @@ -54,8 +54,7 @@ private Font getScaledFont(int targetZoom) { private Font scaleFont(int zoom) { FontData fontData = baseFont.getFontData()[0]; int baseZoom = computeZoom(fontData); - int zoomScaleFactor = Math.round(1.0f * zoom / baseZoom); - fontData.data.lfHeight *= zoomScaleFactor; + fontData.data.lfHeight = Math.round(1.0f * fontData.data.lfHeight * zoom / baseZoom); Font scaledFont = Font.win32_new(device, fontData, zoom); addScaledFont(zoom, scaledFont); return scaledFont;