Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » getFontMetrics() returns invalid results(The GC .getFontMetrics() returns the the same value regadless of Font.)
getFontMetrics() returns invalid results [message #1853465] Sun, 03 July 2022 15:13
Yigal Spinner is currently offline Yigal SpinnerFriend
Messages: 127
Registered: July 2009
Senior Member
I have an application that needs to create Rectangles based on the on the estimated length of a future string. For example if the user sets a length 8, then the rectangle should be large enough to fit any string of 8 characters (any character set or Font).
I used teh GC.getFontMetrics() to get the average character width in current font and character set and calculate the pixel size of the rectangle width. !!!However the value is always much smaller then needed!!!

I create a simple test code:
public class TestMetrics extends AbstractHandler {
	private static final String CHATSET="0987654321ABCDEFGHIJKLMNOPQRSTVUWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()-_=+;:/<>~";

	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell sh = HandlerUtil.getActiveShell(event);
		FontData fontDt = new FontData("Arial", 10, SWT.NORMAL);
		calcSizes (fontDt, sh);
		fontDt = new FontData("Arial", 10, SWT.BOLD);
		calcSizes (fontDt, sh);
		return null;

	private void calcSizes(FontData fontData, Shell sh) {
		Font font = new Font(null, new FontData[] {fontData});
		GC gc = new GC(sh);
		Point p = gc.textExtent(CHATSET);
		FontMetrics fm = gc.getFontMetrics();
		System.out.format ("Font avg width: %.2f, calc width: %.2f %n", fm.getAverageCharacterWidth(), (double)p.x / (double)CHATSET.length());

The results are:
1. Regular Font
------> Font avg width: 6.00, calc width: 7.21
2. Bold Font
-------> ont avg width: 6.00, calc width: 7.54

It is clear that "getAverageCharacterWidth()" returns the same value (6.0) which is smaller than needed and it is the same for any font used.

If I use my calculated average,( i.e. Get the size of a String with a set of characters and calculate average) the required average size is valid, however, this does not take in account Character Set. (I used only English Letters)

How can I get the real average width of the current Character Set and Font that the user is using?
Is this bug????

Previous Topic:Deploy eclipse rap application
Next Topic:[ANN] RAP 3.21 Released!
Goto Forum:

Current Time: Fri Jul 19 22:47:30 GMT 2024

Powered by FUDForum. Page generated in 0.03542 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top