1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package tsukuba_bunko.peko.resource; |
20 |
|
|
21 |
|
import java.awt.Font; |
22 |
|
import java.awt.GraphicsEnvironment; |
23 |
|
import java.awt.font.TextAttribute; |
24 |
|
|
25 |
|
import java.io.File; |
26 |
|
import java.io.InputStream; |
27 |
|
|
28 |
|
import java.net.URL; |
29 |
|
|
30 |
|
import java.util.Iterator; |
31 |
|
import java.util.Map; |
32 |
|
import java.util.List; |
33 |
|
import java.util.Locale; |
34 |
|
import java.util.Set; |
35 |
|
|
36 |
|
import tsukuba_bunko.peko.Logger; |
37 |
|
|
38 |
|
import tsukuba_bunko.peko.resource.ResourceManager; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public class FontManager { |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
0 |
private static FontManager _instance = null; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
0 |
private Map _fonts = null; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
0 |
private Set _fontFamilies = null; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
0 |
private Map _bundledFonts = null; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
protected FontManager() |
74 |
|
{ |
75 |
0 |
super(); |
76 |
0 |
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public Font getFont( Map attributes ) |
85 |
|
{ |
86 |
0 |
Font font = (Font)_fonts.get( attributes ); |
87 |
0 |
if( font == null ) { |
88 |
0 |
String family = (String)attributes.get( TextAttribute.FAMILY ); |
89 |
0 |
Logger.debug( "[font] search font: \"" + family + "\"" ); |
90 |
0 |
if( family == null ) { |
91 |
0 |
Logger.debug( "[font] not specified font family. using serif family (default)" ); |
92 |
0 |
attributes.put( TextAttribute.FAMILY, "Serif" ); |
93 |
|
} |
94 |
|
|
95 |
0 |
if( _bundledFonts.keySet().contains(family) ) { |
96 |
0 |
Logger.debug( "[font] using bundled font" ); |
97 |
0 |
attributes.remove( TextAttribute.FAMILY ); |
98 |
0 |
font = ((Font)_bundledFonts.get(family)).deriveFont( attributes ); |
99 |
0 |
attributes.put( TextAttribute.FAMILY, family ); |
100 |
0 |
} |
101 |
0 |
else if( _fontFamilies.contains(family) ) { |
102 |
0 |
Logger.debug( "[font] using system family" ); |
103 |
0 |
font = new Font( attributes ); |
104 |
0 |
} |
105 |
|
else { |
106 |
0 |
Logger.error( "[font] invalid font family specified. family=" + family ); |
107 |
0 |
Logger.debug( "[font] using serif family (default)" ); |
108 |
0 |
attributes.remove( TextAttribute.FAMILY ); |
109 |
0 |
font = new Font("Serif", 1, Font.PLAIN).deriveFont( attributes ); |
110 |
0 |
attributes.put( TextAttribute.FAMILY, family ); |
111 |
|
} |
112 |
0 |
_fonts.put( attributes, font ); |
113 |
|
} |
114 |
0 |
return font; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
private void initialize() |
121 |
|
{ |
122 |
0 |
ResourceManager resources = ResourceManager.getInstance(); |
123 |
|
|
124 |
0 |
_fontFamilies = new java.util.HashSet( 89 ); |
125 |
|
|
126 |
0 |
Locale locale = (Locale)resources.getResource( "peko.system.locale" ); |
127 |
0 |
if( locale == null ) { |
128 |
0 |
locale = Locale.getDefault(); |
129 |
|
} |
130 |
0 |
String[] families = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames( locale ); |
131 |
0 |
for( int i = 0; i < families.length; ++i ) { |
132 |
0 |
_fontFamilies.add( families[i] ); |
133 |
|
} |
134 |
|
|
135 |
0 |
boolean isJ5 = false; |
136 |
|
try { |
137 |
0 |
Font.class.getMethod( "createFont", new Class[]{int.class, File.class} ); |
138 |
0 |
isJ5 = true; |
139 |
|
} |
140 |
0 |
catch( Exception e ) { |
141 |
|
|
142 |
0 |
} |
143 |
|
|
144 |
0 |
_bundledFonts = new java.util.HashMap( 89 ); |
145 |
0 |
List additional = (List)resources.getResource( "peko.system.fonts", true ); |
146 |
0 |
if( (additional != null) && !additional.isEmpty() ) { |
147 |
0 |
InputStream fontStream = null; |
148 |
0 |
URL basedir = resources.getLocationResources().getMiscDirecotryURL(); |
149 |
0 |
String fontPath = null; |
150 |
0 |
Iterator itr = additional.iterator(); |
151 |
0 |
while( itr.hasNext() ) { |
152 |
|
try { |
153 |
0 |
fontPath = (String)itr.next(); |
154 |
0 |
if( (fontPath == null) || (fontPath.length() == 0) ) { |
155 |
|
continue; |
156 |
|
} |
157 |
0 |
Logger.debug( "[font] loading font \"" + fontPath + "\"." ); |
158 |
0 |
Font font = null; |
159 |
0 |
URL fontURL = new URL( basedir, fontPath ); |
160 |
0 |
String protocol = fontURL.getProtocol(); |
161 |
0 |
if( isJ5 && "file".equals(protocol) ) { |
162 |
0 |
File fontFile = new File( fontURL.getFile() ); |
163 |
0 |
font = Font.createFont( Font.TRUETYPE_FONT, fontFile ); |
164 |
0 |
} |
165 |
|
else { |
166 |
0 |
fontStream = fontURL.openStream(); |
167 |
0 |
font = Font.createFont(Font.TRUETYPE_FONT, fontStream ); |
168 |
|
} |
169 |
0 |
String fontName = fontPath; |
170 |
0 |
int extDelim = fontName.lastIndexOf( '.' ); |
171 |
0 |
if( extDelim != -1 ) { |
172 |
0 |
fontName = fontName.substring( 0, extDelim ); |
173 |
|
} |
174 |
0 |
_bundledFonts.put( fontName, font ); |
175 |
0 |
_bundledFonts.put( (String)font.getAttributes().get(TextAttribute.FAMILY), font ); |
176 |
|
} |
177 |
0 |
catch( Exception e ) { |
178 |
0 |
Logger.warn( "[font] fail to load font \"" + fontPath + "\".", e ); |
179 |
|
} |
180 |
|
finally { |
181 |
0 |
if( fontStream != null ) { |
182 |
|
try { |
183 |
0 |
fontStream.close(); |
184 |
|
} |
185 |
0 |
catch( Exception e ) { |
186 |
0 |
Logger.error( "[font] fail to close font stream. \"" + fontPath + "\".", e ); |
187 |
0 |
} |
188 |
0 |
fontStream = null; |
189 |
0 |
} |
190 |
0 |
} |
191 |
0 |
} |
192 |
|
} |
193 |
|
|
194 |
0 |
_fonts = new java.util.WeakHashMap( 89 ); |
195 |
0 |
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
public static FontManager getInstance() |
204 |
|
{ |
205 |
0 |
if( _instance == null ) { |
206 |
0 |
synchronized( FontManager.class ) { |
207 |
0 |
if( _instance == null ) { |
208 |
0 |
_instance = new FontManager(); |
209 |
0 |
_instance.initialize(); |
210 |
|
} |
211 |
0 |
} |
212 |
|
} |
213 |
0 |
return _instance; |
214 |
|
} |
215 |
|
} |