Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/solaris/native/sun/awt/fontpath.c
48789 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
ret = (*env)->NewStringUTF(env, ptr);
569
return ret;
570
}
571
572
#include <dlfcn.h>
573
574
#include <fontconfig/fontconfig.h>
575
576
577
static void* openFontConfig() {
578
579
char *homeEnv;
580
static char *homeEnvStr = "HOME="; /* must be static */
581
void* libfontconfig = NULL;
582
#ifdef __solaris__
583
#define SYSINFOBUFSZ 8
584
char sysinfobuf[SYSINFOBUFSZ];
585
#endif
586
587
/* Private workaround to not use fontconfig library.
588
* May be useful during testing/debugging
589
*/
590
char *useFC = getenv("USE_J2D_FONTCONFIG");
591
if (useFC != NULL && !strcmp(useFC, "no")) {
592
return NULL;
593
}
594
595
#ifdef __solaris__
596
/* fontconfig is likely not properly configured on S8/S9 - skip it,
597
* although allow user to override this behaviour with an env. variable
598
* ie if USE_J2D_FONTCONFIG=yes then we skip this test.
599
* NB "4" is the length of a string which matches our patterns.
600
*/
601
if (useFC == NULL || strcmp(useFC, "yes")) {
602
if (sysinfo(SI_RELEASE, sysinfobuf, SYSINFOBUFSZ) == 4) {
603
if ((!strcmp(sysinfobuf, "5.8") || !strcmp(sysinfobuf, "5.9"))) {
604
return NULL;
605
}
606
}
607
}
608
#endif
609
610
#if defined(_AIX)
611
/* On AIX, fontconfig is not a standard package supported by IBM.
612
* instead it has to be installed from the "AIX Toolbox for Linux Applications"
613
* site http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html
614
* and will be installed under /opt/freeware/lib/libfontconfig.a.
615
* Notice that the archive contains the real 32- and 64-bit shared libraries.
616
* We first try to load 'libfontconfig.so' from the default library path in the
617
* case the user has installed a private version of the library and if that
618
* doesn't succeed, we try the version from /opt/freeware/lib/libfontconfig.a
619
*/
620
libfontconfig = dlopen("libfontconfig.so", RTLD_LOCAL|RTLD_LAZY);
621
if (libfontconfig == NULL) {
622
libfontconfig = dlopen("/opt/freeware/lib/libfontconfig.a(libfontconfig.so.1)", RTLD_MEMBER|RTLD_LOCAL|RTLD_LAZY);
623
if (libfontconfig == NULL) {
624
return NULL;
625
}
626
}
627
#else
628
/* 64 bit sparc should pick up the right version from the lib path.
629
* New features may be added to libfontconfig, this is expected to
630
* be compatible with old features, but we may need to start
631
* distinguishing the library version, to know whether to expect
632
* certain symbols - and functionality - to be available.
633
* Also add explicit search for .so.1 in case .so symlink doesn't exist.
634
*/
635
libfontconfig = dlopen(FONTCONFIG_DLL_VERSIONED, RTLD_LOCAL|RTLD_LAZY);
636
if (libfontconfig == NULL) {
637
libfontconfig = dlopen(FONTCONFIG_DLL, RTLD_LOCAL|RTLD_LAZY);
638
if (libfontconfig == NULL) {
639
return NULL;
640
}
641
}
642
#endif
643
644
/* Version 1.0 of libfontconfig crashes if HOME isn't defined in
645
* the environment. This should generally never happen, but we can't
646
* control it, and can't control the version of fontconfig, so iff
647
* its not defined we set it to an empty value which is sufficient
648
* to prevent a crash. I considered unsetting it before exit, but
649
* it doesn't appear to work on Solaris, so I will leave it set.
650
*/
651
homeEnv = getenv("HOME");
652
if (homeEnv == NULL) {
653
putenv(homeEnvStr);
654
}
655
656
return libfontconfig;
657
}
658
659
typedef void* (FcFiniFuncType)();
660
661
static void closeFontConfig(void* libfontconfig, jboolean fcFini) {
662
663
/* NB FcFini is not in (eg) the Solaris 10 version of fontconfig. Its not
664
* clear if this means we are really leaking resources in those cases
665
* but it seems we should call this function when its available.
666
* But since the Swing GTK code may be still accessing the lib, its probably
667
* safest for now to just let this "leak" rather than potentially
668
* concurrently free global data still in use by other code.
669
*/
670
#if 0
671
if (fcFini) { /* release resources */
672
FcFiniFuncType FcFini = (FcFiniFuncType)dlsym(libfontconfig, "FcFini");
673
674
if (FcFini != NULL) {
675
(*FcFini)();
676
}
677
}
678
#endif
679
dlclose(libfontconfig);
680
}
681
682
typedef FcConfig* (*FcInitLoadConfigFuncType)();
683
typedef FcPattern* (*FcPatternBuildFuncType)(FcPattern *orig, ...);
684
typedef FcObjectSet* (*FcObjectSetFuncType)(const char *first, ...);
685
typedef FcFontSet* (*FcFontListFuncType)(FcConfig *config,
686
FcPattern *p,
687
FcObjectSet *os);
688
typedef FcResult (*FcPatternGetBoolFuncType)(const FcPattern *p,
689
const char *object,
690
int n,
691
FcBool *b);
692
typedef FcResult (*FcPatternGetIntegerFuncType)(const FcPattern *p,
693
const char *object,
694
int n,
695
int *i);
696
typedef FcResult (*FcPatternGetStringFuncType)(const FcPattern *p,
697
const char *object,
698
int n,
699
FcChar8 ** s);
700
typedef FcChar8* (*FcStrDirnameFuncType)(const FcChar8 *file);
701
typedef void (*FcPatternDestroyFuncType)(FcPattern *p);
702
typedef void (*FcFontSetDestroyFuncType)(FcFontSet *s);
703
typedef FcPattern* (*FcNameParseFuncType)(const FcChar8 *name);
704
typedef FcBool (*FcPatternAddStringFuncType)(FcPattern *p,
705
const char *object,
706
const FcChar8 *s);
707
typedef void (*FcDefaultSubstituteFuncType)(FcPattern *p);
708
typedef FcBool (*FcConfigSubstituteFuncType)(FcConfig *config,
709
FcPattern *p,
710
FcMatchKind kind);
711
typedef FcPattern* (*FcFontMatchFuncType)(FcConfig *config,
712
FcPattern *p,
713
FcResult *result);
714
typedef FcFontSet* (*FcFontSetCreateFuncType)();
715
typedef FcBool (*FcFontSetAddFuncType)(FcFontSet *s, FcPattern *font);
716
717
typedef FcResult (*FcPatternGetCharSetFuncType)(FcPattern *p,
718
const char *object,
719
int n,
720
FcCharSet **c);
721
typedef FcFontSet* (*FcFontSortFuncType)(FcConfig *config,
722
FcPattern *p,
723
FcBool trim,
724
FcCharSet **csp,
725
FcResult *result);
726
typedef FcCharSet* (*FcCharSetUnionFuncType)(const FcCharSet *a,
727
const FcCharSet *b);
728
typedef FcChar32 (*FcCharSetSubtractCountFuncType)(const FcCharSet *a,
729
const FcCharSet *b);
730
731
typedef int (*FcGetVersionFuncType)();
732
733
typedef FcStrList* (*FcConfigGetCacheDirsFuncType)(FcConfig *config);
734
typedef FcChar8* (*FcStrListNextFuncType)(FcStrList *list);
735
typedef FcChar8* (*FcStrListDoneFuncType)(FcStrList *list);
736
737
static char **getFontConfigLocations() {
738
739
char **fontdirs;
740
int numdirs = 0;
741
FcInitLoadConfigFuncType FcInitLoadConfig;
742
FcPatternBuildFuncType FcPatternBuild;
743
FcObjectSetFuncType FcObjectSetBuild;
744
FcFontListFuncType FcFontList;
745
FcPatternGetStringFuncType FcPatternGetString;
746
FcStrDirnameFuncType FcStrDirname;
747
FcPatternDestroyFuncType FcPatternDestroy;
748
FcFontSetDestroyFuncType FcFontSetDestroy;
749
750
FcConfig *fontconfig;
751
FcPattern *pattern;
752
FcObjectSet *objset;
753
FcFontSet *fontSet;
754
FcStrList *strList;
755
FcChar8 *str;
756
int i, f, found, len=0;
757
char **fontPath;
758
759
void* libfontconfig = openFontConfig();
760
761
if (libfontconfig == NULL) {
762
return NULL;
763
}
764
765
FcPatternBuild =
766
(FcPatternBuildFuncType)dlsym(libfontconfig, "FcPatternBuild");
767
FcObjectSetBuild =
768
(FcObjectSetFuncType)dlsym(libfontconfig, "FcObjectSetBuild");
769
FcFontList =
770
(FcFontListFuncType)dlsym(libfontconfig, "FcFontList");
771
FcPatternGetString =
772
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
773
FcStrDirname =
774
(FcStrDirnameFuncType)dlsym(libfontconfig, "FcStrDirname");
775
FcPatternDestroy =
776
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
777
FcFontSetDestroy =
778
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
779
780
if (FcPatternBuild == NULL ||
781
FcObjectSetBuild == NULL ||
782
FcPatternGetString == NULL ||
783
FcFontList == NULL ||
784
FcStrDirname == NULL ||
785
FcPatternDestroy == NULL ||
786
FcFontSetDestroy == NULL) { /* problem with the library: return. */
787
closeFontConfig(libfontconfig, JNI_FALSE);
788
return NULL;
789
}
790
791
/* Make calls into the fontconfig library to build a search for
792
* outline fonts, and to get the set of full file paths from the matches.
793
* This set is returned from the call to FcFontList(..)
794
* We allocate an array of char* pointers sufficient to hold all
795
* the matches + 1 extra which ensures there will be a NULL after all
796
* valid entries.
797
* We call FcStrDirname strip the file name from the path, and
798
* check if we have yet seen this directory. If not we add a pointer to
799
* it into our array of char*. Note that FcStrDirname returns newly
800
* allocated storage so we can use this in the return char** value.
801
* Finally we clean up, freeing allocated resources, and return the
802
* array of unique directories.
803
*/
804
pattern = (*FcPatternBuild)(NULL, FC_OUTLINE, FcTypeBool, FcTrue, NULL);
805
objset = (*FcObjectSetBuild)(FC_FILE, NULL);
806
fontSet = (*FcFontList)(NULL, pattern, objset);
807
if (fontSet == NULL) {
808
/* FcFontList() may return NULL if fonts are not installed. */
809
fontdirs = NULL;
810
} else {
811
fontdirs = (char**)calloc(fontSet->nfont+1, sizeof(char*));
812
for (f=0; f < fontSet->nfont; f++) {
813
FcChar8 *file;
814
FcChar8 *dir;
815
if ((*FcPatternGetString)(fontSet->fonts[f], FC_FILE, 0, &file) ==
816
FcResultMatch) {
817
dir = (*FcStrDirname)(file);
818
found = 0;
819
for (i=0;i<numdirs; i++) {
820
if (strcmp(fontdirs[i], (char*)dir) == 0) {
821
found = 1;
822
break;
823
}
824
}
825
if (!found) {
826
fontdirs[numdirs++] = (char*)dir;
827
} else {
828
free((char*)dir);
829
}
830
}
831
}
832
/* Free fontset if one was returned */
833
(*FcFontSetDestroy)(fontSet);
834
}
835
836
/* Free memory and close the ".so" */
837
(*FcPatternDestroy)(pattern);
838
closeFontConfig(libfontconfig, JNI_TRUE);
839
return fontdirs;
840
}
841
842
/* These are copied from sun.awt.SunHints.
843
* Consider initialising them as ints using JNI for more robustness.
844
*/
845
#define TEXT_AA_OFF 1
846
#define TEXT_AA_ON 2
847
#define TEXT_AA_LCD_HRGB 4
848
#define TEXT_AA_LCD_HBGR 5
849
#define TEXT_AA_LCD_VRGB 6
850
#define TEXT_AA_LCD_VBGR 7
851
852
JNIEXPORT jint JNICALL
853
Java_sun_font_FontConfigManager_getFontConfigAASettings
854
(JNIEnv *env, jclass obj, jstring localeStr, jstring fcNameStr) {
855
856
FcNameParseFuncType FcNameParse;
857
FcPatternAddStringFuncType FcPatternAddString;
858
FcConfigSubstituteFuncType FcConfigSubstitute;
859
FcDefaultSubstituteFuncType FcDefaultSubstitute;
860
FcFontMatchFuncType FcFontMatch;
861
FcPatternGetBoolFuncType FcPatternGetBool;
862
FcPatternGetIntegerFuncType FcPatternGetInteger;
863
FcPatternDestroyFuncType FcPatternDestroy;
864
865
FcPattern *pattern, *matchPattern;
866
FcResult result;
867
FcBool antialias = FcFalse;
868
int rgba = 0;
869
const char *locale=NULL, *fcName=NULL;
870
void* libfontconfig;
871
872
if (fcNameStr == NULL || localeStr == NULL) {
873
return -1;
874
}
875
876
fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
877
if (fcName == NULL) {
878
return -1;
879
}
880
locale = (*env)->GetStringUTFChars(env, localeStr, 0);
881
882
if ((libfontconfig = openFontConfig()) == NULL) {
883
(*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
884
if (locale) {
885
(*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale);
886
}
887
return -1;
888
}
889
890
FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
891
FcPatternAddString =
892
(FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
893
FcConfigSubstitute =
894
(FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
895
FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
896
dlsym(libfontconfig, "FcDefaultSubstitute");
897
FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
898
FcPatternGetBool = (FcPatternGetBoolFuncType)
899
dlsym(libfontconfig, "FcPatternGetBool");
900
FcPatternGetInteger = (FcPatternGetIntegerFuncType)
901
dlsym(libfontconfig, "FcPatternGetInteger");
902
FcPatternDestroy =
903
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
904
905
if (FcNameParse == NULL ||
906
FcPatternAddString == NULL ||
907
FcConfigSubstitute == NULL ||
908
FcDefaultSubstitute == NULL ||
909
FcFontMatch == NULL ||
910
FcPatternGetBool == NULL ||
911
FcPatternGetInteger == NULL ||
912
FcPatternDestroy == NULL) { /* problem with the library: return. */
913
914
(*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
915
if (locale) {
916
(*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale);
917
}
918
closeFontConfig(libfontconfig, JNI_FALSE);
919
return -1;
920
}
921
922
923
pattern = (*FcNameParse)((FcChar8 *)fcName);
924
if (locale != NULL) {
925
(*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
926
}
927
(*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
928
(*FcDefaultSubstitute)(pattern);
929
matchPattern = (*FcFontMatch)(NULL, pattern, &result);
930
/* Perhaps should call FcFontRenderPrepare() here as some pattern
931
* elements might change as a result of that call, but I'm not seeing
932
* any difference in testing.
933
*/
934
if (matchPattern) {
935
(*FcPatternGetBool)(matchPattern, FC_ANTIALIAS, 0, &antialias);
936
(*FcPatternGetInteger)(matchPattern, FC_RGBA, 0, &rgba);
937
(*FcPatternDestroy)(matchPattern);
938
}
939
(*FcPatternDestroy)(pattern);
940
941
(*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
942
if (locale) {
943
(*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale);
944
}
945
closeFontConfig(libfontconfig, JNI_TRUE);
946
947
if (antialias == FcFalse) {
948
return TEXT_AA_OFF;
949
} else if (rgba <= FC_RGBA_UNKNOWN || rgba >= FC_RGBA_NONE) {
950
return TEXT_AA_ON;
951
} else {
952
switch (rgba) {
953
case FC_RGBA_RGB : return TEXT_AA_LCD_HRGB;
954
case FC_RGBA_BGR : return TEXT_AA_LCD_HBGR;
955
case FC_RGBA_VRGB : return TEXT_AA_LCD_VRGB;
956
case FC_RGBA_VBGR : return TEXT_AA_LCD_VBGR;
957
default : return TEXT_AA_LCD_HRGB; // should not get here.
958
}
959
}
960
}
961
962
JNIEXPORT jint JNICALL
963
Java_sun_font_FontConfigManager_getFontConfigVersion
964
(JNIEnv *env, jclass obj) {
965
966
void* libfontconfig;
967
FcGetVersionFuncType FcGetVersion;
968
int version = 0;
969
970
if ((libfontconfig = openFontConfig()) == NULL) {
971
return 0;
972
}
973
974
FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
975
976
if (FcGetVersion == NULL) {
977
closeFontConfig(libfontconfig, JNI_FALSE);
978
return 0;
979
}
980
version = (*FcGetVersion)();
981
closeFontConfig(libfontconfig, JNI_FALSE);
982
983
return version;
984
}
985
986
987
JNIEXPORT void JNICALL
988
Java_sun_font_FontConfigManager_getFontConfig
989
(JNIEnv *env, jclass obj, jstring localeStr, jobject fcInfoObj,
990
jobjectArray fcCompFontArray, jboolean includeFallbacks) {
991
992
FcNameParseFuncType FcNameParse;
993
FcPatternAddStringFuncType FcPatternAddString;
994
FcConfigSubstituteFuncType FcConfigSubstitute;
995
FcDefaultSubstituteFuncType FcDefaultSubstitute;
996
FcFontMatchFuncType FcFontMatch;
997
FcPatternGetStringFuncType FcPatternGetString;
998
FcPatternDestroyFuncType FcPatternDestroy;
999
FcPatternGetCharSetFuncType FcPatternGetCharSet;
1000
FcFontSortFuncType FcFontSort;
1001
FcFontSetDestroyFuncType FcFontSetDestroy;
1002
FcCharSetUnionFuncType FcCharSetUnion;
1003
FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
1004
FcGetVersionFuncType FcGetVersion;
1005
FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs;
1006
FcStrListNextFuncType FcStrListNext;
1007
FcStrListDoneFuncType FcStrListDone;
1008
1009
int i, arrlen;
1010
jobject fcCompFontObj;
1011
jstring fcNameStr, jstr;
1012
const char *locale, *fcName;
1013
FcPattern *pattern;
1014
FcResult result;
1015
void* libfontconfig;
1016
jfieldID fcNameID, fcFirstFontID, fcAllFontsID, fcVersionID, fcCacheDirsID;
1017
jfieldID familyNameID, styleNameID, fullNameID, fontFileID;
1018
jmethodID fcFontCons;
1019
char* debugMinGlyphsStr = getenv("J2D_DEBUG_MIN_GLYPHS");
1020
1021
CHECK_NULL(fcInfoObj);
1022
CHECK_NULL(fcCompFontArray);
1023
1024
jclass fcInfoClass =
1025
(*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigInfo");
1026
CHECK_NULL(fcInfoClass);
1027
jclass fcCompFontClass =
1028
(*env)->FindClass(env, "sun/font/FontConfigManager$FcCompFont");
1029
CHECK_NULL(fcCompFontClass);
1030
jclass fcFontClass =
1031
(*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigFont");
1032
CHECK_NULL(fcFontClass);
1033
1034
1035
CHECK_NULL(fcVersionID = (*env)->GetFieldID(env, fcInfoClass, "fcVersion", "I"));
1036
CHECK_NULL(fcCacheDirsID = (*env)->GetFieldID(env, fcInfoClass, "cacheDirs",
1037
"[Ljava/lang/String;"));
1038
CHECK_NULL(fcNameID = (*env)->GetFieldID(env, fcCompFontClass,
1039
"fcName", "Ljava/lang/String;"));
1040
CHECK_NULL(fcFirstFontID = (*env)->GetFieldID(env, fcCompFontClass, "firstFont",
1041
"Lsun/font/FontConfigManager$FontConfigFont;"));
1042
CHECK_NULL(fcAllFontsID = (*env)->GetFieldID(env, fcCompFontClass, "allFonts",
1043
"[Lsun/font/FontConfigManager$FontConfigFont;"));
1044
CHECK_NULL(fcFontCons = (*env)->GetMethodID(env, fcFontClass, "<init>", "()V"));
1045
CHECK_NULL(familyNameID = (*env)->GetFieldID(env, fcFontClass,
1046
"familyName", "Ljava/lang/String;"));
1047
CHECK_NULL(styleNameID = (*env)->GetFieldID(env, fcFontClass,
1048
"styleStr", "Ljava/lang/String;"));
1049
CHECK_NULL(fullNameID = (*env)->GetFieldID(env, fcFontClass,
1050
"fullName", "Ljava/lang/String;"));
1051
CHECK_NULL(fontFileID = (*env)->GetFieldID(env, fcFontClass,
1052
"fontFile", "Ljava/lang/String;"));
1053
1054
jclass fontUtilitiesClass =
1055
(*env)->FindClass(env, "sun/font/FontUtilities");
1056
CHECK_NULL(fontUtilitiesClass);
1057
jfieldID isOpenJDKID =
1058
(*env)->GetStaticFieldID(env, fontUtilitiesClass, "isOpenJDK", "Z");
1059
CHECK_NULL(isOpenJDKID);
1060
jboolean isOpenJDK =
1061
(*env)->GetStaticBooleanField(env, fontUtilitiesClass, isOpenJDKID);
1062
1063
1064
if ((libfontconfig = openFontConfig()) == NULL) {
1065
return;
1066
}
1067
1068
FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
1069
FcPatternAddString =
1070
(FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
1071
FcConfigSubstitute =
1072
(FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
1073
FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
1074
dlsym(libfontconfig, "FcDefaultSubstitute");
1075
FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
1076
FcPatternGetString =
1077
(FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
1078
FcPatternDestroy =
1079
(FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
1080
FcPatternGetCharSet =
1081
(FcPatternGetCharSetFuncType)dlsym(libfontconfig,
1082
"FcPatternGetCharSet");
1083
FcFontSort =
1084
(FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort");
1085
FcFontSetDestroy =
1086
(FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
1087
FcCharSetUnion =
1088
(FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion");
1089
FcCharSetSubtractCount =
1090
(FcCharSetSubtractCountFuncType)dlsym(libfontconfig,
1091
"FcCharSetSubtractCount");
1092
FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
1093
1094
if (FcNameParse == NULL ||
1095
FcPatternAddString == NULL ||
1096
FcConfigSubstitute == NULL ||
1097
FcDefaultSubstitute == NULL ||
1098
FcFontMatch == NULL ||
1099
FcPatternGetString == NULL ||
1100
FcPatternDestroy == NULL ||
1101
FcPatternGetCharSet == NULL ||
1102
FcFontSetDestroy == NULL ||
1103
FcCharSetUnion == NULL ||
1104
FcGetVersion == NULL ||
1105
FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/
1106
closeFontConfig(libfontconfig, JNI_FALSE);
1107
return;
1108
}
1109
1110
(*env)->SetIntField(env, fcInfoObj, fcVersionID, (*FcGetVersion)());
1111
1112
/* Optionally get the cache dir locations. This isn't
1113
* available until v 2.4.x, but this is OK since on those later versions
1114
* we can check the time stamps on the cache dirs to see if we
1115
* are out of date. There are a couple of assumptions here. First
1116
* that the time stamp on the directory changes when the contents are
1117
* updated. Secondly that the locations don't change. The latter is
1118
* most likely if a new version of fontconfig is installed, but we also
1119
* invalidate the cache if we detect that. Arguably even that is "rare",
1120
* and most likely is tied to an OS upgrade which gets a new file anyway.
1121
*/
1122
FcConfigGetCacheDirs =
1123
(FcConfigGetCacheDirsFuncType)dlsym(libfontconfig,
1124
"FcConfigGetCacheDirs");
1125
FcStrListNext =
1126
(FcStrListNextFuncType)dlsym(libfontconfig, "FcStrListNext");
1127
FcStrListDone =
1128
(FcStrListDoneFuncType)dlsym(libfontconfig, "FcStrListDone");
1129
if (FcStrListNext != NULL && FcStrListDone != NULL &&
1130
FcConfigGetCacheDirs != NULL) {
1131
1132
FcStrList* cacheDirs;
1133
FcChar8* cacheDir;
1134
int cnt = 0;
1135
jobject cacheDirArray =
1136
(*env)->GetObjectField(env, fcInfoObj, fcCacheDirsID);
1137
int max = (*env)->GetArrayLength(env, cacheDirArray);
1138
1139
cacheDirs = (*FcConfigGetCacheDirs)(NULL);
1140
if (cacheDirs != NULL) {
1141
while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) {
1142
jstr = (*env)->NewStringUTF(env, (const char*)cacheDir);
1143
JNU_CHECK_EXCEPTION(env);
1144
1145
(*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr);
1146
}
1147
(*FcStrListDone)(cacheDirs);
1148
}
1149
}
1150
1151
locale = (*env)->GetStringUTFChars(env, localeStr, 0);
1152
if (locale == NULL) {
1153
(*env)->ExceptionClear(env);
1154
JNU_ThrowOutOfMemoryError(env, "Could not create locale");
1155
return;
1156
}
1157
1158
arrlen = (*env)->GetArrayLength(env, fcCompFontArray);
1159
for (i=0; i<arrlen; i++) {
1160
FcFontSet* fontset;
1161
int fn, j, fontCount, nfonts;
1162
unsigned int minGlyphs;
1163
FcChar8 **family, **styleStr, **fullname, **file;
1164
jarray fcFontArr;
1165
1166
fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i);
1167
fcNameStr =
1168
(jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameID));
1169
fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
1170
if (fcName == NULL) {
1171
continue;
1172
}
1173
pattern = (*FcNameParse)((FcChar8 *)fcName);
1174
(*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName);
1175
if (pattern == NULL) {
1176
closeFontConfig(libfontconfig, JNI_FALSE);
1177
return;
1178
}
1179
1180
/* locale may not usually be necessary as fontconfig appears to apply
1181
* this anyway based on the user's environment. However we want
1182
* to use the value of the JDK startup locale so this should take
1183
* care of it.
1184
*/
1185
if (locale != NULL) {
1186
(*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
1187
}
1188
(*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
1189
(*FcDefaultSubstitute)(pattern);
1190
fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result);
1191
if (fontset == NULL) {
1192
(*FcPatternDestroy)(pattern);
1193
closeFontConfig(libfontconfig, JNI_FALSE);
1194
return;
1195
}
1196
1197
/* fontconfig returned us "nfonts". If we are just getting the
1198
* first font, we set nfont to zero. Otherwise we use "nfonts".
1199
* Next create separate C arrrays of length nfonts for family file etc.
1200
* Inspect the returned fonts and the ones we like (adds enough glyphs)
1201
* are added to the arrays and we increment 'fontCount'.
1202
*/
1203
nfonts = fontset->nfont;
1204
family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1205
styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1206
fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1207
file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
1208
if (family == NULL || styleStr == NULL ||
1209
fullname == NULL || file == NULL) {
1210
if (family != NULL) {
1211
free(family);
1212
}
1213
if (styleStr != NULL) {
1214
free(styleStr);
1215
}
1216
if (fullname != NULL) {
1217
free(fullname);
1218
}
1219
if (file != NULL) {
1220
free(file);
1221
}
1222
(*FcPatternDestroy)(pattern);
1223
(*FcFontSetDestroy)(fontset);
1224
closeFontConfig(libfontconfig, JNI_FALSE);
1225
return;
1226
}
1227
fontCount = 0;
1228
minGlyphs = 20;
1229
if (debugMinGlyphsStr != NULL) {
1230
int val = minGlyphs;
1231
sscanf(debugMinGlyphsStr, "%5d", &val);
1232
if (val >= 0 && val <= 65536) {
1233
minGlyphs = val;
1234
}
1235
}
1236
FcCharSet *unionCharset = NULL;
1237
for (j=0; j<nfonts; j++) {
1238
FcPattern *fontPattern = fontset->fonts[j];
1239
FcChar8 *fontformat;
1240
FcCharSet *charset = NULL;
1241
1242
fontformat = NULL;
1243
(*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat);
1244
/* We only want TrueType fonts but some Linuxes still depend
1245
* on Type 1 fonts for some Locale support, so we'll allow
1246
* them there.
1247
*/
1248
if (fontformat != NULL
1249
&& (strcmp((char*)fontformat, "TrueType") != 0)
1250
#if defined(__linux__) || defined(_AIX)
1251
&& (strcmp((char*)fontformat, "Type 1") != 0)
1252
&& !(isOpenJDK && (strcmp((char*)fontformat, "CFF") == 0))
1253
#endif
1254
) {
1255
continue;
1256
}
1257
result = (*FcPatternGetCharSet)(fontPattern,
1258
FC_CHARSET, 0, &charset);
1259
if (result != FcResultMatch) {
1260
free(family);
1261
free(fullname);
1262
free(styleStr);
1263
free(file);
1264
(*FcPatternDestroy)(pattern);
1265
(*FcFontSetDestroy)(fontset);
1266
closeFontConfig(libfontconfig, JNI_FALSE);
1267
return;
1268
}
1269
1270
/* We don't want 20 or 30 fonts, so once we hit 10 fonts,
1271
* then require that they really be adding value. Too many
1272
* adversely affects load time for minimal value-add.
1273
* This is still likely far more than we've had in the past.
1274
*/
1275
if (j==10) {
1276
minGlyphs = 50;
1277
}
1278
if (unionCharset == NULL) {
1279
unionCharset = charset;
1280
} else {
1281
if ((*FcCharSetSubtractCount)(charset, unionCharset)
1282
> minGlyphs) {
1283
unionCharset = (* FcCharSetUnion)(unionCharset, charset);
1284
} else {
1285
continue;
1286
}
1287
}
1288
1289
fontCount++; // found a font we will use.
1290
(*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]);
1291
(*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]);
1292
(*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]);
1293
(*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]);
1294
if (!includeFallbacks) {
1295
break;
1296
}
1297
if (fontCount == 254) {
1298
break; // CompositeFont will only use up to 254 slots from here.
1299
}
1300
}
1301
1302
/* Once we get here 'fontCount' is the number of returned fonts
1303
* we actually want to use, so we create 'fcFontArr' of that length.
1304
* The non-null entries of "family[]" etc are those fonts.
1305
* Then loop again over all nfonts adding just those non-null ones
1306
* to 'fcFontArr'. If its null (we didn't want the font)
1307
* then we don't enter the main body.
1308
* So we should never get more than 'fontCount' entries.
1309
*/
1310
if (includeFallbacks) {
1311
fcFontArr =
1312
(*env)->NewObjectArray(env, fontCount, fcFontClass, NULL);
1313
if (IS_NULL(fcFontArr)) {
1314
free(family);
1315
free(fullname);
1316
free(styleStr);
1317
free(file);
1318
(*FcPatternDestroy)(pattern);
1319
(*FcFontSetDestroy)(fontset);
1320
closeFontConfig(libfontconfig, JNI_FALSE);
1321
return;
1322
}
1323
(*env)->SetObjectField(env,fcCompFontObj, fcAllFontsID, fcFontArr);
1324
}
1325
fn=0;
1326
1327
for (j=0;j<nfonts;j++) {
1328
if (family[j] != NULL) {
1329
jobject fcFont =
1330
(*env)->NewObject(env, fcFontClass, fcFontCons);
1331
if (IS_NULL(fcFont)) break;
1332
jstr = (*env)->NewStringUTF(env, (const char*)family[j]);
1333
if (IS_NULL(jstr)) break;
1334
(*env)->SetObjectField(env, fcFont, familyNameID, jstr);
1335
if (file[j] != NULL) {
1336
jstr = (*env)->NewStringUTF(env, (const char*)file[j]);
1337
if (IS_NULL(jstr)) break;
1338
(*env)->SetObjectField(env, fcFont, fontFileID, jstr);
1339
}
1340
if (styleStr[j] != NULL) {
1341
jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]);
1342
if (IS_NULL(jstr)) break;
1343
(*env)->SetObjectField(env, fcFont, styleNameID, jstr);
1344
}
1345
if (fullname[j] != NULL) {
1346
jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]);
1347
if (IS_NULL(jstr)) break;
1348
(*env)->SetObjectField(env, fcFont, fullNameID, jstr);
1349
}
1350
if (fn==0) {
1351
(*env)->SetObjectField(env, fcCompFontObj,
1352
fcFirstFontID, fcFont);
1353
}
1354
if (includeFallbacks) {
1355
(*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont);
1356
} else {
1357
break;
1358
}
1359
}
1360
}
1361
(*FcFontSetDestroy)(fontset);
1362
(*FcPatternDestroy)(pattern);
1363
free(family);
1364
free(styleStr);
1365
free(fullname);
1366
free(file);
1367
}
1368
1369
/* release resources and close the ".so" */
1370
1371
if (locale) {
1372
(*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale);
1373
}
1374
closeFontConfig(libfontconfig, JNI_TRUE);
1375
}
1376
1377