8072436: Refactor X11FontManager
authorrkennke
Wed, 04 Mar 2015 15:42:02 +0100
changeset 29513 643ff69483e8
parent 29512 073c09fc07fd
child 29514 973914c573fc
8072436: Refactor X11FontManager Summary: Factor fontconfig related code out of X11FontManager into its own superclass FcFontManager. Reviewed-by: prr, serb
jdk/make/mapfiles/libawt/mapfile-mawt-vers
jdk/make/mapfiles/libawt/mapfile-vers-linux
jdk/make/mapfiles/libawt_headless/mapfile-vers
jdk/make/mapfiles/libawt_xawt/mapfile-vers
jdk/src/java.desktop/unix/classes/sun/awt/FcFontManager.java
jdk/src/java.desktop/unix/classes/sun/awt/X11FontManager.java
jdk/src/java.desktop/unix/classes/sun/font/FcFontConfiguration.java
jdk/src/java.desktop/unix/native/common/awt/fontpath.c
--- a/jdk/make/mapfiles/libawt/mapfile-mawt-vers	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/make/mapfiles/libawt/mapfile-mawt-vers	Wed Mar 04 15:42:02 2015 +0100
@@ -243,7 +243,7 @@
 		getDefaultConfig;
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
-		Java_sun_awt_X11FontManager_getFontPathNative;
+		Java_sun_awt_FcFontManager_getFontPathNative;
 		Java_sun_font_SunFontManager_populateFontFileNameMap;
 
 		# CDE private entry point
--- a/jdk/make/mapfiles/libawt/mapfile-vers-linux	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/make/mapfiles/libawt/mapfile-vers-linux	Wed Mar 04 15:42:02 2015 +0100
@@ -270,7 +270,7 @@
 		getDefaultConfig;
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
-		Java_sun_awt_X11FontManager_getFontPathNative;
+		Java_sun_awt_FcFontManager_getFontPathNative;
 		Java_sun_font_SunFontManager_populateFontFileNameMap;
 
 		# CDE private entry point
--- a/jdk/make/mapfiles/libawt_headless/mapfile-vers	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/make/mapfiles/libawt_headless/mapfile-vers	Wed Mar 04 15:42:02 2015 +0100
@@ -65,7 +65,7 @@
                 Java_sun_font_FontConfigManager_getFontConfig;
                 Java_sun_font_FontConfigManager_getFontConfigAASettings;
                 Java_sun_font_FontConfigManager_getFontConfigVersion;
-                Java_sun_awt_X11FontManager_getFontPathNative;
+                Java_sun_awt_FcFontManager_getFontPathNative;
 
 		Java_sun_awt_FontDescriptor_initIDs;
 		Java_sun_awt_PlatformFont_initIDs;
--- a/jdk/make/mapfiles/libawt_xawt/mapfile-vers	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/make/mapfiles/libawt_xawt/mapfile-vers	Wed Mar 04 15:42:02 2015 +0100
@@ -188,7 +188,7 @@
         Java_sun_font_FontConfigManager_getFontConfig;
         Java_sun_font_FontConfigManager_getFontConfigAASettings;
         Java_sun_font_FontConfigManager_getFontConfigVersion;
