Skip to content

Commit 7b46268

Browse files
committed
[Win32] Properly handle setting null font to GC #2350
When calling GC#setFont(), a copy of the given font is created. However, the font may be null, leading to an exception. In addition, a font should be retrieved from the font provider instead of creating a new font that is manually disposed later on. This change ensures that null fonts are properly handled and that the stored font is retrieved from the provider. Fixes #2350
1 parent a68fd8d commit 7b46268

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,8 +5015,7 @@ private class SetFontOperation extends Operation {
50155015
private final Font font;
50165016

50175017
SetFontOperation(Font font) {
5018-
this.font = new Font(font.getDevice(), font.getFontData());
5019-
registerForDisposal(this.font);
5018+
this.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], data.nativeZoom) : null;
50205019
}
50215020

50225021
@Override

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,9 @@ public void test_setClippingLorg_eclipse_swt_graphics_Rectangle() {
605605

606606
@Test
607607
public void test_setFontLorg_eclipse_swt_graphics_Font() {
608+
gc.setFont(null);
609+
assertEquals(shell.getDisplay().getSystemFont(), gc.getFont());
610+
608611
gc.setFont(shell.getDisplay().getSystemFont());
609612
Font font = gc.getFont();
610613
assertEquals(shell.getDisplay().getSystemFont(), font);

0 commit comments

Comments
 (0)