Skip to content

Commit

Permalink
fix boundary condition in StyledText#getTextBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmelcher committed Jul 12, 2024
1 parent d95a444 commit ded8838
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit ded8838

Please sign in to comment.