Skip to content

Commit 4f760cf

Browse files
committed
Corrected TextLayout to produce right number of lines when '\r\n'
sequence comes. Also updated review comments. Fixes #184
1 parent ba97568 commit 4f760cf

File tree

1 file changed

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

1 file changed

+37
-10
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
*/
4343
public final class TextLayout extends Resource {
4444
Font font;
45-
String text, segmentsText;
46-
int lineSpacingInPoints;
45+
String text, segmentsText, originalText;
46+
int lineSpacingInPoints, previousCountNextLine, previousEnd;
4747
int ascent, descent;
4848
int alignment;
4949
int wrapWidth;
@@ -310,6 +310,8 @@ public TextLayout (Device device) {
310310
styles[1] = new StyleItem();
311311
stylesCount = 2;
312312
text = ""; //$NON-NLS-1$
313+
previousCountNextLine = 0;
314+
previousEnd = -1;
313315
long[] ppv = new long[1];
314316
OS.OleInitialize(0);
315317
if (COM.CoCreateInstance(COM.CLSID_CMultiLanguage, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IMLangFontLink2, ppv) == OS.S_OK) {
@@ -2952,15 +2954,11 @@ StyleItem[] merge (long items, int itemCount) {
29522954
linkBefore = false;
29532955
}
29542956
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;
2957+
if (ch == '\r' && start + 1 < end) {
2958+
ch = segmentsText.charAt(start + 1);
29632959
}
2960+
item.lineBreak = ch == '\n';
2961+
item.tab = ch == '\t';
29642962
if (itemLimit == -1) {
29652963
nextItemIndex = itemIndex + 1;
29662964
OS.MoveMemory(scriptItem, items + nextItemIndex * SCRIPT_ITEM.sizeof, SCRIPT_ITEM.sizeof);
@@ -3451,6 +3449,28 @@ public void setStyle (TextStyle style, int start, int end) {
34513449
int length = text.length();
34523450
if (length == 0) return;
34533451
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;
34543474
start = Math.min(Math.max(0, start), length - 1);
34553475
end = Math.min(Math.max(0, end), length - 1);
34563476
int low = -1;
@@ -3564,6 +3584,13 @@ public void setTabs (int[] tabs) {
35643584
*/
35653585
public void setText (String text) {
35663586
checkLayout();
3587+
3588+
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+
35673594
if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
35683595
if (text.equals(this.text)) return;
35693596
freeRuns();

0 commit comments

Comments
 (0)