Bug 4776 - StyledText - GC.stringExtent returns incorrect value (1GI7ALA)
Summary: StyledText - GC.stringExtent returns incorrect value (1GI7ALA)
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 2.0   Edit
Hardware: All Windows NT
: P3 normal (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Knut Radloff CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 34570 34571 (view as bug list)
Depends on:
Blocks: 7170
  Show dependency tree
 
Reported: 2001-10-11 14:22 EDT by Knut Radloff CLA
Modified: 2003-09-09 11:03 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Knut Radloff CLA 2001-10-11 14:22:49 EDT
Run the test case below. It renders and measures character using a non True Type font (MS Sans Serif).
gc.stringExtent returns a width of 8 for the regular font which matches the rendered glyph width.
When I render the same character using the bold version of the font stringExtent returns a width of 9
but the rendered glyph is actually 10 pixels wide.
This only appears to happen with non True Type fonts and causes problems in the bidi StyledText.
Bidi StyledText doesn't use gc.stringExtent but instead GetCharacterPlacement. GetCharacterPlacement
also returns the wrong width.


import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class PR_1GI7ALA {
	Shell shell;
	Canvas canvas;
	Font boldFont;
	Font normalFont;

public void create() {
	shell = new Shell();
	Display display = shell.getDisplay();
	normalFont = new Font(display, "MS Sans Serif", 10, SWT.NORMAL);
	boldFont = new Font(display, "MS Sans Serif", 10, SWT.BOLD);	

	shell.setFont(normalFont);
	shell.setBackground(new Color(display, 155, 0, 0));
	shell.addListener(SWT.Paint, new Listener() {
		public void handleEvent(Event event) {
			String text = "s";	
			event.gc.setBackground(new Color(shell.getDisplay(), 255, 255, 255));
			event.gc.drawLine(9, 0, 9, 40);						
			event.gc.drawString(text, 0, 0);
			System.out.println("regular font " + event.gc.stringExtent(text));
			event.gc.setFont(boldFont);			
			event.gc.drawString(text, 0, 20);			
			System.out.println("bold font " + event.gc.stringExtent(text));
		}
	});
	shell.open();
	shell.setSize(300, 200);
}
public static void main(String[] args) {
	PR_1GI7ALA test = new PR_1GI7ALA();

	test.create();	
	test.run();
}
public void run() {
	Display display = shell.getDisplay ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}	
}
}

NOTES:

KR (8/9/01 1:15:47 PM)
	We'd like to know if this is an SWT bug or if the platform is just plain not doing the right thing. 
	If it's the latter we'd have to work around it in StyledText (which would require non-trivial changes). 
	An actual fix is not particularly urgent (i.e., the problem only causes cheese, not a crash).

KR (8/20/01 2:20:29 PM)
	A similar problem occurs with True Type fonts in certain sizes. For example Times New Roman 10 
	regular (non-bold) is measured correctly as being 11 pixels wide. 
	Times New Roman 12, regular is measured incorrectly as 13 pixels wide when it really is 14 pixels 
	wide.
	This is on Arabic NT and with an Arabic 's' instead of the Latin 's' in the test case above.

KR (8/20/01 3:38:08 PM)
	Wordpad has problems with Times New Roman in sizes > 10 as well. The cursor is not placed 
	correctly. It overlaps the last pixel of the (logical) first character like we do. However if I then add 
	a space it doesn't overlap that pixel. This only happens on Arabic. I haven't seen this on Hebrew. 
	Perhaps the Arabic TrueType fonts are just bad?
Comment 1 DJ Houghton CLA 2001-10-29 16:37:44 EST
PRODUCT VERSION:
2.002

Comment 2 Lynne Kues CLA 2001-11-19 15:53:15 EST
In order for the StyledText widget to be appropriately bidi enabled, this PR 
needs to be fixed.  The incorrect measuring leads to cheese on Arabic 
platforms.  Can you have someone look into this? 
Comment 3 Mike Wilson CLA 2001-11-20 09:02:16 EST
It may be that we simply do not get correct measuring information from the o/s 
for non-truetype fonts. If that is true, then we will be unable to fix this 
problem. We'll look at it again however.
Comment 4 Knut Radloff CLA 2002-02-14 13:02:29 EST
Cheese does not happen on Win2k. However, the test case above still fails on 
2k. Cheese does happen on NT.
We may be able to work arround the cheese by rendering text transparent. We 
already do this in the non-bidi case. Need to investigate how/where the 
unselected/uncolored background is rendered. This seems to work on non-bidi 
platforms.
Comment 5 Silenio Quarti CLA 2002-02-14 13:20:51 EST
Does not work on 2K.  The problem is font-specific, not just true-type 
specific.  On 2K works with Microsoft Sans Serif (not MS Sans Serif).
Comment 6 Knut Radloff CLA 2002-04-30 11:09:31 EDT
Revisit post v2
Comment 7 Veronika Irvine CLA 2002-09-10 10:22:03 EDT
Post 2.0. Re-opening bug reports for review.
Comment 8 Knut Radloff CLA 2002-09-16 09:36:53 EDT
Lynne, Silenio and I agreed back in February that StyledText should try to 
workaround by rendering transparent text. SWT probably can't fix this because 
the OS is returning bad values.
Comment 9 Lynne Kues CLA 2003-03-11 11:06:54 EST
*** Bug 34571 has been marked as a duplicate of this bug. ***
Comment 10 Lynne Kues CLA 2003-03-11 11:07:29 EST
*** Bug 34570 has been marked as a duplicate of this bug. ***
Comment 11 Knut Radloff CLA 2003-05-27 10:31:22 EDT
Released a workaround as part of fixing bug 30434. The workaround currently 
only applies to bidi scripts or RIGHT_TO_LEFT widget.
Bug 31385 may be a duplicate. Should always use the workaround if we find it is 
the same measuring problem.
Comment 12 Knut Radloff CLA 2003-09-04 17:07:43 EDT
This should be marked as fixed. I think bug 40019 is a duplicate or at least 
related and was fixed, too.
Bug 40006 has a patch that implements transparent rendering. I can not 
reproduce the problem the patch was intended to fix (and the problem is 
different from the one the bug 40006 was opened for). Should open a separate 
bug when this is reproducable.
Comment 13 Knut Radloff CLA 2003-09-09 10:32:55 EDT
The fix for bug 40019 makes the +1 workaround in StyledText.redraw unnecessary. 
There is no longer selection cheese when mouse drag selecting. There is still a 
pixel wide selection cheese when the drag select triggers autoscroll. That in 
turn is fixed by the transparent rendering suggested in bug 40006. If we 
release both fixes we can get rid of the workaround in StyledText.redraw.
Comment 14 Knut Radloff CLA 2003-09-09 11:03:41 EDT
Removed workaround in StyledText.redraw.