< prev index next >

src/java.desktop/unix/classes/sun/font/FontConfigManager.java

Print this page
rev 60042 : 8248802: Add log helper methods to FontUtilities.java
   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


 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         }


 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


   1 /*
   2  * Copyright (c) 2008, 2020, 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


 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                 FontUtilities.logInfo("Fontconfig returned no font for " + fontArr[i].fcName);




 198                 fontConfigFailed = true;
 199             } else if (anyFont == null) {
 200                 anyFont = fci.firstFont;
 201             }
 202         }
 203 
 204         if (anyFont == null) {
 205             FontUtilities.logInfo("Fontconfig returned no fonts at all.");



 206             fontConfigFailed = true;
 207             return;
 208         } else if (fontConfigFailed) {
 209             for (int i = 0; i< fontArr.length; i++) {
 210                 if (fontArr[i].firstFont == null) {
 211                     fontArr[i].firstFont = anyFont;
 212                 }
 213             }
 214         }
 215 
 216         fontConfigFonts = fontArr;
 217 
 218         if (FontUtilities.isLogging()) {



 219             long t1 = System.nanoTime();
 220             FontUtilities.logInfo("Time spent accessing fontconfig="
 221                         + ((t1 - t0) / 1000000) + "ms.");
 222 
 223             for (int i = 0; i< fontConfigFonts.length; i++) {
 224                 FcCompFont fci = fontConfigFonts[i];
 225                 FontUtilities.logInfo("FC font " + fci.fcName+" maps to family " +
 226                             fci.firstFont.familyName +
 227                             " in file " + fci.firstFont.fontFile);
 228                 if (fci.allFonts != null) {
 229                     for (int f=0;f<fci.allFonts.length;f++) {
 230                         FontConfigFont fcf = fci.allFonts[f];
 231                         FontUtilities.logInfo("Family=" + fcf.familyName +
 232                                     " Style="+ fcf.styleStr +
 233                                     " Fullname="+fcf.fullName +
 234                                     " File="+fcf.fontFile);
 235                     }
 236                 }
 237             }
 238         }
 239     }
 240 
 241     public PhysicalFont registerFromFcInfo(FcCompFont fcInfo) {
 242 
 243         SunFontManager fm = SunFontManager.getInstance();
 244 
 245         /* If it's a TTC file we need to know that as we will need to
 246          * make sure we return the right font */
 247         String fontFile = fcInfo.firstFont.fontFile;
 248         int offset = fontFile.length()-4;
 249         if (offset <= 0) {
 250             return null;
 251         }


 332 
 333         initFontConfigFonts(false);
 334         if (fontConfigFonts == null) {
 335             // This avoids an immediate NPE if fontconfig look up failed
 336             // but doesn't guarantee this is a recoverable situation.
 337             return null;
 338         }
 339 
 340         FcCompFont fcInfo = null;
 341         for (int i=0; i<fontConfigFonts.length; i++) {
 342             if (name.equals(fontConfigFonts[i].fcFamily) &&
 343                 style == fontConfigFonts[i].style) {
 344                 fcInfo = fontConfigFonts[i];
 345                 break;
 346             }
 347         }
 348         if (fcInfo == null) {
 349             fcInfo = fontConfigFonts[0];
 350         }
 351 
 352         FontUtilities.logInfo("FC name=" + name + " style=" + style +


 353                               " uses " + fcInfo.firstFont.familyName +
 354                               " in file: " + fcInfo.firstFont.fontFile);

 355 
 356         if (fcInfo.compFont != null) {
 357             return fcInfo.compFont;
 358         }
 359 
 360         /* jdkFont is going to be used for slots 1..N and as a fallback.
 361          * Slot 0 will be the physical font from fontconfig.
 362          */
 363         FontManager fm = FontManagerFactory.getInstance();
 364         CompositeFont jdkFont = (CompositeFont)
 365             fm.findFont2D(fcInfo.jdkName, style, FontManager.LOGICAL_FALLBACK);
 366 
 367         if (fcInfo.firstFont.familyName == null ||
 368             fcInfo.firstFont.fontFile == null) {
 369             return (fcInfo.compFont = jdkFont);
 370         }
 371 
 372         /* First, see if the family and exact style is already registered.
 373          * If it is, use it. If it's not, then try to register it.
 374          * If that registration fails (signalled by null) just return the


< prev index next >