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