@@ -55,6 +55,16 @@ public final class Font extends Resource {
5555 */
5656 int zoom ;
5757
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+
5868 /**
5969 * Font height in points. As the conversion to pixel height involves rounding the fontHeight must
6070 * be cached.
@@ -66,12 +76,14 @@ public final class Font extends Resource {
6676 */
6777Font (Device device ) {
6878 super (device );
79+ this .fontData = null ;
6980 this .zoom = DPIUtil .getNativeDeviceZoom ();
7081 this .fontHeight = 0 ;
7182}
7283
7384private Font (Device device , long handle , int zoom ) {
7485 super (device );
86+ this .fontData = null ;
7587 this .handle = handle ;
7688 this .zoom = zoom ;
7789 this .fontHeight = device .computePoints (fetchLogFontData (), handle , zoom );
@@ -99,16 +111,18 @@ private Font(Device device, long handle, int zoom) {
99111 */
100112public Font (Device device , FontData fd ) {
101113 super (device );
114+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
102115 this .zoom = DPIUtil .getNativeDeviceZoom ();
103- init (fd );
116+ this . fontData = new FontData (fd . toString () );
104117 this .fontHeight = fd .height ;
105118 init ();
106119}
107120
108121private Font (Device device , FontData fd , int zoom ) {
109122 super (device );
123+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
110124 this .zoom = zoom ;
111- init (fd );
125+ this . fontData = new FontData (fd . toString () );
112126 this .fontHeight = fd .height ;
113127 init ();
114128}
@@ -147,7 +161,7 @@ public Font(Device device, FontData[] fds) {
147161 }
148162 this .zoom = DPIUtil .getNativeDeviceZoom ();
149163 FontData fd = fds [0 ];
150- init ( fds [ 0 ] );
164+ this . fontData = new FontData ( fd . toString () );
151165 this .fontHeight = fd .height ;
152166 init ();
153167}
@@ -180,8 +194,7 @@ public Font(Device device, String name, int height, int style) {
180194 super (device );
181195 if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
182196 this .zoom = DPIUtil .getNativeDeviceZoom ();
183- FontData fd = new FontData (name , height , style );
184- init (fd );
197+ this .fontData = new FontData (name , height , style );
185198 this .fontHeight = height ;
186199 init ();
187200}
@@ -190,15 +203,15 @@ public Font(Device device, String name, int height, int style) {
190203 super (device );
191204 if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
192205 this .zoom = DPIUtil .getNativeDeviceZoom ();
193- FontData fd = new FontData (name , height , style );
194- init (fd );
206+ this .fontData = new FontData (name , height , style );
195207 this .fontHeight = height ;
196208 init ();
197209}
198210@ Override
199211void destroy () {
200212 OS .DeleteObject (handle );
201213 handle = 0 ;
214+ isDestroyed = true ;
202215}
203216
204217/**
@@ -216,7 +229,7 @@ public boolean equals(Object object) {
216229 if (object == this ) return true ;
217230 if (!(object instanceof Font )) return false ;
218231 Font font = (Font ) object ;
219- return device == font .device && handle == font . handle ;
232+ return device == font .device && win32_getHandle ( this ) == win32_getHandle ( font ) ;
220233}
221234
222235/**
@@ -239,7 +252,7 @@ public FontData[] getFontData() {
239252
240253private LOGFONT fetchLogFontData () {
241254 LOGFONT logFont = new LOGFONT ();
242- OS .GetObject (handle , LOGFONT .sizeof , logFont );
255+ OS .GetObject (win32_getHandle ( this ) , LOGFONT .sizeof , logFont );
243256 return logFont ;
244257}
245258
@@ -255,7 +268,7 @@ private LOGFONT fetchLogFontData() {
255268 */
256269@ Override
257270public int hashCode () {
258- return (int )handle ;
271+ return (int ) win32_getHandle ( this ) ;
259272}
260273
261274void init (FontData fd ) {
@@ -280,7 +293,7 @@ void init (FontData fd) {
280293 */
281294@ Override
282295public boolean isDisposed () {
283- return handle == 0 ;
296+ return isDestroyed ;
284297}
285298
286299/**
@@ -302,7 +315,7 @@ public String toString () {
302315 * available on all platforms, and should never be called from
303316 * application code.
304317 *
305- * Creates a new handle for the requested font
318+ * Creates a new handle for the requested font or return the existing one
306319 *
307320 * @param font the font to get the handle of
308321 *
@@ -311,7 +324,10 @@ public String toString () {
311324 * @noreference This method is not intended to be referenced by clients.
312325 */
313326public static long win32_getHandle (Font font ) {
314- return font .handle ;
327+ if (font .handle == 0 && font .fontData != null ) {
328+ font .init (font .fontData );
329+ }
330+ return font .handle ;
315331}
316332
317333/**
0 commit comments