Skip to content

Commit 9cf9fa7

Browse files
fix boundary condition in StyledText#getTextBounds (#1328)
1 parent 8eda32a commit 9cf9fa7

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4892,7 +4892,7 @@ public Rectangle getTextBounds(int start, int end) {
48924892
Rectangle rect;
48934893
int y = getLinePixel(lineStart);
48944894
int height = 0;
4895-
int left = 0x7fffffff, right = 0;
4895+
int left = Integer.MAX_VALUE, right = 0;
48964896
for (int i = lineStart; i <= lineEnd; i++) {
48974897
int lineOffset = content.getOffsetAtLine(i);
48984898
TextLayout layout = renderer.getTextLayout(i);
@@ -4918,6 +4918,9 @@ public Rectangle getTextBounds(int start, int end) {
49184918
}
49194919
renderer.disposeTextLayout(layout);
49204920
}
4921+
if (left == Integer.MAX_VALUE) {
4922+
left = 0;
4923+
}
49214924
rect = new Rectangle (left, y, right-left, height);
49224925
rect.x += leftMargin - horizontalScrollOffset;
49234926
return rect;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ public void test_ConstructorLorg_eclipse_swt_widgets_CompositeI(){
180180
text.dispose();
181181
}
182182

183+
@Test
184+
public void test_getTextBounds() {
185+
StyledText text = new StyledText(shell,SWT.BORDER);
186+
try {
187+
text.setText("\r\n\r\ntext");
188+
int firstLineOffset = text.getOffsetAtLine(0);
189+
Rectangle r = text.getTextBounds(firstLineOffset, firstLineOffset);
190+
assertEquals(0,r.x);
191+
assertEquals(0,r.y);
192+
assertEquals(0,r.width);
193+
assertTrue(r.height > 0);
194+
195+
text.setText("\r\n\r\ntext");
196+
int thirdLineOffset = text.getOffsetAtLine(2);
197+
r = text.getTextBounds(thirdLineOffset, thirdLineOffset);
198+
assertEquals(0, r.x);
199+
assertTrue(r.y > 0);
200+
assertTrue(r.width > 0);
201+
assertTrue(r.height > 0);
202+
}finally {
203+
text.dispose();
204+
}
205+
}
206+
183207
@Test
184208
public void test_addExtendedModifyListenerLorg_eclipse_swt_custom_ExtendedModifyListener() {
185209
final String line = "Line1";

0 commit comments

Comments
 (0)