author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 560 | 3a8029083a72 |
child 889 | 6549643c008c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.font; |
|
27 |
||
28 |
import java.awt.Font; |
|
29 |
import java.awt.GraphicsEnvironment; |
|
30 |
import java.awt.FontFormatException; |
|
31 |
import java.io.File; |
|
32 |
import java.io.FilenameFilter; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.PrivilegedAction; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.Hashtable; |
|
39 |
import java.util.Iterator; |
|
40 |
import java.util.Locale; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.NoSuchElementException; |
|
43 |
import java.util.StringTokenizer; |
|
44 |
import java.util.TreeMap; |
|
45 |
import java.util.Vector; |
|
46 |
import java.util.concurrent.ConcurrentHashMap; |
|
47 |
import java.util.logging.Level; |
|
48 |
import java.util.logging.Logger; |
|
49 |
import javax.swing.plaf.FontUIResource; |
|
50 |
||
51 |
import sun.awt.AppContext; |
|
52 |
import sun.awt.FontConfiguration; |
|
53 |
import sun.awt.SunHints; |
|
54 |
import sun.awt.SunToolkit; |
|
55 |
import sun.java2d.HeadlessGraphicsEnvironment; |
|
56 |
import sun.java2d.SunGraphicsEnvironment; |
|
57 |
||
58 |
import java.awt.geom.GeneralPath; |
|
59 |
import java.awt.geom.Point2D; |
|
60 |
import java.awt.geom.Rectangle2D; |
|
61 |
||
62 |
import java.lang.reflect.Constructor; |
|
63 |
||
64 |
import sun.java2d.Disposer; |
|
65 |
||
66 |
/* |
|
67 |
* Interface between Java Fonts (java.awt.Font) and the underlying |
|
68 |
* font files/native font resources and the Java and native font scalers. |
|
69 |
*/ |
|
70 |
public final class FontManager { |
|
71 |
||
72 |
public static final int FONTFORMAT_NONE = -1; |
|
73 |
public static final int FONTFORMAT_TRUETYPE = 0; |
|
74 |
public static final int FONTFORMAT_TYPE1 = 1; |
|
75 |
public static final int FONTFORMAT_T2K = 2; |
|
76 |
public static final int FONTFORMAT_TTC = 3; |
|
77 |
public static final int FONTFORMAT_COMPOSITE = 4; |
|
78 |
public static final int FONTFORMAT_NATIVE = 5; |
|
79 |
||
80 |
public static final int NO_FALLBACK = 0; |
|
81 |
public static final int PHYSICAL_FALLBACK = 1; |
|
82 |
public static final int LOGICAL_FALLBACK = 2; |
|
83 |
||
84 |
public static final int QUADPATHTYPE = 1; |
|
85 |
public static final int CUBICPATHTYPE = 2; |
|
86 |
||
87 |
/* Pool of 20 font file channels chosen because some UTF-8 locale |
|
88 |
* composite fonts can use up to 16 platform fonts (including the |
|
89 |
* Lucida fall back). This should prevent channel thrashing when |
|
90 |
* dealing with one of these fonts. |
|
91 |
* The pool array stores the fonts, rather than directly referencing |
|
92 |
* the channels, as the font needs to do the open/close work. |
|
93 |
*/ |
|
94 |
private static final int CHANNELPOOLSIZE = 20; |
|
95 |
private static int lastPoolIndex = 0; |
|
96 |
private static FileFont fontFileCache[] = new FileFont[CHANNELPOOLSIZE]; |
|
97 |
||
98 |
/* Need to implement a simple linked list scheme for fast |
|
99 |
* traversal and lookup. |
|
100 |
* Also want to "fast path" dialog so there's minimal overhead. |
|
101 |
*/ |
|
102 |
/* There are at exactly 20 composite fonts: 5 faces (but some are not |
|
103 |
* usually different), in 4 styles. The array may be auto-expanded |
|
104 |
* later if more are needed, eg for user-defined composites or locale |
|
105 |
* variants. |
|
106 |
*/ |
|
107 |
private static int maxCompFont = 0; |
|
108 |
private static CompositeFont [] compFonts = new CompositeFont[20]; |
|
109 |
private static ConcurrentHashMap<String, CompositeFont> |
|
110 |
compositeFonts = new ConcurrentHashMap<String, CompositeFont>(); |
|
111 |
private static ConcurrentHashMap<String, PhysicalFont> |
|
112 |
physicalFonts = new ConcurrentHashMap<String, PhysicalFont>(); |
|
113 |
private static ConcurrentHashMap<String, PhysicalFont> |
|
114 |
registeredFontFiles = new ConcurrentHashMap<String, PhysicalFont>(); |
|
115 |
||
116 |
/* given a full name find the Font. Remind: there's duplication |
|
117 |
* here in that this contains the content of compositeFonts + |
|
118 |
* physicalFonts. |
|
119 |
*/ |
|
120 |
private static ConcurrentHashMap<String, Font2D> |
|
121 |
fullNameToFont = new ConcurrentHashMap<String, Font2D>(); |
|
122 |
||
123 |
/* TrueType fonts have localised names. Support searching all |
|
124 |
* of these before giving up on a name. |
|
125 |
*/ |
|
126 |
private static HashMap<String, TrueTypeFont> localeFullNamesToFont; |
|
127 |
||
128 |
private static PhysicalFont defaultPhysicalFont; |
|
129 |
||
130 |
/* deprecated, unsupported hack - actually invokes a bug! */ |
|
131 |
private static boolean usePlatformFontMetrics = false; |
|
132 |
||
133 |
public static Logger logger = null; |
|
134 |
public static boolean logging; |
|
135 |
static boolean longAddresses; |
|
136 |
static String osName; |
|
137 |
static boolean useT2K; |
|
138 |
static boolean isWindows; |
|
139 |
static boolean isSolaris; |
|
140 |
public static boolean isSolaris8; // needed to check for JA wavedash fix. |
|
141 |
public static boolean isSolaris9; // needed to check for songti font usage. |
|
142 |
private static boolean loaded1dot0Fonts = false; |
|
143 |
static SunGraphicsEnvironment sgEnv; |
|
144 |
static boolean loadedAllFonts = false; |
|
145 |
static boolean loadedAllFontFiles = false; |
|
146 |
static TrueTypeFont eudcFont; |
|
147 |
static HashMap<String,String> jreFontMap; |
|
148 |
static HashSet<String> jreLucidaFontFiles; |
|
149 |
static String[] jreOtherFontFiles; |
|
150 |
static boolean noOtherJREFontFiles = false; // initial assumption. |
|
151 |
||
152 |
/* Used to indicate required return type from toArray(..); */ |
|
153 |
private static String[] STR_ARRAY = new String[0]; |
|
154 |
||
155 |
private static void initJREFontMap() { |
|
156 |
||
157 |
/* Key is familyname+style value as an int. |
|
158 |
* Value is filename containing the font. |
|
159 |
* If no mapping exists, it means there is no font file for the style |
|
160 |
* If the mapping exists but the file doesn't exist in the deferred |
|
161 |
* list then it means its not installed. |
|
162 |
* This looks like a lot of code and strings but if it saves even |
|
163 |
* a single file being opened at JRE start-up there's a big payoff. |
|
164 |
* Lucida Sans is probably the only important case as the others |
|
165 |
* are rarely used. Consider removing the other mappings if there's |
|
166 |
* no evidence they are useful in practice. |
|
167 |
*/ |
|
168 |
jreFontMap = new HashMap<String,String>(); |
|
169 |
jreLucidaFontFiles = new HashSet<String>(); |
|
170 |
if (SunGraphicsEnvironment.isOpenJDK()) { |
|
171 |
return; |
|
172 |
} |
|
173 |
/* Lucida Sans Family */ |
|
174 |
jreFontMap.put("lucida sans0", "LucidaSansRegular.ttf"); |
|
175 |
jreFontMap.put("lucida sans1", "LucidaSansDemiBold.ttf"); |
|
176 |
/* Lucida Sans full names (map Bold and DemiBold to same file) */ |
|
177 |
jreFontMap.put("lucida sans regular0", "LucidaSansRegular.ttf"); |
|
178 |
jreFontMap.put("lucida sans regular1", "LucidaSansDemiBold.ttf"); |
|
179 |
jreFontMap.put("lucida sans bold1", "LucidaSansDemiBold.ttf"); |
|
180 |
jreFontMap.put("lucida sans demibold1", "LucidaSansDemiBold.ttf"); |
|
181 |
||
182 |
/* Lucida Sans Typewriter Family */ |
|
183 |
jreFontMap.put("lucida sans typewriter0", |
|
184 |
"LucidaTypewriterRegular.ttf"); |
|
185 |
jreFontMap.put("lucida sans typewriter1", "LucidaTypewriterBold.ttf"); |
|
186 |
/* Typewriter full names (map Bold and DemiBold to same file) */ |
|
187 |
jreFontMap.put("lucida sans typewriter regular0", |
|
188 |
"LucidaTypewriter.ttf"); |
|
189 |
jreFontMap.put("lucida sans typewriter regular1", |
|
190 |
"LucidaTypewriterBold.ttf"); |
|
191 |
jreFontMap.put("lucida sans typewriter bold1", |
|
192 |
"LucidaTypewriterBold.ttf"); |
|
193 |
jreFontMap.put("lucida sans typewriter demibold1", |
|
194 |
"LucidaTypewriterBold.ttf"); |
|
195 |
||
196 |
/* Lucida Bright Family */ |
|
197 |
jreFontMap.put("lucida bright0", "LucidaBrightRegular.ttf"); |
|
198 |
jreFontMap.put("lucida bright1", "LucidaBrightDemiBold.ttf"); |
|
199 |
jreFontMap.put("lucida bright2", "LucidaBrightItalic.ttf"); |
|
200 |
jreFontMap.put("lucida bright3", "LucidaBrightDemiItalic.ttf"); |
|
201 |
/* Lucida Bright full names (map Bold and DemiBold to same file) */ |
|
202 |
jreFontMap.put("lucida bright regular0", "LucidaBrightRegular.ttf"); |
|
203 |
jreFontMap.put("lucida bright regular1", "LucidaBrightDemiBold.ttf"); |
|
204 |
jreFontMap.put("lucida bright regular2", "LucidaBrightItalic.ttf"); |
|
205 |
jreFontMap.put("lucida bright regular3", "LucidaBrightDemiItalic.ttf"); |
|
206 |
jreFontMap.put("lucida bright bold1", "LucidaBrightDemiBold.ttf"); |
|
207 |
jreFontMap.put("lucida bright bold3", "LucidaBrightDemiItalic.ttf"); |
|
208 |
jreFontMap.put("lucida bright demibold1", "LucidaBrightDemiBold.ttf"); |
|
209 |
jreFontMap.put("lucida bright demibold3","LucidaBrightDemiItalic.ttf"); |
|
210 |
jreFontMap.put("lucida bright italic2", "LucidaBrightItalic.ttf"); |
|
211 |
jreFontMap.put("lucida bright italic3", "LucidaBrightDemiItalic.ttf"); |
|
212 |
jreFontMap.put("lucida bright bold italic3", |
|
213 |
"LucidaBrightDemiItalic.ttf"); |
|
214 |
jreFontMap.put("lucida bright demibold italic3", |
|
215 |
"LucidaBrightDemiItalic.ttf"); |
|
216 |
for (String ffile : jreFontMap.values()) { |
|
217 |
jreLucidaFontFiles.add(ffile); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
static { |
|
222 |
||
223 |
if (SunGraphicsEnvironment.debugFonts) { |
|
224 |
logger = Logger.getLogger("sun.java2d", null); |
|
225 |
logging = logger.getLevel() != Level.OFF; |
|
226 |
} |
|
227 |
initJREFontMap(); |
|
228 |
||
229 |
java.security.AccessController.doPrivileged( |
|
230 |
new java.security.PrivilegedAction() { |
|
231 |
public Object run() { |
|
232 |
FontManagerNativeLibrary.load(); |
|
233 |
||
234 |
// JNI throws an exception if a class/method/field is not found, |
|
235 |
// so there's no need to do anything explicit here. |
|
236 |
initIDs(); |
|
237 |
||
238 |
switch (StrikeCache.nativeAddressSize) { |
|
239 |
case 8: longAddresses = true; break; |
|
240 |
case 4: longAddresses = false; break; |
|
241 |
default: throw new RuntimeException("Unexpected address size"); |
|
242 |
} |
|
243 |
||
244 |
osName = System.getProperty("os.name", "unknownOS"); |
|
245 |
isSolaris = osName.startsWith("SunOS"); |
|
246 |
||
550
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
532
diff
changeset
|
247 |
String t2kStr = System.getProperty("sun.java2d.font.scaler"); |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
532
diff
changeset
|
248 |
if (t2kStr != null) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
532
diff
changeset
|
249 |
useT2K = "t2k".equals(t2kStr); |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
532
diff
changeset
|
250 |
} |
2 | 251 |
if (isSolaris) { |
252 |
String version = System.getProperty("os.version", "unk"); |
|
253 |
isSolaris8 = version.equals("5.8"); |
|
254 |
isSolaris9 = version.equals("5.9"); |
|
255 |
} else { |
|
256 |
isWindows = osName.startsWith("Windows"); |
|
257 |
if (isWindows) { |
|
258 |
String eudcFile = |
|
259 |
SunGraphicsEnvironment.eudcFontFileName; |
|
260 |
if (eudcFile != null) { |
|
261 |
try { |
|
262 |
eudcFont = new TrueTypeFont(eudcFile, null, 0, |
|
263 |
true); |
|
264 |
} catch (FontFormatException e) { |
|
265 |
} |
|
266 |
} |
|
267 |
String prop = |
|
268 |
System.getProperty("java2d.font.usePlatformFont"); |
|
269 |
if (("true".equals(prop) || getPlatformFontVar())) { |
|
270 |
usePlatformFontMetrics = true; |
|
271 |
System.out.println("Enabling platform font metrics for win32. This is an unsupported option."); |
|
272 |
System.out.println("This yields incorrect composite font metrics as reported by 1.1.x releases."); |
|
273 |
System.out.println("It is appropriate only for use by applications which do not use any Java 2"); |
|
274 |
System.out.println("functionality. This property will be removed in a later release."); |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
return null; |
|
279 |
} |
|
280 |
}); |
|
281 |
} |
|
282 |
||
283 |
/* Initialise ptrs used by JNI methods */ |
|
284 |
private static native void initIDs(); |
|
285 |
||
286 |
public static void addToPool(FileFont font) { |
|
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
287 |
|
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
288 |
FileFont fontFileToClose = null; |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
289 |
int freeSlot = -1; |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
290 |
|
2 | 291 |
synchronized (fontFileCache) { |
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
292 |
/* Avoid duplicate entries in the pool, and don't close() it, |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
293 |
* since this method is called only from within open(). |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
294 |
* Seeing a duplicate is most likely to happen if the thread |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
295 |
* was interrupted during a read, forcing perhaps repeated |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
296 |
* close and open calls and it eventually it ends up pointing |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
297 |
* at the same slot. |
2 | 298 |
*/ |
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
299 |
for (int i=0;i<CHANNELPOOLSIZE;i++) { |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
300 |
if (fontFileCache[i] == font) { |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
301 |
return; |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
302 |
} |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
303 |
if (fontFileCache[i] == null && freeSlot < 0) { |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
304 |
freeSlot = i; |
2 | 305 |
} |
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
306 |
} |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
307 |
if (freeSlot >= 0) { |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
308 |
fontFileCache[freeSlot] = font; |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
309 |
return; |
2 | 310 |
} else { |
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
311 |
/* replace with new font. */ |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
312 |
fontFileToClose = fontFileCache[lastPoolIndex]; |
2 | 313 |
fontFileCache[lastPoolIndex] = font; |
314 |
/* lastPoolIndex is updated so that the least recently opened |
|
315 |
* file will be closed next. |
|
316 |
*/ |
|
317 |
lastPoolIndex = (lastPoolIndex+1) % CHANNELPOOLSIZE; |
|
318 |
} |
|
319 |
} |
|
532
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
320 |
/* Need to close the font file outside of the synchronized block, |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
321 |
* since its possible some other thread is in an open() call on |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
322 |
* this font file, and could be holding its lock and the pool lock. |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
323 |
* Releasing the pool lock allows that thread to continue, so it can |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
324 |
* then release the lock on this font, allowing the close() call |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
325 |
* below to proceed. |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
326 |
* Also, calling close() is safe because any other thread using |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
327 |
* the font we are closing() synchronizes all reading, so we |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
328 |
* will not close the file while its in use. |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
329 |
*/ |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
330 |
if (fontFileToClose != null) { |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
331 |
fontFileToClose.close(); |
6eb7f9aa0b05
6640532: Graphics.getFontMetrics() throws NullPointerException
prr
parents:
2
diff
changeset
|
332 |
} |
2 | 333 |
} |
334 |
||
335 |
/* |
|
336 |
* In the normal course of events, the pool of fonts can remain open |
|
337 |
* ready for quick access to their contents. The pool is sized so |
|
338 |
* that it is not an excessive consumer of system resources whilst |
|
339 |
* facilitating performance by providing ready access to the most |
|
340 |
* recently used set of font files. |
|
341 |
* The only reason to call removeFromPool(..) is for a Font that |
|
342 |
* you want to to have GC'd. Currently this would apply only to fonts |
|
343 |
* created with java.awt.Font.createFont(..). |
|
344 |
* In this case, the caller is expected to have arranged for the file |
|
345 |
* to be closed. |
|
346 |
* REMIND: consider how to know when a createFont created font should |
|
347 |
* be closed. |
|
348 |
*/ |
|
349 |
public static void removeFromPool(FileFont font) { |
|
350 |
synchronized (fontFileCache) { |
|
351 |
for (int i=0; i<CHANNELPOOLSIZE; i++) { |
|
352 |
if (fontFileCache[i] == font) { |
|
353 |
fontFileCache[i] = null; |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* This method is provided for internal and exclusive use by Swing. |
|
361 |
* |
|
362 |
* @param font representing a physical font. |
|
363 |
* @return true if the underlying font is a TrueType or OpenType font |
|
364 |
* that claims to support the Microsoft Windows encoding corresponding to |
|
365 |
* the default file.encoding property of this JRE instance. |
|
366 |
* This narrow value is useful for Swing to decide if the font is useful |
|
367 |
* for the the Windows Look and Feel, or, if a composite font should be |
|
368 |
* used instead. |
|
369 |
* The information used to make the decision is obtained from |
|
370 |
* the ulCodePageRange fields in the font. |
|
371 |
* A caller can use isLogicalFont(Font) in this class before calling |
|
372 |
* this method and would not need to call this method if that |
|
373 |
* returns true. |
|
374 |
*/ |
|
375 |
// static boolean fontSupportsDefaultEncoding(Font font) { |
|
376 |
// String encoding = |
|
377 |
// (String) java.security.AccessController.doPrivileged( |
|
378 |
// new sun.security.action.GetPropertyAction("file.encoding")); |
|
379 |
||
380 |
// if (encoding == null || font == null) { |
|
381 |
// return false; |
|
382 |
// } |
|
383 |
||
384 |
// encoding = encoding.toLowerCase(Locale.ENGLISH); |
|
385 |
||
386 |
// return FontManager.fontSupportsEncoding(font, encoding); |
|
387 |
// } |
|
388 |
||
389 |
/* Revise the implementation to in fact mean "font is a composite font. |
|
390 |
* This ensures that Swing components will always benefit from the |
|
391 |
* fall back fonts |
|
392 |
*/ |
|
393 |
public static boolean fontSupportsDefaultEncoding(Font font) { |
|
394 |
return getFont2D(font) instanceof CompositeFont; |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* This method is provided for internal and exclusive use by Swing. |
|
399 |
* |
|
400 |
* It may be used in conjunction with fontSupportsDefaultEncoding(Font) |
|
401 |
* In the event that a desktop properties font doesn't directly |
|
402 |
* support the default encoding, (ie because the host OS supports |
|
403 |
* adding support for the current locale automatically for native apps), |
|
404 |
* then Swing calls this method to get a font which uses the specified |
|
405 |
* font for the code points it covers, but also supports this locale |
|
406 |
* just as the standard composite fonts do. |
|
407 |
* Note: this will over-ride any setting where an application |
|
408 |
* specifies it prefers locale specific composite fonts. |
|
409 |
* The logic for this, is that this method is used only where the user or |
|
410 |
* application has specified that the native L&F be used, and that |
|
411 |
* we should honour that request to use the same font as native apps use. |
|
412 |
* |
|
413 |
* The behaviour of this method is to construct a new composite |
|
414 |
* Font object that uses the specified physical font as its first |
|
415 |
* component, and adds all the components of "dialog" as fall back |
|
416 |
* components. |
|
417 |
* The method currently assumes that only the size and style attributes |
|
418 |
* are set on the specified font. It doesn't copy the font transform or |
|
419 |
* other attributes because they aren't set on a font created from |
|
420 |
* the desktop. This will need to be fixed if use is broadened. |
|
421 |
* |
|
422 |
* Operations such as Font.deriveFont will work properly on the |
|
423 |
* font returned by this method for deriving a different point size. |
|
424 |
* Additionally it tries to support a different style by calling |
|
425 |
* getNewComposite() below. That also supports replacing slot zero |
|
426 |
* with a different physical font but that is expected to be "rare". |
|
427 |
* Deriving with a different style is needed because its been shown |
|
428 |
* that some applications try to do this for Swing FontUIResources. |
|
429 |
* Also operations such as new Font(font.getFontName(..), Font.PLAIN, 14); |
|
430 |
* will NOT yield the same result, as the new underlying CompositeFont |
|
431 |
* cannot be "looked up" in the font registry. |
|
432 |
* This returns a FontUIResource as that is the Font sub-class needed |
|
433 |
* by Swing. |
|
434 |
* Suggested usage is something like : |
|
435 |
* FontUIResource fuir; |
|
436 |
* Font desktopFont = getDesktopFont(..); |
|
437 |
* // NOTE even if fontSupportsDefaultEncoding returns true because |
|
438 |
* // you get Tahoma and are running in an English locale, you may |
|
439 |
* // still want to just call getCompositeFontUIResource() anyway |
|
440 |
* // as only then will you get fallback fonts - eg for CJK. |
|
441 |
* if (FontManager.fontSupportsDefaultEncoding(desktopFont)) { |
|
442 |
* fuir = new FontUIResource(..); |
|
443 |
* } else { |
|
444 |
* fuir = FontManager.getCompositeFontUIResource(desktopFont); |
|
445 |
* } |
|
446 |
* return fuir; |
|
447 |
*/ |
|
448 |
public static FontUIResource getCompositeFontUIResource(Font font) { |
|
449 |
||
450 |
FontUIResource fuir = |
|
451 |
new FontUIResource(font.getName(),font.getStyle(),font.getSize()); |
|
452 |
Font2D font2D = getFont2D(font); |
|
453 |
||
454 |
if (!(font2D instanceof PhysicalFont)) { |
|
455 |
/* Swing should only be calling this when a font is obtained |
|
456 |
* from desktop properties, so should generally be a physical font, |
|
457 |
* an exception might be for names like "MS Serif" which are |
|
458 |
* automatically mapped to "Serif", so there's no need to do |
|
459 |
* anything special in that case. But note that suggested usage |
|
460 |
* is first to call fontSupportsDefaultEncoding(Font) and this |
|
461 |
* method should not be called if that were to return true. |
|
462 |
*/ |
|
463 |
return fuir; |
|
464 |
} |
|
465 |
||
466 |
CompositeFont dialog2D = |
|
467 |
(CompositeFont)findFont2D("dialog", font.getStyle(), NO_FALLBACK); |
|
468 |
if (dialog2D == null) { /* shouldn't happen */ |
|
469 |
return fuir; |
|
470 |
} |
|
471 |
PhysicalFont physicalFont = (PhysicalFont)font2D; |
|
472 |
CompositeFont compFont = new CompositeFont(physicalFont, dialog2D); |
|
473 |
setFont2D(fuir, compFont.handle); |
|
474 |
/* marking this as a created font is needed as only created fonts |
|
475 |
* copy their creator's handles. |
|
476 |
*/ |
|
477 |
setCreatedFont(fuir); |
|
478 |
return fuir; |
|
479 |
} |
|
480 |
||
481 |
public static Font2DHandle getNewComposite(String family, int style, |
|
482 |
Font2DHandle handle) { |
|
483 |
||
484 |
if (!(handle.font2D instanceof CompositeFont)) { |
|
485 |
return handle; |
|
486 |
} |
|
487 |
||
488 |
CompositeFont oldComp = (CompositeFont)handle.font2D; |
|
489 |
PhysicalFont oldFont = oldComp.getSlotFont(0); |
|
490 |
||
491 |
if (family == null) { |
|
492 |
family = oldFont.getFamilyName(null); |
|
493 |
} |
|
494 |
if (style == -1) { |
|
495 |
style = oldComp.getStyle(); |
|
496 |
} |
|
497 |
||
498 |
Font2D newFont = findFont2D(family, style, NO_FALLBACK); |
|
499 |
if (!(newFont instanceof PhysicalFont)) { |
|
500 |
newFont = oldFont; |
|
501 |
} |
|
502 |
PhysicalFont physicalFont = (PhysicalFont)newFont; |
|
503 |
CompositeFont dialog2D = |
|
504 |
(CompositeFont)findFont2D("dialog", style, NO_FALLBACK); |
|
505 |
if (dialog2D == null) { /* shouldn't happen */ |
|
506 |
return handle; |
|
507 |
} |
|
508 |
CompositeFont compFont = new CompositeFont(physicalFont, dialog2D); |
|
509 |
Font2DHandle newHandle = new Font2DHandle(compFont); |
|
510 |
return newHandle; |
|
511 |
} |
|
512 |
||
513 |
public static native void setFont2D(Font font, Font2DHandle font2DHandle); |
|
514 |
||
515 |
private static native boolean isCreatedFont(Font font); |
|
516 |
private static native void setCreatedFont(Font font); |
|
517 |
||
518 |
public static void registerCompositeFont(String compositeName, |
|
519 |
String[] componentFileNames, |
|
520 |
String[] componentNames, |
|
521 |
int numMetricsSlots, |
|
522 |
int[] exclusionRanges, |
|
523 |
int[] exclusionMaxIndex, |
|
524 |
boolean defer) { |
|
525 |
||
526 |
CompositeFont cf = new CompositeFont(compositeName, |
|
527 |
componentFileNames, |
|
528 |
componentNames, |
|
529 |
numMetricsSlots, |
|
530 |
exclusionRanges, |
|
531 |
exclusionMaxIndex, defer); |
|
532 |
addCompositeToFontList(cf, Font2D.FONT_CONFIG_RANK); |
|
533 |
synchronized (compFonts) { |
|
534 |
compFonts[maxCompFont++] = cf; |
|
535 |
} |
|
536 |
} |
|
537 |
||
538 |
/* This variant is used only when the application specifies |
|
539 |
* a variant of composite fonts which prefers locale specific or |
|
540 |
* proportional fonts. |
|
541 |
*/ |
|
542 |
public static void registerCompositeFont(String compositeName, |
|
543 |
String[] componentFileNames, |
|
544 |
String[] componentNames, |
|
545 |
int numMetricsSlots, |
|
546 |
int[] exclusionRanges, |
|
547 |
int[] exclusionMaxIndex, |
|
548 |
boolean defer, |
|
549 |
ConcurrentHashMap<String, Font2D> |
|
550 |
altNameCache) { |
|
551 |
||
552 |
CompositeFont cf = new CompositeFont(compositeName, |
|
553 |
componentFileNames, |
|
554 |
componentNames, |
|
555 |
numMetricsSlots, |
|
556 |
exclusionRanges, |
|
557 |
exclusionMaxIndex, defer); |
|
558 |
/* if the cache has an existing composite for this case, make |
|
559 |
* its handle point to this new font. |
|
560 |
* This ensures that when the altNameCache that is passed in |
|
561 |
* is the global mapNameCache - ie we are running as an application - |
|
562 |
* that any statically created java.awt.Font instances which already |
|
563 |
* have a Font2D instance will have that re-directed to the new Font |
|
564 |
* on subsequent uses. This is particularly important for "the" |
|
565 |
* default font instance, or similar cases where a UI toolkit (eg |
|
566 |
* Swing) has cached a java.awt.Font. Note that if Swing is using |
|
567 |
* a custom composite APIs which update the standard composites have |
|
568 |
* no effect - this is typically the case only when using the Windows |
|
569 |
* L&F where these APIs would conflict with that L&F anyway. |
|
570 |
*/ |
|
571 |
Font2D oldFont = (Font2D) |
|
572 |
altNameCache.get(compositeName.toLowerCase(Locale.ENGLISH)); |
|
573 |
if (oldFont instanceof CompositeFont) { |
|
574 |
oldFont.handle.font2D = cf; |
|
575 |
} |
|
576 |
altNameCache.put(compositeName.toLowerCase(Locale.ENGLISH), cf); |
|
577 |
} |
|
578 |
||
579 |
private static void addCompositeToFontList(CompositeFont f, int rank) { |
|
580 |
||
581 |
if (logging) { |
|
582 |
logger.info("Add to Family "+ f.familyName + |
|
583 |
", Font " + f.fullName + " rank="+rank); |
|
584 |
} |
|
585 |
f.setRank(rank); |
|
586 |
compositeFonts.put(f.fullName, f); |
|
587 |
fullNameToFont.put(f.fullName.toLowerCase(Locale.ENGLISH), f); |
|
588 |
||
589 |
FontFamily family = FontFamily.getFamily(f.familyName); |
|
590 |
if (family == null) { |
|
591 |
family = new FontFamily(f.familyName, true, rank); |
|
592 |
} |
|
593 |
family.setFont(f, f.style); |
|
594 |
} |
|
595 |
||
596 |
/* |
|
597 |
* Systems may have fonts with the same name. |
|
598 |
* We want to register only one of such fonts (at least until |
|
599 |
* such time as there might be APIs which can accommodate > 1). |
|
600 |
* Rank is 1) font configuration fonts, 2) JRE fonts, 3) OT/TT fonts, |
|
601 |
* 4) Type1 fonts, 5) native fonts. |
|
602 |
* |
|
603 |
* If the new font has the same name as the old font, the higher |
|
604 |
* ranked font gets added, replacing the lower ranked one. |
|
605 |
* If the fonts are of equal rank, then make a special case of |
|
606 |
* font configuration rank fonts, which are on closer inspection, |
|
607 |
* OT/TT fonts such that the larger font is registered. This is |
|
608 |
* a heuristic since a font may be "larger" in the sense of more |
|
609 |
* code points, or be a larger "file" because it has more bitmaps. |
|
610 |
* So it is possible that using filesize may lead to less glyphs, and |
|
611 |
* using glyphs may lead to lower quality display. Probably number |
|
612 |
* of glyphs is the ideal, but filesize is information we already |
|
613 |
* have and is good enough for the known cases. |
|
614 |
* Also don't want to register fonts that match JRE font families |
|
615 |
* but are coming from a source other than the JRE. |
|
616 |
* This will ensure that we will algorithmically style the JRE |
|
617 |
* plain font and get the same set of glyphs for all styles. |
|
618 |
* |
|
619 |
* Note that this method returns a value |
|
620 |
* if it returns the same object as its argument that means this |
|
621 |
* font was newly registered. |
|
622 |
* If it returns a different object it means this font already exists, |
|
623 |
* and you should use that one. |
|
624 |
* If it returns null means this font was not registered and none |
|
625 |
* in that name is registered. The caller must find a substitute |
|
626 |
*/ |
|
627 |
private static PhysicalFont addToFontList(PhysicalFont f, int rank) { |
|
628 |
||
629 |
String fontName = f.fullName; |
|
630 |
String familyName = f.familyName; |
|
631 |
if (fontName == null || "".equals(fontName)) { |
|
632 |
return null; |
|
633 |
} |
|
634 |
if (compositeFonts.containsKey(fontName)) { |
|
635 |
/* Don't register any font that has the same name as a composite */ |
|
636 |
return null; |
|
637 |
} |
|
638 |
f.setRank(rank); |
|
639 |
if (!physicalFonts.containsKey(fontName)) { |
|
640 |
if (logging) { |
|
641 |
logger.info("Add to Family "+familyName + |
|
642 |
", Font " + fontName + " rank="+rank); |
|
643 |
} |
|
644 |
physicalFonts.put(fontName, f); |
|
645 |
FontFamily family = FontFamily.getFamily(familyName); |
|
646 |
if (family == null) { |
|
647 |
family = new FontFamily(familyName, false, rank); |
|
648 |
family.setFont(f, f.style); |
|
649 |
} else if (family.getRank() >= rank) { |
|
650 |
family.setFont(f, f.style); |
|
651 |
} |
|
652 |
fullNameToFont.put(fontName.toLowerCase(Locale.ENGLISH), f); |
|
653 |
return f; |
|
654 |
} else { |
|
655 |
PhysicalFont newFont = f; |
|
656 |
PhysicalFont oldFont = physicalFonts.get(fontName); |
|
657 |
if (oldFont == null) { |
|
658 |
return null; |
|
659 |
} |
|
660 |
/* If the new font is of an equal or higher rank, it is a |
|
661 |
* candidate to replace the current one, subject to further tests. |
|
662 |
*/ |
|
663 |
if (oldFont.getRank() >= rank) { |
|
664 |
||
665 |
/* All fonts initialise their mapper when first |
|
666 |
* used. If the mapper is non-null then this font |
|
667 |
* has been accessed at least once. In that case |
|
668 |
* do not replace it. This may be overly stringent, |
|
669 |
* but its probably better not to replace a font that |
|
670 |
* someone is already using without a compelling reason. |
|
671 |
* Additionally the primary case where it is known |
|
672 |
* this behaviour is important is in certain composite |
|
673 |
* fonts, and since all the components of a given |
|
674 |
* composite are usually initialised together this |
|
675 |
* is unlikely. For this to be a problem, there would |
|
676 |
* have to be a case where two different composites used |
|
677 |
* different versions of the same-named font, and they |
|
678 |
* were initialised and used at separate times. |
|
679 |
* In that case we continue on and allow the new font to |
|
680 |
* be installed, but replaceFont will continue to allow |
|
681 |
* the original font to be used in Composite fonts. |
|
682 |
*/ |
|
683 |
if (oldFont.mapper != null && rank > Font2D.FONT_CONFIG_RANK) { |
|
684 |
return oldFont; |
|
685 |
} |
|
686 |
||
687 |
/* Normally we require a higher rank to replace a font, |
|
688 |
* but as a special case, if the two fonts are the same rank, |
|
689 |
* and are instances of TrueTypeFont we want the |
|
690 |
* more complete (larger) one. |
|
691 |
*/ |
|
692 |
if (oldFont.getRank() == rank) { |
|
693 |
if (oldFont instanceof TrueTypeFont && |
|
694 |
newFont instanceof TrueTypeFont) { |
|
695 |
TrueTypeFont oldTTFont = (TrueTypeFont)oldFont; |
|
696 |
TrueTypeFont newTTFont = (TrueTypeFont)newFont; |
|
697 |
if (oldTTFont.fileSize >= newTTFont.fileSize) { |
|
698 |
return oldFont; |
|
699 |
} |
|
700 |
} else { |
|
701 |
return oldFont; |
|
702 |
} |
|
703 |
} |
|
704 |
/* Don't replace ever JRE fonts. |
|
705 |
* This test is in case a font configuration references |
|
706 |
* a Lucida font, which has been mapped to a Lucida |
|
707 |
* from the host O/S. The assumption here is that any |
|
708 |
* such font configuration file is probably incorrect, or |
|
709 |
* the host O/S version is for the use of AWT. |
|
710 |
* In other words if we reach here, there's a possible |
|
711 |
* problem with our choice of font configuration fonts. |
|
712 |
*/ |
|
713 |
if (oldFont.platName.startsWith( |
|
714 |
SunGraphicsEnvironment.jreFontDirName)) { |
|
715 |
if (logging) { |
|
716 |
logger.warning("Unexpected attempt to replace a JRE " + |
|
717 |
" font " + fontName + " from " + |
|
718 |
oldFont.platName + |
|
719 |
" with " + newFont.platName); |
|
720 |
} |
|
721 |
return oldFont; |
|
722 |
} |
|
723 |
||
724 |
if (logging) { |
|
725 |
logger.info("Replace in Family " + familyName + |
|
726 |
",Font " + fontName + " new rank="+rank + |
|
727 |
" from " + oldFont.platName + |
|
728 |
" with " + newFont.platName); |
|
729 |
} |
|
730 |
replaceFont(oldFont, newFont); |
|
731 |
physicalFonts.put(fontName, newFont); |
|
732 |
fullNameToFont.put(fontName.toLowerCase(Locale.ENGLISH), |
|
733 |
newFont); |
|
734 |
||
735 |
FontFamily family = FontFamily.getFamily(familyName); |
|
736 |
if (family == null) { |
|
737 |
family = new FontFamily(familyName, false, rank); |
|
738 |
family.setFont(newFont, newFont.style); |
|
739 |
} else if (family.getRank() >= rank) { |
|
740 |
family.setFont(newFont, newFont.style); |
|
741 |
} |
|
742 |
return newFont; |
|
743 |
} else { |
|
744 |
return oldFont; |
|
745 |
} |
|
746 |
} |
|
747 |
} |
|
748 |
||
749 |
public static Font2D[] getRegisteredFonts() { |
|
750 |
PhysicalFont[] physFonts = getPhysicalFonts(); |
|
751 |
int mcf = maxCompFont; /* for MT-safety */ |
|
752 |
Font2D[] regFonts = new Font2D[physFonts.length+mcf]; |
|
753 |
System.arraycopy(compFonts, 0, regFonts, 0, mcf); |
|
754 |
System.arraycopy(physFonts, 0, regFonts, mcf, physFonts.length); |
|
755 |
return regFonts; |
|
756 |
} |
|
757 |
||
758 |
public static PhysicalFont[] getPhysicalFonts() { |
|
759 |
return physicalFonts.values().toArray(new PhysicalFont[0]); |
|
760 |
} |
|
761 |
||
762 |
||
763 |
/* The class FontRegistrationInfo is used when a client says not |
|
764 |
* to register a font immediately. This mechanism is used to defer |
|
765 |
* initialisation of all the components of composite fonts at JRE |
|
766 |
* start-up. The CompositeFont class is "aware" of this and when it |
|
767 |
* is first used it asks for the registration of its components. |
|
768 |
* Also in the event that any physical font is requested the |
|
769 |
* deferred fonts are initialised before triggering a search of the |
|
770 |
* system. |
|
771 |
* Two maps are used. One to track the deferred fonts. The |
|
772 |
* other to track the fonts that have been initialised through this |
|
773 |
* mechanism. |
|
774 |
*/ |
|
775 |
||
776 |
private static final class FontRegistrationInfo { |
|
777 |
||
778 |
String fontFilePath; |
|
779 |
String[] nativeNames; |
|
780 |
int fontFormat; |
|
781 |
boolean javaRasterizer; |
|
782 |
int fontRank; |
|
783 |
||
784 |
FontRegistrationInfo(String fontPath, String[] names, int format, |
|
785 |
boolean useJavaRasterizer, int rank) { |
|
786 |
this.fontFilePath = fontPath; |
|
787 |
this.nativeNames = names; |
|
788 |
this.fontFormat = format; |
|
789 |
this.javaRasterizer = useJavaRasterizer; |
|
790 |
this.fontRank = rank; |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
private static final ConcurrentHashMap<String, FontRegistrationInfo> |
|
795 |
deferredFontFiles = |
|
796 |
new ConcurrentHashMap<String, FontRegistrationInfo>(); |
|
797 |
private static final ConcurrentHashMap<String, Font2DHandle> |
|
798 |
initialisedFonts = new ConcurrentHashMap<String, Font2DHandle>(); |
|
799 |
||
800 |
/* Remind: possibly enhance initialiseDeferredFonts() to be |
|
801 |
* optionally given a name and a style and it could stop when it |
|
802 |
* finds that font - but this would be a problem if two of the |
|
803 |
* fonts reference the same font face name (cf the Solaris |
|
804 |
* euro fonts). |
|
805 |
*/ |
|
806 |
public static synchronized void initialiseDeferredFonts() { |
|
807 |
for (String fileName : deferredFontFiles.keySet()) { |
|
808 |
initialiseDeferredFont(fileName); |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
public static synchronized void registerDeferredJREFonts(String jreDir) { |
|
813 |
for (FontRegistrationInfo info : deferredFontFiles.values()) { |
|
814 |
if (info.fontFilePath != null && |
|
815 |
info.fontFilePath.startsWith(jreDir)) { |
|
816 |
initialiseDeferredFont(info.fontFilePath); |
|
817 |
} |
|
818 |
} |
|
819 |
} |
|
820 |
||
821 |
/* We keep a map of the files which contain the Lucida fonts so we |
|
822 |
* don't need to search for them. |
|
823 |
* But since we know what fonts these files contain, we can also avoid |
|
824 |
* opening them to look for a font name we don't recognise - see |
|
825 |
* findDeferredFont(). |
|
826 |
* For typical cases where the font isn't a JRE one the overhead is |
|
827 |
* this method call, HashMap.get() and null reference test, then |
|
828 |
* a boolean test of noOtherJREFontFiles. |
|
829 |
*/ |
|
830 |
private static PhysicalFont findJREDeferredFont(String name, int style) { |
|
831 |
||
832 |
PhysicalFont physicalFont; |
|
833 |
String nameAndStyle = name.toLowerCase(Locale.ENGLISH) + style; |
|
834 |
String fileName = jreFontMap.get(nameAndStyle); |
|
835 |
if (fileName != null) { |
|
836 |
initSGEnv(); /* ensure jreFontDirName is initialised */ |
|
837 |
fileName = SunGraphicsEnvironment.jreFontDirName + |
|
838 |
File.separator + fileName; |
|
839 |
if (deferredFontFiles.get(fileName) != null) { |
|
840 |
physicalFont = initialiseDeferredFont(fileName); |
|
841 |
if (physicalFont != null && |
|
842 |
(physicalFont.getFontName(null).equalsIgnoreCase(name) || |
|
843 |
physicalFont.getFamilyName(null).equalsIgnoreCase(name)) |
|
844 |
&& physicalFont.style == style) { |
|
845 |
return physicalFont; |
|
846 |
} |
|
847 |
} |
|
848 |
} |
|
849 |
||
850 |
/* Iterate over the deferred font files looking for any in the |
|
851 |
* jre directory that we didn't recognise, open each of these. |
|
852 |
* In almost all installations this will quickly fall through |
|
853 |
* because only the Lucidas will be present and jreOtherFontFiles |
|
854 |
* will be empty. |
|
855 |
* noOtherJREFontFiles is used so we can skip this block as soon |
|
856 |
* as its determined that its not needed - almost always after the |
|
857 |
* very first time through. |
|
858 |
*/ |
|
859 |
if (noOtherJREFontFiles) { |
|
860 |
return null; |
|
861 |
} |
|
862 |
synchronized (jreLucidaFontFiles) { |
|
863 |
if (jreOtherFontFiles == null) { |
|
864 |
HashSet<String> otherFontFiles = new HashSet<String>(); |
|
865 |
for (String deferredFile : deferredFontFiles.keySet()) { |
|
866 |
File file = new File(deferredFile); |
|
867 |
String dir = file.getParent(); |
|
868 |
String fname = file.getName(); |
|
869 |
/* skip names which aren't absolute, aren't in the JRE |
|
870 |
* directory, or are known Lucida fonts. |
|
871 |
*/ |
|
872 |
if (dir == null || |
|
873 |
!dir.equals(SunGraphicsEnvironment.jreFontDirName) || |
|
874 |
jreLucidaFontFiles.contains(fname)) { |
|
875 |
continue; |
|
876 |
} |
|
877 |
otherFontFiles.add(deferredFile); |
|
878 |
} |
|
879 |
jreOtherFontFiles = otherFontFiles.toArray(STR_ARRAY); |
|
880 |
if (jreOtherFontFiles.length == 0) { |
|
881 |
noOtherJREFontFiles = true; |
|
882 |
} |
|
883 |
} |
|
884 |
||
885 |
for (int i=0; i<jreOtherFontFiles.length;i++) { |
|
886 |
fileName = jreOtherFontFiles[i]; |
|
887 |
if (fileName == null) { |
|
888 |
continue; |
|
889 |
} |
|
890 |
jreOtherFontFiles[i] = null; |
|
891 |
physicalFont = initialiseDeferredFont(fileName); |
|
892 |
if (physicalFont != null && |
|
893 |
(physicalFont.getFontName(null).equalsIgnoreCase(name) || |
|
894 |
physicalFont.getFamilyName(null).equalsIgnoreCase(name)) |
|
895 |
&& physicalFont.style == style) { |
|
896 |
return physicalFont; |
|
897 |
} |
|
898 |
} |
|
899 |
} |
|
900 |
||
901 |
return null; |
|
902 |
} |
|
903 |
||
904 |
/* This skips JRE installed fonts. */ |
|
905 |
private static PhysicalFont findOtherDeferredFont(String name, int style) { |
|
906 |
for (String fileName : deferredFontFiles.keySet()) { |
|
907 |
File file = new File(fileName); |
|
908 |
String dir = file.getParent(); |
|
909 |
String fname = file.getName(); |
|
910 |
if (dir != null && |
|
911 |
dir.equals(SunGraphicsEnvironment.jreFontDirName) && |
|
912 |
jreLucidaFontFiles.contains(fname)) { |
|
913 |
continue; |
|
914 |
} |
|
915 |
PhysicalFont physicalFont = initialiseDeferredFont(fileName); |
|
916 |
if (physicalFont != null && |
|
917 |
(physicalFont.getFontName(null).equalsIgnoreCase(name) || |
|
918 |
physicalFont.getFamilyName(null).equalsIgnoreCase(name)) && |
|
919 |
physicalFont.style == style) { |
|
920 |
return physicalFont; |
|
921 |
} |
|
922 |
} |
|
923 |
return null; |
|
924 |
} |
|
925 |
||
926 |
private static PhysicalFont findDeferredFont(String name, int style) { |
|
927 |
||
928 |
PhysicalFont physicalFont = findJREDeferredFont(name, style); |
|
929 |
if (physicalFont != null) { |
|
930 |
return physicalFont; |
|
931 |
} else { |
|
932 |
return findOtherDeferredFont(name, style); |
|
933 |
} |
|
934 |
} |
|
935 |
||
936 |
public static void registerDeferredFont(String fileNameKey, |
|
937 |
String fullPathName, |
|
938 |
String[] nativeNames, |
|
939 |
int fontFormat, |
|
940 |
boolean useJavaRasterizer, |
|
941 |
int fontRank) { |
|
942 |
FontRegistrationInfo regInfo = |
|
943 |
new FontRegistrationInfo(fullPathName, nativeNames, fontFormat, |
|
944 |
useJavaRasterizer, fontRank); |
|
945 |
deferredFontFiles.put(fileNameKey, regInfo); |
|
946 |
} |
|
947 |
||
948 |
||
949 |
public static synchronized |
|
950 |
PhysicalFont initialiseDeferredFont(String fileNameKey) { |
|
951 |
||
952 |
if (fileNameKey == null) { |
|
953 |
return null; |
|
954 |
} |
|
955 |
if (logging) { |
|
956 |
logger.info("Opening deferred font file " + fileNameKey); |
|
957 |
} |
|
958 |
||
959 |
PhysicalFont physicalFont; |
|
960 |
FontRegistrationInfo regInfo = deferredFontFiles.get(fileNameKey); |
|
961 |
if (regInfo != null) { |
|
962 |
deferredFontFiles.remove(fileNameKey); |
|
963 |
physicalFont = registerFontFile(regInfo.fontFilePath, |
|
964 |
regInfo.nativeNames, |
|
965 |
regInfo.fontFormat, |
|
966 |
regInfo.javaRasterizer, |
|
967 |
regInfo.fontRank); |
|
968 |
||
969 |
||
970 |
if (physicalFont != null) { |
|
971 |
/* Store the handle, so that if a font is bad, we |
|
972 |
* retrieve the substituted font. |
|
973 |
*/ |
|
974 |
initialisedFonts.put(fileNameKey, physicalFont.handle); |
|
975 |
} else { |
|
976 |
initialisedFonts.put(fileNameKey, |
|
977 |
getDefaultPhysicalFont().handle); |
|
978 |
} |
|
979 |
} else { |
|
980 |
Font2DHandle handle = initialisedFonts.get(fileNameKey); |
|
981 |
if (handle == null) { |
|
982 |
/* Probably shouldn't happen, but just in case */ |
|
983 |
physicalFont = getDefaultPhysicalFont(); |
|
984 |
} else { |
|
985 |
physicalFont = (PhysicalFont)(handle.font2D); |
|
986 |
} |
|
987 |
} |
|
988 |
return physicalFont; |
|
989 |
} |
|
990 |
||
991 |
/* Note that the return value from this method is not always |
|
992 |
* derived from this file, and may be null. See addToFontList for |
|
993 |
* some explanation of this. |
|
994 |
*/ |
|
995 |
public static PhysicalFont registerFontFile(String fileName, |
|
996 |
String[] nativeNames, |
|
997 |
int fontFormat, |
|
998 |
boolean useJavaRasterizer, |
|
999 |
int fontRank) { |
|
1000 |
||
1001 |
PhysicalFont regFont = registeredFontFiles.get(fileName); |
|
1002 |
if (regFont != null) { |
|
1003 |
return regFont; |
|
1004 |
} |
|
1005 |
||
1006 |
PhysicalFont physicalFont = null; |
|
1007 |
try { |
|
1008 |
String name; |
|
1009 |
||
1010 |
switch (fontFormat) { |
|
1011 |
||
1012 |
case FontManager.FONTFORMAT_TRUETYPE: |
|
1013 |
int fn = 0; |
|
1014 |
TrueTypeFont ttf; |
|
1015 |
do { |
|
1016 |
ttf = new TrueTypeFont(fileName, nativeNames, fn++, |
|
1017 |
useJavaRasterizer); |
|
1018 |
PhysicalFont pf = addToFontList(ttf, fontRank); |
|
1019 |
if (physicalFont == null) { |
|
1020 |
physicalFont = pf; |
|
1021 |
} |
|
1022 |
} |
|
1023 |
while (fn < ttf.getFontCount()); |
|
1024 |
break; |
|
1025 |
||
1026 |
case FontManager.FONTFORMAT_TYPE1: |
|
1027 |
Type1Font t1f = new Type1Font(fileName, nativeNames); |
|
1028 |
physicalFont = addToFontList(t1f, fontRank); |
|
1029 |
break; |
|
1030 |
||
1031 |
case FontManager.FONTFORMAT_NATIVE: |
|
1032 |
NativeFont nf = new NativeFont(fileName, false); |
|
1033 |
physicalFont = addToFontList(nf, fontRank); |
|
1034 |
default: |
|
1035 |
||
1036 |
} |
|
1037 |
if (logging) { |
|
1038 |
logger.info("Registered file " + fileName + " as font " + |
|
1039 |
physicalFont + " rank=" + fontRank); |
|
1040 |
} |
|
1041 |
} catch (FontFormatException ffe) { |
|
1042 |
if (logging) { |
|
1043 |
logger.warning("Unusable font: " + |
|
1044 |
fileName + " " + ffe.toString()); |
|
1045 |
} |
|
1046 |
} |
|
1047 |
if (physicalFont != null && |
|
1048 |
fontFormat != FontManager.FONTFORMAT_NATIVE) { |
|
1049 |
registeredFontFiles.put(fileName, physicalFont); |
|
1050 |
} |
|
1051 |
return physicalFont; |
|
1052 |
} |
|
1053 |
||
1054 |
public static void registerFonts(String[] fileNames, |
|
1055 |
String[][] nativeNames, |
|
1056 |
int fontCount, |
|
1057 |
int fontFormat, |
|
1058 |
boolean useJavaRasterizer, |
|
1059 |
int fontRank, boolean defer) { |
|
1060 |
||
1061 |
for (int i=0; i < fontCount; i++) { |
|
1062 |
if (defer) { |
|
1063 |
registerDeferredFont(fileNames[i],fileNames[i], nativeNames[i], |
|
1064 |
fontFormat, useJavaRasterizer, fontRank); |
|
1065 |
} else { |
|
1066 |
registerFontFile(fileNames[i], nativeNames[i], |
|
1067 |
fontFormat, useJavaRasterizer, fontRank); |
|
1068 |
} |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
/* |
|
1073 |
* This is the Physical font used when some other font on the system |
|
1074 |
* can't be located. There has to be at least one font or the font |
|
1075 |
* system is not useful and the graphics environment cannot sustain |
|
1076 |
* the Java platform. |
|
1077 |
*/ |
|
1078 |
public static PhysicalFont getDefaultPhysicalFont() { |
|
1079 |
if (defaultPhysicalFont == null) { |
|
1080 |
/* findFont2D will load all fonts before giving up the search. |
|
1081 |
* If the JRE Lucida isn't found (eg because the JRE fonts |
|
1082 |
* directory is missing), it could find another version of Lucida |
|
1083 |
* from the host system. This is OK because at that point we are |
|
1084 |
* trying to gracefully handle/recover from a system |
|
1085 |
* misconfiguration and this is probably a reasonable substitution. |
|
1086 |
*/ |
|
1087 |
defaultPhysicalFont = (PhysicalFont) |
|
1088 |
findFont2D("Lucida Sans Regular", Font.PLAIN, NO_FALLBACK); |
|
1089 |
if (defaultPhysicalFont == null) { |
|
1090 |
defaultPhysicalFont = (PhysicalFont) |
|
1091 |
findFont2D("Arial", Font.PLAIN, NO_FALLBACK); |
|
1092 |
} |
|
1093 |
if (defaultPhysicalFont == null) { |
|
1094 |
/* Because of the findFont2D call above, if we reach here, we |
|
1095 |
* know all fonts have already been loaded, just accept any |
|
1096 |
* match at this point. If this fails we are in real trouble |
|
1097 |
* and I don't know how to recover from there being absolutely |
|
1098 |
* no fonts anywhere on the system. |
|
1099 |
*/ |
|
1100 |
Iterator i = physicalFonts.values().iterator(); |
|
1101 |
if (i.hasNext()) { |
|
1102 |
defaultPhysicalFont = (PhysicalFont)i.next(); |
|
1103 |
} else { |
|
1104 |
throw new Error("Probable fatal error:No fonts found."); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
} |
|
1108 |
return defaultPhysicalFont; |
|
1109 |
} |
|
1110 |
||
1111 |
public static CompositeFont getDefaultLogicalFont(int style) { |
|
1112 |
return (CompositeFont)findFont2D("dialog", style, NO_FALLBACK); |
|
1113 |
} |
|
1114 |
||
1115 |
/* |
|
1116 |
* return String representation of style prepended with "." |
|
1117 |
* This is useful for performance to avoid unnecessary string operations. |
|
1118 |
*/ |
|
1119 |
private static String dotStyleStr(int num) { |
|
1120 |
switch(num){ |
|
1121 |
case Font.BOLD: |
|
1122 |
return ".bold"; |
|
1123 |
case Font.ITALIC: |
|
1124 |
return ".italic"; |
|
1125 |
case Font.ITALIC | Font.BOLD: |
|
1126 |
return ".bolditalic"; |
|
1127 |
default: |
|
1128 |
return ".plain"; |
|
1129 |
} |
|
1130 |
} |
|
1131 |
||
1132 |
static void initSGEnv() { |
|
1133 |
if (sgEnv == null) { |
|
1134 |
GraphicsEnvironment ge = |
|
1135 |
GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|
1136 |
if (ge instanceof HeadlessGraphicsEnvironment) { |
|
1137 |
HeadlessGraphicsEnvironment hgEnv = |
|
1138 |
(HeadlessGraphicsEnvironment)ge; |
|
1139 |
sgEnv = (SunGraphicsEnvironment) |
|
1140 |
hgEnv.getSunGraphicsEnvironment(); |
|
1141 |
} else { |
|
1142 |
sgEnv = (SunGraphicsEnvironment)ge; |
|
1143 |
} |
|
1144 |
} |
|
1145 |
} |
|
1146 |
||
1147 |
/* This is implemented only on windows and is called from code that |
|
1148 |
* executes only on windows. This isn't pretty but its not a precedent |
|
1149 |
* in this file. This very probably should be cleaned up at some point. |
|
1150 |
*/ |
|
1151 |
private static native void |
|
1152 |
populateFontFileNameMap(HashMap<String,String> fontToFileMap, |
|
1153 |
HashMap<String,String> fontToFamilyNameMap, |
|
1154 |
HashMap<String,ArrayList<String>> |
|
1155 |
familyToFontListMap, |
|
1156 |
Locale locale); |
|
1157 |
||
1158 |
/* Obtained from Platform APIs (windows only) |
|
1159 |
* Map from lower-case font full name to basename of font file. |
|
1160 |
* Eg "arial bold" -> ARIALBD.TTF. |
|
1161 |
* For TTC files, there is a mapping for each font in the file. |
|
1162 |
*/ |
|
1163 |
private static HashMap<String,String> fontToFileMap = null; |
|
1164 |
||
1165 |
/* Obtained from Platform APIs (windows only) |
|
1166 |
* Map from lower-case font full name to the name of its font family |
|
1167 |
* Eg "arial bold" -> "Arial" |
|
1168 |
*/ |
|
1169 |
private static HashMap<String,String> fontToFamilyNameMap = null; |
|
1170 |
||
1171 |
/* Obtained from Platform APIs (windows only) |
|
1172 |
* Map from a lower-case family name to a list of full names of |
|
1173 |
* the member fonts, eg: |
|
1174 |
* "arial" -> ["Arial", "Arial Bold", "Arial Italic","Arial Bold Italic"] |
|
1175 |
*/ |
|
1176 |
private static HashMap<String,ArrayList<String>> familyToFontListMap= null; |
|
1177 |
||
1178 |
/* The directories which contain platform fonts */ |
|
1179 |
private static String[] pathDirs = null; |
|
1180 |
||
1181 |
private static boolean haveCheckedUnreferencedFontFiles; |
|
1182 |
||
1183 |
private static String[] getFontFilesFromPath(boolean noType1) { |
|
1184 |
final FilenameFilter filter; |
|
1185 |
if (noType1) { |
|
1186 |
filter = SunGraphicsEnvironment.ttFilter; |
|
1187 |
} else { |
|
1188 |
filter = new SunGraphicsEnvironment.TTorT1Filter(); |
|
1189 |
} |
|
1190 |
return (String[])AccessController.doPrivileged(new PrivilegedAction() { |
|
1191 |
public Object run() { |
|
1192 |
if (pathDirs.length == 1) { |
|
1193 |
File dir = new File(pathDirs[0]); |
|
1194 |
String[] files = dir.list(filter); |
|
1195 |
if (files == null) { |
|
1196 |
return new String[0]; |
|
1197 |
} |
|
1198 |
for (int f=0; f<files.length; f++) { |
|
1199 |
files[f] = files[f].toLowerCase(); |
|
1200 |
} |
|
1201 |
return files; |
|
1202 |
} else { |
|
1203 |
ArrayList<String> fileList = new ArrayList<String>(); |
|
1204 |
for (int i = 0; i< pathDirs.length; i++) { |
|
1205 |
File dir = new File(pathDirs[i]); |
|
1206 |
String[] files = dir.list(filter); |
|
1207 |
if (files == null) { |
|
1208 |
continue; |
|
1209 |
} |
|
1210 |
for (int f=0; f<files.length ; f++) { |
|
1211 |
fileList.add(files[f].toLowerCase()); |
|
1212 |
} |
|
1213 |
} |
|
1214 |
return fileList.toArray(STR_ARRAY); |
|
1215 |
} |
|
1216 |
} |
|
1217 |
}); |
|
1218 |
} |
|
1219 |
||
1220 |
/* This is needed since some windows registry names don't match |
|
1221 |
* the font names. |
|
1222 |
* - UPC styled font names have a double space, but the |
|
1223 |
* registry entry mapping to a file doesn't. |
|
1224 |
* - Marlett is in a hidden file not listed in the registry |
|
1225 |
* - The registry advertises that the file david.ttf contains a |
|
1226 |
* font with the full name "David Regular" when in fact its |
|
1227 |
* just "David". |
|
1228 |
* Directly fix up these known cases as this is faster. |
|
1229 |
* If a font which doesn't match these known cases has no file, |
|
1230 |
* it may be a font that has been temporarily added to the known set |
|
1231 |
* or it may be an installed font with a missing registry entry. |
|
1232 |
* Installed fonts are those in the windows font directories. |
|
1233 |
* Make a best effort attempt to locate these. |
|
1234 |
* We obtain the list of TrueType fonts in these directories and |
|
1235 |
* filter out all the font files we already know about from the registry. |
|
1236 |
* What remains may be "bad" fonts, duplicate fonts, or perhaps the |
|
1237 |
* missing font(s) we are looking for. |
|
1238 |
* Open each of these files to find out. |
|
1239 |
*/ |
|
1240 |
private static void resolveWindowsFonts() { |
|
1241 |
||
1242 |
ArrayList<String> unmappedFontNames = null; |
|
1243 |
for (String font : fontToFamilyNameMap.keySet()) { |
|
1244 |
String file = fontToFileMap.get(font); |
|
1245 |
if (file == null) { |
|
1246 |
if (font.indexOf(" ") > 0) { |
|
1247 |
String newName = font.replaceFirst(" ", " "); |
|
1248 |
file = fontToFileMap.get(newName); |
|
1249 |
/* If this name exists and isn't for a valid name |
|
1250 |
* replace the mapping to the file with this font |
|
1251 |
*/ |
|
1252 |
if (file != null && |
|
1253 |
!fontToFamilyNameMap.containsKey(newName)) { |
|
1254 |
fontToFileMap.remove(newName); |
|
1255 |
fontToFileMap.put(font, file); |
|
1256 |
} |
|
1257 |
} else if (font.equals("marlett")) { |
|
1258 |
fontToFileMap.put(font, "marlett.ttf"); |
|
1259 |
} else if (font.equals("david")) { |
|
1260 |
file = fontToFileMap.get("david regular"); |
|
1261 |
if (file != null) { |
|
1262 |
fontToFileMap.remove("david regular"); |
|
1263 |
fontToFileMap.put("david", file); |
|
1264 |
} |
|
1265 |
} else { |
|
1266 |
if (unmappedFontNames == null) { |
|
1267 |
unmappedFontNames = new ArrayList<String>(); |
|
1268 |
} |
|
1269 |
unmappedFontNames.add(font); |
|
1270 |
} |
|
1271 |
} |
|
1272 |
} |
|
1273 |
||
1274 |
if (unmappedFontNames != null) { |
|
1275 |
HashSet<String> unmappedFontFiles = new HashSet<String>(); |
|
1276 |
||
1277 |
/* Every font key in fontToFileMap ought to correspond to a |
|
1278 |
* font key in fontToFamilyNameMap. Entries that don't seem |
|
1279 |
* to correspond are likely fonts that were named differently |
|
1280 |
* by GDI than in the registry. One known cause of this is when |
|
1281 |
* Windows has had its regional settings changed so that from |
|
1282 |
* GDI we get a localised (eg Chinese or Japanese) name for the |
|
1283 |
* font, but the registry retains the English version of the name |
|
1284 |
* that corresponded to the "install" locale for windows. |
|
1285 |
* Since we are in this code block because there are unmapped |
|
1286 |
* font names, we can look to find unused font->file mappings |
|
1287 |
* and then open the files to read the names. We don't generally |
|
1288 |
* want to open font files, as its a performance hit, but this |
|
1289 |
* occurs only for a small number of fonts on specific system |
|
1290 |
* configs - ie is believed that a "true" Japanese windows would |
|
1291 |
* have JA names in the registry too. |
|
1292 |
* Clone fontToFileMap and remove from the clone all keys which |
|
1293 |
* match a fontToFamilyNameMap key. What remains maps to the |
|
1294 |
* files we want to open to find the fonts GDI returned. |
|
1295 |
* A font in such a file is added to the fontToFileMap after |
|
1296 |
* checking its one of the unmappedFontNames we are looking for. |
|
1297 |
* The original name that didn't map is removed from fontToFileMap |
|
1298 |
* so essentially this "fixes up" fontToFileMap to use the same |
|
1299 |
* name as GDI. |
|
1300 |
* Also note that typically the fonts for which this occurs in |
|
1301 |
* CJK locales are TTC fonts and not all fonts in a TTC may have |
|
1302 |
* localised names. Eg MSGOTHIC.TTC contains 3 fonts and one of |
|
1303 |
* them "MS UI Gothic" has no JA name whereas the other two do. |
|
1304 |
* So not every font in these files is unmapped or new. |
|
1305 |
*/ |
|
1306 |
HashMap<String,String> ffmapCopy = |
|
1307 |
(HashMap<String,String>)(fontToFileMap.clone()); |
|
1308 |
for (String key : fontToFamilyNameMap.keySet()) { |
|
1309 |
ffmapCopy.remove(key); |
|
1310 |
} |
|
1311 |
for (String key : ffmapCopy.keySet()) { |
|
1312 |
unmappedFontFiles.add(ffmapCopy.get(key)); |
|
1313 |
fontToFileMap.remove(key); |
|
1314 |
} |
|
1315 |
||
1316 |
resolveFontFiles(unmappedFontFiles, unmappedFontNames); |
|
1317 |
||
1318 |
/* If there are still unmapped font names, this means there's |
|
1319 |
* something that wasn't in the registry. We need to get all |
|
1320 |
* the font files directly and look at the ones that weren't |
|
1321 |
* found in the registry. |
|
1322 |
*/ |
|
1323 |
if (unmappedFontNames.size() > 0) { |
|
1324 |
||
1325 |
/* getFontFilesFromPath() returns all lower case names. |
|
1326 |
* To compare we also need lower case |
|
1327 |
* versions of the names from the registry. |
|
1328 |
*/ |
|
1329 |
ArrayList<String> registryFiles = new ArrayList<String>(); |
|
1330 |
||
1331 |
for (String regFile : fontToFileMap.values()) { |
|
1332 |
registryFiles.add(regFile.toLowerCase()); |
|
1333 |
} |
|
1334 |
/* We don't look for Type1 files here as windows will |
|
1335 |
* not enumerate these, so aren't useful in reconciling |
|
1336 |
* GDI's unmapped files. We do find these later when |
|
1337 |
* we enumerate all fonts. |
|
1338 |
*/ |
|
1339 |
for (String pathFile : getFontFilesFromPath(true)) { |
|
1340 |
if (!registryFiles.contains(pathFile)) { |
|
1341 |
unmappedFontFiles.add(pathFile); |
|
1342 |
} |
|
1343 |
} |
|
1344 |
||
1345 |
resolveFontFiles(unmappedFontFiles, unmappedFontNames); |
|
1346 |
} |
|
1347 |
||
1348 |
/* remove from the set of names that will be returned to the |
|
1349 |
* user any fonts that can't be mapped to files. |
|
1350 |
*/ |
|
1351 |
if (unmappedFontNames.size() > 0) { |
|
1352 |
int sz = unmappedFontNames.size(); |
|
1353 |
for (int i=0; i<sz; i++) { |
|
1354 |
String name = unmappedFontNames.get(i); |
|
1355 |
String familyName = fontToFamilyNameMap.get(name); |
|
1356 |
if (familyName != null) { |
|
1357 |
ArrayList family = familyToFontListMap.get(familyName); |
|
1358 |
if (family != null) { |
|
1359 |
if (family.size() <= 1) { |
|
1360 |
familyToFontListMap.remove(familyName); |
|
1361 |
} |
|
1362 |
} |
|
1363 |
} |
|
1364 |
fontToFamilyNameMap.remove(name); |
|
1365 |
if (logging) { |
|
1366 |
logger.info("No file for font:" + name); |
|
1367 |
} |
|
1368 |
} |
|
1369 |
} |
|
1370 |
} |
|
1371 |
} |
|
1372 |
||
1373 |
/** |
|
1374 |
* In some cases windows may have fonts in the fonts folder that |
|
1375 |
* don't show up in the registry or in the GDI calls to enumerate fonts. |
|
1376 |
* The only way to find these is to list the directory. We invoke this |
|
1377 |
* only in getAllFonts/Families, so most searches for a specific |
|
1378 |
* font that is satisfied by the GDI/registry calls don't take the |
|
1379 |
* additional hit of listing the directory. This hit is small enough |
|
1380 |
* that its not significant in these 'enumerate all the fonts' cases. |
|
1381 |
* The basic approach is to cross-reference the files windows found |
|
1382 |
* with the ones in the directory listing approach, and for each |
|
1383 |
* in the latter list that is missing from the former list, register it. |
|
1384 |
*/ |
|
1385 |
private static synchronized void checkForUnreferencedFontFiles() { |
|
1386 |
if (haveCheckedUnreferencedFontFiles) { |
|
1387 |
return; |
|
1388 |
} |
|
1389 |
haveCheckedUnreferencedFontFiles = true; |
|
1390 |
if (!isWindows) { |
|
1391 |
return; |
|
1392 |
} |
|
1393 |
/* getFontFilesFromPath() returns all lower case names. |
|
1394 |
* To compare we also need lower case |
|
1395 |
* versions of the names from the registry. |
|
1396 |
*/ |
|
1397 |
ArrayList<String> registryFiles = new ArrayList<String>(); |
|
1398 |
for (String regFile : fontToFileMap.values()) { |
|
1399 |
registryFiles.add(regFile.toLowerCase()); |
|
1400 |
} |
|
1401 |
||
1402 |
/* To avoid any issues with concurrent modification, create |
|
1403 |
* copies of the existing maps, add the new fonts into these |
|
1404 |
* and then replace the references to the old ones with the |
|
1405 |
* new maps. ConcurrentHashmap is another option but its a lot |
|
1406 |
* more changes and with this exception, these maps are intended |
|
1407 |
* to be static. |
|
1408 |
*/ |
|
1409 |
HashMap<String,String> fontToFileMap2 = null; |
|
1410 |
HashMap<String,String> fontToFamilyNameMap2 = null; |
|
1411 |
HashMap<String,ArrayList<String>> familyToFontListMap2 = null;; |
|
1412 |
||
1413 |
for (String pathFile : getFontFilesFromPath(false)) { |
|
1414 |
if (!registryFiles.contains(pathFile)) { |
|
1415 |
if (logging) { |
|
1416 |
logger.info("Found non-registry file : " + pathFile); |
|
1417 |
} |
|
1418 |
PhysicalFont f = registerFontFile(getPathName(pathFile)); |
|
1419 |
if (f == null) { |
|
1420 |
continue; |
|
1421 |
} |
|
1422 |
if (fontToFileMap2 == null) { |
|
1423 |
fontToFileMap2 = new HashMap<String,String>(fontToFileMap); |
|
1424 |
fontToFamilyNameMap2 = |
|
1425 |
new HashMap<String,String>(fontToFamilyNameMap); |
|
1426 |
familyToFontListMap2 = new |
|
1427 |
HashMap<String,ArrayList<String>>(familyToFontListMap); |
|
1428 |
} |
|
1429 |
String fontName = f.getFontName(null); |
|
1430 |
String family = f.getFamilyName(null); |
|
1431 |
String familyLC = family.toLowerCase(); |
|
1432 |
fontToFamilyNameMap2.put(fontName, family); |
|
1433 |
fontToFileMap2.put(fontName, pathFile); |
|
1434 |
ArrayList<String> fonts = familyToFontListMap2.get(familyLC); |
|
1435 |
if (fonts == null) { |
|
1436 |
fonts = new ArrayList<String>(); |
|
1437 |
} else { |
|
1438 |
fonts = new ArrayList<String>(fonts); |
|
1439 |
} |
|
1440 |
fonts.add(fontName); |
|
1441 |
familyToFontListMap2.put(familyLC, fonts); |
|
1442 |
} |
|
1443 |
} |
|
1444 |
if (fontToFileMap2 != null) { |
|
1445 |
fontToFileMap = fontToFileMap2; |
|
1446 |
familyToFontListMap = familyToFontListMap2; |
|
1447 |
fontToFamilyNameMap = fontToFamilyNameMap2; |
|
1448 |
} |
|
1449 |
} |
|
1450 |
||
1451 |
private static void resolveFontFiles(HashSet<String> unmappedFiles, |
|
1452 |
ArrayList<String> unmappedFonts) { |
|
1453 |
||
1454 |
Locale l = SunToolkit.getStartupLocale(); |
|
1455 |
||
1456 |
for (String file : unmappedFiles) { |
|
1457 |
try { |
|
1458 |
int fn = 0; |
|
1459 |
TrueTypeFont ttf; |
|
1460 |
String fullPath = getPathName(file); |
|
1461 |
if (logging) { |
|
1462 |
logger.info("Trying to resolve file " + fullPath); |
|
1463 |
} |
|
1464 |
do { |
|
1465 |
ttf = new TrueTypeFont(fullPath, null, fn++, true); |
|
1466 |
// prefer the font's locale name. |
|
1467 |
String fontName = ttf.getFontName(l).toLowerCase(); |
|
1468 |
if (unmappedFonts.contains(fontName)) { |
|
1469 |
fontToFileMap.put(fontName, file); |
|
1470 |
unmappedFonts.remove(fontName); |
|
1471 |
if (logging) { |
|
1472 |
logger.info("Resolved absent registry entry for " + |
|
1473 |
fontName + " located in " + fullPath); |
|
1474 |
} |
|
1475 |
} |
|
1476 |
} |
|
1477 |
while (fn < ttf.getFontCount()); |
|
1478 |
} catch (Exception e) { |
|
1479 |
} |
|
1480 |
} |
|
1481 |
} |
|
1482 |
||
1483 |
private static synchronized HashMap<String,String> getFullNameToFileMap() { |
|
1484 |
if (fontToFileMap == null) { |
|
1485 |
||
1486 |
initSGEnv(); |
|
1487 |
pathDirs = sgEnv.getPlatformFontDirs(); |
|
1488 |
||
1489 |
fontToFileMap = new HashMap<String,String>(100); |
|
1490 |
fontToFamilyNameMap = new HashMap<String,String>(100); |
|
1491 |
familyToFontListMap = new HashMap<String,ArrayList<String>>(50); |
|
1492 |
populateFontFileNameMap(fontToFileMap, |
|
1493 |
fontToFamilyNameMap, |
|
1494 |
familyToFontListMap, |
|
1495 |
Locale.ENGLISH); |
|
1496 |
if (isWindows) { |
|
1497 |
resolveWindowsFonts(); |
|
1498 |
} |
|
1499 |
if (logging) { |
|
1500 |
logPlatformFontInfo(); |
|
1501 |
} |
|
1502 |
} |
|
1503 |
return fontToFileMap; |
|
1504 |
} |
|
1505 |
||
1506 |
private static void logPlatformFontInfo() { |
|
1507 |
for (int i=0; i< pathDirs.length;i++) { |
|
1508 |
logger.info("fontdir="+pathDirs[i]); |
|
1509 |
} |
|
1510 |
for (String keyName : fontToFileMap.keySet()) { |
|
1511 |
logger.info("font="+keyName+" file="+ fontToFileMap.get(keyName)); |
|
1512 |
} |
|
1513 |
for (String keyName : fontToFamilyNameMap.keySet()) { |
|
1514 |
logger.info("font="+keyName+" family="+ |
|
1515 |
fontToFamilyNameMap.get(keyName)); |
|
1516 |
} |
|
1517 |
for (String keyName : familyToFontListMap.keySet()) { |
|
1518 |
logger.info("family="+keyName+ " fonts="+ |
|
1519 |
familyToFontListMap.get(keyName)); |
|
1520 |
} |
|
1521 |
} |
|
1522 |
||
1523 |
/* Note this return list excludes logical fonts and JRE fonts */ |
|
1524 |
public static String[] getFontNamesFromPlatform() { |
|
1525 |
if (getFullNameToFileMap().size() == 0) { |
|
1526 |
return null; |
|
1527 |
} |
|
1528 |
checkForUnreferencedFontFiles(); |
|
1529 |
/* This odd code with TreeMap is used to preserve a historical |
|
1530 |
* behaviour wrt the sorting order .. */ |
|
1531 |
ArrayList<String> fontNames = new ArrayList<String>(); |
|
1532 |
for (ArrayList<String> a : familyToFontListMap.values()) { |
|
1533 |
for (String s : a) { |
|
1534 |
fontNames.add(s); |
|
1535 |
} |
|
1536 |
} |
|
1537 |
return fontNames.toArray(STR_ARRAY); |
|
1538 |
} |
|
1539 |
||
1540 |
public static boolean gotFontsFromPlatform() { |
|
1541 |
return getFullNameToFileMap().size() != 0; |
|
1542 |
} |
|
1543 |
||
1544 |
public static String getFileNameForFontName(String fontName) { |
|
1545 |
String fontNameLC = fontName.toLowerCase(Locale.ENGLISH); |
|
1546 |
return fontToFileMap.get(fontNameLC); |
|
1547 |
} |
|
1548 |
||
1549 |
private static PhysicalFont registerFontFile(String file) { |
|
1550 |
if (new File(file).isAbsolute() && |
|
1551 |
!registeredFontFiles.contains(file)) { |
|
1552 |
int fontFormat = FONTFORMAT_NONE; |
|
1553 |
int fontRank = Font2D.UNKNOWN_RANK; |
|
1554 |
if (SunGraphicsEnvironment.ttFilter.accept(null, file)) { |
|
1555 |
fontFormat = FONTFORMAT_TRUETYPE; |
|
1556 |
fontRank = Font2D.TTF_RANK; |
|
1557 |
} else if |
|
1558 |
(SunGraphicsEnvironment.t1Filter.accept(null, file)) { |
|
1559 |
fontFormat = FONTFORMAT_TYPE1; |
|
1560 |
fontRank = Font2D.TYPE1_RANK; |
|
1561 |
} |
|
1562 |
if (fontFormat == FONTFORMAT_NONE) { |
|
1563 |
return null; |
|
1564 |
} |
|
1565 |
return registerFontFile(file, null, fontFormat, false, fontRank); |
|
1566 |
} |
|
1567 |
return null; |
|
1568 |
} |
|
1569 |
||
1570 |
/* Used to register any font files that are found by platform APIs |
|
1571 |
* that weren't previously found in the standard font locations. |
|
1572 |
* the isAbsolute() check is needed since that's whats stored in the |
|
1573 |
* set, and on windows, the fonts in the system font directory that |
|
1574 |
* are in the fontToFileMap are just basenames. We don't want to try |
|
1575 |
* to register those again, but we do want to register other registry |
|
1576 |
* installed fonts. |
|
1577 |
*/ |
|
1578 |
public static void registerOtherFontFiles(HashSet registeredFontFiles) { |
|
1579 |
if (getFullNameToFileMap().size() == 0) { |
|
1580 |
return; |
|
1581 |
} |
|
1582 |
for (String file : fontToFileMap.values()) { |
|
1583 |
registerFontFile(file); |
|
1584 |
} |
|
1585 |
} |
|
1586 |
||
1587 |
public static boolean |
|
1588 |
getFamilyNamesFromPlatform(TreeMap<String,String> familyNames, |
|
1589 |
Locale requestedLocale) { |
|
1590 |
if (getFullNameToFileMap().size() == 0) { |
|
1591 |
return false; |
|
1592 |
} |
|
1593 |
checkForUnreferencedFontFiles(); |
|
1594 |
for (String name : fontToFamilyNameMap.values()) { |
|
1595 |
familyNames.put(name.toLowerCase(requestedLocale), name); |
|
1596 |
} |
|
1597 |
return true; |
|
1598 |
} |
|
1599 |
||
1600 |
/* Path may be absolute or a base file name relative to one of |
|
1601 |
* the platform font directories |
|
1602 |
*/ |
|
1603 |
private static String getPathName(String s) { |
|
1604 |
File f = new File(s); |
|
1605 |
if (f.isAbsolute()) { |
|
1606 |
return s; |
|
1607 |
} else if (pathDirs.length==1) { |
|
1608 |
return pathDirs[0] + File.separator + s; |
|
1609 |
} else { |
|
1610 |
for (int p=0; p<pathDirs.length; p++) { |
|
1611 |
f = new File(pathDirs[p] + File.separator + s); |
|
1612 |
if (f.exists()) { |
|
1613 |
return f.getAbsolutePath(); |
|
1614 |
} |
|
1615 |
} |
|
1616 |
} |
|
1617 |
return s; // shouldn't happen, but harmless |
|
1618 |
} |
|
1619 |
||
1620 |
/* lcName is required to be lower case for use as a key. |
|
1621 |
* lcName may be a full name, or a family name, and style may |
|
1622 |
* be specified in addition to either of these. So be sure to |
|
1623 |
* get the right one. Since an app *could* ask for "Foo Regular" |
|
1624 |
* and later ask for "Foo Italic", if we don't register all the |
|
1625 |
* styles, then logic in findFont2D may try to style the original |
|
1626 |
* so we register the entire family if we get a match here. |
|
1627 |
* This is still a big win because this code is invoked where |
|
1628 |
* otherwise we would register all fonts. |
|
1629 |
* It's also useful for the case where "Foo Bold" was specified with |
|
1630 |
* style Font.ITALIC, as we would want in that case to try to return |
|
1631 |
* "Foo Bold Italic" if it exists, and it is only by locating "Foo Bold" |
|
1632 |
* and opening it that we really "know" it's Bold, and can look for |
|
1633 |
* a font that supports that and the italic style. |
|
1634 |
* The code in here is not overtly windows-specific but in fact it |
|
1635 |
* is unlikely to be useful as is on other platforms. It is maintained |
|
1636 |
* in this shared source file to be close to its sole client and |
|
1637 |
* because so much of the logic is intertwined with the logic in |
|
1638 |
* findFont2D. |
|
1639 |
*/ |
|
1640 |
private static Font2D findFontFromPlatform(String lcName, int style) { |
|
1641 |
if (getFullNameToFileMap().size() == 0) { |
|
1642 |
return null; |
|
1643 |
} |
|
1644 |
||
1645 |
ArrayList<String> family = null; |
|
1646 |
String fontFile = null; |
|
1647 |
String familyName = fontToFamilyNameMap.get(lcName); |
|
1648 |
if (familyName != null) { |
|
1649 |
fontFile = fontToFileMap.get(lcName); |
|
1650 |
family = familyToFontListMap.get |
|
1651 |
(familyName.toLowerCase(Locale.ENGLISH)); |
|
1652 |
} else { |
|
1653 |
family = familyToFontListMap.get(lcName); // is lcName is a family? |
|
1654 |
if (family != null && family.size() > 0) { |
|
1655 |
String lcFontName = family.get(0).toLowerCase(Locale.ENGLISH); |
|
1656 |
if (lcFontName != null) { |
|
1657 |
familyName = fontToFamilyNameMap.get(lcFontName); |
|
1658 |
} |
|
1659 |
} |
|
1660 |
} |
|
1661 |
if (family == null || familyName == null) { |
|
1662 |
return null; |
|
1663 |
} |
|
1664 |
String [] fontList = (String[])family.toArray(STR_ARRAY); |
|
1665 |
if (fontList.length == 0) { |
|
1666 |
return null; |
|
1667 |
} |
|
1668 |
||
1669 |
/* first check that for every font in this family we can find |
|
1670 |
* a font file. The specific reason for doing this is that |
|
1671 |
* in at least one case on Windows a font has the face name "David" |
|
1672 |
* but the registry entry is "David Regular". That is the "unique" |
|
1673 |
* name of the font but in other cases the registry contains the |
|
1674 |
* "full" name. See the specifications of name ids 3 and 4 in the |
|
1675 |
* TrueType 'name' table. |
|
1676 |
* In general this could cause a problem that we fail to register |
|
1677 |
* if we all members of a family that we may end up mapping to |
|
1678 |
* the wrong font member: eg return Bold when Plain is needed. |
|
1679 |
*/ |
|
1680 |
for (int f=0;f<fontList.length;f++) { |
|
1681 |
String fontNameLC = fontList[f].toLowerCase(Locale.ENGLISH); |
|
1682 |
String fileName = fontToFileMap.get(fontNameLC); |
|
1683 |
if (fileName == null) { |
|
1684 |
if (logging) { |
|
1685 |
logger.info("Platform lookup : No file for font " + |
|
1686 |
fontList[f] + " in family " +familyName); |
|
1687 |
} |
|
1688 |
return null; |
|
1689 |
} |
|
1690 |
} |
|
1691 |
||
1692 |
/* Currently this code only looks for TrueType fonts, so format |
|
1693 |
* and rank can be specified without looking at the filename. |
|
1694 |
*/ |
|
1695 |
PhysicalFont physicalFont = null; |
|
1696 |
if (fontFile != null) { |
|
1697 |
physicalFont = registerFontFile(getPathName(fontFile), null, |
|
1698 |
FONTFORMAT_TRUETYPE, false, |
|
1699 |
Font2D.TTF_RANK); |
|
1700 |
} |
|
1701 |
/* Register all fonts in this family. */ |
|
1702 |
for (int f=0;f<fontList.length;f++) { |
|
1703 |
String fontNameLC = fontList[f].toLowerCase(Locale.ENGLISH); |
|
1704 |
String fileName = fontToFileMap.get(fontNameLC); |
|
1705 |
if (fontFile != null && fontFile.equals(fileName)) { |
|
1706 |
continue; |
|
1707 |
} |
|
1708 |
/* Currently this code only looks for TrueType fonts, so format |
|
1709 |
* and rank can be specified without looking at the filename. |
|
1710 |
*/ |
|
1711 |
registerFontFile(getPathName(fileName), null, |
|
1712 |
FONTFORMAT_TRUETYPE, false, Font2D.TTF_RANK); |
|
1713 |
} |
|
1714 |
||
1715 |
Font2D font = null; |
|
1716 |
FontFamily fontFamily = FontFamily.getFamily(familyName); |
|
1717 |
/* Handle case where request "MyFont Bold", style=Font.ITALIC */ |
|
1718 |
if (physicalFont != null) { |
|
1719 |
style |= physicalFont.style; |
|
1720 |
} |
|
1721 |
if (fontFamily != null) { |
|
1722 |
font = fontFamily.getFont(style); |
|
1723 |
if (font == null) { |
|
1724 |
font = fontFamily.getClosestStyle(style); |
|
1725 |
} |
|
1726 |
} |
|
1727 |
return font; |
|
1728 |
} |
|
1729 |
||
1730 |
private static ConcurrentHashMap<String, Font2D> fontNameCache = |
|
1731 |
new ConcurrentHashMap<String, Font2D>(); |
|
1732 |
||
1733 |
/* |
|
1734 |
* The client supplies a name and a style. |
|
1735 |
* The name could be a family name, or a full name. |
|
1736 |
* A font may exist with the specified style, or it may |
|
1737 |
* exist only in some other style. For non-native fonts the scaler |
|
1738 |
* may be able to emulate the required style. |
|
1739 |
*/ |
|
1740 |
public static Font2D findFont2D(String name, int style, int fallback) { |
|
1741 |
String lowerCaseName = name.toLowerCase(Locale.ENGLISH); |
|
1742 |
String mapName = lowerCaseName + dotStyleStr(style); |
|
1743 |
Font2D font; |
|
1744 |
||
1745 |
/* If preferLocaleFonts() or preferProportionalFonts() has been |
|
1746 |
* called we may be using an alternate set of composite fonts in this |
|
1747 |
* app context. The presence of a pre-built name map indicates whether |
|
1748 |
* this is so, and gives access to the alternate composite for the |
|
1749 |
* name. |
|
1750 |
*/ |
|
1751 |
if (usingPerAppContextComposites) { |
|
1752 |
ConcurrentHashMap<String, Font2D> altNameCache = |
|
1753 |
(ConcurrentHashMap<String, Font2D>) |
|
1754 |
AppContext.getAppContext().get(CompositeFont.class); |
|
1755 |
if (altNameCache != null) { |
|
1756 |
font = (Font2D)altNameCache.get(mapName); |
|
1757 |
} else { |
|
1758 |
font = null; |
|
1759 |
} |
|
1760 |
} else { |
|
1761 |
font = fontNameCache.get(mapName); |
|
1762 |
} |
|
1763 |
if (font != null) { |
|
1764 |
return font; |
|
1765 |
} |
|
1766 |
||
1767 |
if (logging) { |
|
1768 |
logger.info("Search for font: " + name); |
|
1769 |
} |
|
1770 |
||
1771 |
// The check below is just so that the bitmap fonts being set by |
|
1772 |
// AWT and Swing thru the desktop properties do not trigger the |
|
1773 |
// the load fonts case. The two bitmap fonts are now mapped to |
|
1774 |
// appropriate equivalents for serif and sansserif. |
|
1775 |
// Note that the cost of this comparison is only for the first |
|
1776 |
// call until the map is filled. |
|
1777 |
if (isWindows) { |
|
1778 |
if (lowerCaseName.equals("ms sans serif")) { |
|
1779 |
name = "sansserif"; |
|
1780 |
} else if (lowerCaseName.equals("ms serif")) { |
|
1781 |
name = "serif"; |
|
1782 |
} |
|
1783 |
} |
|
1784 |
||
1785 |
/* This isn't intended to support a client passing in the |
|
1786 |
* string default, but if a client passes in null for the name |
|
1787 |
* the java.awt.Font class internally substitutes this name. |
|
1788 |
* So we need to recognise it here to prevent a loadFonts |
|
1789 |
* on the unrecognised name. The only potential problem with |
|
1790 |
* this is it would hide any real font called "default"! |
|
1791 |
* But that seems like a potential problem we can ignore for now. |
|
1792 |
*/ |
|
1793 |
if (lowerCaseName.equals("default")) { |
|
1794 |
name = "dialog"; |
|
1795 |
} |
|
1796 |
||
1797 |
/* First see if its a family name. */ |
|
1798 |
FontFamily family = FontFamily.getFamily(name); |
|
1799 |
if (family != null) { |
|
1800 |
font = family.getFontWithExactStyleMatch(style); |
|
1801 |
if (font == null) { |
|
1802 |
font = findDeferredFont(name, style); |
|
1803 |
} |
|
1804 |
if (font == null) { |
|
1805 |
font = family.getFont(style); |
|
1806 |
} |
|
1807 |
if (font == null) { |
|
1808 |
font = family.getClosestStyle(style); |
|
1809 |
} |
|
1810 |
if (font != null) { |
|
1811 |
fontNameCache.put(mapName, font); |
|
1812 |
return font; |
|
1813 |
} |
|
1814 |
} |
|
1815 |
||
1816 |
/* If it wasn't a family name, it should be a full name of |
|
1817 |
* either a composite, or a physical font |
|
1818 |
*/ |
|
1819 |
font = fullNameToFont.get(lowerCaseName); |
|
1820 |
if (font != null) { |
|
1821 |
/* Check that the requested style matches the matched font's style. |
|
1822 |
* But also match style automatically if the requested style is |
|
1823 |
* "plain". This because the existing behaviour is that the fonts |
|
1824 |
* listed via getAllFonts etc always list their style as PLAIN. |
|
1825 |
* This does lead to non-commutative behaviours where you might |
|
1826 |
* start with "Lucida Sans Regular" and ask for a BOLD version |
|
1827 |
* and get "Lucida Sans DemiBold" but if you ask for the PLAIN |
|
1828 |
* style of "Lucida Sans DemiBold" you get "Lucida Sans DemiBold". |
|
1829 |
* This consistent however with what happens if you have a bold |
|
1830 |
* version of a font and no plain version exists - alg. styling |
|
1831 |
* doesn't "unbolden" the font. |
|
1832 |
*/ |
|
1833 |
if (font.style == style || style == Font.PLAIN) { |
|
1834 |
fontNameCache.put(mapName, font); |
|
1835 |
return font; |
|
1836 |
} else { |
|
1837 |
/* If it was a full name like "Lucida Sans Regular", but |
|
1838 |
* the style requested is "bold", then we want to see if |
|
1839 |
* there's the appropriate match against another font in |
|
1840 |
* that family before trying to load all fonts, or applying a |
|
1841 |
* algorithmic styling |
|
1842 |
*/ |
|
1843 |
family = FontFamily.getFamily(font.getFamilyName(null)); |
|
1844 |
if (family != null) { |
|
1845 |
Font2D familyFont = family.getFont(style|font.style); |
|
1846 |
/* We exactly matched the requested style, use it! */ |
|
1847 |
if (familyFont != null) { |
|
1848 |
fontNameCache.put(mapName, familyFont); |
|
1849 |
return familyFont; |
|
1850 |
} else { |
|
1851 |
/* This next call is designed to support the case |
|
1852 |
* where bold italic is requested, and if we must |
|
1853 |
* style, then base it on either bold or italic - |
|
1854 |
* not on plain! |
|
1855 |
*/ |
|
1856 |
familyFont = family.getClosestStyle(style|font.style); |
|
1857 |
if (familyFont != null) { |
|
1858 |
/* The next check is perhaps one |
|
1859 |
* that shouldn't be done. ie if we get this |
|
1860 |
* far we have probably as close a match as we |
|
1861 |
* are going to get. We could load all fonts to |
|
1862 |
* see if somehow some parts of the family are |
|
1863 |
* loaded but not all of it. |
|
1864 |
*/ |
|
1865 |
if (familyFont.canDoStyle(style|font.style)) { |
|
1866 |
fontNameCache.put(mapName, familyFont); |
|
1867 |
return familyFont; |
|
1868 |
} |
|
1869 |
} |
|
1870 |
} |
|
1871 |
} |
|
1872 |
} |
|
1873 |
} |
|
1874 |
||
1875 |
/* If reach here its possible that this is in a client which never |
|
1876 |
* loaded the GraphicsEnvironment, so we haven't even loaded ANY of |
|
1877 |
* the fonts from the environment. Do so now and recurse. |
|
1878 |
*/ |
|
1879 |
if (sgEnv == null) { |
|
1880 |
initSGEnv(); |
|
1881 |
return findFont2D(name, style, fallback); |
|
1882 |
} |
|
1883 |
||
1884 |
if (isWindows) { |
|
1885 |
/* Don't want Windows to return a Lucida Sans font from |
|
1886 |
* C:\Windows\Fonts |
|
1887 |
*/ |
|
1888 |
if (deferredFontFiles.size() > 0) { |
|
1889 |
font = findJREDeferredFont(lowerCaseName, style); |
|
1890 |
if (font != null) { |
|
1891 |
fontNameCache.put(mapName, font); |
|
1892 |
return font; |
|
1893 |
} |
|
1894 |
} |
|
1895 |
font = findFontFromPlatform(lowerCaseName, style); |
|
1896 |
if (font != null) { |
|
1897 |
if (logging) { |
|
1898 |
logger.info("Found font via platform API for request:\"" + |
|
1899 |
name + "\":, style="+style+ |
|
1900 |
" found font: " + font); |
|
1901 |
} |
|
1902 |
fontNameCache.put(mapName, font); |
|
1903 |
return font; |
|
1904 |
} |
|
1905 |
} |
|
1906 |
||
1907 |
/* If reach here and no match has been located, then if there are |
|
1908 |
* uninitialised deferred fonts, load as many of those as needed |
|
1909 |
* to find the deferred font. If none is found through that |
|
1910 |
* search continue on. |
|
1911 |
* There is possibly a minor issue when more than one |
|
1912 |
* deferred font implements the same font face. Since deferred |
|
1913 |
* fonts are only those in font configuration files, this is a |
|
1914 |
* controlled situation, the known case being Solaris euro_fonts |
|
1915 |
* versions of Arial, Times New Roman, Courier New. However |
|
1916 |
* the larger font will transparently replace the smaller one |
|
1917 |
* - see addToFontList() - when it is needed by the composite font. |
|
1918 |
*/ |
|
1919 |
if (deferredFontFiles.size() > 0) { |
|
1920 |
font = findDeferredFont(name, style); |
|
1921 |
if (font != null) { |
|
1922 |
fontNameCache.put(mapName, font); |
|
1923 |
return font; |
|
1924 |
} |
|
1925 |
} |
|
1926 |
||
1927 |
/* Some apps use deprecated 1.0 names such as helvetica and courier. On |
|
1928 |
* Solaris these are Type1 fonts in /usr/openwin/lib/X11/fonts/Type1. |
|
1929 |
* If running on Solaris will register all the fonts in this |
|
1930 |
* directory. |
|
1931 |
* May as well register the whole directory without actually testing |
|
1932 |
* the font name is one of the deprecated names as the next step would |
|
1933 |
* load all fonts which are in this directory anyway. |
|
1934 |
* In the event that this lookup is successful it potentially "hides" |
|
1935 |
* TrueType versions of such fonts that are elsewhere but since they |
|
1936 |
* do not exist on Solaris this is not a problem. |
|
1937 |
* Set a flag to indicate we've done this registration to avoid |
|
1938 |
* repetition and more seriously, to avoid recursion. |
|
1939 |
*/ |
|
1940 |
if (isSolaris&&!loaded1dot0Fonts) { |
|
1941 |
/* "timesroman" is a special case since that's not the |
|
1942 |
* name of any known font on Solaris or elsewhere. |
|
1943 |
*/ |
|
1944 |
if (lowerCaseName.equals("timesroman")) { |
|
1945 |
font = findFont2D("serif", style, fallback); |
|
1946 |
fontNameCache.put(mapName, font); |
|
1947 |
} |
|
1948 |
sgEnv.register1dot0Fonts(); |
|
1949 |
loaded1dot0Fonts = true; |
|
1950 |
Font2D ff = findFont2D(name, style, fallback); |
|
1951 |
return ff; |
|
1952 |
} |
|
1953 |
||
1954 |
/* We check for application registered fonts before |
|
1955 |
* explicitly loading all fonts as if necessary the registration |
|
1956 |
* code will have done so anyway. And we don't want to needlessly |
|
1957 |
* load the actual files for all fonts. |
|
1958 |
* Just as for installed fonts we check for family before fullname. |
|
1959 |
* We do not add these fonts to fontNameCache for the |
|
1960 |
* app context case which eliminates the overhead of a per context |
|
1961 |
* cache for these. |
|
1962 |
*/ |
|
1963 |
||
1964 |
if (fontsAreRegistered || fontsAreRegisteredPerAppContext) { |
|
1965 |
Hashtable<String, FontFamily> familyTable = null; |
|
1966 |
Hashtable<String, Font2D> nameTable; |
|
1967 |
||
1968 |
if (fontsAreRegistered) { |
|
1969 |
familyTable = createdByFamilyName; |
|
1970 |
nameTable = createdByFullName; |
|
1971 |
} else { |
|
1972 |
AppContext appContext = AppContext.getAppContext(); |
|
1973 |
familyTable = |
|
1974 |
(Hashtable<String,FontFamily>)appContext.get(regFamilyKey); |
|
1975 |
nameTable = |
|
1976 |
(Hashtable<String,Font2D>)appContext.get(regFullNameKey); |
|
1977 |
} |
|
1978 |
||
1979 |
family = familyTable.get(lowerCaseName); |
|
1980 |
if (family != null) { |
|
1981 |
font = family.getFontWithExactStyleMatch(style); |
|
1982 |
if (font == null) { |
|
1983 |
font = family.getFont(style); |
|
1984 |
} |
|
1985 |
if (font == null) { |
|
1986 |
font = family.getClosestStyle(style); |
|
1987 |
} |
|
1988 |
if (font != null) { |
|
1989 |
if (fontsAreRegistered) { |
|
1990 |
fontNameCache.put(mapName, font); |
|
1991 |
} |
|
1992 |
return font; |
|
1993 |
} |
|
1994 |
} |
|
1995 |
font = nameTable.get(lowerCaseName); |
|
1996 |
if (font != null) { |
|
1997 |
if (fontsAreRegistered) { |
|
1998 |
fontNameCache.put(mapName, font); |
|
1999 |
} |
|
2000 |
return font; |
|
2001 |
} |
|
2002 |
} |
|
2003 |
||
2004 |
/* If reach here and no match has been located, then if all fonts |
|
2005 |
* are not yet loaded, do so, and then recurse. |
|
2006 |
*/ |
|
2007 |
if (!loadedAllFonts) { |
|
2008 |
if (logging) { |
|
2009 |
logger.info("Load fonts looking for:" + name); |
|
2010 |
} |
|
2011 |
sgEnv.loadFonts(); |
|
2012 |
loadedAllFonts = true; |
|
2013 |
return findFont2D(name, style, fallback); |
|
2014 |
} |
|
2015 |
||
2016 |
if (!loadedAllFontFiles) { |
|
2017 |
if (logging) { |
|
2018 |
logger.info("Load font files looking for:" + name); |
|
2019 |
} |
|
2020 |
sgEnv.loadFontFiles(); |
|
2021 |
loadedAllFontFiles = true; |
|
2022 |
return findFont2D(name, style, fallback); |
|
2023 |
} |
|
2024 |
||
2025 |
/* The primary name is the locale default - ie not US/English but |
|
2026 |
* whatever is the default in this locale. This is the way it always |
|
2027 |
* has been but may be surprising to some developers if "Arial Regular" |
|
2028 |
* were hard-coded in their app and yet "Arial Regular" was not the |
|
2029 |
* default name. Fortunately for them, as a consequence of the JDK |
|
2030 |
* supporting returning names and family names for arbitrary locales, |
|
2031 |
* we also need to support searching all localised names for a match. |
|
2032 |
* But because this case of the name used to reference a font is not |
|
2033 |
* the same as the default for this locale is rare, it makes sense to |
|
2034 |
* search a much shorter list of default locale names and only go to |
|
2035 |
* a longer list of names in the event that no match was found. |
|
2036 |
* So add here code which searches localised names too. |
|
2037 |
* As in 1.4.x this happens only after loading all fonts, which |
|
2038 |
* is probably the right order. |
|
2039 |
*/ |
|
2040 |
if ((font = findFont2DAllLocales(name, style)) != null) { |
|
2041 |
fontNameCache.put(mapName, font); |
|
2042 |
return font; |
|
2043 |
} |
|
2044 |
||
2045 |
/* Perhaps its a "compatibility" name - timesroman, helvetica, |
|
2046 |
* or courier, which 1.0 apps used for logical fonts. |
|
2047 |
* We look for these "late" after a loadFonts as we must not |
|
2048 |
* hide real fonts of these names. |
|
2049 |
* Map these appropriately: |
|
2050 |
* On windows this means according to the rules specified by the |
|
2051 |
* FontConfiguration : do it only for encoding==Cp1252 |
|
2052 |
* |
|
2053 |
* REMIND: this is something we plan to remove. |
|
2054 |
*/ |
|
2055 |
if (isWindows) { |
|
2056 |
String compatName = |
|
2057 |
sgEnv.getFontConfiguration().getFallbackFamilyName(name, null); |
|
2058 |
if (compatName != null) { |
|
2059 |
font = findFont2D(compatName, style, fallback); |
|
2060 |
fontNameCache.put(mapName, font); |
|
2061 |
return font; |
|
2062 |
} |
|
2063 |
} else if (lowerCaseName.equals("timesroman")) { |
|
2064 |
font = findFont2D("serif", style, fallback); |
|
2065 |
fontNameCache.put(mapName, font); |
|
2066 |
return font; |
|
2067 |
} else if (lowerCaseName.equals("helvetica")) { |
|
2068 |
font = findFont2D("sansserif", style, fallback); |
|
2069 |
fontNameCache.put(mapName, font); |
|
2070 |
return font; |
|
2071 |
} else if (lowerCaseName.equals("courier")) { |
|
2072 |
font = findFont2D("monospaced", style, fallback); |
|
2073 |
fontNameCache.put(mapName, font); |
|
2074 |
return font; |
|
2075 |
} |
|
2076 |
||
2077 |
if (logging) { |
|
2078 |
logger.info("No font found for:" + name); |
|
2079 |
} |
|
2080 |
||
2081 |
switch (fallback) { |
|
2082 |
case PHYSICAL_FALLBACK: return getDefaultPhysicalFont(); |
|
2083 |
case LOGICAL_FALLBACK: return getDefaultLogicalFont(style); |
|
2084 |
default: return null; |
|
2085 |
} |
|
2086 |
} |
|
2087 |
||
2088 |
/* This method can be more efficient as it will only need to |
|
2089 |
* do the lookup once, and subsequent calls on the java.awt.Font |
|
2090 |
* instance can utilise the cached Font2D on that object. |
|
2091 |
* Its unfortunate it needs to be a native method, but the font2D |
|
2092 |
* variable has to be private. |
|
2093 |
*/ |
|
2094 |
public static native Font2D getFont2D(Font font); |
|
2095 |
||
2096 |
/* Stuff below was in NativeFontWrapper and needed a new home */ |
|
2097 |
||
2098 |
/* |
|
2099 |
* Workaround for apps which are dependent on a font metrics bug |
|
2100 |
* in JDK 1.1. This is an unsupported win32 private setting. |
|
2101 |
*/ |
|
2102 |
public static boolean usePlatformFontMetrics() { |
|
2103 |
return usePlatformFontMetrics; |
|
2104 |
} |
|
2105 |
||
2106 |
static native boolean getPlatformFontVar(); |
|
2107 |
||
2108 |
private static final short US_LCID = 0x0409; // US English - default |
|
2109 |
private static Map<String, Short> lcidMap; |
|
2110 |
||
2111 |
// Return a Microsoft LCID from the given Locale. |
|
2112 |
// Used when getting localized font data. |
|
2113 |
||
2114 |
public static short getLCIDFromLocale(Locale locale) { |
|
2115 |
// optimize for common case |
|
2116 |
if (locale.equals(Locale.US)) { |
|
2117 |
return US_LCID; |
|
2118 |
} |
|
2119 |
||
2120 |
if (lcidMap == null) { |
|
2121 |
createLCIDMap(); |
|
2122 |
} |
|
2123 |
||
2124 |
String key = locale.toString(); |
|
2125 |
while (!"".equals(key)) { |
|
2126 |
Short lcidObject = (Short) lcidMap.get(key); |
|
2127 |
if (lcidObject != null) { |
|
2128 |
return lcidObject.shortValue(); |
|
2129 |
} |
|
2130 |
int pos = key.lastIndexOf('_'); |
|
2131 |
if (pos < 1) { |
|
2132 |
return US_LCID; |
|
2133 |
} |
|
2134 |
key = key.substring(0, pos); |
|
2135 |
} |
|
2136 |
||
2137 |
return US_LCID; |
|
2138 |
} |
|
2139 |
||
2140 |
||
2141 |
private static void addLCIDMapEntry(Map<String, Short> map, |
|
2142 |
String key, short value) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
2143 |
map.put(key, Short.valueOf(value)); |
2 | 2144 |
} |
2145 |
||
2146 |
private static synchronized void createLCIDMap() { |
|
2147 |
if (lcidMap != null) { |
|
2148 |
return; |
|
2149 |
} |
|
2150 |
||
2151 |
Map<String, Short> map = new HashMap<String, Short>(200); |
|
2152 |
||
2153 |
// the following statements are derived from the langIDMap |
|
2154 |
// in src/windows/native/java/lang/java_props_md.c using the following |
|
2155 |
// awk script: |
|
2156 |
// $1~/\/\*/ { next} |
|
2157 |
// $3~/\?\?/ { next } |
|
2158 |
// $3!~/_/ { next } |
|
2159 |
// $1~/0x0409/ { next } |
|
2160 |
// $1~/0x0c0a/ { next } |
|
2161 |
// $1~/0x042c/ { next } |
|
2162 |
// $1~/0x0443/ { next } |
|
2163 |
// $1~/0x0812/ { next } |
|
2164 |
// $1~/0x04/ { print " addLCIDMapEntry(map, " substr($3, 0, 3) "\", (short) " substr($1, 0, 6) ");" ; next } |
|
2165 |
// $3~/,/ { print " addLCIDMapEntry(map, " $3 " (short) " substr($1, 0, 6) ");" ; next } |
|
2166 |
// { print " addLCIDMapEntry(map, " $3 ", (short) " substr($1, 0, 6) ");" ; next } |
|
2167 |
// The lines of this script: |
|
2168 |
// - eliminate comments |
|
2169 |
// - eliminate questionable locales |
|
2170 |
// - eliminate language-only locales |
|
2171 |
// - eliminate the default LCID value |
|
2172 |
// - eliminate a few other unneeded LCID values |
|
2173 |
// - print language-only locale entries for x04* LCID values |
|
2174 |
// (apparently Microsoft doesn't use language-only LCID values - |
|
2175 |
// see http://www.microsoft.com/OpenType/otspec/name.htm |
|
2176 |
// - print complete entries for all other LCID values |
|
2177 |
// Run |
|
2178 |
// awk -f awk-script langIDMap > statements |
|
2179 |
addLCIDMapEntry(map, "ar", (short) 0x0401); |
|
2180 |
addLCIDMapEntry(map, "bg", (short) 0x0402); |
|
2181 |
addLCIDMapEntry(map, "ca", (short) 0x0403); |
|
2182 |
addLCIDMapEntry(map, "zh", (short) 0x0404); |
|
2183 |
addLCIDMapEntry(map, "cs", (short) 0x0405); |
|
2184 |
addLCIDMapEntry(map, "da", (short) 0x0406); |
|
2185 |
addLCIDMapEntry(map, "de", (short) 0x0407); |
|
2186 |
addLCIDMapEntry(map, "el", (short) 0x0408); |
|
2187 |
addLCIDMapEntry(map, "es", (short) 0x040a); |
|
2188 |
addLCIDMapEntry(map, "fi", (short) 0x040b); |
|
2189 |
addLCIDMapEntry(map, "fr", (short) 0x040c); |
|
2190 |
addLCIDMapEntry(map, "iw", (short) 0x040d); |
|
2191 |
addLCIDMapEntry(map, "hu", (short) 0x040e); |
|
2192 |
addLCIDMapEntry(map, "is", (short) 0x040f); |
|
2193 |
addLCIDMapEntry(map, "it", (short) 0x0410); |
|
2194 |
addLCIDMapEntry(map, "ja", (short) 0x0411); |
|
2195 |
addLCIDMapEntry(map, "ko", (short) 0x0412); |
|
2196 |
addLCIDMapEntry(map, "nl", (short) 0x0413); |
|
2197 |
addLCIDMapEntry(map, "no", (short) 0x0414); |
|
2198 |
addLCIDMapEntry(map, "pl", (short) 0x0415); |
|
2199 |
addLCIDMapEntry(map, "pt", (short) 0x0416); |
|
2200 |
addLCIDMapEntry(map, "rm", (short) 0x0417); |
|
2201 |
addLCIDMapEntry(map, "ro", (short) 0x0418); |
|
2202 |
addLCIDMapEntry(map, "ru", (short) 0x0419); |
|
2203 |
addLCIDMapEntry(map, "hr", (short) 0x041a); |
|
2204 |
addLCIDMapEntry(map, "sk", (short) 0x041b); |
|
2205 |
addLCIDMapEntry(map, "sq", (short) 0x041c); |
|
2206 |
addLCIDMapEntry(map, "sv", (short) 0x041d); |
|
2207 |
addLCIDMapEntry(map, "th", (short) 0x041e); |
|
2208 |
addLCIDMapEntry(map, "tr", (short) 0x041f); |
|
2209 |
addLCIDMapEntry(map, "ur", (short) 0x0420); |
|
2210 |
addLCIDMapEntry(map, "in", (short) 0x0421); |
|
2211 |
addLCIDMapEntry(map, "uk", (short) 0x0422); |
|
2212 |
addLCIDMapEntry(map, "be", (short) 0x0423); |
|
2213 |
addLCIDMapEntry(map, "sl", (short) 0x0424); |
|
2214 |
addLCIDMapEntry(map, "et", (short) 0x0425); |
|
2215 |
addLCIDMapEntry(map, "lv", (short) 0x0426); |
|
2216 |
addLCIDMapEntry(map, "lt", (short) 0x0427); |
|
2217 |
addLCIDMapEntry(map, "fa", (short) 0x0429); |
|
2218 |
addLCIDMapEntry(map, "vi", (short) 0x042a); |
|
2219 |
addLCIDMapEntry(map, "hy", (short) 0x042b); |
|
2220 |
addLCIDMapEntry(map, "eu", (short) 0x042d); |
|
2221 |
addLCIDMapEntry(map, "mk", (short) 0x042f); |
|
2222 |
addLCIDMapEntry(map, "tn", (short) 0x0432); |
|
2223 |
addLCIDMapEntry(map, "xh", (short) 0x0434); |
|
2224 |
addLCIDMapEntry(map, "zu", (short) 0x0435); |
|
2225 |
addLCIDMapEntry(map, "af", (short) 0x0436); |
|
2226 |
addLCIDMapEntry(map, "ka", (short) 0x0437); |
|
2227 |
addLCIDMapEntry(map, "fo", (short) 0x0438); |
|
2228 |
addLCIDMapEntry(map, "hi", (short) 0x0439); |
|
2229 |
addLCIDMapEntry(map, "mt", (short) 0x043a); |
|
2230 |
addLCIDMapEntry(map, "se", (short) 0x043b); |
|
2231 |
addLCIDMapEntry(map, "gd", (short) 0x043c); |
|
2232 |
addLCIDMapEntry(map, "ms", (short) 0x043e); |
|
2233 |
addLCIDMapEntry(map, "kk", (short) 0x043f); |
|
2234 |
addLCIDMapEntry(map, "ky", (short) 0x0440); |
|
2235 |
addLCIDMapEntry(map, "sw", (short) 0x0441); |
|
2236 |
addLCIDMapEntry(map, "tt", (short) 0x0444); |
|
2237 |
addLCIDMapEntry(map, "bn", (short) 0x0445); |
|
2238 |
addLCIDMapEntry(map, "pa", (short) 0x0446); |
|
2239 |
addLCIDMapEntry(map, "gu", (short) 0x0447); |
|
2240 |
addLCIDMapEntry(map, "ta", (short) 0x0449); |
|
2241 |
addLCIDMapEntry(map, "te", (short) 0x044a); |
|
2242 |
addLCIDMapEntry(map, "kn", (short) 0x044b); |
|
2243 |
addLCIDMapEntry(map, "ml", (short) 0x044c); |
|
2244 |
addLCIDMapEntry(map, "mr", (short) 0x044e); |
|
2245 |
addLCIDMapEntry(map, "sa", (short) 0x044f); |
|
2246 |
addLCIDMapEntry(map, "mn", (short) 0x0450); |
|
2247 |
addLCIDMapEntry(map, "cy", (short) 0x0452); |
|
2248 |
addLCIDMapEntry(map, "gl", (short) 0x0456); |
|
2249 |
addLCIDMapEntry(map, "dv", (short) 0x0465); |
|
2250 |
addLCIDMapEntry(map, "qu", (short) 0x046b); |
|
2251 |
addLCIDMapEntry(map, "mi", (short) 0x0481); |
|
2252 |
addLCIDMapEntry(map, "ar_IQ", (short) 0x0801); |
|
2253 |
addLCIDMapEntry(map, "zh_CN", (short) 0x0804); |
|
2254 |
addLCIDMapEntry(map, "de_CH", (short) 0x0807); |
|
2255 |
addLCIDMapEntry(map, "en_GB", (short) 0x0809); |
|
2256 |
addLCIDMapEntry(map, "es_MX", (short) 0x080a); |
|
2257 |
addLCIDMapEntry(map, "fr_BE", (short) 0x080c); |
|
2258 |
addLCIDMapEntry(map, "it_CH", (short) 0x0810); |
|
2259 |
addLCIDMapEntry(map, "nl_BE", (short) 0x0813); |
|
2260 |
addLCIDMapEntry(map, "no_NO_NY", (short) 0x0814); |
|
2261 |
addLCIDMapEntry(map, "pt_PT", (short) 0x0816); |
|
2262 |
addLCIDMapEntry(map, "ro_MD", (short) 0x0818); |
|
2263 |
addLCIDMapEntry(map, "ru_MD", (short) 0x0819); |
|
2264 |
addLCIDMapEntry(map, "sr_CS", (short) 0x081a); |
|
2265 |
addLCIDMapEntry(map, "sv_FI", (short) 0x081d); |
|
2266 |
addLCIDMapEntry(map, "az_AZ", (short) 0x082c); |
|
2267 |
addLCIDMapEntry(map, "se_SE", (short) 0x083b); |
|
2268 |
addLCIDMapEntry(map, "ga_IE", (short) 0x083c); |
|
2269 |
addLCIDMapEntry(map, "ms_BN", (short) 0x083e); |
|
2270 |
addLCIDMapEntry(map, "uz_UZ", (short) 0x0843); |
|
2271 |
addLCIDMapEntry(map, "qu_EC", (short) 0x086b); |
|
2272 |
addLCIDMapEntry(map, "ar_EG", (short) 0x0c01); |
|
2273 |
addLCIDMapEntry(map, "zh_HK", (short) 0x0c04); |
|
2274 |
addLCIDMapEntry(map, "de_AT", (short) 0x0c07); |
|
2275 |
addLCIDMapEntry(map, "en_AU", (short) 0x0c09); |
|
2276 |
addLCIDMapEntry(map, "fr_CA", (short) 0x0c0c); |
|
2277 |
addLCIDMapEntry(map, "sr_CS", (short) 0x0c1a); |
|
2278 |
addLCIDMapEntry(map, "se_FI", (short) 0x0c3b); |
|
2279 |
addLCIDMapEntry(map, "qu_PE", (short) 0x0c6b); |
|
2280 |
addLCIDMapEntry(map, "ar_LY", (short) 0x1001); |
|
2281 |
addLCIDMapEntry(map, "zh_SG", (short) 0x1004); |
|
2282 |
addLCIDMapEntry(map, "de_LU", (short) 0x1007); |
|
2283 |
addLCIDMapEntry(map, "en_CA", (short) 0x1009); |
|
2284 |
addLCIDMapEntry(map, "es_GT", (short) 0x100a); |
|
2285 |
addLCIDMapEntry(map, "fr_CH", (short) 0x100c); |
|
2286 |
addLCIDMapEntry(map, "hr_BA", (short) 0x101a); |
|
2287 |
addLCIDMapEntry(map, "ar_DZ", (short) 0x1401); |
|
2288 |
addLCIDMapEntry(map, "zh_MO", (short) 0x1404); |
|
2289 |
addLCIDMapEntry(map, "de_LI", (short) 0x1407); |
|
2290 |
addLCIDMapEntry(map, "en_NZ", (short) 0x1409); |
|
2291 |
addLCIDMapEntry(map, "es_CR", (short) 0x140a); |
|
2292 |
addLCIDMapEntry(map, "fr_LU", (short) 0x140c); |
|
2293 |
addLCIDMapEntry(map, "bs_BA", (short) 0x141a); |
|
2294 |
addLCIDMapEntry(map, "ar_MA", (short) 0x1801); |
|
2295 |
addLCIDMapEntry(map, "en_IE", (short) 0x1809); |
|
2296 |
addLCIDMapEntry(map, "es_PA", (short) 0x180a); |
|
2297 |
addLCIDMapEntry(map, "fr_MC", (short) 0x180c); |
|
2298 |
addLCIDMapEntry(map, "sr_BA", (short) 0x181a); |
|
2299 |
addLCIDMapEntry(map, "ar_TN", (short) 0x1c01); |
|
2300 |
addLCIDMapEntry(map, "en_ZA", (short) 0x1c09); |
|
2301 |
addLCIDMapEntry(map, "es_DO", (short) 0x1c0a); |
|
2302 |
addLCIDMapEntry(map, "sr_BA", (short) 0x1c1a); |
|
2303 |
addLCIDMapEntry(map, "ar_OM", (short) 0x2001); |
|
2304 |
addLCIDMapEntry(map, "en_JM", (short) 0x2009); |
|
2305 |
addLCIDMapEntry(map, "es_VE", (short) 0x200a); |
|
2306 |
addLCIDMapEntry(map, "ar_YE", (short) 0x2401); |
|
2307 |
addLCIDMapEntry(map, "es_CO", (short) 0x240a); |
|
2308 |
addLCIDMapEntry(map, "ar_SY", (short) 0x2801); |
|
2309 |
addLCIDMapEntry(map, "en_BZ", (short) 0x2809); |
|
2310 |
addLCIDMapEntry(map, "es_PE", (short) 0x280a); |
|
2311 |
addLCIDMapEntry(map, "ar_JO", (short) 0x2c01); |
|
2312 |
addLCIDMapEntry(map, "en_TT", (short) 0x2c09); |
|
2313 |
addLCIDMapEntry(map, "es_AR", (short) 0x2c0a); |
|
2314 |
addLCIDMapEntry(map, "ar_LB", (short) 0x3001); |
|
2315 |
addLCIDMapEntry(map, "en_ZW", (short) 0x3009); |
|
2316 |
addLCIDMapEntry(map, "es_EC", (short) 0x300a); |
|
2317 |
addLCIDMapEntry(map, "ar_KW", (short) 0x3401); |
|
2318 |
addLCIDMapEntry(map, "en_PH", (short) 0x3409); |
|
2319 |
addLCIDMapEntry(map, "es_CL", (short) 0x340a); |
|
2320 |
addLCIDMapEntry(map, "ar_AE", (short) 0x3801); |
|
2321 |
addLCIDMapEntry(map, "es_UY", (short) 0x380a); |
|
2322 |
addLCIDMapEntry(map, "ar_BH", (short) 0x3c01); |
|
2323 |
addLCIDMapEntry(map, "es_PY", (short) 0x3c0a); |
|
2324 |
addLCIDMapEntry(map, "ar_QA", (short) 0x4001); |
|
2325 |
addLCIDMapEntry(map, "es_BO", (short) 0x400a); |
|
2326 |
addLCIDMapEntry(map, "es_SV", (short) 0x440a); |
|
2327 |
addLCIDMapEntry(map, "es_HN", (short) 0x480a); |
|
2328 |
addLCIDMapEntry(map, "es_NI", (short) 0x4c0a); |
|
2329 |
addLCIDMapEntry(map, "es_PR", (short) 0x500a); |
|
2330 |
||
2331 |
lcidMap = map; |
|
2332 |
} |
|
2333 |
||
2334 |
public static int getNumFonts() { |
|
2335 |
return physicalFonts.size()+maxCompFont; |
|
2336 |
} |
|
2337 |
||
2338 |
private static boolean fontSupportsEncoding(Font font, String encoding) { |
|
2339 |
return getFont2D(font).supportsEncoding(encoding); |
|
2340 |
} |
|
2341 |
||
2342 |
public synchronized static native String getFontPath(boolean noType1Fonts); |
|
2343 |
public synchronized static native void setNativeFontPath(String fontPath); |
|
2344 |
||
2345 |
||
2346 |
private static Thread fileCloser = null; |
|
2347 |
static Vector<File> tmpFontFiles = null; |
|
2348 |
||
2349 |
public static Font2D createFont2D(File fontFile, int fontFormat, |
|
2350 |
boolean isCopy) |
|
2351 |
throws FontFormatException { |
|
2352 |
||
2353 |
String fontFilePath = fontFile.getPath(); |
|
2354 |
FileFont font2D = null; |
|
2355 |
final File fFile = fontFile; |
|
2356 |
try { |
|
2357 |
switch (fontFormat) { |
|
2358 |
case Font.TRUETYPE_FONT: |
|
2359 |
font2D = new TrueTypeFont(fontFilePath, null, 0, true); |
|
2360 |
break; |
|
2361 |
case Font.TYPE1_FONT: |
|
2362 |
font2D = new Type1Font(fontFilePath, null); |
|
2363 |
break; |
|
2364 |
default: |
|
2365 |
throw new FontFormatException("Unrecognised Font Format"); |
|
2366 |
} |
|
2367 |
} catch (FontFormatException e) { |
|
2368 |
if (isCopy) { |
|
2369 |
java.security.AccessController.doPrivileged( |
|
2370 |
new java.security.PrivilegedAction() { |
|
2371 |
public Object run() { |
|
2372 |
fFile.delete(); |
|
2373 |
return null; |
|
2374 |
} |
|
2375 |
}); |
|
2376 |
} |
|
2377 |
throw(e); |
|
2378 |
} |
|
2379 |
if (isCopy) { |
|
2380 |
font2D.setFileToRemove(fontFile); |
|
2381 |
synchronized (FontManager.class) { |
|
2382 |
||
2383 |
if (tmpFontFiles == null) { |
|
2384 |
tmpFontFiles = new Vector<File>(); |
|
2385 |
} |
|
2386 |
tmpFontFiles.add(fontFile); |
|
2387 |
||
2388 |
if (fileCloser == null) { |
|
2389 |
final Runnable fileCloserRunnable = new Runnable() { |
|
2390 |
public void run() { |
|
2391 |
java.security.AccessController.doPrivileged( |
|
2392 |
new java.security.PrivilegedAction() { |
|
2393 |
public Object run() { |
|
2394 |
||
2395 |
for (int i=0;i<CHANNELPOOLSIZE;i++) { |
|
2396 |
if (fontFileCache[i] != null) { |
|
2397 |
try { |
|
2398 |
fontFileCache[i].close(); |
|
2399 |
} catch (Exception e) { |
|
2400 |
} |
|
2401 |
} |
|
2402 |
} |
|
2403 |
if (tmpFontFiles != null) { |
|
2404 |
File[] files = new File[tmpFontFiles.size()]; |
|
2405 |
files = tmpFontFiles.toArray(files); |
|
2406 |
for (int f=0; f<files.length;f++) { |
|
2407 |
try { |
|
2408 |
files[f].delete(); |
|
2409 |
} catch (Exception e) { |
|
2410 |
} |
|
2411 |
} |
|
2412 |
} |
|
2413 |
||
2414 |
return null; |
|
2415 |
} |
|
2416 |
||
2417 |
}); |
|
2418 |
} |
|
2419 |
}; |
|
2420 |
java.security.AccessController.doPrivileged( |
|
2421 |
new java.security.PrivilegedAction() { |
|
2422 |
public Object run() { |
|
2423 |
/* The thread must be a member of a thread group |
|
2424 |
* which will not get GCed before VM exit. |
|
2425 |
* Make its parent the top-level thread group. |
|
2426 |
*/ |
|
2427 |
ThreadGroup tg = |
|
2428 |
Thread.currentThread().getThreadGroup(); |
|
2429 |
for (ThreadGroup tgn = tg; |
|
2430 |
tgn != null; |
|
2431 |
tg = tgn, tgn = tg.getParent()); |
|
2432 |
fileCloser = new Thread(tg, fileCloserRunnable); |
|
2433 |
Runtime.getRuntime().addShutdownHook(fileCloser); |
|
2434 |
return null; |
|
2435 |
} |
|
2436 |
}); |
|
2437 |
} |
|
2438 |
} |
|
2439 |
} |
|
2440 |
return font2D; |
|
2441 |
} |
|
2442 |
||
2443 |
/* remind: used in X11GraphicsEnvironment and called often enough |
|
2444 |
* that we ought to obsolete this code |
|
2445 |
*/ |
|
2446 |
public synchronized static String getFullNameByFileName(String fileName) { |
|
2447 |
PhysicalFont[] physFonts = getPhysicalFonts(); |
|
2448 |
for (int i=0;i<physFonts.length;i++) { |
|
2449 |
if (physFonts[i].platName.equals(fileName)) { |
|
2450 |
return (physFonts[i].getFontName(null)); |
|
2451 |
} |
|
2452 |
} |
|
2453 |
return null; |
|
2454 |
} |
|
2455 |
||
2456 |
/* |
|
2457 |
* This is called when font is determined to be invalid/bad. |
|
2458 |
* It designed to be called (for example) by the font scaler |
|
2459 |
* when in processing a font file it is discovered to be incorrect. |
|
2460 |
* This is different than the case where fonts are discovered to |
|
2461 |
* be incorrect during initial verification, as such fonts are |
|
2462 |
* never registered. |
|
2463 |
* Handles to this font held are re-directed to a default font. |
|
2464 |
* This default may not be an ideal substitute buts it better than |
|
2465 |
* crashing This code assumes a PhysicalFont parameter as it doesn't |
|
2466 |
* make sense for a Composite to be "bad". |
|
2467 |
*/ |
|
2468 |
public static synchronized void deRegisterBadFont(Font2D font2D) { |
|
2469 |
if (!(font2D instanceof PhysicalFont)) { |
|
2470 |
/* We should never reach here, but just in case */ |
|
2471 |
return; |
|
2472 |
} else { |
|
2473 |
if (logging) { |
|
2474 |
logger.severe("Deregister bad font: " + font2D); |
|
2475 |
} |
|
2476 |
replaceFont((PhysicalFont)font2D, getDefaultPhysicalFont()); |
|
2477 |
} |
|
2478 |
} |
|
2479 |
||
2480 |
/* |
|
2481 |
* This encapsulates all the work that needs to be done when a |
|
2482 |
* Font2D is replaced by a different Font2D. |
|
2483 |
*/ |
|
2484 |
public static synchronized void replaceFont(PhysicalFont oldFont, |
|
2485 |
PhysicalFont newFont) { |
|
2486 |
||
2487 |
if (oldFont.handle.font2D != oldFont) { |
|
2488 |
/* already done */ |
|
2489 |
return; |
|
2490 |
} |
|
2491 |
||
2492 |
/* If we try to replace the font with itself, that won't work, |
|
2493 |
* so pick any alternative physical font |
|
2494 |
*/ |
|
2495 |
if (oldFont == newFont) { |
|
2496 |
if (logging) { |
|
2497 |
logger.severe("Can't replace bad font with itself " + oldFont); |
|
2498 |
} |
|
2499 |
PhysicalFont[] physFonts = getPhysicalFonts(); |
|
2500 |
for (int i=0; i<physFonts.length;i++) { |
|
2501 |
if (physFonts[i] != newFont) { |
|
2502 |
newFont = physFonts[i]; |
|
2503 |
break; |
|
2504 |
} |
|
2505 |
} |
|
2506 |
if (oldFont == newFont) { |
|
2507 |
if (logging) { |
|
2508 |
logger.severe("This is bad. No good physicalFonts found."); |
|
2509 |
} |
|
2510 |
return; |
|
2511 |
} |
|
2512 |
} |
|
2513 |
||
2514 |
/* eliminate references to this font, so it won't be located |
|
2515 |
* by future callers, and will be eligible for GC when all |
|
2516 |
* references are removed |
|
2517 |
*/ |
|
2518 |
oldFont.handle.font2D = newFont; |
|
2519 |
physicalFonts.remove(oldFont.fullName); |
|
2520 |
fullNameToFont.remove(oldFont.fullName.toLowerCase(Locale.ENGLISH)); |
|
2521 |
FontFamily.remove(oldFont); |
|
2522 |
||
2523 |
if (localeFullNamesToFont != null) { |
|
2524 |
Map.Entry[] mapEntries = |
|
2525 |
(Map.Entry[])localeFullNamesToFont.entrySet(). |
|
2526 |
toArray(new Map.Entry[0]); |
|
2527 |
/* Should I be replacing these, or just I just remove |
|
2528 |
* the names from the map? |
|
2529 |
*/ |
|
2530 |
for (int i=0; i<mapEntries.length;i++) { |
|
2531 |
if (mapEntries[i].getValue() == oldFont) { |
|
2532 |
try { |
|
2533 |
mapEntries[i].setValue(newFont); |
|
2534 |
} catch (Exception e) { |
|
2535 |
/* some maps don't support this operation. |
|
2536 |
* In this case just give up and remove the entry. |
|
2537 |
*/ |
|
2538 |
localeFullNamesToFont.remove(mapEntries[i].getKey()); |
|
2539 |
} |
|
2540 |
} |
|
2541 |
} |
|
2542 |
} |
|
2543 |
||
2544 |
for (int i=0; i<maxCompFont; i++) { |
|
2545 |
/* Deferred initialization of composites shouldn't be |
|
2546 |
* a problem for this case, since a font must have been |
|
2547 |
* initialised to be discovered to be bad. |
|
2548 |
* Some JRE composites on Solaris use two versions of the same |
|
2549 |
* font. The replaced font isn't bad, just "smaller" so there's |
|
2550 |
* no need to make the slot point to the new font. |
|
2551 |
* Since composites have a direct reference to the Font2D (not |
|
2552 |
* via a handle) making this substitution is not safe and could |
|
2553 |
* cause an additional problem and so this substitution is |
|
2554 |
* warranted only when a font is truly "bad" and could cause |
|
2555 |
* a crash. So we now replace it only if its being substituted |
|
2556 |
* with some font other than a fontconfig rank font |
|
2557 |
* Since in practice a substitution will have the same rank |
|
2558 |
* this may never happen, but the code is safer even if its |
|
2559 |
* also now a no-op. |
|
2560 |
* The only obvious "glitch" from this stems from the current |
|
2561 |
* implementation that when asked for the number of glyphs in a |
|
2562 |
* composite it lies and returns the number in slot 0 because |
|
2563 |
* composite glyphs aren't contiguous. Since we live with that |
|
2564 |
* we can live with the glitch that depending on how it was |
|
2565 |
* initialised a composite may return different values for this. |
|
2566 |
* Fixing the issues with composite glyph ids is tricky as |
|
2567 |
* there are exclusion ranges and unlike other fonts even the |
|
2568 |
* true "numGlyphs" isn't a contiguous range. Likely the only |
|
2569 |
* solution is an API that returns an array of glyph ranges |
|
2570 |
* which takes precedence over the existing API. That might |
|
2571 |
* also need to address excluding ranges which represent a |
|
2572 |
* code point supported by an earlier component. |
|
2573 |
*/ |
|
2574 |
if (newFont.getRank() > Font2D.FONT_CONFIG_RANK) { |
|
2575 |
compFonts[i].replaceComponentFont(oldFont, newFont); |
|
2576 |
} |
|
2577 |
} |
|
2578 |
} |
|
2579 |
||
2580 |
private static synchronized void loadLocaleNames() { |
|
2581 |
if (localeFullNamesToFont != null) { |
|
2582 |
return; |
|
2583 |
} |
|
2584 |
localeFullNamesToFont = new HashMap<String, TrueTypeFont>(); |
|
2585 |
Font2D[] fonts = getRegisteredFonts(); |
|
2586 |
for (int i=0; i<fonts.length; i++) { |
|
2587 |
if (fonts[i] instanceof TrueTypeFont) { |
|
2588 |
TrueTypeFont ttf = (TrueTypeFont)fonts[i]; |
|
2589 |
String[] fullNames = ttf.getAllFullNames(); |
|
2590 |
for (int n=0; n<fullNames.length; n++) { |
|
2591 |
localeFullNamesToFont.put(fullNames[n], ttf); |
|
2592 |
} |
|
2593 |
FontFamily family = FontFamily.getFamily(ttf.familyName); |
|
2594 |
if (family != null) { |
|
2595 |
FontFamily.addLocaleNames(family, ttf.getAllFamilyNames()); |
|
2596 |
} |
|
2597 |
} |
|
2598 |
} |
|
2599 |
} |
|
2600 |
||
2601 |
/* This replicate the core logic of findFont2D but operates on |
|
2602 |
* all the locale names. This hasn't been merged into findFont2D to |
|
2603 |
* keep the logic simpler and reduce overhead, since this case is |
|
2604 |
* almost never used. The main case in which it is called is when |
|
2605 |
* a bogus font name is used and we need to check all possible names |
|
2606 |
* before returning the default case. |
|
2607 |
*/ |
|
2608 |
private static Font2D findFont2DAllLocales(String name, int style) { |
|
2609 |
||
2610 |
if (logging) { |
|
2611 |
logger.info("Searching localised font names for:" + name); |
|
2612 |
} |
|
2613 |
||
2614 |
/* If reach here and no match has been located, then if we have |
|
2615 |
* not yet built the map of localeFullNamesToFont for TT fonts, do so |
|
2616 |
* now. This method must be called after all fonts have been loaded. |
|
2617 |
*/ |
|
2618 |
if (localeFullNamesToFont == null) { |
|
2619 |
loadLocaleNames(); |
|
2620 |
} |
|
2621 |
String lowerCaseName = name.toLowerCase(); |
|
2622 |
Font2D font = null; |
|
2623 |
||
2624 |
/* First see if its a family name. */ |
|
2625 |
FontFamily family = FontFamily.getLocaleFamily(lowerCaseName); |
|
2626 |
if (family != null) { |
|
2627 |
font = family.getFont(style); |
|
2628 |
if (font == null) { |
|
2629 |
font = family.getClosestStyle(style); |
|
2630 |
} |
|
2631 |
if (font != null) { |
|
2632 |
return font; |
|
2633 |
} |
|
2634 |
} |
|
2635 |
||
2636 |
/* If it wasn't a family name, it should be a full name. */ |
|
2637 |
synchronized (FontManager.class) { |
|
2638 |
font = localeFullNamesToFont.get(name); |
|
2639 |
} |
|
2640 |
if (font != null) { |
|
2641 |
if (font.style == style || style == Font.PLAIN) { |
|
2642 |
return font; |
|
2643 |
} else { |
|
2644 |
family = FontFamily.getFamily(font.getFamilyName(null)); |
|
2645 |
if (family != null) { |
|
2646 |
Font2D familyFont = family.getFont(style); |
|
2647 |
/* We exactly matched the requested style, use it! */ |
|
2648 |
if (familyFont != null) { |
|
2649 |
return familyFont; |
|
2650 |
} else { |
|
2651 |
familyFont = family.getClosestStyle(style); |
|
2652 |
if (familyFont != null) { |
|
2653 |
/* The next check is perhaps one |
|
2654 |
* that shouldn't be done. ie if we get this |
|
2655 |
* far we have probably as close a match as we |
|
2656 |
* are going to get. We could load all fonts to |
|
2657 |
* see if somehow some parts of the family are |
|
2658 |
* loaded but not all of it. |
|
2659 |
* This check is commented out for now. |
|
2660 |
*/ |
|
2661 |
if (!familyFont.canDoStyle(style)) { |
|
2662 |
familyFont = null; |
|
2663 |
} |
|
2664 |
return familyFont; |
|
2665 |
} |
|
2666 |
} |
|
2667 |
} |
|
2668 |
} |
|
2669 |
} |
|
2670 |
return font; |
|
2671 |
} |
|
2672 |
||
2673 |
/* Supporting "alternate" composite fonts on 2D graphics objects |
|
2674 |
* is accessed by the application by calling methods on the local |
|
2675 |
* GraphicsEnvironment. The overall implementation is described |
|
2676 |
* in one place, here, since otherwise the implementation is spread |
|
2677 |
* around it may be difficult to track. |
|
2678 |
* The methods below call into SunGraphicsEnvironment which creates a |
|
2679 |
* new FontConfiguration instance. The FontConfiguration class, |
|
2680 |
* and its platform sub-classes are updated to take parameters requesting |
|
2681 |
* these behaviours. This is then used to create new composite font |
|
2682 |
* instances. Since this calls the initCompositeFont method in |
|
2683 |
* SunGraphicsEnvironment it performs the same initialization as is |
|
2684 |
* performed normally. There may be some duplication of effort, but |
|
2685 |
* that code is already written to be able to perform properly if called |
|
2686 |
* to duplicate work. The main difference is that if we detect we are |
|
2687 |
* running in an applet/browser/Java plugin environment these new fonts |
|
2688 |
* are not placed in the "default" maps but into an AppContext instance. |
|
2689 |
* The font lookup mechanism in java.awt.Font.getFont2D() is also updated |
|
2690 |
* so that look-up for composite fonts will in that case always |
|
2691 |
* do a lookup rather than returning a cached result. |
|
2692 |
* This is inefficient but necessary else singleton java.awt.Font |
|
2693 |
* instances would not retrieve the correct Font2D for the appcontext. |
|
2694 |
* sun.font.FontManager.findFont2D is also updated to that it uses |
|
2695 |
* a name map cache specific to that appcontext. |
|
2696 |
* |
|
2697 |
* Getting an AppContext is expensive, so there is a global variable |
|
2698 |
* that records whether these methods have ever been called and can |
|
2699 |
* avoid the expense for almost all applications. Once the correct |
|
2700 |
* CompositeFont is associated with the Font, everything should work |
|
2701 |
* through existing mechanisms. |
|
2702 |
* A special case is that GraphicsEnvironment.getAllFonts() must |
|
2703 |
* return an AppContext specific list. |
|
2704 |
* |
|
2705 |
* Calling the methods below is "heavyweight" but it is expected that |
|
2706 |
* these methods will be called very rarely. |
|
2707 |
* |
|
2708 |
* If usingPerAppContextComposites is true, we are in "applet" |
|
2709 |
* (eg browser) enviroment and at least one context has selected |
|
2710 |
* an alternate composite font behaviour. |
|
2711 |
* If usingAlternateComposites is true, we are not in an "applet" |
|
2712 |
* environment and the (single) application has selected |
|
2713 |
* an alternate composite font behaviour. |
|
2714 |
* |
|
2715 |
* - Printing: The implementation delegates logical fonts to an AWT |
|
2716 |
* mechanism which cannot use these alternate configurations. |
|
2717 |
* We can detect that alternate fonts are in use and back-off to 2D, but |
|
2718 |
* that uses outlines. Much of this can be fixed with additional work |
|
2719 |
* but that may have to wait. The results should be correct, just not |
|
2720 |
* optimal. |
|
2721 |
*/ |
|
2722 |
private static final Object altJAFontKey = new Object(); |
|
2723 |
private static final Object localeFontKey = new Object(); |
|
2724 |
private static final Object proportionalFontKey = new Object(); |
|
2725 |
public static boolean usingPerAppContextComposites = false; |
|
2726 |
private static boolean usingAlternateComposites = false; |
|
2727 |
||
2728 |
/* These values are used only if we are running as a standalone |
|
2729 |
* application, as determined by maybeMultiAppContext(); |
|
2730 |
*/ |
|
2731 |
private static boolean gAltJAFont = false; |
|
2732 |
private static boolean gLocalePref = false; |
|
2733 |
private static boolean gPropPref = false; |
|
2734 |
||
2735 |
/* This method doesn't check if alternates are selected in this app |
|
2736 |
* context. Its used by the FontMetrics caching code which in such |
|
2737 |
* a case cannot retrieve a cached metrics solely on the basis of |
|
2738 |
* the Font.equals() method since it needs to also check if the Font2D |
|
2739 |
* is the same. |
|
2740 |
* We also use non-standard composites for Swing native L&F fonts on |
|
2741 |
* Windows. In that case the policy is that the metrics reported are |
|
2742 |
* based solely on the physical font in the first slot which is the |
|
2743 |
* visible java.awt.Font. So in that case the metrics cache which tests |
|
2744 |
* the Font does what we want. In the near future when we expand the GTK |
|
2745 |
* logical font definitions we may need to revisit this if GTK reports |
|
2746 |
* combined metrics instead. For now though this test can be simple. |
|
2747 |
*/ |
|
2748 |
static boolean maybeUsingAlternateCompositeFonts() { |
|
2749 |
return usingAlternateComposites || usingPerAppContextComposites; |
|
2750 |
} |
|
2751 |
||
2752 |
public static boolean usingAlternateCompositeFonts() { |
|
2753 |
return (usingAlternateComposites || |
|
2754 |
(usingPerAppContextComposites && |
|
2755 |
AppContext.getAppContext().get(CompositeFont.class) != null)); |
|
2756 |
} |
|
2757 |
||
2758 |
private static boolean maybeMultiAppContext() { |
|
2759 |
Boolean appletSM = (Boolean) |
|
2760 |
java.security.AccessController.doPrivileged( |
|
2761 |
new java.security.PrivilegedAction() { |
|
2762 |
public Object run() { |
|
2763 |
SecurityManager sm = System.getSecurityManager(); |
|
2764 |
return new Boolean |
|
2765 |
(sm instanceof sun.applet.AppletSecurity); |
|
2766 |
} |
|
2767 |
}); |
|
2768 |
return appletSM.booleanValue(); |
|
2769 |
} |
|
2770 |
||
2771 |
/* Modifies the behaviour of a subsequent call to preferLocaleFonts() |
|
2772 |
* to use Mincho instead of Gothic for dialoginput in JA locales |
|
2773 |
* on windows. Not needed on other platforms. |
|
2774 |
*/ |
|
2775 |
public static synchronized void useAlternateFontforJALocales() { |
|
2776 |
||
2777 |
if (!isWindows) { |
|
2778 |
return; |
|
2779 |
} |
|
2780 |
||
2781 |
initSGEnv(); |
|
2782 |
if (!maybeMultiAppContext()) { |
|
2783 |
gAltJAFont = true; |
|
2784 |
} else { |
|
2785 |
AppContext appContext = AppContext.getAppContext(); |
|
2786 |
appContext.put(altJAFontKey, altJAFontKey); |
|
2787 |
} |
|
2788 |
} |
|
2789 |
||
2790 |
public static boolean usingAlternateFontforJALocales() { |
|
2791 |
if (!maybeMultiAppContext()) { |
|
2792 |
return gAltJAFont; |
|
2793 |
} else { |
|
2794 |
AppContext appContext = AppContext.getAppContext(); |
|
2795 |
return appContext.get(altJAFontKey) == altJAFontKey; |
|
2796 |
} |
|
2797 |
} |
|
2798 |
||
2799 |
public static synchronized void preferLocaleFonts() { |
|
2800 |
||
2801 |
initSGEnv(); |
|
2802 |
||
2803 |
/* Test if re-ordering will have any effect */ |
|
2804 |
if (!FontConfiguration.willReorderForStartupLocale()) { |
|
2805 |
return; |
|
2806 |
} |
|
2807 |
||
2808 |
if (!maybeMultiAppContext()) { |
|
2809 |
if (gLocalePref == true) { |
|
2810 |
return; |
|
2811 |
} |
|
2812 |
gLocalePref = true; |
|
2813 |
sgEnv.createCompositeFonts(fontNameCache, gLocalePref, gPropPref); |
|
2814 |
usingAlternateComposites = true; |
|
2815 |
} else { |
|
2816 |
AppContext appContext = AppContext.getAppContext(); |
|
2817 |
if (appContext.get(localeFontKey) == localeFontKey) { |
|
2818 |
return; |
|
2819 |
} |
|
2820 |
appContext.put(localeFontKey, localeFontKey); |
|
2821 |
boolean acPropPref = |
|
2822 |
appContext.get(proportionalFontKey) == proportionalFontKey; |
|
2823 |
ConcurrentHashMap<String, Font2D> |
|
2824 |
altNameCache = new ConcurrentHashMap<String, Font2D> (); |
|
2825 |
/* If there is an existing hashtable, we can drop it. */ |
|
2826 |
appContext.put(CompositeFont.class, altNameCache); |
|
2827 |
usingPerAppContextComposites = true; |
|
2828 |
sgEnv.createCompositeFonts(altNameCache, true, acPropPref); |
|
2829 |
} |
|
2830 |
} |
|
2831 |
||
2832 |
public static synchronized void preferProportionalFonts() { |
|
2833 |
||
2834 |
/* If no proportional fonts are configured, there's no need |
|
2835 |
* to take any action. |
|
2836 |
*/ |
|
2837 |
if (!FontConfiguration.hasMonoToPropMap()) { |
|
2838 |
return; |
|
2839 |
} |
|
2840 |
||
2841 |
initSGEnv(); |
|
2842 |
||
2843 |
if (!maybeMultiAppContext()) { |
|
2844 |
if (gPropPref == true) { |
|
2845 |
return; |
|
2846 |
} |
|
2847 |
gPropPref = true; |
|
2848 |
sgEnv.createCompositeFonts(fontNameCache, gLocalePref, gPropPref); |
|
2849 |
usingAlternateComposites = true; |
|
2850 |
} else { |
|
2851 |
AppContext appContext = AppContext.getAppContext(); |
|
2852 |
if (appContext.get(proportionalFontKey) == proportionalFontKey) { |
|
2853 |
return; |
|
2854 |
} |
|
2855 |
appContext.put(proportionalFontKey, proportionalFontKey); |
|
2856 |
boolean acLocalePref = |
|
2857 |
appContext.get(localeFontKey) == localeFontKey; |
|
2858 |
ConcurrentHashMap<String, Font2D> |
|
2859 |
altNameCache = new ConcurrentHashMap<String, Font2D> (); |
|
2860 |
/* If there is an existing hashtable, we can drop it. */ |
|
2861 |
appContext.put(CompositeFont.class, altNameCache); |
|
2862 |
usingPerAppContextComposites = true; |
|
2863 |
sgEnv.createCompositeFonts(altNameCache, acLocalePref, true); |
|
2864 |
} |
|
2865 |
} |
|
2866 |
||
2867 |
private static HashSet<String> installedNames = null; |
|
2868 |
private static HashSet<String> getInstalledNames() { |
|
2869 |
if (installedNames == null) { |
|
2870 |
Locale l = sgEnv.getSystemStartupLocale(); |
|
2871 |
String[] installedFamilies = sgEnv.getInstalledFontFamilyNames(l); |
|
2872 |
Font[] installedFonts = sgEnv.getAllInstalledFonts(); |
|
2873 |
HashSet<String> names = new HashSet<String>(); |
|
2874 |
for (int i=0; i<installedFamilies.length; i++) { |
|
2875 |
names.add(installedFamilies[i].toLowerCase(l)); |
|
2876 |
} |
|
2877 |
for (int i=0; i<installedFonts.length; i++) { |
|
2878 |
names.add(installedFonts[i].getFontName(l).toLowerCase(l)); |
|
2879 |
} |
|
2880 |
installedNames = names; |
|
2881 |
} |
|
2882 |
return installedNames; |
|
2883 |
} |
|
2884 |
||
2885 |
/* Keys are used to lookup per-AppContext Hashtables */ |
|
2886 |
private static final Object regFamilyKey = new Object(); |
|
2887 |
private static final Object regFullNameKey = new Object(); |
|
2888 |
private static Hashtable<String,FontFamily> createdByFamilyName; |
|
2889 |
private static Hashtable<String,Font2D> createdByFullName; |
|
2890 |
private static boolean fontsAreRegistered = false; |
|
2891 |
private static boolean fontsAreRegisteredPerAppContext = false; |
|
2892 |
||
2893 |
public static boolean registerFont(Font font) { |
|
2894 |
/* This method should not be called with "null". |
|
2895 |
* It is the caller's responsibility to ensure that. |
|
2896 |
*/ |
|
2897 |
if (font == null) { |
|
2898 |
return false; |
|
2899 |
} |
|
2900 |
||
2901 |
/* Initialise these objects only once we start to use this API */ |
|
2902 |
synchronized (regFamilyKey) { |
|
2903 |
if (createdByFamilyName == null) { |
|
2904 |
createdByFamilyName = new Hashtable<String,FontFamily>(); |
|
2905 |
createdByFullName = new Hashtable<String,Font2D>(); |
|
2906 |
} |
|
2907 |
} |
|
2908 |
||
2909 |
if (!isCreatedFont(font)) { |
|
2910 |
return false; |
|
2911 |
} |
|
2912 |
if (sgEnv == null) { |
|
2913 |
initSGEnv(); |
|
2914 |
} |
|
2915 |
/* We want to ensure that this font cannot override existing |
|
2916 |
* installed fonts. Check these conditions : |
|
2917 |
* - family name is not that of an installed font |
|
2918 |
* - full name is not that of an installed font |
|
2919 |
* - family name is not the same as the full name of an installed font |
|
2920 |
* - full name is not the same as the family name of an installed font |
|
2921 |
* The last two of these may initially look odd but the reason is |
|
2922 |
* that (unfortunately) Font constructors do not distinuguish these. |
|
2923 |
* An extreme example of such a problem would be a font which has |
|
2924 |
* family name "Dialog.Plain" and full name of "Dialog". |
|
2925 |
* The one arguably overly stringent restriction here is that if an |
|
2926 |
* application wants to supply a new member of an existing family |
|
2927 |
* It will get rejected. But since the JRE can perform synthetic |
|
2928 |
* styling in many cases its not necessary. |
|
2929 |
* We don't apply the same logic to registered fonts. If apps want |
|
2930 |
* to do this lets assume they have a reason. It won't cause problems |
|
2931 |
* except for themselves. |
|
2932 |
*/ |
|
2933 |
HashSet<String> names = getInstalledNames(); |
|
2934 |
Locale l = sgEnv.getSystemStartupLocale(); |
|
2935 |
String familyName = font.getFamily(l).toLowerCase(); |
|
2936 |
String fullName = font.getFontName(l).toLowerCase(); |
|
2937 |
if (names.contains(familyName) || names.contains(fullName)) { |
|
2938 |
return false; |
|
2939 |
} |
|
2940 |
||
2941 |
/* Checks passed, now register the font */ |
|
2942 |
Hashtable<String,FontFamily> familyTable; |
|
2943 |
Hashtable<String,Font2D> fullNameTable; |
|
2944 |
if (!maybeMultiAppContext()) { |
|
2945 |
familyTable = createdByFamilyName; |
|
2946 |
fullNameTable = createdByFullName; |
|
2947 |
fontsAreRegistered = true; |
|
2948 |
} else { |
|
2949 |
AppContext appContext = AppContext.getAppContext(); |
|
2950 |
familyTable = |
|
2951 |
(Hashtable<String,FontFamily>)appContext.get(regFamilyKey); |
|
2952 |
fullNameTable = |
|
2953 |
(Hashtable<String,Font2D>)appContext.get(regFullNameKey); |
|
2954 |
if (familyTable == null) { |
|
2955 |
familyTable = new Hashtable<String,FontFamily>(); |
|
2956 |
fullNameTable = new Hashtable<String,Font2D>(); |
|
2957 |
appContext.put(regFamilyKey, familyTable); |
|
2958 |
appContext.put(regFullNameKey, fullNameTable); |
|
2959 |
} |
|
2960 |
fontsAreRegisteredPerAppContext = true; |
|
2961 |
} |
|
2962 |
/* Create the FontFamily and add font to the tables */ |
|
2963 |
Font2D font2D = getFont2D(font); |
|
2964 |
int style = font2D.getStyle(); |
|
2965 |
FontFamily family = familyTable.get(familyName); |
|
2966 |
if (family == null) { |
|
2967 |
family = new FontFamily(font.getFamily(l)); |
|
2968 |
familyTable.put(familyName, family); |
|
2969 |
} |
|
2970 |
/* Remove name cache entries if not using app contexts. |
|
2971 |
* To accommodate a case where code may have registered first a plain |
|
2972 |
* family member and then used it and is now registering a bold family |
|
2973 |
* member, we need to remove all members of the family, so that the |
|
2974 |
* new style can get picked up rather than continuing to synthesise. |
|
2975 |
*/ |
|
2976 |
if (fontsAreRegistered) { |
|
2977 |
removeFromCache(family.getFont(Font.PLAIN)); |
|
2978 |
removeFromCache(family.getFont(Font.BOLD)); |
|
2979 |
removeFromCache(family.getFont(Font.ITALIC)); |
|
2980 |
removeFromCache(family.getFont(Font.BOLD|Font.ITALIC)); |
|
2981 |
removeFromCache(fullNameTable.get(fullName)); |
|
2982 |
} |
|
2983 |
family.setFont(font2D, style); |
|
2984 |
fullNameTable.put(fullName, font2D); |
|
2985 |
return true; |
|
2986 |
} |
|
2987 |
||
2988 |
/* Remove from the name cache all references to the Font2D */ |
|
2989 |
private static void removeFromCache(Font2D font) { |
|
2990 |
if (font == null) { |
|
2991 |
return; |
|
2992 |
} |
|
2993 |
String[] keys = (String[])(fontNameCache.keySet().toArray(STR_ARRAY)); |
|
2994 |
for (int k=0; k<keys.length;k++) { |
|
2995 |
if (fontNameCache.get(keys[k]) == font) { |
|
2996 |
fontNameCache.remove(keys[k]); |
|
2997 |
} |
|
2998 |
} |
|
2999 |
} |
|
3000 |
||
3001 |
// It may look odd to use TreeMap but its more convenient to the caller. |
|
3002 |
public static TreeMap<String, String> getCreatedFontFamilyNames() { |
|
3003 |
||
3004 |
Hashtable<String,FontFamily> familyTable; |
|
3005 |
if (fontsAreRegistered) { |
|
3006 |
familyTable = createdByFamilyName; |
|
3007 |
} else if (fontsAreRegisteredPerAppContext) { |
|
3008 |
AppContext appContext = AppContext.getAppContext(); |
|
3009 |
familyTable = |
|
3010 |
(Hashtable<String,FontFamily>)appContext.get(regFamilyKey); |
|
3011 |
} else { |
|
3012 |
return null; |
|
3013 |
} |
|
3014 |
||
3015 |
Locale l = sgEnv.getSystemStartupLocale(); |
|
3016 |
synchronized (familyTable) { |
|
3017 |
TreeMap<String, String> map = new TreeMap<String, String>(); |
|
3018 |
for (FontFamily f : familyTable.values()) { |
|
3019 |
Font2D font2D = f.getFont(Font.PLAIN); |
|
3020 |
if (font2D == null) { |
|
3021 |
font2D = f.getClosestStyle(Font.PLAIN); |
|
3022 |
} |
|
3023 |
String name = font2D.getFamilyName(l); |
|
3024 |
map.put(name.toLowerCase(l), name); |
|
3025 |
} |
|
3026 |
return map; |
|
3027 |
} |
|
3028 |
} |
|
3029 |
||
3030 |
public static Font[] getCreatedFonts() { |
|
3031 |
||
3032 |
Hashtable<String,Font2D> nameTable; |
|
3033 |
if (fontsAreRegistered) { |
|
3034 |
nameTable = createdByFullName; |
|
3035 |
} else if (fontsAreRegisteredPerAppContext) { |
|
3036 |
AppContext appContext = AppContext.getAppContext(); |
|
3037 |
nameTable = |
|
3038 |
(Hashtable<String,Font2D>)appContext.get(regFullNameKey); |
|
3039 |
} else { |
|
3040 |
return null; |
|
3041 |
} |
|
3042 |
||
3043 |
Locale l = sgEnv.getSystemStartupLocale(); |
|
3044 |
synchronized (nameTable) { |
|
3045 |
Font[] fonts = new Font[nameTable.size()]; |
|
3046 |
int i=0; |
|
3047 |
for (Font2D font2D : nameTable.values()) { |
|
3048 |
fonts[i++] = new Font(font2D.getFontName(l), Font.PLAIN, 1); |
|
3049 |
} |
|
3050 |
return fonts; |
|
3051 |
} |
|
3052 |
} |
|
3053 |
||
3054 |
/* Begin support for GTK Look and Feel - query libfontconfig and |
|
3055 |
* return a composite Font to Swing that uses the desktop font(s). |
|
3056 |
*/ |
|
3057 |
||
3058 |
/* A small "map" from GTK/fontconfig names to the equivalent JDK |
|
3059 |
* logical font name. |
|
3060 |
*/ |
|
3061 |
private static final String[][] nameMap = { |
|
3062 |
{"sans", "sansserif"}, |
|
3063 |
{"sans-serif", "sansserif"}, |
|
3064 |
{"serif", "serif"}, |
|
3065 |
{"monospace", "monospaced"} |
|
3066 |
}; |
|
3067 |
||
3068 |
public static String mapFcName(String name) { |
|
3069 |
for (int i = 0; i < nameMap.length; i++) { |
|
3070 |
if (name.equals(nameMap[i][0])) { |
|
3071 |
return nameMap[i][1]; |
|
3072 |
} |
|
3073 |
} |
|
3074 |
return null; |
|
3075 |
} |
|
3076 |
||
3077 |
/* fontconfig recognises slants roman, italic, as well as oblique, |
|
3078 |
* and a slew of weights, where the ones that matter here are |
|
3079 |
* regular and bold. |
|
3080 |
* To fully qualify what we want, we can for example ask for (eg) |
|
3081 |
* Font.PLAIN : "serif:regular:roman" |
|
3082 |
* Font.BOLD : "serif:bold:roman" |
|
3083 |
* Font.ITALIC : "serif:regular:italic" |
|
3084 |
* Font.BOLD|Font.ITALIC : "serif:bold:italic" |
|
3085 |
*/ |
|
3086 |
private static String[] fontConfigNames = { |
|
3087 |
"sans:regular:roman", |
|
3088 |
"sans:bold:roman", |
|
3089 |
"sans:regular:italic", |
|
3090 |
"sans:bold:italic", |
|
3091 |
||
3092 |
"serif:regular:roman", |
|
3093 |
"serif:bold:roman", |
|
3094 |
"serif:regular:italic", |
|
3095 |
"serif:bold:italic", |
|
3096 |
||
3097 |
"monospace:regular:roman", |
|
3098 |
"monospace:bold:roman", |
|
3099 |
"monospace:regular:italic", |
|
3100 |
"monospace:bold:italic", |
|
3101 |
}; |
|
3102 |
||
3103 |
/* This class is just a data structure. |
|
3104 |
*/ |
|
3105 |
private static class FontConfigInfo { |
|
3106 |
String fcName; // eg sans |
|
3107 |
String fcFamily; // eg sans |
|
3108 |
String jdkName; // eg sansserif |
|
3109 |
int style; // eg 0=PLAIN |
|
3110 |
String familyName; // eg Bitstream Vera Sans |
|
3111 |
String fontFile; // eg /usr/X11/lib/fonts/foo.ttf |
|
3112 |
//boolean preferBitmaps; // if embedded bitmaps preferred over AA |
|
3113 |
CompositeFont compFont; // null if not yet created/known. |
|
3114 |
} |
|
3115 |
||
3116 |
||
3117 |
private static String getFCLocaleStr() { |
|
3118 |
Locale l = SunToolkit.getStartupLocale(); |
|
3119 |
String localeStr = l.getLanguage(); |
|
3120 |
String country = l.getCountry(); |
|
3121 |
if (!country.equals("")) { |
|
3122 |
localeStr = localeStr + "-" + country; |
|
3123 |
} |
|
3124 |
return localeStr; |
|
3125 |
} |
|
3126 |
||
3127 |
private static native int |
|
3128 |
getFontConfigAASettings(String locale, String fcFamily); |
|
3129 |
||
3130 |
/* This is public solely so that for debugging purposes it can be called |
|
3131 |
* with other names, which might (eg) include a size, eg "sans-24" |
|
3132 |
* The return value is a text aa rendering hint value. |
|
3133 |
* Normally we should call the no-args version. |
|
3134 |
*/ |
|
3135 |
public static Object getFontConfigAAHint(String fcFamily) { |
|
3136 |
if (isWindows) { |
|
3137 |
return null; |
|
3138 |
} else { |
|
3139 |
int hint = getFontConfigAASettings(getFCLocaleStr(), fcFamily); |
|
3140 |
if (hint < 0) { |
|
3141 |
return null; |
|
3142 |
} else { |
|
3143 |
return SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING, |
|
3144 |
hint); |
|
3145 |
} |
|
3146 |
} |
|
3147 |
} |
|
3148 |
||
3149 |
/* Called from code that needs to know what are the AA settings |
|
3150 |
* that apps using FC would pick up for the default desktop font. |
|
3151 |
* Note apps can change the default desktop font. etc, so this |
|
3152 |
* isn't certain to be right but its going to correct for most cases. |
|
3153 |
* Native return values map to the text aa values in sun.awt.SunHints. |
|
3154 |
* which is used to look up the renderinghint value object. |
|
3155 |
*/ |
|
3156 |
public static Object getFontConfigAAHint() { |
|
3157 |
return getFontConfigAAHint("sans"); |
|
3158 |
} |
|
3159 |
||
3160 |
/* This array has the array elements created in Java code and is |
|
3161 |
* passed down to native to be filled in. |
|
3162 |
*/ |
|
3163 |
private static FontConfigInfo[] fontConfigFonts; |
|
3164 |
||
3165 |
/* Return an array of FontConfigInfo structs describing the primary |
|
3166 |
* font located for each of fontconfig/GTK/Pango's logical font names. |
|
3167 |
*/ |
|
3168 |
private static native void getFontConfig(String locale, |
|
3169 |
FontConfigInfo[] fonts); |
|
3170 |
||
3171 |
||
3172 |
/* This can be made public if it's needed to force a re-read |
|
3173 |
* rather than using the cached values. The re-read would be needed |
|
3174 |
* only if some event signalled that the fontconfig has changed. |
|
3175 |
* In that event this method would need to return directly the array |
|
3176 |
* to be used by the caller in case it subsequently changed. |
|
3177 |
*/ |
|
3178 |
private static void initFontConfigFonts() { |
|
3179 |
||
3180 |
if (fontConfigFonts != null) { |
|
3181 |
return; |
|
3182 |
} |
|
3183 |
||
3184 |
if (isWindows) { |
|
3185 |
return; |
|
3186 |
} |
|
3187 |
||
3188 |
long t0 = 0; |
|
3189 |
if (logging) { |
|
3190 |
t0 = System.currentTimeMillis(); |
|
3191 |
} |
|
3192 |
||
3193 |
FontConfigInfo[] fontArr = new FontConfigInfo[fontConfigNames.length]; |
|
3194 |
for (int i = 0; i< fontArr.length; i++) { |
|
3195 |
fontArr[i] = new FontConfigInfo(); |
|
3196 |
fontArr[i].fcName = fontConfigNames[i]; |
|
3197 |
int colonPos = fontArr[i].fcName.indexOf(':'); |
|
3198 |
fontArr[i].fcFamily = fontArr[i].fcName.substring(0, colonPos); |
|
3199 |
fontArr[i].jdkName = mapFcName(fontArr[i].fcFamily); |
|
3200 |
fontArr[i].style = i % 4; // depends on array order. |
|
3201 |
} |
|
3202 |
getFontConfig(getFCLocaleStr(), fontArr); |
|
3203 |
fontConfigFonts = fontArr; |
|
3204 |
||
3205 |
if (logging) { |
|
3206 |
long t1 = System.currentTimeMillis(); |
|
3207 |
logger.info("Time spent accessing fontconfig="+(t1-t0)+"ms."); |
|
3208 |
||
3209 |
for (int i = 0; i< fontConfigFonts.length; i++) { |
|
3210 |
FontConfigInfo fci = fontConfigFonts[i]; |
|
3211 |
logger.info("FC font " + fci.fcName+" maps to family " + |
|
3212 |
fci.familyName + " in file " + fci.fontFile); |
|
3213 |
} |
|
3214 |
} |
|
3215 |
} |
|
3216 |
||
3217 |
private static PhysicalFont registerFromFcInfo(FontConfigInfo fcInfo) { |
|
3218 |
||
3219 |
/* If it's a TTC file we need to know that as we will need to |
|
3220 |
* make sure we return the right font */ |
|
3221 |
int offset = fcInfo.fontFile.length()-4; |
|
3222 |
if (offset <= 0) { |
|
3223 |
return null; |
|
3224 |
} |
|
3225 |
String ext = fcInfo.fontFile.substring(offset).toLowerCase(); |
|
3226 |
boolean isTTC = ext.equals(".ttc"); |
|
3227 |
||
3228 |
/* If this file is already registered, can just return its font. |
|
3229 |
* However we do need to check in case it's a TTC as we need |
|
3230 |
* a specific font, so rather than directly returning it, let |
|
3231 |
* findFont2D resolve that. |
|
3232 |
*/ |
|
3233 |
PhysicalFont physFont = registeredFontFiles.get(fcInfo.fontFile); |
|
3234 |
if (physFont != null) { |
|
3235 |
if (isTTC) { |
|
3236 |
Font2D f2d = findFont2D(fcInfo.familyName, |
|
3237 |
fcInfo.style, NO_FALLBACK); |
|
3238 |
if (f2d instanceof PhysicalFont) { /* paranoia */ |
|
3239 |
return (PhysicalFont)f2d; |
|
3240 |
} else { |
|
3241 |
return null; |
|
3242 |
} |
|
3243 |
} else { |
|
3244 |
return physFont; |
|
3245 |
} |
|
3246 |
} |
|
3247 |
||
3248 |
/* If the font may hide a JRE font (eg fontconfig says it is |
|
3249 |
* Lucida Sans), we want to use the JRE version, so make it |
|
3250 |
* point to the JRE font. |
|
3251 |
*/ |
|
3252 |
physFont = findJREDeferredFont(fcInfo.familyName, fcInfo.style); |
|
3253 |
||
3254 |
/* It is also possible the font file is on the "deferred" list, |
|
3255 |
* in which case we can just initialise it now. |
|
3256 |
*/ |
|
3257 |
if (physFont == null && |
|
3258 |
deferredFontFiles.get(fcInfo.fontFile) != null) { |
|
3259 |
physFont = initialiseDeferredFont(fcInfo.fontFile); |
|
3260 |
/* use findFont2D to get the right font from TTC's */ |
|
3261 |
if (physFont != null) { |
|
3262 |
if (isTTC) { |
|
3263 |
Font2D f2d = findFont2D(fcInfo.familyName, |
|
3264 |
fcInfo.style, NO_FALLBACK); |
|
3265 |
if (f2d instanceof PhysicalFont) { /* paranoia */ |
|
3266 |
return (PhysicalFont)f2d; |
|
3267 |
} else { |
|
3268 |
return null; |
|
3269 |
} |
|
3270 |
} else { |
|
3271 |
return physFont; |
|
3272 |
} |
|
3273 |
} |
|
3274 |
} |
|
3275 |
||
3276 |
/* In the majority of cases we reach here, and need to determine |
|
3277 |
* the type and rank to register the font. |
|
3278 |
*/ |
|
3279 |
if (physFont == null) { |
|
3280 |
int fontFormat = FONTFORMAT_NONE; |
|
3281 |
int fontRank = Font2D.UNKNOWN_RANK; |
|
3282 |
||
3283 |
if (ext.equals(".ttf") || isTTC) { |
|
3284 |
fontFormat = FONTFORMAT_TRUETYPE; |
|
3285 |
fontRank = Font2D.TTF_RANK; |
|
3286 |
} else if (ext.equals(".pfa") || ext.equals(".pfb")) { |
|
3287 |
fontFormat = FONTFORMAT_TYPE1; |
|
3288 |
fontRank = Font2D.TYPE1_RANK; |
|
3289 |
} |
|
3290 |
physFont = registerFontFile(fcInfo.fontFile, null, |
|
3291 |
fontFormat, true, fontRank); |
|
3292 |
} |
|
3293 |
return physFont; |
|
3294 |
} |
|
3295 |
||
3296 |
private static String[] getPlatformFontDirs() { |
|
3297 |
String path = getFontPath(true); |
|
3298 |
StringTokenizer parser = |
|
3299 |
new StringTokenizer(path, File.pathSeparator); |
|
3300 |
ArrayList<String> pathList = new ArrayList<String>(); |
|
3301 |
try { |
|
3302 |
while (parser.hasMoreTokens()) { |
|
3303 |
pathList.add(parser.nextToken()); |
|
3304 |
} |
|
3305 |
} catch (NoSuchElementException e) { |
|
3306 |
} |
|
3307 |
return pathList.toArray(new String[0]); |
|
3308 |
} |
|
3309 |
||
3310 |
/** returns an array of two strings. The first element is the |
|
3311 |
* name of the font. The second element is the file name. |
|
3312 |
*/ |
|
3313 |
private static String[] defaultPlatformFont = null; |
|
3314 |
public static String[] getDefaultPlatformFont() { |
|
3315 |
||
3316 |
if (defaultPlatformFont != null) { |
|
3317 |
return defaultPlatformFont; |
|
3318 |
} |
|
3319 |
||
3320 |
String[] info = new String[2]; |
|
3321 |
if (isWindows) { |
|
3322 |
info[0] = "Arial"; |
|
3323 |
info[1] = "c:\\windows\\fonts"; |
|
3324 |
final String[] dirs = getPlatformFontDirs(); |
|
3325 |
if (dirs.length > 1) { |
|
3326 |
String dir = (String) |
|
3327 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
3328 |
public Object run() { |
|
3329 |
for (int i=0; i<dirs.length; i++) { |
|
3330 |
String path = |
|
3331 |
dirs[i] + File.separator + "arial.ttf"; |
|
3332 |
File file = new File(path); |
|
3333 |
if (file.exists()) { |
|
3334 |
return dirs[i]; |
|
3335 |
} |
|
3336 |
} |
|
3337 |
return null; |
|
3338 |
} |
|
3339 |
}); |
|
3340 |
if (dir != null) { |
|
3341 |
info[1] = dir; |
|
3342 |
} |
|
3343 |
} else { |
|
3344 |
info[1] = dirs[0]; |
|
3345 |
} |
|
3346 |
info[1] = info[1] + File.separator + "arial.ttf"; |
|
3347 |
} else { |
|
3348 |
initFontConfigFonts(); |
|
3349 |
for (int i=0; i<fontConfigFonts.length; i++) { |
|
3350 |
if ("sans".equals(fontConfigFonts[i].fcFamily) && |
|
3351 |
0 == fontConfigFonts[i].style) { |
|
3352 |
info[0] = fontConfigFonts[i].familyName; |
|
3353 |
info[1] = fontConfigFonts[i].fontFile; |
|
3354 |
break; |
|
3355 |
} |
|
3356 |
} |
|
3357 |
/* Absolute last ditch attempt in the face of fontconfig problems. |
|
3358 |
* If we didn't match, pick the first, or just make something |
|
3359 |
* up so we don't NPE. |
|
3360 |
*/ |
|
3361 |
if (info[0] == null) { |
|
3362 |
if (fontConfigFonts.length > 0 && |
|
3363 |
fontConfigFonts[0].fontFile != null) { |
|
3364 |
info[0] = fontConfigFonts[0].familyName; |
|
3365 |
info[1] = fontConfigFonts[0].fontFile; |
|
3366 |
} else { |
|
3367 |
info[0] = "Dialog"; |
|
3368 |
info[1] = "/dialog.ttf"; |
|
3369 |
} |
|
3370 |
} |
|
3371 |
} |
|
3372 |
defaultPlatformFont = info; |
|
3373 |
return defaultPlatformFont; |
|
3374 |
} |
|
3375 |
||
3376 |
private FontConfigInfo getFontConfigInfo() { |
|
3377 |
initFontConfigFonts(); |
|
3378 |
for (int i=0; i<fontConfigFonts.length; i++) { |
|
3379 |
if ("sans".equals(fontConfigFonts[i].fcFamily) && |
|
3380 |
0 == fontConfigFonts[i].style) { |
|
3381 |
return fontConfigFonts[i]; |
|
3382 |
} |
|
3383 |
} |
|
3384 |
return null; |
|
3385 |
} |
|
3386 |
/* |
|
3387 |
* We need to return a Composite font which has as the font in |
|
3388 |
* its first slot one obtained from fontconfig. |
|
3389 |
*/ |
|
3390 |
private static CompositeFont getFontConfigFont(String name, int style) { |
|
3391 |
||
3392 |
name = name.toLowerCase(); |
|
3393 |
||
3394 |
initFontConfigFonts(); |
|
3395 |
||
3396 |
FontConfigInfo fcInfo = null; |
|
3397 |
for (int i=0; i<fontConfigFonts.length; i++) { |
|
3398 |
if (name.equals(fontConfigFonts[i].fcFamily) && |
|
3399 |
style == fontConfigFonts[i].style) { |
|
3400 |
fcInfo = fontConfigFonts[i]; |
|
3401 |
break; |
|
3402 |
} |
|
3403 |
} |
|
3404 |
if (fcInfo == null) { |
|
3405 |
fcInfo = fontConfigFonts[0]; |
|
3406 |
} |
|
3407 |
||
3408 |
if (logging) { |
|
3409 |
logger.info("FC name=" + name + " style=" + style + " uses " + |
|
3410 |
fcInfo.familyName + " in file: " + fcInfo.fontFile); |
|
3411 |
} |
|
3412 |
||
3413 |
if (fcInfo.compFont != null) { |
|
3414 |
return fcInfo.compFont; |
|
3415 |
} |
|
3416 |
||
3417 |
/* jdkFont is going to be used for slots 1..N and as a fallback. |
|
3418 |
* Slot 0 will be the physical font from fontconfig. |
|
3419 |
*/ |
|
3420 |
CompositeFont jdkFont = (CompositeFont) |
|
3421 |
findFont2D(fcInfo.jdkName, style, LOGICAL_FALLBACK); |
|
3422 |
||
3423 |
if (fcInfo.familyName == null || fcInfo.fontFile == null) { |
|
3424 |
return (fcInfo.compFont = jdkFont); |
|
3425 |
} |
|
3426 |
||
3427 |
/* First, see if the family and exact style is already registered. |
|
3428 |
* If it is, use it. If it's not, then try to register it. |
|
3429 |
* If that registration fails (signalled by null) just return the |
|
3430 |
* regular JDK composite. |
|
3431 |
* Algorithmically styled fonts won't match on exact style, so |
|
3432 |
* will fall through this code, but the regisration code will |
|
3433 |
* find that file already registered and return its font. |
|
3434 |
*/ |
|
3435 |
FontFamily family = FontFamily.getFamily(fcInfo.familyName); |
|
3436 |
PhysicalFont physFont = null; |
|
3437 |
if (family != null) { |
|
3438 |
Font2D f2D = family.getFontWithExactStyleMatch(fcInfo.style); |
|
3439 |
if (f2D instanceof PhysicalFont) { |
|
3440 |
physFont = (PhysicalFont)f2D; |
|
3441 |
} |
|
3442 |
} |
|
3443 |
||
3444 |
if (physFont == null || !fcInfo.fontFile.equals(physFont.platName)) { |
|
3445 |
physFont = registerFromFcInfo(fcInfo); |
|
3446 |
if (physFont == null) { |
|
3447 |
return (fcInfo.compFont = jdkFont); |
|
3448 |
} |
|
3449 |
family = FontFamily.getFamily(physFont.getFamilyName(null)); |
|
3450 |
} |
|
3451 |
||
3452 |
/* Now register the fonts in the family (the other styles) after |
|
3453 |
* checking that they aren't already registered and are actually in |
|
3454 |
* a different file. They may be the same file in CJK cases. |
|
3455 |
* For cases where they are different font files - eg as is common for |
|
3456 |
* Latin fonts, then we rely on fontconfig to report these correctly. |
|
3457 |
* Assume that all styles of this font are found by fontconfig, |
|
3458 |
* so we can find all the family members which must be registered |
|
3459 |
* together to prevent synthetic styling. |
|
3460 |
*/ |
|
3461 |
for (int i=0; i<fontConfigFonts.length; i++) { |
|
3462 |
FontConfigInfo fc = fontConfigFonts[i]; |
|
3463 |
if (fc != fcInfo && |
|
3464 |
physFont.getFamilyName(null).equals(fc.familyName) && |
|
3465 |
!fc.fontFile.equals(physFont.platName) && |
|
3466 |
family.getFontWithExactStyleMatch(fc.style) == null) { |
|
3467 |
||
3468 |
registerFromFcInfo(fontConfigFonts[i]); |
|
3469 |
} |
|
3470 |
} |
|
3471 |
||
3472 |
/* Now we have a physical font. We will back this up with the JDK |
|
3473 |
* logical font (sansserif, serif, or monospaced) that corresponds |
|
3474 |
* to the Pango/GTK/FC logical font name. |
|
3475 |
*/ |
|
3476 |
return (fcInfo.compFont = new CompositeFont(physFont, jdkFont)); |
|
3477 |
} |
|
3478 |
||
3479 |
/* This is called by Swing passing in a fontconfig family name |
|
3480 |
* such as "sans". In return Swing gets a FontUIResource instance |
|
3481 |
* that has queried fontconfig to resolve the font(s) used for this. |
|
3482 |
* Fontconfig will if asked return a list of fonts to give the largest |
|
3483 |
* possible code point coverage. |
|
3484 |
* For now we use only the first font returned by fontconfig, and |
|
3485 |
* back it up with the most closely matching JDK logical font. |
|
3486 |
* Essentially this means pre-pending what we return now with fontconfig's |
|
3487 |
* preferred physical font. This could lead to some duplication in cases, |
|
3488 |
* if we already included that font later. We probably should remove such |
|
3489 |
* duplicates, but it is not a significant problem. It can be addressed |
|
3490 |
* later as part of creating a Composite which uses more of the |
|
3491 |
* same fonts as fontconfig. At that time we also should pay more |
|
3492 |
* attention to the special rendering instructions fontconfig returns, |
|
3493 |
* such as whether we should prefer embedded bitmaps over antialiasing. |
|
3494 |
* There's no way to express that via a Font at present. |
|
3495 |
*/ |
|
3496 |
public static FontUIResource getFontConfigFUIR(String fcFamily, |
|
3497 |
int style, int size) { |
|
3498 |
||
3499 |
String mappedName = mapFcName(fcFamily); |
|
3500 |
if (mappedName == null) { |
|
3501 |
mappedName = "sansserif"; |
|
3502 |
} |
|
3503 |
||
3504 |
/* If GTK L&F were to be used on windows, we need to return |
|
3505 |
* something. Since on windows Swing won't have the code to |
|
3506 |
* call fontconfig, even if it is present, fcFamily and mapped |
|
3507 |
* name will default to sans and therefore sansserif so this |
|
3508 |
* should be fine. |
|
3509 |
*/ |
|
3510 |
if (isWindows) { |
|
3511 |
return new FontUIResource(mappedName, style, size); |
|
3512 |
} |
|
3513 |
||
3514 |
CompositeFont font2D = getFontConfigFont(fcFamily, style); |
|
3515 |
if (font2D == null) { // Not expected, just a precaution. |
|
3516 |
return new FontUIResource(mappedName, style, size); |
|
3517 |
} |
|
3518 |
||
3519 |
/* The name of the font will be that of the physical font in slot, |
|
3520 |
* but by setting the handle to that of the CompositeFont it |
|
3521 |
* renders as that CompositeFont. |
|
3522 |
* It also needs to be marked as a created font which is the |
|
3523 |
* current mechanism to signal that deriveFont etc must copy |
|
3524 |
* the handle from the original font. |
|
3525 |
*/ |
|
3526 |
FontUIResource fuir = |
|
3527 |
new FontUIResource(font2D.getFamilyName(null), style, size); |
|
3528 |
setFont2D(fuir, font2D.handle); |
|
3529 |
setCreatedFont(fuir); |
|
3530 |
return fuir; |
|
3531 |
} |
|
3532 |
||
3533 |
/* The following fields and methods which relate to layout |
|
3534 |
* perhaps belong in some other class but FontManager is already |
|
3535 |
* widely used as an entry point for other JDK code that needs |
|
3536 |
* access to the font system internals. |
|
3537 |
*/ |
|
3538 |
||
3539 |
/** |
|
3540 |
* Referenced by code in the JDK which wants to test for the |
|
3541 |
* minimum char code for which layout may be required. |
|
3542 |
* Note that even basic latin text can benefit from ligatures, |
|
3543 |
* eg "ffi" but we presently apply those only if explicitly |
|
3544 |
* requested with TextAttribute.LIGATURES_ON. |
|
3545 |
* The value here indicates the lowest char code for which failing |
|
3546 |
* to invoke layout would prevent acceptable rendering. |
|
3547 |
*/ |
|
3548 |
public static final int MIN_LAYOUT_CHARCODE = 0x0300; |
|
3549 |
||
3550 |
/** |
|
3551 |
* Referenced by code in the JDK which wants to test for the |
|
3552 |
* maximum char code for which layout may be required. |
|
3553 |
* Note this does not account for supplementary characters |
|
3554 |
* where the caller interprets 'layout' to mean any case where |
|
3555 |
* one 'char' (ie the java type char) does not map to one glyph |
|
3556 |
*/ |
|
3557 |
public static final int MAX_LAYOUT_CHARCODE = 0x206F; |
|
3558 |
||
3559 |
/* If the character code falls into any of a number of unicode ranges |
|
3560 |
* where we know that simple left->right layout mapping chars to glyphs |
|
3561 |
* 1:1 and accumulating advances is going to produce incorrect results, |
|
3562 |
* we want to know this so the caller can use a more intelligent layout |
|
3563 |
* approach. A caller who cares about optimum performance may want to |
|
3564 |
* check the first case and skip the method call if its in that range. |
|
3565 |
* Although there's a lot of tests in here, knowing you can skip |
|
3566 |
* CTL saves a great deal more. The rest of the checks are ordered |
|
3567 |
* so that rather than checking explicitly if (>= start & <= end) |
|
3568 |
* which would mean all ranges would need to be checked so be sure |
|
3569 |
* CTL is not needed, the method returns as soon as it recognises |
|
3570 |
* the code point is outside of a CTL ranges. |
|
3571 |
* NOTE: Since this method accepts an 'int' it is asssumed to properly |
|
3572 |
* represent a CHARACTER. ie it assumes the caller has already |
|
3573 |
* converted surrogate pairs into supplementary characters, and so |
|
3574 |
* can handle this case and doesn't need to be told such a case is |
|
3575 |
* 'complex'. |
|
3576 |
*/ |
|
3577 |
static boolean isComplexCharCode(int code) { |
|
3578 |
||
3579 |
if (code < MIN_LAYOUT_CHARCODE || code > MAX_LAYOUT_CHARCODE) { |
|
3580 |
return false; |
|
3581 |
} |
|
3582 |
else if (code <= 0x036f) { |
|
3583 |
// Trigger layout for combining diacriticals 0x0300->0x036f |
|
3584 |
return true; |
|
3585 |
} |
|
3586 |
else if (code < 0x0590) { |
|
3587 |
// No automatic layout for Greek, Cyrillic, Armenian. |
|
3588 |
return false; |
|
3589 |
} |
|
3590 |
else if (code <= 0x06ff) { |
|
3591 |
// Hebrew 0590 - 05ff |
|
3592 |
// Arabic 0600 - 06ff |
|
3593 |
return true; |
|
3594 |
} |
|
3595 |
else if (code < 0x0900) { |
|
3596 |
return false; // Syriac and Thaana |
|
3597 |
} |
|
3598 |
else if (code <= 0x0e7f) { |
|
3599 |
// if Indic, assume shaping for conjuncts, reordering: |
|
3600 |
// 0900 - 097F Devanagari |
|
3601 |
// 0980 - 09FF Bengali |
|
3602 |
// 0A00 - 0A7F Gurmukhi |
|
3603 |
// 0A80 - 0AFF Gujarati |
|
3604 |
// 0B00 - 0B7F Oriya |
|
3605 |
// 0B80 - 0BFF Tamil |
|
3606 |
// 0C00 - 0C7F Telugu |
|
3607 |
// 0C80 - 0CFF Kannada |
|
3608 |
// 0D00 - 0D7F Malayalam |
|
3609 |
// 0D80 - 0DFF Sinhala |
|
3610 |
// 0E00 - 0E7F if Thai, assume shaping for vowel, tone marks |
|
3611 |
return true; |
|
3612 |
} |
|
3613 |
else if (code < 0x1780) { |
|
3614 |
return false; |
|
3615 |
} |
|
3616 |
else if (code <= 0x17ff) { // 1780 - 17FF Khmer |
|
3617 |
return true; |
|
3618 |
} |
|
3619 |
else if (code < 0x200c) { |
|
3620 |
return false; |
|
3621 |
} |
|
3622 |
else if (code <= 0x200d) { // zwj or zwnj |
|
3623 |
return true; |
|
3624 |
} |
|
3625 |
else if (code >= 0x202a && code <= 0x202e) { // directional control |
|
3626 |
return true; |
|
3627 |
} |
|
3628 |
else if (code >= 0x206a && code <= 0x206f) { // directional control |
|
3629 |
return true; |
|
3630 |
} |
|
3631 |
return false; |
|
3632 |
} |
|
3633 |
||
3634 |
/* This is almost the same as the method above, except it takes a |
|
3635 |
* char which means it may include undecoded surrogate pairs. |
|
3636 |
* The distinction is made so that code which needs to identify all |
|
3637 |
* cases in which we do not have a simple mapping from |
|
3638 |
* char->unicode character->glyph can be be identified. |
|
3639 |
* For example measurement cannot simply sum advances of 'chars', |
|
3640 |
* the caret in editable text cannot advance one 'char' at a time, etc. |
|
3641 |
* These callers really are asking for more than whether 'layout' |
|
3642 |
* needs to be run, they need to know if they can assume 1->1 |
|
3643 |
* char->glyph mapping. |
|
3644 |
*/ |
|
3645 |
static boolean isNonSimpleChar(char ch) { |
|
3646 |
return |
|
3647 |
isComplexCharCode(ch) || |
|
3648 |
(ch >= CharToGlyphMapper.HI_SURROGATE_START && |
|
3649 |
ch <= CharToGlyphMapper.LO_SURROGATE_END); |
|
3650 |
} |
|
3651 |
||
3652 |
/** |
|
3653 |
* If there is anything in the text which triggers a case |
|
3654 |
* where char->glyph does not map 1:1 in straightforward |
|
3655 |
* left->right ordering, then this method returns true. |
|
3656 |
* Scripts which might require it but are not treated as such |
|
3657 |
* due to JDK implementations will not return true. |
|
3658 |
* ie a 'true' return is an indication of the treatment by |
|
3659 |
* the implementation. |
|
3660 |
* Whether supplementary characters should be considered is dependent |
|
3661 |
* on the needs of the caller. Since this method accepts the 'char' type |
|
3662 |
* then such chars are always represented by a pair. From a rendering |
|
3663 |
* perspective these will all (in the cases I know of) still be one |
|
3664 |
* unicode character -> one glyph. But if a caller is using this to |
|
3665 |
* discover any case where it cannot make naive assumptions about |
|
3666 |
* the number of chars, and how to index through them, then it may |
|
3667 |
* need the option to have a 'true' return in such a case. |
|
3668 |
*/ |
|
3669 |
public static boolean isComplexText(char [] chs, int start, int limit) { |
|
3670 |
||
3671 |
for (int i = start; i < limit; i++) { |
|
3672 |
if (chs[i] < MIN_LAYOUT_CHARCODE) { |
|
3673 |
continue; |
|
3674 |
} |
|
3675 |
else if (isNonSimpleChar(chs[i])) { |
|
3676 |
return true; |
|
3677 |
} |
|
3678 |
} |
|
3679 |
return false; |
|
3680 |
} |
|
3681 |
||
3682 |
/** |
|
3683 |
* Used by windows printing to assess if a font is likely to |
|
3684 |
* be layout compatible with JDK |
|
3685 |
* TrueType fonts should be, but if they have no GPOS table, |
|
3686 |
* but do have a GSUB table, then they are probably older |
|
3687 |
* fonts GDI handles differently. |
|
3688 |
*/ |
|
3689 |
public static boolean textLayoutIsCompatible(Font font) { |
|
3690 |
||
3691 |
Font2D font2D = FontManager.getFont2D(font); |
|
3692 |
if (font2D instanceof TrueTypeFont) { |
|
3693 |
TrueTypeFont ttf = (TrueTypeFont)font2D; |
|
3694 |
return |
|
3695 |
ttf.getDirectoryEntry(TrueTypeFont.GSUBTag) == null || |
|
3696 |
ttf.getDirectoryEntry(TrueTypeFont.GPOSTag) != null; |
|
3697 |
} else { |
|
3698 |
return false; |
|
3699 |
} |
|
3700 |
} |
|
3701 |
||
3702 |
private static FontScaler nullScaler = null; |
|
3703 |
private static Constructor<FontScaler> scalerConstructor = null; |
|
3704 |
||
3705 |
//Find preferred font scaler |
|
3706 |
// |
|
3707 |
//NB: we can allow property based preferences |
|
3708 |
// (theoretically logic can be font type specific) |
|
3709 |
static { |
|
3710 |
Class scalerClass = null; |
|
3711 |
Class arglst[] = new Class[] {Font2D.class, int.class, |
|
3712 |
boolean.class, int.class}; |
|
3713 |
||
3714 |
try { |
|
3715 |
if (SunGraphicsEnvironment.isOpenJDK()) { |
|
3716 |
scalerClass = Class.forName("sun.font.FreetypeFontScaler"); |
|
3717 |
} else { |
|
3718 |
scalerClass = Class.forName("sun.font.T2KFontScaler"); |
|
3719 |
} |
|
3720 |
} catch (ClassNotFoundException e) { |
|
3721 |
scalerClass = NullFontScaler.class; |
|
3722 |
} |
|
3723 |
||
3724 |
//NB: rewrite using factory? constructor is ugly way |
|
3725 |
try { |
|
3726 |
scalerConstructor = scalerClass.getConstructor(arglst); |
|
3727 |
} catch (NoSuchMethodException e) { |
|
3728 |
//should not happen |
|
3729 |
} |
|
3730 |
} |
|
3731 |
||
3732 |
/* At the moment it is harmless to create 2 null scalers |
|
3733 |
so, technically, syncronized keyword is not needed. |
|
3734 |
||
3735 |
But it is safer to keep it to avoid subtle problems if we will be |
|
3736 |
adding checks like whether scaler is null scaler. */ |
|
3737 |
public synchronized static FontScaler getNullScaler() { |
|
3738 |
if (nullScaler == null) { |
|
3739 |
nullScaler = new NullFontScaler(); |
|
3740 |
} |
|
3741 |
return nullScaler; |
|
3742 |
} |
|
3743 |
||
3744 |
/* This is the only place to instantiate new FontScaler. |
|
3745 |
* Therefore this is very convinient place to register |
|
3746 |
* scaler with Disposer as well as trigger deregistring bad font |
|
3747 |
* in case when scaler reports this. |
|
3748 |
*/ |
|
3749 |
||
3750 |
public static FontScaler getScaler(Font2D font, |
|
3751 |
int indexInCollection, |
|
3752 |
boolean supportsCJK, |
|
3753 |
int filesize) { |
|
3754 |
FontScaler scaler = null; |
|
3755 |
||
3756 |
try { |
|
3757 |
Object args[] = new Object[] {font, indexInCollection, |
|
3758 |
supportsCJK, filesize}; |
|
3759 |
scaler = scalerConstructor.newInstance(args); |
|
3760 |
Disposer.addObjectRecord(font, scaler); |
|
3761 |
} catch (Throwable e) { |
|
3762 |
scaler = nullScaler; |
|
3763 |
||
3764 |
//if we can not instantiate scaler assume bad font |
|
3765 |
//NB: technically it could be also because of internal scaler |
|
3766 |
// error but here we are assuming scaler is ok. |
|
3767 |
deRegisterBadFont(font); |
|
3768 |
} |
|
3769 |
return scaler; |
|
3770 |
} |
|
3771 |
} |