|
42 | 42 | */ |
43 | 43 | public final class TextLayout extends Resource { |
44 | 44 | Font font; |
45 | | - String text, segmentsText; |
| 45 | + String text, segmentsText, originalText; |
46 | 46 | int lineSpacingInPoints; |
47 | 47 | int ascent, descent; |
48 | 48 | int alignment; |
@@ -309,7 +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$ |
| 312 | + text = originalText = ""; //$NON-NLS-1$ |
313 | 313 | long[] ppv = new long[1]; |
314 | 314 | OS.OleInitialize(0); |
315 | 315 | if (COM.CoCreateInstance(COM.CLSID_CMultiLanguage, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IMLangFontLink2, ppv) == OS.S_OK) { |
@@ -2952,15 +2952,11 @@ StyleItem[] merge (long items, int itemCount) { |
2952 | 2952 | linkBefore = false; |
2953 | 2953 | } |
2954 | 2954 | char ch = segmentsText.charAt(start); |
2955 | | - switch (ch) { |
2956 | | - case '\r': |
2957 | | - case '\n': |
2958 | | - item.lineBreak = true; |
2959 | | - break; |
2960 | | - case '\t': |
2961 | | - item.tab = true; |
2962 | | - break; |
| 2955 | + if (ch == '\r' && start + 1 < end) { |
| 2956 | + ch = segmentsText.charAt(start + 1); |
2963 | 2957 | } |
| 2958 | + item.lineBreak = ch == '\n'; |
| 2959 | + item.tab = ch == '\t'; |
2964 | 2960 | if (itemLimit == -1) { |
2965 | 2961 | nextItemIndex = itemIndex + 1; |
2966 | 2962 | OS.MoveMemory(scriptItem, items + nextItemIndex * SCRIPT_ITEM.sizeof, SCRIPT_ITEM.sizeof); |
@@ -3451,6 +3447,19 @@ public void setStyle (TextStyle style, int start, int end) { |
3451 | 3447 | int length = text.length(); |
3452 | 3448 | if (length == 0) return; |
3453 | 3449 | if (start > end) return; |
| 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 | + |
3454 | 3463 | start = Math.min(Math.max(0, start), length - 1); |
3455 | 3464 | end = Math.min(Math.max(0, end), length - 1); |
3456 | 3465 | int low = -1; |
@@ -3564,6 +3573,8 @@ public void setTabs (int[] tabs) { |
3564 | 3573 | */ |
3565 | 3574 | public void setText (String text) { |
3566 | 3575 | checkLayout(); |
| 3576 | + this.originalText = text; |
| 3577 | + text = text.replace("\r", ""); |
3567 | 3578 | if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
3568 | 3579 | if (text.equals(this.text)) return; |
3569 | 3580 | freeRuns(); |
|
0 commit comments