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

Bug 312956

Summary: GC#textExtents(String) throws ArrayIndexOutOfBoundsException with GDI+ and if '\r' is present in the string
Product: [Eclipse Project] Platform Reporter: Alex Boyko <aboyko>
Component: SWTAssignee: Felipe Heidrich <eclipse.felipe>
Status: RESOLVED FIXED QA Contact:
Severity: critical    
Priority: P3 CC: ahunter.eclipse, eclipse.felipe, gheorghe
Version: 3.6Flags: gheorghe: review+
grant_gayed: review+
Target Milestone: 3.6 RC2   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
patch none

Description Alex Boyko CLA 2010-05-14 14:23:51 EDT
Try the following snippet:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestSnippet {
	
private static String text = "Label\r\n"; //$NON-NLS-1$

public static void main(String [] args) {
	final Display display = new Display();
	final Shell shell = new Shell(display);
	final Font font = new Font(display, "Tahoma", 16, SWT.BOLD);  //$NON-NLS-1$
	shell.addPaintListener(new PaintListener() {
		public void paintControl(PaintEvent event) {
			event.gc.setAntialias(SWT.ON);
			event.gc.setFont(font);
			event.gc.drawText(text, 10, 10);
			Point pt = event.gc.textExtent(text);
			event.gc.setClipping(10, 10 + pt.y, pt.x - 1, pt.y);
			event.gc.drawText(text, 10, 10 + pt.y);
		}
	});
	shell.setBounds(10, 10, 200, 200);
	shell.open ();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	font.dispose();
	display.dispose();
}
}

Launch it as "Java Application" and get ArrayIndexOutOfBoundsException
If you remove '\r' from the text, no exception
If you don't set anti-alias (i.e. advanced flag - GDI+ support) it will also work.

Think it's a typo, hence priority == critical just to make sure this gets fixed asap.
Comment 1 Felipe Heidrich CLA 2010-05-14 15:14:34 EDT
apparently we never tested this case, we should fix this for the next milestone.
Comment 2 Felipe Heidrich CLA 2010-05-14 15:16:57 EDT
Created attachment 168590 [details]
patch

The patch fixes this case, more testing would be good.

Thank you for reporting this bug.
Comment 3 Alex Boyko CLA 2010-05-14 17:20:27 EDT
This fix worked great for me. Didn't notice any problems with it.
Comment 4 Alex Boyko CLA 2010-05-14 17:21:05 EDT
Thanks for getting back on this so quickly!
Comment 5 Felipe Heidrich CLA 2010-05-17 13:08:13 EDT
Please, review the patch so we can have this code out today.
You can reproduce the problem on any windows machine (that supports advance graphics).
Comment 6 Felipe Heidrich CLA 2010-05-17 15:51:20 EDT
Fixed in HEAD > 20100517