Skip to content

Commit ddd1862

Browse files
akoch-yattafedejeanne
authored andcommitted
[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.
1 parent 807b159 commit ddd1862

File tree

10 files changed

+41
-26
lines changed

10 files changed

+41
-26
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ void checkGC(int mask) {
333333
}
334334
}
335335
if ((state & FONT) != 0) {
336-
Font font = data.font;
337-
OS.SelectObject(handle, font.handle);
336+
long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
337+
OS.SelectObject(handle, fontHandle);
338338
long[] hFont = new long[1];
339-
long gdipFont = createGdipFont(handle, font.handle, gdipGraphics, device.fontCollection, null, hFont);
339+
long gdipFont = createGdipFont(handle, fontHandle, gdipGraphics, device.fontCollection, null, hFont);
340340
if (hFont[0] != 0) OS.SelectObject(handle, hFont[0]);
341341
if (data.hGDIFont != 0) OS.DeleteObject(data.hGDIFont);
342342
data.hGDIFont = hFont[0];
@@ -454,8 +454,8 @@ void checkGC(int mask) {
454454
OS.SetTextColor(handle, data.foreground);
455455
}
456456
if ((state & FONT) != 0) {
457-
Font font = data.font;
458-
OS.SelectObject(handle, font.handle);
457+
long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
458+
OS.SelectObject(handle, fontHandle);
459459
}
460460
}
461461

