@@ -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,7 +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- init ( new FontData (name , height , style ) );
197+ this . fontData = new FontData (name , height , style );
184198 this .fontHeight = height ;
185199 init ();
186200}
@@ -189,14 +203,15 @@ public Font(Device device, String name, int height, int style) {
189203 super (device );
190204 if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
191205 this .zoom = DPIUtil .getNativeDeviceZoom ();
192- init ( new FontData (name , height , style ) );
206+ this . fontData = new FontData (name , height , style );
193207 this .fontHeight = height ;
194208 init ();
195209}
196210@ Override
197211void destroy () {
198212 OS .DeleteObject (handle );
199213 handle = 0 ;
214+ isDestroyed = true ;
200215}
201216
202217/**
@@ -214,7 +229,7 @@ public boolean equals(Object object) {
214229 if (object == this ) return true ;
215230 if (!(object instanceof Font )) return false ;
216231 Font font = (Font ) object ;
217- return device == font .device && handle == font . handle ;
232+ return device == font .device && win32_getHandle ( this ) == win32_getHandle ( font ) ;
218233}
219234
220235/**
@@ -237,7 +252,7 @@ public FontData[] getFontData() {
237252
238253private LOGFONT fetchLogFontData () {
239254 LOGFONT logFont = new LOGFONT ();
240- OS .GetObject (handle , LOGFONT .sizeof , logFont );
255+ OS .GetObject (win32_getHandle ( this ) , LOGFONT .sizeof , logFont );
241256 return logFont ;
242257}
243258
@@ -253,10 +268,11 @@ private LOGFONT fetchLogFontData() {
253268 */
254269@ Override
255270public int hashCode () {
256- return (int )handle ;
271+ // return (int)handle;
272+ return (int ) win32_getHandle (this );
257273}
258274
259- void init (FontData fd ) {
275+ private void init (FontData fd ) {
260276 if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
261277 LOGFONT logFont = fd .data ;
262278 int lfHeight = logFont .lfHeight ;
@@ -278,7 +294,7 @@ void init (FontData fd) {
278294 */
279295@ Override
280296public boolean isDisposed () {
281- return handle == 0 ;
297+ return isDestroyed ;
282298}
283299
284300/**
@@ -309,7 +325,7 @@ public String toString () {
309325 * @noreference This method is not intended to be referenced by clients.
310326 */
311327public static long win32_getHandle (Font font ) {
312- if (font .handle == 0 ) {
328+ if (font .handle == 0 && font . fontData != null ) {
313329 font .init (font .fontData );
314330 }
315331 return font .handle ;
0 commit comments