Bug 156775 Allow fonts to benefit from ResourceManager
Eclipse 3.x added a lot of features for manipulating ImageDescriptors and managing them through ResourceManager. Currently, it is cumbersome to do the same thing with Fonts, so most people are still using global Font registry. This enhancement requests that we add features to make Fonts as easy to manage as Images are. - FontDescriptors are provided globally - FontDescriptors are easy to manipulate - Concrete fonts are obtained from the FontDescriptors via a ResourceManager as needed - Fonts benefit from reference-counted shared allocations just like Images do. The attached patch adds the following enhancements for Font management. New Features: 1. (feature) Add helper methods to FontDescriptor to allow font size and style to be set without packing and unpacking the FontData. 2. (performance) Add a getFontData() method to FontDescriptor, to make it possible to manipulate font descriptors without creating a concrete Fonts. 3. (cleanup) Remove NamedFontDescriptor, and replace it with (the more general) ArrayFontDescriptor. 4. (performance) Refactor FontRegistry to use the ResourceManager tree in order to benefit from shared allocations (like Images currently do). 5. (feature) Wherever there is a prominent method that returns a global Font, provide a version that returns a FontDescriptor. Refactor where necessary such that a concrete Font won't be allocated as long as clients are only calling the descriptor version.
bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FontRegistry.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
 * A font registry maintains a mapping between symbolic font names 
 * and SWT fonts.
 * <p>
 * A font registry owns all of the font objects registered
 * with it, and automatically disposes of them when the SWT Display
 * that creates the fonts is disposed. Because of this, clients do 
 * not need to (indeed, must not attempt to) dispose of font 
 * objects themselves.
 * </p>
 * <p>
 * A special constructor is provided for populating a font registry
 * from a property files using the standard Java resource bundle mechanism.
 * </p>
 * <p>
 * Methods are provided for registering listeners that will be kept
 * apprised of changes to list of registed fonts.
 * </p>
 * <p>
 * Clients may instantiate this class (it was not designed to be subclassed).
 * </p>
 * 
 * Since 3.0 this class extends ResourceRegistry.
 */
public class FontRegistry extends ResourceRegistry {