/
PrismFontFactory.java
2005 lines (1822 loc) · 77.3 KB
/
PrismFontFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.font;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import com.sun.glass.ui.Screen;
import com.sun.glass.utils.NativeLibLoader;
import com.sun.javafx.PlatformUtil;
import com.sun.javafx.text.GlyphLayout;
import static com.sun.javafx.FXPermissions.LOAD_FONT_PERMISSION;
public abstract class PrismFontFactory implements FontFactory {
public static final boolean debugFonts;
public static final boolean isWindows;
public static final boolean isLinux;
public static final boolean isMacOSX;
public static final boolean isIOS;
public static final boolean isAndroid;
public static final boolean isEmbedded;
public static final int cacheLayoutSize;
private static int subPixelMode;
public static final int SUB_PIXEL_ON = 1;
public static final int SUB_PIXEL_Y = 2;
public static final int SUB_PIXEL_NATIVE = 4;
private static float fontSizeLimit = 80f;
private static boolean lcdEnabled;
private static float lcdContrast = -1;
private static String jreFontDir;
private static final String jreDefaultFont = "Lucida Sans Regular";
private static final String jreDefaultFontLC = "lucida sans regular";
private static final String jreDefaultFontFile = "LucidaSansRegular.ttf";
private static final String CT_FACTORY = "com.sun.javafx.font.coretext.CTFactory";
private static final String DW_FACTORY = "com.sun.javafx.font.directwrite.DWFactory";
private static final String FT_FACTORY = "com.sun.javafx.font.freetype.FTFactory";
/* We need two maps. One to hold pointers to the raw fonts, another
* to hold pointers to the composite resources. Top level look ups
* to createFont() will look first in the compResourceMap, and
* only go to the second map to create a wrapped resource.
* Logical Fonts are handled separately.
*/
HashMap<String, FontResource> fontResourceMap =
new HashMap<String, FontResource>();
HashMap<String, CompositeFontResource> compResourceMap =
new HashMap<String, CompositeFontResource>();
static {
isWindows = PlatformUtil.isWindows();
isMacOSX = PlatformUtil.isMac();
isLinux = PlatformUtil.isLinux();
isIOS = PlatformUtil.isIOS();
isAndroid = PlatformUtil.isAndroid();
isEmbedded = PlatformUtil.isEmbedded();
int[] tempCacheLayoutSize = {0x10000};
debugFonts = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) () -> {
NativeLibLoader.loadLibrary("javafx_font");
String dbg = System.getProperty("prism.debugfonts", "");
boolean debug = "true".equals(dbg);
jreFontDir = getJDKFontDir();
String s = System.getProperty("com.sun.javafx.fontSize");
systemFontSize = -1f;
if (s != null) {
try {
systemFontSize = Float.parseFloat(s);
} catch (NumberFormatException nfe) {
System.err.println("Cannot parse font size '"
+ s + "'");
}
}
s = System.getProperty("prism.subpixeltext", "on");
if (s.indexOf("on") != -1 || s.indexOf("true") != -1) {
subPixelMode = SUB_PIXEL_ON;
}
if (s.indexOf("native") != -1) {
subPixelMode |= SUB_PIXEL_NATIVE | SUB_PIXEL_ON;
}
if (s.indexOf("vertical") != -1) {
subPixelMode |= SUB_PIXEL_Y | SUB_PIXEL_NATIVE | SUB_PIXEL_ON;
}
s = System.getProperty("prism.fontSizeLimit");
if (s != null) {
try {
fontSizeLimit = Float.parseFloat(s);
if (fontSizeLimit <= 0) {
fontSizeLimit = Float.POSITIVE_INFINITY;
}
} catch (NumberFormatException nfe) {
System.err.println("Cannot parse fontSizeLimit '" + s + "'");
}
}
boolean lcdTextOff = isIOS || isAndroid || isEmbedded;
String defLCDProp = lcdTextOff ? "false" : "true";
String lcdProp = System.getProperty("prism.lcdtext", defLCDProp);
lcdEnabled = lcdProp.equals("true");
s = System.getProperty("prism.cacheLayoutSize");
if (s != null) {
try {
tempCacheLayoutSize[0] = Integer.parseInt(s);
if (tempCacheLayoutSize[0] < 0) {
tempCacheLayoutSize[0] = 0;
}
} catch (NumberFormatException nfe) {
System.err.println("Cannot parse cache layout size '"
+ s + "'");
}
}
return debug;
}
);
cacheLayoutSize = tempCacheLayoutSize[0];
}
private static String getJDKFontDir() {
return System.getProperty("java.home","") + File.separator +
"lib" + File.separator + "fonts";
}
private static String getNativeFactoryName() {
if (isWindows) return DW_FACTORY;
if (isMacOSX || isIOS) return CT_FACTORY;
if (isLinux || isAndroid) return FT_FACTORY;
return null;
}
public static float getFontSizeLimit() {
return fontSizeLimit;
}
private static PrismFontFactory theFontFactory = null;
public static synchronized PrismFontFactory getFontFactory() {
if (theFontFactory != null) {
return theFontFactory;
}
String factoryClass = getNativeFactoryName();
if (factoryClass == null) {
throw new InternalError("cannot find a native font factory");
}
if (debugFonts) {
System.err.println("Loading FontFactory " + factoryClass);
if (subPixelMode != 0) {
String s = "Subpixel: enabled";
if ((subPixelMode & SUB_PIXEL_Y) != 0) {
s += ", vertical";
}
if ((subPixelMode & SUB_PIXEL_NATIVE) != 0) {
s += ", native";
}
System.err.println(s);
}
}
theFontFactory = getFontFactory(factoryClass);
if (theFontFactory == null) {
throw new InternalError("cannot load font factory: "+ factoryClass);
}
return theFontFactory;
}
private static synchronized PrismFontFactory getFontFactory(String factoryClass) {
try {
Class<?> clazz = Class.forName(factoryClass);
Method mid = clazz.getMethod("getFactory", (Class[])null);
return (PrismFontFactory)mid.invoke(null);
} catch (Throwable t) {
if (debugFonts) {
System.err.println("Loading font factory failed "+ factoryClass);
}
}
return null;
}
private HashMap<String, PrismFontFile>
fileNameToFontResourceMap = new HashMap<String, PrismFontFile>();
protected abstract PrismFontFile
createFontFile(String name, String filename,
int fIndex, boolean register,
boolean embedded,
boolean copy, boolean tracked)
throws Exception;
public abstract GlyphLayout createGlyphLayout();
// For an caller who has recognised a TTC file and wants to create
// the instances one at a time so as to have visibility into the
// contents of the TTC. Onus is on caller to enumerate all the fonts.
private PrismFontFile createFontResource(String filename, int index) {
return createFontResource(null, filename, index,
true, false, false, false);
}
private PrismFontFile createFontResource(String name,
String filename, int index,
boolean register, boolean embedded,
boolean copy, boolean tracked) {
String key = (filename+index).toLowerCase();
PrismFontFile fr = fileNameToFontResourceMap.get(key);
if (fr != null) {
return fr;
}
try {
fr = createFontFile(name, filename, index, register,
embedded, copy, tracked);
if (register) {
storeInMap(fr.getFullName(), fr);
fileNameToFontResourceMap.put(key, fr);
}
return fr;
} catch (Exception e) {
if (PrismFontFactory.debugFonts) {
e.printStackTrace();
}
return null;
}
}
private PrismFontFile createFontResource(String name, String filename) {
PrismFontFile[] pffArr =
createFontResources(name, filename,
true, false, false, false, false);
if (pffArr == null || pffArr.length == 0) {
return null;
} else {
return pffArr[0];
}
}
private PrismFontFile[] createFontResources(String name, String filename,
boolean register,
boolean embedded,
boolean copy,
boolean tracked,
boolean loadAll) {
PrismFontFile[] fArr = null;
if (filename == null) {
return null;
}
PrismFontFile fr = createFontResource(name, filename, 0, register,
embedded, copy, tracked);
if (fr == null) {
return null;
}
int cnt = (!loadAll) ? 1 : fr.getFontCount();
fArr = new PrismFontFile[cnt];
fArr[0] = fr;
if (cnt == 1) { // Not a TTC, or only requesting one font.
return fArr;
}
PrismFontFile.FileRefCounter rc = null;
if (copy) {
rc = fr.createFileRefCounter();
}
int index = 1;
do {
String key = (filename+index).toLowerCase();
try {
fr = fileNameToFontResourceMap.get(key);
if (fr != null) {
fArr[index] = fr;
continue;
} else {
fr = createFontFile(null, filename, index,
register, embedded,
copy, tracked);
if (fr == null) {
return null;
}
if (rc != null) {
fr.setAndIncFileRefCounter(rc);
}
fArr[index] = fr;
String fontname = fr.getFullName();
if (register) {
storeInMap(fontname, fr);
fileNameToFontResourceMap.put(key, fr);
}
}
} catch (Exception e) {
if (PrismFontFactory.debugFonts) {
e.printStackTrace();
}
return null;
}
} while (++index < cnt);
return fArr;
}
private String dotStyleStr(boolean bold, boolean italic) {
if (!bold) {
if (!italic) {
return "";
}
else {
return ".italic";
}
} else {
if (!italic) {
return ".bold";
}
else {
return ".bolditalic";
}
}
}
private void storeInMap(String name, FontResource resource) {
if (name == null || resource == null) {
return;
}
if (resource instanceof PrismCompositeFontResource) {
System.err.println(name + " is a composite " +
resource);
Thread.dumpStack();
return;
}
fontResourceMap.put(name.toLowerCase(), resource);
}
private ArrayList<WeakReference<PrismFontFile>> tmpFonts;
synchronized void addDecodedFont(PrismFontFile fr) {
fr.setIsDecoded(true);
addTmpFont(fr);
}
private synchronized void addTmpFont(PrismFontFile fr) {
if (tmpFonts == null) {
tmpFonts = new ArrayList<WeakReference<PrismFontFile>>();
}
WeakReference<PrismFontFile> ref;
/* Registered fonts are enumerable by the application and are
* expected to persist until VM shutdown.
* Other fonts - notably ones temporarily loaded in a web page via
* webview - should be eligible to be collected and have their
* temp files deleted at any time.
*/
if (fr.isRegistered()) {
ref = new WeakReference<PrismFontFile>(fr);
} else {
ref = fr.createFileDisposer(this, fr.getFileRefCounter());
}
tmpFonts.add(ref);
addFileCloserHook();
}
synchronized void removeTmpFont(WeakReference<PrismFontFile> ref) {
if (tmpFonts != null) {
tmpFonts.remove(ref);
}
}
/* familyName is expected to be a physical font family name.
*/
public synchronized FontResource getFontResource(String familyName,
boolean bold,
boolean italic,
boolean wantComp) {
if (familyName == null || familyName.isEmpty()) {
return null;
}
String lcFamilyName = familyName.toLowerCase();
String styleStr = dotStyleStr(bold, italic);
FontResource fr;
fr = lookupResource(lcFamilyName+styleStr, wantComp);
if (fr != null) {
return fr;
}
/* We may have registered this as an embedded font.
* In which case we should also try to locate it in
* the non-composite map before looking elsewhere.
* First look for a font with the exact styles specified.
* If that fails, look for any font in the family.
* Later on this should be a lot smarter about finding the best
* match, but that can wait until we have better style matching
* for all cases.
*/
if (embeddedFonts != null && wantComp) {
fr = lookupResource(lcFamilyName+styleStr, false);
if (fr != null) {
return new PrismCompositeFontResource(fr, lcFamilyName+styleStr);
}
for (PrismFontFile embeddedFont : embeddedFonts.values()) {
String lcEmFamily = embeddedFont.getFamilyName().toLowerCase();
if (lcEmFamily.equals(lcFamilyName)) {
return new PrismCompositeFontResource(embeddedFont,
lcFamilyName+styleStr);
}
}
}
/* We have hard coded some of the most commonly used Windows fonts
* so as to avoid the overhead of doing a lookup via GDI.
*/
if (isWindows) {
int style = ((bold ? 1 : 0)) + ((italic) ? 2 : 0);
String fontFile = WindowsFontMap.findFontFile(lcFamilyName, style);
if (fontFile != null) {
fr = createFontResource(null, fontFile);
if (fr != null) {
if (bold == fr.isBold() && italic == fr.isItalic() &&
!styleStr.isEmpty())
{
storeInMap(lcFamilyName+styleStr, fr);
}
if (wantComp) { // wrap with fallback support
fr = new PrismCompositeFontResource(fr,
lcFamilyName+styleStr);
}
return fr;
}
}
}
getFullNameToFileMap();
ArrayList<String> family = familyToFontListMap.get(lcFamilyName);
if (family == null) {
return null;
}
FontResource plainFR = null, boldFR = null,
italicFR = null, boldItalicFR = null;
for (String fontName : family) {
String lcFontName = fontName.toLowerCase();
fr = fontResourceMap.get(lcFontName);
if (fr == null) {
String file = findFile(lcFontName);
if (file != null) {
fr = getFontResource(fontName, file);
}
if (fr == null) {
continue;
}
storeInMap(lcFontName, fr);
}
if (bold == fr.isBold() && italic == fr.isItalic()) {
storeInMap(lcFamilyName+styleStr, fr);
if (wantComp) { // wrap with fallback support
fr = new PrismCompositeFontResource(fr,
lcFamilyName+styleStr);
}
return fr;
}
if (!fr.isBold()) {
if (!fr.isItalic()) {
plainFR = fr;
} else {
italicFR = fr;
}
} else {
if (!fr.isItalic()) {
boldFR = fr;
} else {
boldItalicFR = fr;
}
}
}
/* If get here, no perfect match in family. Substitute the
* closest one we found.
*/
if (!bold && !italic) {
if (boldFR != null) {
fr = boldFR;
} else if (italicFR != null) {
fr = italicFR;
} else {
fr = boldItalicFR;
}
} else if (bold && !italic) {
if (plainFR != null) {
fr = plainFR;
} else if (boldItalicFR != null) {
fr = boldItalicFR;
} else {
fr = italicFR;
}
} else if (!bold && italic) {
if (boldItalicFR != null) {
fr = boldItalicFR;
} else if (plainFR != null) {
fr = plainFR;
} else {
fr = boldFR;
}
} else /* (bold && italic) */ {
if (italicFR != null) {
fr = italicFR;
} else if (boldFR != null) {
fr = boldFR;
} else {
fr = plainFR;
}
}
if (fr != null) {
storeInMap(lcFamilyName+styleStr, fr);
if (wantComp) { // wrap with fallback support
fr = new PrismCompositeFontResource(fr, lcFamilyName+styleStr);
}
}
return fr;
}
public synchronized PGFont createFont(String familyName, boolean bold,
boolean italic, float size) {
FontResource fr = null;
if (familyName != null && !familyName.isEmpty()) {
PGFont logFont =
LogicalFont.getLogicalFont(familyName, bold, italic, size);
if (logFont != null) {
return logFont;
}
fr = getFontResource(familyName, bold, italic, true);
}
if (fr == null) {
// "System" is the default if we didn't recognise the family
return LogicalFont.getLogicalFont("System", bold, italic, size);
}
return new PrismFont(fr, fr.getFullName(), size);
}
public synchronized PGFont createFont(String name, float size) {
FontResource fr = null;
if (name != null && !name.isEmpty()) {
PGFont logFont =
LogicalFont.getLogicalFont(name, size);
if (logFont != null) {
return logFont;
}
fr = getFontResource(name, null, true);
}
if (fr == null) {
return LogicalFont.getLogicalFont(DEFAULT_FULLNAME, size);
}
return new PrismFont(fr, fr.getFullName(), size);
}
private PrismFontFile getFontResource(String name, String file) {
/* caller assures file not null */
PrismFontFile fr = null;
/* Still need decode the dfont (event when coretext is used)
* so that JFXFontFont can read it */
if (isMacOSX) {
DFontDecoder decoder = null;
if (name != null) {
if (file.endsWith(".dfont")) {
decoder = new DFontDecoder();
try {
decoder.openFile();
decoder.decode(name);
decoder.closeFile();
file = decoder.getFile().getPath();
} catch (Exception e) {
file = null;
decoder.deleteFile();
decoder = null;
if (PrismFontFactory.debugFonts) {
e.printStackTrace();
}
}
}
}
if (file != null) {
fr = createFontResource(name, file);
}
if (decoder != null) {
if (fr != null) {
addDecodedFont(fr);
} else {
decoder.deleteFile();
}
}
} else {
fr = createFontResource(name, file);
}
return fr;
}
public synchronized PGFont deriveFont(PGFont font, boolean bold,
boolean italic, float size) {
FontResource fr = font.getFontResource();
//TODO honor bold and italic
return new PrismFont(fr, fr.getFullName(), size);
}
private FontResource lookupResource(String lcName, boolean wantComp) {
if (wantComp) {
return compResourceMap.get(lcName);
} else {
return fontResourceMap.get(lcName);
}
}
public synchronized FontResource getFontResource(String name, String file,
boolean wantComp) {
FontResource fr = null;
// First check if the font is already known.
if (name != null) {
String lcName = name.toLowerCase();
// if requesting a wrapped resource, look in the composite map
// else look in the physical resource map
FontResource fontResource = lookupResource(lcName, wantComp);
if (fontResource != null) {
return fontResource;
}
/* We may have registered this as an embedded font.
* In which case we should also try to locate it in
* the non-composite map before looking elsewhere.
*/
if (embeddedFonts != null && wantComp) {
fr = lookupResource(lcName, false);
if (fr != null) {
fr = new PrismCompositeFontResource(fr, lcName);
}
if (fr != null) {
return fr;
}
}
}
/* We have hard coded some of the most commonly used Windows fonts
* so as to avoid the overhead of doing a lookup via GDI.
*/
if (isWindows && name != null) {
String lcName = name.toLowerCase();
String fontFile = WindowsFontMap.findFontFile(lcName, -1);
if (fontFile != null) {
fr = createFontResource(null, fontFile);
if (fr != null) {
if (wantComp) {
fr = new PrismCompositeFontResource(fr, lcName);
}
return fr;
}
}
}
getFullNameToFileMap(); // init maps
if (name != null && file != null) {
// Typically the TTC case used in font linking.
// The called method adds the resources to the physical
// map so no need to do it here.
fr = getFontResource(name, file);
if (fr != null) {
if (wantComp) {
fr = new PrismCompositeFontResource(fr, name.toLowerCase());
}
return fr;
}
}
if (name != null) { // Typically normal application lookup
fr = getFontResourceByFullName(name, wantComp);
if (fr != null) {
return fr;
}
}
if (file != null) { // Typically the TTF case used in font linking
fr = getFontResourceByFileName(file, wantComp);
if (fr != null) {
return fr;
}
}
/* can't find the requested font, caller will fall back to default */
return null;
}
boolean isInstalledFont(String fileName) {
// avoid loading the full windows map. Ignore drive letter
// as its common to install on D: too in multi-boot.
String fileKey;
if (isWindows) {
if (fileName.toLowerCase().contains("\\windows\\fonts")) {
return true;
}
File f = new File(fileName);
fileKey = f.getName();
} else {
if (isMacOSX && fileName.toLowerCase().contains("/library/fonts")) {
// Most fonts are installed in either /System/Library/Fonts/
// or /Library/Fonts/
return true;
}
File f = new File(fileName);
// fileToFontMap key is the full path on non-windows
fileKey = f.getPath();
}
getFullNameToFileMap();
return fileToFontMap.get(fileKey.toLowerCase()) != null;
}
/* To be called only by methods that already inited the maps
*/
synchronized private FontResource
getFontResourceByFileName(String file, boolean wantComp) {
if (fontToFileMap.size() <= 1) {
return null;
}
/* This is a little tricky: we know the file we want but we need
* to check if its already a loaded resource. The maps are set up
* to check if a font is loaded by its name, not file.
* To help I added a map from file->font for all the windows fonts
* but that is valid only for TTF fonts (not TTC). So it should only
* be used in a context where we know its a TTF (or OTF) file.
*/
String name = fileToFontMap.get(file.toLowerCase()); // basename
FontResource fontResource = null;
if (name == null) {
// We should not normally get here with a name that we did
// not find from the platform but any EUDC font is in the
// list of linked fonts but it is not enumerated by Windows.
// So we need to open the file and load it as requested.
fontResource = createFontResource(file, 0);
if (fontResource != null) {
String lcName = fontResource.getFullName().toLowerCase();
storeInMap(lcName, fontResource);
// Checking wantComp, alhough the linked/fallback font
// case doesn't use this.
if (wantComp) {
fontResource =
new PrismCompositeFontResource(fontResource, lcName);
}
}
} else {
String lcName = name.toLowerCase();
fontResource = lookupResource(lcName, wantComp);
if (fontResource == null) {
String fullPath = findFile(lcName);
if (fullPath != null) {
fontResource = getFontResource(name, fullPath);
if (fontResource != null) {
storeInMap(lcName, fontResource);
}
if (wantComp) {
// wrap with fallback support
fontResource =
new PrismCompositeFontResource(fontResource, lcName);
}
}
}
}
return fontResource; // maybe null
}
/* To be called only by methods that already inited the maps
* and checked the font is not already loaded.
*/
synchronized private FontResource
getFontResourceByFullName(String name, boolean wantComp) {
String lcName = name.toLowerCase();
if (fontToFileMap.size() <= 1) {
// Do this even though findFile also fails over to Lucida, as
// without this step, we'd create new instances.
name = jreDefaultFont;
}
FontResource fontResource = null;
String file = findFile(lcName);
if (file != null) {
fontResource = getFontResource(name, file);
if (fontResource != null) {
storeInMap(lcName, fontResource);
if (wantComp) {
// wrap with fallback support
fontResource =
new PrismCompositeFontResource(fontResource, lcName);
}
}
}
return fontResource;
}
FontResource getDefaultFontResource(boolean wantComp) {
FontResource fontResource = lookupResource(jreDefaultFontLC, wantComp);
if (fontResource == null) {
fontResource = createFontResource(jreDefaultFont,
jreFontDir+jreDefaultFontFile);
if (fontResource == null) {
// Normally use the JRE default font as the last fallback.
// If we can't find even that, use any platform font;
for (String font : fontToFileMap.keySet()) {
String file = findFile(font); // gets full path
fontResource = createFontResource(jreDefaultFontLC, file);
if (fontResource != null) {
break;
}
}
if (fontResource == null && isLinux) {
String path = FontConfigManager.getDefaultFontPath();
if (path != null) {
fontResource = createFontResource(jreDefaultFontLC,
path);
}
}
if (fontResource == null) {
return null; // We tried really hard!
}
}
storeInMap(jreDefaultFontLC, fontResource);
if (wantComp) { // wrap primary for map key
fontResource =
new PrismCompositeFontResource(fontResource,
jreDefaultFontLC);
}
}
return fontResource;
}
private String findFile(String name) {
if (name.equals(jreDefaultFontLC)) {
return jreFontDir+jreDefaultFontFile;
}
getFullNameToFileMap();
String filename = fontToFileMap.get(name);
if (isWindows) {
filename = getPathNameWindows(filename);
}
// Caller needs to check for null and explicitly request
// the JRE default font, if that is what is needed.
// since we don't want the JRE's Lucida font to be the
// default for "unknown" fonts.
return filename;
}
/* Used to indicate required return type from toArray(..); */
private static final String[] STR_ARRAY = new String[0];
/* Obtained from Platform APIs (windows only)
* Map from lower-case font full name to basename of font file.
* Eg "arial bold" -> ARIALBD.TTF.
* For TTC files, there is a mapping for each font in the file.
*/
private volatile HashMap<String,String> fontToFileMap = null;
/* TTF/OTF Font File to Font Full Name */
private HashMap<String,String> fileToFontMap = null;
/* Obtained from Platform APIs (windows only)
* Map from lower-case font full name to the name of its font family
* Eg "arial bold" -> "Arial"
*/
private HashMap<String,String> fontToFamilyNameMap = null;
/* Obtained from Platform APIs (windows only)
* Map from a lower-case family name to a list of full names of
* the member fonts, eg:
* "arial" -> ["Arial", "Arial Bold", "Arial Italic","Arial Bold Italic"]
*/
private HashMap<String,ArrayList<String>> familyToFontListMap= null;
/* For a terminal server there may be two font directories */
private static String sysFontDir = null;
private static String userFontDir = null;
private static native byte[] getFontPath();
private static native String regReadFontLink(String searchfont);
private static native String getEUDCFontFile();
private static void getPlatformFontDirs() {
if (userFontDir != null || sysFontDir != null) {
return;
}
byte [] pathBytes = getFontPath();
String path = new String(pathBytes);
int scIdx = path.indexOf(';');
if (scIdx < 0) {
sysFontDir = path;
} else {
sysFontDir = path.substring(0, scIdx);
userFontDir = path.substring(scIdx+1, path.length());
}
}
/**
* This will return an array of size 2, each element being an array
* list of <code>String</code>. The first (zeroth) array holds file
* names, and, the second array holds the corresponding fontnames.
* If the file does not have a corresponding font name, its corresponding
* name is assigned an empty string "".
* As a further complication, Windows 7 frequently lists a font twice,
* once with some scaling values, and again without. We don't use this
* so find these and exclude duplicates.
*/
static ArrayList<String> [] getLinkedFonts(String searchFont,
boolean addSearchFont) {
ArrayList<String> [] fontRegInfo = new ArrayList[2];
// index 0 = file names, 1 = font name.
// the name is only specified for TTC files.
fontRegInfo[0] = new ArrayList<String>();
fontRegInfo[1] = new ArrayList<String>();
if (isMacOSX) {
// Hotkey implementation of fallback font on Mac
fontRegInfo[0].add("/Library/Fonts/Arial Unicode.ttf");
fontRegInfo[1].add("Arial Unicode MS");
// Add Lucida Sans Regular to Mac OS X fallback list
fontRegInfo[0].add(jreFontDir + jreDefaultFontFile);
fontRegInfo[1].add(jreDefaultFont);
// Add Apple Symbols to Mac OS X fallback list
fontRegInfo[0].add("/System/Library/Fonts/Apple Symbols.ttf");
fontRegInfo[1].add("Apple Symbols");
// Add Apple Emoji Symbols to Mac OS X fallback list
fontRegInfo[0].add("/System/Library/Fonts/Apple Color Emoji.ttc");
fontRegInfo[1].add("Apple Color Emoji");
// Add CJK Ext B supplementary characters.
fontRegInfo[0].add("/System/Library/Fonts/STHeiti Light.ttf");
fontRegInfo[1].add("Heiti SC Light");
return fontRegInfo;
}
if (!isWindows) {
return fontRegInfo;
}
if (addSearchFont) {
fontRegInfo[0].add(null);
fontRegInfo[1].add(searchFont);
}
String fontRegBuf = regReadFontLink(searchFont);
if (fontRegBuf != null && fontRegBuf.length() > 0) {
// split registry data into null terminated strings
String[] fontRegList = fontRegBuf.split("\u0000");