6869705: Missing files of CR6795908, FontManager refactoring

Reviewed-by: prr, igor
This commit is contained in:
Roman Kennke 2009-08-07 19:36:28 +02:00
parent eb12553aa3
commit db5938b208
8 changed files with 5956 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.font;
import java.awt.Font;
public abstract class FontAccess {
private static FontAccess access;
public static synchronized void setFontAccess(FontAccess acc) {
if (access != null) {
throw new InternalError("Attempt to set FontAccessor twice");
}
access = acc;
}
public static synchronized FontAccess getFontAccess() {
return access;
}
public abstract Font2D getFont2D(Font f);
public abstract void setFont2D(Font f, Font2DHandle h);
public abstract void setCreatedFont(Font f);
public abstract boolean isCreatedFont(Font f);
}

View File

@ -0,0 +1,106 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.font;
import java.awt.AWTError;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.action.GetPropertyAction;
/**
* Factory class used to retrieve a valid FontManager instance for the current
* platform.
*
* A default implementation is given for Linux, Solaris and Windows.
* You can alter the behaviour of the {@link #getInstance()} method by setting
* the {@code sun.font.fontmanager} property. For example:
* {@code sun.font.fontmanager=sun.awt.X11FontManager}
*/
public final class FontManagerFactory {
/** Our singleton instance. */
private static FontManager instance = null;
private static final String DEFAULT_CLASS;
static {
if (FontUtilities.isWindows)
DEFAULT_CLASS = "sun.awt.Win32FontManager";
else
DEFAULT_CLASS = "sun.awt.X11FontManager";
}
/**
* Get a valid FontManager implementation for the current platform.
*
* @return a valid FontManager instance for the current platform
*/
public static synchronized FontManager getInstance() {
if (instance != null) {
return instance;
}
String fmClassName = AccessController.doPrivileged(
new GetPropertyAction("sun.font.fontmanager",
DEFAULT_CLASS));
try {
@SuppressWarnings("unchecked")
ClassLoader cl = (ClassLoader)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return ClassLoader.getSystemClassLoader();
}
});
@SuppressWarnings("unchecked")
Class fmClass = Class.forName(fmClassName, true, cl);
instance = (FontManager) fmClass.newInstance();
} catch (ClassNotFoundException ex) {
InternalError err = new InternalError();
err.initCause(ex);
throw err;
} catch (InstantiationException ex) {
InternalError err = new InternalError();
err.initCause(ex);
throw err;
} catch (IllegalAccessException ex) {
InternalError err = new InternalError();
err.initCause(ex);
throw err;
}
return instance;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.font;
import java.awt.Font;
import java.util.Locale;
import java.util.TreeMap;
/**
* This is an extension of the {@link FontManager} interface which has to
* be implemented on systems that want to use SunGraphicsEnvironment. It
* adds a couple of methods that are only required by SGE. Graphics
* implementations that use their own GraphicsEnvironment are not required
* to implement this and can use plain FontManager instead.
*/
public interface FontManagerForSGE extends FontManager {
/**
* Return an array of created Fonts, or null, if no fonts were created yet.
*/
public Font[] getCreatedFonts();
/**
* Similar to getCreatedFonts, but returns a TreeMap of fonts by family name.
*/
public TreeMap<String, String> getCreatedFontFamilyNames();
/**
* Returns all fonts installed in this environment.
*/
public Font[] getAllInstalledFonts();
public String[] getInstalledFontFamilyNames(Locale requestedLocale);
}

View File

