Skip to content

Commit 504c82b

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[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 039d965 commit 504c82b

File tree

7 files changed

+55
-8
lines changed

7 files changed

+55
-8
lines changed

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

+4-4
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

+5
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
FontData clonedFontData = new FontData(fontData.toString());
7681
fontsMap.put(clonedFontData, font);

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

+15
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

+12
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

+10
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) {
@@ -137,6 +139,14 @@ public Font getFont(FontData fontData, int zoom) {
137139
return container.getScaledFont(zoom);
138140
}
139141

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,6 @@ 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), getNativeZoom());
15291528
data.uiState = (int)OS.SendMessage (handle, OS.WM_QUERYUISTATE, 0, 0);
15301529
if ((style & SWT.NO_BACKGROUND) != 0) {
15311530
/* This code is intentionally commented because it may be slow to copy bits from the screen */
@@ -1536,6 +1535,8 @@ LRESULT WM_PAINT (long wParam, long lParam) {
15361535
drawBackground (phdc [0], rect);
15371536
}
15381537
GC gc = createNewGC(phdc [0], data);
1538+
data.font = SWTFontProvider.getFont(display, OS.SendMessage (handle, OS.WM_GETFONT, 0, 0), data.nativeZoom);
1539+
15391540
Event event = new Event ();
15401541
event.gc = gc;
15411542
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

+7-3
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, getNativeZoom());
1318+
return SWTFontProvider.getFont(display, hFont, getNativeZoom());
13191319
}
13201320

13211321
/**
@@ -1753,14 +1753,18 @@ public long internal_new_GC (GCData data) {
17531753
}
17541754
}
17551755
data.device = display;
1756-
data.nativeZoom = nativeZoom;
1756+
data.nativeZoom = getNativeZoom();
17571757
int foreground = getForegroundPixel ();
17581758
if (foreground != OS.GetTextColor (hDC)) data.foreground = foreground;
17591759
Control control = findBackgroundControl ();
17601760
if (control == null) control = this;
17611761
int background = control.getBackgroundPixel ();
17621762
if (background != OS.GetBkColor (hDC)) data.background = background;
1763-
data.font = font != null ? font : Font.win32_new (display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0));
1763+
if (font != null) {
1764+
data.font = font;
1765+
} else {
1766+
data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), data.nativeZoom);
1767+
}
17641768
data.uiState = (int)OS.SendMessage (hwnd, OS.WM_QUERYUISTATE, 0, 0);
17651769
}
17661770
return hDC;

0 commit comments

Comments
 (0)