@@ -55,6 +55,16 @@ public final class Font extends Resource {
55
55
*/
56
56
int zoom ;
57
57
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
+
58
68
/**
59
69
* Font height in points. As the conversion to pixel height involves rounding the fontHeight must
60
70
* be cached.
@@ -63,6 +73,7 @@ public final class Font extends Resource {
63
73
64
74
private Font (Device device , long handle , int zoom ) {
65
75
super (device );
76
+ this .fontData = null ;
66
77
this .handle = handle ;
67
78
this .zoom = zoom ;
68
79
this .fontHeight = device .computePoints (fetchLogFontData (), handle , zoom );
@@ -90,16 +101,18 @@ private Font(Device device, long handle, int zoom) {
90
101
*/
91
102
public Font (Device device , FontData fd ) {
92
103
super (device );
104
+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
93
105
this .zoom = DPIUtil .getNativeDeviceZoom ();
94
- init (fd );
106
+ this . fontData = new FontData (fd . toString () );
95
107
this .fontHeight = fd .height ;
96
108
init ();
97
109
}
98
110
99
111
private Font (Device device , FontData fd , int zoom ) {
100
112
super (device );
113
+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
101
114
this .zoom = zoom ;
102
- init (fd );
115
+ this . fontData = new FontData (fd . toString () );
103
116
this .fontHeight = fd .height ;
104
117
init ();
105
118
}
@@ -138,7 +151,7 @@ public Font(Device device, FontData[] fds) {
138
151
}
139
152
this .zoom = DPIUtil .getNativeDeviceZoom ();
140
153
FontData fd = fds [0 ];
141
- init ( fds [ 0 ] );
154
+ this . fontData = new FontData ( fd . toString () );
142
155
this .fontHeight = fd .height ;
143
156
init ();
144
157
}
@@ -171,8 +184,7 @@ public Font(Device device, String name, int height, int style) {
171
184
super (device );
172
185
if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
173
186
this .zoom = DPIUtil .getNativeDeviceZoom ();
174
- FontData fd = new FontData (name , height , style );
175
- init (fd );
187
+ this .fontData = new FontData (name , height , style );
176
188
this .fontHeight = height ;
177
189
init ();
178
190
}
@@ -181,15 +193,15 @@ public Font(Device device, String name, int height, int style) {
181
193
super (device );
182
194
if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
183
195
this .zoom = DPIUtil .getNativeDeviceZoom ();
184
- FontData fd = new FontData (name , height , style );
185
- init (fd );
196
+ this .fontData = new FontData (name , height , style );
186
197
this .fontHeight = height ;
187
198
init ();
188
199
}
189
200
@ Override
190
201
void destroy () {
191
202
OS .DeleteObject (handle );
192
203
handle = 0 ;
204
+ isDestroyed = true ;
193
205
}
194
206
195
207
/**
@@ -207,7 +219,7 @@ public boolean equals(Object object) {
207
219
if (object == this ) return true ;
208
220
if (!(object instanceof Font )) return false ;
209
221
Font font = (Font ) object ;
210
- return device == font .device && handle == font . handle ;
222
+ return device == font .device && win32_getHandle ( this ) == win32_getHandle ( font ) ;
211
223
}
212
224
213
225
/**
@@ -230,7 +242,7 @@ public FontData[] getFontData() {
230
242
231
243
private LOGFONT fetchLogFontData () {
232
244
LOGFONT logFont = new LOGFONT ();
233
- OS .GetObject (handle , LOGFONT .sizeof , logFont );
245
+ OS .GetObject (win32_getHandle ( this ) , LOGFONT .sizeof , logFont );
234
246
return logFont ;
235
247
}
236
248
@@ -246,7 +258,7 @@ private LOGFONT fetchLogFontData() {
246
258
*/
247
259
@ Override
248
260
public int hashCode () {
249
- return (int )handle ;
261
+ return (int ) win32_getHandle ( this ) ;
250
262
}
251
263
252
264
void init (FontData fd ) {
@@ -271,7 +283,7 @@ void init (FontData fd) {
271
283
*/
272
284
@ Override
273
285
public boolean isDisposed () {
274
- return handle == 0 ;
286
+ return isDestroyed ;
275
287
}
276
288
277
289
/**
@@ -300,6 +312,9 @@ public String toString () {
300
312
* @noreference This method is not intended to be referenced by clients.
301
313
*/
302
314
public static long win32_getHandle (Font font ) {
315
+ if (font .handle == 0 && font .fontData != null ) {
316
+ font .init (font .fontData );
317
+ }
303
318
return font .handle ;
304
319
}
305
320
0 commit comments