From 35c3b173892676373ef2403fb94935ed426c10b1 Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Mon, 31 Mar 2025 14:49:03 +0200 Subject: [PATCH] [win32] Fetch Font#handle through SWTFontProvider This commit replaces several calls to Font#handle to use the SWTFontProvider instead. This assures the most Font reusage possible. --- .../win32/org/eclipse/swt/graphics/GC.java | 14 +++++++------- .../win32/org/eclipse/swt/graphics/Path.java | 2 +- .../org/eclipse/swt/graphics/TextLayout.java | 14 +++++++------- .../eclipse/swt/internal/SWTFontProvider.java | 16 ++++++++++++++++ .../win32/org/eclipse/swt/widgets/Caret.java | 2 +- .../win32/org/eclipse/swt/widgets/Composite.java | 2 +- .../win32/org/eclipse/swt/widgets/Control.java | 7 +++---- .../win32/org/eclipse/swt/widgets/ExpandBar.java | 2 +- .../win32/org/eclipse/swt/widgets/TableItem.java | 4 ++-- .../win32/org/eclipse/swt/widgets/TreeItem.java | 4 ++-- 10 files changed, 41 insertions(+), 26 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java index 0fe325f075e..fd4035c5292 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java @@ -333,10 +333,10 @@ void checkGC(int mask) { } } if ((state & FONT) != 0) { - Font font = data.font; - OS.SelectObject(handle, font.handle); + long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + OS.SelectObject(handle, fontHandle); long[] hFont = new long[1]; - long gdipFont = createGdipFont(handle, font.handle, gdipGraphics, device.fontCollection, null, hFont); + long gdipFont = createGdipFont(handle, fontHandle, gdipGraphics, device.fontCollection, null, hFont); if (hFont[0] != 0) OS.SelectObject(handle, hFont[0]); if (data.hGDIFont != 0) OS.DeleteObject(data.hGDIFont); data.hGDIFont = hFont[0]; @@ -454,8 +454,8 @@ void checkGC(int mask) { OS.SetTextColor(handle, data.foreground); } if ((state & FONT) != 0) { - Font font = data.font; - OS.SelectObject(handle, font.handle); + long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + OS.SelectObject(handle, fontHandle); } } @@ -2388,7 +2388,7 @@ void drawText(long gdipGraphics, String string, int x, int y, int flags, Point s char[] chars = string.toCharArray(); long hdc = Gdip.Graphics_GetHDC(gdipGraphics); long hFont = data.hGDIFont; - if (hFont == 0 && data.font != null) hFont = data.font.handle; + if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); long oldFont = 0; if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont); TEXTMETRIC lptm = new TEXTMETRIC(); @@ -2478,7 +2478,7 @@ RectF drawText(long gdipGraphics, char[] buffer, int start, int length, int x, i } long hdc = Gdip.Graphics_GetHDC(gdipGraphics); long hFont = data.hGDIFont; - if (hFont == 0 && data.font != null) hFont = data.font.handle; + if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); long oldFont = 0; if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont); if (start != 0) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java index 799300b963d..99f44be4d7f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java @@ -763,7 +763,7 @@ private void addStringInPixels(PathHandle pathHandle, float x, float y) { char[] buffer = string.toCharArray(); long hDC = device.internal_new_GC(null); long [] family = new long [1]; - long gdipFont = GC.createGdipFont(hDC, SWTFontProvider.getFont(device, this.fontData, zoom).handle, 0, device.fontCollection, family, null); + long gdipFont = GC.createGdipFont(hDC, SWTFontProvider.getFontHandle(device, this.fontData, zoom), 0, device.fontCollection, family, null); PointF point = new PointF(); point.X = x - (Gdip.Font_GetSize(gdipFont) / 6); point.Y = y; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java index 0db4dc40e3a..f56955adfc1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java @@ -1969,12 +1969,12 @@ long getItemFont (StyleItem item, GC gc) { if (item.fallbackFont != 0) return item.fallbackFont; final int zoom = getNativeZoom(gc); if (item.style != null && item.style.font != null) { - return Font.win32_new(item.style.font, zoom).handle; + return SWTFontProvider.getFontHandle(item.style.font, zoom); } if (this.font != null) { - return Font.win32_new(this.font, zoom).handle; + return SWTFontProvider.getFontHandle(this.font, zoom); } - return SWTFontProvider.getSystemFont(device, zoom).handle; + return SWTFontProvider.getSystemFontHandle(device, zoom); } /** @@ -2131,15 +2131,15 @@ public FontMetrics getLineMetrics (int lineIndex) { long hDC = device.internal_new_GC(null); long srcHdc = OS.CreateCompatibleDC(hDC); TEXTMETRIC lptm = new TEXTMETRIC(); - Font availableFont = font != null ? font : device.systemFont; - OS.SelectObject(srcHdc, availableFont.handle); + final int zoom = getZoom(); + long availableFont = font != null ? SWTFontProvider.getFontHandle(font, zoom) : SWTFontProvider.getSystemFontHandle(device, zoom); + OS.SelectObject(srcHdc, availableFont); metricsAdapter.GetTextMetrics(srcHdc, lptm); OS.DeleteDC(srcHdc); device.internal_dispose_GC(hDC, null); - final int zoom = getZoom(); int ascentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmAscent, zoom), this.ascent); int descentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmDescent, zoom), this.descent); - int leadingInPoints = DPIUtil.scaleDown(this.device, lptm.tmInternalLeading, availableFont.zoom); + int leadingInPoints = DPIUtil.scaleDown(this.device, lptm.tmInternalLeading, zoom); if (text.length() != 0) { for (StyleItem run : runs[lineIndex]) { if (run.ascentInPoints > ascentInPoints) { 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 9588982ed75..f7217445871 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 @@ -16,6 +16,7 @@ import java.util.*; import java.util.concurrent.*; +import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; @@ -46,6 +47,10 @@ public static Font getSystemFont(Device device, int zoom) { return getFontRegistry(device).getSystemFont(zoom); } + public static long getSystemFontHandle(Device device, int zoom) { + return getSystemFont(device, zoom).handle; + } + /** * Returns the font with the given font data for the given device at the * specified zoom. @@ -61,6 +66,17 @@ public static Font getFont(Device device, FontData fontData, int zoom) { return getFontRegistry(device).getFont(fontData, zoom); } + public static long getFontHandle(Device device, FontData fontData, int zoom) { + return getFont(device, fontData, zoom).handle; + } + + 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; + } + /** * Returns the font with the given fontHandle for the given device at the * specified zoom. diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java index 44e6175eddb..ade9c8d87e2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java @@ -509,7 +509,7 @@ public void setImage (Image image) { void setIMEFont () { if (!OS.IsDBLocale) return; long hFont = 0; - if (font != null) hFont = font.handle; + if (font != null) hFont = SWTFontProvider.getFontHandle(font, getNativeZoom()); if (hFont == 0) hFont = defaultFont (); long hwnd = parent.handle; long hIMC = OS.ImmGetContext (hwnd); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java index 1d727031dcc..ba8088a9249 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java @@ -1419,7 +1419,7 @@ LRESULT WM_GETFONT (long wParam, long lParam) { if (result != null) return result; long code = callWindowProc (handle, OS.WM_GETFONT, wParam, lParam); if (code != 0) return new LRESULT (code); - return new LRESULT (font != null ? font.handle : defaultFont ()); + return new LRESULT (font != null ? SWTFontProvider.getFontHandle(font, getNativeZoom()) : defaultFont ()); } @Override 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 c7617eeb70a..bb9e8fe3ca5 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 @@ -721,7 +721,7 @@ int defaultBackground () { } long defaultFont() { - return display.getSystemFont(getNativeZoom()).handle; + return SWTFontProvider.getSystemFontHandle(display, getNativeZoom()); } int defaultForeground () { @@ -3334,7 +3334,7 @@ public void setCursor (Cursor cursor) { } void setDefaultFont () { - long hFont = display.getSystemFont (getNativeZoom()).handle; + long hFont = SWTFontProvider.getSystemFontHandle(display, getNativeZoom()); OS.SendMessage (handle, OS.WM_SETFONT, hFont, 0); } @@ -5894,8 +5894,7 @@ private static void resizeFont(Control control, int newZoom) { if (font == null) { long currentFontHandle = OS.SendMessage (control.handle, OS.WM_GETFONT, 0, 0); if (currentFontHandle != 0) { - Font newFont = display.getSystemFont(newZoom); - long newFontHandle = newFont.handle; + long newFontHandle = SWTFontProvider.getSystemFontHandle(display, newZoom); OS.SendMessage(control.handle, OS.WM_SETFONT, newFontHandle, 1); } } else { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java index 6185d628325..f51f0406187 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java @@ -509,7 +509,7 @@ void setBackgroundPixel (int pixel) { @Override public void setFont (Font font) { super.setFont (font); - hFont = font != null ? font.handle : 0; + hFont = font != null ? SWTFontProvider.getFontHandle(font, getNativeZoom()) : 0; layoutItems (0, true); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java index 157bd962cb7..216f75f6f7c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java @@ -157,8 +157,8 @@ void destroyWidget () { } long fontHandle (int index) { - if (cellFont != null && cellFont [index] != null) return cellFont [index].handle; - if (font != null) return font.handle; + if (cellFont != null && cellFont [index] != null) return SWTFontProvider.getFontHandle(cellFont[index], getNativeZoom()); + if (font != null) return SWTFontProvider.getFontHandle(font, getNativeZoom()); return -1; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java index 022ee7b1bd0..1d7b8daf7c6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java @@ -316,8 +316,8 @@ void destroyWidget () { } long fontHandle (int index) { - if (cellFont != null && cellFont [index] != null) return cellFont [index].handle; - if (font != null) return font.handle; + if (cellFont != null && cellFont [index] != null) return SWTFontProvider.getFontHandle(cellFont[index], getNativeZoom()); + if (font != null) return SWTFontProvider.getFontHandle(font, getNativeZoom()); return -1; }