Community
Participate
Working Groups
In the following testcase, the value given by GC.stringExtent() for a lowercase 'j' when a 24-point Arial font is used is incorrect. The leftmost part of the "hook" of the 'j' is cut off in this example.
import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; public class PR_5190 { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("SWT italic font test"); shell.setBackground(display.getSystemColor(SWT.COLOR_GRAY)); Font font = new Font(display, "Arial", 24, SWT.ITALIC); Canvas canvas = new Canvas(shell, 0); canvas.setFont(font); GC gc = new GC(canvas); gc.setFont(font); FontMetrics metrics = gc.getFontMetrics(); canvas.setSize(200, metrics.getHeight()); shell.setSize(200, 200); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { String tx = "jj"; Point extent = e.gc.stringExtent(tx); e.gc.setBackground(display.getSystemColor (SWT.COLOR_RED)); e.gc.fillRectangle(0, 0, extent.x, extent.y); e.gc.drawString(tx, 0, 0); } }); shell.open(); gc.dispose(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } font.dispose(); } }
This isn't just with Arial fonts and isn't just with the letter 'j'. The text is cutoff to the right with almost any string in italics. The only fonts I tested were Arial and Tahoma and both have the same problem. public class ItalicsIssue { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("SWT italic font test"); shell.setBackground(display.getSystemColor(SWT.COLOR_GRAY)); Font font = new Font(display, "Arial", 24, SWT.ITALIC); Canvas canvas = new Canvas(shell, 0); canvas.setFont(font); GC gc = new GC(canvas); gc.setFont(font); FontMetrics metrics = gc.getFontMetrics(); canvas.setSize(200, metrics.getHeight()); shell.setSize(200, 200); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { String tx = "jj"; Point extent = e.gc.stringExtent(tx); e.gc.setBackground(display.getSystemColor (SWT.COLOR_RED)); e.gc.fillRectangle(0, 0, extent.x, extent.y); e.gc.drawString(tx, 0, 0, true); } }); shell.open(); gc.dispose(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } font.dispose(); } }
The bug also applies to the GC.textExtent - methods. I'm using Windows XP SP2 and the bug still exists. We also tested this with linux using GTK with the result that GTK is not affected.
Can you review this issue? We needed to add some workaround code to GMF and I would really like to see it cleaned up.
Created attachment 117812 [details] The bug still exists in Eclipse 3.4.1 Build-Id M20080911-1700 Verified that the bug still exists with Eclipse 3.4.1 Build-Id M20080911-1700 running under Windows Vista Ultimate 32bit SP1.
Either fix for Eclipse 3.4 M4 or WONTFIX.
Either fix for Eclipse 3.5 M4 or WONTFIX.
the text is being drawn exactly where it is supposed to be. text is drawn at the x + left bearing (sometimes called left overhang or left underhang or padding). This is consistent in all platforms, I tested win32, wpf, motif, gtk, cocoa, and carbon. Closing as WONTFIX. To fix this, in the application, you will need to request new API in GC to return text metrics such as black width, left bearing, and right bearing. Equivalent to GetCharABCWidths of win32. Steve, we don't have already a problem report for that ?
Open one.
(In reply to comment #9) > Open one. I verified that native text editors on linux and mac have this problem (one just has to set italic and to increase the font size). The right thing for an application to do is to add margin around the text, drawing text at 0,0 is always going to have this problem. The question is: how much margin is needed ? To use left and right bearing as left and right margin is probably okay, but has to be the max bearing for the font (and not for a specific char/text). I think we can get this information on win32, I'll have to check if that is possible in all other platforms.
result for get left and right bearing for a font: win32: TEXTMETRIC.tmOverhang tested but it didn't work. gtk: no API motif: XFontStruct has min and max XCharStruct with lbearing and rbearing - didn't try. carbon: ATSFontMetrics have minLeftSideBearing and a minRightSideBearing, didn't try. cocoa: no API wpf: Typeface->GlyphTypeface->LeftSidebearing, didn't try.