FontConfigManager.java revision 1689
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* Small utility class to manage FontConfig.
*/
public class FontConfigManager {
static boolean fontConfigFailed = false;
/* This is populated by native */
/* Begin support for GTK Look and Feel - query libfontconfig and
* return a composite Font to Swing that uses the desktop font(s).
*/
/* These next three classes are just data structures.
*/
public static class FontConfigFont {
}
public static class FcCompFont {
public int style; // eg 0=PLAIN
public FontConfigFont firstFont;
public FontConfigFont[] allFonts;
//boolean preferBitmaps; // if embedded bitmaps preferred over AA
}
public static class FontConfigInfo {
public int fcVersion;
}
/* fontconfig recognises slants roman, italic, as well as oblique,
* and a slew of weights, where the ones that matter here are
* regular and bold.
* To fully qualify what we want, we can for example ask for (eg)
* Font.PLAIN : "serif:regular:roman"
* Font.BOLD : "serif:bold:roman"
* Font.ITALIC : "serif:regular:italic"
* Font.BOLD|Font.ITALIC : "serif:bold:italic"
*/
private static String[] fontConfigNames = {
"sans:regular:roman",
"sans:bold:roman",
"sans:regular:italic",
"sans:bold:italic",
"serif:regular:roman",
"serif:bold:roman",
"serif:regular:italic",
"serif:bold:italic",
"monospace:regular:roman",
"monospace:bold:roman",
"monospace:regular:italic",
"monospace:bold:italic",
};
/* This array has the array elements created in Java code and is
* passed down to native to be filled in.
*/
private FcCompFont[] fontConfigFonts;
/**
* Instantiates a new FontConfigManager getting the default instance
* of FontManager from the FontManagerFactory.
*/
public FontConfigManager() {
}
public static String[] getFontConfigNames() {
return fontConfigNames;
}
/* Called from code that needs to know what are the AA settings
* that apps using FC would pick up for the default desktop font.
* Note apps can change the default desktop font. etc, so this
* isn't certain to be right but its going to correct for most cases.
* Native return values map to the text aa values in sun.awt.SunHints.
* which is used to look up the renderinghint value object.
*/
public static Object getFontConfigAAHint() {
return getFontConfigAAHint("sans");
}
/* This is public solely so that for debugging purposes it can be called
* with other names, which might (eg) include a size, eg "sans-24"
* The return value is a text aa rendering hint value.
* Normally we should call the no-args version.
*/
if (FontUtilities.isWindows) {
return null;
} else {
if (hint < 0) {
return null;
} else {
hint);
}
}
}
private static String getFCLocaleStr() {
}
return localeStr;
}
/* This does cause the native libfontconfig to be loaded and unloaded,
* but it does not incur the overhead of initialisation of its
* data structures, so shouldn't have a measurable impact.
*/
public static native int getFontConfigVersion();
/* This can be made public if it's needed to force a re-read
* rather than using the cached values. The re-read would be needed
* only if some event signalled that the fontconfig has changed.
* In that event this method would need to return directly the array
* to be used by the caller in case it subsequently changed.
*/
public synchronized void initFontConfigFonts(boolean includeFallbacks) {
if (fontConfigFonts != null) {
return;
}
}
return;
}
long t0 = 0;
if (FontUtilities.isLogging()) {
}
fontArr[i] = new FcCompFont();
}
/* If don't find anything (eg no libfontconfig), then just return */
if (FontUtilities.isLogging()) {
}
fontConfigFailed = true;
}
}
if (FontUtilities.isLogging()) {
return;
}
} else if (fontConfigFailed) {
}
}
}
if (FontUtilities.isLogging()) {
}
}
}
}
}
/* If it's a TTC file we need to know that as we will need to
* make sure we return the right font */
if (offset <= 0) {
return null;
}
/* If this file is already registered, can just return its font.
* However we do need to check in case it's a TTC as we need
* a specific font, so rather than directly returning it, let
* findFont2D resolve that.
*/
if (isTTC) {
return (PhysicalFont)f2d;
} else {
return null;
}
} else {
return physFont;
}
}
/* If the font may hide a JRE font (eg fontconfig says it is
* Lucida Sans), we want to use the JRE version, so make it
* point to the JRE font.
*/
/* It is also possible the font file is on the "deferred" list,
* in which case we can just initialise it now.
*/
/* use findFont2D to get the right font from TTC's */
if (isTTC) {
return (PhysicalFont)f2d;
} else {
return null;
}
} else {
return physFont;
}
}
}
/* In the majority of cases we reach here, and need to determine
* the type and rank to register the font.
*/
}
fontFormat, true, fontRank);
}
return physFont;
}
/*
* We need to return a Composite font which has as the font in
* its first slot one obtained from fontconfig.
*/
initFontConfigFonts(false);
fcInfo = fontConfigFonts[i];
break;
}
}
}
if (FontUtilities.isLogging()) {
}
}
/* jdkFont is going to be used for slots 1..N and as a fallback.
* Slot 0 will be the physical font from fontconfig.
*/
}
/* First, see if the family and exact style is already registered.
* If it is, use it. If it's not, then try to register it.
* If that registration fails (signalled by null) just return the
* regular JDK composite.
* Algorithmically styled fonts won't match on exact style, so
* will fall through this code, but the regisration code will
* find that file already registered and return its font.
*/
if (f2D instanceof PhysicalFont) {
}
}
}
}
/* Now register the fonts in the family (the other styles) after
* checking that they aren't already registered and are actually in
* a different file. They may be the same file in CJK cases.
* For cases where they are different font files - eg as is common for
* Latin fonts, then we rely on fontconfig to report these correctly.
* Assume that all styles of this font are found by fontconfig,
* so we can find all the family members which must be registered
* together to prevent synthetic styling.
*/
}
}
/* Now we have a physical font. We will back this up with the JDK
* logical font (sansserif, serif, or monospaced) that corresponds
*/
}
/**
*
* @param locale
* @param fcFamily
* @return
*/
public FcCompFont[] getFontConfigFonts() {
return fontConfigFonts;
}
/* Return an array of FcCompFont structs describing the primary
* font located for each of fontconfig/GTK/Pango's logical font names.
*/
FcCompFont[] fonts,
boolean includeFallbacks);
}
FcCompFont[] loadFontConfig() {
initFontConfigFonts(true);
return fontConfigFonts;
}
initFontConfigFonts(true);
return fcInfo;
}
private static native int
}