From ded8838b4909a4b9df9c1576c1e5a1d05ea8d292 Mon Sep 17 00:00:00 2001 From: Tobias Melcher Date: Mon, 8 Jul 2024 16:06:18 +0200 Subject: [PATCH] fix boundary condition in StyledText#getTextBounds --- .../org/eclipse/swt/custom/StyledText.java | 5 +++- ...est_org_eclipse_swt_custom_StyledText.java | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java index 335acfb8bfb..c0c6c7e1e72 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java @@ -4892,7 +4892,7 @@ public Rectangle getTextBounds(int start, int end) { Rectangle rect; int y = getLinePixel(lineStart); int height = 0; - int left = 0x7fffffff, right = 0; + int left = Integer.MAX_VALUE, right = 0; for (int i = lineStart; i <= lineEnd; i++) { int lineOffset = content.getOffsetAtLine(i); TextLayout layout = renderer.getTextLayout(i); @@ -4918,6 +4918,9 @@ public Rectangle getTextBounds(int start, int end) { } renderer.disposeTextLayout(layout); } + if (left == Integer.MAX_VALUE) { + left = 0; + } rect = new Rectangle (left, y, right-left, height); rect.x += leftMargin - horizontalScrollOffset; return rect; diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java index 331a1d40459..21765b99a44 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java @@ -180,6 +180,30 @@ public void test_ConstructorLorg_eclipse_swt_widgets_CompositeI(){ text.dispose(); } +@Test +public void test_getTextBounds() { + StyledText text = new StyledText(shell,SWT.BORDER); + try { + text.setText("\r\n\r\ntext"); + int firstLineOffset = text.getOffsetAtLine(0); + Rectangle r = text.getTextBounds(firstLineOffset, firstLineOffset); + assertEquals(0,r.x); + assertEquals(0,r.y); + assertEquals(0,r.width); + assertTrue(r.height > 0); + + text.setText("\r\n\r\ntext"); + int thirdLineOffset = text.getOffsetAtLine(2); + r = text.getTextBounds(thirdLineOffset, thirdLineOffset); + assertEquals(0, r.x); + assertTrue(r.y > 0); + assertTrue(r.width > 0); + assertTrue(r.height > 0); + }finally { + text.dispose(); + } +} + @Test public void test_addExtendedModifyListenerLorg_eclipse_swt_custom_ExtendedModifyListener() { final String line = "Line1";