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 FontConfigManager fcManager = null;
  48     private static Hashtable<String, Font2D> genericFonts = new Hashtable<String, Font2D>();
  49 
  50     @Override
  51     protected FontConfiguration createFontConfiguration() {
  52         FontConfiguration fc = new CFontConfiguration(this);
  53         fc.init();
  54         return fc;
  55     }
  56 
  57     @Override
  58     public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
  59                                                      boolean preferPropFonts)
  60     {
  61         return new CFontConfiguration(this, preferLocaleFonts, preferPropFonts);
  62     }
  63 
  64     /*
  65      * Returns an array of two strings. The first element is the
  66      * name of the font. The second element is the file name.
  67      */
  68     @Override
  69     protected String[] getDefaultPlatformFont() {
  70         return new String[]{"Lucida Grande",
  71                             "/System/Library/Fonts/LucidaGrande.ttc"};
  72     }
  73 
  74     // This is a way to register any kind of Font2D, not just files and composites.
  75     public static Font2D[] getGenericFonts() {
  76         return genericFonts.values().toArray(new Font2D[0]);
  77     }
  78 
  79     public Font2D registerGenericFont(Font2D f)
  80     {
  81         return registerGenericFont(f, false);
  82     }
  83     public Font2D registerGenericFont(Font2D f, boolean logicalFont)
  84     {
  85         int rank = 4;
  86 
  87         String fontName = f.fullName;
  88         String familyName = f.familyName;
  89 
  90         if (fontName == null || "".equals(fontName)) {
  91             return null;
  92         }
  93 
  94         // logical fonts always need to be added to the family
  95         // plus they never need to be added to the generic font list
  96         // or the fullNameToFont table since they are covers for
  97         // already existing fonts in this list
  98         if (logicalFont || !genericFonts.containsKey(fontName)) {
  99             if (FontUtilities.debugFonts()) {
 100                 FontUtilities.getLogger().info("Add to Family "+familyName +
 101                     ", Font " + fontName + " rank="+rank);
 102             }
 103             FontFamily family = FontFamily.getFamily(familyName);
 104             if (family == null) {
 105                 family = new FontFamily(familyName, false, rank);
 106                 family.setFont(f, f.style);
 107             } else if (family.getRank() >= rank) {
 108                 family.setFont(f, f.style);
 109             }
 110             if (!logicalFont)
 111             {
 112                 genericFonts.put(fontName, f);
 113                 fullNameToFont.put(fontName.toLowerCase(Locale.ENGLISH), f);
 114             }
 115             return f;
 116         } else {
 117             return genericFonts.get(fontName);
 118         }
 119     }
 120 
 121     @Override
 122     public Font2D[] getRegisteredFonts() {
 123         Font2D[] regFonts = super.getRegisteredFonts();
 124 
 125         // Add in the Mac OS X native fonts
 126         Font2D[] genericFonts = getGenericFonts();
 127         Font2D[] allFonts = new Font2D[regFonts.length+genericFonts.length];
 128         System.arraycopy(regFonts, 0, allFonts, 0, regFonts.length);
 129         System.arraycopy(genericFonts, 0, allFonts, regFonts.length, genericFonts.length);
 130 
 131         return allFonts;
 132     }
 133 
 134     @Override
 135     protected void addNativeFontFamilyNames(TreeMap<String, String> familyNames, Locale requestedLocale) {
 136         Font2D[] genericfonts = getGenericFonts();
 137         for (int i=0; i < genericfonts.length; i++) {
 138             if (!(genericfonts[i] instanceof NativeFont)) {
 139                 String name = genericfonts[i].getFamilyName(requestedLocale);
 140                 familyNames.put(name.toLowerCase(requestedLocale), name);
 141             }
 142         }
 143     }
 144 
 145     @Override
 146     public Font2D createFont2D(File fontFile, int fontFormat, boolean isCopy, CreatedFontTracker tracker) throws FontFormatException {
 147 
 148     String fontFilePath = fontFile.getPath();
 149     Font2D font2D = null;
 150     final File fFile = fontFile;
 151     final CreatedFontTracker _tracker = tracker;
 152     try {
 153         switch (fontFormat) {
 154             case Font.TRUETYPE_FONT:
 155                         font2D = new TrueTypeFont(fontFilePath, null, 0, true);
 156                 break;
 157             case Font.TYPE1_FONT:
 158                         font2D = new Type1Font(fontFilePath, null, isCopy);
 159                 break;
 160             default:
 161                 throw new FontFormatException("Unrecognised Font Format");
 162         }
 163     } catch (FontFormatException e) {
 164         if (isCopy) {
 165             java.security.AccessController.doPrivileged(
 166                     new java.security.PrivilegedAction<Object>() {
 167                         public Object run() {
 168                             if (_tracker != null) {
 169                                 _tracker.subBytes((int)fFile.length());
 170                             }
 171                             fFile.delete();
 172                             return null;
 173                         }
 174                     });
 175         }
 176         throw(e);
 177     }
 178     if (isCopy) {
 179         FileFont.setFileToRemove(font2D, fontFile, tracker);
 180         synchronized (FontManager.class) {
 181 
 182             if (tmpFontFiles == null) {
 183                 tmpFontFiles = new Vector<File>();
 184             }
 185             tmpFontFiles.add(fontFile);
 186 
 187             if (fileCloser == null) {
 188                 final Runnable fileCloserRunnable = new Runnable() {
 189                     public void run() {
 190                         java.security.AccessController.doPrivileged(
 191                                 new java.security.PrivilegedAction<Object>() {
 192                                     public Object run() {
 193 
 194                                         for (int i=0;i<CHANNELPOOLSIZE;i++) {
 195                                             if (fontFileCache[i] != null) {
 196                                                 try {
 197                                                     fontFileCache[i].close();
 198                                                 } catch (Exception e) {}
 199                                             }
 200                                         }
 201                                         if (tmpFontFiles != null) {
 202                                             File[] files = new File[tmpFontFiles.size()];
 203                                             files = tmpFontFiles.toArray(files);
 204                                             for (int f=0; f<files.length;f++) {
 205                                                 try {
 206                                                     files[f].delete();
 207                                                 } catch (Exception e) {}
 208                                             }
 209                                         }
 210                                         return null;
 211                                     }
 212                                 });
 213                     }
 214                 };
 215                 AccessController.doPrivileged(
 216                         (PrivilegedAction<Void>) () -> {
 217                             /* The thread must be a member of a thread group
 218                              * which will not get GCed before VM exit.
 219                              * Make its parent the top-level thread group.
 220                              */
 221                             ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
 222                             fileCloser = new Thread(rootTG, fileCloserRunnable);
 223                             fileCloser.setContextClassLoader(null);
 224                             Runtime.getRuntime().addShutdownHook(fileCloser);
 225                             return null;
 226                         }
 227                 );
 228                 }
 229             }
 230         }
 231         return font2D;
 232     }
 233 
 234     /*
 235     public synchronized FontConfigManager getFontConfigManager() {
 236         if (fcManager  == null) {
 237             fcManager = new FontConfigManager();
 238         }
 239         return fcManager;
 240     }
 241     */
 242 
 243     protected void registerFontsInDir(String dirName, boolean useJavaRasterizer, int fontRank, boolean defer, boolean resolveSymLinks) {
 244         loadNativeDirFonts(dirName);
 245         super.registerFontsInDir(dirName, useJavaRasterizer, fontRank, defer, resolveSymLinks);
 246     }
 247 
 248     private native void loadNativeDirFonts(String dirName);
 249     private native void loadNativeFonts();
 250 
 251     void registerFont(String fontName, String fontFamilyName) {
 252         final CFont font = new CFont(fontName, fontFamilyName);
 253 
 254         registerGenericFont(font);
 255     }
 256 
 257     void registerItalicDerived() {
 258         FontFamily[] famArr = FontFamily.getAllFontFamilies();
 259         for (int i=0; i<famArr.length; i++) {
 260             FontFamily family = famArr[i];
 261 
 262             Font2D f2dPlain = family.getFont(Font.PLAIN);
 263             if (f2dPlain != null && !(f2dPlain instanceof CFont)) continue;
 264             Font2D f2dBold = family.getFont(Font.BOLD);
 265             if (f2dBold != null && !(f2dBold instanceof CFont)) continue;
 266             Font2D f2dItalic = family.getFont(Font.ITALIC);
 267             if (f2dItalic != null && !(f2dItalic instanceof CFont)) continue;
 268             Font2D f2dBoldItalic = family.getFont(Font.BOLD|Font.ITALIC);
 269             if (f2dBoldItalic != null && !(f2dBoldItalic instanceof CFont)) continue;
 270 
 271             CFont plain = (CFont)f2dPlain;
 272             CFont bold = (CFont)f2dBold;
 273             CFont italic = (CFont)f2dItalic;
 274             CFont boldItalic = (CFont)f2dBoldItalic;
 275 
 276             if (bold == null) bold = plain;
 277             if (plain == null && bold == null) continue;
 278             if (italic != null && boldItalic != null) continue;
 279             if (plain != null && italic == null) {
 280                registerGenericFont(plain.createItalicVariant(), true);
 281             }
 282             if (bold != null && boldItalic == null) {
 283                registerGenericFont(bold.createItalicVariant(), true);
 284             }
 285         }
 286     }
 287 
 288     Object waitForFontsToBeLoaded  = new Object();
 289     private boolean loadedAllFonts = false;
 290 
 291     public void loadFonts()
 292     {
 293         synchronized(waitForFontsToBeLoaded)
 294         {
 295             super.loadFonts();
 296             java.security.AccessController.doPrivileged(
 297                 new java.security.PrivilegedAction<Object>() {
 298                     public Object run() {
 299                         if (!loadedAllFonts) {
 300                            loadNativeFonts();
 301                            registerItalicDerived();
 302                            loadedAllFonts = true;
 303                         }
 304                         return null;
 305                     }
 306                 }
 307             );
 308 
 309             String defaultFont = "Lucida Grande";
 310             String defaultFallback = "Lucida Sans";
 311 
 312             setupLogicalFonts("Dialog", defaultFont, defaultFallback);
 313             setupLogicalFonts("Serif", "Times", "Lucida Bright");
 314             setupLogicalFonts("SansSerif", defaultFont, defaultFallback);
 315             setupLogicalFonts("Monospaced", "Menlo", "Lucida Sans Typewriter");
 316             setupLogicalFonts("DialogInput", defaultFont, defaultFallback);
 317         }
 318     }
 319 
 320     protected void setupLogicalFonts(String logicalName, String realName, String fallbackName) {
 321         FontFamily realFamily = getFontFamilyWithExtraTry(logicalName, realName, fallbackName);
 322 
 323         cloneStyledFont(realFamily, logicalName, Font.PLAIN);
 324         cloneStyledFont(realFamily, logicalName, Font.BOLD);
 325         cloneStyledFont(realFamily, logicalName, Font.ITALIC);
 326         cloneStyledFont(realFamily, logicalName, Font.BOLD | Font.ITALIC);
 327     }
 328 
 329     protected FontFamily getFontFamilyWithExtraTry(String logicalName, String realName, String fallbackName){
 330         FontFamily family = getFontFamily(realName, fallbackName);
 331         if (family != null) return family;
 332 
 333         // at this point, we recognize that we probably needed a fallback font
 334         super.loadFonts();
 335 
 336         family = getFontFamily(realName, fallbackName);
 337         if (family != null) return family;
 338 
 339         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.");
 340         return null;
 341     }
 342 
 343     protected FontFamily getFontFamily(String realName, String fallbackName){
 344         FontFamily family = FontFamily.getFamily(realName);
 345         if (family != null) return family;
 346 
 347         family = FontFamily.getFamily(fallbackName);
 348         if (family != null){
 349             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.");
 350             return family;
 351         }
 352 
 353         return null;
 354     }
 355 
 356     protected boolean cloneStyledFont(FontFamily realFamily, String logicalFamilyName, int style) {
 357         if (realFamily == null) return false;
 358 
 359         Font2D realFont = realFamily.getFontWithExactStyleMatch(style);
 360         if (realFont == null || !(realFont instanceof CFont)) return false;
 361 
 362         CFont newFont = new CFont((CFont)realFont, logicalFamilyName);
 363         registerGenericFont(newFont, true);
 364 
 365         return true;
 366     }
 367 
 368     @Override
 369     public String getFontPath(boolean noType1Fonts) {
 370         // In the case of the Cocoa toolkit, since we go through NSFont, we don't need to register /Library/Fonts
 371         Toolkit tk = Toolkit.getDefaultToolkit();
 372         if (tk instanceof HeadlessToolkit) {
 373             tk = ((HeadlessToolkit)tk).getUnderlyingToolkit();
 374         }
 375         if (tk instanceof LWCToolkit) {
 376             return "";
 377         }
 378 
 379         // X11 case
 380         return "/Library/Fonts";
 381     }
 382 
 383     @Override
 384     protected FontUIResource getFontConfigFUIR(
 385             String family, int style, int size)
 386     {
 387         String mappedName = FontUtilities.mapFcName(family);
 388         if (mappedName == null) {
 389             mappedName = "sansserif";
 390         }
 391         return new FontUIResource(mappedName, style, size);
 392     }
 393 
 394     // Only implemented on Windows
 395     @Override
 396     protected void populateFontFileNameMap(HashMap<String, String> fontToFileMap, HashMap<String, String> fontToFamilyNameMap,
 397             HashMap<String, ArrayList<String>> familyToFontListMap, Locale locale) {}
 398 }