Bug 2914 - Unsafe font lookup in the FontRegistry (1GKM997)
Summary: Unsafe font lookup in the FontRegistry (1GKM997)
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Kevin Haaland CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2001-10-10 22:46 EDT by Tod Creasey CLA
Modified: 2002-01-22 11:51 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tod Creasey CLA 2001-10-10 22:46:04 EDT
Our FontRegistry assumes that any FontData that we specify is valid. It makes 
this mistake in three
places in createFont()

FontData[] fixedFonts = display.getFontList(fd.getName(), false);
		if (isFixedFont(fixedFonts, fd)) {
			return new Font(display, fd);

/********This test does a check and uses the FontData we have and not the one 
we lookup from the
System. This has failed on NT for German and Japanese***********/.

		}

		FontData[] scalableFonts = display.getFontList(fd.getName(), 
true);
		if (scalableFonts.length > 0) {
			return new Font(display, fd);
		}
/********This test does a check and uses the FontData we have and not the one 
we lookup from the
System. This has failed on NT for German and Japanese***********/.
	}
	//unable to find a valid font.
	if (fonts.length > 0) {
		return new Font(display, fonts[0]);
	} else {
		//Failed to find any reasonable font. 
		return null;

/********This test returns a Font using the first value even if the first two 
are invalid***********/.

This method should use the FontDatas we lookup by asking the display, not the 
ones we defined
ourselves as we can't be sure it will be valid - the SWT provided ones are 
guaranteed. Also if we
fail our lookup we should return null - the current code returns the font we 
defined regardless.

The following methods fix these problems and have been released into the 2.0 
stream

/**
 * Creates a new font with the given font datas.
 */
private Font createFont(FontData[] fonts) {
	Display display = Display.getCurrent();
	for (int i = 0; i < fonts.length; i++) {
		FontData fd = fonts[i];
		if (fd == null)
			break;

		FontData[] fixedFonts = display.getFontList(fd.getName(), 
false);
		FontData fixedFont = getMatchingFont(fixedFonts,fd);
		if (fixedFont != null) {
			return new Font(display, fixedFont);
		}

		FontData[] scalableFonts = display.getFontList(fd.getName(), 
true);
		FontData scalableFont = getMatchingFont(scalableFonts,fd);
		if (scalableFont != null) {
			return new Font(display, scalableFont);
		}
	}
	//unable to find a valid font.
	return null;
}

/**
 * Returns the matching  font from the list. Return null if it is not found.
 */
private FontData getMatchingFont(FontData[] fonts, FontData fd) {
	// Can't use FontData.equals() since some values aren't
	// set if a fontdata isn't used.
	int height = fd.getHeight();
	String name = fd.getName();
	for (int i = 0; i < fonts.length; i++) {
		FontData font = fonts[i];
		if (font.getHeight() == height && font.getName().equals(name))
			return font;
	}
	return null;
}

NOTES:
Comment 1 DJ Houghton CLA 2001-10-29 19:25:11 EST
PRODUCT VERSION: 136a


Comment 2 Tod Creasey CLA 2002-01-22 11:51:14 EST
This has been fixed in 20020115.