@ -0,0 +1,486 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.font;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.AccessController;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.plaf.FontUIResource;
import sun.security.action.GetPropertyAction;
/**
* A collection of utility methods.
*/
public final class FontUtilities {
public static final boolean isSolaris;
public static final boolean isLinux;
public static final boolean isSolaris8;
public static final boolean isSolaris9;
public static final boolean isOpenSolaris;
public static final boolean useT2K;
public static final boolean isWindows;
public static final boolean isOpenJDK;
static final String LUCIDA_FILE_NAME = "LucidaSansRegular.ttf";
// This static initializer block figures out the OS constants.
static {
String osName = AccessController.doPrivileged(
new GetPropertyAction("os.name", "unknownOS"));
isSolaris = osName.startsWith("SunOS");
isLinux = osName.startsWith("Linux");
String t2kStr = AccessController.doPrivileged(
new GetPropertyAction("sun.java2d.font.scaler"));
if (t2kStr != null) {
useT2K = "t2k".equals(t2kStr);
} else {
useT2K = false;
}
if (isSolaris) {
String version = AccessController.doPrivileged(
new GetPropertyAction("os.version", "0.0"));
isSolaris8 = version.startsWith("5.8");
isSolaris9 = version.startsWith("5.9");
float ver = Float.parseFloat(version);
if (ver > 5.10f) {
File f = new File("/etc/release");
String line = null;
try {
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(
fis, "ISO-8859-1");
BufferedReader br = new BufferedReader(isr);
line = br.readLine();
fis.close();
} catch (Exception ex) {
// Nothing to do here.
}
if (line != null && line.indexOf("OpenSolaris") >= 0) {
isOpenSolaris = true;
} else {
isOpenSolaris = false;
}
} else {
isOpenSolaris= false;
}
} else {
isSolaris8 = false;
isSolaris9 = false;
isOpenSolaris = false;
}
isWindows = osName.startsWith("Windows");
String jreLibDirName = AccessController.doPrivileged(
new GetPropertyAction("java.home","")) + File.separator + "lib";
String jreFontDirName = jreLibDirName + File.separator + "fonts";
File lucidaFile =
new File(jreFontDirName + File.separator + LUCIDA_FILE_NAME);
isOpenJDK = !lucidaFile.exists();
}
/**
* Referenced by code in the JDK which wants to test for the
* minimum char code for which layout may be required.
* Note that even basic latin text can benefit from ligatures,
* eg "ffi" but we presently apply those only if explicitly
* requested with TextAttribute.LIGATURES_ON.
* The value here indicates the lowest char code for which failing
* to invoke layout would prevent acceptable rendering.
*/
public static final int MIN_LAYOUT_CHARCODE = 0x0300;
/**
* Referenced by code in the JDK which wants to test for the
* maximum char code for which layout may be required.
* Note this does not account for supplementary characters
* where the caller interprets 'layout' to mean any case where
* one 'char' (ie the java type char) does not map to one glyph
*/
public static final int MAX_LAYOUT_CHARCODE = 0x206F;
private static boolean debugFonts = false;
private static Logger logger = null;
private static boolean logging;
static {
String debugLevel =
System.getProperty("sun.java2d.debugfonts");
if (debugLevel != null && !debugLevel.equals("false")) {
debugFonts = true;
logger = Logger.getLogger("sun.java2d");
if (debugLevel.equals("warning")) {
logger.setLevel(Level.WARNING);
} else if (debugLevel.equals("severe")) {
logger.setLevel(Level.SEVERE);
}
}
if (debugFonts) {
logger = Logger.getLogger("sun.java2d", null);
logging = logger.getLevel() != Level.OFF;
}
}
/**
* Calls the private getFont2D() method in java.awt.Font objects.
*
* @param font the font object to call
*
* @return the Font2D object returned by Font.getFont2D()
*/
public static Font2D getFont2D(Font font) {
return FontAccess.getFontAccess().getFont2D(font);
}
/**
* If there is anything in the text which triggers a case
* where char->glyph does not map 1:1 in straightforward
* left->right ordering, then this method returns true.
* Scripts which might require it but are not treated as such
* due to JDK implementations will not return true.
* ie a 'true' return is an indication of the treatment by
* the implementation.
* Whether supplementary characters should be considered is dependent
* on the needs of the caller. Since this method accepts the 'char' type
* then such chars are always represented by a pair. From a rendering
* perspective these will all (in the cases I know of) still be one
* unicode character -> one glyph. But if a caller is using this to
* discover any case where it cannot make naive assumptions about
* the number of chars, and how to index through them, then it may
* need the option to have a 'true' return in such a case.
*/
public static boolean isComplexText(char [] chs, int start, int limit) {
for (int i = start; i < limit; i++) {
if (chs[i] < MIN_LAYOUT_CHARCODE) {
continue;
}
else if (isNonSimpleChar(chs[i])) {
return true;
}
}
return false;
}
/* This is almost the same as the method above, except it takes a
* char which means it may include undecoded surrogate pairs.
* The distinction is made so that code which needs to identify all
* cases in which we do not have a simple mapping from
* char->unicode character->glyph can be be identified.
* For example measurement cannot simply sum advances of 'chars',
* the caret in editable text cannot advance one 'char' at a time, etc.
* These callers really are asking for more than whether 'layout'
* needs to be run, they need to know if they can assume 1->1
* char->glyph mapping.
*/
public static boolean isNonSimpleChar(char ch) {
return
isComplexCharCode(ch) ||
(ch >= CharToGlyphMapper.HI_SURROGATE_START &&
ch <= CharToGlyphMapper.LO_SURROGATE_END);
}
/* If the character code falls into any of a number of unicode ranges
* where we know that simple left->right layout mapping chars to glyphs
* 1:1 and accumulating advances is going to produce incorrect results,
* we want to know this so the caller can use a more intelligent layout
* approach. A caller who cares about optimum performance may want to
* check the first case and skip the method call if its in that range.
* Although there's a lot of tests in here, knowing you can skip
* CTL saves a great deal more. The rest of the checks are ordered
* so that rather than checking explicitly if (>= start & <= end)
* which would mean all ranges would need to be checked so be sure
* CTL is not needed, the method returns as soon as it recognises
* the code point is outside of a CTL ranges.
* NOTE: Since this method accepts an 'int' it is asssumed to properly
* represent a CHARACTER. ie it assumes the caller has already
* converted surrogate pairs into supplementary characters, and so
* can handle this case and doesn't need to be told such a case is
* 'complex'.
*/
public static boolean isComplexCharCode(int code) {
if (code < MIN_LAYOUT_CHARCODE || code > MAX_LAYOUT_CHARCODE) {
return false;
}
else if (code <= 0x036f) {
// Trigger layout for combining diacriticals 0x0300->0x036f
return true;
}
else if (code < 0x0590) {
// No automatic layout for Greek, Cyrillic, Armenian.
return false;
}
else if (code <= 0x06ff) {
// Hebrew 0590 - 05ff
// Arabic 0600 - 06ff
return true;
}
else if (code < 0x0900) {
return false; // Syriac and Thaana
}
else if (code <= 0x0e7f) {
// if Indic, assume shaping for conjuncts, reordering:
// 0900 - 097F Devanagari
// 0980 - 09FF Bengali
// 0A00 - 0A7F Gurmukhi
// 0A80 - 0AFF Gujarati
// 0B00 - 0B7F Oriya
// 0B80 - 0BFF Tamil
// 0C00 - 0C7F Telugu
// 0C80 - 0CFF Kannada
// 0D00 - 0D7F Malayalam
// 0D80 - 0DFF Sinhala
// 0E00 - 0E7F if Thai, assume shaping for vowel, tone marks
return true;
}
else if (code < 0x1780) {
return false;
}
else if (code <= 0x17ff) { // 1780 - 17FF Khmer
return true;
}
else if (code < 0x200c) {
return false;
}
else if (code <= 0x200d) { // zwj or zwnj
return true;
}
else if (code >= 0x202a && code <= 0x202e) { // directional control
return true;
}
else if (code >= 0x206a && code <= 0x206f) { // directional control
return true;
}
return false;
}
public static Logger getLogger() {
return logger;
}
public static boolean isLogging() {
return logging;
}
public static boolean debugFonts() {
return debugFonts;
}
// The following methods are used by Swing.
/* Revise the implementation to in fact mean "font is a composite font.
* This ensures that Swing components will always benefit from the
* fall back fonts
*/
public static boolean fontSupportsDefaultEncoding(Font font) {
return getFont2D(font) instanceof CompositeFont;
}
/**
* This method is provided for internal and exclusive use by Swing.
*
* It may be used in conjunction with fontSupportsDefaultEncoding(Font)
* In the event that a desktop properties font doesn't directly
* support the default encoding, (ie because the host OS supports
* adding support for the current locale automatically for native apps),
* then Swing calls this method to get a font which uses the specified
* font for the code points it covers, but also supports this locale
* just as the standard composite fonts do.
* Note: this will over-ride any setting where an application
* specifies it prefers locale specific composite fonts.
* The logic for this, is that this method is used only where the user or
* application has specified that the native L&F be used, and that
* we should honour that request to use the same font as native apps use.
*
* The behaviour of this method is to construct a new composite
* Font object that uses the specified physical font as its first
* component, and adds all the components of "dialog" as fall back
* components.
* The method currently assumes that only the size and style attributes
* are set on the specified font. It doesn't copy the font transform or
* other attributes because they aren't set on a font created from
* the desktop. This will need to be fixed if use is broadened.
*
* Operations such as Font.deriveFont will work properly on the
* font returned by this method for deriving a different point size.
* Additionally it tries to support a different style by calling
* getNewComposite() below. That also supports replacing slot zero
* with a different physical font but that is expected to be "rare".
* Deriving with a different style is needed because its been shown
* that some applications try to do this for Swing FontUIResources.
* Also operations such as new Font(font.getFontName(..), Font.PLAIN, 14);
* will NOT yield the same result, as the new underlying CompositeFont
* cannot be "looked up" in the font registry.
* This returns a FontUIResource as that is the Font sub-class needed
* by Swing.
* Suggested usage is something like :
* FontUIResource fuir;
* Font desktopFont = getDesktopFont(..);
* // NOTE even if fontSupportsDefaultEncoding returns true because
* // you get Tahoma and are running in an English locale, you may
* // still want to just call getCompositeFontUIResource() anyway
* // as only then will you get fallback fonts - eg for CJK.
* if (FontManager.fontSupportsDefaultEncoding(desktopFont)) {
* fuir = new FontUIResource(..);
* } else {
* fuir = FontManager.getCompositeFontUIResource(desktopFont);
* }
* return fuir;
*/
public static FontUIResource getCompositeFontUIResource(Font font) {
FontUIResource fuir =
new FontUIResource(font.getName(),font.getStyle(),font.getSize());
Font2D font2D = FontUtilities.getFont2D(font);
if (!(font2D instanceof PhysicalFont)) {
/* Swing should only be calling this when a font is obtained
* from desktop properties, so should generally be a physical font,
* an exception might be for names like "MS Serif" which are
* automatically mapped to "Serif", so there's no need to do
* anything special in that case. But note that suggested usage
* is first to call fontSupportsDefaultEncoding(Font) and this
* method should not be called if that were to return true.
*/
return fuir;
}
FontManager fm = FontManagerFactory.getInstance();
CompositeFont dialog2D =
(CompositeFont) fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
if (dialog2D == null) { /* shouldn't happen */
return fuir;
}
PhysicalFont physicalFont = (PhysicalFont)font2D;
CompositeFont compFont = new CompositeFont(physicalFont, dialog2D);
FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
/* marking this as a created font is needed as only created fonts
* copy their creator's handles.
*/
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
/* A small "map" from GTK/fontconfig names to the equivalent JDK
* logical font name.
*/
private static final String[][] nameMap = {
{"sans", "sansserif"},
{"sans-serif", "sansserif"},
{"serif", "serif"},
{"monospace", "monospaced"}
};
public static String mapFcName(String name) {
for (int i = 0; i < nameMap.length; i++) {
if (name.equals(nameMap[i][0])) {
return nameMap[i][1];
}
}
return null;
}
/* This is called by Swing passing in a fontconfig family name
* such as "sans". In return Swing gets a FontUIResource instance
* that has queried fontconfig to resolve the font(s) used for this.
* Fontconfig will if asked return a list of fonts to give the largest
* possible code point coverage.
* For now we use only the first font returned by fontconfig, and
* back it up with the most closely matching JDK logical font.
* Essentially this means pre-pending what we return now with fontconfig's
* preferred physical font. This could lead to some duplication in cases,
* if we already included that font later. We probably should remove such
* duplicates, but it is not a significant problem. It can be addressed
* later as part of creating a Composite which uses more of the
* same fonts as fontconfig. At that time we also should pay more
* attention to the special rendering instructions fontconfig returns,
* such as whether we should prefer embedded bitmaps over antialiasing.
* There's no way to express that via a Font at present.
*/
public static FontUIResource getFontConfigFUIR(String fcFamily,
int style, int size) {
String mapped = mapFcName(fcFamily);
if (mapped == null) {
mapped = "sansserif";
}
FontUIResource fuir;
FontManager fm = FontManagerFactory.getInstance();
if (fm instanceof SunFontManager) {
SunFontManager sfm = (SunFontManager) fm;
fuir = sfm.getFontConfigFUIR(mapped, style, size);
} else {
fuir = new FontUIResource(mapped, style, size);
}
return fuir;
}
/**
* Used by windows printing to assess if a font is likely to
* be layout compatible with JDK
* TrueType fonts should be, but if they have no GPOS table,
* but do have a GSUB table, then they are probably older
* fonts GDI handles differently.
*/
public static boolean textLayoutIsCompatible(Font font) {
Font2D font2D = getFont2D(font);
if (font2D instanceof TrueTypeFont) {
TrueTypeFont ttf = (TrueTypeFont) font2D;
return
ttf.getDirectoryEntry(TrueTypeFont.GSUBTag) == null ||
ttf.getDirectoryEntry(TrueTypeFont.GPOSTag) != null;
} else {
return false;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,850 @@
package sun.awt;
import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.plaf.FontUIResource;
import sun.awt.motif.MFontConfiguration;
import sun.font.CompositeFont;
import sun.font.FontManager;
import sun.font.SunFontManager;
import sun.font.FontConfigManager;
import sun.font.FcFontConfiguration;
import sun.font.FontAccess;
import sun.font.FontUtilities;
import sun.font.NativeFont;
/**
* The X11 implementation of {@link FontManager}.
*/
public class X11FontManager extends SunFontManager {
// constants identifying XLFD and font ID fields
private static final int FOUNDRY_FIELD = 1;
private static final int FAMILY_NAME_FIELD = 2;
private static final int WEIGHT_NAME_FIELD = 3;
private static final int SLANT_FIELD = 4;
private static final int SETWIDTH_NAME_FIELD = 5;
private static final int ADD_STYLE_NAME_FIELD = 6;
private static final int PIXEL_SIZE_FIELD = 7;
private static final int POINT_SIZE_FIELD = 8;
private static final int RESOLUTION_X_FIELD = 9;
private static final int RESOLUTION_Y_FIELD = 10;
private static final int SPACING_FIELD = 11;
private static final int AVERAGE_WIDTH_FIELD = 12;
private static final int CHARSET_REGISTRY_FIELD = 13;
private static final int CHARSET_ENCODING_FIELD = 14;
/*
* fontNameMap is a map from a fontID (which is a substring of an XLFD like
* "-monotype-arial-bold-r-normal-iso8859-7")
* to font file path like
* /usr/openwin/lib/locale/iso_8859_7/X11/fonts/TrueType/ArialBoldItalic.ttf
* It's used in a couple of methods like
* getFileNameFomPlatformName(..) to help locate the font file.
* We use this substring of a full XLFD because the font configuration files
* define the XLFDs in a way that's easier to make into a request.
* E.g., the -0-0-0-0-p-0- reported by X is -*-%d-*-*-p-*- in the font
* configuration files. We need to remove that part for comparisons.
*/
private static Map fontNameMap = new HashMap();
/*
* xlfdMap is a map from a platform path like
* /usr/openwin/lib/locale/ja/X11/fonts/TT/HG-GothicB.ttf to an XLFD like
* "-ricoh-hg gothic b-medium-r-normal--0-0-0-0-m-0-jisx0201.1976-0"
* Because there may be multiple native names, because the font is used
* to support multiple X encodings for example, the value of an entry in
* this map is always a vector where we store all the native names.
* For fonts which we don't understand the key isn't a pathname, its
* the full XLFD string like :-
* "-ricoh-hg gothic b-medium-r-normal--0-0-0-0-m-0-jisx0201.1976-0"
*/
private static Map xlfdMap = new HashMap();
/* xFontDirsMap is also a map from a font ID to a font filepath.
* The difference from fontNameMap is just that it does not have
* resolved symbolic links. Normally this is not interesting except
* that we need to know the directory in which a font was found to
* add it to the X font server path, since although the files may
* be linked, the fonts.dir is different and specific to the encoding
* handled by that directory. This map is nulled out after use to free
* heap space. If the optimal path is taken, such that all fonts in
* font configuration files are referenced by filename, then the font
* dir can be directly derived as its parent directory.
* If a font is used by two XLFDs, each corresponding to a different
* X11 font directory, then precautions must be taken to include both
* directories.
*/
private static Map xFontDirsMap;
/*
* This is the set of font directories needed to be on the X font path
* to enable AWT heavyweights to find all of the font configuration fonts.
* It is populated by :
* - awtfontpath entries in the fontconfig.properties
* - parent directories of "core" fonts used in the fontconfig.properties
* - looking up font dirs in the xFontDirsMap where the key is a fontID
* (cut down version of the XLFD read from the font configuration file).
* This set is nulled out after use to free heap space.
*/
private static HashSet<String> fontConfigDirs = null;
/* These maps are used on Linux where we reference the Lucida oblique
* fonts in fontconfig files even though they aren't in the standard
* font directory. This explicitly remaps the XLFDs for these to the
* correct base font. This is needed to prevent composite fonts from
* defaulting to the Lucida Sans which is a bad substitute for the
* monospaced Lucida Sans Typewriter. Also these maps prevent the
* JRE from doing wasted work at start up.
*/
HashMap<String, String> oblmap = null;
/*
* Used to eliminate redundant work. When a font directory is
* registered it added to this list. Subsequent registrations for the
* same directory can then be skipped by checking this Map.
* Access to this map is not synchronised here since creation
* of the singleton GE instance is already synchronised and that is
* the only code path that accesses this map.
*/
private static HashMap registeredDirs = new HashMap();
/* Array of directories to be added to the X11 font path.
* Used by static method called from Toolkits which use X11 fonts.
* Specifically this means MToolkit
*/
private static String[] fontdirs = null;
private static String[] defaultPlatformFont = null;
private FontConfigManager fcManager = null;
public static X11FontManager getInstance() {
return (X11FontManager) SunFontManager.getInstance();
}
/**
* Takes family name property in the following format:
* "-linotype-helvetica-medium-r-normal-sans-*-%d-*-*-p-*-iso8859-1"
* and returns the name of the corresponding physical font.
* This code is used to resolve font configuration fonts, and expects
* only to get called for these fonts.
*/
@Override
public String getFileNameFromPlatformName(String platName) {
/* If the FontConfig file doesn't use xlfds, or its
* FcFontConfiguration, this may be already a file name.
*/
if (platName.startsWith("/")) {
return platName;
}
String fileName = null;
String fontID = specificFontIDForName(platName);
/* If the font filename has been explicitly assigned in the
* font configuration file, use it. This avoids accessing
* the wrong fonts on Linux, where different fonts (some
* of which may not be usable by 2D) may share the same
* specific font ID. It may also speed up the lookup.
*/
fileName = super.getFileNameFromPlatformName(platName);
if (fileName != null) {
if (isHeadless() && fileName.startsWith("-")) {
/* if it's headless, no xlfd should be used */
return null;
}
if (fileName.startsWith("/")) {
/* If a path is assigned in the font configuration file,
* it is required that the config file also specify using the
* new awtfontpath key the X11 font directories
* which must be added to the X11 font path to support
* AWT access to that font. For that reason we no longer
* have code here to add the parent directory to the list
* of font config dirs, since the parent directory may not
* be sufficient if fonts are symbolically linked to a
* different directory.
*
* Add this XLFD (platform name) to the list of known
* ones for this file.
*/
Vector xVal = (Vector) xlfdMap.get(fileName);
if (xVal == null) {
/* Try to be robust on Linux distros which move fonts
* around by verifying that the fileName represents a
* file that exists. If it doesn't, set it to null
* to trigger a search.
*/
if (getFontConfiguration().needToSearchForFile(fileName)) {
fileName = null;
}
if (fileName != null) {
xVal = new Vector();
xVal.add(platName);
xlfdMap.put(fileName, xVal);
}
} else {
if (!xVal.contains(platName)) {
xVal.add(platName);
}
}
}
if (fileName != null) {
fontNameMap.put(fontID, fileName);
return fileName;
}
}
if (fontID != null) {
fileName = (String)fontNameMap.get(fontID);
/* On Linux check for the Lucida Oblique fonts */
if (fileName == null && FontUtilities.isLinux && !isOpenJDK()) {
if (oblmap == null) {
initObliqueLucidaFontMap();
}
String oblkey = getObliqueLucidaFontID(fontID);
if (oblkey != null) {
fileName = oblmap.get(oblkey);
}
}
if (fontPath == null &&
(fileName == null || !fileName.startsWith("/"))) {
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.warning("** Registering all font paths because " +
"can't find file for " + platName);
}
fontPath = getPlatformFontPath(noType1Font);
registerFontDirs(fontPath);
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.warning("** Finished registering all font paths");
}
fileName = (String)fontNameMap.get(fontID);
}
if (fileName == null && !isHeadless()) {
/* Query X11 directly to see if this font is available
* as a native font.
*/
fileName = getX11FontName(platName);
}
if (fileName == null) {
fontID = switchFontIDForName(platName);
fileName = (String)fontNameMap.get(fontID);
}
if (fileName != null) {
fontNameMap.put(fontID, fileName);
}
}
return fileName;
}
@Override
protected String[] getNativeNames(String fontFileName,
String platformName) {
Vector nativeNames;
if ((nativeNames=(Vector)xlfdMap.get(fontFileName))==null) {
if (platformName == null) {
return null;
} else {
/* back-stop so that at least the name used in the
* font configuration file is known as a native name
*/
String []natNames = new String[1];
natNames[0] = platformName;
return natNames;
}
} else {
int len = nativeNames.size();
return (String[])nativeNames.toArray(new String[len]);
}
}
/* NOTE: this method needs to be executed in a privileged context.
* The superclass constructor which is the primary caller of
* this method executes entirely in such a context. Additionally
* the loadFonts() method does too. So all should be well.
*/
@Override
protected void registerFontDir(String path) {
/* fonts.dir file format looks like :-
* 47
* Arial.ttf -monotype-arial-regular-r-normal--0-0-0-0-p-0-iso8859-1
* Arial-Bold.ttf -monotype-arial-bold-r-normal--0-0-0-0-p-0-iso8859-1
* ...
*/
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger().info("ParseFontDir " + path);
}
File fontsDotDir = new File(path + File.separator + "fonts.dir");
FileReader fr = null;
try {
if (fontsDotDir.canRead()) {
fr = new FileReader(fontsDotDir);
BufferedReader br = new BufferedReader(fr, 8192);
StreamTokenizer st = new StreamTokenizer(br);
st.eolIsSignificant(true);
int ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_NUMBER) {
int numEntries = (int)st.nval;
ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_EOL) {
st.resetSyntax();
st.wordChars(32, 127);
st.wordChars(128 + 32, 255);
st.whitespaceChars(0, 31);
for (int i=0; i < numEntries; i++) {
ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_EOF) {
break;
}
if (ttype != StreamTokenizer.TT_WORD) {
break;
}
int breakPos = st.sval.indexOf(' ');
if (breakPos <= 0) {
/* On TurboLinux 8.0 a fonts.dir file had
* a line with integer value "24" which
* appeared to be the number of remaining
* entries in the file. This didn't add to
* the value on the first line of the file.
* Seemed like XFree86 didn't like this line
* much either. It failed to parse the file.
* Ignore lines like this completely, and
* don't let them count as an entry.
*/
numEntries++;
ttype = st.nextToken();
if (ttype != StreamTokenizer.TT_EOL) {
break;
}
continue;
}
if (st.sval.charAt(0) == '!') {
/* TurboLinux 8.0 comment line: ignore.
* can't use st.commentChar('!') to just
* skip because this line mustn't count
* against numEntries.
*/
numEntries++;
ttype = st.nextToken();
if (ttype != StreamTokenizer.TT_EOL) {
break;
}
continue;
}
String fileName = st.sval.substring(0, breakPos);
/* TurboLinux 8.0 uses some additional syntax to
* indicate algorithmic styling values.
* Ignore ':' separated files at the beginning
* of the fileName
*/
int lastColon = fileName.lastIndexOf(':');
if (lastColon > 0) {
if (lastColon+1 >= fileName.length()) {
continue;
}
fileName = fileName.substring(lastColon+1);
}
String fontPart = st.sval.substring(breakPos+1);
String fontID = specificFontIDForName(fontPart);
String sVal = (String) fontNameMap.get(fontID);
if (FontUtilities.debugFonts()) {
Logger logger = FontUtilities.getLogger();
logger.info("file=" + fileName +
" xlfd=" + fontPart);
logger.info("fontID=" + fontID +
" sVal=" + sVal);
}
String fullPath = null;
try {
File file = new File(path,fileName);
/* we may have a resolved symbolic link
* this becomes important for an xlfd we
* still need to know the location it was
* found to update the X server font path
* for use by AWT heavyweights - and when 2D
* wants to use the native rasteriser.
*/
if (xFontDirsMap == null) {
xFontDirsMap = new HashMap();
}
xFontDirsMap.put(fontID, path);
fullPath = file.getCanonicalPath();
} catch (IOException e) {
fullPath = path + File.separator + fileName;
}
Vector xVal = (Vector) xlfdMap.get(fullPath);
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.info("fullPath=" + fullPath +
" xVal=" + xVal);
}
if ((xVal == null || !xVal.contains(fontPart)) &&
(sVal == null) || !sVal.startsWith("/")) {
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.info("Map fontID:"+fontID +
"to file:" + fullPath);
}
fontNameMap.put(fontID, fullPath);
if (xVal == null) {
xVal = new Vector();
xlfdMap.put (fullPath, xVal);
}
xVal.add(fontPart);
}
ttype = st.nextToken();
if (ttype != StreamTokenizer.TT_EOL) {
break;
}
}
}
}
fr.close();
}
} catch (IOException ioe1) {
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException ioe2) {
}
}
}
}
@Override
public void loadFonts() {
super.loadFonts();
/* These maps are greatly expanded during a loadFonts but
* can be reset to their initial state afterwards.
* Since preferLocaleFonts() and preferProportionalFonts() will
* trigger a partial repopulating from the FontConfiguration
* it has to be the inital (empty) state for the latter two, not
* simply nulling out.
* xFontDirsMap is a special case in that the implementation
* will typically not ever need to initialise it so it can be null.
*/
xFontDirsMap = null;
xlfdMap = new HashMap(1);
fontNameMap = new HashMap(1);
}
private String getObliqueLucidaFontID(String fontID) {
if (fontID.startsWith("-lucidasans-medium-i-normal") ||
fontID.startsWith("-lucidasans-bold-i-normal") ||
fontID.startsWith("-lucidatypewriter-medium-i-normal") ||
fontID.startsWith("-lucidatypewriter-bold-i-normal")) {
return fontID.substring(0, fontID.indexOf("-i-"));
} else {
return null;
}
}
private static String getX11FontName(String platName) {
String xlfd = platName.replaceAll("%d", "*");
if (NativeFont.fontExists(xlfd)) {
return xlfd;
} else {
return null;
}
}
private void initObliqueLucidaFontMap() {
oblmap = new HashMap<String, String>();
oblmap.put("-lucidasans-medium",
jreLibDirName+"/fonts/LucidaSansRegular.ttf");
oblmap.put("-lucidasans-bold",
jreLibDirName+"/fonts/LucidaSansDemiBold.ttf");
oblmap.put("-lucidatypewriter-medium",
jreLibDirName+"/fonts/LucidaTypewriterRegular.ttf");
oblmap.put("-lucidatypewriter-bold",
jreLibDirName+"/fonts/LucidaTypewriterBold.ttf");
}
private boolean isHeadless() {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
return GraphicsEnvironment.isHeadless();
}
private String specificFontIDForName(String name) {
int[] hPos = new int[14];
int hyphenCnt = 1;
int pos = 1;
while (pos != -1 && hyphenCnt < 14) {
pos = name.indexOf('-', pos);
if (pos != -1) {
hPos[hyphenCnt++] = pos;
pos++;
}
}
if (hyphenCnt != 14) {
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.severe("Font Configuration Font ID is malformed:" + name);
}
return name; // what else can we do?
}
StringBuffer sb =
new StringBuffer(name.substring(hPos[FAMILY_NAME_FIELD-1],
hPos[SETWIDTH_NAME_FIELD]));
sb.append(name.substring(hPos[CHARSET_REGISTRY_FIELD-1]));
String retval = sb.toString().toLowerCase (Locale.ENGLISH);
return retval;
}
private String switchFontIDForName(String name) {
int[] hPos = new int[14];
int hyphenCnt = 1;
int pos = 1;
while (pos != -1 && hyphenCnt < 14) {
pos = name.indexOf('-', pos);
if (pos != -1) {
hPos[hyphenCnt++] = pos;
pos++;
}
}
if (hyphenCnt != 14) {
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger()
.severe("Font Configuration Font ID is malformed:" + name);
}
return name; // what else can we do?
}
String slant = name.substring(hPos[SLANT_FIELD-1]+1,
hPos[SLANT_FIELD]);
String family = name.substring(hPos[FAMILY_NAME_FIELD-1]+1,
hPos[FAMILY_NAME_FIELD]);
String registry = name.substring(hPos[CHARSET_REGISTRY_FIELD-1]+1,
hPos[CHARSET_REGISTRY_FIELD]);
String encoding = name.substring(hPos[CHARSET_ENCODING_FIELD-1]+1);
if (slant.equals("i")) {
slant = "o";
} else if (slant.equals("o")) {
slant = "i";
}
// workaround for #4471000
if (family.equals("itc zapfdingbats")
&& registry.equals("sun")
&& encoding.equals("fontspecific")){
registry = "adobe";
}
StringBuffer sb =
new StringBuffer(name.substring(hPos[FAMILY_NAME_FIELD-1],
hPos[SLANT_FIELD-1]+1));
sb.append(slant);
sb.append(name.substring(hPos[SLANT_FIELD],
hPos[SETWIDTH_NAME_FIELD]+1));
sb.append(registry);
sb.append(name.substring(hPos[CHARSET_ENCODING_FIELD-1]));
String retval = sb.toString().toLowerCase (Locale.ENGLISH);
return retval;
}
/**
* Returns the face name for the given XLFD.
*/
public String getFileNameFromXLFD(String name) {
String fileName = null;
String fontID = specificFontIDForName(name);
if (fontID != null) {
fileName = (String)fontNameMap.get(fontID);
if (fileName == null) {
fontID = switchFontIDForName(name);
fileName = (String)fontNameMap.get(fontID);
}
if (fileName == null) {
fileName = getDefaultFontFile();
}
}
return fileName;
}
/* Register just the paths, (it doesn't register the fonts).
* If a font configuration file has specified a baseFontPath
* fontPath is just those directories, unless on usage we
* find it doesn't contain what we need for the logical fonts.
* Otherwise, we register all the paths on Solaris, because
* the fontPath we have here is the complete one from
* parsing /var/sadm/install/contents, not just
* what's on the X font path (may be this should be
* changed).
* But for now what it means is that if we didn't do
* this then if the font weren't listed anywhere on the
* less complete font path we'd trigger loadFonts which
* actually registers the fonts. This may actually be
* the right thing tho' since that would also set up
* the X font path without which we wouldn't be able to
* display some "native" fonts.
* So something to revisit is that probably fontPath
* here ought to be only the X font path + jre font dir.
* loadFonts should have a separate native call to
* get the rest of the platform font path.
*
* Registering the directories can now be avoided in the
* font configuration initialisation when filename entries
* exist in the font configuration file for all fonts.
* (Perhaps a little confusingly a filename entry is
* actually keyed using the XLFD used in the font entries,
* and it maps *to* a real filename).
* In the event any are missing, registration of all
* directories will be invoked to find the real files.
*
* But registering the directory performed other
* functions such as filling in the map of all native names
* for the font. So when this method isn't invoked, they still
* must be found. This is mitigated by getNativeNames now
* being able to return at least the platform name, but mostly
* by ensuring that when a filename key is found, that
* xlfd key is stored as one of the set of platform names
* for the font. Its a set because typical font configuration
* files reference the same CJK font files using multiple
* X11 encodings. For the code that adds this to the map
* see X11GE.getFileNameFromPlatformName(..)
* If you don't get all of these then some code points may
* not use the Xserver, and will not get the PCF bitmaps
* that are available for some point sizes.
* So, in the event that there is such a problem,
* unconditionally making this call may be necessary, at
* some cost to JRE start-up
*/
@Override
protected void registerFontDirs(String pathName) {
StringTokenizer parser = new StringTokenizer(pathName,
File.pathSeparator);
try {
while (parser.hasMoreTokens()) {
String dirPath = parser.nextToken();
if (dirPath != null && !registeredDirs.containsKey(dirPath)) {
registeredDirs.put(dirPath, null);
registerFontDir(dirPath);
}
}
} catch (NoSuchElementException e) {
}
}
// An X font spec (xlfd) includes an encoding. The same TrueType font file
// may be referenced from different X font directories in font.dir files
// to support use in multiple encodings by X apps.
// So for the purposes of font configuration logical fonts where AWT
// heavyweights need to access the font via X APIs we need to ensure that
// the directory for precisely the encodings needed by this are added to
// the x font path. This requires that we note the platform names
// specified in font configuration files and use that to identify the
// X font directory that contains a font.dir file for that platform name
// and add it to the X font path (if display is local)
// Here we make use of an already built map of xlfds to font locations
// to add the font location to the set of those required to build the
// x font path needed by AWT.
// These are added to the x font path later.
// All this is necessary because on Solaris the font.dir directories
// may contain not real font files, but symbolic links to the actual
// location but that location is not suitable for the x font path, since
// it probably doesn't have a font.dir at all and certainly not one
// with the required encodings
// If the fontconfiguration file is properly set up so that all fonts
// are mapped to files then we will never trigger initialising
// xFontDirsMap (it will be null). In this case the awtfontpath entries
// must specify all the X11 directories needed by AWT.
@Override
protected void addFontToPlatformFontPath(String platformName) {
// Lazily initialize fontConfigDirs.
getPlatformFontPathFromFontConfig();
if (xFontDirsMap != null) {
String fontID = specificFontIDForName(platformName);
String dirName = (String)xFontDirsMap.get(fontID);
if (dirName != null) {
fontConfigDirs.add(dirName);
}
}
return;
}
private void getPlatformFontPathFromFontConfig() {
if (fontConfigDirs == null) {
fontConfigDirs = getFontConfiguration().getAWTFontPathSet();
if (FontUtilities.debugFonts() && fontConfigDirs != null) {
String[] names = fontConfigDirs.toArray(new String[0]);
for (int i=0;i<names.length;i++) {
FontUtilities.getLogger().info("awtfontpath : " + names[i]);
}
}
}
}
@Override
protected void registerPlatformFontsUsedByFontConfiguration() {
// Lazily initialize fontConfigDirs.
getPlatformFontPathFromFontConfig();
if (fontConfigDirs == null) {
return;
}
if (FontUtilities.isLinux) {
fontConfigDirs.add(jreLibDirName+File.separator+"oblique-fonts");
}
fontdirs = (String[])fontConfigDirs.toArray(new String[0]);
}
/* Called by MToolkit to set the X11 font path */
public static void setNativeFontPath() {
if (fontdirs == null) {
return;
}
// need to register these individually rather than by one call
// to ensure that one bad directory doesn't cause all to be rejected
for (int i=0; i<fontdirs.length; i++) {
if (FontUtilities.debugFonts()) {
FontUtilities.getLogger().info("Add " + fontdirs[i] + " to X11 fontpath");
}
setNativeFontPath(fontdirs[i]);
}
}
private synchronized static native void setNativeFontPath(String fontPath);
// Implements SunGraphicsEnvironment.createFontConfiguration.
protected FontConfiguration createFontConfiguration() {
/* The logic here decides whether to use a preconfigured
* fontconfig.properties file, or synthesise one using platform APIs.
* On Solaris (as opposed to OpenSolaris) we try to use the
* pre-configured ones, but if the files it specifies are missing
* we fail-safe to synthesising one. This might happen if Solaris
* changes its fonts.
* For OpenSolaris I don't expect us to ever create fontconfig files,
* so it will always synthesise. Note that if we misidentify
* OpenSolaris as Solaris, then the test for the presence of
* Solaris-only font files will correct this.
* For Linux we require an exact match of distro and version to
* use the preconfigured file, and also that it points to
* existent fonts.
* If synthesising fails, we fall back to any preconfigured file
* and do the best we can. For the commercial JDK this will be
* fine as it includes the Lucida fonts. OpenJDK should not hit
* this as the synthesis should always work on its platforms.
*/
FontConfiguration mFontConfig = new MFontConfiguration(this);
if (FontUtilities.isOpenSolaris ||
(FontUtilities.isLinux &&
(!mFontConfig.foundOsSpecificFile() ||
!mFontConfig.fontFilesArePresent()) ||
(FontUtilities.isSolaris && !mFontConfig.fontFilesArePresent()))) {
FcFontConfiguration fcFontConfig =
new FcFontConfiguration(this);
if (fcFontConfig.init()) {
return fcFontConfig;
}
}
mFontConfig.init();
return mFontConfig;
}
public FontConfiguration
createFontConfiguration(boolean preferLocaleFonts,
boolean preferPropFonts) {
return new MFontConfiguration(this,
preferLocaleFonts, preferPropFonts);
}
public synchronized native String getFontPath(boolean noType1Fonts);
public String[] getDefaultPlatformFont() {
if (defaultPlatformFont != null) {
return defaultPlatformFont;
}
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";
}
}
defaultPlatformFont = info;
return defaultPlatformFont;
}
public synchronized FontConfigManager getFontConfigManager() {
if (fcManager == null) {
fcManager = new FontConfigManager();
}
return fcManager;
}
@Override
protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
if (font2D == null) { // Not expected, just a precaution.
return new FontUIResource(family, style, size);
}
/* The name of the font will be that of the physical font in slot,
* but by setting the handle to that of the CompositeFont it
* renders as that CompositeFont.
* It also needs to be marked as a created font which is the
* current mechanism to signal that deriveFont etc must copy
* the handle from the original font.
*/
FontUIResource fuir =
new FontUIResource(font2D.getFamilyName(null), style, size);
FontAccess.getFontAccess().setFont2D(fuir, font2D.handle);
FontAccess.getFontAccess().setCreatedFont(fuir);
return fuir;
}
}

