< prev index next >

modules/graphics/src/main/java/javafx/scene/text/Font.java

Print this page

        

@@ -397,10 +397,63 @@
      * @param urlStr from which to load the font, specified as a String.
      * @param size of the returned font.
      * @return the Font, or null if the font cannot be created.
      */
     public static Font loadFont(String urlStr, double size) {
+        Font[] fonts = loadFontInternal(urlStr, size, false);
+        return (fonts == null) ? null : fonts[0];
+    }
+
+    /**
+     * Loads font resources from the specified URL. If the load is successful
+     * such that the location is readable, and it represents a supported
+     * font format then an array of<code>Font</code> object will be returned.
+     * <p>
+     * The use case for this method is for loading all fonts
+     * from a TrueType Collection (TTC).
+     * <p>
+     * If a security manager is present, the application
+     * must have both permission to read from the specified URL location
+     * and the {@link javafx.util.FXPermission} "loadFont".
+     * If the application does not have permission to read from the specified
+     * URL location, then null is returned.
+     * If the application does not have the "loadFont" permission then this method
+     * will return an array of one element which is the default
+     *  system font with the specified font size.
+     * <p>
+     * Any failure such as a malformed URL being unable to locate or read
+     * from the resource, or if it doesn't represent a font, will result in
+     * a <code>null</code> return. It is the application's responsibility
+     * to check this before use.
+     * <p>
+     * On a successful (non-null) return the fonts will be registered
+     * with the FX graphics system for creation by available constructors
+     * and factory methods, and the application should use it in this
+     * manner rather than calling this method again, which would
+     * repeat the overhead of downloading and installing the fonts.
+     * <p>
+     * The font <code>size</code> parameter is a convenience so that in
+     * typical usage the application can directly use the returned (non-null)
+     * font rather than needing to create one via a constructor. Invalid sizes
+     * are those <=0 and will result in a default size.
+     * <p>
+     * If the URL represents a local disk file, then no copying is performed
+     * and the font file is required to persist for the lifetime of the
+     * application. Updating the file in any manner will result
+     * in unspecified and likely undesired behaviours.
+     *
+     * @param urlStr from which to load the fonts, specified as a String.
+     * @param size of the returned fonts.
+     * @return array of Font, or null if the fonts cannot be created.
+     * @since 9
+     */
+    public static Font[] loadFonts(String urlStr, double size) {
+        return loadFontInternal(urlStr, size, true);
+    }
+
+    private static Font[] loadFontInternal(String urlStr, double size,
+                                           boolean loadAll) {
         URL url = null;
         try {
             url = new URL(urlStr); // null string arg. is caught here.
         } catch (Exception e) {
             return null;

@@ -424,38 +477,84 @@
                     sm.checkPermission(filePermission);
                 }
             } catch (Exception e) {
                 return null;
             }
-            return Toolkit.getToolkit().getFontLoader().loadFont(path, size);
+            return
+            Toolkit.getToolkit().getFontLoader().loadFont(path, size, loadAll);
         }
-        Font font = null;
+        Font[] fonts = null;
         URLConnection connection = null;
         InputStream in = null;
         try {
             connection = url.openConnection();
             in = connection.getInputStream();
-            font = Toolkit.getToolkit().getFontLoader().loadFont(in, size);
+            fonts =
+               Toolkit.getToolkit().getFontLoader().loadFont(in, size, loadAll);
         } catch (Exception e) {
             return null;
         } finally {
             try {
                 if (in != null) {
                     in.close();
                 }
             } catch (Exception e) {
             }
         }
-        return font;
+        return fonts;
     }
 
     /**
      * Loads a font resource from the specified input stream.
      * If the load is successful such that the stream can be
      * fully read, and it represents a supported font format then a
      * <code>Font</code> object will be returned.
      * <p>
+     * The use case for this method is for loading all fonts
+     * from a TrueType Collection (TTC).
+     * <p>
+     * If a security manager is present, the application
+     * must have the {@link javafx.util.FXPermission} "loadFont".
+     * If the application does not have permission then this method
+     * will return the default system font with the specified font size.
+     * <p>
+     * Any failure such as abbreviated input, or an unsupported font format
+     * will result in a <code>null</code> return. It is the application's
+     * responsibility to check this before use.
+     * <p>
+     * On a successful (non-null) return the fonts will be registered
+     * with the FX graphics system for creation by available constructors
+     * and factory methods, and the application should use it in this
+     * manner rather than calling this method again, which would
+     * repeat the overhead of re-reading and installing the font.
+     * <p>
+     * The font <code>size</code> parameter is a convenience so that in
+     * typical usage the application can directly use the returned (non-null)
+     * fonts rather than needing to create one via a constructor. Invalid sizes
+     * are those <=0 and will result in a default size.
+     * <p>
+     * This method does not close the input stream.
+     * @param in stream from which to load the font.
+     * @param size of the returned font.
+     * @return array of Font, or null if the fonts cannot be created.
+     * @since 9
+     */
+    public static Font loadFont(InputStream in, double size) {
+        if (size <= 0) {
+            size = getDefaultSystemFontSize();
+        }
+        Font[] fonts =
+           Toolkit.getToolkit().getFontLoader().loadFont(in, size, false);
+        return (fonts == null) ? null : fonts[0];
+    }
+
+    /**
+     * Loads font resources from the specified input stream.
+     * If the load is successful such that the stream can be
+     * fully read, and it represents a supported font format then a
+     * an array of <code>Font</code> object will be returned.
+     * <p>
      * If a security manager is present, the application
      * must have the {@link javafx.util.FXPermission} "loadFont".
      * If the application does not have permission then this method
      * will return the default system font with the specified font size.
      * <p>

@@ -477,15 +576,17 @@
      * This method does not close the input stream.
      * @param in stream from which to load the font.
      * @param size of the returned font.
      * @return the Font, or null if the font cannot be created.
      */
-    public static Font loadFont(InputStream in, double size) {
+    public static Font[] loadFonts(InputStream in, double size) {
         if (size <= 0) {
             size = getDefaultSystemFontSize();
         }
-        return Toolkit.getToolkit().getFontLoader().loadFont(in, size);
+        Font[] fonts =
+            Toolkit.getToolkit().getFontLoader().loadFont(in, size, true);
+        return (fonts == null) ? null : fonts;
     }
 
     /**
      * Converts this {@code Font} object to a {@code String} representation.
      * The String representation is for informational use only and will change.
< prev index next >