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 com.sun.prism.j2d;
  27 
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.InvocationTargetException;
  30 
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.awt.Font;
  34 import java.io.File;
  35 import java.io.FileInputStream;
  36 import java.io.InputStream;
  37 
  38 import com.sun.javafx.font.FontResource;
  39 import com.sun.javafx.font.PGFont;
  40 import com.sun.javafx.font.FontFactory;
  41 import com.sun.javafx.PlatformUtil;
  42 
  43 /**
  44  * This is mostly just delegating, except for allowing for the
  45  * registration of embedded fonts with Java 2D.
  46  * Its possible it could also be check the names of all fonts that
  47  * are being used are known to 2D but I'm not sure I need that.
  48  */
  49 
  50 final class J2DFontFactory implements FontFactory {
  51 
  52     FontFactory prismFontFactory;
  53 
  54     J2DFontFactory(FontFactory fontFactory) {
  55         prismFontFactory = fontFactory;
  56     }
  57 
  58     public PGFont createFont(String name, float size) {
  59         return prismFontFactory.createFont(name, size);
  60     }
  61 
  62     public PGFont createFont(String family,
  63                              boolean bold, boolean italic, float size) {
  64         return prismFontFactory.createFont(family, bold, italic, size);
  65     }
  66 
  67     public synchronized PGFont deriveFont(PGFont font, boolean bold,
  68                                           boolean italic, float size) {
  69         return prismFontFactory.deriveFont(font, bold, italic, size);
  70     }
  71 
  72     public String[] getFontFamilyNames() {
  73         return prismFontFactory.getFontFamilyNames();
  74     }
  75 
  76     public String[] getFontFullNames() {
  77         return prismFontFactory.getFontFullNames();
  78     }
  79 
  80     public String[] getFontFullNames(String family) {
  81         return prismFontFactory.getFontFullNames(family);
  82     }
  83 
  84     public boolean isPlatformFont(String name) {
  85         return prismFontFactory.isPlatformFont(name);
  86     }
  87 
  88     public final boolean hasPermission() {
  89         return prismFontFactory.hasPermission();
  90     }
  91 
  92     /* This is an important but tricky one. We need to copy the
  93      * stream. I don't want to have to manage the temp file deletion here,
  94      * so although its non-optimal I will create a temp file, provide
  95      * input streams on it to both prism and 2D, then when they are done,
  96      * remove it.
  97      */
  98     public PGFont[] loadEmbeddedFont(String name, InputStream fontStream,
  99                                      float size,
 100                                      boolean register,
 101                                      boolean loadAll) {
 102 
 103         if (!hasPermission()) {
 104             PGFont[] fonts = new PGFont[1];
 105             fonts[0] = createFont(DEFAULT_FULLNAME, size);
 106             return fonts;
 107         }
 108 
 109         PGFont[] fonts =
 110           prismFontFactory.loadEmbeddedFont(name, fontStream,
 111                                             size, register, loadAll);
 112 
 113         if (fonts == null || fonts.length == 0) return null;
 114         final FontResource fr = fonts[0].getFontResource();
 115         // REMIND: this needs to be upgraded to use JDK9 createFont
 116         // which can handle a collection.
 117         registerFont(fonts[0].getFontResource());
 118         return fonts;
 119     }
 120 
 121     /**
 122      * Printing uses the 2D pipeline which isn't initialised until
 123      * printing begins, so grabs a copy of the file holding an
 124      * embedded font to 2D on first use.
 125      */
 126     public static void registerFont(final FontResource fr) {
 127 
 128         AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
 129             InputStream stream = null;
 130             try {
 131                 File file = new File(fr.getFileName());
 132                 stream = new FileInputStream(file);
 133                 Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
 134                 fr.setPeer(font);
 135             } catch (Exception e) {
 136                 e.printStackTrace();
 137             } finally {
 138                 if (stream != null) {
 139                     try {
 140                         stream.close();
 141                     } catch (Exception e2) {
 142                     }
 143                 }
 144             }
 145             return null;
 146         });
 147     }
 148 
 149     public PGFont[] loadEmbeddedFont(String name, String path,
 150                                      float size,
 151                                      boolean register,
 152                                      boolean loadAll) {
 153 
 154         if (!hasPermission()) {
 155             PGFont[] fonts = new PGFont[1];
 156             fonts[0] = createFont(DEFAULT_FULLNAME, size);
 157             return fonts;
 158         }
 159 
 160         PGFont[] fonts =
 161             prismFontFactory.loadEmbeddedFont(name, path,
 162                                               size, register, loadAll);
 163 
 164         if (fonts == null || fonts.length == 0) return null;
 165         // REMIND: this needs to be upgraded to use JDK9 createFont
 166         // which can handle a collection.
 167         final FontResource fr = fonts[0].getFontResource();
 168         AccessController.doPrivileged(new PrivilegedAction<Object>() {
 169             public Object run() {
 170                 try {
 171                     File file = new File(fr.getFileName());
 172                     Font font = Font.createFont(Font.TRUETYPE_FONT, file);
 173                     fr.setPeer(font);
 174                 } catch (Exception e) {
 175                     e.printStackTrace();
 176                 }
 177                 return null;
 178             }
 179         });
 180         return fonts;
 181     }
 182 
 183     private static boolean compositeFontMethodsInitialized = false;
 184     private static Method getCompositeFontUIResource = null;
 185 
 186     /**
 187      * Returns a composite font from the font passed in the argument.
 188      * Note: This method relies on Java2D's sun.* APIs which are
 189      * subject to change.
 190      * ALso this may be just a stop gap measure.
 191      */
 192     static java.awt.Font getCompositeFont(final java.awt.Font srcFont) {
 193         if (PlatformUtil.isMac()) {
 194             return srcFont;
 195         }
 196         synchronized (J2DFontFactory.class) {
 197             if (!compositeFontMethodsInitialized) {
 198                 AccessController.doPrivileged(
 199                         (PrivilegedAction<Void>) () -> {
 200                             compositeFontMethodsInitialized = true;
 201                             Class<?> fontMgrCls;
 202                             try {
 203                                 // JDK7
 204                                 fontMgrCls = Class.forName(
 205                                         "sun.font.FontUtilities", true, null);
 206                             } catch (ClassNotFoundException cnfe) {
 207                                 try {
 208                                     // JDK5/6
 209                                     fontMgrCls = Class.forName(
 210                                        "sun.font.FontManager", true, null);
 211                                 } catch (ClassNotFoundException cnfe2) {
 212                                     return null;
 213                                 }
 214                             }
 215 
 216                             try {
 217                                 getCompositeFontUIResource =
 218                                     fontMgrCls.getMethod(
 219                                     "getCompositeFontUIResource",
 220                                     Font.class);
 221                             } catch (NoSuchMethodException nsme) {
 222                             }
 223                             return null;
 224                         }
 225                 );
 226             }
 227         }
 228 
 229         if (getCompositeFontUIResource != null) {
 230             try {
 231                 return
 232                     (java.awt.Font)getCompositeFontUIResource.
 233                     invoke(null, srcFont);
 234             } catch (IllegalAccessException iae) {
 235             } catch (InvocationTargetException ite) {}
 236         }
 237 
 238         return srcFont;
 239     }
 240 }
--- EOF ---