src/share/classes/javax/swing/text/StyleContext.java

Print this page
rev 1379 : [mq]: fontmanager.patch


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.awt.*;
  28 import java.util.*;
  29 import java.io.*;
  30 
  31 import javax.swing.SwingUtilities;
  32 import javax.swing.event.ChangeListener;
  33 import javax.swing.event.EventListenerList;
  34 import javax.swing.event.ChangeEvent;
  35 import java.lang.ref.WeakReference;
  36 import java.util.WeakHashMap;
  37 
  38 import sun.font.FontManager;
  39 
  40 /**
  41  * A pool of styles and their associated resources.  This class determines
  42  * the lifetime of a group of resources by being a container that holds
  43  * caches for various resources such as font and color that get reused
  44  * by the various style definitions.  This can be shared by multiple
  45  * documents if desired to maximize the sharing of related resources.
  46  * <p>
  47  * This class also provides efficient support for small sets of attributes
  48  * and compresses them by sharing across uses and taking advantage of
  49  * their immutable nature.  Since many styles are replicated, the potential
  50  * for sharing is significant, and copies can be extremely cheap.
  51  * Larger sets reduce the possibility of sharing, and therefore revert
  52  * automatically to a less space-efficient implementation.
  53  * <p>
  54  * <strong>Warning:</strong>
  55  * Serialized objects of this class will not be compatible with
  56  * future Swing releases. The current serialization support is
  57  * appropriate for short term storage or RMI between applications running
  58  * the same version of Swing.  As of 1.4, support for long term storage


 246      */
 247     public Font getFont(String family, int style, int size) {
 248         fontSearch.setValue(family, style, size);
 249         Font f = fontTable.get(fontSearch);
 250         if (f == null) {
 251             // haven't seen this one yet.
 252             Style defaultStyle =
 253                 getStyle(StyleContext.DEFAULT_STYLE);
 254             if (defaultStyle != null) {
 255                 final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
 256                 Font defaultFont =
 257                     (Font) defaultStyle.getAttribute(FONT_ATTRIBUTE_KEY);
 258                 if (defaultFont != null
 259                       && defaultFont.getFamily().equalsIgnoreCase(family)) {
 260                     f = defaultFont.deriveFont(style, size);
 261                 }
 262             }
 263             if (f == null) {
 264                 f = new Font(family, style, size);
 265             }
 266             if (! FontManager.fontSupportsDefaultEncoding(f)) {
 267                 f = FontManager.getCompositeFontUIResource(f);
 268             }
 269             FontKey key = new FontKey(family, style, size);
 270             fontTable.put(key, f);
 271         }
 272         return f;
 273     }
 274 
 275     /**
 276      * Returns font metrics for a font.
 277      *
 278      * @param f the font
 279      * @return the metrics
 280      */
 281     public FontMetrics getFontMetrics(Font f) {
 282         // The Toolkit implementations cache, so we just forward
 283         // to the default toolkit.
 284         return Toolkit.getDefaultToolkit().getFontMetrics(f);
 285     }
 286 
 287     // --- AttributeContext methods --------------------




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.awt.*;
  28 import java.util.*;
  29 import java.io.*;
  30 
  31 import javax.swing.SwingUtilities;
  32 import javax.swing.event.ChangeListener;
  33 import javax.swing.event.EventListenerList;
  34 import javax.swing.event.ChangeEvent;
  35 import java.lang.ref.WeakReference;
  36 import java.util.WeakHashMap;
  37 
  38 import sun.font.FontUtilities;
  39 
  40 /**
  41  * A pool of styles and their associated resources.  This class determines
  42  * the lifetime of a group of resources by being a container that holds
  43  * caches for various resources such as font and color that get reused
  44  * by the various style definitions.  This can be shared by multiple
  45  * documents if desired to maximize the sharing of related resources.
  46  * <p>
  47  * This class also provides efficient support for small sets of attributes
  48  * and compresses them by sharing across uses and taking advantage of
  49  * their immutable nature.  Since many styles are replicated, the potential
  50  * for sharing is significant, and copies can be extremely cheap.
  51  * Larger sets reduce the possibility of sharing, and therefore revert
  52  * automatically to a less space-efficient implementation.
  53  * <p>
  54  * <strong>Warning:</strong>
  55  * Serialized objects of this class will not be compatible with
  56  * future Swing releases. The current serialization support is
  57  * appropriate for short term storage or RMI between applications running
  58  * the same version of Swing.  As of 1.4, support for long term storage


 246      */
 247     public Font getFont(String family, int style, int size) {
 248         fontSearch.setValue(family, style, size);
 249         Font f = fontTable.get(fontSearch);
 250         if (f == null) {
 251             // haven't seen this one yet.
 252             Style defaultStyle =
 253                 getStyle(StyleContext.DEFAULT_STYLE);
 254             if (defaultStyle != null) {
 255                 final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
 256                 Font defaultFont =
 257                     (Font) defaultStyle.getAttribute(FONT_ATTRIBUTE_KEY);
 258                 if (defaultFont != null
 259                       && defaultFont.getFamily().equalsIgnoreCase(family)) {
 260                     f = defaultFont.deriveFont(style, size);
 261                 }
 262             }
 263             if (f == null) {
 264                 f = new Font(family, style, size);
 265             }
 266             if (! FontUtilities.fontSupportsDefaultEncoding(f)) {
 267                 f = FontUtilities.getCompositeFontUIResource(f);
 268             }
 269             FontKey key = new FontKey(family, style, size);
 270             fontTable.put(key, f);
 271         }
 272         return f;
 273     }
 274 
 275     /**
 276      * Returns font metrics for a font.
 277      *
 278      * @param f the font
 279      * @return the metrics
 280      */
 281     public FontMetrics getFontMetrics(Font f) {
 282         // The Toolkit implementations cache, so we just forward
 283         // to the default toolkit.
 284         return Toolkit.getDefaultToolkit().getFontMetrics(f);
 285     }
 286 
 287     // --- AttributeContext methods --------------------