Skip to content

Commit 8cdd4e4

Browse files
committed
[Win32] Consider SWT.DRAW_DELIMITER flag when drawing text with GDI+
The GC.drawText() implementation falls back to using GDI instead of GDI+ (even if enabled) in many cases for historic reasons. There were issues with rendering tabs back then. One of the few cases where GDI+ is used is when glyphs that are non existing for GDI are used. In that case, however, the DRAW_DELIMITER flag is not properly considered. The behavior is always as if the flag was enabled, i.e., line delimiters is always drawn even if the flag is not set. This change corrects the behavior of the GDI+ text drawing implementation to consider if the DRAW_DELIMITER flag is not set. In consequence, strings with glyphs that are not supported by GDI are now rendered without line breaks if undesired as per defined flags. Contributes to #3091
1 parent 41d3797 commit 8cdd4e4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,6 +3037,9 @@ private RectF drawText(long gdipGraphics, char[] buffer, int start, int length,
30373037
private void drawTextGDIP(long gdipGraphics, String string, int x, int y, int flags, boolean draw, Point size) {
30383038
boolean needsBounds = !draw || (flags & SWT.DRAW_TRANSPARENT) == 0;
30393039
char[] buffer;
3040+
if ((flags & SWT.DRAW_DELIMITER) == 0) {
3041+
string = string.replaceAll("[\\r\\n]+", "");
3042+
}
30403043
int length = string.length();
30413044
if (length != 0) {
30423045
buffer = string.toCharArray();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,18 @@ public void test_textExtentLjava_lang_StringI() {
954954
assertEquals(SWT.ERROR_GRAPHIC_DISPOSED, e.code);
955955
}
956956

957+
@Test
958+
public void test_textExtentLjava_lang_StringI_disabledLineDelimiter() {
959+
gc.setAdvanced(false);
960+
Point ptWithoutAdvanced = gc.textExtent("abc\u4E2D" + System.lineSeparator() + "def", 0);
961+
gc.setAdvanced(true);
962+
Point ptWithAdvanced = gc.textExtent("abc\u4E2D" + System.lineSeparator() + "def", 0);
963+
// Due to slightly different rendering, size must not be identical but similar in advanced/non-advanced mode
964+
assertTrue(Math.abs(ptWithAdvanced.x - ptWithoutAdvanced.x) <= 2);
965+
assertTrue(Math.abs(ptWithAdvanced.y - ptWithoutAdvanced.y) <= 2);
966+
gc.dispose();
967+
}
968+
957969
@Test
958970
public void test_toString() {
959971
String s = gc.toString();

0 commit comments

Comments
 (0)