< prev index next >

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

Print this page




 382      * with the FX graphics system for creation by available constructors
 383      * and factory methods, and the application should use it in this
 384      * manner rather than calling this method again, which would
 385      * repeat the overhead of downloading and installing the font.
 386      * <p>
 387      * The font <code>size</code> parameter is a convenience so that in
 388      * typical usage the application can directly use the returned (non-null)
 389      * font rather than needing to create one via a constructor. Invalid sizes
 390      * are those <=0 and will result in a default size.
 391      * <p>
 392      * If the URL represents a local disk file, then no copying is performed
 393      * and the font file is required to persist for the lifetime of the
 394      * application. Updating the file in any manner will result
 395      * in unspecified and likely undesired behaviours.
 396      *
 397      * @param urlStr from which to load the font, specified as a String.
 398      * @param size of the returned font.
 399      * @return the Font, or null if the font cannot be created.
 400      */
 401     public static Font loadFont(String urlStr, double size) {





















































 402         URL url = null;
 403         try {
 404             url = new URL(urlStr); // null string arg. is caught here.
 405         } catch (Exception e) {
 406             return null;
 407         }
 408         if (size <= 0) {
 409             size = getDefaultSystemFontSize();
 410         }
 411         // Now lets parse the URL and decide if its a file,
 412         // or a remote URL from which we need to read.
 413         if (url.getProtocol().equals("file")) {
 414             String path = url.getFile();
 415             // The URL path may have a leading "/", when obtained
 416             // via ClassLoader. This can cause problems on Windows.
 417             // Getting the path from a File fixes this.
 418             path = new java.io.File(path).getPath();
 419             try {
 420                 SecurityManager sm = System.getSecurityManager();
 421                 if (sm != null) {
 422                     FilePermission filePermission =
 423                         new FilePermission(path, "read");
 424                     sm.checkPermission(filePermission);
 425                 }
 426             } catch (Exception e) {
 427                 return null;
 428             }
 429             return Toolkit.getToolkit().getFontLoader().loadFont(path, size);

 430         }
 431         Font font = null;
 432         URLConnection connection = null;
 433         InputStream in = null;
 434         try {
 435             connection = url.openConnection();
 436             in = connection.getInputStream();
 437             font = Toolkit.getToolkit().getFontLoader().loadFont(in, size);

 438         } catch (Exception e) {
 439             return null;
 440         } finally {
 441             try {
 442                 if (in != null) {
 443                     in.close();
 444                 }
 445             } catch (Exception e) {
 446             }
 447         }
 448         return font;
 449     }
 450 
 451     /**
 452      * Loads a font resource from the specified input stream.
 453      * If the load is successful such that the stream can be
 454      * fully read, and it represents a supported font format then a
 455      * <code>Font</code> object will be returned.
 456      * <p>












































 457      * If a security manager is present, the application
 458      * must have the {@link javafx.util.FXPermission} "loadFont".
 459      * If the application does not have permission then this method
 460      * will return the default system font with the specified font size.
 461      * <p>
 462      * Any failure such as abbreviated input, or an unsupported font format
 463      * will result in a <code>null</code> return. It is the application's
 464      * responsibility to check this before use.
 465      * <p>
 466      * On a successful (non-null) return the font will be registered
 467      * with the FX graphics system for creation by available constructors
 468      * and factory methods, and the application should use it in this
 469      * manner rather than calling this method again, which would
 470      * repeat the overhead of re-reading and installing the font.
 471      * <p>
 472      * The font <code>size</code> parameter is a convenience so that in
 473      * typical usage the application can directly use the returned (non-null)
 474      * font rather than needing to create one via a constructor. Invalid sizes
 475      * are those <=0 and will result in a default size.
 476      * <p>
 477      * This method does not close the input stream.
 478      * @param in stream from which to load the font.
 479      * @param size of the returned font.
 480      * @return the Font, or null if the font cannot be created.
 481      */
 482     public static Font loadFont(InputStream in, double size) {
 483         if (size <= 0) {
 484             size = getDefaultSystemFontSize();
 485         }
 486         return Toolkit.getToolkit().getFontLoader().loadFont(in, size);


 487     }
 488 
 489     /**
 490      * Converts this {@code Font} object to a {@code String} representation.
 491      * The String representation is for informational use only and will change.
 492      * Do not use this string representation for any programmatic purpose.
 493      */
 494     @Override public String toString() {
 495         StringBuilder builder = new StringBuilder("Font[name=");
 496         builder = builder.append(name);
 497         builder = builder.append(", family=").append(family);
 498         builder = builder.append(", style=").append(style);
 499         builder = builder.append(", size=").append(size);
 500         builder = builder.append("]");
 501         return builder.toString();
 502     }
 503 
 504     /**
 505      * Indicates whether some other object is "equal to" this one.
 506      * @param obj the reference object with which to compare.




 382      * with the FX graphics system for creation by available constructors
 383      * and factory methods, and the application should use it in this
 384      * manner rather than calling this method again, which would
 385      * repeat the overhead of downloading and installing the font.
 386      * <p>
 387      * The font <code>size</code> parameter is a convenience so that in
 388      * typical usage the application can directly use the returned (non-null)
 389      * font rather than needing to create one via a constructor. Invalid sizes
 390      * are those <=0 and will result in a default size.
 391      * <p>
 392      * If the URL represents a local disk file, then no copying is performed
 393      * and the font file is required to persist for the lifetime of the
 394      * application. Updating the file in any manner will result
 395      * in unspecified and likely undesired behaviours.
 396      *
 397      * @param urlStr from which to load the font, specified as a String.
 398      * @param size of the returned font.
 399      * @return the Font, or null if the font cannot be created.
 400      */
 401     public static Font loadFont(String urlStr, double size) {
 402         Font[] fonts = loadFontInternal(urlStr, size, false);
 403         return (fonts == null) ? null : fonts[0];
 404     }
 405 
 406     /**
 407      * Loads font resources from the specified URL. If the load is successful
 408      * such that the location is readable, and it represents a supported
 409      * font format then an array of<code>Font</code> object will be returned.
 410      * <p>
 411      * The use case for this method is for loading all fonts
 412      * from a TrueType Collection (TTC).
 413      * <p>
 414      * If a security manager is present, the application
 415      * must have both permission to read from the specified URL location
 416      * and the {@link javafx.util.FXPermission} "loadFont".
 417      * If the application does not have permission to read from the specified
 418      * URL location, then null is returned.
 419      * If the application does not have the "loadFont" permission then this method
 420      * will return an array of one element which is the default
 421      *  system font with the specified font size.
 422      * <p>
 423      * Any failure such as a malformed URL being unable to locate or read
 424      * from the resource, or if it doesn't represent a font, will result in
 425      * a <code>null</code> return. It is the application's responsibility
 426      * to check this before use.
 427      * <p>
 428      * On a successful (non-null) return the fonts will be registered
 429      * with the FX graphics system for creation by available constructors
 430      * and factory methods, and the application should use it in this
 431      * manner rather than calling this method again, which would
 432      * repeat the overhead of downloading and installing the fonts.
 433      * <p>
 434      * The font <code>size</code> parameter is a convenience so that in
 435      * typical usage the application can directly use the returned (non-null)
 436      * font rather than needing to create one via a constructor. Invalid sizes
 437      * are those <=0 and will result in a default size.
 438      * <p>
 439      * If the URL represents a local disk file, then no copying is performed
 440      * and the font file is required to persist for the lifetime of the
 441      * application. Updating the file in any manner will result
 442      * in unspecified and likely undesired behaviours.
 443      *
 444      * @param urlStr from which to load the fonts, specified as a String.
 445      * @param size of the returned fonts.
 446      * @return array of Font, or null if the fonts cannot be created.
 447      * @since 9
 448      */
 449     public static Font[] loadFonts(String urlStr, double size) {
 450         return loadFontInternal(urlStr, size, true);
 451     }
 452 
 453     private static Font[] loadFontInternal(String urlStr, double size,
 454                                            boolean loadAll) {
 455         URL url = null;
 456         try {
 457             url = new URL(urlStr); // null string arg. is caught here.
 458         } catch (Exception e) {
 459             return null;
 460         }
 461         if (size <= 0) {
 462             size = getDefaultSystemFontSize();
 463         }
 464         // Now lets parse the URL and decide if its a file,
 465         // or a remote URL from which we need to read.
 466         if (url.getProtocol().equals("file")) {
 467             String path = url.getFile();
 468             // The URL path may have a leading "/", when obtained
 469             // via ClassLoader. This can cause problems on Windows.
 470             // Getting the path from a File fixes this.
 471             path = new java.io.File(path).getPath();
 472             try {
 473                 SecurityManager sm = System.getSecurityManager();
 474                 if (sm != null) {
 475                     FilePermission filePermission =
 476                         new FilePermission(path, "read");
 477                     sm.checkPermission(filePermission);
 478                 }
 479             } catch (Exception e) {
 480                 return null;
 481             }
 482             return
 483             Toolkit.getToolkit().getFontLoader().loadFont(path, size, loadAll);
 484         }
 485         Font[] fonts = null;
 486         URLConnection connection = null;
 487         InputStream in = null;
 488         try {
 489             connection = url.openConnection();
 490             in = connection.getInputStream();
 491             fonts =
 492                Toolkit.getToolkit().getFontLoader().loadFont(in, size, loadAll);
 493         } catch (Exception e) {
 494             return null;
 495         } finally {
 496             try {
 497                 if (in != null) {
 498                     in.close();
 499                 }
 500             } catch (Exception e) {
 501             }
 502         }
 503         return fonts;
 504     }
 505 
 506     /**
 507      * Loads a font resource from the specified input stream.
 508      * If the load is successful such that the stream can be
 509      * fully read, and it represents a supported font format then a
 510      * <code>Font</code> object will be returned.
 511      * <p>
 512      * The use case for this method is for loading all fonts
 513      * from a TrueType Collection (TTC).
 514      * <p>
 515      * If a security manager is present, the application
 516      * must have the {@link javafx.util.FXPermission} "loadFont".
 517      * If the application does not have permission then this method
 518      * will return the default system font with the specified font size.
 519      * <p>
 520      * Any failure such as abbreviated input, or an unsupported font format
 521      * will result in a <code>null</code> return. It is the application's
 522      * responsibility to check this before use.
 523      * <p>
 524      * On a successful (non-null) return the fonts will be registered
 525      * with the FX graphics system for creation by available constructors
 526      * and factory methods, and the application should use it in this
 527      * manner rather than calling this method again, which would
 528      * repeat the overhead of re-reading and installing the font.
 529      * <p>
 530      * The font <code>size</code> parameter is a convenience so that in
 531      * typical usage the application can directly use the returned (non-null)
 532      * fonts rather than needing to create one via a constructor. Invalid sizes
 533      * are those <=0 and will result in a default size.
 534      * <p>
 535      * This method does not close the input stream.
 536      * @param in stream from which to load the font.
 537      * @param size of the returned font.
 538      * @return array of Font, or null if the fonts cannot be created.
 539      * @since 9
 540      */
 541     public static Font loadFont(InputStream in, double size) {
 542         if (size <= 0) {
 543             size = getDefaultSystemFontSize();
 544         }
 545         Font[] fonts =
 546            Toolkit.getToolkit().getFontLoader().loadFont(in, size, false);
 547         return (fonts == null) ? null : fonts[0];
 548     }
 549 
 550     /**
 551      * Loads font resources from the specified input stream.
 552      * If the load is successful such that the stream can be
 553      * fully read, and it represents a supported font format then a
 554      * an array of <code>Font</code> object will be returned.
 555      * <p>
 556      * If a security manager is present, the application
 557      * must have the {@link javafx.util.FXPermission} "loadFont".
 558      * If the application does not have permission then this method
 559      * will return the default system font with the specified font size.
 560      * <p>
 561      * Any failure such as abbreviated input, or an unsupported font format
 562      * will result in a <code>null</code> return. It is the application's
 563      * responsibility to check this before use.
 564      * <p>
 565      * On a successful (non-null) return the font will be registered
 566      * with the FX graphics system for creation by available constructors
 567      * and factory methods, and the application should use it in this
 568      * manner rather than calling this method again, which would
 569      * repeat the overhead of re-reading and installing the font.
 570      * <p>
 571      * The font <code>size</code> parameter is a convenience so that in
 572      * typical usage the application can directly use the returned (non-null)
 573      * font rather than needing to create one via a constructor. Invalid sizes
 574      * are those <=0 and will result in a default size.
 575      * <p>
 576      * This method does not close the input stream.
 577      * @param in stream from which to load the font.
 578      * @param size of the returned font.
 579      * @return the Font, or null if the font cannot be created.
 580      */
 581     public static Font[] loadFonts(InputStream in, double size) {
 582         if (size <= 0) {
 583             size = getDefaultSystemFontSize();
 584         }
 585         Font[] fonts =
 586             Toolkit.getToolkit().getFontLoader().loadFont(in, size, true);
 587         return (fonts == null) ? null : fonts;
 588     }
 589 
 590     /**
 591      * Converts this {@code Font} object to a {@code String} representation.
 592      * The String representation is for informational use only and will change.
 593      * Do not use this string representation for any programmatic purpose.
 594      */
 595     @Override public String toString() {
 596         StringBuilder builder = new StringBuilder("Font[name=");
 597         builder = builder.append(name);
 598         builder = builder.append(", family=").append(family);
 599         builder = builder.append(", style=").append(style);
 600         builder = builder.append(", size=").append(size);
 601         builder = builder.append("]");
 602         return builder.toString();
 603     }
 604 
 605     /**
 606      * Indicates whether some other object is "equal to" this one.
 607      * @param obj the reference object with which to compare.


< prev index next >