Skip to content

Commit 2e2b765

Browse files
committed
Corrected TextLayout to produce right number of lines when
'\r\n'sequence comes. Also updated as per review comments. Fixes #184
1 parent 1111547 commit 2e2b765

File tree

1 file changed

+16
-32
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+16
-32
lines changed

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public final class TextLayout extends Resource {
4444
Font font;
4545
String text, segmentsText, originalText;
46-
int lineSpacingInPoints, previousCountNextLine, previousEnd;
46+
int lineSpacingInPoints;
4747
int ascent, descent;
4848
int alignment;
4949
int wrapWidth;
@@ -309,9 +309,7 @@ public TextLayout (Device device) {
309309
styles[0] = new StyleItem();
310310
styles[1] = new StyleItem();
311311
stylesCount = 2;
312-
text = ""; //$NON-NLS-1$
313-
previousCountNextLine = 0;
314-
previousEnd = -1;
312+
text = originalText = ""; //$NON-NLS-1$
315313
long[] ppv = new long[1];
316314
OS.OleInitialize(0);
317315
if (COM.CoCreateInstance(COM.CLSID_CMultiLanguage, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IMLangFontLink2, ppv) == OS.S_OK) {
@@ -3449,28 +3447,19 @@ public void setStyle (TextStyle style, int start, int end) {
34493447
int length = text.length();
34503448
if (length == 0) return;
34513449
if (start > end) return;
3452-
int countNextLine = 0, noStyleCountNextLine = 0, loop = 0;
3453-
if (previousEnd < 0)
3454-
loop = previousEnd+1;
3455-
else
3456-
loop = previousEnd;
3457-
for (int i = loop; i < end; i++) {
3458-
if (originalText.charAt(i) == '\r' && originalText.charAt(i+1) =='\n')
3459-
{
3460-
countNextLine++;
3461-
if (i < start)
3462-
noStyleCountNextLine++;
3463-
}
3464-
}
3465-
previousCountNextLine = previousCountNextLine + countNextLine;
3466-
if (start == 0 || previousEnd < 0) {
3467-
start = start - noStyleCountNextLine;
3468-
}
3469-
else {
3470-
start = start - previousCountNextLine;
3471-
}
3472-
previousEnd = end;
3473-
end = end - previousCountNextLine;
3450+
3451+
String startString = originalText.substring(0, start+1);
3452+
int stringLength = startString.length();
3453+
3454+
startString = startString.charAt(stringLength-1) == '\r' ? startString.substring(0,stringLength-1) : startString;
3455+
int startCount = (int) startString.chars().filter(ch -> ch == '\r').count();
3456+
3457+
String endString = originalText.substring(0, end+1);
3458+
int endCount = (int) endString.chars().filter(ch -> ch == '\r').count();
3459+
3460+
start = start == 0 ? start : start - startCount;
3461+
end = end - endCount;
3462+
34743463
start = Math.min(Math.max(0, start), length - 1);
34753464
end = Math.min(Math.max(0, end), length - 1);
34763465
int low = -1;
@@ -3584,13 +3573,8 @@ public void setTabs (int[] tabs) {
35843573
*/
35853574
public void setText (String text) {
35863575
checkLayout();
3587-
35883576
this.originalText = text;
3589-
String[] strings = text.split("\r");
3590-
text = "";
3591-
for (int i = 0; i < strings.length; i++)
3592-
text += strings[i];
3593-
3577+
text = text.replace("\r", "");
35943578
if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
35953579
if (text.equals(this.text)) return;
35963580
freeRuns();

0 commit comments

Comments
 (0)