diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java index 95b51c0c69a..8f625ed698f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Font.java @@ -46,9 +46,8 @@ public final class Font extends Resource { * platforms and should never be accessed from application code. *

* - * @noreference This field is not intended to be referenced by clients. */ - public long handle; + private long handle; /** * The zoom in % of the standard resolution used for conversion of point height to pixel height @@ -56,6 +55,16 @@ public final class Font extends Resource { */ int zoom; + /** + * this field is used to mark destroyed fonts + */ + private boolean isDestroyed; + + /** + * this field is used to store fontData provided during initialization + */ + private final FontData fontData; + /** * Font height in points. As the conversion to pixel height involves rounding the fontHeight must * be cached. @@ -64,6 +73,7 @@ public final class Font extends Resource { private Font(Device device, long handle, int zoom) { super(device); + this.fontData = null; this.handle = handle; this.zoom = zoom; this.fontHeight = device.computePoints(fetchLogFontData(), handle, zoom); @@ -91,16 +101,18 @@ private Font(Device device, long handle, int zoom) { */ public Font(Device device, FontData fd) { super(device); + if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.zoom = DPIUtil.getNativeDeviceZoom(); - init(fd); + this.fontData = new FontData(fd.toString()); this.fontHeight = fd.height; init(); } private Font(Device device, FontData fd, int zoom) { super(device); + if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.zoom = zoom; - init(fd); + this.fontData = new FontData(fd.toString()); this.fontHeight = fd.height; init(); } @@ -139,7 +151,7 @@ public Font(Device device, FontData[] fds) { } this.zoom = DPIUtil.getNativeDeviceZoom(); FontData fd = fds[0]; - init(fds[0]); + this.fontData = new FontData(fd.toString()); this.fontHeight = fd.height; init(); } @@ -172,7 +184,7 @@ public Font(Device device, String name, int height, int style) { super(device); if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.zoom = DPIUtil.getNativeDeviceZoom(); - init(new FontData (name, height, style)); + this.fontData = new FontData (name, height, style); this.fontHeight = height; init(); } @@ -181,7 +193,7 @@ public Font(Device device, String name, int height, int style) { super(device); if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.zoom = DPIUtil.getNativeDeviceZoom(); - init(new FontData (name, height, style)); + this.fontData = new FontData (name, height, style); this.fontHeight = height; init(); } @@ -189,6 +201,7 @@ public Font(Device device, String name, int height, int style) { void destroy() { OS.DeleteObject(handle); handle = 0; + isDestroyed = true; } /** @@ -206,7 +219,7 @@ public boolean equals(Object object) { if (object == this) return true; if (!(object instanceof Font)) return false; Font font = (Font) object; - return device == font.device && handle == font.handle; + return device == font.device && win32_getHandle(this) == win32_getHandle(font); } /** @@ -229,7 +242,7 @@ public FontData[] getFontData() { private LOGFONT fetchLogFontData() { LOGFONT logFont = new LOGFONT (); - OS.GetObject(handle, LOGFONT.sizeof, logFont); + OS.GetObject(win32_getHandle(this), LOGFONT.sizeof, logFont); return logFont; } @@ -245,7 +258,7 @@ private LOGFONT fetchLogFontData() { */ @Override public int hashCode () { - return (int)handle; + return (int) win32_getHandle(this); } void init (FontData fd) { @@ -270,7 +283,7 @@ void init (FontData fd) { */ @Override public boolean isDisposed() { - return handle == 0; + return isDestroyed; } /** @@ -285,6 +298,26 @@ public String toString () { return "Font {" + handle + "}"; } +/** + * Creates or returns a handle for the requested font. + *

+ * IMPORTANT: This method is not part of the public API for + * Font. It is marked public only so that it can be shared within + * the packages provided by SWT. It is not available on all platforms, and + * should never be called from application code. + * + * @param font the font to get the handle of + * @return handle of the font + * + * @noreference This method is not intended to be referenced by clients. + */ +public static long win32_getHandle(Font font) { + if (font.handle == 0 && font.fontData != null) { + font.init(font.fontData); + } + return font.handle; +} + /** * Invokes platform specific functionality to allocate a new font. *

diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java index f7217445871..4901a2e01f0 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java @@ -48,7 +48,7 @@ public static Font getSystemFont(Device device, int zoom) { } public static long getSystemFontHandle(Device device, int zoom) { - return getSystemFont(device, zoom).handle; + return Font.win32_getHandle(getSystemFont(device, zoom)); } /** @@ -67,14 +67,14 @@ public static Font getFont(Device device, FontData fontData, int zoom) { } public static long getFontHandle(Device device, FontData fontData, int zoom) { - return getFont(device, fontData, zoom).handle; + return Font.win32_getHandle(getFont(device, fontData, zoom)); } public static long getFontHandle(Font font, int zoom) { if (font == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } - return getFont(font.getDevice(), font.getFontData()[0], zoom).handle; + return Font.win32_getHandle(getFont(font.getDevice(), font.getFontData()[0], zoom)); } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java index 55dc435c004..fcd71786440 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ScalingSWTFontRegistry.java @@ -44,7 +44,7 @@ private Font getScaledFont(int zoom) { private Font createAndCacheFont(int zoom) { Font newFont = createFont(zoom); - customFontHandlesKeyMap.put(newFont.handle, this); + customFontHandlesKeyMap.put(Font.win32_getHandle(newFont), this); scaledFonts.put(zoom, newFont); return newFont; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java index 7d4ef4988ea..9c2b0d25393 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java @@ -3439,7 +3439,7 @@ public void setFont (Font font) { long hFont = 0; if (newFont != null) { if (newFont.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); - hFont = newFont.handle; + hFont = Font.win32_getHandle(newFont); } this.font = newFont; if (hFont == 0) hFont = defaultFont ();