1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.font; 27 28 import java.awt.*; 29 import java.io.File; 30 import java.security.AccessController; 31 import java.security.PrivilegedAction; 32 import java.util.ArrayList; 33 import java.util.HashMap; 34 import java.util.Hashtable; 35 import java.util.Locale; 36 import java.util.TreeMap; 37 import java.util.Vector; 38 39 import javax.swing.plaf.FontUIResource; 40 41 import sun.awt.FontConfiguration; 42 import sun.awt.HeadlessToolkit; 43 import sun.awt.util.ThreadGroupUtils; 44 import sun.lwawt.macosx.*; 45 46 public final class CFontManager extends SunFontManager { 47 private static Hashtable<String, Font2D> genericFonts = new Hashtable<String, Font2D>(); 48 49 @Override createFontConfiguration()50 protected FontConfiguration createFontConfiguration() { 51 FontConfiguration fc = new CFontConfiguration(this); 52 fc.init(); 53 return fc; 54 } 55 56 @Override createFontConfiguration(boolean preferLocaleFonts, boolean preferPropFonts)57 public FontConfiguration createFontConfiguration(boolean preferLocaleFonts, 58 boolean preferPropFonts) 59 { 60 return new CFontConfiguration(this, preferLocaleFonts, preferPropFonts); 61 } 62 63 /* 64 * Returns an array of two strings. The first element is the 65 * name of the font. The second element is the file name. 66 */ 67 @Override getDefaultPlatformFont()68 protected String[] getDefaultPlatformFont() { 69 return new String[]{"Lucida Grande", 70 "/System/Library/Fonts/LucidaGrande.ttc"}; 71 } 72 73 // This is a way to register any kind of Font2D, not just files and composites. getGenericFonts()74 public static Font2D[] getGenericFonts() { 75 return genericFonts.values().toArray(new Font2D[0]); 76 } 77 registerGenericFont(Font2D f)78 public Font2D registerGenericFont(Font2D f) 79 { 80 return registerGenericFont(f, false); 81 } registerGenericFont(Font2D f, boolean logicalFont)82 public Font2D registerGenericFont(Font2D f, boolean logicalFont) 83 { 84 int rank = 4; 85 86 String fontName = f.fullName; 87 String familyName = f.familyName; 88 89 if (fontName == null || "".equals(fontName)) { 90 return null; 91 } 92 93 // logical fonts always need to be added to the family 94 // plus they never need to be added to the generic font list 95 // or the fullNameToFont table since they are covers for 96 // already existing fonts in this list 97 if (logicalFont || !genericFonts.containsKey(fontName)) { 98 if (FontUtilities.debugFonts()) { 99 FontUtilities.getLogger().info("Add to Family "+familyName + 100 ", Font " + fontName + " rank="+rank); 101 } 102 FontFamily family = FontFamily.getFamily(familyName); 103 if (family == null) { 104 family = new FontFamily(familyName, false, rank); 105 family.setFont(f, f.style); 106 } else if (family.getRank() >= rank) { 107 family.setFont(f, f.style); 108 } 109 if (!logicalFont) 110 { 111 genericFonts.put(fontName, f); 112 fullNameToFont.put(fontName.toLowerCase(Locale.ENGLISH), f); 113 } 114 return f; 115 } else { 116 return genericFonts.get(fontName); 117 } 118 } 119 120 @Override getRegisteredFonts()121 public Font2D[] getRegisteredFonts() { 122 Font2D[] regFonts = super.getRegisteredFonts(); 123 124 // Add in the Mac OS X native fonts 125 Font2D[] genericFonts = getGenericFonts(); 126 Font2D[] allFonts = new Font2D[regFonts.length+genericFonts.length]; 127 System.arraycopy(regFonts, 0, allFonts, 0, regFonts.length); 128 System.arraycopy(genericFonts, 0, allFonts, regFonts.length, genericFonts.length); 129 130 return allFonts; 131 } 132 133 @Override addNativeFontFamilyNames(TreeMap<String, String> familyNames, Locale requestedLocale)134 protected void addNativeFontFamilyNames(TreeMap<String, String> familyNames, Locale requestedLocale) { 135 Font2D[] genericfonts = getGenericFonts(); 136 for (int i=0; i < genericfonts.length; i++) { 137 if (!(genericfonts[i] instanceof NativeFont)) { 138 String name = genericfonts[i].getFamilyName(requestedLocale); 139 familyNames.put(name.toLowerCase(requestedLocale), name); 140 } 141 } 142 } 143 registerFontsInDir(final String dirName, boolean useJavaRasterizer, int fontRank, boolean defer, boolean resolveSymLinks)144 protected void registerFontsInDir(final String dirName, boolean useJavaRasterizer, 145 int fontRank, boolean defer, boolean resolveSymLinks) { 146 147 String[] files = AccessController.doPrivileged((PrivilegedAction<String[]>) () -> { 148 return new File(dirName).list(getTrueTypeFilter()); 149 }); 150 151 if (files == null) { 152 return; 153 } else { 154 for (String f : files) { 155 loadNativeDirFonts(dirName+File.separator+f); 156 } 157 } 158 super.registerFontsInDir(dirName, useJavaRasterizer, fontRank, defer, resolveSymLinks); 159 } 160 loadNativeDirFonts(String fontPath)161 private native void loadNativeDirFonts(String fontPath); loadNativeFonts()162 private native void loadNativeFonts(); 163 registerFont(String fontName, String fontFamilyName)164 void registerFont(String fontName, String fontFamilyName) { 165 final CFont font = new CFont(fontName, fontFamilyName); 166 167 registerGenericFont(font); 168 } 169 registerItalicDerived()170 void registerItalicDerived() { 171 FontFamily[] famArr = FontFamily.getAllFontFamilies(); 172 for (int i=0; i<famArr.length; i++) { 173 FontFamily family = famArr[i]; 174 175 Font2D f2dPlain = family.getFont(Font.PLAIN); 176 if (f2dPlain != null && !(f2dPlain instanceof CFont)) continue; 177 Font2D f2dBold = family.getFont(Font.BOLD); 178 if (f2dBold != null && !(f2dBold instanceof CFont)) continue; 179 Font2D f2dItalic = family.getFont(Font.ITALIC); 180 if (f2dItalic != null && !(f2dItalic instanceof CFont)) continue; 181 Font2D f2dBoldItalic = family.getFont(Font.BOLD|Font.ITALIC); 182 if (f2dBoldItalic != null && !(f2dBoldItalic instanceof CFont)) continue; 183 184 CFont plain = (CFont)f2dPlain; 185 CFont bold = (CFont)f2dBold; 186 CFont italic = (CFont)f2dItalic; 187 CFont boldItalic = (CFont)f2dBoldItalic; 188 189 if (bold == null) bold = plain; 190 if (plain == null && bold == null) continue; 191 if (italic != null && boldItalic != null) continue; 192 if (plain != null && italic == null) { 193 registerGenericFont(plain.createItalicVariant(), true); 194 } 195 if (bold != null && boldItalic == null) { 196 registerGenericFont(bold.createItalicVariant(), true); 197 } 198 } 199 } 200 201 Object waitForFontsToBeLoaded = new Object(); 202 private boolean loadedAllFonts = false; 203 loadFonts()204 public void loadFonts() 205 { 206 synchronized(waitForFontsToBeLoaded) 207 { 208 super.loadFonts(); 209 java.security.AccessController.doPrivileged( 210 new java.security.PrivilegedAction<Object>() { 211 public Object run() { 212 if (!loadedAllFonts) { 213 loadNativeFonts(); 214 registerItalicDerived(); 215 loadedAllFonts = true; 216 } 217 return null; 218 } 219 } 220 ); 221 222 String defaultFont = "Lucida Grande"; 223 String defaultFallback = "Lucida Grande"; 224 225 setupLogicalFonts("Dialog", defaultFont, defaultFallback); 226 setupLogicalFonts("Serif", "Times", "Times"); 227 setupLogicalFonts("SansSerif", defaultFont, defaultFallback); 228 setupLogicalFonts("Monospaced", "Menlo", "Courier"); 229 setupLogicalFonts("DialogInput", defaultFont, defaultFallback); 230 } 231 } 232 setupLogicalFonts(String logicalName, String realName, String fallbackName)233 protected void setupLogicalFonts(String logicalName, String realName, String fallbackName) { 234 FontFamily realFamily = getFontFamilyWithExtraTry(logicalName, realName, fallbackName); 235 236 cloneStyledFont(realFamily, logicalName, Font.PLAIN); 237 cloneStyledFont(realFamily, logicalName, Font.BOLD); 238 cloneStyledFont(realFamily, logicalName, Font.ITALIC); 239 cloneStyledFont(realFamily, logicalName, Font.BOLD | Font.ITALIC); 240 } 241 getFontFamilyWithExtraTry(String logicalName, String realName, String fallbackName)242 protected FontFamily getFontFamilyWithExtraTry(String logicalName, String realName, String fallbackName){ 243 FontFamily family = getFontFamily(realName, fallbackName); 244 if (family != null) return family; 245 246 // at this point, we recognize that we probably needed a fallback font 247 super.loadFonts(); 248 249 family = getFontFamily(realName, fallbackName); 250 if (family != null) return family; 251 252 System.err.println("Warning: the fonts \"" + realName + "\" and \"" + fallbackName + "\" are not available for the Java logical font \"" + logicalName + "\", which may have unexpected appearance or behavior. Re-enable the \""+ realName +"\" font to remove this warning."); 253 return null; 254 } 255 getFontFamily(String realName, String fallbackName)256 protected FontFamily getFontFamily(String realName, String fallbackName){ 257 FontFamily family = FontFamily.getFamily(realName); 258 if (family != null) return family; 259 260 family = FontFamily.getFamily(fallbackName); 261 if (family != null){ 262 System.err.println("Warning: the font \"" + realName + "\" is not available, so \"" + fallbackName + "\" has been substituted, but may have unexpected appearance or behavor. Re-enable the \""+ realName +"\" font to remove this warning."); 263 return family; 264 } 265 266 return null; 267 } 268 cloneStyledFont(FontFamily realFamily, String logicalFamilyName, int style)269 protected boolean cloneStyledFont(FontFamily realFamily, String logicalFamilyName, int style) { 270 if (realFamily == null) return false; 271 272 Font2D realFont = realFamily.getFontWithExactStyleMatch(style); 273 if (realFont == null || !(realFont instanceof CFont)) return false; 274 275 CFont newFont = new CFont((CFont)realFont, logicalFamilyName); 276 registerGenericFont(newFont, true); 277 278 return true; 279 } 280 281 @Override getFontPath(boolean noType1Fonts)282 public String getFontPath(boolean noType1Fonts) { 283 // In the case of the Cocoa toolkit, since we go through NSFont, we don't need to register /Library/Fonts 284 Toolkit tk = Toolkit.getDefaultToolkit(); 285 if (tk instanceof HeadlessToolkit) { 286 tk = ((HeadlessToolkit)tk).getUnderlyingToolkit(); 287 } 288 if (tk instanceof LWCToolkit) { 289 return ""; 290 } 291 292 // X11 case 293 return "/Library/Fonts"; 294 } 295 296 @Override getFontConfigFUIR( String family, int style, int size)297 protected FontUIResource getFontConfigFUIR( 298 String family, int style, int size) 299 { 300 String mappedName = FontUtilities.mapFcName(family); 301 if (mappedName == null) { 302 mappedName = "sansserif"; 303 } 304 return new FontUIResource(mappedName, style, size); 305 } 306 307 // Only implemented on Windows 308 @Override populateFontFileNameMap(HashMap<String, String> fontToFileMap, HashMap<String, String> fontToFamilyNameMap, HashMap<String, ArrayList<String>> familyToFontListMap, Locale locale)309 protected void populateFontFileNameMap(HashMap<String, String> fontToFileMap, HashMap<String, String> fontToFamilyNameMap, 310 HashMap<String, ArrayList<String>> familyToFontListMap, Locale locale) {} 311 } 312