Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 5190 - GC.stringExtent() gives incorrect data for italic fonts
Summary: GC.stringExtent() gives incorrect data for italic fonts
Status: RESOLVED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows NT
: P3 normal with 1 vote (vote)
Target Milestone: 3.5 M4   Edit
Assignee: Felipe Heidrich CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 104384
  Show dependency tree
 
Reported: 2001-10-23 17:21 EDT by David Whiteman CLA
Modified: 2008-11-26 17:45 EST (History)
5 users (show)

See Also:


Attachments
The bug still exists in Eclipse 3.4.1 Build-Id M20080911-1700 (5.05 KB, image/pjpeg)
2008-11-13 13:50 EST, Heiko Böttger CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Whiteman CLA 2001-10-23 17:21:06 EDT
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.
Comment 1 David Whiteman CLA 2001-10-23 17:21:39 EDT
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();
	}
}
Comment 2 Cherie Revells CLA 2007-08-02 15:53:46 EDT
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();
    }

}
Comment 3 Heiko Böttger CLA 2007-12-20 03:35:05 EST
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.
Comment 4 Anthony Hunter CLA 2008-11-13 12:06:38 EST
Can you review this issue?

We needed to add some workaround code to GMF and I would really like to see it cleaned up.
Comment 5 Heiko Böttger CLA 2008-11-13 13:50:43 EST
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.
Comment 6 Steve Northover CLA 2008-11-25 11:08:50 EST
Either fix for Eclipse 3.4 M4 or WONTFIX.
Comment 7 Steve Northover CLA 2008-11-25 11:12:01 EST
Either fix for Eclipse 3.5 M4 or WONTFIX.
Comment 8 Felipe Heidrich CLA 2008-11-25 17:48:58 EST
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 ?
Comment 9 Steve Northover CLA 2008-11-25 17:57:02 EST
Open one.
Comment 10 Felipe Heidrich CLA 2008-11-26 17:02:43 EST
(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.
Comment 11 Felipe Heidrich CLA 2008-11-26 17:45:02 EST
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.