1 /* 2 * Copyright (c) 1998, 2015, 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 JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE); 182 } else { 183 isLocal = True; 184 } 185 isLocalSet = True; 186 } 187 188 return isLocal; 189 } 190 191 static void AddFontsToX11FontPath ( fDirRecord *fDirP ) 192 { 193 char *onePath; 194 int index, nPaths; 195 int origNumPaths, length; 196 int origIndex; 197 int totalDirCount; 198 char **origFontPath; 199 char **tempFontPath; 200 int doNotAppend; 201 int *appendDirList; 202 char **newFontPath; 203 int err, compareLength; 204 char fontDirPath[512]; 205 int dirFile; 206 207 doNotAppend = 0; 208 209 if ( fDirP->num == 0 ) return; 210 211 appendDirList = SAFE_SIZE_ARRAY_ALLOC(malloc, fDirP->num, sizeof ( int )); 212 if ( appendDirList == NULL ) { 213 return; /* if it fails we cannot do much */ 214 } 215 216 origFontPath = XGetFontPath ( awt_display, &nPaths ); 217 218 totalDirCount = nPaths; 219 origNumPaths = nPaths; 220 tempFontPath = origFontPath; 221 222 223 for (index = 0; index < fDirP->num; index++ ) { 224 225 doNotAppend = 0; 226 227 tempFontPath = origFontPath; 228 for ( origIndex = 0; origIndex < nPaths; origIndex++ ) { 229 230 onePath = *tempFontPath; 231 232 compareLength = strlen ( onePath ); 233 if ( onePath[compareLength -1] == '/' ) 234 compareLength--; 235 236 /* there is a slash at the end of every solaris X11 font path name */ 237 if ( strncmp ( onePath, fDirP->name[index], compareLength ) == 0 ) { 238 doNotAppend = 1; 239 break; 240 } 241 tempFontPath++; 242 } 243 244 appendDirList[index] = 0; 245 if ( doNotAppend == 0 ) { 246 strcpy ( fontDirPath, fDirP->name[index] ); 247 strcat ( fontDirPath, "/fonts.dir" ); 248 dirFile = open ( fontDirPath, O_RDONLY, 0 ); 249 if ( dirFile == -1 ) { 250 doNotAppend = 1; 251 } else { 252 close ( dirFile ); 253 totalDirCount++; 254 appendDirList[index] = 1; 255 } 256 } 257 258 } 259 260 /* if no changes are required do not bother to do a setfontpath */ 261 if ( totalDirCount == nPaths ) { 262 free ( ( void *) appendDirList ); 263 XFreeFontPath ( origFontPath ); 264 return; 265 } 266 267 268 newFontPath = SAFE_SIZE_ARRAY_ALLOC(malloc, totalDirCount, sizeof ( char **) ); 269 /* if it fails free things and get out */ 270 if ( newFontPath == NULL ) { 271 free ( ( void *) appendDirList ); 272 XFreeFontPath ( origFontPath ); 273 return; 274 } 275 276 for ( origIndex = 0; origIndex < nPaths; origIndex++ ) { 277 onePath = origFontPath[origIndex]; 278 newFontPath[origIndex] = onePath; 279 } 280 281 /* now add the other font paths */ 282 283 for (index = 0; index < fDirP->num; index++ ) { 284 285 if ( appendDirList[index] == 1 ) { 286 287 /* printf ( "Appending %s\n", fDirP->name[index] ); */ 288 289 onePath = SAFE_SIZE_ARRAY_ALLOC(malloc, strlen (fDirP->name[index]) + 2, sizeof( char ) ); 290 if (onePath == NULL) { 291 free ( ( void *) appendDirList ); 292 XFreeFontPath ( origFontPath ); 293 return; 294 } 295 strcpy ( onePath, fDirP->name[index] ); 296 strcat ( onePath, "/" ); 297 newFontPath[nPaths++] = onePath; 298 /* printf ( "The path to be appended is %s\n", onePath ); */ 299 } 300 } 301 302 /* printf ( "The dir count = %d\n", totalDirCount ); */ 303 free ( ( void *) appendDirList ); 304 305 XSetFontPath ( awt_display, newFontPath, totalDirCount ); 306 307 for ( index = origNumPaths; index < totalDirCount; index++ ) { 308 free( newFontPath[index] ); 309 } 310 311 free ( (void *) newFontPath ); 312 XFreeFontPath ( origFontPath ); 313 return; 314 } 315 #endif /* !HEADLESS */ 316 317 318 #ifndef HEADLESS 319 static char **getX11FontPath () 320 { 321 char **x11Path, **fontdirs; 322 int i, pos, slen, nPaths, numDirs; 323 324 x11Path = XGetFontPath (awt_display, &nPaths); 325 326 /* This isn't ever going to be perfect: the font path may contain 327 * much we aren't interested in, but the cost should be moderate 328 * Exclude all directories that contain the strings "Speedo","/F3/", 329 * "75dpi", "100dpi", "misc" or "bitmap", or don't begin with a "/", 330 * the last of which should exclude font servers. 331 * Also exclude the user specific ".gnome*" directories which 332 * aren't going to contain the system fonts we need. 333 * Hopefully we are left only with Type1 and TrueType directories. 334 * It doesn't matter much if there are extraneous directories, it'll just 335 * cost us a little wasted effort upstream. 336 */ 337 fontdirs = (char**)calloc(nPaths+1, sizeof(char*)); 338 pos = 0; 339 for (i=0; i < nPaths; i++) { 340 if (x11Path[i][0] != '/') { 341 continue; 342 } 343 if (strstr(x11Path[i], "/75dpi") != NULL) { 344 continue; 345 } 346 if (strstr(x11Path[i], "/100dpi") != NULL) { 347 continue; 348 } 349 if (strstr(x11Path[i], "/misc") != NULL) { 350 continue; 351 } 352 if (strstr(x11Path[i], "/Speedo") != NULL) { 353 continue; 354 } 355 if (strstr(x11Path[i], ".gnome") != NULL) { 356 continue; 357 } 358 #ifdef __solaris__ 359 if (strstr(x11Path[i], "/F3/") != NULL) { 360 continue; 361 } 362 if (strstr(x11Path[i], "bitmap") != NULL) { 363 continue; 364 } 365 #endif 366 fontdirs[pos] = strdup(x11Path[i]); 367 slen = strlen(fontdirs[pos]); 368 if (slen > 0 && fontdirs[pos][slen-1] == '/') { 369 fontdirs[pos][slen-1] = '\0'; /* null out trailing "/" */ 370 } 371 pos++; 372 } 373 374 XFreeFontPath(x11Path); 375 if (pos == 0) { 376 free(fontdirs); 377 fontdirs = NULL; 378 } 379 return fontdirs; 380 } 381 382 383 #endif /* !HEADLESS */ 384 385 #if defined(__linux__) 386 /* from awt_LoadLibrary.c */ 387 JNIEXPORT jboolean JNICALL AWTIsHeadless(); 388 #endif 389 390 /* This eliminates duplicates, at a non-linear but acceptable cost 391 * since the lists are expected to be reasonably short, and then 392 * deletes references to non-existent directories, and returns 393 * a single path consisting of unique font directories. 394 */ 395 static char* mergePaths(char **p1, char **p2, char **p3, jboolean noType1) { 396 397 int len1=0, len2=0, len3=0, totalLen=0, numDirs=0, 398 currLen, i, j, found, pathLen=0; 399 char **ptr, **fontdirs; 400 char *fontPath = NULL; 401 402 if (p1 != NULL) { 403 ptr = p1; 404 while (*ptr++ != NULL) len1++; 405 } 406 if (p2 != NULL) { 407 ptr = p2; 408 409 while (*ptr++ != NULL) len2++; 410 } 411 if (p3 != NULL) { 412 ptr = p3; 413 while (*ptr++ != NULL) len3++; 414 } 415 totalLen = len1+len2+len3; 416 fontdirs = (char**)calloc(totalLen, sizeof(char*)); 417 418 for (i=0; i < len1; i++) { 419 if (noType1 && strstr(p1[i], "Type1") != NULL) { 420 continue; 421 } 422 fontdirs[numDirs++] = p1[i]; 423 } 424 425 currLen = numDirs; /* only compare against previous path dirs */ 426 for (i=0; i < len2; i++) { 427 if (noType1 && strstr(p2[i], "Type1") != NULL) { 428 continue; 429 } 430 found = 0; 431 for (j=0; j < currLen; j++) { 432 if (strcmp(fontdirs[j], p2[i]) == 0) { 433 found = 1; 434 break; 435 } 436 } 437 if (!found) { 438 fontdirs[numDirs++] = p2[i]; 439 } 440 } 441 442 currLen = numDirs; /* only compare against previous path dirs */ 443 for (i=0; i < len3; i++) { 444 if (noType1 && strstr(p3[i], "Type1") != NULL) { 445 continue; 446 } 447 found = 0; 448 for (j=0; j < currLen; j++) { 449 if (strcmp(fontdirs[j], p3[i]) == 0) { 450 found = 1; 451 break; 452 } 453 } 454 if (!found) { 455 fontdirs[numDirs++] = p3[i]; 456 } 457 } 458 459 /* Now fontdirs contains unique dirs and numDirs records how many. 460 * What we don't know is if they all exist. On reflection I think 461 * this isn't an issue, so for now I will return all these locations, 462 * converted to one string */ 463 for (i=0; i<numDirs; i++) { 464 pathLen += (strlen(fontdirs[i]) + 1); 465 } 466 if (pathLen > 0 && (fontPath = malloc(pathLen))) { 467 *fontPath = '\0'; 468 for (i = 0; i<numDirs; i++) { 469 if (i != 0) { 470 strcat(fontPath, ":"); 471 } 472 strcat(fontPath, fontdirs[i]); 473 } 474 } 475 free (fontdirs); 476 477 return fontPath; 478 } 479 480 /* 481 * The goal of this function is to find all "system" fonts which 482 * are needed by the JRE to display text in supported locales etc, and 483 * to support APIs which allow users to enumerate all system fonts and use 484 * them from their Java applications. 485 * The preferred mechanism is now using the new "fontconfig" library 486 * This exists on newer versions of Linux and Solaris (S10 and above) 487 * The library is dynamically located. The results are merged with 488 * a set of "known" locations and with the X11 font path, if running in 489 * a local X11 environment. 490 * The hardwired paths are built into the JDK binary so as new font locations 491 * are created on a host plaform for them to be located by the JRE they will 492 * need to be added ito the host's font configuration database, typically 493 * /etc/fonts/local.conf, and to ensure that directory contains a fonts.dir 494 * NB: Fontconfig also depends heavily for performance on the host O/S 495 * maintaining up to date caches. 496 * This is consistent with the requirements of the desktop environments 497 * on these OSes. 498 * This also frees us from X11 APIs as JRE is required to function in 499 * a "headless" mode where there is no Xserver. 500 */ 501 static char *getPlatformFontPathChars(JNIEnv *env, jboolean noType1, jboolean isX11) { 502 503 char **fcdirs = NULL, **x11dirs = NULL, **knowndirs = NULL, *path = NULL; 504 505 /* As of 1.5 we try to use fontconfig on both Solaris and Linux. 506 * If its not available NULL is returned. 507 */ 508 fcdirs = getFontConfigLocations(); 509 510 #if defined(__linux__) 511 knowndirs = fullLinuxFontPath; 512 #elif defined(__solaris__) 513 knowndirs = fullSolarisFontPath; 514 #elif defined(_AIX) 515 knowndirs = fullAixFontPath; 516 #endif 517 /* REMIND: this code requires to be executed when the GraphicsEnvironment 518 * is already initialised. That is always true, but if it were not so, 519 * this code could throw an exception and the fontpath would fail to 520 * be initialised. 521 */ 522 #ifndef HEADLESS 523 if (isX11) { // The following only works in an x11 environment. 524 #if defined(__linux__) 525 /* There's no headless build on linux ... */ 526 if (!AWTIsHeadless()) { /* .. so need to call a function to check */ 527 #endif 528 /* Using the X11 font path to locate font files is now a fallback 529 * useful only if fontconfig failed, or is incomplete. So we could 530 * remove this code completely and the consequences should be rare 531 * and non-fatal. If this happens, then the calling Java code can 532 * be modified to no longer require that the AWT lock (the X11GE) 533 * be initialised prior to calling this code. 534 */ 535 AWT_LOCK(); 536 if (isDisplayLocal(env)) { 537 x11dirs = getX11FontPath(); 538 } 539 AWT_UNLOCK(); 540 #if defined(__linux__) 541 } 542 #endif 543 } 544 #endif /* !HEADLESS */ 545 path = mergePaths(fcdirs, x11dirs, knowndirs, noType1); 546 if (fcdirs != NULL) { 547 char **p = fcdirs; 548 while (*p != NULL) free(*p++); 549 free(fcdirs); 550 } 551 552 if (x11dirs != NULL) { 553 char **p = x11dirs; 554 while (*p != NULL) free(*p++); 555 free(x11dirs); 556 } 557 558 return path; 559 } 560 561 JNIEXPORT jstring JNICALL Java_sun_awt_FcFontManager_getFontPathNative 562 (JNIEnv *env, jobject thiz, jboolean noType1, jboolean isX11) { 563 jstring ret; 564 static char *ptr = NULL; /* retain result across calls */ 565 566 if (ptr == NULL) { 567 ptr = getPlatformFontPathChars(env, noType1, isX11); 568 } 569 ret = (*env)->NewStringUTF(env, ptr); 570 return ret; 571 } 572 573 #include <dlfcn.h> 574 575 #include "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 fontdirs = (char**)calloc(fontSet->nfont+1, sizeof(char*)); 809 for (f=0; f < fontSet->nfont; f++) { 810 FcChar8 *file; 811 FcChar8 *dir; 812 if ((*FcPatternGetString)(fontSet->fonts[f], FC_FILE, 0, &file) == 813 FcResultMatch) { 814 dir = (*FcStrDirname)(file); 815 found = 0; 816 for (i=0;i<numdirs; i++) { 817 if (strcmp(fontdirs[i], (char*)dir) == 0) { 818 found = 1; 819 break; 820 } 821 } 822 if (!found) { 823 fontdirs[numdirs++] = (char*)dir; 824 } else { 825 free((char*)dir); 826 } 827 } 828 } 829 830 /* Free memory and close the ".so" */ 831 (*FcFontSetDestroy)(fontSet); 832 (*FcPatternDestroy)(pattern); 833 closeFontConfig(libfontconfig, JNI_TRUE); 834 return fontdirs; 835 } 836 837 /* These are copied from sun.awt.SunHints. 838 * Consider initialising them as ints using JNI for more robustness. 839 */ 840 #define TEXT_AA_OFF 1 841 #define TEXT_AA_ON 2 842 #define TEXT_AA_LCD_HRGB 4 843 #define TEXT_AA_LCD_HBGR 5 844 #define TEXT_AA_LCD_VRGB 6 845 #define TEXT_AA_LCD_VBGR 7 846 847 JNIEXPORT jint JNICALL 848 Java_sun_font_FontConfigManager_getFontConfigAASettings 849 (JNIEnv *env, jclass obj, jstring localeStr, jstring fcNameStr) { 850 851 FcNameParseFuncType FcNameParse; 852 FcPatternAddStringFuncType FcPatternAddString; 853 FcConfigSubstituteFuncType FcConfigSubstitute; 854 FcDefaultSubstituteFuncType FcDefaultSubstitute; 855 FcFontMatchFuncType FcFontMatch; 856 FcPatternGetBoolFuncType FcPatternGetBool; 857 FcPatternGetIntegerFuncType FcPatternGetInteger; 858 FcPatternDestroyFuncType FcPatternDestroy; 859 860 FcPattern *pattern, *matchPattern; 861 FcResult result; 862 FcBool antialias = FcFalse; 863 int rgba = 0; 864 const char *locale=NULL, *fcName=NULL; 865 void* libfontconfig; 866 867 if (fcNameStr == NULL || localeStr == NULL) { 868 return -1; 869 } 870 871 fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0); 872 if (fcName == NULL) { 873 return -1; 874 } 875 locale = (*env)->GetStringUTFChars(env, localeStr, 0); 876 877 if ((libfontconfig = openFontConfig()) == NULL) { 878 (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName); 879 if (locale) { 880 (*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale); 881 } 882 return -1; 883 } 884 885 FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse"); 886 FcPatternAddString = 887 (FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString"); 888 FcConfigSubstitute = 889 (FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute"); 890 FcDefaultSubstitute = (FcDefaultSubstituteFuncType) 891 dlsym(libfontconfig, "FcDefaultSubstitute"); 892 FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch"); 893 FcPatternGetBool = (FcPatternGetBoolFuncType) 894 dlsym(libfontconfig, "FcPatternGetBool"); 895 FcPatternGetInteger = (FcPatternGetIntegerFuncType) 896 dlsym(libfontconfig, "FcPatternGetInteger"); 897 FcPatternDestroy = 898 (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy"); 899 900 if (FcNameParse == NULL || 901 FcPatternAddString == NULL || 902 FcConfigSubstitute == NULL || 903 FcDefaultSubstitute == NULL || 904 FcFontMatch == NULL || 905 FcPatternGetBool == NULL || 906 FcPatternGetInteger == NULL || 907 FcPatternDestroy == NULL) { /* problem with the library: return. */ 908 909 (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName); 910 if (locale) { 911 (*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale); 912 } 913 closeFontConfig(libfontconfig, JNI_FALSE); 914 return -1; 915 } 916 917 918 pattern = (*FcNameParse)((FcChar8 *)fcName); 919 if (locale != NULL) { 920 (*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale); 921 } 922 (*FcConfigSubstitute)(NULL, pattern, FcMatchPattern); 923 (*FcDefaultSubstitute)(pattern); 924 matchPattern = (*FcFontMatch)(NULL, pattern, &result); 925 /* Perhaps should call FcFontRenderPrepare() here as some pattern 926 * elements might change as a result of that call, but I'm not seeing 927 * any difference in testing. 928 */ 929 if (matchPattern) { 930 (*FcPatternGetBool)(matchPattern, FC_ANTIALIAS, 0, &antialias); 931 (*FcPatternGetInteger)(matchPattern, FC_RGBA, 0, &rgba); 932 (*FcPatternDestroy)(matchPattern); 933 } 934 (*FcPatternDestroy)(pattern); 935 936 (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName); 937 if (locale) { 938 (*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale); 939 } 940 closeFontConfig(libfontconfig, JNI_TRUE); 941 942 if (antialias == FcFalse) { 943 return TEXT_AA_OFF; 944 } else if (rgba <= FC_RGBA_UNKNOWN || rgba >= FC_RGBA_NONE) { 945 return TEXT_AA_ON; 946 } else { 947 switch (rgba) { 948 case FC_RGBA_RGB : return TEXT_AA_LCD_HRGB; 949 case FC_RGBA_BGR : return TEXT_AA_LCD_HBGR; 950 case FC_RGBA_VRGB : return TEXT_AA_LCD_VRGB; 951 case FC_RGBA_VBGR : return TEXT_AA_LCD_VBGR; 952 default : return TEXT_AA_LCD_HRGB; // should not get here. 953 } 954 } 955 } 956 957 JNIEXPORT jint JNICALL 958 Java_sun_font_FontConfigManager_getFontConfigVersion 959 (JNIEnv *env, jclass obj) { 960 961 void* libfontconfig; 962 FcGetVersionFuncType FcGetVersion; 963 int version = 0; 964 965 if ((libfontconfig = openFontConfig()) == NULL) { 966 return 0; 967 } 968 969 FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion"); 970 971 if (FcGetVersion == NULL) { 972 closeFontConfig(libfontconfig, JNI_FALSE); 973 return 0; 974 } 975 version = (*FcGetVersion)(); 976 closeFontConfig(libfontconfig, JNI_FALSE); 977 978 return version; 979 } 980 981 982 JNIEXPORT void JNICALL 983 Java_sun_font_FontConfigManager_getFontConfig 984 (JNIEnv *env, jclass obj, jstring localeStr, jobject fcInfoObj, 985 jobjectArray fcCompFontArray, jboolean includeFallbacks) { 986 987 FcNameParseFuncType FcNameParse; 988 FcPatternAddStringFuncType FcPatternAddString; 989 FcConfigSubstituteFuncType FcConfigSubstitute; 990 FcDefaultSubstituteFuncType FcDefaultSubstitute; 991 FcFontMatchFuncType FcFontMatch; 992 FcPatternGetStringFuncType FcPatternGetString; 993 FcPatternDestroyFuncType FcPatternDestroy; 994 FcPatternGetCharSetFuncType FcPatternGetCharSet; 995 FcFontSortFuncType FcFontSort; 996 FcFontSetDestroyFuncType FcFontSetDestroy; 997 FcCharSetUnionFuncType FcCharSetUnion; 998 FcCharSetSubtractCountFuncType FcCharSetSubtractCount; 999 FcGetVersionFuncType FcGetVersion; 1000 FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs; 1001 FcStrListNextFuncType FcStrListNext; 1002 FcStrListDoneFuncType FcStrListDone; 1003 1004 int i, arrlen; 1005 jobject fcCompFontObj; 1006 jstring fcNameStr, jstr; 1007 const char *locale, *fcName; 1008 FcPattern *pattern; 1009 FcResult result; 1010 void* libfontconfig; 1011 jfieldID fcNameID, fcFirstFontID, fcAllFontsID, fcVersionID, fcCacheDirsID; 1012 jfieldID familyNameID, styleNameID, fullNameID, fontFileID; 1013 jmethodID fcFontCons; 1014 char* debugMinGlyphsStr = getenv("J2D_DEBUG_MIN_GLYPHS"); 1015 jclass fcInfoClass; 1016 jclass fcCompFontClass; 1017 jclass fcFontClass; 1018 1019 CHECK_NULL(fcInfoObj); 1020 CHECK_NULL(fcCompFontArray); 1021 1022 fcInfoClass = 1023 (*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigInfo"); 1024 CHECK_NULL(fcInfoClass); 1025 fcCompFontClass = 1026 (*env)->FindClass(env, "sun/font/FontConfigManager$FcCompFont"); 1027 CHECK_NULL(fcCompFontClass); 1028 fcFontClass = 1029 (*env)->FindClass(env, "sun/font/FontConfigManager$FontConfigFont"); 1030 CHECK_NULL(fcFontClass); 1031 1032 1033 CHECK_NULL(fcVersionID = (*env)->GetFieldID(env, fcInfoClass, "fcVersion", "I")); 1034 CHECK_NULL(fcCacheDirsID = (*env)->GetFieldID(env, fcInfoClass, "cacheDirs", 1035 "[Ljava/lang/String;")); 1036 CHECK_NULL(fcNameID = (*env)->GetFieldID(env, fcCompFontClass, 1037 "fcName", "Ljava/lang/String;")); 1038 CHECK_NULL(fcFirstFontID = (*env)->GetFieldID(env, fcCompFontClass, "firstFont", 1039 "Lsun/font/FontConfigManager$FontConfigFont;")); 1040 CHECK_NULL(fcAllFontsID = (*env)->GetFieldID(env, fcCompFontClass, "allFonts", 1041 "[Lsun/font/FontConfigManager$FontConfigFont;")); 1042 CHECK_NULL(fcFontCons = (*env)->GetMethodID(env, fcFontClass, "<init>", "()V")); 1043 CHECK_NULL(familyNameID = (*env)->GetFieldID(env, fcFontClass, 1044 "familyName", "Ljava/lang/String;")); 1045 CHECK_NULL(styleNameID = (*env)->GetFieldID(env, fcFontClass, 1046 "styleStr", "Ljava/lang/String;")); 1047 CHECK_NULL(fullNameID = (*env)->GetFieldID(env, fcFontClass, 1048 "fullName", "Ljava/lang/String;")); 1049 CHECK_NULL(fontFileID = (*env)->GetFieldID(env, fcFontClass, 1050 "fontFile", "Ljava/lang/String;")); 1051 1052 if ((libfontconfig = openFontConfig()) == NULL) { 1053 return; 1054 } 1055 1056 FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse"); 1057 FcPatternAddString = 1058 (FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString"); 1059 FcConfigSubstitute = 1060 (FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute"); 1061 FcDefaultSubstitute = (FcDefaultSubstituteFuncType) 1062 dlsym(libfontconfig, "FcDefaultSubstitute"); 1063 FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch"); 1064 FcPatternGetString = 1065 (FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString"); 1066 FcPatternDestroy = 1067 (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy"); 1068 FcPatternGetCharSet = 1069 (FcPatternGetCharSetFuncType)dlsym(libfontconfig, 1070 "FcPatternGetCharSet"); 1071 FcFontSort = 1072 (FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort"); 1073 FcFontSetDestroy = 1074 (FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy"); 1075 FcCharSetUnion = 1076 (FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion"); 1077 FcCharSetSubtractCount = 1078 (FcCharSetSubtractCountFuncType)dlsym(libfontconfig, 1079 "FcCharSetSubtractCount"); 1080 FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion"); 1081 1082 if (FcNameParse == NULL || 1083 FcPatternAddString == NULL || 1084 FcConfigSubstitute == NULL || 1085 FcDefaultSubstitute == NULL || 1086 FcFontMatch == NULL || 1087 FcPatternGetString == NULL || 1088 FcPatternDestroy == NULL || 1089 FcPatternGetCharSet == NULL || 1090 FcFontSetDestroy == NULL || 1091 FcCharSetUnion == NULL || 1092 FcGetVersion == NULL || 1093 FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/ 1094 closeFontConfig(libfontconfig, JNI_FALSE); 1095 return; 1096 } 1097 1098 (*env)->SetIntField(env, fcInfoObj, fcVersionID, (*FcGetVersion)()); 1099 1100 /* Optionally get the cache dir locations. This isn't 1101 * available until v 2.4.x, but this is OK since on those later versions 1102 * we can check the time stamps on the cache dirs to see if we 1103 * are out of date. There are a couple of assumptions here. First 1104 * that the time stamp on the directory changes when the contents are 1105 * updated. Secondly that the locations don't change. The latter is 1106 * most likely if a new version of fontconfig is installed, but we also 1107 * invalidate the cache if we detect that. Arguably even that is "rare", 1108 * and most likely is tied to an OS upgrade which gets a new file anyway. 1109 */ 1110 FcConfigGetCacheDirs = 1111 (FcConfigGetCacheDirsFuncType)dlsym(libfontconfig, 1112 "FcConfigGetCacheDirs"); 1113 FcStrListNext = 1114 (FcStrListNextFuncType)dlsym(libfontconfig, "FcStrListNext"); 1115 FcStrListDone = 1116 (FcStrListDoneFuncType)dlsym(libfontconfig, "FcStrListDone"); 1117 if (FcStrListNext != NULL && FcStrListDone != NULL && 1118 FcConfigGetCacheDirs != NULL) { 1119 1120 FcStrList* cacheDirs; 1121 FcChar8* cacheDir; 1122 int cnt = 0; 1123 jobject cacheDirArray = 1124 (*env)->GetObjectField(env, fcInfoObj, fcCacheDirsID); 1125 int max = (*env)->GetArrayLength(env, cacheDirArray); 1126 1127 cacheDirs = (*FcConfigGetCacheDirs)(NULL); 1128 if (cacheDirs != NULL) { 1129 while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) { 1130 jstr = (*env)->NewStringUTF(env, (const char*)cacheDir); 1131 JNU_CHECK_EXCEPTION(env); 1132 1133 (*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr); 1134 } 1135 (*FcStrListDone)(cacheDirs); 1136 } 1137 } 1138 1139 locale = (*env)->GetStringUTFChars(env, localeStr, 0); 1140 if (locale == NULL) { 1141 (*env)->ExceptionClear(env); 1142 JNU_ThrowOutOfMemoryError(env, "Could not create locale"); 1143 return; 1144 } 1145 1146 arrlen = (*env)->GetArrayLength(env, fcCompFontArray); 1147 for (i=0; i<arrlen; i++) { 1148 FcFontSet* fontset; 1149 int fn, j, fontCount, nfonts; 1150 unsigned int minGlyphs; 1151 FcChar8 **family, **styleStr, **fullname, **file; 1152 jarray fcFontArr = NULL; 1153 FcCharSet *unionCharset = NULL; 1154 1155 fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i); 1156 fcNameStr = 1157 (jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameID)); 1158 fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0); 1159 if (fcName == NULL) { 1160 continue; 1161 } 1162 pattern = (*FcNameParse)((FcChar8 *)fcName); 1163 (*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName); 1164 if (pattern == NULL) { 1165 closeFontConfig(libfontconfig, JNI_FALSE); 1166 return; 1167 } 1168 1169 /* locale may not usually be necessary as fontconfig appears to apply 1170 * this anyway based on the user's environment. However we want 1171 * to use the value of the JDK startup locale so this should take 1172 * care of it. 1173 */ 1174 if (locale != NULL) { 1175 (*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale); 1176 } 1177 (*FcConfigSubstitute)(NULL, pattern, FcMatchPattern); 1178 (*FcDefaultSubstitute)(pattern); 1179 fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result); 1180 if (fontset == NULL) { 1181 (*FcPatternDestroy)(pattern); 1182 closeFontConfig(libfontconfig, JNI_FALSE); 1183 return; 1184 } 1185 1186 /* fontconfig returned us "nfonts". If we are just getting the 1187 * first font, we set nfont to zero. Otherwise we use "nfonts". 1188 * Next create separate C arrrays of length nfonts for family file etc. 1189 * Inspect the returned fonts and the ones we like (adds enough glyphs) 1190 * are added to the arrays and we increment 'fontCount'. 1191 */ 1192 nfonts = fontset->nfont; 1193 family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1194 styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1195 fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1196 file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1197 if (family == NULL || styleStr == NULL || 1198 fullname == NULL || file == NULL) { 1199 if (family != NULL) { 1200 free(family); 1201 } 1202 if (styleStr != NULL) { 1203 free(styleStr); 1204 } 1205 if (fullname != NULL) { 1206 free(fullname); 1207 } 1208 if (file != NULL) { 1209 free(file); 1210 } 1211 (*FcPatternDestroy)(pattern); 1212 (*FcFontSetDestroy)(fontset); 1213 closeFontConfig(libfontconfig, JNI_FALSE); 1214 return; 1215 } 1216 fontCount = 0; 1217 minGlyphs = 20; 1218 if (debugMinGlyphsStr != NULL) { 1219 int val = minGlyphs; 1220 sscanf(debugMinGlyphsStr, "%5d", &val); 1221 if (val >= 0 && val <= 65536) { 1222 minGlyphs = val; 1223 } 1224 } 1225 1226 for (j=0; j<nfonts; j++) { 1227 FcPattern *fontPattern = fontset->fonts[j]; 1228 FcChar8 *fontformat; 1229 FcCharSet *charset = NULL; 1230 1231 fontformat = NULL; 1232 (*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat); 1233 /* We only want TrueType fonts but some Linuxes still depend 1234 * on Type 1 fonts for some Locale support, so we'll allow 1235 * them there. 1236 */ 1237 if (fontformat != NULL 1238 && (strcmp((char*)fontformat, "TrueType") != 0) 1239 #if defined(__linux__) || defined(_AIX) 1240 && (strcmp((char*)fontformat, "Type 1") != 0) 1241 #endif 1242 ) { 1243 continue; 1244 } 1245 result = (*FcPatternGetCharSet)(fontPattern, 1246 FC_CHARSET, 0, &charset); 1247 if (result != FcResultMatch) { 1248 free(family); 1249 free(fullname); 1250 free(styleStr); 1251 free(file); 1252 (*FcPatternDestroy)(pattern); 1253 (*FcFontSetDestroy)(fontset); 1254 closeFontConfig(libfontconfig, JNI_FALSE); 1255 return; 1256 } 1257 1258 /* We don't want 20 or 30 fonts, so once we hit 10 fonts, 1259 * then require that they really be adding value. Too many 1260 * adversely affects load time for minimal value-add. 1261 * This is still likely far more than we've had in the past. 1262 */ 1263 if (j==10) { 1264 minGlyphs = 50; 1265 } 1266 if (unionCharset == NULL) { 1267 unionCharset = charset; 1268 } else { 1269 if ((*FcCharSetSubtractCount)(charset, unionCharset) 1270 > minGlyphs) { 1271 unionCharset = (* FcCharSetUnion)(unionCharset, charset); 1272 } else { 1273 continue; 1274 } 1275 } 1276 1277 fontCount++; // found a font we will use. 1278 (*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]); 1279 (*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]); 1280 (*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]); 1281 (*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]); 1282 if (!includeFallbacks) { 1283 break; 1284 } 1285 if (fontCount == 254) { 1286 break; // CompositeFont will only use up to 254 slots from here. 1287 } 1288 } 1289 1290 /* Once we get here 'fontCount' is the number of returned fonts 1291 * we actually want to use, so we create 'fcFontArr' of that length. 1292 * The non-null entries of "family[]" etc are those fonts. 1293 * Then loop again over all nfonts adding just those non-null ones 1294 * to 'fcFontArr'. If its null (we didn't want the font) 1295 * then we don't enter the main body. 1296 * So we should never get more than 'fontCount' entries. 1297 */ 1298 if (includeFallbacks) { 1299 fcFontArr = 1300 (*env)->NewObjectArray(env, fontCount, fcFontClass, NULL); 1301 if (IS_NULL(fcFontArr)) { 1302 free(family); 1303 free(fullname); 1304 free(styleStr); 1305 free(file); 1306 (*FcPatternDestroy)(pattern); 1307 (*FcFontSetDestroy)(fontset); 1308 closeFontConfig(libfontconfig, JNI_FALSE); 1309 return; 1310 } 1311 (*env)->SetObjectField(env,fcCompFontObj, fcAllFontsID, fcFontArr); 1312 } 1313 fn=0; 1314 1315 for (j=0;j<nfonts;j++) { 1316 if (family[j] != NULL) { 1317 jobject fcFont = 1318 (*env)->NewObject(env, fcFontClass, fcFontCons); 1319 if (IS_NULL(fcFont)) break; 1320 jstr = (*env)->NewStringUTF(env, (const char*)family[j]); 1321 if (IS_NULL(jstr)) break; 1322 (*env)->SetObjectField(env, fcFont, familyNameID, jstr); 1323 if (file[j] != NULL) { 1324 jstr = (*env)->NewStringUTF(env, (const char*)file[j]); 1325 if (IS_NULL(jstr)) break; 1326 (*env)->SetObjectField(env, fcFont, fontFileID, jstr); 1327 } 1328 if (styleStr[j] != NULL) { 1329 jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]); 1330 if (IS_NULL(jstr)) break; 1331 (*env)->SetObjectField(env, fcFont, styleNameID, jstr); 1332 } 1333 if (fullname[j] != NULL) { 1334 jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]); 1335 if (IS_NULL(jstr)) break; 1336 (*env)->SetObjectField(env, fcFont, fullNameID, jstr); 1337 } 1338 if (fn==0) { 1339 (*env)->SetObjectField(env, fcCompFontObj, 1340 fcFirstFontID, fcFont); 1341 } 1342 if (includeFallbacks) { 1343 (*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont); 1344 } else { 1345 break; 1346 } 1347 } 1348 } 1349 (*FcFontSetDestroy)(fontset); 1350 (*FcPatternDestroy)(pattern); 1351 free(family); 1352 free(styleStr); 1353 free(fullname); 1354 free(file); 1355 } 1356 1357 /* release resources and close the ".so" */ 1358 1359 if (locale) { 1360 (*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale); 1361 } 1362 closeFontConfig(libfontconfig, JNI_TRUE); 1363 }