source: trunk/src/org/expeditee/gio/swing/SwingFontManager.java@ 1125

Last change on this file since 1125 was 1125, checked in by bln4, 6 years ago

org.apollo.items.EmulatedTextItem ->
org.expeditee.agents.GraphFramesetLinks ->
org.expeditee.gio.FontManager ->
org.expeditee.gio.javafx.JavaFXFontManager ->
org.expeditee.gio.swing.SwingFontManager ->
org.expeditee.gui.MessageBay ->
org.expeditee.io.WebParser ->
org.expeditee.items.Text ->

The above files are been altered so that there is no longer any chance Font references will be shared amongst Text Items. How default Fonts are used has been altered so that a Text Item using the default font remember that this is the case. This is a follow up to an initial fix for the same problem that turned out to be insufficient.

File size: 4.7 KB
Line 
1package org.expeditee.gio.swing;
2
3import java.awt.GraphicsEnvironment;
4import java.io.File;
5import java.util.HashMap;
6import java.util.HashSet;
7
8import org.expeditee.core.Font;
9import org.expeditee.gio.FontManager;
10
11/**
12 * The font manager for a Swing system.
13 *
14 * @author cts16
15 */
16public class SwingFontManager extends FontManager {
17
18 private static final String DEFAULT_SWING_FONT = DEFAULT_FONT_FAMILY + "-Plain-" + DEFAULT_FONT_SIZE;
19
20 /** Singleton instance. */
21 private static SwingFontManager _instance = null;
22
23 /** Singleton instantiator. */
24 public static SwingFontManager getInstance()
25 {
26 if (_instance == null) _instance = new SwingFontManager();
27
28 return _instance;
29 }
30
31 /** Mapping from handles to internal fonts. */
32 private HashMap<Font, java.awt.Font> _fontMap;
33 /** The font family names registered with the manager. */
34 private HashSet<String> _fontNames;
35
36 /** Private constructor for singleton instance. */
37 private SwingFontManager()
38 {
39 // Initialise the font map
40 _fontMap = new HashMap<Font, java.awt.Font>();
41
42 // Initialise the set of family names
43 _fontNames = new HashSet<String>();
44
45 // Initialise default font to Swing default
46 setDefaultFont(register(java.awt.Font.decode(DEFAULT_SWING_FONT)));
47
48 // Add Swing standard fonts to registered fonts
49 registerSwingStandardFonts();// TODO: Reinstate. cts16
50 }
51
52 @Override
53 public Font registerFontFile(File fontFile)
54 {
55 if (fontFile == null) return null;
56
57 java.awt.Font swingFont = null;
58 boolean registerOkay = true;
59
60 // Create the internal font from the given file
61 try {
62 swingFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);
63 if (swingFont == null) registerOkay = false;
64 } catch (Exception e) {
65 registerOkay = false;
66 }
67
68 // Register the new font with the Swing system
69 if (registerOkay) {
70 registerOkay = GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(swingFont);
71 }
72
73 // Register the new font with ourselves
74 if (registerOkay) {
75 return register(swingFont).clone();
76 }
77
78 // If we made it here the registration failed
79 return null;
80 }
81
82 @Override
83 public String[] getRegisteredFontFamilies() {
84
85 return _fontNames.toArray(new String[_fontNames.size()]);
86 }
87
88 @Override
89 public boolean isFontFamilyRegistered(String fontFamily)
90 {
91 return _fontNames.contains(fontFamily);
92 }
93
94 @Override
95 public Font getActualFont(Font font)
96 {
97 return extractFontForInternalFont(register(font));
98 }
99
100 /** Gets a string for the font style in the form expected by java.awt.Font.decode(String). */
101 private String getStringForStyle(Font.Style style)
102 {
103 if (style == Font.Style.BOLD_ITALIC) return "BOLDITALIC";
104 else if (style == Font.Style.BOLD) return "BOLD";
105 else if (style == Font.Style.ITALIC) return "ITALIC";
106 else return "PLAIN";
107 }
108
109 /** Registers the given handle with the manager and returns the internal font. */
110 private java.awt.Font register(Font font)
111 {
112 if (register(null, font) == null) return null;
113
114 return _fontMap.get(font);
115 }
116
117 /** Registers the given font with the manager and returns the generated handle. */
118 private Font register(java.awt.Font swingFont)
119 {
120 return register(swingFont, null);
121 }
122
123 /** Registers a handle/font pair. */
124 private Font register(java.awt.Font swingFont, Font font)
125 {
126 if (swingFont == null && font == null) return null;
127
128 if (_fontMap.containsKey(font)) return font;
129
130 if (font == null) {
131 font = extractFontForInternalFont(swingFont);
132 } else if (swingFont == null) {
133 swingFont = java.awt.Font.decode(font.getFamilyName() + "-" + getStringForStyle(font.getStyle()) + "-" + font.getSize());
134 }
135
136 if (swingFont == null || font == null) return null;
137
138 _fontMap.put(font, swingFont);
139 _fontNames.add(swingFont.getFamily());
140
141 return font;
142 }
143
144 /** Creates a handle to match the given Swing font. */
145 private Font extractFontForInternalFont(java.awt.Font swingFont)
146 {
147 if (swingFont == null) return null;
148
149 Font font = new Font(swingFont.getFamily());
150 font.setStyle(Font.Style.PLAIN);
151 if (swingFont.isBold()) font.toggleBold();
152 if (swingFont.isItalic()) font.toggleItalic();
153 font.setSize(swingFont.getSize());
154 Number spacing = (Number) swingFont.getAttributes().get(java.awt.font.TextAttribute.TRACKING);
155 if (spacing != null) font.setSpacing(spacing.floatValue());
156
157 return font;
158 }
159
160 /** Gets the internal Swing font object for use by other Swing managers. */
161 public java.awt.Font getInternalFont(Font font)
162 {
163 return register(font);
164 }
165
166 /** Registers any standard fonts available on the system with the manager. */
167 private void registerSwingStandardFonts()
168 {
169 String[] standardFontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
170
171 for (String name : standardFontNames) {
172 register(new Font(name));
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.