1 /*
   2  * Copyright (c) 2008, 2019, 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
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.util.Locale;
  29 
  30 import sun.awt.SunHints;
  31 import sun.awt.SunToolkit;
  32 import sun.util.logging.PlatformLogger;
  33 
  34 /**
  35  * Small utility class to manage FontConfig.
  36  */
  37 public class FontConfigManager {
  38 
  39     static boolean fontConfigFailed = false;
  40 
  41     /* This is populated by native */
  42     private static final FontConfigInfo fcInfo = new FontConfigInfo();
  43 
  44     /* Begin support for GTK Look and Feel - query libfontconfig and
  45      * return a composite Font to Swing that uses the desktop font(s).
  46      */
  47 
  48     /* These next three classes are just data structures.
  49      */
  50     public static class FontConfigFont {
  51         public String familyName;        // eg Bitstream Vera Sans
  52         public String styleStr;          // eg Bold
  53         public String fullName;          // eg Bitstream Vera Sans Bold
  54         public String fontFile;          // eg /usr/X11/lib/fonts/foo.ttf
  55     }
  56 
  57     public static class FcCompFont {
  58         public String fcName;            // eg sans
  59         public String fcFamily;          // eg sans
  60         public String jdkName;           // eg sansserif
  61         public int style;                // eg 0=PLAIN
  62         public FontConfigFont firstFont;
  63         public FontConfigFont[] allFonts;
  64         //boolean preferBitmaps;    // if embedded bitmaps preferred over AA
  65         public CompositeFont compFont;   // null if not yet created/known.
  66     }
  67 
  68     public static class FontConfigInfo {
  69         public int fcVersion;
  70         public String[] cacheDirs = new String[4];
  71     }
  72 
  73     /* fontconfig recognises slants roman, italic, as well as oblique,
  74      * and a slew of weights, where the ones that matter here are
  75      * regular and bold.
  76      * To fully qualify what we want, we can for example ask for (eg)
  77      * Font.PLAIN             : "serif:regular:roman"
  78      * Font.BOLD              : "serif:bold:roman"
  79      * Font.ITALIC            : "serif:regular:italic"
  80      * Font.BOLD|Font.ITALIC  : "serif:bold:italic"
  81      */
  82     private static String[] fontConfigNames = {
  83         "sans:regular:roman",
  84         "sans:bold:roman",
  85         "sans:regular:italic",
  86         "sans:bold:italic",
  87 
  88         "serif:regular:roman",
  89         "serif:bold:roman",
  90         "serif:regular:italic",
  91         "serif:bold:italic",
  92 
  93         "monospace:regular:roman",
  94         "monospace:bold:roman",
  95         "monospace:regular:italic",
  96         "monospace:bold:italic",
  97     };
  98 
  99     /* This array has the array elements created in Java code and is
 100      * passed down to native to be filled in.
 101      */
 102     private FcCompFont[] fontConfigFonts;
 103 
 104     /**
 105      * Instantiates a new FontConfigManager getting the default instance
 106      * of FontManager from the FontManagerFactory.
 107      */
 108     public FontConfigManager() {
 109     }
 110 
 111     /* Called from code that needs to know what are the AA settings
 112      * that apps using FC would pick up for the default desktop font.
 113      * Note apps can change the default desktop font. etc, so this
 114      * isn't certain to be right but its going to correct for most cases.
 115      * Native return values map to the text aa values in sun.awt.SunHints.
 116      * which is used to look up the renderinghint value object.
 117      */
 118     public static Object getFontConfigAAHint() {
 119         return getFontConfigAAHint("sans");
 120     }
 121 
 122     /* This is public solely so that for debugging purposes it can be called
 123      * with other names, which might (eg) include a size, eg "sans-24"
 124      * The return value is a text aa rendering hint value.
 125      * Normally we should call the no-args version.
 126      */
 127     public static Object getFontConfigAAHint(String fcFamily) {
 128         if (FontUtilities.isWindows) {
 129             return null;
 130         } else {
 131             int hint = getFontConfigAASettings(getFCLocaleStr(), fcFamily);
 132             if (hint < 0) {
 133                 return null;
 134             } else {
 135                 return SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING,
 136                                           hint);
 137             }
 138         }
 139     }
 140 
 141 
 142     private static String getFCLocaleStr() {
 143         Locale l = SunToolkit.getStartupLocale();
 144         String localeStr = l.getLanguage();
 145         String country = l.getCountry();
 146         if (!country.isEmpty()) {
 147             localeStr = localeStr + "-" + country;
 148         }
 149         return localeStr;
 150     }
 151 
 152     /* This does cause the native libfontconfig to be loaded and unloaded,
 153      * but it does not incur the overhead of initialisation of its
 154      * data structures, so shouldn't have a measurable impact.
 155      */
 156     public static native int getFontConfigVersion();
 157 
 158     /* This can be made public if it's needed to force a re-read
 159      * rather than using the cached values. The re-read would be needed
 160      * only if some event signalled that the fontconfig has changed.
 161      * In that event this method would need to return directly the array
 162      * to be used by the caller in case it subsequently changed.
 163      */
 164     public synchronized void initFontConfigFonts(boolean includeFallbacks) {
 165 
 166         if (fontConfigFonts != null) {
 167             if (!includeFallbacks || (fontConfigFonts[0].allFonts != null)) {
 168                 return;
 169             }
 170         }
 171 
 172         if (FontUtilities.isWindows || fontConfigFailed) {
 173             return;
 174         }
 175 
 176         long t0 = 0;
 177         if (FontUtilities.isLogging()) {
 178             t0 = System.nanoTime();
 179         }
 180 
 181         FcCompFont[] fontArr = new FcCompFont[fontConfigNames.length];
 182 
 183         for (int i = 0; i< fontArr.length; i++) {
 184             fontArr[i] = new FcCompFont();
 185             fontArr[i].fcName = fontConfigNames[i];
 186             int colonPos = fontArr[i].fcName.indexOf(':');
 187             fontArr[i].fcFamily = fontArr[i].fcName.substring(0, colonPos);
 188             fontArr[i].jdkName = FontUtilities.mapFcName(fontArr[i].fcFamily);
 189             fontArr[i].style = i % 4; // depends on array order.
 190         }
 191         getFontConfig(getFCLocaleStr(), fcInfo, fontArr, includeFallbacks);
 192         FontConfigFont anyFont = null;
 193         /* If don't find anything (eg no libfontconfig), then just return */
 194         for (int i = 0; i< fontArr.length; i++) {
 195             FcCompFont fci = fontArr[i];
 196             if (fci.firstFont == null) {
 197                 if (FontUtilities.isLogging()) {
 198                     PlatformLogger logger = FontUtilities.getLogger();
 199                     logger.info("Fontconfig returned no font for " +
 200                                 fontArr[i].fcName);
 201                 }
 202                 fontConfigFailed = true;
 203             } else if (anyFont == null) {
 204                 anyFont = fci.firstFont;
 205             }
 206         }
 207 
 208         if (anyFont == null) {
 209             if (FontUtilities.isLogging()) {
 210                 PlatformLogger logger = FontUtilities.getLogger();
 211                 logger.info("Fontconfig returned no fonts at all.");
 212             }
 213             fontConfigFailed = true;
 214             return;
 215         } else if (fontConfigFailed) {
 216             for (int i = 0; i< fontArr.length; i++) {
 217                 if (fontArr[i].firstFont == null) {
 218                     fontArr[i].firstFont = anyFont;
 219                 }
 220             }
 221         }
 222 
 223         fontConfigFonts = fontArr;
 224 
 225         if (FontUtilities.isLogging()) {
 226 
 227             PlatformLogger logger = FontUtilities.getLogger();
 228 
 229             long t1 = System.nanoTime();
 230             logger.info("Time spent accessing fontconfig="
 231                         + ((t1 - t0) / 1000000) + "ms.");
 232 
 233             for (int i = 0; i< fontConfigFonts.length; i++) {
 234                 FcCompFont fci = fontConfigFonts[i];
 235                 logger.info("FC font " + fci.fcName+" maps to family " +
 236                             fci.firstFont.familyName +
 237                             " in file " + fci.firstFont.fontFile);
 238                 if (fci.allFonts != null) {
 239                     for (int f=0;f<fci.allFonts.length;f++) {
 240                         FontConfigFont fcf = fci.allFonts[f];
 241                         logger.info("Family=" + fcf.familyName +
 242                                     " Style="+ fcf.styleStr +
 243                                     " Fullname="+fcf.fullName +
 244                                     " File="+fcf.fontFile);
 245                     }
 246                 }
 247             }
 248         }
 249     }
 250 
 251     public PhysicalFont registerFromFcInfo(FcCompFont fcInfo) {
 252 
 253         SunFontManager fm = SunFontManager.getInstance();
 254 
 255         /* If it's a TTC file we need to know that as we will need to
 256          * make sure we return the right font */
 257         String fontFile = fcInfo.firstFont.fontFile;
 258         int offset = fontFile.length()-4;
 259         if (offset <= 0) {
 260             return null;
 261         }
 262         String ext = fontFile.substring(offset).toLowerCase();
 263         boolean isTTC = ext.equals(".ttc");
 264 
 265         /* If this file is already registered, can just return its font.
 266          * However we do need to check in case it's a TTC as we need
 267          * a specific font, so rather than directly returning it, let
 268          * findFont2D resolve that.
 269          */
 270         PhysicalFont physFont = fm.getRegisteredFontFile(fontFile);
 271         if (physFont != null) {
 272             if (isTTC) {
 273                 Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName,
 274                                            fcInfo.style,
 275                                            FontManager.NO_FALLBACK);
 276                 if (f2d instanceof PhysicalFont) { /* paranoia */
 277                     return (PhysicalFont)f2d;
 278                 } else {
 279                     return null;
 280                 }
 281             } else {
 282                 return physFont;
 283             }
 284         }
 285 
 286         /* If the font may hide a JRE font, we want to use the JRE version,
 287          * so make it point to the JRE font.
 288          */
 289         physFont = fm.findJREDeferredFont(fcInfo.firstFont.familyName,
 290                                           fcInfo.style);
 291 
 292         /* It is also possible the font file is on the "deferred" list,
 293          * in which case we can just initialise it now.
 294          */
 295         if (physFont == null &&
 296             fm.isDeferredFont(fontFile) == true) {
 297             physFont = fm.initialiseDeferredFont(fcInfo.firstFont.fontFile);
 298             /* use findFont2D to get the right font from TTC's */
 299             if (physFont != null) {
 300                 if (isTTC) {
 301                     Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName,
 302                                                fcInfo.style,
 303                                                FontManager.NO_FALLBACK);
 304                     if (f2d instanceof PhysicalFont) { /* paranoia */
 305                         return (PhysicalFont)f2d;
 306                     } else {
 307                         return null;
 308                     }
 309                 } else {
 310                     return physFont;
 311                 }
 312             }
 313         }
 314 
 315         /* In the majority of cases we reach here, and need to determine
 316          * the type and rank to register the font.
 317          */
 318         if (physFont == null) {
 319             int fontFormat = SunFontManager.FONTFORMAT_NONE;
 320             int fontRank = Font2D.UNKNOWN_RANK;
 321 
 322             if (ext.equals(".ttf") || isTTC) {
 323                 fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;
 324                 fontRank = Font2D.TTF_RANK;
 325             } else if (ext.equals(".pfa") || ext.equals(".pfb")) {
 326                 fontFormat = SunFontManager.FONTFORMAT_TYPE1;
 327                 fontRank = Font2D.TYPE1_RANK;
 328             }
 329             physFont = fm.registerFontFile(fcInfo.firstFont.fontFile, null,
 330                                       fontFormat, true, fontRank);
 331         }
 332         return physFont;
 333     }
 334 
 335     /*
 336      * We need to return a Composite font which has as the font in
 337      * its first slot one obtained from fontconfig.
 338      */
 339     public CompositeFont getFontConfigFont(String name, int style) {
 340 
 341         name = name.toLowerCase();
 342 
 343         initFontConfigFonts(false);
 344         if (fontConfigFonts == null) {
 345             // This avoids an immediate NPE if fontconfig look up failed
 346             // but doesn't guarantee this is a recoverable situation.
 347             return null;
 348         }
 349 
 350         FcCompFont fcInfo = null;
 351         for (int i=0; i<fontConfigFonts.length; i++) {
 352             if (name.equals(fontConfigFonts[i].fcFamily) &&
 353                 style == fontConfigFonts[i].style) {
 354                 fcInfo = fontConfigFonts[i];
 355                 break;
 356             }
 357         }
 358         if (fcInfo == null) {
 359             fcInfo = fontConfigFonts[0];
 360         }
 361 
 362         if (FontUtilities.isLogging()) {
 363             FontUtilities.getLogger()
 364                           .info("FC name=" + name + " style=" + style +
 365                                 " uses " + fcInfo.firstFont.familyName +
 366                                 " in file: " + fcInfo.firstFont.fontFile);
 367         }
 368 
 369         if (fcInfo.compFont != null) {
 370             return fcInfo.compFont;
 371         }
 372 
 373         /* jdkFont is going to be used for slots 1..N and as a fallback.
 374          * Slot 0 will be the physical font from fontconfig.
 375          */
 376         FontManager fm = FontManagerFactory.getInstance();
 377         CompositeFont jdkFont = (CompositeFont)
 378             fm.findFont2D(fcInfo.jdkName, style, FontManager.LOGICAL_FALLBACK);
 379 
 380         if (fcInfo.firstFont.familyName == null ||
 381             fcInfo.firstFont.fontFile == null) {
 382             return (fcInfo.compFont = jdkFont);
 383         }
 384 
 385         /* First, see if the family and exact style is already registered.
 386          * If it is, use it. If it's not, then try to register it.
 387          * If that registration fails (signalled by null) just return the
 388          * regular JDK composite.
 389          * Algorithmically styled fonts won't match on exact style, so
 390          * will fall through this code, but the regisration code will
 391          * find that file already registered and return its font.
 392          */
 393         FontFamily family = FontFamily.getFamily(fcInfo.firstFont.familyName);
 394         PhysicalFont physFont = null;
 395         if (family != null) {
 396             Font2D f2D = family.getFontWithExactStyleMatch(fcInfo.style);
 397             if (f2D instanceof PhysicalFont) {
 398                 physFont = (PhysicalFont)f2D;
 399             }
 400         }
 401 
 402         if (physFont == null ||
 403             !fcInfo.firstFont.fontFile.equals(physFont.platName)) {
 404             physFont = registerFromFcInfo(fcInfo);
 405             if (physFont == null) {
 406                 return (fcInfo.compFont = jdkFont);
 407             }
 408             family = FontFamily.getFamily(physFont.getFamilyName(null));
 409         }
 410 
 411         /* Now register the fonts in the family (the other styles) after
 412          * checking that they aren't already registered and are actually in
 413          * a different file. They may be the same file in CJK cases.
 414          * For cases where they are different font files - eg as is common for
 415          * Latin fonts, then we rely on fontconfig to report these correctly.
 416          * Assume that all styles of this font are found by fontconfig,
 417          * so we can find all the family members which must be registered
 418          * together to prevent synthetic styling.
 419          */
 420         for (int i=0; i<fontConfigFonts.length; i++) {
 421             FcCompFont fc = fontConfigFonts[i];
 422             if (fc != fcInfo &&
 423                 physFont.getFamilyName(null).equals(fc.firstFont.familyName) &&
 424                 !fc.firstFont.fontFile.equals(physFont.platName) &&
 425                 family.getFontWithExactStyleMatch(fc.style) == null) {
 426 
 427                 registerFromFcInfo(fontConfigFonts[i]);
 428             }
 429         }
 430 
 431         /* Now we have a physical font. We will back this up with the JDK
 432          * logical font (sansserif, serif, or monospaced) that corresponds
 433          * to the Pango/GTK/FC logical font name.
 434          */
 435         return (fcInfo.compFont = new CompositeFont(physFont, jdkFont));
 436     }
 437 
 438     public FcCompFont[] getFontConfigFonts() {
 439         return fontConfigFonts;
 440     }
 441 
 442     /* Return an array of FcCompFont structs describing the primary
 443      * font located for each of fontconfig/GTK/Pango's logical font names.
 444      */
 445     private static native void getFontConfig(String locale,
 446                                              FontConfigInfo fcInfo,
 447                                              FcCompFont[] fonts,
 448                                              boolean includeFallbacks);
 449 
 450     void populateFontConfig(FcCompFont[] fcInfo) {
 451         fontConfigFonts = fcInfo;
 452     }
 453 
 454     FcCompFont[] loadFontConfig() {
 455         initFontConfigFonts(true);
 456         return fontConfigFonts;
 457     }
 458 
 459     FontConfigInfo getFontConfigInfo() {
 460         initFontConfigFonts(true);
 461         return fcInfo;
 462     }
 463 
 464     private static native int
 465     getFontConfigAASettings(String locale, String fcFamily);
 466 }