Skip to content

Commit 1799f06

Browse files
committed
Modify gc.data.nativeDeviceZoom to use autoscaled zoom when
swt.autoScale is fixed This change ensures that images and fonts share the same zoom level when a fixed autoscale value is used. Previously, when different GCs were created for images and widgets, their native zoom levels could differ, causing inconsistencies. For example, in LineNumberRuler, one GC is created from an image and another from a widget for text measurement. The differing native zooms led to incorrect text width calculations when applied across GCs. This update aligns nativeDeviceZoom to the autoscaled zoom value Fixes #2311
1 parent 74feaa5 commit 1799f06

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static Optional<AutoScaleMethod> forString(String s) {
5555
}
5656

5757
}
58+
private static boolean useAutoScaledFontZoom = false;
5859
private static final AutoScaleMethod AUTO_SCALE_METHOD_SETTING;
5960
private static AutoScaleMethod autoScaleMethod;
6061

@@ -108,7 +109,7 @@ public static Optional<AutoScaleMethod> forString(String s) {
108109
*/
109110
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
110111
static {
111-
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
112+
setAutoScaleValue(System.getProperty (SWT_AUTOSCALE));
112113

113114
String value = System.getProperty (SWT_AUTOSCALE_METHOD);
114115
AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO);
@@ -347,6 +348,29 @@ public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
347348
return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleValue);
348349
}
349350

