Skip to content

Commit

Permalink
Corrected TextLayout to produce right number of lines when '\r\n'
Browse files Browse the repository at this point in the history
sequence comes.

Fixes #184
  • Loading branch information
deepika-u committed Jul 9, 2024
1 parent b2c0366 commit 6736342
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
*/
public final class TextLayout extends Resource {
Font font;
String text, segmentsText;
int lineSpacingInPoints;
String text, segmentsText, originalText;
int lineSpacingInPoints, previousCountNextLine = 0, previousEnd = -1;
int ascent, descent;
int alignment;
int wrapWidth;
Expand Down Expand Up @@ -2952,8 +2952,11 @@ StyleItem[] merge (long items, int itemCount) {
linkBefore = false;
}
char ch = segmentsText.charAt(start);
if(ch == '\r') {
ch = segmentsText.charAt(start+1);
}
switch (ch) {
case '\r':
//case '\r':
case '\n':
item.lineBreak = true;
break;
Expand Down Expand Up @@ -3448,6 +3451,28 @@ public void setVerticalIndent (int verticalIndent) {
*/
public void setStyle (TextStyle style, int start, int end) {
checkLayout();
int countNextLine = 0, noStyleCountNextLine = 0, loop = 0;
if (previousEnd < 0)
loop = previousEnd+1;
else
loop = previousEnd;
for(int i = loop; i < end; i++) {
if(originalText.charAt(i) == '\r' && originalText.charAt(i+1) =='\n')
{
countNextLine++;
if(i < start)
noStyleCountNextLine++;
}
}
previousCountNextLine = previousCountNextLine + countNextLine;
if(start == 0 || previousEnd < 0) {
start = start - noStyleCountNextLine;
}
else {
start = start - previousCountNextLine;
}
previousEnd = end;
end = end - previousCountNextLine;
int length = text.length();
if (length == 0) return;
if (start > end) return;
Expand Down Expand Up @@ -3564,6 +3589,13 @@ public void setTabs (int[] tabs) {
*/
public void setText (String text) {
checkLayout();

this.originalText = text;
String[] strings = text.split("\r");
text = "";
for(int i = 0; i < strings.length; i++)
text += strings[i];

if (text == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (text.equals(this.text)) return;
freeRuns();
Expand Down

0 comments on commit 6736342

Please sign in to comment.