jdk/src/share/classes/sun/font/FontUtilities.java
changeset 8357 7da1b1af2da0
parent 7668 d4a77089c587
child 9035 1255eb81cc2f
equal deleted inserted replaced
8356:64c792dbf671 8357:7da1b1af2da0
    28 import java.awt.Font;
    28 import java.awt.Font;
    29 import java.io.BufferedReader;
    29 import java.io.BufferedReader;
    30 import java.io.File;
    30 import java.io.File;
    31 import java.io.FileInputStream;
    31 import java.io.FileInputStream;
    32 import java.io.InputStreamReader;
    32 import java.io.InputStreamReader;
       
    33 import java.lang.ref.SoftReference;
       
    34 import java.util.concurrent.ConcurrentHashMap;
    33 import java.security.AccessController;
    35 import java.security.AccessController;
    34 
    36 
    35 import java.security.PrivilegedAction;
    37 import java.security.PrivilegedAction;
    36 import javax.swing.plaf.FontUIResource;
    38 import javax.swing.plaf.FontUIResource;
    37 
    39 
   381      * } else {
   383      * } else {
   382      *   fuir = FontManager.getCompositeFontUIResource(desktopFont);
   384      *   fuir = FontManager.getCompositeFontUIResource(desktopFont);
   383      * }
   385      * }
   384      * return fuir;
   386      * return fuir;
   385      */
   387      */
       
   388     private static volatile
       
   389         SoftReference<ConcurrentHashMap<PhysicalFont, CompositeFont>>
       
   390         compMapRef = new SoftReference(null);
       
   391 
   386     public static FontUIResource getCompositeFontUIResource(Font font) {
   392     public static FontUIResource getCompositeFontUIResource(Font font) {
   387 
   393 
   388         FontUIResource fuir = new FontUIResource(font);
   394         FontUIResource fuir = new FontUIResource(font);
   389         Font2D font2D = FontUtilities.getFont2D(font);
   395         Font2D font2D = FontUtilities.getFont2D(font);
   390 
   396 
   400              return fuir;
   406              return fuir;
   401         }
   407         }
   402 
   408 
   403         FontManager fm = FontManagerFactory.getInstance();
   409         FontManager fm = FontManagerFactory.getInstance();
   404         CompositeFont dialog2D =
   410         CompositeFont dialog2D =
   405           (CompositeFont) fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
   411           (CompositeFont) fm.findFont2D("dialog", font.getStyle(),
       
   412                                         FontManager.NO_FALLBACK);
   406         if (dialog2D == null) { /* shouldn't happen */
   413         if (dialog2D == null) { /* shouldn't happen */
   407             return fuir;
   414             return fuir;
   408         }
   415         }
   409         PhysicalFont physicalFont = (PhysicalFont)font2D;
   416         PhysicalFont physicalFont = (PhysicalFont)font2D;
   410         CompositeFont compFont = new CompositeFont(physicalFont, dialog2D);
   417         ConcurrentHashMap<PhysicalFont, CompositeFont> compMap = compMapRef.get();
       
   418         if (compMap == null) { // Its been collected.
       
   419             compMap = new ConcurrentHashMap<PhysicalFont, CompositeFont>();
       
   420             compMapRef = new SoftReference(compMap);
       
   421         }
       
   422         CompositeFont compFont = compMap.get(physicalFont);
       
   423         if (compFont == null) {
       
   424             compFont = new CompositeFont(physicalFont, dialog2D);
       
   425             compMap.put(physicalFont, compFont);
       
   426         }
   411         FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
   427         FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
   412         /* marking this as a created font is needed as only created fonts
   428         /* marking this as a created font is needed as only created fonts
   413          * copy their creator's handles.
   429          * copy their creator's handles.
   414          */
   430          */
   415         FontAccess.getFontAccess().setCreatedFont(fuir);
   431         FontAccess.getFontAccess().setCreatedFont(fuir);