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, boolean register) {


 100 
 101         if (!hasPermission()) {
 102             return createFont(DEFAULT_FULLNAME, size);


 103         }
 104 
 105         PGFont font = prismFontFactory.loadEmbeddedFont(name, fontStream,
 106                                                         size, register);
 107 
 108         if (font == null) return null;
 109         final FontResource fr = font.getFontResource();
 110         registerFont(font.getFontResource());
 111         return font;



 112     }
 113 
 114     /**
 115      * Printing uses the 2D pipeline which isn't initialised until
 116      * printing begins, so grabs a copy of the file holding an
 117      * embedded font to 2D on first use.
 118      */
 119     public static void registerFont(final FontResource fr) {
 120 
 121         AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
 122             InputStream stream = null;
 123             try {
 124                 File file = new File(fr.getFileName());
 125                 stream = new FileInputStream(file);
 126                 Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
 127                 fr.setPeer(font);
 128             } catch (Exception e) {
 129                 e.printStackTrace();
 130             } finally {
 131                 if (stream != null) {
 132                     try {
 133                         stream.close();
 134                     } catch (Exception e2) {
 135                     }
 136                 }
 137             }
 138             return null;
 139         });
 140     }
 141 
 142     public PGFont loadEmbeddedFont(String name, String path,
 143                                    float size, boolean register) {


 144 
 145         if (!hasPermission()) {
 146             return createFont(DEFAULT_FULLNAME, size);


 147         }
 148 
 149         PGFont font = prismFontFactory.loadEmbeddedFont(name, path,
 150                                                         size, register);
 151 
 152         if (font == null) return null;
 153         final FontResource fr = font.getFontResource();



 154         AccessController.doPrivileged(new PrivilegedAction<Object>() {
 155             public Object run() {
 156                 try {
 157                     File file = new File(fr.getFileName());
 158                     Font font = Font.createFont(Font.TRUETYPE_FONT, file);
 159                     fr.setPeer(font);
 160                 } catch (Exception e) {
 161                     e.printStackTrace();
 162                 }
 163                 return null;
 164             }
 165         });
 166         return font;
 167     }
 168 
 169     private static boolean compositeFontMethodsInitialized = false;
 170     private static Method getCompositeFontUIResource = null;
 171 
 172     /**
 173      * Returns a composite font from the font passed in the argument.
 174      * Note: This method relies on Java2D's sun.* APIs which are
 175      * subject to change.
 176      * ALso this may be just a stop gap measure.
 177      */
 178     static java.awt.Font getCompositeFont(final java.awt.Font srcFont) {
 179         if (PlatformUtil.isMac()) {
 180             return srcFont;
 181         }
 182         synchronized (J2DFontFactory.class) {
 183             if (!compositeFontMethodsInitialized) {
 184                 AccessController.doPrivileged(
 185                         (PrivilegedAction<Void>) () -> {
 186                             compositeFontMethodsInitialized = true;
 187                             Class<?> fontMgrCls;
 188                             try {
 189                                 // JDK7
 190                                 fontMgrCls = Class.forName(
 191                                         "sun.font.FontUtilities", true, null);
 192                             } catch (ClassNotFoundException cnfe) {
 193                                 try {
 194                                     // JDK5/6
 195                                     fontMgrCls = Class.forName(
 196                                        "sun.font.FontManager", true, null);
 197                                 } catch (ClassNotFoundException cnfe2) {
 198                                     return null;
 199                                 }
 200                             }
 201 
 202                             try {
 203                                 getCompositeFontUIResource =
 204                                     fontMgrCls.getMethod(
 205                                     "getCompositeFontUIResource",
 206                                     Font.class);
 207                             } catch (NoSuchMethodException nsme) {
 208                             }
 209                             return null;
 210                         }
 211                 );
 212             }
 213         }
 214 
 215         if (getCompositeFontUIResource != null) {
 216             try {
 217                 return
 218                     (java.awt.Font)getCompositeFontUIResource.
 219                     invoke(null, srcFont);
 220             } catch (IllegalAccessException iae) {
 221             } catch (InvocationTargetException ite) {}
 222         }
 223 
 224         return srcFont;
 225     }
 226 }
--- EOF ---