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

Bug 320641

Summary: TextLayout breaks lines at the wrong place, then reports its bounds incorrectly
Product: [Eclipse Project] Platform Reporter: Missing name <harely>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 4.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug
Attachments:
Description Flags
snippet reproducing the bug
none
screenshot none

Description Missing name CLA 2010-07-22 10:39:28 EDT
Build Identifier: 20100218-1602

When a TextLayout needs to break a line in the middle of word, it may break one character ahead of the correct one causing it to overflow the width set using setWidth(..). getBounds() then returns that width, rather than the actual width of the overflowing layout.
As far as I can tell, this happens when the correct break location is after a letter and before a non-letter.

Reproducible: Always

Steps to Reproduce:
package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TextLayoutBug {
	
	

	public static void main (String [] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);

		shell.addListener(SWT.Paint, new Listener() {
			@Override
			public void handleEvent(Event event) {
				GC gc = event.gc;
				gc.setBackground (new Color (display, 128, 128, 255));
				gc.fillRectangle (shell.getClientArea());
				
				TextLayout layout = new TextLayout(display);
				layout.setFont (new Font(display, "Arial", 10, SWT.NORMAL));
				layout.setText("abcd&& abcde&&  abcdef&&");
				layout.setWidth(36);
				layout.setAlignment(SWT.CENTER);
				Rectangle layoutSize = layout.getBounds();
				
				Rectangle rect = new Rectangle (20, 20, layoutSize.width, layoutSize.height);
				gc.setBackground (new Color (display, 255, 255, 0));
				gc.fillRectangle(rect);
				layout.draw (gc, rect.x, rect.y);
				
				gc.drawText ("Reported layout bounds: " + layoutSize.width + " X " + layoutSize.height, 
						rect.x, rect.y + rect.height + 5, true);
			}
		});

		shell.pack();
		shell.setSize(300, 150);
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

}
Comment 1 Missing name CLA 2010-07-22 10:42:20 EDT
Created attachment 174986 [details]
snippet reproducing the bug
Comment 2 Missing name CLA 2010-07-22 10:44:20 EDT
Created attachment 174987 [details]
screenshot
Comment 3 Eclipse Webmaster CLA 2019-09-06 16:18:50 EDT
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.

If you have further information on the current state of the bug, please add it. 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.
Comment 4 Eclipse Genie CLA 2021-11-02 13:44:48 EDT
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.