View File

@ -0,0 +1,454 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.font;
import java.util.Locale;
import java.util.logging.Logger;
import sun.awt.SunHints;
import sun.awt.SunToolkit;
/**
* Small utility class to manage FontConfig.
*/
public class FontConfigManager {
static boolean fontConfigFailed = false;
/* This is populated by native */
private static final FontConfigInfo fcInfo = new FontConfigInfo();
/* 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 String familyName; // eg Bitstream Vera Sans
public String styleStr; // eg Bold
public String fullName; // eg Bitstream Vera Sans Bold
public String fontFile; // eg /usr/X11/lib/fonts/foo.ttf
}
public static class FcCompFont {
public String fcName; // eg sans
public String fcFamily; // eg sans
public String jdkName; // eg sansserif
public int style; // eg 0=PLAIN
public FontConfigFont firstFont;
public FontConfigFont[] allFonts;
//boolean preferBitmaps; // if embedded bitmaps preferred over AA
public CompositeFont compFont; // null if not yet created/known.
}
public static class FontConfigInfo {
public int fcVersion;
public String[] cacheDirs = new String[4];
}
/* 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.
*/
public static Object getFontConfigAAHint(String fcFamily) {
if (FontUtilities.isWindows) {
return null;
} else {
int hint = getFontConfigAASettings(getFCLocaleStr(), fcFamily);
if (hint < 0) {
return null;
} else {
return SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING,
hint);
}
}
}
private static String getFCLocaleStr() {
Locale l = SunToolkit.getStartupLocale();
String localeStr = l.getLanguage();
String country = l.getCountry();
if (!country.equals("")) {
localeStr = localeStr + "-" + country;
}
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) {
if (!includeFallbacks || (fontConfigFonts[0].allFonts != null)) {
return;
}
}
if (FontUtilities.isWindows || fontConfigFailed) {
return;
}
long t0 = 0;
if (FontUtilities.isLogging()) {
t0 = System.nanoTime();
}
String[] fontConfigNames = FontConfigManager.getFontConfigNames();
FcCompFont[] fontArr = new FcCompFont[fontConfigNames.length];
for (int i = 0; i< fontArr.length; i++) {
fontArr[i] = new FcCompFont();
fontArr[i].fcName = fontConfigNames[i];
int colonPos = fontArr[i].fcName.indexOf(':');
fontArr[i].fcFamily = fontArr[i].fcName.substring(0, colonPos);
fontArr[i].jdkName = FontUtilities.mapFcName(fontArr[i].fcFamily);
fontArr[i].style = i % 4; // depends on array order.
}
getFontConfig(getFCLocaleStr(), fcInfo, fontArr, includeFallbacks);
/* If don't find anything (eg no libfontconfig), then just return */
for (int i = 0; i< fontArr.length; i++) {
FcCompFont fci = fontArr[i];
if (fci.firstFont == null) {
if (FontUtilities.isLogging()) {
Logger logger = FontUtilities.getLogger();
logger.info("Fontconfig returned no fonts.");
}
fontConfigFailed = true;
return;
}
}
fontConfigFonts = fontArr;
if (FontUtilities.isLogging()) {
Logger logger = FontUtilities.getLogger();
long t1 = System.nanoTime();
logger.info("Time spent accessing fontconfig="
+ ((t1 - t0) / 1000000) + "ms.");
for (int i = 0; i< fontConfigFonts.length; i++) {
FcCompFont fci = fontConfigFonts[i];
logger.info("FC font " + fci.fcName+" maps to family " +
fci.firstFont.familyName +
" in file " + fci.firstFont.fontFile);
if (fci.allFonts != null) {
for (int f=0;f<fci.allFonts.length;f++) {
FontConfigFont fcf = fci.allFonts[f];
logger.info("Family=" + fcf.familyName +
" Style="+ fcf.styleStr +
" Fullname="+fcf.fullName +
" File="+fcf.fontFile);
}
}
}
}
}
public PhysicalFont registerFromFcInfo(FcCompFont fcInfo) {
SunFontManager fm = SunFontManager.getInstance();
/* If it's a TTC file we need to know that as we will need to
* make sure we return the right font */
String fontFile = fcInfo.firstFont.fontFile;
int offset = fontFile.length()-4;
if (offset <= 0) {
return null;
}
String ext = fontFile.substring(offset).toLowerCase();
boolean isTTC = ext.equals(".ttc");
/* 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.
*/
PhysicalFont physFont = fm.getRegisteredFontFile(fontFile);
if (physFont != null) {
if (isTTC) {
Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName,
fcInfo.style,
FontManager.NO_FALLBACK);
if (f2d instanceof PhysicalFont) { /* paranoia */
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.
*/
physFont = fm.findJREDeferredFont(fcInfo.firstFont.familyName,
fcInfo.style);
/* It is also possible the font file is on the "deferred" list,
* in which case we can just initialise it now.
*/
if (physFont == null &&
fm.isDeferredFont(fontFile) == true) {
physFont = fm.initialiseDeferredFont(fcInfo.firstFont.fontFile);
/* use findFont2D to get the right font from TTC's */
if (physFont != null) {
if (isTTC) {
Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName,
fcInfo.style,
FontManager.NO_FALLBACK);
if (f2d instanceof PhysicalFont) { /* paranoia */
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.
*/
if (physFont == null) {
int fontFormat = SunFontManager.FONTFORMAT_NONE;
int fontRank = Font2D.UNKNOWN_RANK;
if (ext.equals(".ttf") || isTTC) {
fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;
fontRank = Font2D.TTF_RANK;
} else if (ext.equals(".pfa") || ext.equals(".pfb")) {
fontFormat = SunFontManager.FONTFORMAT_TYPE1;
fontRank = Font2D.TYPE1_RANK;
}
physFont = fm.registerFontFile(fcInfo.firstFont.fontFile, null,
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.
*/
public CompositeFont getFontConfigFont(String name, int style) {
name = name.toLowerCase();
initFontConfigFonts(false);
FcCompFont fcInfo = null;
for (int i=0; i<fontConfigFonts.length; i++) {
if (name.equals(fontConfigFonts[i].fcFamily) &&
style == fontConfigFonts[i].style) {
fcInfo = fontConfigFonts[i];
break;
}
}
if (fcInfo == null) {
fcInfo = fontConfigFonts[0];
}
if (FontUtilities.isLogging()) {
FontUtilities.getLogger()
.info("FC name=" + name + " style=" + style +
" uses " + fcInfo.firstFont.familyName +
" in file: " + fcInfo.firstFont.fontFile);
}
if (fcInfo.compFont != null) {
return fcInfo.compFont;
}
/* jdkFont is going to be used for slots 1..N and as a fallback.
* Slot 0 will be the physical font from fontconfig.
*/
FontManager fm = FontManagerFactory.getInstance();
CompositeFont jdkFont = (CompositeFont)
fm.findFont2D(fcInfo.jdkName, style, FontManager.LOGICAL_FALLBACK);
if (fcInfo.firstFont.familyName == null ||
fcInfo.firstFont.fontFile == null) {
return (fcInfo.compFont = jdkFont);
}
/* 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.
*/
FontFamily family = FontFamily.getFamily(fcInfo.firstFont.familyName);
PhysicalFont physFont = null;
if (family != null) {
Font2D f2D = family.getFontWithExactStyleMatch(fcInfo.style);
if (f2D instanceof PhysicalFont) {
physFont = (PhysicalFont)f2D;
}
}
if (physFont == null ||
!fcInfo.firstFont.fontFile.equals(physFont.platName)) {
physFont = registerFromFcInfo(fcInfo);
if (physFont == null) {
return (fcInfo.compFont = jdkFont);
}
family = FontFamily.getFamily(physFont.getFamilyName(null));
}
/* 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.
*/
for (int i=0; i<fontConfigFonts.length; i++) {
FcCompFont fc = fontConfigFonts[i];
if (fc != fcInfo &&
physFont.getFamilyName(null).equals(fc.firstFont.familyName) &&
!fc.firstFont.fontFile.equals(physFont.platName) &&
family.getFontWithExactStyleMatch(fc.style) == null) {
registerFromFcInfo(fontConfigFonts[i]);
}
}
/* Now we have a physical font. We will back this up with the JDK
* logical font (sansserif, serif, or monospaced) that corresponds
* to the Pango/GTK/FC logical font name.
*/
return (fcInfo.compFont = new CompositeFont(physFont, jdkFont));
}
/**
*
* @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.
*/
private static native void getFontConfig(String locale,
FontConfigInfo fcInfo,
FcCompFont[] fonts,
boolean includeFallbacks);
void populateFontConfig(FcCompFont[] fcInfo) {
fontConfigFonts = fcInfo;
}
FcCompFont[] loadFontConfig() {
initFontConfigFonts(true);
return fontConfigFonts;
}
FontConfigInfo getFontConfigInfo() {
initFontConfigFonts(true);
return fcInfo;
}
private static native int
getFontConfigAASettings(String locale, String fcFamily);
}

View File

@ -0,0 +1,280 @@
/*
* Copyright 2008 Sun Microsystems, Inc. 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. 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.
*/
package sun.awt;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import sun.awt.Win32GraphicsEnvironment;
import sun.awt.windows.WFontConfiguration;
import sun.font.FontManager;
import sun.font.SunFontManager;
import sun.font.TrueTypeFont;
import sun.java2d.HeadlessGraphicsEnvironment;
import sun.java2d.SunGraphicsEnvironment;
/**
* The X11 implementation of {@link FontManager}.
*/
public class Win32FontManager extends SunFontManager {
private static String[] defaultPlatformFont = null;
private static TrueTypeFont eudcFont;
static {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
String eudcFile = getEUDCFontFile();
if (eudcFile != null) {
try {
eudcFont = new TrueTypeFont(eudcFile, null, 0,
true);
} catch (FontFormatException e) {
}
}
return null;
}
});
}
/* Used on Windows to obtain from the windows registry the name
* of a file containing the system EUFC font. If running in one of
* the locales for which this applies, and one is defined, the font
* defined by this file is appended to all composite fonts as a
* fallback component.
*/
private static native String getEUDCFontFile();
public Win32FontManager() {
super();
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
/* Register the JRE fonts so that the native platform can
* access them. This is used only on Windows so that when
* printing the printer driver can access the fonts.
*/
registerJREFontsWithPlatform(jreFontDirName);
return null;
}
});
}
/* Unlike the shared code version, this expects a base file name -
* not a full path name.
* The font configuration file has base file names and the FontConfiguration
* class reports these back to the GraphicsEnvironment, so these
* are the componentFileNames of CompositeFonts.
*/
protected void registerFontFile(String fontFileName, String[] nativeNames,
int fontRank, boolean defer) {
// REMIND: case compare depends on platform
if (registeredFontFiles.contains(fontFileName)) {
return;
}
registeredFontFiles.add(fontFileName);
int fontFormat;
if (getTrueTypeFilter().accept(null, fontFileName)) {
fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;
} else if (getType1Filter().accept(null, fontFileName)) {
fontFormat = SunFontManager.FONTFORMAT_TYPE1;
} else {
/* on windows we don't use/register native fonts */
return;
}
if (fontPath == null) {
fontPath = getPlatformFontPath(noType1Font);
}
/* Look in the JRE font directory first.
* This is playing it safe as we would want to find fonts in the
* JRE font directory ahead of those in the system directory
*/
String tmpFontPath = jreFontDirName+File.pathSeparator+fontPath;
StringTokenizer parser = new StringTokenizer(tmpFontPath,
File.pathSeparator);
boolean found = false;
try {
while (!found && parser.hasMoreTokens()) {
String newPath = parser.nextToken();
File theFile = new File(newPath, fontFileName);
if (theFile.canRead()) {
found = true;
String path = theFile.getAbsolutePath();
if (defer) {
registerDeferredFont(fontFileName, path,
nativeNames,
fontFormat, true,
fontRank);
} else {
registerFontFile(path, nativeNames,
fontFormat, true,
fontRank);
}
break;
}
}
} catch (NoSuchElementException e) {
System.err.println(e);
}
if (!found) {
addToMissingFontFileList(fontFileName);
}
}
@Override
protected FontConfiguration createFontConfiguration() {
FontConfiguration fc = new WFontConfiguration(this);
fc.init();
return fc;
}
@Override
public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
boolean preferPropFonts) {
return new WFontConfiguration(this,
preferLocaleFonts,preferPropFonts);
}
protected void
populateFontFileNameMap(HashMap<String,String> fontToFileMap,
HashMap<String,String> fontToFamilyNameMap,
HashMap<String,ArrayList<String>>
familyToFontListMap,
Locale locale) {
populateFontFileNameMap0(fontToFileMap, fontToFamilyNameMap,
familyToFontListMap, locale);
}
private static native void
populateFontFileNameMap0(HashMap<String,String> fontToFileMap,
HashMap<String,String> fontToFamilyNameMap,
HashMap<String,ArrayList<String>>
familyToFontListMap,
Locale locale);
public synchronized native String getFontPath(boolean noType1Fonts);
public String[] getDefaultPlatformFont() {
if (defaultPlatformFont != null) {
return defaultPlatformFont;
}
String[] info = new String[2];
info[0] = "Arial";
info[1] = "c:\\windows\\fonts";
final String[] dirs = getPlatformFontDirs(true);
if (dirs.length > 1) {
String dir = (String)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
for (int i=0; i<dirs.length; i++) {
String path =
dirs[i] + File.separator + "arial.ttf";
File file = new File(path);
if (file.exists()) {
return dirs[i];
}
}
return null;
}
});
if (dir != null) {
info[1] = dir;
}
} else {
info[1] = dirs[0];
}
info[1] = info[1] + File.separator + "arial.ttf";
defaultPlatformFont = info;
return defaultPlatformFont;
}
/* register only TrueType/OpenType fonts
* Because these need to be registed just for use when printing,
* we defer the actual registration and the static initialiser
* for the printing class makes the call to registerJREFontsForPrinting()
*/
static String fontsForPrinting = null;
protected void registerJREFontsWithPlatform(String pathName) {
fontsForPrinting = pathName;
}
public static void registerJREFontsForPrinting() {
final String pathName;
synchronized (Win32GraphicsEnvironment.class) {
GraphicsEnvironment.getLocalGraphicsEnvironment();
if (fontsForPrinting == null) {
return;
}
pathName = fontsForPrinting;
fontsForPrinting = null;
}
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
File f1 = new File(pathName);
String[] ls = f1.list(SunFontManager.getInstance().
getTrueTypeFilter());
if (ls == null) {
return null;
}
for (int i=0; i <ls.length; i++ ) {
File fontFile = new File(f1, ls[i]);
registerFontWithPlatform(fontFile.getAbsolutePath());
}
return null;
}
});
}
protected static native void registerFontWithPlatform(String fontName);
protected static native void deRegisterFontWithPlatform(String fontName);
}