|
43 | 43 | public final class TextLayout extends Resource { |
44 | 44 | Font font; |
45 | 45 | String text, segmentsText, originalText; |
46 | | - int lineSpacingInPoints, previousCountNextLine, previousEnd; |
| 46 | + int lineSpacingInPoints; |
47 | 47 | int ascent, descent; |
48 | 48 | int alignment; |
49 | 49 | int wrapWidth; |
@@ -309,9 +309,7 @@ public TextLayout (Device device) { |
309 | 309 | styles[0] = new StyleItem(); |
310 | 310 | styles[1] = new StyleItem(); |
311 | 311 | stylesCount = 2; |
312 | | - text = ""; //$NON-NLS-1$ |
313 | | - previousCountNextLine = 0; |
314 | | - previousEnd = -1; |
| 312 | + text = originalText = ""; //$NON-NLS-1$ |
315 | 313 | long[] ppv = new long[1]; |
316 | 314 | OS.OleInitialize(0); |
317 | 315 | 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) { |
3449 | 3447 | int length = text.length(); |
3450 | 3448 | if (length == 0) return; |
3451 | 3449 | 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 | + |
3474 | 3463 | start = Math.min(Math.max(0, start), length - 1); |
3475 | 3464 | end = Math.min(Math.max(0, end), length - 1); |
3476 | 3465 | int low = -1; |
@@ -3584,13 +3573,8 @@ public void setTabs (int[] tabs) { |
3584 | 3573 | */ |
3585 | 3574 | public void setText (String text) { |
3586 | 3575 | checkLayout(); |
3587 | | - |
3588 | 3576 | 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", ""); |
3594 | 3578 | if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
3595 | 3579 | if (text.equals(this.text)) return; |
3596 | 3580 | freeRuns(); |
|
0 commit comments