Skip to content

Commit e62c8ab

Browse files
ShahzaibIbrahimfedejeanne
authored andcommitted
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 52a5129 commit e62c8ab

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
6777
Font(Device device) {
6878
super(device);
79+
this.fontData = null;
6980
this.zoom = DPIUtil.getNativeDeviceZoom();
7081
this.fontHeight = 0;
7182
}
7283

7384
private 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
*/
100112
public 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

108121
private 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
197211
void 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

238253
private 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
255270
public 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
280296
public 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
*/
311327
public 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;

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)