Skip to content

Commit f95af17

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 b6b36e0 commit f95af17

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +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;
52-
53-
/**
54-
* this field is used to store fontData provided during initialization
55-
*/
56-
private final FontData fontData;
50+
private long handle;
5751

5852
/**
5953
* The zoom in % of the standard resolution used for conversion of point height to pixel height
6054
* (Warning: This field is platform dependent)
6155
*/
6256
int zoom;
6357

58+
/**
59+
* this field is used to mark destroyed fonts
60+
*/
61+
private boolean isDestroyed;
62+
63+
/**
64+
* this field is used to store fontData provided during initialization
65+
*/
66+
private final FontData fontData;
67+
6468
/**
6569
* Font height in points. As the conversion to pixel height involves rounding the fontHeight must
6670
* be cached.
@@ -107,16 +111,18 @@ private Font(Device device, long handle, int zoom) {
107111
*/
108112
public Font(Device device, FontData fd) {
109113
super(device);
114+
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
110115
this.zoom = DPIUtil.getNativeDeviceZoom();
111-
this.fontData = fd;
116+
this.fontData = new FontData(fd.toString());
112117
this.fontHeight = fd.height;
113118
init();
114119
}
115120

116121
private Font(Device device, FontData fd, int zoom) {
117122
super(device);
123+
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
118124
this.zoom = zoom;
119-
this.fontData = fd;
125+
this.fontData = new FontData(fd.toString());
120126
this.fontHeight = fd.height;
121127
init();
122128
}
@@ -155,7 +161,7 @@ public Font(Device device, FontData[] fds) {
155161
}
156162
this.zoom = DPIUtil.getNativeDeviceZoom();
157163
FontData fd = fds[0];
158-
this.fontData = fd;
164+
this.fontData = new FontData(fd.toString());
159165
this.fontHeight = fd.height;
160166
init();
161167
}
@@ -197,14 +203,15 @@ public Font(Device device, String name, int height, int style) {
197203
super(device);
198204
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
199205
this.zoom = DPIUtil.getNativeDeviceZoom();
200-
this.fontData = new FontData (name, height, style);
206+
this.fontData = new FontData (name, height, style);
201207
this.fontHeight = height;
202208
init();
203209
}
204210
@Override
205211
void destroy() {
206212
OS.DeleteObject(handle);
207213
handle = 0;
214+
isDestroyed = true;
208215
}
209216

210217
/**
@@ -222,7 +229,7 @@ public boolean equals(Object object) {
222229
if (object == this) return true;
223230
if (!(object instanceof Font)) return false;
224231
Font font = (Font) object;
225-
return device == font.device && handle == font.handle;
232+
return device == font.device && win32_getHandle(this) == win32_getHandle(font);
226233
}
227234

228235
/**
@@ -245,7 +252,7 @@ public FontData[] getFontData() {
245252

246253
private LOGFONT fetchLogFontData() {
247254
LOGFONT logFont = new LOGFONT ();
248-
OS.GetObject(handle, LOGFONT.sizeof, logFont);
255+
OS.GetObject(win32_getHandle(this), LOGFONT.sizeof, logFont);
249256
return logFont;
250257
}
251258

@@ -261,7 +268,7 @@ private LOGFONT fetchLogFontData() {
261268
*/
262269
@Override
263270
public int hashCode () {
264-
return (int)handle;
271+
return (int) win32_getHandle(this);
265272
}
266273

267274
void init (FontData fd) {
@@ -286,7 +293,7 @@ void init (FontData fd) {
286293
*/
287294
@Override
288295
public boolean isDisposed() {
289-
return handle == 0;
296+
return isDestroyed;
290297
}
291298

292299
/**
@@ -308,7 +315,7 @@ public String toString () {
308315
* available on all platforms, and should never be called from
309316
* application code.
310317
*
311-
* Creates a new handle for the requested font or returns the existing one
318+
* Creates a new handle for the requested font or return the existing one
312319
*
313320
* @param font the font to get the handle of
314321
*

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)