Skip to content

Commit d53e86e

Browse files
committed
Corrected TextLayout to produce right number of lines when
'\r\n'sequence comes. Fixes #184
1 parent 8aec5fc commit d53e86e

File tree

1 file changed

+21
-10
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+21
-10
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
public final class TextLayout extends Resource {
4444
Font font;
45-
String text, segmentsText;
45+
String text, segmentsText, originalText;
4646
int lineSpacingInPoints;
4747
int ascent, descent;
4848
int alignment;
@@ -309,7 +309,7 @@ public TextLayout (Device device) {
309309
styles[0] = new StyleItem();
310310
styles[1] = new StyleItem();
311311
stylesCount = 2;
312-
text = ""; //$NON-NLS-1$
312+
text = originalText = ""; //$NON-NLS-1$
313313
long[] ppv = new long[1];
314314
OS.OleInitialize(0);
315315
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) {
29522952
linkBefore = false;
29532953
}
29542954
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);
29632957
}
2958+
item.lineBreak = ch == '\n';
2959+
item.tab = ch == '\t';
29642960
if (itemLimit == -1) {
29652961
nextItemIndex = itemIndex + 1;
29662962
OS.MoveMemory(scriptItem, items + nextItemIndex * SCRIPT_ITEM.sizeof, SCRIPT_ITEM.sizeof);
@@ -3451,6 +3447,19 @@ public void setStyle (TextStyle style, int start, int end) {
34513447
int length = text.length();
34523448
if (length == 0) return;
34533449
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+
34543463
start = Math.min(Math.max(0, start), length - 1);
34553464
end = Math.min(Math.max(0, end), length - 1);
34563465
int low = -1;
@@ -3564,6 +3573,8 @@ public void setTabs (int[] tabs) {
35643573
*/
35653574
public void setText (String text) {
35663575
checkLayout();
3576+
this.originalText = text;
3577+
text = text.replace("\r", "");
35673578
if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
35683579
if (text.equals(this.text)) return;
35693580
freeRuns();

0 commit comments

Comments
 (0)