< prev index next >

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

Print this page
rev 60064 : 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


 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         }


 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 


   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


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


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

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



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


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

 358                                   " uses " + fcInfo.firstFont.familyName +
 359                                   " in file: " + fcInfo.firstFont.fontFile);
 360         }
 361 
 362         if (fcInfo.compFont != null) {
 363             return fcInfo.compFont;
 364         }
 365 
 366         /* jdkFont is going to be used for slots 1..N and as a fallback.
 367          * Slot 0 will be the physical font from fontconfig.
 368          */
 369         FontManager fm = FontManagerFactory.getInstance();
 370         CompositeFont jdkFont = (CompositeFont)
 371             fm.findFont2D(fcInfo.jdkName, style, FontManager.LOGICAL_FALLBACK);
 372 
 373         if (fcInfo.firstFont.familyName == null ||
 374             fcInfo.firstFont.fontFile == null) {
 375             return (fcInfo.compFont = jdkFont);
 376         }
 377 


< prev index next >