Community
Participate
Working Groups
Snippet showing the problem (in 3.1M6): Display d = new Display(); Shell shell = new Shell(d, SWT.SHELL_TRIM); final String s = "All"; Canvas canvas = new Canvas(shell, SWT.NONE); FontData fd = canvas.getFont().getFontData()[0]; fd.setStyle(SWT.ITALIC); Font font = new Font(d, fd); canvas.setFont(font); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawString(s, 0, 0); } }); GC gc = new GC(shell); gc.setFont(font); Point p = gc.stringExtent(s); canvas.setBounds(0, 0, p.x, p.y); gc.dispose(); shell.pack(); shell.open(); while (!shell.isDisposed()) while (!d.readAndDispatch()) d.sleep(); Using textExtents() creates the same problem. If you change the font to be bold instead of italicized, everything works fine.
What seems to happen here is that the GC uses the total advance width of the string when measuring its extent, ignoring any overhang caused by an italic font style. While this may be undesired in some cases, it is probably the intended API behaviour. The same thing occurs when using the TextLayout class. I think the solution would be to provide additional methods that would return the entire visual bounding box of the string. Not sure how difficult implementing such a feature would be, though. See also attachment 22859 [details] and the related bug 99298.
*** Bug 140919 has been marked as a duplicate of this bug. ***
*** Bug 159594 has been marked as a duplicate of this bug. ***
Same problem with SWT 4.2.0 on Gtk. It happens with Labels too (I'm not sure of both problems are somehow related though). Setting a minimal width is a possible workaround... But we cannot do this for every piece of italic text we want to display.
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.