-	Java_sun_awt_X11FontManager_getFontPathNative;
+	Java_sun_awt_FcFontManager_getFontPathNative;
         Java_sun_awt_X11GraphicsEnvironment_initDisplay;
         Java_sun_awt_X11GraphicsEnvironment_initGLX;
 	Java_sun_awt_X11GraphicsEnvironment_initXRender;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/FcFontManager.java	Wed Mar 04 15:42:02 2015 +0100
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.awt;
+
+import sun.font.FcFontConfiguration;
+import sun.font.FontConfigManager;
+import sun.font.SunFontManager;
+
+/**
+ * A {@link sun.font.FontManager} that uses fontconfig to find system fonts.
+ */
+public class FcFontManager extends SunFontManager {
+
+    private FontConfigManager fcManager = null;
+
+    public synchronized FontConfigManager getFontConfigManager() {
+
+        if (fcManager == null) {
+            fcManager = new FontConfigManager();
+        }
+
+        return fcManager;
+    }
+
+    @Override
+    protected FontConfiguration createFontConfiguration() {
+        FcFontConfiguration fcFontConfig = new FcFontConfiguration(this);
+        if (fcFontConfig.init()) {
+            return fcFontConfig;
+        } else {
+            throw new InternalError("failed to initialize fontconfig");
+        }
+    }
+
+    @Override
+    public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
+                                                     boolean preferPropFonts) {
+        FcFontConfiguration fcFontConfig =
+            new FcFontConfiguration(this, preferLocaleFonts, preferPropFonts);
+        if (fcFontConfig.init()) {
+            return fcFontConfig;
+        } else {
+            throw new InternalError("failed to initialize fontconfig");
+        }
+    }
+
+    @Override
+    protected String[] getDefaultPlatformFont() {
+        final String[] info = new String[2];
+        getFontConfigManager().initFontConfigFonts(false);
+        FontConfigManager.FcCompFont[] fontConfigFonts =
+            getFontConfigManager().getFontConfigFonts();
+        for (int i=0; i<fontConfigFonts.length; i++) {
+            if ("sans".equals(fontConfigFonts[i].fcFamily) &&
+                0 == fontConfigFonts[i].style) {
+                info[0] = fontConfigFonts[i].firstFont.familyName;
+                info[1] = fontConfigFonts[i].firstFont.fontFile;
+                break;
+            }
+        }
+        /* Absolute last ditch attempt in the face of fontconfig problems.
+         * If we didn't match, pick the first, or just make something
+         * up so we don't NPE.
+         */
+        if (info[0] == null) {
+            if (fontConfigFonts.length > 0 &&
+                fontConfigFonts[0].firstFont.fontFile != null) {
+                info[0] = fontConfigFonts[0].firstFont.familyName;
+                info[1] = fontConfigFonts[0].firstFont.fontFile;
+            } else {
+                info[0] = "Dialog";
+                info[1] = "/dialog.ttf";
+            }
+        }
+        return info;
+    }
+
+    protected native String getFontPathNative(boolean noType1Fonts,
+                                              boolean isX11GE);
+
+    protected synchronized String getFontPath(boolean noType1Fonts) {
+        return getFontPathNative(noType1Fonts, false);
+    }
+
+}
--- a/jdk/src/java.desktop/unix/classes/sun/awt/X11FontManager.java	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/src/java.desktop/unix/classes/sun/awt/X11FontManager.java	Wed Mar 04 15:42:02 2015 +0100
@@ -54,7 +54,7 @@
 /**
  * The X11 implementation of {@link FontManager}.
  */
-public final class X11FontManager extends SunFontManager {
+public final class X11FontManager extends FcFontManager {
 
     // constants identifying XLFD and font ID fields
     private static final int FOUNDRY_FIELD = 1;
@@ -154,8 +154,6 @@
       */
      private static String[] fontdirs = null;
 
-    private FontConfigManager fcManager = null;
-
     public static X11FontManager getInstance() {
         return (X11FontManager) SunFontManager.getInstance();
     }
@@ -784,51 +782,9 @@
                                       preferLocaleFonts, preferPropFonts);
     }
 
-    public synchronized native String getFontPathNative(boolean noType1Fonts);
-
     protected synchronized String getFontPath(boolean noType1Fonts) {
         isHeadless(); // make sure GE is inited, as its the X11 lock.
-        return getFontPathNative(noType1Fonts);
-    }
-
-    @Override
-    protected String[] getDefaultPlatformFont() {
-        final String[] info = new String[2];
-        getFontConfigManager().initFontConfigFonts(false);
-        FontConfigManager.FcCompFont[] fontConfigFonts =
-            getFontConfigManager().getFontConfigFonts();
-        for (int i=0; i<fontConfigFonts.length; i++) {
-            if ("sans".equals(fontConfigFonts[i].fcFamily) &&
-                0 == fontConfigFonts[i].style) {
-                info[0] = fontConfigFonts[i].firstFont.familyName;
-                info[1] = fontConfigFonts[i].firstFont.fontFile;
-                break;
-            }
-        }
-        /* Absolute last ditch attempt in the face of fontconfig problems.
-         * If we didn't match, pick the first, or just make something
-         * up so we don't NPE.
-         */
-        if (info[0] == null) {
-            if (fontConfigFonts.length > 0 &&
-                fontConfigFonts[0].firstFont.fontFile != null) {
-                info[0] = fontConfigFonts[0].firstFont.familyName;
-                info[1] = fontConfigFonts[0].firstFont.fontFile;
-            } else {
-                info[0] = "Dialog";
-                info[1] = "/dialog.ttf";
-            }
-        }
-        return info;
-    }
-
-    public synchronized FontConfigManager getFontConfigManager() {
-
-        if (fcManager == null) {
-            fcManager = new FontConfigManager();
-        }
-
-        return fcManager;
+        return getFontPathNative(noType1Fonts, true);
     }
 
     @Override
