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