-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathfontpath_linux.c
852 lines (789 loc) · 32 KB
/
fontpath_linux.c
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
/*
* Copyright (c) 2012, 2019, 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.
*/
#if defined (__linux__) && ! defined (ANDROID_NDK)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <dlfcn.h>
#include <fontconfig/fontconfig.h>
#include <jni.h>
#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL
JNI_OnLoad_javafx_font(JavaVM * vm, void * reserved) {
#ifdef JNI_VERSION_1_8
//min. returned JNI_VERSION required by JDK8 for builtin libraries
JNIEnv *env;
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_8) != JNI_OK) {
return JNI_VERSION_1_4;
}
return JNI_VERSION_1_8;
#else
return JNI_VERSION_1_4;
#endif
}
#endif
/*
* We are not explicitly linking against fontconfig. This isn't so
* relevant for desktop platforms any more but could help on embedded
* platforms where it may not exist.
*/
static void* openFontConfig() {
char *homeEnv;
static char *homeEnvStr = "HOME="; /* must be static */
void* libfontconfig = NULL;
/* 64 bit sparc should pick up the right version from the lib path.
* New features may be added to libfontconfig, this is expected to
* be compatible with old features, but we may need to start
* distinguishing the library version, to know whether to expect
* certain symbols - and functionality - to be available.
* Also add explicit search for .so.1 in case .so symlink doesn't exist.
*/
libfontconfig = dlopen("libfontconfig.so.1", RTLD_LOCAL|RTLD_LAZY);
if (libfontconfig == NULL) {
libfontconfig = dlopen("libfontconfig.so", RTLD_LOCAL|RTLD_LAZY);
if (libfontconfig == NULL) {
return NULL;
}
}
/* Version 1.0 of libfontconfig crashes if HOME isn't defined in
* the environment. This should generally never happen, but we can't
* control it, and can't control the version of fontconfig, so iff
* its not defined we set it to an empty value which is sufficient
* to prevent a crash. I considered unsetting it before exit, but
* it doesn't appear to work on Solaris, so I will leave it set.
*/
homeEnv = getenv("HOME");
if (homeEnv == NULL) {
putenv(homeEnvStr);
}
return libfontconfig;
}
typedef void* (FcFiniFuncType)();
static void closeFontConfig(void* libfontconfig, jboolean fcFini) {
/* NB FcFini is not in (eg) the Solaris 10 version of fontconfig. Its not
* clear if this means we are really leaking resources in those cases
* but it seems we should call this function when its available.
* But since the Swing GTK code may be still accessing the lib, its probably
* safest for now to just let this "leak" rather than potentially
* concurrently free global data still in use by other code.
*/
#if 0
if (fcFini) { /* release resources */
FcFiniFuncType FcFini = (FcFiniFuncType)dlsym(libfontconfig, "FcFini");
if (FcFini != NULL) {
(*FcFini)();
}
}
#endif
dlclose(libfontconfig);
}
typedef FcConfig* (*FcInitLoadConfigFuncType)();
typedef FcPattern* (*FcPatternBuildFuncType)(FcPattern *orig, ...);
typedef FcObjectSet* (*FcObjectSetFuncType)(const char *first, ...);
typedef FcFontSet* (*FcFontListFuncType)(FcConfig *config,
FcPattern *p,
FcObjectSet *os);
typedef FcResult (*FcPatternGetBoolFuncType)(const FcPattern *p,
const char *object,
int n,
FcBool *b);
typedef FcResult (*FcPatternGetIntegerFuncType)(const FcPattern *p,
const char *object,
int n,
int *i);
typedef FcResult (*FcPatternGetStringFuncType)(const FcPattern *p,
const char *object,
int n,
FcChar8 ** s);
typedef void (*FcPatternDestroyFuncType)(FcPattern *p);
typedef void (*FcFontSetDestroyFuncType)(FcFontSet *s);
typedef FcPattern* (*FcNameParseFuncType)(const FcChar8 *name);
typedef FcBool (*FcPatternAddStringFuncType)(FcPattern *p,
const char *object,
const FcChar8 *s);
typedef void (*FcDefaultSubstituteFuncType)(FcPattern *p);
typedef FcBool (*FcConfigSubstituteFuncType)(FcConfig *config,
FcPattern *p,
FcMatchKind kind);
typedef FcPattern* (*FcFontMatchFuncType)(FcConfig *config,
FcPattern *p,
FcResult *result);
typedef FcFontSet* (*FcFontSetCreateFuncType)();
typedef FcBool (*FcFontSetAddFuncType)(FcFontSet *s, FcPattern *font);
typedef FcResult (*FcPatternGetCharSetFuncType)(FcPattern *p,
const char *object,
int n,
FcCharSet **c);
typedef FcFontSet* (*FcFontSortFuncType)(FcConfig *config,
FcPattern *p,
FcBool trim,
FcCharSet **csp,
FcResult *result);
typedef FcCharSet* (*FcCharSetUnionFuncType)(const FcCharSet *a,
const FcCharSet *b);
typedef FcChar32 (*FcCharSetSubtractCountFuncType)(const FcCharSet *a,
const FcCharSet *b);
JNIEXPORT jboolean JNICALL
Java_com_sun_javafx_font_FontConfigManager_getFontConfig
(JNIEnv *env, jclass obj, jstring localeStr,
jobjectArray fcCompFontArray, jboolean includeFallbacks) {
FcNameParseFuncType FcNameParse;
FcPatternAddStringFuncType FcPatternAddString;
FcConfigSubstituteFuncType FcConfigSubstitute;
FcDefaultSubstituteFuncType FcDefaultSubstitute;
FcFontMatchFuncType FcFontMatch;
FcPatternGetStringFuncType FcPatternGetString;
FcPatternDestroyFuncType FcPatternDestroy;
FcPatternGetCharSetFuncType FcPatternGetCharSet;
FcFontSortFuncType FcFontSort;
FcFontSetDestroyFuncType FcFontSetDestroy;
FcCharSetUnionFuncType FcCharSetUnion;
FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
int i, arrlen;
jstring fcNameStr, jstr;
const char *locale, *fcName;
FcPattern *pattern;
FcResult result;
void* libfontconfig;
jfieldID fcNameFID, fcFirstFontFID, fcAllFontsFID;
jfieldID familyNameFID, styleNameFID, fullNameFID, fontFileFID;
jmethodID fcFontCons;
jclass fcCompFontClass, fcFontClass;
// Deleting local refs as we go along so this should be plenty
// Unlikely to matter even if it fails.
(*env)->EnsureLocalCapacity(env, 64);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcCompFontClass =
(*env)->FindClass(env,
"com/sun/javafx/font/FontConfigManager$FcCompFont");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcFontClass =
(*env)->FindClass(env,
"com/sun/javafx/font/FontConfigManager$FontConfigFont");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
if (fcCompFontArray == NULL ||
fcCompFontClass == NULL ||
fcFontClass == NULL)
{
return JNI_FALSE;
}
fcNameFID = (*env)->GetFieldID(env, fcCompFontClass,
"fcName", "Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcFirstFontFID =
(*env)->GetFieldID(env, fcCompFontClass, "firstFont",
"Lcom/sun/javafx/font/FontConfigManager$FontConfigFont;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcAllFontsFID =
(*env)->GetFieldID(env, fcCompFontClass, "allFonts",
"[Lcom/sun/javafx/font/FontConfigManager$FontConfigFont;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcFontCons = (*env)->GetMethodID(env, fcFontClass, "<init>", "()V");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
familyNameFID = (*env)->GetFieldID(env, fcFontClass,
"familyName", "Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
styleNameFID = (*env)->GetFieldID(env, fcFontClass,
"styleStr", "Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fullNameFID = (*env)->GetFieldID(env, fcFontClass,
"fullName", "Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fontFileFID = (*env)->GetFieldID(env, fcFontClass,
"fontFile", "Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
if (fcNameFID == NULL ||
fcFirstFontFID == NULL ||
fcAllFontsFID == NULL ||
fcFontCons == NULL ||
familyNameFID == NULL ||
styleNameFID == NULL ||
fullNameFID == NULL ||
fontFileFID == NULL)
{
return JNI_FALSE;
}
if ((libfontconfig = openFontConfig()) == NULL) {
return JNI_FALSE;
}
FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
FcPatternAddString =
(FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
FcConfigSubstitute =
(FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
dlsym(libfontconfig, "FcDefaultSubstitute");
FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
FcPatternGetString =
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
FcPatternDestroy =
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
FcPatternGetCharSet =
(FcPatternGetCharSetFuncType)dlsym(libfontconfig,
"FcPatternGetCharSet");
FcFontSort =
(FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort");
FcFontSetDestroy =
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
FcCharSetUnion =
(FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion");
FcCharSetSubtractCount =
(FcCharSetSubtractCountFuncType)dlsym(libfontconfig,
"FcCharSetSubtractCount");
if (FcNameParse == NULL ||
FcPatternAddString == NULL ||
FcConfigSubstitute == NULL ||
FcDefaultSubstitute == NULL ||
FcFontMatch == NULL ||
FcPatternGetString == NULL ||
FcPatternDestroy == NULL ||
FcPatternGetCharSet == NULL ||
FcFontSetDestroy == NULL ||
FcCharSetUnion == NULL ||
FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
locale = (*env)->GetStringUTFChars(env, localeStr, 0);
arrlen = (*env)->GetArrayLength(env, fcCompFontArray);
for (i=0; i<arrlen; i++) {
FcFontSet* fontset;
int fn, j, fontCount, nfonts;
unsigned int minGlyphs;
FcChar8 **family, **styleStr, **fullname, **file;
jarray fcFontArr;
jobject fcCompFontObj;
fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
fcNameStr =
(jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameFID));
fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
if (fcName == NULL) {
continue;
}
pattern = (*FcNameParse)((FcChar8 *)fcName);
if (pattern == NULL) {
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
/* locale may not usually be necessary as fontconfig appears to apply
* this anyway based on the user's environment. However we want
* to use the value of the JDK startup locale so this should take
* care of it.
*/
if (locale != NULL) {
(*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
}
(*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
(*FcDefaultSubstitute)(pattern);
fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result);
if (fontset == NULL) {
(*FcPatternDestroy)(pattern);
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
/* fontconfig returned us "nfonts". It may include Type 1 fonts
* but we are going to skip those.
* Next create separate C arrays of length nfonts for family file etc.
* Inspect the returned fonts and the ones we like (adds enough glyphs)
* are added to the arrays and we increment 'fontCount'.
*/
nfonts = fontset->nfont;
family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
if (family == NULL || styleStr == NULL ||
fullname == NULL || file == NULL) {
if (family != NULL) {
free(family);
}
if (styleStr != NULL) {
free(styleStr);
}
if (fullname != NULL) {
free(fullname);
}
if (file != NULL) {
free(file);
}
(*FcPatternDestroy)(pattern);
(*FcFontSetDestroy)(fontset);
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
fontCount = 0;
minGlyphs = 20;
FcCharSet *unionCharset = NULL;
for (j=0; j<nfonts; j++) {
FcPattern *fontPattern = fontset->fonts[j];
FcChar8 *fontformat;
FcCharSet *charset;
fontformat = NULL;
(*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat);
/* We only want TrueType fonts for Java FX */
if (fontformat != NULL
&& (strcmp((char*)fontformat, "TrueType") != 0)) {
continue;
}
result = (*FcPatternGetCharSet)(fontPattern,
FC_CHARSET, 0, &charset);
if (result != FcResultMatch) {
free(family);
free(fullname);
free(styleStr);
free(file);
(*FcPatternDestroy)(pattern);
(*FcFontSetDestroy)(fontset);
(*env)->ReleaseStringUTFChars(env,
fcNameStr, (const char*)fcName);
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
/* We don't want 20 or 30 fonts, so once we hit 10 fonts,
* then require that they really be adding value. Too many
* adversely affects load time for minimal value-add.
* This is still likely far more than we've had in the past.
*/
if (j==10) {
minGlyphs = 50;
}
if (unionCharset == NULL) {
unionCharset = charset;
} else {
if ((*FcCharSetSubtractCount)(charset, unionCharset)
> minGlyphs) {
unionCharset = (* FcCharSetUnion)(unionCharset, charset);
} else {
continue;
}
}
fontCount++; // found a font we will use.
(*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]);
(*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]);
(*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]);
(*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]);
if (!includeFallbacks) {
break;
}
if (fontCount == 254) {
/* Upstream Java code currently stores this in a byte;
* And we need one slot free for when this sequence is
* used as a fallback sequeunce.
*/
break;
}
}
/* Once we get here 'fontCount' is the number of returned fonts
* we actually want to use, so we create 'fcFontArr' of that length.
* The non-null entries of "family[]" etc are those fonts.
* Then loop again over all nfonts adding just those non-null ones
* to 'fcFontArr'. If its null (we didn't want the font)
* then we don't enter the main body.
* So we should never get more than 'fontCount' entries.
*/
if (includeFallbacks) {
fcFontArr =
(*env)->NewObjectArray(env, fontCount, fcFontClass, NULL);
(*env)->SetObjectField(env,
fcCompFontObj, fcAllFontsFID, fcFontArr);
} else {
fcFontArr = NULL;
}
fn=0;
for (j=0;j<nfonts;j++) {
if (family[j] != NULL) {
jobject fcFont =
(*env)->NewObject(env, fcFontClass, fcFontCons);
jstr = (*env)->NewStringUTF(env, (const char*)family[j]);
(*env)->SetObjectField(env, fcFont, familyNameFID, jstr);
(*env)->DeleteLocalRef(env, jstr);
if (file[j] != NULL) {
jstr = (*env)->NewStringUTF(env, (const char*)file[j]);
(*env)->SetObjectField(env, fcFont, fontFileFID, jstr);
(*env)->DeleteLocalRef(env, jstr);
}
if (styleStr[j] != NULL) {
jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]);
(*env)->SetObjectField(env, fcFont, styleNameFID, jstr);
(*env)->DeleteLocalRef(env, jstr);
}
if (fullname[j] != NULL) {
jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]);
(*env)->SetObjectField(env, fcFont, fullNameFID, jstr);
(*env)->DeleteLocalRef(env, jstr);
}
if (fn==0) {
(*env)->SetObjectField(env, fcCompFontObj,
fcFirstFontFID, fcFont);
}
if (includeFallbacks && fcFontArr != NULL) {
(*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont);
(*env)->DeleteLocalRef(env, fcFont);
} else {
(*env)->DeleteLocalRef(env, fcFont);
break;
}
}
}
if (fcFontArr != NULL) {
(*env)->DeleteLocalRef(env, fcFontArr);
}
(*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
(*env)->DeleteLocalRef(env, fcNameStr);
(*FcFontSetDestroy)(fontset);
(*FcPatternDestroy)(pattern);
free(family);
free(styleStr);
free(fullname);
free(file);
}
/* release resources and close the ".so" */
if (locale) {
(*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale);
}
closeFontConfig(libfontconfig, JNI_TRUE);
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL
Java_com_sun_javafx_font_FontConfigManager_populateMapsNative
(JNIEnv *env, jclass obj,
jobject fontToFileMap,
jobject fontToFamilyNameMap,
jobject familyToFontListMap,
jobject locale
)
{
void *libfontconfig;
const char *lang;
int langLen, f;
FcPatternBuildFuncType FcPatternBuild;
FcObjectSetFuncType FcObjectSetBuild;
FcFontListFuncType FcFontList;
FcPatternGetStringFuncType FcPatternGetString;
FcFontSetDestroyFuncType FcFontSetDestroy;
FcPattern *pattern;
FcObjectSet *objset;
FcFontSet *fontSet;
jclass classID, arrayListClass;
jmethodID arrayListCtr, addMID, getMID;
jmethodID toLowerCaseMID;
jmethodID putMID, containsKeyMID;
jboolean debugFC = getenv("PRISM_FONTCONFIG_DEBUG") != NULL;
if (fontToFileMap == NULL ||
fontToFamilyNameMap == NULL ||
familyToFontListMap == NULL ||
locale == NULL)
{
if (debugFC) {
fprintf(stderr, "Null arg to native fontconfig lookup");
}
return JNI_FALSE;
}
if ((libfontconfig = openFontConfig()) == NULL) {
if (debugFC) {
fprintf(stderr,"Could not open libfontconfig\n");
}
return JNI_FALSE;
}
FcPatternBuild =
(FcPatternBuildFuncType)dlsym(libfontconfig, "FcPatternBuild");
FcObjectSetBuild =
(FcObjectSetFuncType)dlsym(libfontconfig, "FcObjectSetBuild");
FcFontList =
(FcFontListFuncType)dlsym(libfontconfig, "FcFontList");
FcPatternGetString =
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
FcFontSetDestroy =
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
if (FcPatternBuild == NULL ||
FcObjectSetBuild == NULL ||
FcPatternGetString == NULL ||
FcFontList == NULL ||
FcFontSetDestroy == NULL) { /* problem with the library: return. */
if (debugFC) {
fprintf(stderr,"Could not find symbols in libfontconfig\n");
}
closeFontConfig(libfontconfig, JNI_FALSE);
return JNI_FALSE;
}
// Deleting local refs as we go along so this should be plenty
// Unlikely to matter even if it fails.
(*env)->EnsureLocalCapacity(env, 64);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
classID = (*env)->FindClass(env, "java/util/HashMap");
if ((*env)->ExceptionOccurred(env) || classID == NULL) {
return JNI_FALSE;
}
getMID = (*env)->GetMethodID(env, classID, "get",
"(Ljava/lang/Object;)Ljava/lang/Object;");
if ((*env)->ExceptionOccurred(env) || getMID == NULL) {
return JNI_FALSE;
}
putMID = (*env)->GetMethodID(env, classID, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
if ((*env)->ExceptionOccurred(env) || putMID == NULL) {
return JNI_FALSE;
}
containsKeyMID = (*env)->GetMethodID(env, classID, "containsKey",
"(Ljava/lang/Object;)Z");
if ((*env)->ExceptionOccurred(env) || containsKeyMID == NULL) {
return JNI_FALSE;
}
arrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
if ((*env)->ExceptionOccurred(env) || arrayListClass == NULL) {
return JNI_FALSE;
}
arrayListCtr = (*env)->GetMethodID(env, arrayListClass,
"<init>", "(I)V");
if ((*env)->ExceptionOccurred(env) || arrayListCtr == NULL) {
return JNI_FALSE;
}
addMID = (*env)->GetMethodID(env, arrayListClass,
"add", "(Ljava/lang/Object;)Z");
if ((*env)->ExceptionOccurred(env) || addMID == NULL) {
return JNI_FALSE;
}
classID = (*env)->FindClass(env, "java/lang/String");
if ((*env)->ExceptionOccurred(env) || classID == NULL) {
return JNI_FALSE;
}
toLowerCaseMID =
(*env)->GetMethodID(env, classID, "toLowerCase",
"(Ljava/util/Locale;)Ljava/lang/String;");
if ((*env)->ExceptionOccurred(env) || toLowerCaseMID == NULL) {
return JNI_FALSE;
}
pattern = (*FcPatternBuild)(NULL, FC_OUTLINE, FcTypeBool, FcTrue, NULL);
objset = (*FcObjectSetBuild)(FC_FAMILY, FC_FAMILYLANG,
FC_FULLNAME, FC_FULLNAMELANG,
FC_FILE, FC_FONTFORMAT, NULL);
fontSet = (*FcFontList)(NULL, pattern, objset);
if (debugFC) {
fprintf(stderr,"Fontconfig found %d fonts\n", fontSet->nfont);
}
for (f=0; f < fontSet->nfont; f++) {
int n=0, done=0;
FcPattern *fp = fontSet->fonts[f];
FcChar8 *family = NULL;
FcChar8 *familyEN = NULL;
FcChar8 *familyLang = NULL;
FcChar8 *fullName = NULL;
FcChar8 *fullNameEN = NULL;
FcChar8 *fullNameLang = NULL;
FcChar8 *file;
FcResult res;
jstring jFileStr;
jstring jFamilyStr, jFamilyStrLC;
jstring jFullNameStr, jFullNameStrLC;
jobject jList;
FcChar8 *format = NULL;
/* We only want TrueType & OpenType fonts for Java FX */
format = NULL;
if ((*FcPatternGetString)(fp, FC_FONTFORMAT, 0, &format)
!= FcResultMatch) {
continue;
}
if (format == NULL ||
((strcmp((char*)format, "TrueType") != 0) &&
(strcmp((char*)format, "CFF") != 0))) {
continue;
}
if ((*FcPatternGetString)(fp, FC_FILE, 0, &file) != FcResultMatch) {
continue;
} else {
char pathname[PATH_MAX+1];
char* path = realpath((char*)file, pathname);
if (path == NULL) {
continue;
} else {
file = (FcChar8*)path;
}
}
n=0;
while (!done) {
family = NULL;
familyLang = NULL;
fullName = NULL;
fullNameLang = NULL;
if (((*FcPatternGetString)(fp, FC_FAMILY, n, &family)
== FcResultMatch) &&
((*FcPatternGetString)(fp, FC_FAMILYLANG, n, &familyLang)
== FcResultMatch) &&
(family != NULL && familyLang != NULL) &&
(familyEN == NULL || (strcmp((char*)familyLang, "en") == 0)))
{
familyEN = family;
}
if (((*FcPatternGetString)(fp, FC_FULLNAME, n, &fullName)
== FcResultMatch) &&
((*FcPatternGetString)(fp, FC_FULLNAMELANG, n, &fullNameLang)
== FcResultMatch) &&
(fullName != NULL && fullNameLang != NULL) &&
(fullNameEN == NULL ||
(strcmp((char*)fullNameLang,"en") == 0)))
{
fullNameEN = fullName;
}
if (family == NULL && fullName == NULL) {
done = 1;
break;
}
n++;
}
if (debugFC) {
fprintf(stderr,"Read FC font family=%s fullname=%s file=%s\n",
(familyEN == NULL) ? "null" : (char*)familyEN,
(fullNameEN == NULL) ? "null" : (char*)fullNameEN,
(file == NULL) ? "null" : (char*)file);
fflush(stderr);
}
/* We set the names from the first found names for a font, updating
* to the English ones as they are found. If these are null
* we must not have found any name, so we'd better skip.
*/
if (familyEN == NULL || fullNameEN == NULL || file == NULL) {
if (debugFC) {
fprintf(stderr,"FC: Skipping on error for above font\n");
fflush(stderr);
}
continue;
}
jFileStr = (*env)->NewStringUTF(env, (const char*)file);
jFamilyStr = (*env)->NewStringUTF(env, (const char*)familyEN);
jFullNameStr = (*env)->NewStringUTF(env, (const char*)fullNameEN);
if (jFileStr == NULL || jFamilyStr == NULL || jFullNameStr == NULL) {
if (debugFC) {
fprintf(stderr,"Failed to create string object");
}
continue;
}
jFamilyStrLC = (*env)->CallObjectMethod(env, jFamilyStr,
toLowerCaseMID, locale);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
jFullNameStrLC = (*env)->CallObjectMethod(env, jFullNameStr,
toLowerCaseMID, locale);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
if (jFamilyStrLC == NULL || jFullNameStrLC == NULL) {
if (debugFC) {
fprintf(stderr,"Failed to create lower case string object");
fflush(stderr);
}
continue;
}
(*env)->CallObjectMethod(env, fontToFileMap, putMID,
jFullNameStrLC, jFileStr);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
(*env)->CallObjectMethod(env, fontToFamilyNameMap, putMID,
jFullNameStrLC, jFamilyStr);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
jList = (*env)->CallObjectMethod(env, familyToFontListMap,
getMID, jFamilyStrLC);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
if (jList == NULL) {
jList = (*env)->NewObject(env, arrayListClass, arrayListCtr, 4);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
(*env)->CallObjectMethod(env, familyToFontListMap,
putMID, jFamilyStrLC, jList);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
}
if (jList == NULL) {
if (debugFC) {
fprintf(stderr,"Fontconfig: List is null\n");
fflush(stderr);
}
continue;
}
(*env)->CallObjectMethod(env, jList, addMID, jFullNameStr);
if ((*env)->ExceptionOccurred(env)) {
return JNI_FALSE;
}
/* Now referenced from the passed in maps, so can delete local refs. */
(*env)->DeleteLocalRef(env, jFileStr);
(*env)->DeleteLocalRef(env, jFamilyStr);
(*env)->DeleteLocalRef(env, jFamilyStrLC);
(*env)->DeleteLocalRef(env, jFullNameStr);
(*env)->DeleteLocalRef(env, jFullNameStrLC);
(*env)->DeleteLocalRef(env, jList);
}
if (debugFC) {
fprintf(stderr,"Done enumerating fontconfig fonts\n");
fflush(stderr);
}
(*FcFontSetDestroy)(fontSet);
closeFontConfig(libfontconfig, JNI_TRUE);
return JNI_TRUE;
}
#endif /* __linux__ */