Win32FontManager.java revision 1687
0N/A/*
2362N/A * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
2362N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A
0N/Apackage sun.awt;
0N/A
0N/Aimport java.awt.FontFormatException;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.io.File;
119N/Aimport java.security.AccessController;
119N/Aimport java.security.PrivilegedAction;
119N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Locale;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport java.util.StringTokenizer;
0N/A
0N/Aimport sun.awt.Win32GraphicsEnvironment;
119N/Aimport sun.awt.windows.WFontConfiguration;
119N/Aimport sun.font.FontManager;
119N/Aimport sun.font.SunFontManager;
119N/Aimport sun.font.TrueTypeFont;
119N/Aimport sun.java2d.HeadlessGraphicsEnvironment;
119N/Aimport sun.java2d.SunGraphicsEnvironment;
119N/A
119N/A/**
0N/A * The X11 implementation of {@link FontManager}.
0N/A */
0N/Apublic class Win32FontManager extends SunFontManager {
0N/A
0N/A private static String[] defaultPlatformFont = null;
0N/A
119N/A private static TrueTypeFont eudcFont;
0N/A
0N/A static {
0N/A
0N/A AccessController.doPrivileged(new PrivilegedAction() {
0N/A
0N/A public Object run() {
0N/A String eudcFile = getEUDCFontFile();
0N/A if (eudcFile != null) {
0N/A try {
0N/A eudcFont = new TrueTypeFont(eudcFile, null, 0,
0N/A true);
0N/A } catch (FontFormatException e) {
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A });
0N/A }
0N/A
119N/A /* Used on Windows to obtain from the windows registry the name
0N/A * of a file containing the system EUFC font. If running in one of
0N/A * the locales for which this applies, and one is defined, the font
0N/A * defined by this file is appended to all composite fonts as a
0N/A * fallback component.
0N/A */
0N/A private static native String getEUDCFontFile();
0N/A
119N/A public Win32FontManager() {
0N/A super();
0N/A AccessController.doPrivileged(new PrivilegedAction() {
119N/A public Object run() {
0N/A
0N/A /* Register the JRE fonts so that the native platform can
119N/A * access them. This is used only on Windows so that when
119N/A * printing the printer driver can access the fonts.
0N/A */
119N/A registerJREFontsWithPlatform(jreFontDirName);
119N/A return null;
0N/A }
119N/A });
119N/A }
0N/A
119N/A /* Unlike the shared code version, this expects a base file name -
119N/A * not a full path name.
0N/A * The font configuration file has base file names and the FontConfiguration
0N/A * class reports these back to the GraphicsEnvironment, so these
0N/A * are the componentFileNames of CompositeFonts.
0N/A */
0N/A protected void registerFontFile(String fontFileName, String[] nativeNames,
0N/A int fontRank, boolean defer) {
0N/A
0N/A // REMIND: case compare depends on platform
0N/A if (registeredFontFiles.contains(fontFileName)) {
0N/A return;
0N/A }
0N/A registeredFontFiles.add(fontFileName);
0N/A
0N/A int fontFormat;
0N/A if (getTrueTypeFilter().accept(null, fontFileName)) {
0N/A fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;
0N/A } else if (getType1Filter().accept(null, fontFileName)) {
0N/A fontFormat = SunFontManager.FONTFORMAT_TYPE1;
119N/A } else {
119N/A /* on windows we don't use/register native fonts */
0N/A return;
0N/A }
0N/A
119N/A if (fontPath == null) {
119N/A fontPath = getPlatformFontPath(noType1Font);
119N/A }
0N/A
119N/A /* Look in the JRE font directory first.
0N/A * This is playing it safe as we would want to find fonts in the
0N/A * JRE font directory ahead of those in the system directory
0N/A */
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);
}