Skip to content

Commit d1337cf

Browse files
Create Font handles on demand
Creating a new static method to retrieve font handle. Handles will no longer be created during initialization. Also destroy condition is changed since handle being 0 doesn't mean that it was destroyed anymore.
1 parent 52a5129 commit d1337cf

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public final class Font extends Resource {
5555
*/
5656
int zoom;
5757

58+
/**
59+
* this field is used to mark destroyed images
60+
*/
61+
private boolean isDestroyed;
62+
63+
/**
64+
* this field is used to store fontData provided during initialization
65+
*/
66+
private FontData fontData;
67+
5868
/**
5969
* Font height in points. As the conversion to pixel height involves rounding the fontHeight must
6070
* be cached.
@@ -100,15 +110,15 @@ private Font(Device device, long handle, int zoom) {
100110
public Font(Device device, FontData fd) {
101111
super(device);
102112
this.zoom = DPIUtil.getNativeDeviceZoom();
103-
init(fd);
113+
this.fontData = new FontData(fd.toString());
104114
this.fontHeight = fd.height;
105115
init();
106116
}
107117

108118
private Font(Device device, FontData fd, int zoom) {
109119
super(device);
110120
this.zoom = zoom;
111-
init(fd);
121+
this.fontData = new FontData(fd.toString());
112122
this.fontHeight = fd.height;
113123
init();
114124
}
@@ -147,7 +157,7 @@ public Font(Device device, FontData[] fds) {
147157
}
148158
this.zoom = DPIUtil.getNativeDeviceZoom();
149159
FontData fd = fds[0];
150-
init(fds[0]);
160+
this.fontData = new FontData(fd.toString());
151161
this.fontHeight = fd.height;
152162
init();
153163
}
@@ -180,7 +190,7 @@ public Font(Device device, String name, int height, int style) {
180190
super(device);
181191
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
182192
this.zoom = DPIUtil.getNativeDeviceZoom();
183-
init(new FontData (name, height, style));
193+
this.fontData = new FontData (name, height, style);
184194
this.fontHeight = height;
185195
init();
186196
}
@@ -189,14 +199,15 @@ public Font(Device device, String name, int height, int style) {
189199
super(device);
190200
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
191201
this.zoom = DPIUtil.getNativeDeviceZoom();
192-
init(new FontData (name, height, style));
202+
this.fontData = new FontData (name, height, style);
193203
this.fontHeight = height;
194204
init();
195205
}
196206
@Override
197207
void destroy() {
198208
OS.DeleteObject(handle);
199209
handle = 0;
210+
isDestroyed = true;
200211
}
201212

202213
/**
@@ -237,7 +248,7 @@ public FontData[] getFontData() {
237248

238249
private LOGFONT fetchLogFontData() {
239250
LOGFONT logFont = new LOGFONT ();
240-
OS.GetObject(handle, LOGFONT.sizeof, logFont);
251+
OS.GetObject(win32_getHandle(this), LOGFONT.sizeof, logFont);
241252
return logFont;
242253
}
243254

@@ -278,7 +289,7 @@ void init (FontData fd) {
278289
*/
279290
@Override
280291
public boolean isDisposed() {
281-
return handle == 0;
292+
return isDestroyed;
282293
}
283294

284295
/**

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static Font getSystemFont(Device device, int zoom) {
4848
}
4949

5050
public static long getSystemFontHandle(Device device, int zoom) {
51-
return getSystemFont(device, zoom).handle;
51+
return Font.win32_getHandle(getSystemFont(device, zoom));
5252
}
5353

5454
/**
@@ -67,14 +67,14 @@ public static Font getFont(Device device, FontData fontData, int zoom) {
6767
}
6868

6969
public static long getFontHandle(Device device, FontData fontData, int zoom) {
70-
return getFont(device, fontData, zoom).handle;
70+
return Font.win32_getHandle(getFont(device, fontData, zoom));
7171
}
7272

7373
public static long getFontHandle(Font font, int zoom) {
7474
if (font == null) {
7575
SWT.error(SWT.ERROR_NULL_ARGUMENT);
7676
}
77-
return getFont(font.getDevice(), font.getFontData()[0], zoom).handle;
77+
return Font.win32_getHandle(getFont(font.getDevice(), font.getFontData()[0], zoom));
7878
}
7979

8080
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +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);
47+
customFontHandlesKeyMap.put(Font.win32_getHandle(newFont), this);
4848
scaledFonts.put(zoom, newFont);
4949
return newFont;
5050
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3441,7 +3441,7 @@ public void setFont (Font font) {
34413441
long hFont = 0;
34423442
if (newFont != null) {
34433443
if (newFont.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
3444-
hFont = newFont.handle;
3444+
hFont = Font.win32_getHandle(newFont);
34453445
}
34463446
this.font = newFont;
34473447
if (hFont == 0) hFont = defaultFont ();

0 commit comments

Comments
 (0)