Skip to content

Create Font handles on demand #2087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,25 @@ public final class Font extends Resource {
* platforms and should never be accessed from application code.
* </p>
*
* @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
* (Warning: This field is platform dependent)
*/
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.
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -181,14 +193,15 @@ 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();
}
@Override
void destroy() {
OS.DeleteObject(handle);
handle = 0;
isDestroyed = true;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

Expand All @@ -245,7 +258,7 @@ private LOGFONT fetchLogFontData() {
*/
@Override
public int hashCode () {
return (int)handle;
return (int) win32_getHandle(this);
}

void init (FontData fd) {
Expand All @@ -270,7 +283,7 @@ void init (FontData fd) {
*/
@Override
public boolean isDisposed() {
return handle == 0;
return isDestroyed;
}

/**
Expand All @@ -285,6 +298,26 @@ public String toString () {
return "Font {" + handle + "}";
}

/**
* Creates or returns a handle for the requested font.
* <p>
* <b>IMPORTANT:</b> This method is not part of the public API for
* <code>Font</code>. 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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
Loading