Skip to content

Commit 835f3da

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 65ffcb0 commit 835f3da

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,25 @@ public final class Font extends Resource {
4646
* platforms and should never be accessed from application code.
4747
* </p>
4848
*
49-
* @noreference This field is not intended to be referenced by clients.
5049
*/
51-
public long handle;
50+
private long handle;
5251

5352
/**
5453
* The zoom in % of the standard resolution used for conversion of point height to pixel height
5554
* (Warning: This field is platform dependent)
5655
*/
5756
int zoom;
5857

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+
5968
/**
6069
* Font height in points. As the conversion to pixel height involves rounding the fontHeight must
6170
* be cached.
@@ -101,15 +110,15 @@ private Font(Device device, long handle, int zoom) {
101110
public Font(Device device, FontData fd) {
102111
super(device);
103112
this.zoom = DPIUtil.getNativeDeviceZoom();
104-
init(fd);
113+
this.fontData = new FontData(fd.toString());
105114
this.fontHeight = fd.height;
106115
init();
107116
}
108117

109118
private Font(Device device, FontData fd, int zoom) {
110119
super(device);
111120
this.zoom = zoom;
112-
init(fd);
121+
this.fontData = new FontData(fd.toString());
113122
this.fontHeight = fd.height;
114123
init();
115124
}
@@ -148,7 +157,7 @@ public Font(Device device, FontData[] fds) {
148157
}
149158
this.zoom = DPIUtil.getNativeDeviceZoom();
150159
FontData fd = fds[0];
151-
init(fds[0]);
160+
this.fontData = new FontData(fd.toString());
152161
this.fontHeight = fd.height;
153162
init();
154163
}
@@ -181,7 +190,7 @@ public Font(Device device, String name, int height, int style) {
181190
super(device);
182191
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
183192
this.zoom = DPIUtil.getNativeDeviceZoom();
184-
init(new FontData (name, height, style));
193+
this.fontData = new FontData (name, height, style);
185194
this.fontHeight = height;
186195
init();
187196
}
@@ -190,14 +199,15 @@ public Font(Device device, String name, int height, int style) {
190199
super(device);
191200
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
192201
this.zoom = DPIUtil.getNativeDeviceZoom();
193-
init(new FontData (name, height, style));
202+
this.fontData = new FontData (name, height, style);
194203
this.fontHeight = height;
195204
init();
196205
}
197206
@Override
198207
void destroy() {
199208
OS.DeleteObject(handle);
200209
handle = 0;
210+
isDestroyed = true;
201211
}
202212

203213
/**
@@ -238,7 +248,7 @@ public FontData[] getFontData() {
238248

239249
private LOGFONT fetchLogFontData() {
240250
LOGFONT logFont = new LOGFONT ();
241-
OS.GetObject(handle, LOGFONT.sizeof, logFont);
251+
OS.GetObject(win32_getHandle(this), LOGFONT.sizeof, logFont);
242252
return logFont;
243253
}
244254

@@ -279,7 +289,7 @@ void init (FontData fd) {
279289
*/
280290
@Override
281291
public boolean isDisposed() {
282-
return handle == 0;
292+
return isDestroyed;
283293
}
284294

285295
/**
@@ -294,6 +304,28 @@ public String toString () {
294304
return "Font {" + handle + "}";
295305
}
296306

307+
/**
308+
* <b>IMPORTANT:</b> This method is not part of the public
309+
* API for Font. It is marked public only so that it
310+
* can be shared within the packages provided by SWT. It is not
311+
* available on all platforms, and should never be called from
312+
* application code.
313+
*
314+
* Creates a new handle for the requested font or return the existing one
315+
*
316+
* @param font the font to get the handle of
317+
*
318+
* @return handle of the font
319+
*
320+
* @noreference This method is not intended to be referenced by clients.
321+
*/
322+
public static long win32_getHandle (Font font) {
323+
if(font.handle == 0) {
324+
font.init(font.fontData);
325+
}
326+
return font.handle;
327+
}
328+
297329
/**
298330
* Invokes platform specific functionality to allocate a new font.
299331
* <p>

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)