@@ -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.
@@ -63,6 +73,7 @@ public final class Font extends Resource {
6373
6474private Font (Device device , long handle , int zoom ) {
6575 super (device );
76+ this .fontData = null ;
6677 this .handle = handle ;
6778 this .zoom = zoom ;
6879 this .fontHeight = device .computePoints (fetchLogFontData (), handle , zoom );
@@ -90,16 +101,18 @@ private Font(Device device, long handle, int zoom) {
90101 */
91102public Font (Device device , FontData fd ) {
92103 super (device );
104+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
93105 this .zoom = DPIUtil .getNativeDeviceZoom ();
94- init (fd );
106+ this . fontData = new FontData (fd . toString () );
95107 this .fontHeight = fd .height ;
96108 init ();
97109}
98110
99111private Font (Device device , FontData fd , int zoom ) {
100112 super (device );
113+ if (fd == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
101114 this .zoom = zoom ;
102- init (fd );
115+ this . fontData = new FontData (fd . toString () );
103116 this .fontHeight = fd .height ;
104117 init ();
105118}
@@ -138,7 +151,7 @@ public Font(Device device, FontData[] fds) {
138151 }
139152 this .zoom = DPIUtil .getNativeDeviceZoom ();
140153 FontData fd = fds [0 ];
141- init ( fds [ 0 ] );
154+ this . fontData = new FontData ( fd . toString () );
142155 this .fontHeight = fd .height ;
143156 init ();
144157}
@@ -171,8 +184,7 @@ public Font(Device device, String name, int height, int style) {
171184 super (device );
172185 if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
173186 this .zoom = DPIUtil .getNativeDeviceZoom ();
174- FontData fd = new FontData (name , height , style );
175- init (fd );
187+ this .fontData = new FontData (name , height , style );
176188 this .fontHeight = height ;
177189 init ();
178190}
@@ -181,15 +193,15 @@ public Font(Device device, String name, int height, int style) {
181193 super (device );
182194 if (name == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
183195 this .zoom = DPIUtil .getNativeDeviceZoom ();
184- FontData fd = new FontData (name , height , style );
185- init (fd );
196+ this .fontData = new FontData (name , height , style );
186197 this .fontHeight = height ;
187198 init ();
188199}
189200@ Override
190201void destroy () {
191202 OS .DeleteObject (handle );
192203 handle = 0 ;
204+ isDestroyed = true ;
193205}
194206
195207/**
@@ -207,7 +219,7 @@ public boolean equals(Object object) {
207219 if (object == this ) return true ;
208220 if (!(object instanceof Font )) return false ;
209221 Font font = (Font ) object ;
210- return device == font .device && handle == font . handle ;
222+ return device == font .device && win32_getHandle ( this ) == win32_getHandle ( font ) ;
211223}
212224
213225/**
@@ -230,7 +242,7 @@ public FontData[] getFontData() {
230242
231243private LOGFONT fetchLogFontData () {
232244 LOGFONT logFont = new LOGFONT ();
233- OS .GetObject (handle , LOGFONT .sizeof , logFont );
245+ OS .GetObject (win32_getHandle ( this ) , LOGFONT .sizeof , logFont );
234246 return logFont ;
235247}
236248
@@ -246,7 +258,7 @@ private LOGFONT fetchLogFontData() {
246258 */
247259@ Override
248260public int hashCode () {
249- return (int )handle ;
261+ return (int ) win32_getHandle ( this ) ;
250262}
251263
252264void init (FontData fd ) {
@@ -271,7 +283,7 @@ void init (FontData fd) {
271283 */
272284@ Override
273285public boolean isDisposed () {
274- return handle == 0 ;
286+ return isDestroyed ;
275287}
276288
277289/**
@@ -300,6 +312,9 @@ public String toString () {
300312 * @noreference This method is not intended to be referenced by clients.
301313 */
302314public static long win32_getHandle (Font font ) {
315+ if (font .handle == 0 && font .fontData != null ) {
316+ font .init (font .fontData );
317+ }
303318 return font .handle ;
304319}
305320
0 commit comments