Skip to content

Commit 97ebf99

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 97ebf99

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

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

Lines changed: 20 additions & 7 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);
@@ -100,15 +112,15 @@ private Font(Device device, long handle, int zoom) {
100112
public Font(Device device, FontData fd) {
101113
super(device);
102114
this.zoom = DPIUtil.getNativeDeviceZoom();
103-
init(fd);
115+
this.fontData = new FontData(fd.toString());
104116
this.fontHeight = fd.height;
105117
init();
106118
}
107119

108120
private Font(Device device, FontData fd, int zoom) {
109121
super(device);
110122
this.zoom = zoom;
111-
init(fd);
123+
this.fontData = new FontData(fd.toString());
112124
this.fontHeight = fd.height;
113125
init();
114126
}
@@ -147,7 +159,7 @@ public Font(Device device, FontData[] fds) {
147159
}
148160
this.zoom = DPIUtil.getNativeDeviceZoom();
149161
FontData fd = fds[0];
150-
init(fds[0]);
162+
this.fontData = new FontData(fd.toString());
151163
this.fontHeight = fd.height;
152164
init();
153165
}
@@ -180,7 +192,7 @@ public Font(Device device, String name, int height, int style) {
180192
super(device);
181193
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
182194
this.zoom = DPIUtil.getNativeDeviceZoom();
183-
init(new FontData (name, height, style));
195+
this.fontData = new FontData (name, height, style);
184196
this.fontHeight = height;
185197
init();
186198
}
@@ -189,14 +201,15 @@ public Font(Device device, String name, int height, int style) {
189201
super(device);
190202
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
191203
this.zoom = DPIUtil.getNativeDeviceZoom();
192-
init(new FontData (name, height, style));
204+
this.fontData = new FontData (name, height, style);
193205
this.fontHeight = height;
194206
init();
195207
}
196208
@Override
197209
void destroy() {
198210
OS.DeleteObject(handle);
199211
handle = 0;
212+
isDestroyed = true;
200213
}
201214

202215
/**
@@ -237,7 +250,7 @@ public FontData[] getFontData() {
237250

238251
private LOGFONT fetchLogFontData() {
239252
LOGFONT logFont = new LOGFONT ();
240-
OS.GetObject(handle, LOGFONT.sizeof, logFont);
253+
OS.GetObject(win32_getHandle(this), LOGFONT.sizeof, logFont);
241254
return logFont;
242255
}
243256

@@ -278,7 +291,7 @@ void init (FontData fd) {
278291
*/
279292
@Override
280293
public boolean isDisposed() {
281-
return handle == 0;
294+
return isDestroyed;
282295
}
283296

284297
/**

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)