1 /*
   2  * Copyright (c) 2015 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.awt;
  27 
  28 import sun.font.FcFontConfiguration;
  29 import sun.font.FontConfigManager;
  30 import sun.font.SunFontManager;
  31 
  32 public class FcFontManager extends SunFontManager {
  33 
  34     private FontConfigManager fcManager = null;
  35 
  36     public synchronized FontConfigManager getFontConfigManager() {
  37 
  38         if (fcManager == null) {
  39             fcManager = new FontConfigManager();
  40         }
  41 
  42         return fcManager;
  43     }
  44 
  45     @Override
  46     protected FontConfiguration createFontConfiguration() {
  47         FcFontConfiguration fcFontConfig = new FcFontConfiguration(this);
  48         if (fcFontConfig.init()) {
  49             return fcFontConfig;
  50         } else {
  51             throw new InternalError();
  52         }
  53     }
  54 
  55     @Override
  56     public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
  57                                                      boolean preferPropFonts) {
  58         FcFontConfiguration fcFontConfig = new FcFontConfiguration(this, preferLocaleFonts, preferPropFonts);
  59         if (fcFontConfig.init()) {
  60             return fcFontConfig;
  61         } else {
  62             throw new InternalError();
  63         }
  64     }
  65 
  66     @Override
  67     protected String[] getDefaultPlatformFont() {
  68         final String[] info = new String[2];
  69         getFontConfigManager().initFontConfigFonts(false);
  70         FontConfigManager.FcCompFont[] fontConfigFonts =
  71             getFontConfigManager().getFontConfigFonts();
  72         for (int i=0; i<fontConfigFonts.length; i++) {
  73             if ("sans".equals(fontConfigFonts[i].fcFamily) &&
  74                 0 == fontConfigFonts[i].style) {
  75                 info[0] = fontConfigFonts[i].firstFont.familyName;
  76                 info[1] = fontConfigFonts[i].firstFont.fontFile;
  77                 break;
  78             }
  79         }
  80         /* Absolute last ditch attempt in the face of fontconfig problems.
  81          * If we didn't match, pick the first, or just make something
  82          * up so we don't NPE.
  83          */
  84         if (info[0] == null) {
  85             if (fontConfigFonts.length > 0 &&
  86                 fontConfigFonts[0].firstFont.fontFile != null) {
  87                 info[0] = fontConfigFonts[0].firstFont.familyName;
  88                 info[1] = fontConfigFonts[0].firstFont.fontFile;
  89             } else {
  90                 info[0] = "Dialog";
  91                 info[1] = "/dialog.ttf";
  92             }
  93         }
  94         return info;
  95     }
  96 
  97     protected native String getFontPathNative(boolean noType1Fonts, boolean isX11GE);
  98 
  99     protected synchronized String getFontPath(boolean noType1Fonts) {
 100         return getFontPathNative(noType1Fonts, false);
 101     }
 102 
 103 }