-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathFontConfigManager.java
605 lines (545 loc) · 23.1 KB
/
FontConfigManager.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
/*
* Copyright (c) 2012, 2023, 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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;
public class FontConfigManager {
static boolean debugFonts = false;
static boolean useFontConfig = true;
static boolean fontConfigFailed = false;
static boolean useEmbeddedFontSupport = false;
static {
@SuppressWarnings("removal")
var dummy = AccessController.doPrivileged(
(PrivilegedAction<Void>) () -> {
String dbg = System.getProperty("prism.debugfonts", "");
debugFonts = "true".equals(dbg);
String ufc = System.getProperty("prism.useFontConfig", "true");
useFontConfig = "true".equals(ufc);
String emb = System.getProperty("prism.embeddedfonts", "");
useEmbeddedFontSupport = "true".equals(emb);
return null;
}
);
}
/* These next three classes are just data structures.
*/
public static class FontConfigFont {
public String familyName; // eg Bitstream Vera Sans
public String styleStr; // eg Bold
public String fullName; // eg Bitstream Vera Sans Bold
public String fontFile; // eg /usr/X11/lib/fonts/foo.ttf
}
public static class FcCompFont {
public String fcName; // eg sans
public String fcFamily; // eg sans
//public String fxName; // eg sans serif
public int style; // eg 0=PLAIN
public FontConfigFont firstFont;
public FontConfigFont[] allFonts;
//public CompositeFont compFont; // null if not yet created/known.
}
/* fontconfig recognises slants roman, italic, as well as oblique,
* and a slew of weights, where the ones that matter here are
* regular and bold.
* To fully qualify what we want, we can for example ask for (eg)
* Font.PLAIN : "serif:regular:roman"
* Font.BOLD : "serif:bold:roman"
* Font.ITALIC : "serif:regular:italic"
* Font.BOLD|Font.ITALIC : "serif:bold:italic"
*/
private static final String[] fontConfigNames = {
"sans:regular:roman",
"sans:bold:roman",
"sans:regular:italic",
"sans:bold:italic",
"serif:regular:roman",
"serif:bold:roman",
"serif:regular:italic",
"serif:bold:italic",
"monospace:regular:roman",
"monospace:bold:roman",
"monospace:regular:italic",
"monospace:bold:italic",
};
/* This array has the array elements created in Java code and is
* passed down to native to be filled in.
*/
private static FcCompFont[] fontConfigFonts;
private FontConfigManager() {
}
private static String[] getFontConfigNames() {
return fontConfigNames;
}
private static String getFCLocaleStr() {
Locale l = Locale.getDefault();
String localeStr = l.getLanguage();
String country = l.getCountry();
if (!country.equals("")) {
localeStr = localeStr + "-" + country;
}
return localeStr;
}
/* Return an array of FcCompFont structs describing the primary
* font located for each of fontconfig/GTK/Pango's logical font names.
*/
private static native boolean getFontConfig(String locale,
FcCompFont[] fonts,
boolean includeFallbacks);
private static synchronized void initFontConfigLogFonts() {
if (fontConfigFonts != null || fontConfigFailed) {
return;
}
long t0 = 0;
if (debugFonts) {
t0 = System.nanoTime();
}
String[] fontConfigNames = FontConfigManager.getFontConfigNames();
FcCompFont[] fontArr = new FcCompFont[fontConfigNames.length];
for (int i = 0; i< fontArr.length; i++) {
fontArr[i] = new FcCompFont();
fontArr[i].fcName = fontConfigNames[i];
int colonPos = fontArr[i].fcName.indexOf(':');
fontArr[i].fcFamily = fontArr[i].fcName.substring(0, colonPos);
fontArr[i].style = i % 4; // depends on array order.
}
boolean foundFontConfig = false;
if (useFontConfig) {
foundFontConfig = getFontConfig(getFCLocaleStr(), fontArr, true);
} else {
if (debugFonts) {
System.err.println("Not using FontConfig");
}
}
if (useEmbeddedFontSupport ||
!foundFontConfig)
{
EmbeddedFontSupport.initLogicalFonts(fontArr);
}
FontConfigFont anyFont = null;
/* If don't find anything (eg no libfontconfig), then just return */
for (int i = 0; i< fontArr.length; i++) {
FcCompFont fci = fontArr[i];
if (fci.firstFont == null) {
if (debugFonts) {
System.err.println("Fontconfig returned no font for " +
fontArr[i].fcName);
}
fontConfigFailed = true;
} else if (anyFont == null) {
anyFont = fci.firstFont;
defaultFontFile = anyFont.fontFile;
}
}
if (anyFont == null) {
fontConfigFailed = true;
System.err.println("Error: JavaFX detected no fonts! " +
"Please refer to release notes for proper font configuration");
return;
} else if (fontConfigFailed) {
for (int i = 0; i< fontArr.length; i++) {
if (fontArr[i].firstFont == null) {
fontArr[i].firstFont = anyFont;
}
}
}
fontConfigFonts = fontArr;
if (debugFonts) {
long t1 = System.nanoTime();
System.err.println("Time spent accessing fontconfig="
+ ((t1 - t0) / 1000000) + "ms.");
for (int i = 0; i<fontConfigFonts.length; i++) {
FcCompFont fci = fontConfigFonts[i];
System.err.println("FC font " + fci.fcName+" maps to " +
fci.firstFont.fullName +
" in file " + fci.firstFont.fontFile);
if (fci.allFonts != null) {
for (int f=0;f<fci.allFonts.length;f++) {
FontConfigFont fcf = fci.allFonts[f];
System.err.println(" "+f+ ") Family=" +
fcf.familyName +
", Style="+ fcf.styleStr +
", Fullname="+fcf.fullName +
", File="+fcf.fontFile);
}
}
}
}
}
private static native boolean populateMapsNative
(HashMap<String,String> fontToFileMap,
HashMap<String,String> fontToFamilyNameMap,
HashMap<String,ArrayList<String>> familyToFontListMap,
Locale locale);
public static void populateMaps
(HashMap<String,String> fontToFileMap,
HashMap<String,String> fontToFamilyNameMap,
HashMap<String,ArrayList<String>> familyToFontListMap,
Locale locale) {
boolean pnm = false;
if (useFontConfig && !fontConfigFailed) {
pnm = populateMapsNative(fontToFileMap, fontToFamilyNameMap,
familyToFontListMap, locale);
}
if (fontConfigFailed ||
useEmbeddedFontSupport ||
!pnm) {
EmbeddedFontSupport.populateMaps(fontToFileMap,
fontToFamilyNameMap,
familyToFontListMap, locale);
}
}
private static String mapFxToFcLogicalFamilyName(String fxName) {
if (fxName.equals("serif")) {
return "serif";
} else if (fxName.equals("monospaced")) {
return "monospace";
} else {
return "sans";
}
}
public static FcCompFont getFontConfigFont(String fxFamilyName,
boolean bold, boolean italic) {
initFontConfigLogFonts();
if (fontConfigFonts == null) {
return null;
}
String name = mapFxToFcLogicalFamilyName(fxFamilyName.toLowerCase());
int style = (bold) ? 1 : 0;
if (italic) {
style +=2;
}
FcCompFont fcInfo = null;
for (int i=0; i<fontConfigFonts.length; i++) {
if (name.equals(fontConfigFonts[i].fcFamily) &&
style == fontConfigFonts[i].style) {
fcInfo = fontConfigFonts[i];
break;
}
}
if (fcInfo == null) {
fcInfo = fontConfigFonts[0];
}
if (debugFonts) {
System.err.println("FC name=" + name + " style=" + style +
" uses " + fcInfo.firstFont.fullName +
" in file: " + fcInfo.firstFont.fontFile);
}
return fcInfo;
}
private static String defaultFontFile;
public static String getDefaultFontPath() {
if (fontConfigFonts == null && !fontConfigFailed) {
// trigger font config initialisation
getFontConfigFont("System", false, false);
}
return defaultFontFile;
}
public static ArrayList<String>
getFileNames(FcCompFont font, boolean fallBacksOnly) {
ArrayList fileList = new ArrayList<String>();
if (font.allFonts != null) {
int start = (fallBacksOnly) ? 1 : 0;
for (int i=start; i<font.allFonts.length; i++) {
fileList.add(font.allFonts[i].fontFile);
}
}
return fileList;
}
public static ArrayList<String>
getFontNames(FcCompFont font, boolean fallBacksOnly) {
ArrayList fontList = new ArrayList<String>();
if (font.allFonts != null) {
int start = (fallBacksOnly) ? 1 : 0;
for (int i=start; i<font.allFonts.length; i++) {
fontList.add(font.allFonts[i].fullName);
}
}
return fontList;
}
/* Embedded Linux may not have fontconfig, in which case we look for
* 1) A single directory which contains all font files.
* 2) A java properties style file which describes which of these files
* to use for the logical fonts.
* Optionally a system property can be used to set the directory
* else it will default to "{FXHOME}/lib/fonts".
*/
private static class EmbeddedFontSupport {
private static String fontDirProp = null;
private static String fontDir;
private static boolean fontDirFromJRE = false;
static {
@SuppressWarnings("removal")
var dummy = AccessController.doPrivileged(
(PrivilegedAction<Void>) () -> {
initEmbeddedFonts();
return null;
}
);
}
private static void initEmbeddedFonts() {
fontDirProp = System.getProperty("prism.fontdir");
if (fontDirProp != null) {
fontDir = fontDirProp;
} else {
try {
// no fontConfig or -Dprism.fontdir, lets fallback
// to {java.home}/lib/fonts if it exists
final String javaHome = System.getProperty("java.home");
if (javaHome == null) {
return;
}
File fontDirectory = new File(javaHome, "lib/fonts");
if (fontDirectory.exists()) {
fontDirFromJRE = true;
fontDir = fontDirectory.getPath();
}
if (debugFonts) {
System.err.println("Fallback fontDir is " + fontDirectory +
" exists = " +
fontDirectory.exists());
}
} catch (Exception e) {
if (debugFonts) {
e.printStackTrace();
}
fontDir = "/";
}
}
}
private static String getStyleStr(int style) {
switch (style) {
case 0 : return "regular";
case 1 : return "bold";
case 2 : return "italic";
case 3 : return "bolditalic";
default : return "regular";
}
}
@SuppressWarnings("removal")
private static boolean exists(final File f) {
return AccessController.doPrivileged(
(PrivilegedAction<Boolean>) () -> f.exists()
);
}
// this mapping is used in the embedded world when
// fontconfig is not used and we find the jre fonts
static String[] jreFontsProperties = {
"sans.regular.0.font", "Lucida Sans Regular",
"sans.regular.0.file", "LucidaSansRegular.ttf",
"sans.bold.0.font", "Lucida Sans Bold",
"sans.bold.0.file", "LucidaSansDemiBold.ttf",
"monospace.regular.0.font", "Lucida Typewriter Regular",
"monospace.regular.0.file", "LucidaTypewriterRegular.ttf",
"monospace.bold.0.font", "Lucida Typewriter Bold",
"monospace.bold.0.file", "LucidaTypewriterBold.ttf",
"serif.regular.0.font", "Lucida Bright",
"serif.regular.0.file", "LucidaBrightRegular.ttf",
"serif.bold.0.font", "Lucida Bright Demibold",
"serif.bold.0.file", "LucidaBrightDemiBold.ttf",
"serif.italic.0.font", "Lucida Bright Italic",
"serif.italic.0.file", "LucidaBrightItalic.ttf",
"serif.bolditalic.0.font", "Lucida Bright Demibold Italic",
"serif.bolditalic.0.file", "LucidaBrightDemiItalic.ttf"
};
/**
* Logical Font Support for FX on embedded platforms
*
* Reads a simple properties file which defines the 3
* font families "sans", "serif", "monospace". This uses
* fontconfig names because the interface to access this
* goes via internal APIs which communicate with FontConfig.
* The format using sans as an example, looks like
* sans.regular.0.font=Arial
* sans.regular.0.file=Arial.ttf
* sans.bold.0.font=Arial Bold
* sans.bold.0.file=Arial-Bold.ttf
* sans.italic.0.font=Arial Italic
* sans.italic.0.file=Arial-Italic.ttf
* sans.bolditalic.0.font=Arial Bold Italic
* sans.bolditalic.0.file=Arial-BoldItalic.ttf
*
* Additional fonts for each style would be indexed 1, 2, etc ..
* The fonts must be made visible to the embedded FX - see
* <code>populateMaps()</code>
*/
static void initLogicalFonts(FcCompFont[] fonts) {
Properties props = new Properties();
try {
File f = new File(fontDir,"logicalfonts.properties");
if (f.exists()) {
FileInputStream fis = new FileInputStream(f);
props.load(fis);
fis.close();
} else if (fontDirFromJRE) {
// Our fontDir is in the JRE (at least relative to our Jar)
// we have no logicalfonts.properties, but we can see
// if we can intuit one.... this is checked next.
for(int i=0; i < jreFontsProperties.length; i += 2) {
props.setProperty(jreFontsProperties[i],jreFontsProperties[i+1]);
}
if (debugFonts) {
System.err.println("Using fallback implied logicalfonts.properties");
}
}
} catch (IOException ioe) {
if (debugFonts) {
System.err.println(ioe);
return;
}
}
for (int f=0; f<fonts.length; f++) {
String fcFamily = fonts[f].fcFamily;
String styleStr = getStyleStr(fonts[f].style);
String key = fcFamily+"."+styleStr+".";
ArrayList<FontConfigFont> allFonts = new ArrayList<>();
int i=0;
while (true) {
String file = props.getProperty(key+i+".file");
String font = props.getProperty(key+i+".font");
i++;
if (file == null) {
break;
}
File ff = new File(fontDir, file);
if (!exists(ff)) {
if (debugFonts) {
System.out.println("Failed to find logical font file "+ff);
}
continue;
}
FontConfigFont fcFont = new FontConfigFont();
fcFont.fontFile = ff.getPath(); // required.
fcFont.fullName = font; // optional.
fcFont.familyName = null; // not used.
fcFont.styleStr = null; // not used.
if (fonts[f].firstFont == null) {
fonts[f].firstFont = fcFont;
}
allFonts.add(fcFont);
}
if (allFonts.size() > 0) {
fonts[f].allFonts = new FontConfigFont[allFonts.size()];
allFonts.toArray(fonts[f].allFonts);
}
}
}
/*
* To speed lookup of fonts, check for a file called
* allfonts.properties. Only fonts in that file will be enumerated.
* If this file isn't present we'll open all the files.
* The file name must be a simple basename, and is mandatory.
* But you are encouraged to name the font and family too.
* If this file doesn't say the name or family of a font,
* we'll still open the file. The names must be the actual
* font names. No promises to fix it if you spell it wrongly.
* We look for "maxFont" and if that's a valid property
* we'll look from 0 .. maxFont, else we stop at the first
* index for which no file exists. Eg file :
* maxFont=1
* family.0=Arial
* font.0=Arial Regular
* file.0=arial.ttf
* family.1=Times New Roman
* font.1=Times New Roman Regular
* file.1=times.ttf
*
* NOTE: if you are testing embedded on desktop, and are using
* the 2D pipeline, you will want to use /home/<you>/.fonts as
* the font directory, else Java 2D won't know where to find
* the fonts. If this turns out to be a major impediment, we
* can arrange to register them with Java 2D.
*/
static void populateMaps
(HashMap<String,String> fontToFileMap,
HashMap<String,String> fontToFamilyNameMap,
HashMap<String,ArrayList<String>> familyToFontListMap,
Locale locale)
{
final Properties props = new Properties();
@SuppressWarnings("removal")
var dummy = AccessController.doPrivileged(
(PrivilegedAction<Void>) () -> {
try {
String lFile = fontDir+"/allfonts.properties";
FileInputStream fis = new FileInputStream(lFile);
props.load(fis);
fis.close();
} catch (IOException ioe) {
props.clear();
if (debugFonts) {
System.err.println(ioe);
System.err.println("Fall back to opening the files");
}
}
return null;
}
);
if (!props.isEmpty()) {
int maxFont = Integer.MAX_VALUE;
try {
maxFont = Integer.parseInt(props.getProperty("maxFont",""));
} catch (NumberFormatException e) {
}
if (maxFont <= 0) {
maxFont = Integer.MAX_VALUE;
}
for (int f=0; f<maxFont; f++) {
String family = props.getProperty("family."+f);
String font = props.getProperty("font."+f);
String file = props.getProperty("file."+f);
if (file == null) {
break;
}
File ff = new File(fontDir, file);
if (!exists(ff)) {
continue;
}
if (family == null || font == null) {
continue; // TBD
}
String fontLC = font.toLowerCase(Locale.ENGLISH);
String familyLC = family.toLowerCase(Locale.ENGLISH);
fontToFileMap.put(fontLC, ff.getPath());
fontToFamilyNameMap.put(fontLC, family);
ArrayList<String> familyArr =
familyToFontListMap.get(familyLC);
if (familyArr == null) {
familyArr = new ArrayList<>(4);
familyToFontListMap.put(familyLC, familyArr);
}
familyArr.add(font);
}
}
}
}
}