1 /*
2  * Copyright (c) 2015, 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.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 
getFontConfigManager()39     public synchronized FontConfigManager getFontConfigManager() {
40 
41         if (fcManager == null) {
42             fcManager = new FontConfigManager();
43         }
44 
45         return fcManager;
46     }
47 
48     @Override
createFontConfiguration()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
createFontConfiguration(boolean preferLocaleFonts, boolean preferPropFonts)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
getDefaultPlatformFont()71     protected String[] getDefaultPlatformFont() {
72         final String[] info = new String[2];
73         getFontConfigManager().initFontConfigFonts(false);
74         FontConfigManager.FcCompFont[] fontConfigFonts =
75             getFontConfigManager().getFontConfigFonts();
76         if (fontConfigFonts != null) {
77             for (int i=0; i<fontConfigFonts.length; i++) {
78                 if ("sans".equals(fontConfigFonts[i].fcFamily) &&
79                     0 == fontConfigFonts[i].style) {
80                     info[0] = fontConfigFonts[i].firstFont.fullName;
81                     info[1] = fontConfigFonts[i].firstFont.fontFile;
82                     break;
83                 }
84             }
85         }
86         /* Absolute last ditch attempt in the face of fontconfig problems.
87          * If we didn't match, pick the first, or just make something
88          * up so we don't NPE.
89          */
90         if (info[0] == null) {
91             if (fontConfigFonts != null && fontConfigFonts.length > 0 &&
92                 fontConfigFonts[0].firstFont.fontFile != null) {
93                 info[0] = fontConfigFonts[0].firstFont.fullName;
94                 info[1] = fontConfigFonts[0].firstFont.fontFile;
95             } else {
96                 info[0] = "Dialog";
97                 info[1] = "/dialog.ttf";
98             }
99         }
100         return info;
101     }
102 
getFontPathNative(boolean noType1Fonts, boolean isX11GE)103     native String getFontPathNative(boolean noType1Fonts, boolean isX11GE);
104 
getFontPath(boolean noType1Fonts)105     protected synchronized String getFontPath(boolean noType1Fonts) {
106         return getFontPathNative(noType1Fonts, false);
107     }
108 
109 }
110