Skip to content

Commit 518533c

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 fontZoom to the autoscaled zoom value when autoScale value is fixed Fixes #2311
1 parent 63049a1 commit 518533c

File tree

5 files changed

+64
-33
lines changed

5 files changed

+64
-33
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: 37 additions & 10 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.
@@ -97,21 +96,41 @@ public static Optional<AutoScaleMethod> forString(String s) {
9796
private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method";
9897

9998
static {
100-
autoScaleValue = System.getProperty (SWT_AUTOSCALE);
101-
99+
setAutoScaleValue(System.getProperty (SWT_AUTOSCALE));
102100
String value = System.getProperty (SWT_AUTOSCALE_METHOD);
103101
AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO);
104102
autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST;
105103
}
106104

107105
static String getAutoScaleValue() {
108-
return autoScaleValue;
106+
return autoScaleConfig.value;
109107
}
110108

111109
static void setAutoScaleValue(String autoScaleValueArg) {
112-
autoScaleValue = autoScaleValueArg;
110+
autoScaleConfig = new AutoScaleConfig(autoScaleValueArg);
113111
}
114112

113+
private static final class AutoScaleConfig {
114+
private final String value;
115+
private final boolean useAutoScaledFontZoom;
116+
117+
AutoScaleConfig(String value) {
118+
this.value = value;
119+
useAutoScaledFontZoom = isIntegerAutoScale(value);
120+
}
121+
122+
private static boolean isIntegerAutoScale(String value) {
123+
if (value == null)
124+
return false;
125+
try {
126+
Integer.parseInt(value);
127+
return true;
128+
} catch (NumberFormatException e) {
129+
return false;
130+
}
131+
}
132+
}
133+
115134
public static int pixelToPoint(int size, int zoom) {
116135
if (zoom == 100 || size == SWT.DEFAULT) return size;
117136
float scaleFactor = getScalingFactor (zoom);
@@ -343,7 +362,15 @@ static void setUseSmoothScalingByDefaultProvider(UseSmoothScalingProvider provid
343362
}
344363

345364
public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
346-
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+
}
347374
}
348375

349376
private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String autoScaleValue) {
@@ -376,13 +403,13 @@ private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String aut
376403
}
377404

378405
public static void runWithAutoScaleValue(String autoScaleValue, Runnable runnable) {
379-
String initialAutoScaleValue = DPIUtil.autoScaleValue;
380-
DPIUtil.autoScaleValue = autoScaleValue;
406+
String initialAutoScaleValue = autoScaleConfig.value;
407+
setAutoScaleValue(autoScaleValue);
381408
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
382409
try {
383410
runnable.run();
384411
} finally {
385-
DPIUtil.autoScaleValue = initialAutoScaleValue;
412+
setAutoScaleValue(initialAutoScaleValue);
386413
DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom);
387414
}
388415
}

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)