source: trunk/src/org/expeditee/gio/javafx/JavaFXFontManager.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 6.1 KB
Line 
1package org.expeditee.gio.javafx;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.List;
9
10import org.expeditee.core.Font;
11import org.expeditee.gio.FontManager;
12
13import javafx.scene.text.FontPosture;
14import javafx.scene.text.FontWeight;
15
16public class JavaFXFontManager extends FontManager {
17
18 /** Singleton instance. */
19 private static JavaFXFontManager _instance;
20
21 /** Singleton instantiator. */
22 public static JavaFXFontManager getInstance()
23 {
24 if (_instance == null) _instance = new JavaFXFontManager();
25
26 return _instance;
27 }
28
29 /** Mapping from handles to internal fonts. */
30 private HashMap<Font, JavaFXFont> _fontMap;
31 /** The font family names registered with the manager. */
32 private HashSet<String> _fontNames;
33
34 /** Private constructor for singleton instance. */
35 private JavaFXFontManager()
36 {
37 // Initialise the font map
38 _fontMap = new HashMap<Font, JavaFXFont>();
39
40 // Initialise the set of family names
41 _fontNames = new HashSet<String>();
42
43 // Initialise default font to JavaFX default
44 setDefaultFont(register(JavaFXFont.getDefault()));
45
46 // Add JavaFX standard fonts to registered fonts
47 registerJavaFXStandardFonts();
48 }
49
50 @Override
51 public Font registerFontFile(File fontFile)
52 {
53 if (fontFile == null) return null;
54
55 JavaFXFont jfxFont = null;
56 boolean registerOkay = true;
57
58 // Create the internal font from the given file
59 try {
60 jfxFont = JavaFXFont.loadFont(new FileInputStream(fontFile), 0); // Size of 0 forces default size
61 if (jfxFont == null) registerOkay = false;
62 } catch (Exception e) {
63 registerOkay = false;
64 }
65
66 // Register the new font with ourselves
67 if (registerOkay) {
68 return register(jfxFont).clone();
69 }
70
71 // If we made it here the registration failed
72 return null;
73 }
74
75 @Override
76 public String[] getRegisteredFontFamilies() {
77
78 return _fontNames.toArray(new String[_fontNames.size()]);
79 }
80
81 @Override
82 public boolean isFontFamilyRegistered(String fontFamily)
83 {
84 return _fontNames.contains(fontFamily);
85 }
86
87 @Override
88 public Font getActualFont(Font font)
89 {
90 return extractFontForInternalFont(register(font));
91 }
92
93 /** Registers the given handle with the manager and returns the internal font. */
94 private JavaFXFont register(Font font)
95 {
96 if (register(null, font) == null) return null;
97
98 return _fontMap.get(font);
99 }
100
101 /** Registers the given font with the manager and returns the generated handle. */
102 private Font register(JavaFXFont jfxFont)
103 {
104 return register(jfxFont, null);
105 }
106
107 /** Registers a handle/font pair. */
108 private Font register(JavaFXFont jfxFont, Font font)
109 {
110 if (jfxFont == null && font == null) return null;
111
112 if (_fontMap.containsKey(font)) return font;
113
114 if (font == null) {
115 font = extractFontForInternalFont(jfxFont);
116 } else if (jfxFont == null) {
117 jfxFont = JavaFXFont.font(font.getFamilyName(), getJavaFXFontWeight(font), getJavaFXFontPosture(font), font.getSize());
118 }
119
120 if (jfxFont == null || font == null) return null;
121
122 _fontMap.put(font, jfxFont);
123 _fontNames.add(jfxFont.getFamily());
124
125 return font;
126 }
127
128 /** Creates a handle to match the given JavaFX font. */
129 private Font extractFontForInternalFont(JavaFXFont jfxFont)
130 {
131 if (jfxFont == null) return null;
132
133 Font font = new Font(jfxFont.getFamily());
134 font.setStyle(Font.Style.PLAIN);
135 if (jfxFont.isBold()) font.toggleBold();
136 if (jfxFont.isItalics()) font.toggleItalic();
137 font.setSize((int) jfxFont.getSize());
138 font.setSpacing(0); // Can't get tracking value for JavaFXFonts
139
140 return font;
141 }
142
143 /** Gets the internal JavaFX font object for use by other Swing managers. */
144 public javafx.scene.text.Font getInternalFont(Font font)
145 {
146 return register(font).getRealFont();
147 }
148
149 /** Registers any standard fonts available on the system with the manager. */
150 private void registerJavaFXStandardFonts()
151 {
152 List<String> standardFontNames = javafx.scene.text.Font.getFamilies();
153
154 for (String name : standardFontNames) {
155 register(new Font(name));
156 }
157 }
158
159 /** Gets the JavaFX font weight for a given font. */
160 private javafx.scene.text.FontWeight getJavaFXFontWeight(Font font)
161 {
162 if (font.isBold()) {
163 return javafx.scene.text.FontWeight.BOLD;
164 } else {
165 return javafx.scene.text.FontWeight.NORMAL;
166 }
167 }
168
169 /** Gets the JavaFX font posture for a given font. */
170 private javafx.scene.text.FontPosture getJavaFXFontPosture(Font font)
171 {
172 if (font.isItalic()) {
173 return javafx.scene.text.FontPosture.ITALIC;
174 } else {
175 return javafx.scene.text.FontPosture.REGULAR;
176 }
177 }
178
179 /** Wrapper for JavaFX fonts as they do not retain knowledge of their bold/italics status. */
180 private static class JavaFXFont {
181
182 private javafx.scene.text.Font _font = null;
183
184 private boolean _isBold;
185
186 private boolean _isItalics;
187
188 /** Default constructor for internal use only. */
189 private JavaFXFont() {}
190
191 public javafx.scene.text.Font getRealFont()
192 {
193 return _font;
194 }
195
196 public String getFamily()
197 {
198 return _font.getFamily();
199 }
200
201 public boolean isBold()
202 {
203 return _isBold;
204 }
205
206 public boolean isItalics()
207 {
208 return _isItalics;
209 }
210
211 public double getSize()
212 {
213 return _font.getSize();
214 }
215
216 public static JavaFXFont getDefault()
217 {
218 JavaFXFont font = new JavaFXFont();
219 font._font = javafx.scene.text.Font.getDefault();
220 if (font._font == null) return null;
221 font._isBold = false;
222 font._isItalics = false;
223 return font;
224 }
225
226 public static JavaFXFont loadFont(InputStream in, double size)
227 {
228 JavaFXFont font = new JavaFXFont();
229 font._font = javafx.scene.text.Font.loadFont(in, size);
230 if (font._font == null) return null;
231 font._isBold = false;
232 font._isItalics = false;
233 return font;
234 }
235
236 public static JavaFXFont font(String family, FontWeight weight, FontPosture posture, double size)
237 {
238 JavaFXFont font = new JavaFXFont();
239 font._font = javafx.scene.text.Font.font(family, weight, posture, size);
240 if (font._font == null) return null;
241 font._isBold = (weight == FontWeight.BOLD);
242 font._isItalics = (posture == FontPosture.ITALIC);
243 return font;
244 }
245 }
246}
Note: See TracBrowser for help on using the repository browser.