Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 117604

Summary: [Graphics] TextLayout#getOffset() isn't correct with RTL text
Product: [Eclipse Project] Platform Reporter: Felipe Heidrich <eclipse.felipe>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact: Felipe Heidrich <eclipse.felipe>
Severity: normal    
Priority: P3 CC: jim
Version: 3.1Keywords: triaged
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug

Description Felipe Heidrich CLA 2005-11-22 18:10:10 EST
when a line L wraps is impossible to tell from the result of getOffset if the click happened on the left of line L or on the right of line L + 1.
Here the test case and its result:

public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.addListener(SWT.Paint, new Listener() {
		public void handleEvent(Event event) {
			TextLayout layout = new TextLayout(event.display);
			layout.setText ("English wraps");
			layout.setWidth(layout.getBounds().width - 2);
			layout.draw(event.gc, 10, 10);
			int[] trailing = new int [1]; 
			int[] lineOffsets;
			int offset;
			offset = layout.getOffset(0, 0, trailing);
			System.out.print("English text, line offsets: ");
			lineOffsets = layout.getLineOffsets();
			for (int i = 0; i < lineOffsets.length; i++) {
				System.out.print(lineOffsets[i] + " ");
			}
			System.out.println();
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(layout.getLineBounds(0).width + 2, 0, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(0, layout.getLineBounds(0).height + 2, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(layout.getLineBounds(0).width + 2, layout.getLineBounds(0).height + 2, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			layout.dispose();
			
			layout = new TextLayout(event.display);
			layout.setText ("\u0623\u0639\u0644\u0646\u062A \u0648\u0632\u0627\u0631\u0629");
			layout.setWidth(layout.getBounds().width - 2);
			layout.draw(event.gc, 10, 70);
			System.out.print("Arabic text, line offsets: ");
			lineOffsets = layout.getLineOffsets();
			for (int i = 0; i < lineOffsets.length; i++) {
				System.out.print(lineOffsets[i] + " ");
			}
			System.out.println();
			offset = layout.getOffset(0, 0, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(layout.getLineBounds(0).width + 2, 0, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(0, layout.getLineBounds(0).height + 2, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			offset = layout.getOffset(layout.getLineBounds(0).width + 2, layout.getLineBounds(0).height + 2, trailing);
			System.out.println("offset:" + offset + " trailing:" + trailing[0]);
			layout.dispose();
		}
	});
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose ();
}

---- 

English text, line offsets: 0 8 13 
offset:0 trailing:0
offset:7 trailing:1
offset:8 trailing:0
offset:12 trailing:1
Arabic text, line offsets: 0 6 11 
offset:6 trailing:0
offset:0 trailing:0
offset:11 trailing:0
offset:6 trailing:0
Comment 1 Felipe Heidrich CLA 2005-11-22 18:11:04 EST
This problem makes hard to fix StyledText#getOffsetLinePoint(int,int,int)


int getOffsetAtPoint(int x, int y, int lineIndex) {
	TextLayout layout = renderer.getTextLayout(lineIndex);
	x += horizontalScrollOffset - leftMargin;
	int[] trailing = new int[1];
	int offsetInLine = layout.getOffset(x, y, trailing);
	int index = layout.getLineIndex(offsetInLine);
	int[] offsets = layout.getLineOffsets();
	if (offsetInLine == offsets[index] && trailing[0] == 0) {
		caretAlignment = OFFSET_LEADING;
	} else {
		offsetInLine += trailing[0];
		String line = content.getLine(lineIndex);
		int lineEnd;
		if (offsetInLine == line.length()) {
			lineEnd = offsetInLine;
		} else {
			index = layout.getLineIndex(offsetInLine);
			lineEnd = offsets[index];	
		}
		if (offsetInLine == lineEnd) {
			caretAlignment = PREVIOUS_OFFSET_TRAILING;
		} else {
			caretAlignment = OFFSET_LEADING;
			if (trailing[0] != 0) {
				int level;
				int offset = offsetInLine - trailing[0];
				while (offset > 0 && Character.isDigit(line.charAt(offset))) offset--;
				if (offset == 0 && Character.isDigit(line.charAt(offset))) {
					level = isMirrored() ? 1 : 0;
				} else {
					level = layout.getLevel(offset) & 0x1;
				}
				int trailingLevel = layout.getLevel(offsetInLine) & 0x1;
				if ((level ^ trailingLevel) != 0) {
					caretAlignment = PREVIOUS_OFFSET_TRAILING;
				}
			}
		}
	}
	renderer.disposeTextLayout(layout);
	return offsetInLine + content.getOffsetAtLine(lineIndex);
}
Comment 2 Felipe Heidrich CLA 2009-08-19 15:59:58 EDT
Your bug has been moved to triage, visit http://www.eclipse.org/swt/triage.php for more info.
Comment 3 Jim Mayer CLA 2014-08-28 15:46:21 EDT
This issue looks like it could be related to a bug report I just filed on getLineBounds.  See https://bugs.eclipse.org/bugs/show_bug.cgi?id=442831
Comment 4 Leo Ufimtsev CLA 2017-08-03 12:30:53 EDT
This is a one-off bulk update. (The last one in the triage migration).

Moving bugs from swt-triaged@eclipse to platform-swt-inbox@eclipse.org and adding "triaged" keyword as per new triage process:
https://wiki.eclipse.org/SWT/Devel/Triage

See Bug 518478 for details.

Tag for notification/mail filters:
@TriageBulkUpdate
Comment 5 Eclipse Genie CLA 2020-01-31 03:27:57 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.