351+
public static int getFontZoomForAutoscaleProperty(int nativeDeviceZoom) {
352+
if (useAutoScaledFontZoom) {
353+
return getZoomForAutoscaleProperty(nativeDeviceZoom);
354+
} else {
355+
return nativeDeviceZoom;
356+
}
357+
}
358+
359+
private static void setAutoScaleValue(String value) {
360+
autoScaleValue = value;
361+
useAutoScaledFontZoom = isIntegerAutoScale(value);
362+
}
363+
364+
private static boolean isIntegerAutoScale(String value) {
365+
if (value == null) return false;
366+
try {
367+
Integer.parseInt(value);
368+
return true;
369+
} catch (NumberFormatException e) {
370+
return false;
371+
}
372+
}
373+
350374
private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String autoScaleValue) {
351375
int zoom = 0;
352376
if (autoScaleValue != null) {
@@ -378,12 +402,12 @@ private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String aut
378402

379403
public static void runWithAutoScaleValue(String autoScaleValue, Runnable runnable) {
380404
String initialAutoScaleValue = DPIUtil.autoScaleValue;
381-
DPIUtil.autoScaleValue = autoScaleValue;
405+
setAutoScaleValue(autoScaleValue);
382406
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
383407
try {
384408
runnable.run();
385409
} finally {
386-
DPIUtil.autoScaleValue = initialAutoScaleValue;
410+
setAutoScaleValue(initialAutoScaleValue);
387411
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
388412
}
389413
}
@@ -400,7 +424,7 @@ public static boolean isMonitorSpecificScalingActive() {
400424
public static void setAutoScaleForMonitorSpecificScaling() {
401425
boolean isDefaultAutoScale = autoScaleValue == null;
402426
if (isDefaultAutoScale) {
403-
autoScaleValue = "quarter";
427+
setAutoScaleValue("quarter");
404428
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
405429
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
406430
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ void checkGC(int mask) {
342342
}
343343
}
344344
if ((state & FONT) != 0) {
345-
long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
345+
long fontHandle = SWTFontProvider.getFontHandle(data.font, getNativeZoom());
346346
OS.SelectObject(handle, fontHandle);
347347
long[] hFont = new long[1];
348348
long gdipFont = createGdipFont(handle, fontHandle, gdipGraphics, device.fontCollection, null, hFont);
@@ -463,7 +463,7 @@ void checkGC(int mask) {
463463
OS.SetTextColor(handle, data.foreground);
464464
}
465465
if ((state & FONT) != 0) {
466-
long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
466+
long fontHandle = SWTFontProvider.getFontHandle(data.font, getNativeZoom());
467467
OS.SelectObject(handle, fontHandle);
468468
}
469469
}
@@ -2712,7 +2712,7 @@ void drawText(long gdipGraphics, String string, int x, int y, int flags, Point s
27122712
char[] chars = string.toCharArray();
27132713
long hdc = Gdip.Graphics_GetHDC(gdipGraphics);
27142714
long hFont = data.hGDIFont;
2715-
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
2715+
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, getNativeZoom());
27162716
long oldFont = 0;
27172717
if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont);
27182718
TEXTMETRIC lptm = new TEXTMETRIC();
@@ -2802,7 +2802,7 @@ private RectF drawText(long gdipGraphics, char[] buffer, int start, int length,
28022802
}
28032803
long hdc = Gdip.Graphics_GetHDC(gdipGraphics);
28042804
long hFont = data.hGDIFont;
2805-
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
2805+
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, getNativeZoom());
28062806
long oldFont = 0;
28072807
if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont);
28082808
if (start != 0) {
@@ -3945,7 +3945,7 @@ public FontMetrics getFontMetrics() {
39453945
checkGC(FONT);
39463946
TEXTMETRIC lptm = new TEXTMETRIC();
39473947
OS.GetTextMetrics(handle, lptm);
3948-
return FontMetrics.win32_new(lptm, data.nativeZoom);
3948+
return FontMetrics.win32_new(lptm, getNativeZoom());
39493949
}
39503950

39513951
/**
@@ -4378,9 +4378,9 @@ private void init(Drawable drawable, GCData data, long hDC) {
43784378
}
43794379
if (data.font != null) {
43804380
data.state &= ~FONT;
4381-
data.font = Font.win32_new(data.font, data.nativeZoom);
4381+
data.font = Font.win32_new(data.font, DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom));
43824382
} else {
4383-
data.font = SWTFontProvider.getFont(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT), data.nativeZoom);
4383+
data.font = SWTFontProvider.getFont(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT), DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom));
43844384
}
43854385
Image image = data.image;
43864386
if (image != null) {
@@ -5015,12 +5015,12 @@ private class SetFontOperation extends Operation {
50155015
private final Font font;
50165016

50175017
SetFontOperation(Font font) {
5018-
this.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], data.nativeZoom) : null;
5018+
this.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], getNativeZoom()) : null;
50195019
}
50205020

50215021
@Override
50225022
void apply() {
5023-
data.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], data.nativeZoom) : SWTFontProvider.getSystemFont(device, data.nativeZoom);
5023+
data.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], getNativeZoom()) : SWTFontProvider.getSystemFont(device, getNativeZoom());
50245024
data.state &= ~FONT;
50255025
}
50265026
}
@@ -5939,7 +5939,11 @@ private static int sin(int angle, int length) {
59395939
}
59405940

59415941
int getZoom() {
5942-
return DPIUtil.getZoomForAutoscaleProperty(data.nativeZoom);
5942+
return DPIUtil.getZoomForAutoscaleProperty(getNativeZoom());
5943+
}
5944+
5945+
int getNativeZoom() {
5946+
return DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom);
59435947
}
59445948

59455949
private void storeAndApplyOperationForExistingHandle(Operation operation) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,9 @@ public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Col
770770

771771
private int getNativeZoom(GC gc) {
772772
if (gc != null) {
773-
return gc.data.nativeZoom;
773+
return DPIUtil.getFontZoomForAutoscaleProperty(gc.data.nativeZoom);
774774
}
775-
return nativeZoom;
775+
return DPIUtil.getFontZoomForAutoscaleProperty(nativeZoom);
776776
}
777777

778778
private int getZoom(GC gc){

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ public long internal_new_GC (GCData data) {
17631763
if (font != null) {
17641764
data.font = font;
17651765
} else {
1766-
data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), data.nativeZoom);
1766+
data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom));
17671767
}
17681768
data.uiState = (int)OS.SendMessage (hwnd, OS.WM_QUERYUISTATE, 0, 0);
17691769
}

0 commit comments

Comments
 (0)