From 2d3b49650c58a36385a27a222dbac747761efbdd Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Mon, 5 May 2025 19:31:40 +0200 Subject: [PATCH 1/2] [win32] Encapsulate Font handle with win32_getHandle The handle of Font is encapsulated and exposed only via static method win32_getHandle and direct access to the field is replaced by access to that method. --- .../win32/org/eclipse/swt/graphics/Font.java | 26 ++++++++++++++++--- .../eclipse/swt/internal/SWTFontProvider.java | 6 ++--- .../swt/internal/ScalingSWTFontRegistry.java | 2 +- .../org/eclipse/swt/widgets/Control.java | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) 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..02ba5ca3c79 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 @@ -172,7 +171,8 @@ 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)); + FontData fd = new FontData (name, height, style); + init(fd); this.fontHeight = height; init(); } @@ -181,7 +181,8 @@ 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)); + FontData fd = new FontData (name, height, style); + init(fd); this.fontHeight = height; init(); } @@ -285,6 +286,23 @@ 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) { + 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 (); From 1b0ba0cfd0b4d165770f30544181df7792cc2399 Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Mon, 5 May 2025 19:33:28 +0200 Subject: [PATCH 2/2] [win32] Create Font handles on demand Handles will no longer be created during initialization but upon first access to the handle via its accessor. Also the destroy condition is changed since handle being 0 doesn't mean that it was destroyed anymore. --- .../win32/org/eclipse/swt/graphics/Font.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) 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 02ba5ca3c79..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 @@ -55,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. @@ -63,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); @@ -90,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(); } @@ -138,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(); } @@ -171,8 +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(); - FontData fd = new FontData (name, height, style); - init(fd); + this.fontData = new FontData (name, height, style); this.fontHeight = height; init(); } @@ -181,8 +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(); - FontData fd = new FontData (name, height, style); - init(fd); + this.fontData = new FontData (name, height, style); this.fontHeight = height; init(); } @@ -190,6 +201,7 @@ public Font(Device device, String name, int height, int style) { void destroy() { OS.DeleteObject(handle); handle = 0; + isDestroyed = true; } /** @@ -207,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); } /** @@ -230,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; } @@ -246,7 +258,7 @@ private LOGFONT fetchLogFontData() { */ @Override public int hashCode () { - return (int)handle; + return (int) win32_getHandle(this); } void init (FontData fd) { @@ -271,7 +283,7 @@ void init (FontData fd) { */ @Override public boolean isDisposed() { - return handle == 0; + return isDestroyed; } /** @@ -300,6 +312,9 @@ public String toString () { * @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; }