Skip to content

Commit af6c974

Browse files
committed
Deprecate DefaultSWTFontRegistry and switch to ScalingSWTFontRegistry
1)Always use ScalingSWTFontRegistry 2)Rename DefaultSWTFontRegistry -> LegacySWTFontRegistry as this is not default anymore 3)Add a system property to use LegacySWTFontRegistry without monitor specific scaling active 4)Deprecate the LegacySWTFontRegistry(DefaultSWTFontRegistry)
1 parent 0a68dea commit af6c974

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
import org.junit.jupiter.api.extension.*;
2424

2525
@ExtendWith(PlatformSpecificExecutionExtension.class)
26-
class DefaultSWTFontRegistryTests {
26+
class LegacySWTFontRegistryTests {
2727
private static String TEST_FONT = "Helvetica";
2828
private Display display;
2929
private SWTFontRegistry fontRegistry;
3030

3131
@BeforeEach
3232
public void setUp() {
3333
this.display = Display.getDefault();
34-
this.fontRegistry = new DefaultSWTFontRegistry(display);
34+
this.fontRegistry = new LegacySWTFontRegistry(display);
3535
}
3636

3737
@AfterEach

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,38 @@ public void testScaleFontCorrectlyInAutoScaleSzenario() {
4444
}
4545

4646
@Test
47-
public void testDoNotScaleFontCorrectlyInNoAutoScaleSzenario() {
47+
public void testScaleFontCorrectlyInNoAutoScaleSzenario() {
4848
DPIUtil.setMonitorSpecificScaling(false);
4949
Display display = Display.getDefault();
5050

51-
assertFalse("Autoscale property is not set to false", display.isRescalingAtRuntime());
51+
assertFalse("Autoscale property is not set to true", display.isRescalingAtRuntime());
5252
int scalingFactor = 2;
5353
FontComparison fontComparison = updateFont(scalingFactor);
54-
assertEquals("Font height in pixels is different when setting the same font again",
55-
fontComparison.originalFontHeight, fontComparison.currentFontHeight);
54+
assertEquals("Font height in pixels is not adjusted according to the scale factor",
55+
fontComparison.originalFontHeight * scalingFactor, fontComparison.currentFontHeight);
56+
}
57+
58+
@Test
59+
public void testDoNotScaleFontCorrectlyInNoAutoScaleSzenarioWithLegacyFontRegistry() {
60+
DPIUtil.setMonitorSpecificScaling(false);
61+
String originalValue = System.getProperty("swt.fontRegistry");
62+
System.setProperty("swt.fontRegistry", "legacy");
63+
try {
64+
Display display = Display.getDefault();
65+
66+
assertFalse("Autoscale property is not set to false", display.isRescalingAtRuntime());
67+
int scalingFactor = 2;
68+
FontComparison fontComparison = updateFont(scalingFactor);
69+
assertEquals("Font height in pixels is different when setting the same font again",
70+
fontComparison.originalFontHeight, fontComparison.currentFontHeight);
71+
}
72+
finally {
73+
if (originalValue != null) {
74+
System.setProperty("swt.fontRegistry", originalValue);
75+
} else {
76+
System.clearProperty("swt.fontRegistry");
77+
}
78+
}
5679
}
5780

5881
@Test
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@
1919
import org.eclipse.swt.internal.win32.*;
2020

2121
/**
22-
* This class is used in the win32 implementation only to support
23-
* unscaled fonts in multiple DPI zoom levels.
22+
* <p>
23+
* Formerly {@code DefaultSWTFontRegistry}, this class is deprecated. Use {@code ScalingSWTFontRegistry} instead.
24+
* To temporarily fall back to legacy font behavior ({@code LegacySWTFontRegistry})
25+
* (e.g., if issues arise in existing RCP products), set the system property: {@code
26+
* -Dswt.fontRegistry=legacy
27+
* }
28+
* </p>
2429
*
2530
* As this class is only intended to be used internally via {@code SWTFontProvider},
2631
* it should neither be instantiated nor referenced in a client application.
2732
* The behavior can change any time in a future release.
2833
*/
29-
final class DefaultSWTFontRegistry implements SWTFontRegistry {
34+
@Deprecated(forRemoval= true, since= "2025-09")
35+
final class LegacySWTFontRegistry implements SWTFontRegistry {
3036
private static FontData KEY_SYSTEM_FONTS = new FontData();
3137
private Map<FontData, Font> fontsMap = new HashMap<>();
3238
private Device device;
3339

34-
DefaultSWTFontRegistry(Device device) {
40+
LegacySWTFontRegistry(Device device) {
3541
this.device = device;
3642
}
3743

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ public static void disposeFontRegistry(Device device) {
105105
}
106106
}
107107

108+
private static final String SWT_FONT_REGISTRY = "swt.fontRegistry";
109+
108110
private static SWTFontRegistry newFontRegistry(Device device) {
109-
if (device instanceof Display display && display.isRescalingAtRuntime()) {
110-
return new ScalingSWTFontRegistry(device);
111+
if ("legacy".equals(System.getProperty(SWT_FONT_REGISTRY)) && device instanceof Display display
112+
&& !display.isRescalingAtRuntime()) {
113+
return new LegacySWTFontRegistry(device);
111114
}
112-
return new DefaultSWTFontRegistry(device);
115+
return new ScalingSWTFontRegistry(device);
116+
113117
}
114118
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ private Font getScaledFont(int zoom) {
4444

4545
private Font createAndCacheFont(int zoom) {
4646
Font newFont = createFont(zoom);
47-
customFontHandlesKeyMap.put(Font.win32_getHandle(newFont), this);
47+
FontData clonedFontData = new FontData(newFont.getFontData()[0].toString());
48+
fontsKeyMap.put(clonedFontData, this);
49+
fontHandlesKeyMap.put(Font.win32_getHandle(newFont), this);
4850
scaledFonts.put(zoom, newFont);
4951
return newFont;
5052
}
@@ -108,8 +110,8 @@ protected void dispose() {
108110
}
109111

110112
private ScaledFontContainer systemFontContainer;
111-
private Map<FontData, ScaledFontContainer> customFontsKeyMap = new HashMap<>();
112-
private Map<Long, ScaledFontContainer> customFontHandlesKeyMap = new HashMap<>();
113+
private Map<FontData, ScaledFontContainer> fontsKeyMap = new HashMap<>();
114+
private Map<Long, ScaledFontContainer> fontHandlesKeyMap = new HashMap<>();
113115
private Device device;
114116

115117
ScalingSWTFontRegistry(Device device) {
@@ -125,27 +127,26 @@ public Font getSystemFont(int zoom) {
125127
@Override
126128
public Font getFont(FontData fontData, int zoom) {
127129
ScaledFontContainer container;
128-
if (customFontsKeyMap.containsKey(fontData)) {
129-
container = customFontsKeyMap.get(fontData);
130+
if (fontsKeyMap.containsKey(fontData)) {
131+
container = fontsKeyMap.get(fontData);
130132
} else {
131133
FontData clonedFontData = new FontData(fontData.toString());
132134
container = new ScaledCustomFontContainer(clonedFontData);
133-
customFontsKeyMap.put(clonedFontData, container);
134135
}
135136
return container.getScaledFont(zoom);
136137
}
137138

138139
@Override
139140
public Font getFont(long fontHandle, int zoom) {
140-
if (customFontHandlesKeyMap.containsKey(fontHandle)) {
141-
return customFontHandlesKeyMap.get(fontHandle).getScaledFont(zoom);
141+
if (fontHandlesKeyMap.containsKey(fontHandle)) {
142+
return fontHandlesKeyMap.get(fontHandle).getScaledFont(zoom);
142143
}
143144
return Font.win32_new(device, fontHandle, zoom);
144145
}
145146

146147
@Override
147148
public void dispose() {
148-
customFontsKeyMap.values().forEach(ScaledFontContainer::dispose);
149-
customFontsKeyMap.clear();
149+
fontsKeyMap.values().forEach(ScaledFontContainer::dispose);
150+
fontsKeyMap.clear();
150151
}
151152
}

0 commit comments

Comments
 (0)