--- a/jdk/src/java.desktop/unix/classes/sun/font/FcFontConfiguration.java	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/src/java.desktop/unix/classes/sun/font/FcFontConfiguration.java	Wed Mar 04 15:42:02 2015 +0100
@@ -39,10 +39,10 @@
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.Scanner;
+import sun.awt.FcFontManager;
 import sun.awt.FontConfiguration;
 import sun.awt.FontDescriptor;
 import sun.awt.SunToolkit;
-import sun.awt.X11FontManager;
 import sun.font.CompositeFontDescriptor;
 import sun.font.FontManager;
 import sun.font.FontConfigManager.FontConfigInfo;
@@ -92,7 +92,7 @@
 
         setFontConfiguration();
         readFcInfo();
-        X11FontManager fm = (X11FontManager) fontManager;
+        FcFontManager fm = (FcFontManager) fontManager;
         FontConfigManager fcm = fm.getFontConfigManager();
         if (fcCompFonts == null) {
             fcCompFonts = fcm.loadFontConfig();
@@ -194,7 +194,7 @@
     @Override
     public String[] getPlatformFontNames() {
         HashSet<String> nameSet = new HashSet<String>();
-        X11FontManager fm = (X11FontManager) fontManager;
+        FcFontManager fm = (FcFontManager) fontManager;
         FontConfigManager fcm = fm.getFontConfigManager();
         FcCompFont[] fcCompFonts = fcm.loadFontConfig();
         for (int i=0; i<fcCompFonts.length; i++) {
@@ -235,7 +235,7 @@
     @Override
     public CompositeFontDescriptor[] get2DCompositeFontInfo() {
 
-        X11FontManager fm = (X11FontManager) fontManager;
+        FcFontManager fm = (FcFontManager) fontManager;
         FontConfigManager fcm = fm.getFontConfigManager();
         FcCompFont[] fcCompFonts = fcm.loadFontConfig();
 
@@ -368,7 +368,7 @@
     private void writeFcInfo() {
         Properties props = new Properties();
         props.setProperty("version", fileVersion);
-        X11FontManager fm = (X11FontManager) fontManager;
+        FcFontManager fm = (FcFontManager) fontManager;
         FontConfigManager fcm = fm.getFontConfigManager();
         FontConfigInfo fcInfo = fcm.getFontConfigInfo();
         props.setProperty("fcversion", Integer.toString(fcInfo.fcVersion));
@@ -427,7 +427,7 @@
             return;
         }
         Properties props = new Properties();
-        X11FontManager fm = (X11FontManager) fontManager;
+        FcFontManager fm = (FcFontManager) fontManager;
         FontConfigManager fcm = fm.getFontConfigManager();
         try {
             FileInputStream fis = new FileInputStream(fcFile);
--- a/jdk/src/java.desktop/unix/native/common/awt/fontpath.c	Tue Mar 03 10:39:40 2015 -0800
+++ b/jdk/src/java.desktop/unix/native/common/awt/fontpath.c	Wed Mar 04 15:42:02 2015 +0100
@@ -497,7 +497,7 @@
  * This also frees us from X11 APIs as JRE is required to function in
  * a "headless" mode where there is no Xserver.
  */
-static char *getPlatformFontPathChars(JNIEnv *env, jboolean noType1) {
+static char *getPlatformFontPathChars(JNIEnv *env, jboolean noType1, jboolean isX11) {
 
     char **fcdirs = NULL, **x11dirs = NULL, **knowndirs = NULL, *path = NULL;
 
@@ -519,6 +519,7 @@
      * be initialised.
      */
 #ifndef HEADLESS
+    if (isX11) { // The following only works in an x11 environment.
 #if defined(__linux__)
     /* There's no headless build on linux ... */
     if (!AWTIsHeadless()) { /* .. so need to call a function to check */
@@ -538,6 +539,7 @@
 #if defined(__linux__)
     }
 #endif
+    }
 #endif /* !HEADLESS */
     path = mergePaths(fcdirs, x11dirs, knowndirs, noType1);
     if (fcdirs != NULL) {
@@ -555,13 +557,13 @@
     return path;
 }
 
-JNIEXPORT jstring JNICALL Java_sun_awt_X11FontManager_getFontPathNative
-(JNIEnv *env, jobject thiz, jboolean noType1) {
+JNIEXPORT jstring JNICALL Java_sun_awt_FcFontManager_getFontPathNative
+(JNIEnv *env, jobject thiz, jboolean noType1, jboolean isX11) {
     jstring ret;
     static char *ptr = NULL; /* retain result across calls */
 
     if (ptr == NULL) {
-        ptr = getPlatformFontPathChars(env, noType1);
+        ptr = getPlatformFontPathChars(env, noType1, isX11);
     }
     ret = (*env)->NewStringUTF(env, ptr);
     return ret;