Skip to content

Commit d0abdb3

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 d0abdb3

File tree

5 files changed

+67
-37
lines changed

5 files changed

+67
-37
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: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public static Optional<AutoScaleMethod> forString(String s) {
5757
}
5858
private static final AutoScaleMethod AUTO_SCALE_METHOD_SETTING;
5959
private static AutoScaleMethod autoScaleMethod;
60-
61-
private static String autoScaleValue;
60+
private static AutoScaleConfig autoScaleConfig;
6261

6362
/**
6463
* System property that controls the autoScale functionality.
@@ -108,13 +107,32 @@ public static Optional<AutoScaleMethod> forString(String s) {
108107
*/
109108
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
110109
static {
111-
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
112-
110+
autoScaleConfig = new AutoScaleConfig(System.getProperty (SWT_AUTOSCALE));
113111
String value = System.getProperty (SWT_AUTOSCALE_METHOD);
114112
AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO);
115113
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
116114
}
117115

116+
private static final class AutoScaleConfig {
117+
private final String value;
118+
private final boolean useAutoScaledFontZoom;
119+
120+
AutoScaleConfig(String value) {
121+
this.value = value;
122+
useAutoScaledFontZoom = isIntegerAutoScale(value);
123+
}
124+
125+
private static boolean isIntegerAutoScale(String value) {
126+
if (value == null)
127+
return false;
128+
try {
129+
Integer.parseInt(value);
130+
return true;
131+
} catch (NumberFormatException e) {
132+
return false;
133+
}
134+
}
135+
}
118136

119137
public static int pixelToPoint(int size, int zoom) {
120138
if (zoom == 100 || size == SWT.DEFAULT) return size;
@@ -344,7 +362,15 @@ private static boolean sholdUseSmoothScaling() {
344362
}
345363

346364
public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
347-
return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleValue);
365+
return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleConfig.value);
366+
}
367+
368+
public static int getFontZoomForAutoscaleProperty(int nativeDeviceZoom) {
369+
if (autoScaleConfig.useAutoScaledFontZoom ) {
370+
return getZoomForAutoscaleProperty(nativeDeviceZoom);
371+
} else {
372+
return nativeDeviceZoom;
373+
}
348374
}
349375

350376
private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String autoScaleValue) {
@@ -377,13 +403,13 @@ private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String aut
377403
}
378404

379405
public static void runWithAutoScaleValue(String autoScaleValue, Runnable runnable) {
380-
String initialAutoScaleValue = DPIUtil.autoScaleValue;
381-
DPIUtil.autoScaleValue = autoScaleValue;
406+
String initialAutoScaleValue = autoScaleConfig.value;
407+
autoScaleConfig = new AutoScaleConfig(autoScaleValue);
382408
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
383409
try {
384410
runnable.run();
385411
} finally {
386-
DPIUtil.autoScaleValue = initialAutoScaleValue;
412+
autoScaleConfig = new AutoScaleConfig(initialAutoScaleValue);
387413
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
388414
}
389415
}
@@ -398,13 +424,13 @@ public static boolean isMonitorSpecificScalingActive() {
398424
}
399425

400426
public static void setAutoScaleForMonitorSpecificScaling() {
401-
boolean isDefaultAutoScale = autoScaleValue == null;
427+
boolean isDefaultAutoScale = autoScaleConfig.value == null;
402428
if (isDefaultAutoScale) {
403-
autoScaleValue = "quarter";
429+
autoScaleConfig = new AutoScaleConfig("quarter");
404430
} else if (!isSupportedAutoScaleForMonitorSpecificScaling()) {
405431
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED,
406432
"monitor-specific scaling is only implemented for auto-scale values \"quarter\", \"exact\", \"false\" or a concrete zoom value, but \""
407-
+ autoScaleValue + "\" has been specified");
433+
+ autoScaleConfig.value + "\" has been specified");
408434
}
409435
}
410436

@@ -423,14 +449,14 @@ public static void setAutoScaleForMonitorSpecificScaling() {
423449
* "integer"/"integer200" and is thus not supported.
424450
*/
425451
private static boolean isSupportedAutoScaleForMonitorSpecificScaling() {
426-
if (autoScaleValue == null) {
452+
if (autoScaleConfig.value == null) {
427453
return false;
428454
}
429-
switch (autoScaleValue.toLowerCase()) {
455+
switch (autoScaleConfig.value.toLowerCase()) {
430456
case "false", "quarter", "exact": return true;
431457
}
432458
try {
433-
Integer.parseInt(autoScaleValue);
459+
Integer.parseInt(autoScaleConfig.value);
434460
return true;
435461
} catch (NumberFormatException e) {
436462
// unsupported value, use default

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

Lines changed: 13 additions & 9 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, getFontZoom());
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, getFontZoom());
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, getFontZoom());
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, getFontZoom());
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, getFontZoom());
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], getFontZoom()) : 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], getFontZoom()) : SWTFontProvider.getSystemFont(device, getFontZoom());
50245024
data.state &= ~FONT;
50255025
}
50265026
}
@@ -5942,6 +5942,10 @@ int getZoom() {
59425942
return DPIUtil.getZoomForAutoscaleProperty(data.nativeZoom);
59435943
}
59445944

5945+
int getFontZoom() {
5946+
return DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom);
5947+
}
5948+
59455949
private void storeAndApplyOperationForExistingHandle(Operation operation) {
59465950
operations.add(operation);
59475951
operation.apply();

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.getFontZoomForAutoscaleProperty(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)