Skip to content

Commit 54f6226

Browse files
committed
[win32] Handle all scaled font via SWTFontProvider
This commit extends the SWTFontProvider and the SWTFontRegistry to receive scaled variants of a font where only the handle is available. Common use case for this is OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0) to receive the handle of the current font of a handle. As at this point the correct zoom the handle was created is not yet know, it is better to retrieve and manage those fonts via the SWTFontRegistry expecially when monitor specific scaling is used. When the passed font handle is not already managed via the SWTFontRegistry it will create the SWT Font as it was create previously.
1 parent 3670670 commit 54f6226

File tree

8 files changed

+60
-8
lines changed

8 files changed

+60
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,14 +3898,14 @@ void init(Drawable drawable, GCData data, long hDC) {
38983898
data.background = OS.GetBkColor(hDC);
38993899
}
39003900
data.state &= ~(NULL_BRUSH | NULL_PEN);
3901+
if (data.nativeZoom == 0) {
3902+
data.nativeZoom = extractZoom(hDC);
3903+
}
39013904
Font font = data.font;
39023905
if (font != null) {
39033906
data.state &= ~FONT;
39043907
} else {
3905-
data.font = Font.win32_new(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT));
3906-
}
3907-
if (data.nativeZoom == 0) {
3908-
data.nativeZoom = extractZoom(hDC);
3908+
data.font = SWTFontProvider.getFont(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT), data.nativeZoom);
39093909
}
39103910
Image image = data.image;
39113911
if (image != null) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public Font getFont(FontData fontData, int zoom) {
7171
return font;
7272
}
7373

74+
@Override
75+
public Font getFont(long fontHandle, int zoom) {
76+
return Font.win32_new(device, fontHandle, zoom);
77+
}
78+
7479
private Font registerFont(FontData fontData, Font font) {
7580
fontsMap.put(fontData, font);
7681
return font;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ public static Font getFont(Device device, FontData fontData, int zoom) {
6161
return getFontRegistry(device).getFont(fontData, zoom);
6262
}
6363

64+
/**
65+
* Returns the font with the given fontHandle for the given device at the
66+
* specified zoom.
67+
*
68+
* <b>Note:</b> This operation is not thread-safe. It must thus always be called
69+
* from the same thread for the same device, such as the display's UI thread.
70+
*
71+
* @param device the device to retrieve the font for, must not be {@code null}
72+
* @param fontHandle the handle to an existing font
73+
* @param zoom the zoom for which the font shall be scaled
74+
*/
75+
public static Font getFont(Device device, long fontHandle, int zoom) {
76+
return getFontRegistry(device).getFont(fontHandle, zoom);
77+
}
78+
6479
/**
6580
* Disposes the font registry for the given device, if one exists.
6681
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public interface SWTFontRegistry {
4646
*/
4747
Font getFont(FontData fontData, int zoom);
4848

49+
50+
/**
51+
* Provides a font optimally suited for the specified zoom. If the handle is yet unknown to
52+
* the registry, the font will not be managed by the font registry. Only Fonts created in the
53+
* font registry are managed by it and should not be disposed of externally.
54+
*
55+
* @param fontHandle the handle to an existing font
56+
* @param zoom zoom in % of the standard resolution to determine the appropriate font
57+
* @return the font best suited for the specified zoom
58+
*/
59+
Font getFont(long fontHandle, int zoom);
60+
4961
/**
5062
* Disposes all fonts managed by the font registry.
5163
*/

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

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

4545
private Font createAndCacheFont(int zoom) {
4646
Font newFont = createFont(zoom);
47+
customFontHandlesKeyMap.put(newFont.handle, this);
4748
scaledFonts.put(zoom, newFont);
4849
return newFont;
4950
}
@@ -112,6 +113,7 @@ protected void dispose() {
112113

113114
private ScaledFontContainer systemFontContainer;
114115
private Map<FontData, ScaledFontContainer> customFontsKeyMap = new HashMap<>();
116+
private Map<Long, ScaledFontContainer> customFontHandlesKeyMap = new HashMap<>();
115117
private Device device;
116118

117119
ScalingSWTFontRegistry(Device device) {
@@ -136,6 +138,14 @@ public Font getFont(FontData fontData, int zoom) {
136138
return container.getScaledFont(zoom);
137139
}
138140

141+
@Override
142+
public Font getFont(long fontHandle, int zoom) {
143+
if (customFontHandlesKeyMap.containsKey(fontHandle)) {
144+
return customFontHandlesKeyMap.get(fontHandle).getScaledFont(zoom);
145+
}
146+
return Font.win32_new(device, fontHandle, zoom);
147+
}
148+
139149
@Override
140150
public void dispose() {
141151
customFontsKeyMap.values().forEach(ScaledFontContainer::dispose);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ LRESULT WM_PAINT (long wParam, long lParam) {
15251525
Control control = findBackgroundControl ();
15261526
if (control == null) control = this;
15271527
data.background = control.getBackgroundPixel ();
1528-
data.font = Font.win32_new(display, OS.SendMessage (handle, OS.WM_GETFONT, 0, 0), nativeZoom);
1528+
15291529
data.uiState = (int)OS.SendMessage (handle, OS.WM_QUERYUISTATE, 0, 0);
15301530
if ((style & SWT.NO_BACKGROUND) != 0) {
15311531
/* This code is intentionally commented because it may be slow to copy bits from the screen */
@@ -1536,6 +1536,8 @@ LRESULT WM_PAINT (long wParam, long lParam) {
15361536
drawBackground (phdc [0], rect);
15371537
}
15381538
GC gc = createNewGC(phdc [0], data);
1539+
data.font = SWTFontProvider.getFont(display, OS.SendMessage (handle, OS.WM_GETFONT, 0, 0), data.nativeZoom);
1540+
15391541
Event event = new Event ();
15401542
event.gc = gc;
15411543
event.setBounds(DPIUtil.scaleDown(new Rectangle(ps.left, ps.top, width, height), getZoom()));

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ public Font getFont () {
13151315
if (font != null) return font;
13161316
long hFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
13171317
if (hFont == 0) hFont = defaultFont ();
1318-
return Font.win32_new (display, hFont, getShell().nativeZoom);
1318+
return SWTFontProvider.getFont(display, hFont, getNativeZoom());
13191319
}
13201320

13211321
/**
@@ -1748,14 +1748,18 @@ public long internal_new_GC (GCData data) {
17481748
}
17491749
}
17501750
data.device = display;
1751-
data.nativeZoom = nativeZoom;
1751+
data.nativeZoom = getNativeZoom();
17521752
int foreground = getForegroundPixel ();
17531753
if (foreground != OS.GetTextColor (hDC)) data.foreground = foreground;
17541754
Control control = findBackgroundControl ();
17551755
if (control == null) control = this;
17561756
int background = control.getBackgroundPixel ();
17571757
if (background != OS.GetBkColor (hDC)) data.background = background;
1758-
data.font = font != null ? font : Font.win32_new (display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0));
1758+
if (font != null) {
1759+
data.font = font;
1760+
} else {
1761+
data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), data.nativeZoom);
1762+
}
17591763
data.uiState = (int)OS.SendMessage (hwnd, OS.WM_QUERYUISTATE, 0, 0);
17601764
}
17611765
return hDC;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,10 @@ GC createNewGC(long hDC, GCData data) {
26902690
return GC.win32_new(hDC, data);
26912691
}
26922692

2693+
int getNativeZoom() {
2694+
return nativeZoom;
2695+
}
2696+
26932697
int getZoom() {
26942698
return DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
26952699
}

0 commit comments

Comments
 (0)