@@ -2388,7 +2388,7 @@ void drawText(long gdipGraphics, String string, int x, int y, int flags, Point s
23882388
char[] chars = string.toCharArray();
23892389
long hdc = Gdip.Graphics_GetHDC(gdipGraphics);
23902390
long hFont = data.hGDIFont;
2391-
if (hFont == 0 && data.font != null) hFont = data.font.handle;
2391+
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
23922392
long oldFont = 0;
23932393
if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont);
23942394
TEXTMETRIC lptm = new TEXTMETRIC();
@@ -2478,7 +2478,7 @@ RectF drawText(long gdipGraphics, char[] buffer, int start, int length, int x, i
24782478
}
24792479
long hdc = Gdip.Graphics_GetHDC(gdipGraphics);
24802480
long hFont = data.hGDIFont;
2481-
if (hFont == 0 && data.font != null) hFont = data.font.handle;
2481+
if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom);
24822482
long oldFont = 0;
24832483
if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont);
24842484
if (start != 0) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ private void addStringInPixels(PathHandle pathHandle, float x, float y) {
763763
char[] buffer = string.toCharArray();
764764
long hDC = device.internal_new_GC(null);
765765
long [] family = new long [1];
766-
long gdipFont = GC.createGdipFont(hDC, SWTFontProvider.getFont(device, this.fontData, zoom).handle, 0, device.fontCollection, family, null);
766+
long gdipFont = GC.createGdipFont(hDC, SWTFontProvider.getFontHandle(device, this.fontData, zoom), 0, device.fontCollection, family, null);
767767
PointF point = new PointF();
768768
point.X = x - (Gdip.Font_GetSize(gdipFont) / 6);
769769
point.Y = y;

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1969,12 +1969,12 @@ long getItemFont (StyleItem item, GC gc) {
19691969
if (item.fallbackFont != 0) return item.fallbackFont;
19701970
final int zoom = getNativeZoom(gc);
19711971
if (item.style != null && item.style.font != null) {
1972-
return Font.win32_new(item.style.font, zoom).handle;
1972+
return SWTFontProvider.getFontHandle(item.style.font, zoom);
19731973
}
19741974
if (this.font != null) {
1975-
return Font.win32_new(this.font, zoom).handle;
1975+
return SWTFontProvider.getFontHandle(this.font, zoom);
19761976
}
1977-
return SWTFontProvider.getSystemFont(device, zoom).handle;
1977+
return SWTFontProvider.getSystemFontHandle(device, zoom);
19781978
}
19791979

19801980
/**
@@ -2131,15 +2131,15 @@ public FontMetrics getLineMetrics (int lineIndex) {
21312131
long hDC = device.internal_new_GC(null);
21322132
long srcHdc = OS.CreateCompatibleDC(hDC);
21332133
TEXTMETRIC lptm = new TEXTMETRIC();
2134-
Font availableFont = font != null ? font : device.systemFont;
2135-
OS.SelectObject(srcHdc, availableFont.handle);
2134+
final int zoom = getZoom();
2135+
long availableFont = font != null ? SWTFontProvider.getFontHandle(font, zoom) : SWTFontProvider.getSystemFontHandle(device, zoom);
2136+
OS.SelectObject(srcHdc, availableFont);
21362137
metricsAdapter.GetTextMetrics(srcHdc, lptm);
21372138
OS.DeleteDC(srcHdc);
21382139
device.internal_dispose_GC(hDC, null);
2139-
final int zoom = getZoom();
21402140
int ascentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmAscent, zoom), this.ascent);
21412141
int descentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmDescent, zoom), this.descent);
2142-
int leadingInPoints = DPIUtil.scaleDown(this.device, lptm.tmInternalLeading, availableFont.zoom);
2142+
int leadingInPoints = DPIUtil.scaleDown(this.device, lptm.tmInternalLeading, zoom);
21432143
if (text.length() != 0) {
21442144
for (StyleItem run : runs[lineIndex]) {
21452145
if (run.ascentInPoints > ascentInPoints) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.*;
1717
import java.util.concurrent.*;
1818

19+
import org.eclipse.swt.*;
1920
import org.eclipse.swt.graphics.*;
2021
import org.eclipse.swt.widgets.*;
2122

@@ -46,6 +47,10 @@ public static Font getSystemFont(Device device, int zoom) {
4647
return getFontRegistry(device).getSystemFont(zoom);
4748
}
4849

50+
public static long getSystemFontHandle(Device device, int zoom) {
51+
return getSystemFont(device, zoom).handle;
52+
}
53+
4954
/**
5055
* Returns the font with the given font data for the given device at the
5156
* specified zoom.
@@ -61,6 +66,17 @@ public static Font getFont(Device device, FontData fontData, int zoom) {
6166
return getFontRegistry(device).getFont(fontData, zoom);
6267
}
6368

69+
public static long getFontHandle(Device device, FontData fontData, int zoom) {
70+
return getFont(device, fontData, zoom).handle;
71+
}
72+
73+
public static long getFontHandle(Font font, int zoom) {
74+
if (font == null) {
75+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
76+
}
77+
return getFont(font.getDevice(), font.getFontData()[0], zoom).handle;
78+
}
79+
6480
/**
6581
* Returns the font with the given fontHandle for the given device at the
6682
* specified zoom.

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Caret.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public void setImage (Image image) {
509509
void setIMEFont () {
510510
if (!OS.IsDBLocale) return;
511511
long hFont = 0;
512-
if (font != null) hFont = font.handle;
512+
if (font != null) hFont = SWTFontProvider.getFontHandle(font, getNativeZoom());
513513
if (hFont == 0) hFont = defaultFont ();
514514
long hwnd = parent.handle;
515515
long hIMC = OS.ImmGetContext (hwnd);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ LRESULT WM_GETFONT (long wParam, long lParam) {
14191419
if (result != null) return result;
14201420
long code = callWindowProc (handle, OS.WM_GETFONT, wParam, lParam);
14211421
if (code != 0) return new LRESULT (code);
1422-
return new LRESULT (font != null ? font.handle : defaultFont ());
1422+
return new LRESULT (font != null ? SWTFontProvider.getFontHandle(font, getNativeZoom()) : defaultFont ());
14231423
}
14241424

14251425
@Override

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ int defaultBackground () {
721721
}
722722

723723
long defaultFont() {
724-
return display.getSystemFont(getNativeZoom()).handle;
724+
return SWTFontProvider.getSystemFontHandle(display, getNativeZoom());
725725
}
726726

727727
int defaultForeground () {
@@ -3334,7 +3334,7 @@ public void setCursor (Cursor cursor) {
33343334
}
33353335

33363336
void setDefaultFont () {
3337-
long hFont = display.getSystemFont (getNativeZoom()).handle;
3337+
long hFont = SWTFontProvider.getSystemFontHandle(display, getNativeZoom());
33383338
OS.SendMessage (handle, OS.WM_SETFONT, hFont, 0);
33393339
}
33403340

@@ -5894,8 +5894,7 @@ private static void resizeFont(Control control, int newZoom) {
58945894
if (font == null) {
58955895
long currentFontHandle = OS.SendMessage (control.handle, OS.WM_GETFONT, 0, 0);
58965896
if (currentFontHandle != 0) {
5897-
Font newFont = display.getSystemFont(newZoom);
5898-
long newFontHandle = newFont.handle;
5897+
long newFontHandle = SWTFontProvider.getSystemFontHandle(display, newZoom);
58995898
OS.SendMessage(control.handle, OS.WM_SETFONT, newFontHandle, 1);
59005899
}
59015900
} else {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ void setBackgroundPixel (int pixel) {
509509
@Override
510510
public void setFont (Font font) {
511511
super.setFont (font);
512-
hFont = font != null ? font.handle : 0;
512+
hFont = font != null ? SWTFontProvider.getFontHandle(font, getNativeZoom()) : 0;
513513
layoutItems (0, true);
514514
}
515515

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TableItem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ void destroyWidget () {
157157
}
158158

159159
long fontHandle (int index) {
160-
if (cellFont != null && cellFont [index] != null) return cellFont [index].handle;
161-
if (font != null) return font.handle;
160+
if (cellFont != null && cellFont [index] != null) return SWTFontProvider.getFontHandle(cellFont[index], getNativeZoom());
161+
if (font != null) return SWTFontProvider.getFontHandle(font, getNativeZoom());
162162
return -1;
163163
}
164164

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TreeItem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ void destroyWidget () {
316316
}
317317

318318
long fontHandle (int index) {
319-
if (cellFont != null && cellFont [index] != null) return cellFont [index].handle;
320-
if (font != null) return font.handle;
319+
if (cellFont != null && cellFont [index] != null) return SWTFontProvider.getFontHandle(cellFont[index], getNativeZoom());
320+
if (font != null) return SWTFontProvider.getFontHandle(font, getNativeZoom());
321321
return -1;
322322
}
323323

0 commit comments

Comments
 (0)