1 /*
   2  * Copyright (c) 2016, 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 import javafx.application.*;
  27 import javafx.stage.*;
  28 import javafx.scene.*;
  29 import javafx.scene.layout.*;
  30 import javafx.scene.paint.*;
  31 import javafx.scene.text.*;
  32 
  33 import java.io.*;
  34 
  35 public class LoadFonts extends Application {
  36 
  37     static String filename = null;
  38     public static void main(String[] args) {
  39        if (args.length > 0) {
  40            filename = args[0];
  41        } else {
  42           System.err.println("Needs a font file.");
  43           System.err.println("usage : java LoadFonts FOO.ttc");
  44        }
  45        launch(args);
  46     }
  47 
  48     public void start(Stage stage) {
  49         stage.setWidth(600);
  50         stage.setHeight(600);
  51         Group g = new Group();
  52         final Scene scene = new Scene(new Group());
  53         scene.setFill(Color.WHITE);
  54         VBox box = new VBox(10);
  55         ((Group)scene.getRoot()).getChildren().add(box);
  56         stage.setScene(scene);
  57 
  58         String url = "file:" + filename;
  59 
  60         // Load a single font from the TTC file
  61         Font font = Font.loadFont(url, 24.0);
  62         System.out.println(font);
  63         if (font != null) {
  64             addText(box, font);
  65         }
  66 
  67         // Load all fonts from the TTC file
  68         Font[] fonts = Font.loadFonts(url, 24.0);
  69         if (fonts != null) {
  70             for (int i=0; i<fonts.length; i++) {
  71                 System.out.println(fonts[i]);
  72                 addText(box, fonts[i]);
  73             }
  74        }
  75 
  76         // Load a single font from a stream open on the TTC file.
  77         Font sfont = null;
  78         FileInputStream fis = null;
  79         try {
  80             fis = new FileInputStream(filename);
  81             sfont = Font.loadFont(fis, 24.0);
  82         } catch (IOException e) {
  83           e.printStackTrace();
  84         } finally {
  85            if (fis != null) try {
  86                fis.close();
  87             } catch (IOException e) {
  88             }
  89         }
  90         System.out.println(sfont);
  91         if (font != null) {
  92             addText(box, sfont);
  93          }
  94 
  95         // Load all fonts from a stream open on the TTC file.
  96         Font[] sfonts = null;
  97         fis = null;
  98         try {
  99             fis = new FileInputStream(filename);
 100             sfonts = Font.loadFonts(fis, 24.0);
 101         } catch (IOException e) {
 102           e.printStackTrace();
 103         } finally {
 104            if (fis != null) try {
 105                fis.close();
 106             } catch (IOException e) {
 107             }
 108         }
 109         System.out.println("Loaded from stream " + sfonts);
 110         if (sfonts != null) {
 111             for (int i=0; i<sfonts.length; i++) {
 112                 System.out.println("Stream " + sfonts[i]);
 113                 addText(box, sfonts[i]);
 114             }
 115        }
 116 
 117        stage.show();
 118     }
 119 
 120     private void addText(VBox box, Font f) {
 121         String str = "abcdefghihjklmnopqrstuvwxyz " + f.getName();
 122         Text txt1 = new Text(str);
 123         txt1.setFont(f);
 124         txt1.setFill(Color.BLACK);
 125         txt1.setFontSmoothingType(FontSmoothingType.GRAY);
 126         box.getChildren().add(txt1);
 127     }
 128 }
 129