1 /* 2 * Copyright (c) 2011, 2014, 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 50 protected FontConfiguration createFontConfiguration() { 51 FontConfiguration fc = new CFontConfiguration(this); 52 fc.init(); 53 return fc; 54 } 55 56 @Override 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 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. 74 public static Font2D[] getGenericFonts() { 75 return genericFonts.values().toArray(new Font2D[0]); 76 } 77 78 public Font2D registerGenericFont(Font2D f) 79 { 80 return registerGenericFont(f, false); 81 } 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 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 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 144 @Override 145 public Font2D createFont2D(File fontFile, int fontFormat, boolean isCopy, CreatedFontTracker tracker) throws FontFormatException { 146 147 String fontFilePath = fontFile.getPath(); 148 Font2D font2D = null; 149 final File fFile = fontFile; 150 final CreatedFontTracker _tracker = tracker; 151 try { 152 switch (fontFormat) { 153 case Font.TRUETYPE_FONT: 154 font2D = new TrueTypeFont(fontFilePath, null, 0, true); 155 break; 156 case Font.TYPE1_FONT: 157 font2D = new Type1Font(fontFilePath, null, isCopy); 158 break; 159 default: 160 throw new FontFormatException("Unrecognised Font Format"); 161 } 162 } catch (FontFormatException e) { 163 if (isCopy) { 164 java.security.AccessController.doPrivileged( 165 new java.security.PrivilegedAction<Object>() { 166 public Object run() { 167 if (_tracker != null) { 168 _tracker.subBytes((int)fFile.length()); 169 } 170 fFile.delete(); 171 return null; 172 } 173 }); 174 } 175 throw(e); 176 } 177 if (isCopy) { 178 FileFont.setFileToRemove(font2D, fontFile, tracker); 179 synchronized (FontManager.class) { 180 181 if (tmpFontFiles == null) { 182 tmpFontFiles = new Vector<File>(); 183 } 184 tmpFontFiles.add(fontFile); 185 186 if (fileCloser == null) { 187 final Runnable fileCloserRunnable = new Runnable() { 188 public void run() { 189 java.security.AccessController.doPrivileged( 190 new java.security.PrivilegedAction<Object>() { 191 public Object run() { 192 193 for (int i=0;i<CHANNELPOOLSIZE;i++) { 194 if (fontFileCache[i] != null) { 195 try { 196 fontFileCache[i].close(); 197 } catch (Exception e) {} 198 } 199 } 200 if (tmpFontFiles != null) { 201 File[] files = new File[tmpFontFiles.size()]; 202 files = tmpFontFiles.toArray(files); 203 for (int f=0; f<files.length;f++) { 204 try { 205 files[f].delete(); 206 } catch (Exception e) {} 207 } 208 } 209 return null; 210 } 211 }); 212 } 213 }; 214 AccessController.doPrivileged( 215 (PrivilegedAction<Void>) () -> { 216 /* The thread must be a member of a thread group 217 * which will not get GCed before VM exit. 218 * Make its parent the top-level thread group. 219 */ 220 ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup(); 221 fileCloser = new Thread(rootTG, fileCloserRunnable); 222 fileCloser.setContextClassLoader(null); 223 Runtime.getRuntime().addShutdownHook(fileCloser); 224 return null; 225 } 226 ); 227 } 228 } 229 } 230 return font2D; 231 } 232 233 protected void registerFontsInDir(String dirName, boolean useJavaRasterizer, int fontRank, boolean defer, boolean resolveSymLinks) { 234 loadNativeDirFonts(dirName); 235 super.registerFontsInDir(dirName, useJavaRasterizer, fontRank, defer, resolveSymLinks); 236 } 237 238 private native void loadNativeDirFonts(String dirName); 239 private native void loadNativeFonts(); 240 241 void registerFont(String fontName, String fontFamilyName) { 242 final CFont font = new CFont(fontName, fontFamilyName); 243 244 registerGenericFont(font); 245 } 246 247 void registerItalicDerived() { 248 FontFamily[] famArr = FontFamily.getAllFontFamilies(); 249 for (int i=0; i<famArr.length; i++) { 250 FontFamily family = famArr[i]; 251 252 Font2D f2dPlain = family.getFont(Font.PLAIN); 253 if (f2dPlain != null && !(f2dPlain instanceof CFont)) continue; 254 Font2D f2dBold = family.getFont(Font.BOLD); 255 if (f2dBold != null && !(f2dBold instanceof CFont)) continue; 256 Font2D f2dItalic = family.getFont(Font.ITALIC); 257 if (f2dItalic != null && !(f2dItalic instanceof CFont)) continue; 258 Font2D f2dBoldItalic = family.getFont(Font.BOLD|Font.ITALIC); 259 if (f2dBoldItalic != null && !(f2dBoldItalic instanceof CFont)) continue; 260 261 CFont plain = (CFont)f2dPlain; 262 CFont bold = (CFont)f2dBold; 263 CFont italic = (CFont)f2dItalic; 264 CFont boldItalic = (CFont)f2dBoldItalic; 265 266 if (bold == null) bold = plain; 267 if (plain == null && bold == null) continue; 268 if (italic != null && boldItalic != null) continue; 269 if (plain != null && italic == null) { 270 registerGenericFont(plain.createItalicVariant(), true); 271 } 272 if (bold != null && boldItalic == null) { 273 registerGenericFont(bold.createItalicVariant(), true); 274 } 275 } 276 } 277 278 Object waitForFontsToBeLoaded = new Object(); 279 private boolean loadedAllFonts = false; 280 281 public void loadFonts() 282 { 283 synchronized(waitForFontsToBeLoaded) 284 { 285 super.loadFonts(); 286 java.security.AccessController.doPrivileged( 287 new java.security.PrivilegedAction<Object>() { 288 public Object run() { 289 if (!loadedAllFonts) { 290 loadNativeFonts(); 291 registerItalicDerived(); 292 loadedAllFonts = true; 293 } 294 return null; 295 } 296 } 297 ); 298 299 String defaultFont = "Lucida Grande"; 300 String defaultFallback = "Lucida Sans"; 301 302 setupLogicalFonts("Dialog", defaultFont, defaultFallback); 303 setupLogicalFonts("Serif", "Times", "Lucida Bright"); 304 setupLogicalFonts("SansSerif", defaultFont, defaultFallback); 305 setupLogicalFonts("Monospaced", "Menlo", "Lucida Sans Typewriter"); 306 setupLogicalFonts("DialogInput", defaultFont, defaultFallback); 307 } 308 } 309 310 protected void setupLogicalFonts(String logicalName, String realName, String fallbackName) { 311 FontFamily realFamily = getFontFamilyWithExtraTry(logicalName, realName, fallbackName); 312 313 cloneStyledFont(realFamily, logicalName, Font.PLAIN); 314 cloneStyledFont(realFamily, logicalName, Font.BOLD); 315 cloneStyledFont(realFamily, logicalName, Font.ITALIC); 316 cloneStyledFont(realFamily, logicalName, Font.BOLD | Font.ITALIC); 317 } 318 319 protected FontFamily getFontFamilyWithExtraTry(String logicalName, String realName, String fallbackName){ 320 FontFamily family = getFontFamily(realName, fallbackName); 321 if (family != null) return family; 322 323 // at this point, we recognize that we probably needed a fallback font 324 super.loadFonts(); 325 326 family = getFontFamily(realName, fallbackName); 327 if (family != null) return family; 328 329 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."); 330 return null; 331 } 332 333 protected FontFamily getFontFamily(String realName, String fallbackName){ 334 FontFamily family = FontFamily.getFamily(realName); 335 if (family != null) return family; 336 337 family = FontFamily.getFamily(fallbackName); 338 if (family != null){ 339 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."); 340 return family; 341 } 342 343 return null; 344 } 345 346 protected boolean cloneStyledFont(FontFamily realFamily, String logicalFamilyName, int style) { 347 if (realFamily == null) return false; 348 349 Font2D realFont = realFamily.getFontWithExactStyleMatch(style); 350 if (realFont == null || !(realFont instanceof CFont)) return false; 351 352 CFont newFont = new CFont((CFont)realFont, logicalFamilyName); 353 registerGenericFont(newFont, true); 354 355 return true; 356 } 357 358 @Override 359 public String getFontPath(boolean noType1Fonts) { 360 // In the case of the Cocoa toolkit, since we go through NSFont, we don't need to register /Library/Fonts 361 Toolkit tk = Toolkit.getDefaultToolkit(); 362 if (tk instanceof HeadlessToolkit) { 363 tk = ((HeadlessToolkit)tk).getUnderlyingToolkit(); 364 } 365 if (tk instanceof LWCToolkit) { 366 return ""; 367 } 368 369 // X11 case 370 return "/Library/Fonts"; 371 } 372 373 @Override 374 protected FontUIResource getFontConfigFUIR( 375 String family, int style, int size) 376 { 377 String mappedName = FontUtilities.mapFcName(family); 378 if (mappedName == null) { 379 mappedName = "sansserif"; 380 } 381 return new FontUIResource(mappedName, style, size); 382 } 383 384 // Only implemented on Windows 385 @Override 386 protected void populateFontFileNameMap(HashMap<String, String> fontToFileMap, HashMap<String, String> fontToFamilyNameMap, 387 HashMap<String, ArrayList<String>> familyToFontListMap, Locale locale) {} 388 }