Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/unix/native/common/awt/fontpath.c
66645 views
1
/*
2
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#if defined(__linux__)
27
#include <string.h>
28
#endif /* __linux__ */
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <strings.h>
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <sys/mman.h>
35
#include <fcntl.h>
36
#include <unistd.h>
37
38
#include <jni.h>
39
#include <jni_util.h>
40
#include <jvm_md.h>
41
#include <sizecalc.h>
42
#ifndef HEADLESS
43
#include <X11/Xlib.h>
44
#include <awt.h>
45
#else
46
/* locks ought to be included from awt.h */
47
#define AWT_LOCK()
48
#define AWT_UNLOCK()
49
#endif /* !HEADLESS */
50
51
#if defined(__linux__) && !defined(MAP_FAILED)
52
#define MAP_FAILED ((caddr_t)-1)
53
#endif
54
55
#ifndef HEADLESS
56
extern Display *awt_display;
57
#endif /* !HEADLESS */
58
59
#define FONTCONFIG_DLL_VERSIONED VERSIONED_JNI_LIB_NAME("fontconfig", "1")
60
#define FONTCONFIG_DLL JNI_LIB_NAME("fontconfig")
61
62
#define MAXFDIRS 512 /* Max number of directories that contain fonts */
63
64
#if defined( __linux__)
65
/* All the known interesting locations we have discovered on
66
* various flavors of Linux
67
*/
68
static char *fullLinuxFontPath[] = {
69
"/usr/X11R6/lib/X11/fonts/TrueType", /* RH 7.1+ */
70
"/usr/X11R6/lib/X11/fonts/truetype", /* SuSE */
71
"/usr/X11R6/lib/X11/fonts/tt",
72
"/usr/X11R6/lib/X11/fonts/TTF",
73
"/usr/X11R6/lib/X11/fonts/OTF", /* RH 9.0 (but empty!) */
74
"/usr/share/fonts/ja/TrueType", /* RH 7.2+ */
75
"/usr/share/fonts/truetype",
76
"/usr/share/fonts/ko/TrueType", /* RH 9.0 */
77
"/usr/share/fonts/zh_CN/TrueType", /* RH 9.0 */
78
"/usr/share/fonts/zh_TW/TrueType", /* RH 9.0 */
79
"/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType", /* Debian */
80
"/usr/X11R6/lib/X11/fonts/Type1",
81
"/usr/share/fonts/default/Type1", /* RH 9.0 */
82
NULL, /* terminates the list */
83
};
84
#elif defined(_AIX)
85
static char *fullAixFontPath[] = {
86
"/usr/lpp/X11/lib/X11/fonts/Type1", /* from X11.fnt.iso_T1 */
87
"/usr/lpp/X11/lib/X11/fonts/TrueType", /* from X11.fnt.ucs.ttf */
88
NULL, /* terminates the list */
89
};
90
#endif
91
92
static char **getFontConfigLocations();
93
94
typedef struct {
95
const char *name[MAXFDIRS];
96
int num;
97
} fDirRecord, *fDirRecordPtr;
98
99
#ifndef HEADLESS
100
101
/*
102
* Returns True if display is local, False of it's remote.
103
*/
104
jboolean isDisplayLocal(JNIEnv *env) {
105
static jboolean isLocal = False;
106
static jboolean isLocalSet = False;
107
jboolean ret;
108
109
if (! isLocalSet) {
110
jclass geCls = (*env)->FindClass(env, "java/awt/GraphicsEnvironment");
111
CHECK_NULL_RETURN(geCls, JNI_FALSE);
112
jmethodID getLocalGE = (*env)->GetStaticMethodID(env, geCls,
113
"getLocalGraphicsEnvironment",
114
"()Ljava/awt/GraphicsEnvironment;");
115
CHECK_NULL_RETURN(getLocalGE, JNI_FALSE);
116
jobject ge = (*env)->CallStaticObjectMethod(env, geCls, getLocalGE);
117
JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
118
119
jclass sgeCls = (*env)->FindClass(env,
120
"sun/java2d/SunGraphicsEnvironment");
121
CHECK_NULL_RETURN(sgeCls, JNI_FALSE);
122
if ((*env)->IsInstanceOf(env, ge, sgeCls)) {
123
jmethodID isDisplayLocal = (*env)->GetMethodID(env, sgeCls,
124
"isDisplayLocal",
125
"()Z");
126
JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
127
isLocal = (*env)->CallBooleanMethod(env, ge, isDisplayLocal);
128
JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
129
} else {
130
isLocal = True;
131
}
132
isLocalSet = True;
133
}
134
135
return isLocal;
136
}
137
138
static char **getX11FontPath ()
139
{
140
char **x11Path, **fontdirs;
141
int i, pos, slen, nPaths, numDirs;
142
143
x11Path = XGetFontPath (awt_display, &nPaths);
144
145
/* This isn't ever going to be perfect: the font path may contain
146
* much we aren't interested in, but the cost should be moderate
147
* Exclude all directories that contain the strings "Speedo","/F3/",
148
* "75dpi", "100dpi", "misc" or "bitmap", or don't begin with a "/",
149
* the last of which should exclude font servers.
150
* Also exclude the user specific ".gnome*" directories which
151
* aren't going to contain the system fonts we need.
152
* Hopefully we are left only with Type1 and TrueType directories.
153
* It doesn't matter much if there are extraneous directories, it'll just
154
* cost us a little wasted effort upstream.
155
*/
156
fontdirs = (char**)calloc(nPaths+1, sizeof(char*));
157
if (fontdirs == NULL) {
158
return NULL;
159
}
160
pos = 0;
161
for (i=0; i < nPaths; i++) {
162
if (x11Path[i][0] != '/') {
163
continue;
164
}
165
if (strstr(x11Path[i], "/75dpi") != NULL) {
166
continue;
167
}
168
if (strstr(x11Path[i], "/100dpi") != NULL) {
169
continue;
170
}
171
if (strstr(x11Path[i], "/misc") != NULL) {
172
continue;
173
}
174
if (strstr(x11Path[i], "/Speedo") != NULL) {
175
continue;
176
}
177
if (strstr(x11Path[i], ".gnome") != NULL) {
178
continue;
179
}
180
fontdirs[pos] = strdup(x11Path[i]);
181
slen = strlen(fontdirs[pos]);
182
if (slen > 0 && fontdirs[pos][slen-1] == '/') {
183
fontdirs[pos][slen-1] = '\0'; /* null out trailing "/" */
184
}
185
pos++;
186
}
187
188
XFreeFontPath(x11Path);
189
if (pos == 0) {
190
free(fontdirs);
191
fontdirs = NULL;
192
}
193
return fontdirs;
194
}
195
196
197
#endif /* !HEADLESS */
198
199
#if defined(__linux__)
200
/* from awt_LoadLibrary.c */
201
JNIEXPORT jboolean JNICALL AWTIsHeadless();
202
#endif
203
204
/* This eliminates duplicates, at a non-linear but acceptable cost
205
* since the lists are expected to be reasonably short, and then
206
* deletes references to non-existent directories, and returns
207
* a single path consisting of unique font directories.
208
*/
209
static char* mergePaths(char **p1, char **p2, char **p3, jboolean noType1) {
210
211
int len1=0, len2=0, len3=0, totalLen=0, numDirs=0,
212
currLen, i, j, found, pathLen=0;
213
char **ptr, **fontdirs;
214
char *fontPath = NULL;
215
216
if (p1 != NULL) {
217
ptr = p1;
218
while (*ptr++ != NULL) len1++;
219
}
220
if (p2 != NULL) {
221
ptr = p2;
222
223
while (*ptr++ != NULL) len2++;
224
}
225
if (p3 != NULL) {
226
ptr = p3;
227
while (*ptr++ != NULL) len3++;
228
}
229
totalLen = len1+len2+len3;
230
fontdirs = (char**)calloc(totalLen, sizeof(char*));
231
if (fontdirs == NULL) {
232
return NULL;
233
}
234
235
for (i=0; i < len1; i++) {
236
if (noType1 && strstr(p1[i], "Type1") != NULL) {
237
continue;
238
}
239
fontdirs[numDirs++] = p1[i];
240
}
241
242
currLen = numDirs; /* only compare against previous path dirs */
243
for (i=0; i < len2; i++) {
244
if (noType1 && strstr(p2[i], "Type1") != NULL) {
245
continue;
246
}
247
found = 0;
248
for (j=0; j < currLen; j++) {
249
if (strcmp(fontdirs[j], p2[i]) == 0) {
250
found = 1;
251
break;
252
}
253
}
254
if (!found) {
255
fontdirs[numDirs++] = p2[i];
256
}
257
}
258
259
currLen = numDirs; /* only compare against previous path dirs */
260
for (i=0; i < len3; i++) {
261
if (noType1 && strstr(p3[i], "Type1") != NULL) {
262
continue;
263
}
264
found = 0;
265
for (j=0; j < currLen; j++) {
266
if (strcmp(fontdirs[j], p3[i]) == 0) {
267
found = 1;
268
break;
269
}
270
}
271
if (!found) {
272
fontdirs[numDirs++] = p3[i];
273
}
274
}
275
276
/* Now fontdirs contains unique dirs and numDirs records how many.
277
* What we don't know is if they all exist. On reflection I think
278
* this isn't an issue, so for now I will return all these locations,
279
* converted to one string */
280
for (i=0; i<numDirs; i++) {
281
pathLen += (strlen(fontdirs[i]) + 1);
282
}
283
if (pathLen > 0 && (fontPath = malloc(pathLen))) {
284
*fontPath = '\0';
285
for (i = 0; i<numDirs; i++) {
286
if (i != 0) {
287
strcat(fontPath, ":");
288
}
289
strcat(fontPath, fontdirs[i]);
290
}
291
}
292
free (fontdirs);
293
294
return fontPath;
295
}
296
297
/*
298
* The goal of this function is to find all "system" fonts which
299
* are needed by the JRE to display text in supported locales etc, and
300
* to support APIs which allow users to enumerate all system fonts and use
301
* them from their Java applications.
302
* The preferred mechanism is now using the new "fontconfig" library
303
* This exists on newer versions of Linux and Solaris (S10 and above)
304
* The library is dynamically located. The results are merged with
305
* a set of "known" locations and with the X11 font path, if running in
306
* a local X11 environment.
307
* The hardwired paths are built into the JDK binary so as new font locations
308
* are created on a host plaform for them to be located by the JRE they will
309
* need to be added ito the host's font configuration database, typically
310
* /etc/fonts/local.conf, and to ensure that directory contains a fonts.dir
311
* NB: Fontconfig also depends heavily for performance on the host O/S
312
* maintaining up to date caches.
313
* This is consistent with the requirements of the desktop environments
314
* on these OSes.
315
* This also frees us from X11 APIs as JRE is required to function in
316
* a "headless" mode where there is no Xserver.
317
*/
318
static char *getPlatformFontPathChars(JNIEnv *env, jboolean noType1, jboolean isX11) {
319
320
char **fcdirs = NULL, **x11dirs = NULL, **knowndirs = NULL, *path = NULL;
321
322
// mod: NULL -> FALLBACK
323
/* As of 1.5 we try to use fontconfig on both Solaris and Linux.
324
* If its not available FALLBACK is returned.
325
*/
326
fcdirs = getFontConfigLocations();
327
328
#if defined(__linux__)
329
knowndirs = fullLinuxFontPath;
330
#elif defined(_AIX)
331
knowndirs = fullAixFontPath;
332
#endif
333
/* REMIND: this code requires to be executed when the GraphicsEnvironment
334
* is already initialised. That is always true, but if it were not so,
335
* this code could throw an exception and the fontpath would fail to
336
* be initialised.
337
*/
338
#ifndef HEADLESS
339
if (isX11) { // The following only works in an x11 environment.
340
#if defined(__linux__)
341
/* There's no headless build on linux ... */
342
if (!AWTIsHeadless()) { /* .. so need to call a function to check */
343
#endif
344
/* Using the X11 font path to locate font files is now a fallback
345
* useful only if fontconfig failed, or is incomplete. So we could
346
* remove this code completely and the consequences should be rare
347
* and non-fatal. If this happens, then the calling Java code can
348
* be modified to no longer require that the AWT lock (the X11GE)
349
* be initialised prior to calling this code.
350
*/
351
AWT_LOCK();
352
if (isDisplayLocal(env)) {
353
x11dirs = getX11FontPath();
354
}
355
AWT_UNLOCK();
356
#if defined(__linux__)
357
}
358
#endif
359
}
360
#endif /* !HEADLESS */
361
path = mergePaths(fcdirs, x11dirs, knowndirs, noType1);
362
if (fcdirs != NULL) {
363
char **p = fcdirs;
364
while (*p != NULL) free(*p++);
365
free(fcdirs);
366
}
367
368
if (x11dirs != NULL) {
369
char **p = x11dirs;
370
while (*p != NULL) free(*p++);
371
free(x11dirs);
372
}
373
374
return path;
375
}
376
377
JNIEXPORT jstring JNICALL Java_sun_awt_FcFontManager_getFontPathNative
378
(JNIEnv *env, jobject thiz, jboolean noType1, jboolean isX11) {
379
jstring ret;
380
static char *ptr = NULL; /* retain result across calls */
381
382
if (ptr == NULL) {
383
ptr = getPlatformFontPathChars(env, noType1, isX11);
384
}
385
ret = (*env)->NewStringUTF(env, ptr);
386
return ret;
387
}
388
389
#include <dlfcn.h>
390
391
#include <fontconfig/fontconfig.h>
392
393
394
static void* openFontConfig() {
395
396
char *homeEnv;
397
static char *homeEnvStr = "HOME="; /* must be static */
398
void* libfontconfig = NULL;
399
400
/* Private workaround to not use fontconfig library.
401
* May be useful during testing/debugging
402
*/
403
char *useFC = getenv("USE_J2D_FONTCONFIG");
404
if (useFC != NULL && !strcmp(useFC, "no")) {
405
return NULL;
406
}
407
408
#if defined(_AIX)
409
/* On AIX, fontconfig is not a standard package supported by IBM.
410
* instead it has to be installed from the "AIX Toolbox for Linux Applications"
411
* site http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html
412
* and will be installed under /opt/freeware/lib/libfontconfig.a.
413
* Notice that the archive contains the real 32- and 64-bit shared libraries.
414
* We first try to load 'libfontconfig.so' from the default library path in the
415
* case the user has installed a private version of the library and if that
416
* doesn't succeed, we try the version from /opt/freeware/lib/libfontconfig.a
417
*/
418
libfontconfig = dlopen("libfontconfig.so", RTLD_LOCAL|RTLD_LAZY);
419
if (libfontconfig == NULL) {
420
libfontconfig = dlopen("/opt/freeware/lib/libfontconfig.a(libfontconfig.so.1)", RTLD_MEMBER|RTLD_LOCAL|RTLD_LAZY);
421
if (libfontconfig == NULL) {
422
return NULL;
423
}
424
}
425
#else
426
/* 64 bit sparc should pick up the right version from the lib path.
427
* New features may be added to libfontconfig, this is expected to
428
* be compatible with old features, but we may need to start
429
* distinguishing the library version, to know whether to expect
430
* certain symbols - and functionality - to be available.
431
* Also add explicit search for .so.1 in case .so symlink doesn't exist.
432
*/
433
libfontconfig = dlopen(FONTCONFIG_DLL_VERSIONED, RTLD_LOCAL|RTLD_LAZY);
434
if (libfontconfig == NULL) {
435
libfontconfig = dlopen(FONTCONFIG_DLL, RTLD_LOCAL|RTLD_LAZY);
436
if (libfontconfig == NULL) {
437
return NULL;
438
}
439
}
440
#endif
441
442
/* Version 1.0 of libfontconfig crashes if HOME isn't defined in
443
* the environment. This should generally never happen, but we can't
444
* control it, and can't control the version of fontconfig, so iff
445
* its not defined we set it to an empty value which is sufficient
446
* to prevent a crash. I considered unsetting it before exit, but
447
* it doesn't appear to work on Solaris, so I will leave it set.
448
*/
449
homeEnv = getenv("HOME");
450
if (homeEnv == NULL) {
451
putenv(homeEnvStr);
452
}
453
454
return libfontconfig;
455
}
456
457
typedef void* (FcFiniFuncType)();
458
459
static void closeFontConfig(void* libfontconfig, jboolean fcFini) {
460
461
/* NB FcFini is not in (eg) the Solaris 10 version of fontconfig. Its not
462
* clear if this means we are really leaking resources in those cases
463
* but it seems we should call this function when its available.
464
* But since the Swing GTK code may be still accessing the lib, its probably
465
* safest for now to just let this "leak" rather than potentially
466
* concurrently free global data still in use by other code.
467
*/
468
#if 0
469
if (fcFini) { /* release resources */
470
FcFiniFuncType FcFini = (FcFiniFuncType)dlsym(libfontconfig, "FcFini");
471
472
if (FcFini != NULL) {
473
(*FcFini)();
474
}
475
}
476
#endif
477
dlclose(libfontconfig);
478
}
479
480
typedef FcConfig* (*FcInitLoadConfigFuncType)();
481
typedef FcPattern* (*FcPatternBuildFuncType)(FcPattern *orig, ...);
482
typedef FcObjectSet* (*FcObjectSetFuncType)(const char *first, ...);
483
typedef FcFontSet* (*FcFontListFuncType)(FcConfig *config,
484
FcPattern *p,
485
FcObjectSet *os);
486
typedef FcResult (*FcPatternGetBoolFuncType)(const FcPattern *p,
487
const char *object,
488
int n,
489
FcBool *b);
490
typedef FcResult (*FcPatternGetIntegerFuncType)(const FcPattern *p,
491
const char *object,
492
int n,
493
int *i);
494
typedef FcResult (*FcPatternGetStringFuncType)(const FcPattern *p,
495
const char *object,
496
int n,
497
FcChar8 ** s);
498
typedef FcChar8* (*FcStrDirnameFuncType)(const FcChar8 *file);
499
typedef void (*FcPatternDestroyFuncType)(FcPattern *p);
500
typedef void (*FcObjectSetDestroyFuncType)(FcObjectSet *os);
501
typedef void (*FcFontSetDestroyFuncType)(FcFontSet *s);
502
typedef FcPattern* (*FcNameParseFuncType)(const FcChar8 *name);
503
typedef FcBool (*FcPatternAddStringFuncType)(FcPattern *p,
504
const char *object,
505
const FcChar8 *s);
506
typedef void (*FcDefaultSubstituteFuncType)(FcPattern *p);
507
typedef FcBool (*FcConfigSubstituteFuncType)(FcConfig *config,
508
FcPattern *p,
509
FcMatchKind kind);
510
typedef FcPattern* (*FcFontMatchFuncType)(FcConfig *config,
511
FcPattern *p,
512
FcResult *result);
513
typedef FcFontSet* (*FcFontSetCreateFuncType)();
514
typedef FcBool (*FcFontSetAddFuncType)(FcFontSet *s, FcPattern *font);
515
516
typedef FcResult (*FcPatternGetCharSetFuncType)(FcPattern *p,
517
const char *object,
518
int n,
519
FcCharSet **c);
520
typedef FcFontSet* (*FcFontSortFuncType)(FcConfig *config,
521
FcPattern *p,
522
FcBool trim,
523
FcCharSet **csp,
524
FcResult *result);
525
typedef FcCharSet* (*FcCharSetUnionFuncType)(const FcCharSet *a,
526
const FcCharSet *b);
527
typedef FcCharSet* (*FcCharSetDestroyFuncType)(FcCharSet *fcs);
528
typedef FcChar32 (*FcCharSetSubtractCountFuncType)(const FcCharSet *a,
529
const FcCharSet *b);
530
531
typedef int (*FcGetVersionFuncType)();
532
533
typedef FcStrList* (*FcConfigGetCacheDirsFuncType)(FcConfig *config);
534
typedef FcChar8* (*FcStrListNextFuncType)(FcStrList *list);
535
typedef FcChar8* (*FcStrListDoneFuncType)(FcStrList *list);
536
537
// mod: fallback directories
538
static char **getFallbackFontLocations() {
539
540
char **fontdirs = (char**)calloc(3, sizeof(char*));
541
fontdirs[0] = (char *)calloc(1, 4096);
542
fontdirs[1] = (char *)calloc(1, 40);
543
sprintf(fontdirs[0], "%s/lib/fonts", getenv("JAVA_HOME"));
544
sprintf(fontdirs[1], "%s", "/System/Library/Fonts/UnicodeSupport");
545
return fontdirs;
546
547
}
548
549
static char **getFontConfigLocations() {
550
551
char **fontdirs;
552
int numdirs = 0;
553
FcInitLoadConfigFuncType FcInitLoadConfig;
554
FcPatternBuildFuncType FcPatternBuild;
555
FcObjectSetFuncType FcObjectSetBuild;
556
FcFontListFuncType FcFontList;
557
FcPatternGetStringFuncType FcPatternGetString;
558
FcStrDirnameFuncType FcStrDirname;
559
FcPatternDestroyFuncType FcPatternDestroy;
560
FcObjectSetDestroyFuncType FcObjectSetDestroy;
561
FcFontSetDestroyFuncType FcFontSetDestroy;
562
563
FcConfig *fontconfig;
564
FcPattern *pattern;
565
FcObjectSet *objset;
566
FcFontSet *fontSet;
567
FcStrList *strList;
568
FcChar8 *str;
569
int i, f, found, len=0;
570
char **fontPath;
571
572
void* libfontconfig = openFontConfig();
573
574
if (libfontconfig == NULL) {
575
return getFallbackFontLocations();
576
// original: NULL
577
}
578
579
FcPatternBuild =
580
(FcPatternBuildFuncType)dlsym(libfontconfig, "FcPatternBuild");
581
FcObjectSetBuild =
582
(FcObjectSetFuncType)dlsym(libfontconfig, "FcObjectSetBuild");
583
FcFontList =
584
(FcFontListFuncType)dlsym(libfontconfig, "FcFontList");
585
FcPatternGetString =
586
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
587
FcStrDirname =
588
(FcStrDirnameFuncType)dlsym(libfontconfig, "FcStrDirname");
589
FcPatternDestroy =
590
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
591
FcObjectSetDestroy =
592
(FcObjectSetDestroyFuncType)dlsym(libfontconfig, "FcObjectSetDestroy");
593
FcFontSetDestroy =
594
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
595
596
if (FcPatternBuild == NULL ||
597
FcObjectSetBuild == NULL ||
598
FcPatternGetString == NULL ||
599
FcFontList == NULL ||
600
FcStrDirname == NULL ||
601
FcPatternDestroy == NULL ||
602
FcObjectSetDestroy == NULL ||
603
FcFontSetDestroy == NULL) { /* problem with the library: return. */
604
closeFontConfig(libfontconfig, JNI_FALSE);
605
return NULL;
606
}
607
608
/* Make calls into the fontconfig library to build a search for
609
* outline fonts, and to get the set of full file paths from the matches.
610
* This set is returned from the call to FcFontList(..)
611
* We allocate an array of char* pointers sufficient to hold all
612
* the matches + 1 extra which ensures there will be a NULL after all
613
* valid entries.
614
* We call FcStrDirname strip the file name from the path, and
615
* check if we have yet seen this directory. If not we add a pointer to
616
* it into our array of char*. Note that FcStrDirname returns newly
617
* allocated storage so we can use this in the return char** value.
618
* Finally we clean up, freeing allocated resources, and return the
619
* array of unique directories.
620
*/
621
pattern = (*FcPatternBuild)(NULL, FC_OUTLINE, FcTypeBool, FcTrue, NULL);
622
objset = (*FcObjectSetBuild)(FC_FILE, NULL);
623
fontSet = (*FcFontList)(NULL, pattern, objset);
624
if (fontSet == NULL) {
625
/* FcFontList() may return NULL if fonts are not installed. */
626
fontdirs = NULL;
627
} else {
628
fontdirs = (char**)calloc(fontSet->nfont+1, sizeof(char*));
629
if (fontdirs == NULL) {
630
(*FcFontSetDestroy)(fontSet);
631
goto cleanup;
632
}
633
for (f=0; f < fontSet->nfont; f++) {
634
FcChar8 *file;
635
FcChar8 *dir;
636
if ((*FcPatternGetString)(fontSet->fonts[f], FC_FILE, 0, &file) ==
637
FcResultMatch) {
638
dir = (*FcStrDirname)(file);
639
found = 0;
640
for (i=0;i<numdirs; i++) {
641
if (strcmp(fontdirs[i], (char*)dir) == 0) {
642
found = 1;
643
break;
644
}
645
}
646
if (!found) {
647
fontdirs[numdirs++] = (char*)dir;
648
} else {
649
free((char*)dir);
650
}
651
}
652
}
653
/* Free fontset if one was returned */
654
(*FcFontSetDestroy)(fontSet);
655
}
656
657
cleanup:
658
/* Free memory and close the ".so" */
659
(*FcObjectSetDestroy)(objset);
660
(*FcPatternDestroy)(pattern);
661
closeFontConfig(libfontconfig, JNI_TRUE);
662
return fontdirs;
663
}
664
665
/* These are copied from sun.awt.SunHints.
666
* Consider initialising them as ints using JNI for more robustness.
667
*/
668
#define TEXT_AA_OFF 1
669
#define TEXT_AA_ON 2
670
#define TEXT_AA_LCD_HRGB 4
671
#define TEXT_AA_LCD_HBGR 5
672
#define TEXT_AA_LCD_VRGB 6
673
#define TEXT_AA_LCD_VBGR 7
674
675
JNIEXPORT jint JNICALL
676
Java_sun_font_FontConfigManager_getFontConfigAASettings
677
(JNIEnv *env, jclass obj, jstring localeStr, jstring fcNameStr) {
678
679
FcNameParseFuncType FcNameParse;
680
FcPatternAddStringFuncType FcPatternAddString;
681
FcConfigSubstituteFuncType FcConfigSubstitute;
682
FcDefaultSubstituteFuncType FcDefaultSubstitute;
683
FcFontMatchFuncType FcFontMatch;
684
FcPatternGetBoolFuncType FcPatternGetBool;
685
FcPatternGetIntegerFuncType FcPatternGetInteger;
686
FcPatternDestroyFuncType FcPatternDestroy;
687
688
FcPattern *pattern, *matchPattern;
689
FcResult result;
690
FcBool antialias = FcFalse;
691
int rgba = 0;
692
const char *locale=NULL, *fcName=NULL;
693
void* libfontconfig;
694
695
if (fcNameStr == NULL || localeStr == NULL) {
696
return -1;
697
}
698
699
fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
700
if (fcName == NULL) {
701
return -1;
702
}
703
locale = (*env)->GetStringUTFChars(env, localeStr, 0);
704
705
if ((libfontconfig = openFontConfig()) == NULL) {
706
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
707
if (locale) {
708
(*env)->ReleaseStringUTFChars(env, localeStr,(const char*)locale);
709
}
710
return -1;
711
}
712
713
FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
714
FcPatternAddString =
715
(FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
716
FcConfigSubstitute =
717
(FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
718
FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
719
dlsym(libfontconfig, "FcDefaultSubstitute");
720
FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
721
FcPatternGetBool = (FcPatternGetBoolFuncType)
722
dlsym(libfontconfig, "FcPatternGetBool");
723
FcPatternGetInteger = (FcPatternGetIntegerFuncType)
724
dlsym(libfontconfig, "FcPatternGetInteger");
725
FcPatternDestroy =
726
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
727
728
if (FcNameParse == NULL ||
729
FcPatternAddString == NULL ||
730
FcConfigSubstitute == NULL ||
731
FcDefaultSubstitute == NULL ||
732
FcFontMatch == NULL ||
733
FcPatternGetBool == NULL ||
734
FcPatternGetInteger == NULL ||
735
FcPatternDestroy == NULL) { /* problem with the library: return. */
736
737
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
738
if (locale) {
739
(*env)->ReleaseStringUTFChars(env, localeStr,(const char*)locale);
740
}
741
closeFontConfig(libfontconfig, JNI_FALSE);
742
return -1;
743
}
744
745
746
pattern = (*FcNameParse)((FcChar8 *)fcName);
747
if (locale != NULL) {
748
(*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
749
}
750
(*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
751
(*FcDefaultSubstitute)(pattern);
752
matchPattern = (*FcFontMatch)(NULL, pattern, &result);
753
/* Perhaps should call FcFontRenderPrepare() here as some pattern
754
* elements might change as a result of that call, but I'm not seeing
755
* any difference in testing.
756
*/
757
if (matchPattern) {
758
(*FcPatternGetBool)(matchPattern, FC_ANTIALIAS, 0, &antialias);
759
(*FcPatternGetInteger)(matchPattern, FC_RGBA, 0, &rgba);
760
(*FcPatternDestroy)(matchPattern);
761
}
762
(*FcPatternDestroy)(pattern);
763
764
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
765
if (locale) {
766
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
767
}
768
closeFontConfig(libfontconfig, JNI_TRUE);
769
770
if (antialias == FcFalse) {
771
return TEXT_AA_OFF;
772
} else if (rgba <= FC_RGBA_UNKNOWN || rgba >= FC_RGBA_NONE) {
773
return TEXT_AA_ON;
774
} else {
775
switch (rgba) {
776
case FC_RGBA_RGB : return TEXT_AA_LCD_HRGB;
777
case FC_RGBA_BGR : return TEXT_AA_LCD_HBGR;
778
case FC_RGBA_VRGB : return TEXT_AA_LCD_VRGB;
779
case FC_RGBA_VBGR : return TEXT_AA_LCD_VBGR;
780
default : return TEXT_AA_LCD_HRGB; // should not get here.
781
}
782
}
783
}
784
785
JNIEXPORT jint JNICALL
786
Java_sun_font_FontConfigManager_getFontConfigVersion
787
(JNIEnv *env, jclass obj) {
788
789
void* libfontconfig;
790
FcGetVersionFuncType FcGetVersion;
791
int version = 0;
792
793
if ((libfontconfig = openFontConfig()) == NULL) {
794
return 0;
795
}
796
797
FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
798
799
if (FcGetVersion == NULL) {
800
closeFontConfig(libfontconfig, JNI_FALSE);
801
return 0;
802
}
803
version = (*FcGetVersion)();
804
closeFontConfig(libfontconfig, JNI_FALSE);
805
806
return version;
807
}
808
809
810
JNIEXPORT void JNICALL
811
Java_sun_font_FontConfigManager_getFontConfig
812
(JNIEnv *env, jclass obj, jstring localeStr, jobject fcInfoObj,
813
jobjectArray fcCompFontArray, jboolean includeFallbacks) {
814
815
FcNameParseFuncType FcNameParse;
816
FcPatternAddStringFuncType FcPatternAddString;
817
FcConfigSubstituteFuncType FcConfigSubstitute;
818
FcDefaultSubstituteFuncType FcDefaultSubstitute;
819
FcFontMatchFuncType FcFontMatch;
820
FcPatternGetStringFuncType FcPatternGetString;
821
FcPatternDestroyFuncType FcPatternDestroy;
822
FcPatternGetCharSetFuncType FcPatternGetCharSet;
823
FcFontSortFuncType FcFontSort;
824
FcFontSetDestroyFuncType FcFontSetDestroy;
825
FcCharSetUnionFuncType FcCharSetUnion;
826
FcCharSetDestroyFuncType FcCharSetDestroy;
827
FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
828
FcGetVersionFuncType FcGetVersion;
829
FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs;
830
FcStrListNextFuncType FcStrListNext;
831
FcStrListDoneFuncType FcStrListDone;
832
833
int i, arrlen;
834
jobject fcCompFontObj;
835
jstring fcNameStr, jstr;
836
const char *locale, *fcName;
837
FcPattern *pattern;
838
FcResult result;
839
void* libfontconfig;
840
jfieldID fcNameID, fcFirstFontID, fcAllFontsID, fcVersionID, fcCacheDirsID;
841
jfieldID familyNameID, styleNameID, fullNameID, fontFileID;
842
jmethodID fcFontCons;
843
char* debugMinGlyphsStr = getenv("J2D_DEBUG_MIN_GLYPHS");
844
jclass fcInfoClass;
845
jclass fcCompFontClass;
846
jclass fcFontClass;
847
848
CHECK_NULL(fcInfoObj);
849
CHECK_NULL(fcCompFontArray);
850
851
fcInfoClass =
852
(*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigInfo");
853
CHECK_NULL(fcInfoClass);
854
fcCompFontClass =
855
(*env)->FindClass(env, "sun/font/FontConfigManager$FcCompFont");
856
CHECK_NULL(fcCompFontClass);
857
fcFontClass =
858
(*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigFont");
859
CHECK_NULL(fcFontClass);
860
861
862
CHECK_NULL(fcVersionID = (*env)->GetFieldID(env, fcInfoClass, "fcVersion", "I"));
863
CHECK_NULL(fcCacheDirsID = (*env)->GetFieldID(env, fcInfoClass, "cacheDirs",
864
"[Ljava/lang/String;"));
865
CHECK_NULL(fcNameID = (*env)->GetFieldID(env, fcCompFontClass,
866
"fcName", "Ljava/lang/String;"));
867
CHECK_NULL(fcFirstFontID = (*env)->GetFieldID(env, fcCompFontClass, "firstFont",
868
"Lsun/font/FontConfigManager$FontConfigFont;"));
869
CHECK_NULL(fcAllFontsID = (*env)->GetFieldID(env, fcCompFontClass, "allFonts",
870
"[Lsun/font/FontConfigManager$FontConfigFont;"));
871
CHECK_NULL(fcFontCons = (*env)->GetMethodID(env, fcFontClass, "<init>", "()V"));
872
CHECK_NULL(familyNameID = (*env)->GetFieldID(env, fcFontClass,
873
"familyName", "Ljava/lang/String;"));
874
CHECK_NULL(styleNameID = (*env)->GetFieldID(env, fcFontClass,
875
"styleStr", "Ljava/lang/String;"));
876
CHECK_NULL(fullNameID = (*env)->GetFieldID(env, fcFontClass,
877
"fullName", "Ljava/lang/String;"));
878
CHECK_NULL(fontFileID = (*env)->GetFieldID(env, fcFontClass,
879
"fontFile", "Ljava/lang/String;"));
880
881
if ((libfontconfig = openFontConfig()) == NULL) {
882
return;
883
}
884
885
FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
886
FcPatternAddString =
887
(FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
888
FcConfigSubstitute =
889
(FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
890
FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
891
dlsym(libfontconfig, "FcDefaultSubstitute");
892
FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
893
FcPatternGetString =
894
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
895
FcPatternDestroy =
896
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
897
FcPatternGetCharSet =
898
(FcPatternGetCharSetFuncType)dlsym(libfontconfig,
899
"FcPatternGetCharSet");
900
FcFontSort =
901
(FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort");
902
FcFontSetDestroy =
903
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
904
FcCharSetUnion =
905
(FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion");
906
FcCharSetDestroy =
907
(FcCharSetDestroyFuncType)dlsym(libfontconfig, "FcCharSetDestroy");
908
FcCharSetSubtractCount =
909
(FcCharSetSubtractCountFuncType)dlsym(libfontconfig,
910
"FcCharSetSubtractCount");
911
FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
912
913
if (FcNameParse == NULL ||
914
FcPatternAddString == NULL ||
915
FcConfigSubstitute == NULL ||
916
FcDefaultSubstitute == NULL ||
917
FcFontMatch == NULL ||
918
FcPatternGetString == NULL ||
919
FcPatternDestroy == NULL ||
920
FcPatternGetCharSet == NULL ||
921
FcFontSetDestroy == NULL ||
922
FcCharSetUnion == NULL ||
923
FcCharSetDestroy == NULL ||
924
FcGetVersion == NULL ||
925
FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/
926
closeFontConfig(libfontconfig, JNI_FALSE);
927
return;
928
}
929
930
(*env)->SetIntField(env, fcInfoObj, fcVersionID, (*FcGetVersion)());
931
932
/* Optionally get the cache dir locations. This isn't
933
* available until v 2.4.x, but this is OK since on those later versions
934
* we can check the time stamps on the cache dirs to see if we
935
* are out of date. There are a couple of assumptions here. First
936
* that the time stamp on the directory changes when the contents are
937
* updated. Secondly that the locations don't change. The latter is
938
* most likely if a new version of fontconfig is installed, but we also
939
* invalidate the cache if we detect that. Arguably even that is "rare",
940
* and most likely is tied to an OS upgrade which gets a new file anyway.
941
*/
942
FcConfigGetCacheDirs =
943
(FcConfigGetCacheDirsFuncType)dlsym(libfontconfig,
944
"FcConfigGetCacheDirs");
945
FcStrListNext =
946
(FcStrListNextFuncType)dlsym(libfontconfig, "FcStrListNext");
947
FcStrListDone =
948
(FcStrListDoneFuncType)dlsym(libfontconfig, "FcStrListDone");
949
if (FcStrListNext != NULL && FcStrListDone != NULL &&
950
FcConfigGetCacheDirs != NULL) {
951
952
FcStrList* cacheDirs;
953
FcChar8* cacheDir;
954
int cnt = 0;
955
jobject cacheDirArray =
956
(*env)->GetObjectField(env, fcInfoObj, fcCacheDirsID);
957
int max = (*env)->GetArrayLength(env, cacheDirArray);
958
959
cacheDirs = (*FcConfigGetCacheDirs)(NULL);
960
if (cacheDirs != NULL) {
961
while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) {
962
jstr = (*env)->NewStringUTF(env, (const char*)cacheDir);
963
if (IS_NULL(jstr)) {
964
(*FcStrListDone)(cacheDirs);
965
return;
966
}
967
(*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr);
968
(*env)->DeleteLocalRef(env, jstr);
969
}
970
(*FcStrListDone)(cacheDirs);
971
}
972
}
973
974
locale = (*env)->GetStringUTFChars(env, localeStr, 0);
975
if (locale == NULL) {
976
(*env)->ExceptionClear(env);
977
JNU_ThrowOutOfMemoryError(env, "Could not create locale");
978
return;
979
}
980
981
arrlen = (*env)->GetArrayLength(env, fcCompFontArray);
982
for (i=0; i<arrlen; i++) {
983
FcFontSet* fontset;
984
int fn, j, fontCount, nfonts;
985
unsigned int minGlyphs;
986
FcChar8 **family, **styleStr, **fullname, **file;
987
jarray fcFontArr = NULL;
988
FcCharSet *unionCharset = NULL;
989
FcCharSet *prevUnionCharset = NULL;
990
991
fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i);
992
fcNameStr =
993
(jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameID));
994
fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
995
if (fcName == NULL) {
996
(*env)->DeleteLocalRef(env, fcCompFontObj);
997
(*env)->DeleteLocalRef(env, fcNameStr);
998
continue;
999
}
1000
pattern = (*FcNameParse)((FcChar8 *)fcName);
1001
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
1002
(*env)->DeleteLocalRef(env, fcNameStr);
1003
if (pattern == NULL) {
1004
closeFontConfig(libfontconfig, JNI_FALSE);
1005
if (locale) {
1006
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1007
}
1008
return;
1009
}
1010
1011
/* locale may not usually be necessary as fontconfig appears to apply
1012
* this anyway based on the user's environment. However we want
1013
* to use the value of the JDK startup locale so this should take
1014
* care of it.
1015
*/
1016
if (locale != NULL) {
1017
(*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
1018
}
1019
(*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
1020
(*FcDefaultSubstitute)(pattern);
1021
fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result);
1022
if (fontset == NULL) {
1023
(*FcPatternDestroy)(pattern);
1024
closeFontConfig(libfontconfig, JNI_FALSE);
1025
if (locale) {
1026
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1027
}
1028
return;
1029
}
1030
1031
/* fontconfig returned us "nfonts". If we are just getting the
1032
* first font, we set nfont to zero. Otherwise we use "nfonts".
1033
* Next create separate C arrrays of length nfonts for family file etc.
1034
* Inspect the returned fonts and the ones we like (adds enough glyphs)
1035
* are added to the arrays and we increment 'fontCount'.
1036
*/
1037
nfonts = fontset->nfont;
1038
family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1039
styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1040
fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1041
file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1042
if (family == NULL || styleStr == NULL ||
1043
fullname == NULL || file == NULL) {
1044
if (family != NULL) {
1045
free(family);
1046
}
1047
if (styleStr != NULL) {
1048
free(styleStr);
1049
}
1050
if (fullname != NULL) {
1051
free(fullname);
1052
}
1053
if (file != NULL) {
1054
free(file);
1055
}
1056
(*FcPatternDestroy)(pattern);
1057
(*FcFontSetDestroy)(fontset);
1058
closeFontConfig(libfontconfig, JNI_FALSE);
1059
if (locale) {
1060
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1061
}
1062
return;
1063
}
1064
fontCount = 0;
1065
minGlyphs = 20;
1066
if (debugMinGlyphsStr != NULL) {
1067
int val = minGlyphs;
1068
sscanf(debugMinGlyphsStr, "%5d", &val);
1069
if (val >= 0 && val <= 65536) {
1070
minGlyphs = val;
1071
}
1072
}
1073
1074
for (j=0; j<nfonts; j++) {
1075
FcPattern *fontPattern = fontset->fonts[j];
1076
FcChar8 *fontformat;
1077
FcCharSet *charset = NULL;
1078
1079
fontformat = NULL;
1080
(*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat);
1081
/* We only want TrueType fonts but some Linuxes still depend
1082
* on Type 1 fonts for some Locale support, so we'll allow
1083
* them there.
1084
*/
1085
if (fontformat != NULL
1086
&& (strcmp((char*)fontformat, "TrueType") != 0)
1087
#if defined(__linux__) || defined(_AIX)
1088
&& (strcmp((char*)fontformat, "Type 1") != 0)
1089
&& (strcmp((char*)fontformat, "CFF") != 0)
1090
#endif
1091
) {
1092
continue;
1093
}
1094
result = (*FcPatternGetCharSet)(fontPattern,
1095
FC_CHARSET, 0, &charset);
1096
if (result != FcResultMatch) {
1097
free(family);
1098
free(fullname);
1099
free(styleStr);
1100
free(file);
1101
(*FcPatternDestroy)(pattern);
1102
(*FcFontSetDestroy)(fontset);
1103
if (prevUnionCharset != NULL) {
1104
(*FcCharSetDestroy)(prevUnionCharset);
1105
}
1106
closeFontConfig(libfontconfig, JNI_FALSE);
1107
if (locale) {
1108
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1109
}
1110
return;
1111
}
1112
1113
/* We don't want 20 or 30 fonts, so once we hit 10 fonts,
1114
* then require that they really be adding value. Too many
1115
* adversely affects load time for minimal value-add.
1116
* This is still likely far more than we've had in the past.
1117
*/
1118
if (j==10) {
1119
minGlyphs = 50;
1120
}
1121
if (unionCharset == NULL) {
1122
unionCharset = charset;
1123
} else {
1124
if ((*FcCharSetSubtractCount)(charset, unionCharset)
1125
> minGlyphs) {
1126
unionCharset = (* FcCharSetUnion)(unionCharset, charset);
1127
if (prevUnionCharset != NULL) {
1128
(*FcCharSetDestroy)(prevUnionCharset);
1129
}
1130
prevUnionCharset = unionCharset;
1131
} else {
1132
continue;
1133
}
1134
}
1135
1136
fontCount++; // found a font we will use.
1137
(*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]);
1138
(*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]);
1139
(*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]);
1140
(*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]);
1141
if (!includeFallbacks) {
1142
break;
1143
}
1144
if (fontCount == 254) {
1145
break; // CompositeFont will only use up to 254 slots from here.
1146
}
1147
}
1148
1149
// Release last instance of CharSet union
1150
if (prevUnionCharset != NULL) {
1151
(*FcCharSetDestroy)(prevUnionCharset);
1152
}
1153
1154
/* Once we get here 'fontCount' is the number of returned fonts
1155
* we actually want to use, so we create 'fcFontArr' of that length.
1156
* The non-null entries of "family[]" etc are those fonts.
1157
* Then loop again over all nfonts adding just those non-null ones
1158
* to 'fcFontArr'. If its null (we didn't want the font)
1159
* then we don't enter the main body.
1160
* So we should never get more than 'fontCount' entries.
1161
*/
1162
if (includeFallbacks) {
1163
fcFontArr =
1164
(*env)->NewObjectArray(env, fontCount, fcFontClass, NULL);
1165
if (IS_NULL(fcFontArr)) {
1166
free(family);
1167
free(fullname);
1168
free(styleStr);
1169
free(file);
1170
(*FcPatternDestroy)(pattern);
1171
(*FcFontSetDestroy)(fontset);
1172
closeFontConfig(libfontconfig, JNI_FALSE);
1173
if (locale) {
1174
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1175
}
1176
return;
1177
}
1178
(*env)->SetObjectField(env,fcCompFontObj, fcAllFontsID, fcFontArr);
1179
}
1180
fn=0;
1181
1182
for (j=0;j<nfonts;j++) {
1183
if (family[j] != NULL) {
1184
jobject fcFont =
1185
(*env)->NewObject(env, fcFontClass, fcFontCons);
1186
if (IS_NULL(fcFont)) break;
1187
jstr = (*env)->NewStringUTF(env, (const char*)family[j]);
1188
if (IS_NULL(jstr)) break;
1189
(*env)->SetObjectField(env, fcFont, familyNameID, jstr);
1190
(*env)->DeleteLocalRef(env, jstr);
1191
if (file[j] != NULL) {
1192
jstr = (*env)->NewStringUTF(env, (const char*)file[j]);
1193
if (IS_NULL(jstr)) break;
1194
(*env)->SetObjectField(env, fcFont, fontFileID, jstr);
1195
(*env)->DeleteLocalRef(env, jstr);
1196
}
1197
if (styleStr[j] != NULL) {
1198
jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]);
1199
if (IS_NULL(jstr)) break;
1200
(*env)->SetObjectField(env, fcFont, styleNameID, jstr);
1201
(*env)->DeleteLocalRef(env, jstr);
1202
}
1203
if (fullname[j] != NULL) {
1204
jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]);
1205
if (IS_NULL(jstr)) break;
1206
(*env)->SetObjectField(env, fcFont, fullNameID, jstr);
1207
(*env)->DeleteLocalRef(env, jstr);
1208
}
1209
if (fn==0) {
1210
(*env)->SetObjectField(env, fcCompFontObj,
1211
fcFirstFontID, fcFont);
1212
}
1213
if (includeFallbacks) {
1214
(*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont);
1215
} else {
1216
(*env)->DeleteLocalRef(env, fcFont);
1217
break;
1218
}
1219
(*env)->DeleteLocalRef(env, fcFont);
1220
}
1221
}
1222
if (includeFallbacks) {
1223
(*env)->DeleteLocalRef(env, fcFontArr);
1224
}
1225
(*env)->DeleteLocalRef(env, fcCompFontObj);
1226
(*FcFontSetDestroy)(fontset);
1227
(*FcPatternDestroy)(pattern);
1228
free(family);
1229
free(styleStr);
1230
free(fullname);
1231
free(file);
1232
}
1233
1234
/* release resources and close the ".so" */
1235
1236
if (locale) {
1237
(*env)->ReleaseStringUTFChars(env, localeStr, (const char*)locale);
1238
}
1239
closeFontConfig(libfontconfig, JNI_TRUE);
1240
}
1241
1242