Skip to content

Commit 057a824

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 057a824

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testCalculateGetBoundsWithVerticalIndent() {
6666
scaledLayout.setText(text);
6767
Rectangle scaledBounds = scaledLayout.getBounds();
6868

69-
assertNotEquals(layout.nativeZoom, scaledLayout.nativeZoom, "The native zoom for the TextLayouts must differ");
69+
assertNotEquals(layout.fontZoom, scaledLayout.fontZoom, "The font zooms for the TextLayouts must differ");
7070
assertEquals(unscaledBounds.height, scaledBounds.height, 1, "The public API for getBounds with vertical indent > 0 should give a similar result for any zoom level");
7171
}
7272

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: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class TextLayout extends Resource {
6767

6868
private MetricsAdapter metricsAdapter = new MetricsAdapter();
6969

70-
int nativeZoom = DPIUtil.getNativeDeviceZoom();
70+
int fontZoom = DPIUtil.getNativeDeviceZoom();
7171

7272
static final char LTR_MARK = '\u200E', RTL_MARK = '\u200F';
7373
static final int SCRIPT_VISATTR_SIZEOF = 2;
@@ -363,9 +363,9 @@ void checkLayout () {
363363
* Break paragraphs into lines, wraps the text, and initialize caches.
364364
*/
365365
void computeRuns (GC gc) {
366-
int newNativeZoom = getNativeZoom(gc);
367-
if (nativeZoom != newNativeZoom) {
368-
nativeZoom = newNativeZoom;
366+
int newFontZoom = getFontZoom(gc);
367+
if (fontZoom != newFontZoom) {
368+
fontZoom = newFontZoom;
369369
freeRuns();
370370
}
371371
if (runs != null) return;
@@ -768,19 +768,19 @@ public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Col
768768
drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, flags);
769769
}
770770

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

778778
private int getZoom(GC gc){
779-
return DPIUtil.getZoomForAutoscaleProperty(getNativeZoom(gc));
779+
return DPIUtil.getZoomForAutoscaleProperty(getFontZoom(gc));
780780
}
781781

782782
private int getZoom() {
783-
return DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
783+
return DPIUtil.getZoomForAutoscaleProperty(fontZoom);
784784
}
785785

786786
void drawInPixels (GC gc, int xInPoints, int yInPoints) {
@@ -1970,7 +1970,7 @@ public boolean getJustify () {
19701970

19711971
long getItemFont (StyleItem item, GC gc) {
19721972
if (item.fallbackFont != 0) return item.fallbackFont;
1973-
final int zoom = getNativeZoom(gc);
1973+
final int zoom = getFontZoom(gc);
19741974
if (item.style != null && item.style.font != null) {
19751975
return SWTFontProvider.getFontHandle(item.style.font, zoom);
19761976
}
@@ -2157,7 +2157,7 @@ public FontMetrics getLineMetrics (int lineIndex) {
21572157
lptm.tmHeight = Win32DPIUtils.pointToPixel(this.device, ascentInPoints + descentInPoints, zoom);
21582158
lptm.tmInternalLeading = Win32DPIUtils.pointToPixel(this.device, leadingInPoints, zoom);
21592159
lptm.tmAveCharWidth = 0;
2160-
return FontMetrics.win32_new(lptm, nativeZoom);
2160+
return FontMetrics.win32_new(lptm, fontZoom);
21612161
}
21622162

21632163
/**
@@ -3258,7 +3258,7 @@ public void setFont (Font font) {
32583258
Font oldFont = this.font;
32593259
if (oldFont == font) return;
32603260
this.font = font;
3261-
this.nativeZoom = this.font == null ? nativeZoom : this.font.zoom;
3261+
this.fontZoom = this.font == null ? fontZoom : this.font.zoom;
32623262
if (oldFont != null && oldFont.equals(font)) return;
32633263
freeRuns();
32643264
}

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)