Path: blob/master/src/java.desktop/windows/classes/sun/awt/Win32FontManager.java
41153 views
/*1* Copyright (c) 2008, 2021, 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;4344/**45* The X11 implementation of {@link FontManager}.46*/47@SuppressWarnings("removal")48public final class Win32FontManager extends SunFontManager {4950private static TrueTypeFont eudcFont;5152static {5354AccessController.doPrivileged(new PrivilegedAction<Object>() {5556public Object run() {57String eudcFile = getEUDCFontFile();58if (eudcFile != null) {59try {60/* Must use Java rasteriser since GDI doesn't61* enumerate (allow direct use) of EUDC fonts.62*/63eudcFont = new TrueTypeFont(eudcFile, null, 0,64true, false);65} catch (FontFormatException e) {66}67}68return null;69}7071});72}7374/* Used on Windows to obtain from the windows registry the name75* of a file containing the system EUFC font. If running in one of76* the locales for which this applies, and one is defined, the font77* defined by this file is appended to all composite fonts as a78* fallback component.79*/80private static native String getEUDCFontFile();8182public TrueTypeFont getEUDCFont() {83return eudcFont;84}8586public Win32FontManager() {87super();88AccessController.doPrivileged(new PrivilegedAction<Object>() {89public Object run() {9091/* Register the JRE fonts so that the native platform can92* access them. This is used only on Windows so that when93* printing the printer driver can access the fonts.94*/95registerJREFontsWithPlatform(jreFontDirName);96return null;97}98});99}100101/**102* Whether registerFontFile expects absolute or relative103* font file names.104*/105protected boolean useAbsoluteFontFileNames() {106return false;107}108109/* Unlike the shared code version, this expects a base file name -110* not a full path name.111* The font configuration file has base file names and the FontConfiguration112* class reports these back to the GraphicsEnvironment, so these113* are the componentFileNames of CompositeFonts.114*/115protected void registerFontFile(String fontFileName, String[] nativeNames,116int fontRank, boolean defer) {117118// REMIND: case compare depends on platform119if (registeredFontFiles.contains(fontFileName)) {120return;121}122registeredFontFiles.add(fontFileName);123124int fontFormat;125if (getTrueTypeFilter().accept(null, fontFileName)) {126fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;127} else if (getType1Filter().accept(null, fontFileName)) {128fontFormat = SunFontManager.FONTFORMAT_TYPE1;129} else {130/* on windows we don't use/register native fonts */131return;132}133134if (fontPath == null) {135fontPath = getPlatformFontPath(noType1Font);136}137138/* Look in the JRE font directory first.139* This is playing it safe as we would want to find fonts in the140* JRE font directory ahead of those in the system directory141*/142String tmpFontPath = jreFontDirName+File.pathSeparator+fontPath;143StringTokenizer parser = new StringTokenizer(tmpFontPath,144File.pathSeparator);145146boolean found = false;147try {148while (!found && parser.hasMoreTokens()) {149String newPath = parser.nextToken();150boolean isJREFont = newPath.equals(jreFontDirName);151File theFile = new File(newPath, fontFileName);152if (theFile.canRead()) {153found = true;154String path = theFile.getAbsolutePath();155if (defer) {156registerDeferredFont(fontFileName, path,157nativeNames,158fontFormat, isJREFont,159fontRank);160} else {161registerFontFile(path, nativeNames,162fontFormat, isJREFont,163fontRank);164}165break;166}167}168} catch (NoSuchElementException e) {169System.err.println(e);170}171if (!found) {172addToMissingFontFileList(fontFileName);173}174}175176@Override177protected FontConfiguration createFontConfiguration() {178179FontConfiguration fc = new WFontConfiguration(this);180fc.init();181return fc;182}183184@Override185public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,186boolean preferPropFonts) {187188return new WFontConfiguration(this,189preferLocaleFonts,preferPropFonts);190}191192protected void193populateFontFileNameMap(HashMap<String,String> fontToFileMap,194HashMap<String,String> fontToFamilyNameMap,195HashMap<String,ArrayList<String>>196familyToFontListMap,197Locale locale) {198199populateFontFileNameMap0(fontToFileMap, fontToFamilyNameMap,200familyToFontListMap, locale);201202}203204private static native void205populateFontFileNameMap0(HashMap<String,String> fontToFileMap,206HashMap<String,String> fontToFamilyNameMap,207HashMap<String,ArrayList<String>>208familyToFontListMap,209Locale locale);210211protected synchronized native String getFontPath(boolean noType1Fonts);212213@Override214protected String[] getDefaultPlatformFont() {215String[] info = new String[2];216info[0] = "Arial";217info[1] = "c:\\windows\\fonts";218final String[] dirs = getPlatformFontDirs(true);219if (dirs.length > 1) {220String dir = (String)221AccessController.doPrivileged(new PrivilegedAction<Object>() {222public Object run() {223for (int i=0; i<dirs.length; i++) {224String path =225dirs[i] + File.separator + "arial.ttf";226File file = new File(path);227if (file.exists()) {228return dirs[i];229}230}231return null;232}233});234if (dir != null) {235info[1] = dir;236}237} else {238info[1] = dirs[0];239}240info[1] = info[1] + File.separator + "arial.ttf";241return info;242}243244/* register only TrueType/OpenType fonts245* Because these need to be registed just for use when printing,246* we defer the actual registration and the static initialiser247* for the printing class makes the call to registerJREFontsForPrinting()248*/249static String fontsForPrinting = null;250protected void registerJREFontsWithPlatform(String pathName) {251fontsForPrinting = pathName;252}253254public static void registerJREFontsForPrinting() {255final String pathName;256synchronized (Win32GraphicsEnvironment.class) {257GraphicsEnvironment.getLocalGraphicsEnvironment();258if (fontsForPrinting == null) {259return;260}261pathName = fontsForPrinting;262fontsForPrinting = null;263}264java.security.AccessController.doPrivileged(265new java.security.PrivilegedAction<Object>() {266public Object run() {267File f1 = new File(pathName);268String[] ls = f1.list(SunFontManager.getInstance().269getTrueTypeFilter());270if (ls == null) {271return null;272}273for (int i=0; i <ls.length; i++ ) {274File fontFile = new File(f1, ls[i]);275registerFontWithPlatform(fontFile.getAbsolutePath());276}277return null;278}279});280}281282private static native void registerFontWithPlatform(String fontName);283284private static native void deRegisterFontWithPlatform(String fontName);285286/**287* populate the map with the most common windows fonts.288*/289@Override290public HashMap<String, FamilyDescription> populateHardcodedFileNameMap() {291HashMap<String, FamilyDescription> platformFontMap292= new HashMap<String, FamilyDescription>();293FamilyDescription fd;294295/* Segoe UI is the default UI font for Vista and later, and296* is used by the Win L&F which is used by FX too.297* Tahoma is used for the Win L&F on XP.298* Verdana is used in some FX UI controls.299*/300fd = new FamilyDescription();301fd.familyName = "Segoe UI";302fd.plainFullName = "Segoe UI";303fd.plainFileName = "segoeui.ttf";304fd.boldFullName = "Segoe UI Bold";305fd.boldFileName = "segoeuib.ttf";306fd.italicFullName = "Segoe UI Italic";307fd.italicFileName = "segoeuii.ttf";308fd.boldItalicFullName = "Segoe UI Bold Italic";309fd.boldItalicFileName = "segoeuiz.ttf";310platformFontMap.put("segoe", fd);311312fd = new FamilyDescription();313fd.familyName = "Tahoma";314fd.plainFullName = "Tahoma";315fd.plainFileName = "tahoma.ttf";316fd.boldFullName = "Tahoma Bold";317fd.boldFileName = "tahomabd.ttf";318platformFontMap.put("tahoma", fd);319320fd = new FamilyDescription();321fd.familyName = "Verdana";322fd.plainFullName = "Verdana";323fd.plainFileName = "verdana.TTF";324fd.boldFullName = "Verdana Bold";325fd.boldFileName = "verdanab.TTF";326fd.italicFullName = "Verdana Italic";327fd.italicFileName = "verdanai.TTF";328fd.boldItalicFullName = "Verdana Bold Italic";329fd.boldItalicFileName = "verdanaz.TTF";330platformFontMap.put("verdana", fd);331332/* The following are important because they are the core333* members of the default "Dialog" font.334*/335fd = new FamilyDescription();336fd.familyName = "Arial";337fd.plainFullName = "Arial";338fd.plainFileName = "ARIAL.TTF";339fd.boldFullName = "Arial Bold";340fd.boldFileName = "ARIALBD.TTF";341fd.italicFullName = "Arial Italic";342fd.italicFileName = "ARIALI.TTF";343fd.boldItalicFullName = "Arial Bold Italic";344fd.boldItalicFileName = "ARIALBI.TTF";345platformFontMap.put("arial", fd);346347fd = new FamilyDescription();348fd.familyName = "Symbol";349fd.plainFullName = "Symbol";350fd.plainFileName = "Symbol.TTF";351platformFontMap.put("symbol", fd);352353fd = new FamilyDescription();354fd.familyName = "WingDings";355fd.plainFullName = "WingDings";356fd.plainFileName = "WINGDING.TTF";357platformFontMap.put("wingdings", fd);358359return platformFontMap;360}361}362363364