src/share/classes/sun/font/FontUtilities.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  54 
  55     public static boolean isSolaris9;
  56 
  57     public static boolean isOpenSolaris;
  58 
  59     public static boolean useT2K;
  60 
  61     public static boolean isWindows;
  62 
  63     public static boolean isOpenJDK;
  64 
  65     static final String LUCIDA_FILE_NAME = "LucidaSansRegular.ttf";
  66 
  67     private static boolean debugFonts = false;
  68     private static PlatformLogger logger = null;
  69     private static boolean logging;
  70 
  71     // This static initializer block figures out the OS constants.
  72     static {
  73 
  74         AccessController.doPrivileged(new PrivilegedAction () {
  75             public Object run() {
  76                 String osName = System.getProperty("os.name", "unknownOS");
  77                 isSolaris = osName.startsWith("SunOS");
  78 
  79                 isLinux = osName.startsWith("Linux");
  80 
  81                 isMacOSX = osName.contains("OS X"); // TODO: MacOSX
  82 
  83                 String t2kStr = System.getProperty("sun.java2d.font.scaler");
  84                 if (t2kStr != null) {
  85                     useT2K = "t2k".equals(t2kStr);
  86                 } else {
  87                     useT2K = false;
  88                 }
  89                 if (isSolaris) {
  90                     String version = System.getProperty("os.version", "0.0");
  91                     isSolaris8 = version.startsWith("5.8");
  92                     isSolaris9 = version.startsWith("5.9");
  93                     float ver = Float.parseFloat(version);
  94                     if (ver > 5.10f) {


 374      * will NOT yield the same result, as the new underlying CompositeFont
 375      * cannot be "looked up" in the font registry.
 376      * This returns a FontUIResource as that is the Font sub-class needed
 377      * by Swing.
 378      * Suggested usage is something like :
 379      * FontUIResource fuir;
 380      * Font desktopFont = getDesktopFont(..);
 381      * // NOTE even if fontSupportsDefaultEncoding returns true because
 382      * // you get Tahoma and are running in an English locale, you may
 383      * // still want to just call getCompositeFontUIResource() anyway
 384      * // as only then will you get fallback fonts - eg for CJK.
 385      * if (FontManager.fontSupportsDefaultEncoding(desktopFont)) {
 386      *   fuir = new FontUIResource(..);
 387      * } else {
 388      *   fuir = FontManager.getCompositeFontUIResource(desktopFont);
 389      * }
 390      * return fuir;
 391      */
 392     private static volatile
 393         SoftReference<ConcurrentHashMap<PhysicalFont, CompositeFont>>
 394         compMapRef = new SoftReference(null);
 395 
 396     public static FontUIResource getCompositeFontUIResource(Font font) {
 397 
 398         FontUIResource fuir = new FontUIResource(font);
 399         Font2D font2D = FontUtilities.getFont2D(font);
 400 
 401         if (!(font2D instanceof PhysicalFont)) {
 402             /* Swing should only be calling this when a font is obtained
 403              * from desktop properties, so should generally be a physical font,
 404              * an exception might be for names like "MS Serif" which are
 405              * automatically mapped to "Serif", so there's no need to do
 406              * anything special in that case. But note that suggested usage
 407              * is first to call fontSupportsDefaultEncoding(Font) and this
 408              * method should not be called if that were to return true.
 409              */
 410              return fuir;
 411         }
 412 
 413         FontManager fm = FontManagerFactory.getInstance();
 414         Font2D dialog = fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
 415         // Should never be null, but MACOSX fonts are not CompositeFonts
 416         if (dialog == null || !(dialog instanceof CompositeFont)) {
 417             return fuir;
 418         }
 419         CompositeFont dialog2D = (CompositeFont)dialog;
 420         PhysicalFont physicalFont = (PhysicalFont)font2D;
 421         ConcurrentHashMap<PhysicalFont, CompositeFont> compMap = compMapRef.get();
 422         if (compMap == null) { // Its been collected.
 423             compMap = new ConcurrentHashMap<PhysicalFont, CompositeFont>();
 424             compMapRef = new SoftReference(compMap);
 425         }
 426         CompositeFont compFont = compMap.get(physicalFont);
 427         if (compFont == null) {
 428             compFont = new CompositeFont(physicalFont, dialog2D);
 429             compMap.put(physicalFont, compFont);
 430         }
 431         FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
 432         /* marking this as a created font is needed as only created fonts
 433          * copy their creator's handles.
 434          */
 435         FontAccess.getFontAccess().setCreatedFont(fuir);
 436         return fuir;
 437     }
 438 
 439    /* A small "map" from GTK/fontconfig names to the equivalent JDK
 440     * logical font name.
 441     */
 442     private static final String[][] nameMap = {
 443         {"sans",       "sansserif"},
 444         {"sans-serif", "sansserif"},


   1 /*
   2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  54 
  55     public static boolean isSolaris9;
  56 
  57     public static boolean isOpenSolaris;
  58 
  59     public static boolean useT2K;
  60 
  61     public static boolean isWindows;
  62 
  63     public static boolean isOpenJDK;
  64 
  65     static final String LUCIDA_FILE_NAME = "LucidaSansRegular.ttf";
  66 
  67     private static boolean debugFonts = false;
  68     private static PlatformLogger logger = null;
  69     private static boolean logging;
  70 
  71     // This static initializer block figures out the OS constants.
  72     static {
  73 
  74         AccessController.doPrivileged(new PrivilegedAction<Object>() {
  75             public Object run() {
  76                 String osName = System.getProperty("os.name", "unknownOS");
  77                 isSolaris = osName.startsWith("SunOS");
  78 
  79                 isLinux = osName.startsWith("Linux");
  80 
  81                 isMacOSX = osName.contains("OS X"); // TODO: MacOSX
  82 
  83                 String t2kStr = System.getProperty("sun.java2d.font.scaler");
  84                 if (t2kStr != null) {
  85                     useT2K = "t2k".equals(t2kStr);
  86                 } else {
  87                     useT2K = false;
  88                 }
  89                 if (isSolaris) {
  90                     String version = System.getProperty("os.version", "0.0");
  91                     isSolaris8 = version.startsWith("5.8");
  92                     isSolaris9 = version.startsWith("5.9");
  93                     float ver = Float.parseFloat(version);
  94                     if (ver > 5.10f) {


 374      * will NOT yield the same result, as the new underlying CompositeFont
 375      * cannot be "looked up" in the font registry.
 376      * This returns a FontUIResource as that is the Font sub-class needed
 377      * by Swing.
 378      * Suggested usage is something like :
 379      * FontUIResource fuir;
 380      * Font desktopFont = getDesktopFont(..);
 381      * // NOTE even if fontSupportsDefaultEncoding returns true because
 382      * // you get Tahoma and are running in an English locale, you may
 383      * // still want to just call getCompositeFontUIResource() anyway
 384      * // as only then will you get fallback fonts - eg for CJK.
 385      * if (FontManager.fontSupportsDefaultEncoding(desktopFont)) {
 386      *   fuir = new FontUIResource(..);
 387      * } else {
 388      *   fuir = FontManager.getCompositeFontUIResource(desktopFont);
 389      * }
 390      * return fuir;
 391      */
 392     private static volatile
 393         SoftReference<ConcurrentHashMap<PhysicalFont, CompositeFont>>
 394         compMapRef = new SoftReference<>(null);
 395 
 396     public static FontUIResource getCompositeFontUIResource(Font font) {
 397 
 398         FontUIResource fuir = new FontUIResource(font);
 399         Font2D font2D = FontUtilities.getFont2D(font);
 400 
 401         if (!(font2D instanceof PhysicalFont)) {
 402             /* Swing should only be calling this when a font is obtained
 403              * from desktop properties, so should generally be a physical font,
 404              * an exception might be for names like "MS Serif" which are
 405              * automatically mapped to "Serif", so there's no need to do
 406              * anything special in that case. But note that suggested usage
 407              * is first to call fontSupportsDefaultEncoding(Font) and this
 408              * method should not be called if that were to return true.
 409              */
 410              return fuir;
 411         }
 412 
 413         FontManager fm = FontManagerFactory.getInstance();
 414         Font2D dialog = fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
 415         // Should never be null, but MACOSX fonts are not CompositeFonts
 416         if (dialog == null || !(dialog instanceof CompositeFont)) {
 417             return fuir;
 418         }
 419         CompositeFont dialog2D = (CompositeFont)dialog;
 420         PhysicalFont physicalFont = (PhysicalFont)font2D;
 421         ConcurrentHashMap<PhysicalFont, CompositeFont> compMap = compMapRef.get();
 422         if (compMap == null) { // Its been collected.
 423             compMap = new ConcurrentHashMap<PhysicalFont, CompositeFont>();
 424             compMapRef = new SoftReference<>(compMap);
 425         }
 426         CompositeFont compFont = compMap.get(physicalFont);
 427         if (compFont == null) {
 428             compFont = new CompositeFont(physicalFont, dialog2D);
 429             compMap.put(physicalFont, compFont);
 430         }
 431         FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
 432         /* marking this as a created font is needed as only created fonts
 433          * copy their creator's handles.
 434          */
 435         FontAccess.getFontAccess().setCreatedFont(fuir);
 436         return fuir;
 437     }
 438 
 439    /* A small "map" from GTK/fontconfig names to the equivalent JDK
 440     * logical font name.
 441     */
 442     private static final String[][] nameMap = {
 443         {"sans",       "sansserif"},
 444         {"sans-serif", "sansserif"},