< prev index next >

modules/graphics/src/main/java/com/sun/prism/j2d/J2DFontFactory.java

Print this page




  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;




  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;


< prev index next >