Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/awt/Win32FontManager.java
32287 views
/*1* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.awt;2728import java.awt.FontFormatException;29import java.awt.GraphicsEnvironment;30import java.io.File;31import java.security.AccessController;32import java.security.PrivilegedAction;33import java.util.ArrayList;34import java.util.HashMap;35import java.util.Locale;36import java.util.NoSuchElementException;37import java.util.StringTokenizer;3839import sun.awt.windows.WFontConfiguration;40import sun.font.FontManager;41import sun.font.SunFontManager;42import sun.font.TrueTypeFont;43import sun.java2d.HeadlessGraphicsEnvironment;44import sun.java2d.SunGraphicsEnvironment;4546/**47* The X11 implementation of {@link FontManager}.48*/49public final class Win32FontManager extends SunFontManager {5051private static TrueTypeFont eudcFont;5253static {5455AccessController.doPrivileged(new PrivilegedAction() {5657public Object run() {58String eudcFile = getEUDCFontFile();59if (eudcFile != null) {60try {61/* Must use Java rasteriser since GDI doesn't62* enumerate (allow direct use) of EUDC fonts.63*/64eudcFont = new TrueTypeFont(eudcFile, null, 0,65true, false);66} catch (FontFormatException e) {67}68}69return null;70}7172});73}7475/* Used on Windows to obtain from the windows registry the name76* of a file containing the system EUFC font. If running in one of77* the locales for which this applies, and one is defined, the font78* defined by this file is appended to all composite fonts as a79* fallback component.80*/81private static native String getEUDCFontFile();8283public TrueTypeFont getEUDCFont() {84return eudcFont;85}8687public Win32FontManager() {88super();89AccessController.doPrivileged(new PrivilegedAction() {90public Object run() {9192/* Register the JRE fonts so that the native platform can93* access them. This is used only on Windows so that when94* printing the printer driver can access the fonts.95*/96registerJREFontsWithPlatform(jreFontDirName);97return null;98}99});100}101102/**103* Whether registerFontFile expects absolute or relative104* font file names.105*/106protected boolean useAbsoluteFontFileNames() {107return false;108}109110/* Unlike the shared code version, this expects a base file name -111* not a full path name.112* The font configuration file has base file names and the FontConfiguration113* class reports these back to the GraphicsEnvironment, so these114* are the componentFileNames of CompositeFonts.115*/116protected void registerFontFile(String fontFileName, String[] nativeNames,117int fontRank, boolean defer) {118119// REMIND: case compare depends on platform120if (registeredFontFiles.contains(fontFileName)) {121return;122}123registeredFontFiles.add(fontFileName);124125int fontFormat;126if (getTrueTypeFilter().accept(null, fontFileName)) {127fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;128} else if (getType1Filter().accept(null, fontFileName)) {129fontFormat = SunFontManager.FONTFORMAT_TYPE1;130} else {131/* on windows we don't use/register native fonts */132return;133}134135if (fontPath == null) {136fontPath = getPlatformFontPath(noType1Font);137}138139/* Look in the JRE font directory first.140* This is playing it safe as we would want to find fonts in the141* JRE font directory ahead of those in the system directory142*/143String tmpFontPath = jreFontDirName+File.pathSeparator+fontPath;144StringTokenizer parser = new StringTokenizer(tmpFontPath,145File.pathSeparator);146147boolean found = false;148try {149while (!found && parser.hasMoreTokens()) {150String newPath = parser.nextToken();151boolean isJREFont = newPath.equals(jreFontDirName);152File theFile = new File(newPath, fontFileName);153if (theFile.canRead()) {154found = true;155String path = theFile.getAbsolutePath();156if (defer) {157registerDeferredFont(fontFileName, path,158nativeNames,159fontFormat, isJREFont,160fontRank);161} else {162registerFontFile(path, nativeNames,163fontFormat, isJREFont,164fontRank);165}166break;167}168}169} catch (NoSuchElementException e) {170System.err.println(e);171}172if (!found) {173addToMissingFontFileList(fontFileName);174}175}176177@Override178protected FontConfiguration createFontConfiguration() {179180FontConfiguration fc = new WFontConfiguration(this);181fc.init();182return fc;183}184185@Override186public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,187boolean preferPropFonts) {188189return new WFontConfiguration(this,190preferLocaleFonts,preferPropFonts);191}192193protected void194populateFontFileNameMap(HashMap<String,String> fontToFileMap,195HashMap<String,String> fontToFamilyNameMap,196HashMap<String,ArrayList<String>>197familyToFontListMap,198Locale locale) {199200populateFontFileNameMap0(fontToFileMap, fontToFamilyNameMap,201familyToFontListMap, locale);202203}204205private static native void206populateFontFileNameMap0(HashMap<String,String> fontToFileMap,207HashMap<String,String> fontToFamilyNameMap,208HashMap<String,ArrayList<String>>209familyToFontListMap,210Locale locale);211212protected synchronized native String getFontPath(boolean noType1Fonts);213214@Override215protected String[] getDefaultPlatformFont() {216String[] info = new String[2];217info[0] = "Arial";218info[1] = "c:\\windows\\fonts";219final String[] dirs = getPlatformFontDirs(true);220if (dirs.length > 1) {221String dir = (String)222AccessController.doPrivileged(new PrivilegedAction() {223public Object run() {224for (int i=0; i<dirs.length; i++) {225String path =226dirs[i] + File.separator + "arial.ttf";227File file = new File(path);228if (file.exists()) {229return dirs[i];230}231}232return null;233}234});235if (dir != null) {236info[1] = dir;237}238} else {239info[1] = dirs[0];240}241info[1] = info[1] + File.separator + "arial.ttf";242return info;243}244245/* register only TrueType/OpenType fonts246* Because these need to be registed just for use when printing,247* we defer the actual registration and the static initialiser248* for the printing class makes the call to registerJREFontsForPrinting()249*/250static String fontsForPrinting = null;251protected void registerJREFontsWithPlatform(String pathName) {252fontsForPrinting = pathName;253}254255public static void registerJREFontsForPrinting() {256final String pathName;257synchronized (Win32GraphicsEnvironment.class) {258GraphicsEnvironment.getLocalGraphicsEnvironment();259if (fontsForPrinting == null) {260return;261}262pathName = fontsForPrinting;263fontsForPrinting = null;264}265java.security.AccessController.doPrivileged(266new java.security.PrivilegedAction() {267public Object run() {268File f1 = new File(pathName);269String[] ls = f1.list(SunFontManager.getInstance().270getTrueTypeFilter());271if (ls == null) {272return null;273}274for (int i=0; i <ls.length; i++ ) {275File fontFile = new File(f1, ls[i]);276registerFontWithPlatform(fontFile.getAbsolutePath());277}278return null;279}280});281}282283protected static native void registerFontWithPlatform(String fontName);284285protected static native void deRegisterFontWithPlatform(String fontName);286287/**288* populate the map with the most common windows fonts.289*/290@Override291public HashMap<String, FamilyDescription> populateHardcodedFileNameMap() {292HashMap<String, FamilyDescription> platformFontMap293= new HashMap<String, FamilyDescription>();294FamilyDescription fd;295296/* Segoe UI is the default UI font for Vista and later, and297* is used by the Win L&F which is used by FX too.298* Tahoma is used for the Win L&F on XP.299* Verdana is used in some FX UI controls.300*/301fd = new FamilyDescription();302fd.familyName = "Segoe UI";303fd.plainFullName = "Segoe UI";304fd.plainFileName = "segoeui.ttf";305fd.boldFullName = "Segoe UI Bold";306fd.boldFileName = "segoeuib.ttf";307fd.italicFullName = "Segoe UI Italic";308fd.italicFileName = "segoeuii.ttf";309fd.boldItalicFullName = "Segoe UI Bold Italic";310fd.boldItalicFileName = "segoeuiz.ttf";311platformFontMap.put("segoe", fd);312313fd = new FamilyDescription();314fd.familyName = "Tahoma";315fd.plainFullName = "Tahoma";316fd.plainFileName = "tahoma.ttf";317fd.boldFullName = "Tahoma Bold";318fd.boldFileName = "tahomabd.ttf";319platformFontMap.put("tahoma", fd);320321fd = new FamilyDescription();322fd.familyName = "Verdana";323fd.plainFullName = "Verdana";324fd.plainFileName = "verdana.TTF";325fd.boldFullName = "Verdana Bold";326fd.boldFileName = "verdanab.TTF";327fd.italicFullName = "Verdana Italic";328fd.italicFileName = "verdanai.TTF";329fd.boldItalicFullName = "Verdana Bold Italic";330fd.boldItalicFileName = "verdanaz.TTF";331platformFontMap.put("verdana", fd);332333/* The following are important because they are the core334* members of the default "Dialog" font.335*/336fd = new FamilyDescription();337fd.familyName = "Arial";338fd.plainFullName = "Arial";339fd.plainFileName = "ARIAL.TTF";340fd.boldFullName = "Arial Bold";341fd.boldFileName = "ARIALBD.TTF";342fd.italicFullName = "Arial Italic";343fd.italicFileName = "ARIALI.TTF";344fd.boldItalicFullName = "Arial Bold Italic";345fd.boldItalicFileName = "ARIALBI.TTF";346platformFontMap.put("arial", fd);347348fd = new FamilyDescription();349fd.familyName = "Symbol";350fd.plainFullName = "Symbol";351fd.plainFileName = "Symbol.TTF";352platformFontMap.put("symbol", fd);353354fd = new FamilyDescription();355fd.familyName = "WingDings";356fd.plainFullName = "WingDings";357fd.plainFileName = "WINGDING.TTF";358platformFontMap.put("wingdings", fd);359360return platformFontMap;361}362}363364365