-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make FontData key in map of ScalingSWTFontRegistry immutable #218
Make FontData key in map of ScalingSWTFontRegistry immutable #218
Comments
org.eclipse.swt.internal.ScalingSWTFontRegistry.fontKeyMap
uses FontData
as key
If we see the implementation of getFontData(FontData, zoom) in ScalingSWTFontRegistry @Override
public Font getFont(FontData fontData, int zoom) {
ScaledFontContainer container;
if (customFontsKeyMap.containsKey(fontData)) {
container = customFontsKeyMap.get(fontData);
} else {
container = new ScaledCustomFontContainer(fontData);
customFontsKeyMap.put(fontData, container);
}
return container.getScaledFont(zoom);
} If we change the font data it creates a new ScaledCustomFont which for me is correct behavior. Even later if the Font is requested for 200% zoom we will use the modified fontData and then scale it to 200% zoom. Even if we use the clone image data to store it as a key. It won't be updated by reference but the output for your example will still remain.
My conclusion is that we don't really need to do anything here. |
The issue is with the line adding to the map: This results in the passed font data being placed inside the map, but the data be modified by the consumer that passed it in afterwards, i.e., something like: registry.getFont(fontData, zoom);
fontData.height += 1; |
The resulting font might be fine but the fact that there is a dangling (unusable) entry in the map is the issue I described. As Heiko mentioned, the issue described here is that when you change the internal state (a field) of the If we make |
Just to avoid confusion and clarify that we are on the same page: the goal of this is not to make |
But that would mean that you will still have dangling entries in the map. Try this: public static void main(String[] args) {
Map<FontData, String> map = new HashMap<>();
FontData fontData = new FontData("f1", 10, 10);
FontData clone = new FontData(fontData.getName(), fontData.getHeight(), fontData.getStyle());
// store the clone but search for the original one
map.put(clone, "found!");
System.out.println(map.get(fontData));
// modify the original one and search for it
fontData.height += 1;
System.out.println(map.get(fontData));
} The output is still:
Let's talk about it in the daily. |
Discussed: since the idea is to support the use-case where one creates "the same" font at a later point in time and passes it as a parameter to the |
As mentioned in point Nr. 3 of #98 (comment) (pasted below), this could lead to problems if someone changes the internal state of
fontData
.Task
Make the key in the registry map immutable. Possible options are:
The text was updated successfully, but these errors were encountered: