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.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 if ((libfontconfig = openFontConfig()) == NULL) { 1055 return; 1056 } 1057 1058 FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse"); 1059 FcPatternAddString = 1060 (FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString"); 1061 FcConfigSubstitute = 1062 (FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute"); 1063 FcDefaultSubstitute = (FcDefaultSubstituteFuncType) 1064 dlsym(libfontconfig, "FcDefaultSubstitute"); 1065 FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch"); 1066 FcPatternGetString = 1067 (FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString"); 1068 FcPatternDestroy = 1069 (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy"); 1070 FcPatternGetCharSet = 1071 (FcPatternGetCharSetFuncType)dlsym(libfontconfig, 1072 "FcPatternGetCharSet"); 1073 FcFontSort = 1074 (FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort"); 1075 FcFontSetDestroy = 1076 (FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy"); 1077 FcCharSetUnion = 1078 (FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion"); 1079 FcCharSetSubtractCount = 1080 (FcCharSetSubtractCountFuncType)dlsym(libfontconfig, 1081 "FcCharSetSubtractCount"); 1082 FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion"); 1083 1084 if (FcNameParse == NULL || 1085 FcPatternAddString == NULL || 1086 FcConfigSubstitute == NULL || 1087 FcDefaultSubstitute == NULL || 1088 FcFontMatch == NULL || 1089 FcPatternGetString == NULL || 1090 FcPatternDestroy == NULL || 1091 FcPatternGetCharSet == NULL || 1092 FcFontSetDestroy == NULL || 1093 FcCharSetUnion == NULL || 1094 FcGetVersion == NULL || 1095 FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/ 1096 closeFontConfig(libfontconfig, JNI_FALSE); 1097 return; 1098 } 1099 1100 (*env)->SetIntField(env, fcInfoObj, fcVersionID, (*FcGetVersion)()); 1101 1102 /* Optionally get the cache dir locations. This isn't 1103 * available until v 2.4.x, but this is OK since on those later versions 1104 * we can check the time stamps on the cache dirs to see if we 1105 * are out of date. There are a couple of assumptions here. First 1106 * that the time stamp on the directory changes when the contents are 1107 * updated. Secondly that the locations don't change. The latter is 1108 * most likely if a new version of fontconfig is installed, but we also 1109 * invalidate the cache if we detect that. Arguably even that is "rare", 1110 * and most likely is tied to an OS upgrade which gets a new file anyway. 1111 */ 1112 FcConfigGetCacheDirs = 1113 (FcConfigGetCacheDirsFuncType)dlsym(libfontconfig, 1114 "FcConfigGetCacheDirs"); 1115 FcStrListNext = 1116 (FcStrListNextFuncType)dlsym(libfontconfig, "FcStrListNext"); 1117 FcStrListDone = 1118 (FcStrListDoneFuncType)dlsym(libfontconfig, "FcStrListDone"); 1119 if (FcStrListNext != NULL && FcStrListDone != NULL && 1120 FcConfigGetCacheDirs != NULL) { 1121 1122 FcStrList* cacheDirs; 1123 FcChar8* cacheDir; 1124 int cnt = 0; 1125 jobject cacheDirArray = 1126 (*env)->GetObjectField(env, fcInfoObj, fcCacheDirsID); 1127 int max = (*env)->GetArrayLength(env, cacheDirArray); 1128 1129 cacheDirs = (*FcConfigGetCacheDirs)(NULL); 1130 if (cacheDirs != NULL) { 1131 while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) { 1132 jstr = (*env)->NewStringUTF(env, (const char*)cacheDir); 1133 JNU_CHECK_EXCEPTION(env); 1134 1135 (*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr); 1136 } 1137 (*FcStrListDone)(cacheDirs); 1138 } 1139 } 1140 1141 locale = (*env)->GetStringUTFChars(env, localeStr, 0); 1142 if (locale == NULL) { 1143 (*env)->ExceptionClear(env); 1144 JNU_ThrowOutOfMemoryError(env, "Could not create locale"); 1145 return; 1146 } 1147 1148 arrlen = (*env)->GetArrayLength(env, fcCompFontArray); 1149 for (i=0; i<arrlen; i++) { 1150 FcFontSet* fontset; 1151 int fn, j, fontCount, nfonts; 1152 unsigned int minGlyphs; 1153 FcChar8 **family, **styleStr, **fullname, **file; 1154 jarray fcFontArr; 1155 1156 fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i); 1157 fcNameStr = 1158 (jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameID)); 1159 fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0); 1160 if (fcName == NULL) { 1161 continue; 1162 } 1163 pattern = (*FcNameParse)((FcChar8 *)fcName); 1164 (*env)->ReleaseStringUTFChars(env, fcNameStr, (const char*)fcName); 1165 if (pattern == NULL) { 1166 closeFontConfig(libfontconfig, JNI_FALSE); 1167 return; 1168 } 1169 1170 /* locale may not usually be necessary as fontconfig appears to apply 1171 * this anyway based on the user's environment. However we want 1172 * to use the value of the JDK startup locale so this should take 1173 * care of it. 1174 */ 1175 if (locale != NULL) { 1176 (*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale); 1177 } 1178 (*FcConfigSubstitute)(NULL, pattern, FcMatchPattern); 1179 (*FcDefaultSubstitute)(pattern); 1180 fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result); 1181 if (fontset == NULL) { 1182 (*FcPatternDestroy)(pattern); 1183 closeFontConfig(libfontconfig, JNI_FALSE); 1184 return; 1185 } 1186 1187 /* fontconfig returned us "nfonts". If we are just getting the 1188 * first font, we set nfont to zero. Otherwise we use "nfonts". 1189 * Next create separate C arrrays of length nfonts for family file etc. 1190 * Inspect the returned fonts and the ones we like (adds enough glyphs) 1191 * are added to the arrays and we increment 'fontCount'. 1192 */ 1193 nfonts = fontset->nfont; 1194 family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1195 styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1196 fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1197 file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*)); 1198 if (family == NULL || styleStr == NULL || 1199 fullname == NULL || file == NULL) { 1200 if (family != NULL) { 1201 free(family); 1202 } 1203 if (styleStr != NULL) { 1204 free(styleStr); 1205 } 1206 if (fullname != NULL) { 1207 free(fullname); 1208 } 1209 if (file != NULL) { 1210 free(file); 1211 } 1212 (*FcPatternDestroy)(pattern); 1213 (*FcFontSetDestroy)(fontset); 1214 closeFontConfig(libfontconfig, JNI_FALSE); 1215 return; 1216 } 1217 fontCount = 0; 1218 minGlyphs = 20; 1219 if (debugMinGlyphsStr != NULL) { 1220 int val = minGlyphs; 1221 sscanf(debugMinGlyphsStr, "%5d", &val); 1222 if (val >= 0 && val <= 65536) { 1223 minGlyphs = val; 1224 } 1225 } 1226 FcCharSet *unionCharset = NULL; 1227 for (j=0; j<nfonts; j++) { 1228 FcPattern *fontPattern = fontset->fonts[j]; 1229 FcChar8 *fontformat; 1230 FcCharSet *charset = NULL; 1231 1232 fontformat = NULL; 1233 (*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat); 1234 /* We only want TrueType fonts but some Linuxes still depend 1235 * on Type 1 fonts for some Locale support, so we'll allow 1236 * them there. 1237 */ 1238 if (fontformat != NULL 1239 && (strcmp((char*)fontformat, "TrueType") != 0) 1240 #if defined(__linux__) || defined(_AIX) 1241 && (strcmp((char*)fontformat, "Type 1") != 0) 1242 #endif 1243 ) { 1244 continue; 1245 } 1246 result = (*FcPatternGetCharSet)(fontPattern, 1247 FC_CHARSET, 0, &charset); 1248 if (result != FcResultMatch) { 1249 free(family); 1250 free(fullname); 1251 free(styleStr); 1252 free(file); 1253 (*FcPatternDestroy)(pattern); 1254 (*FcFontSetDestroy)(fontset); 1255 closeFontConfig(libfontconfig, JNI_FALSE); 1256 return; 1257 } 1258 1259 /* We don't want 20 or 30 fonts, so once we hit 10 fonts, 1260 * then require that they really be adding value. Too many 1261 * adversely affects load time for minimal value-add. 1262 * This is still likely far more than we've had in the past. 1263 */ 1264 if (j==10) { 1265 minGlyphs = 50; 1266 } 1267 if (unionCharset == NULL) { 1268 unionCharset = charset; 1269 } else { 1270 if ((*FcCharSetSubtractCount)(charset, unionCharset) 1271 > minGlyphs) { 1272 unionCharset = (* FcCharSetUnion)(unionCharset, charset); 1273 } else { 1274 continue; 1275 } 1276 } 1277 1278 fontCount++; // found a font we will use. 1279 (*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]); 1280 (*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]); 1281 (*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]); 1282 (*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]); 1283 if (!includeFallbacks) { 1284 break; 1285 } 1286 if (fontCount == 254) { 1287 break; // CompositeFont will only use up to 254 slots from here. 1288 } 1289 } 1290 1291 /* Once we get here 'fontCount' is the number of returned fonts 1292 * we actually want to use, so we create 'fcFontArr' of that length. 1293 * The non-null entries of "family[]" etc are those fonts. 1294 * Then loop again over all nfonts adding just those non-null ones 1295 * to 'fcFontArr'. If its null (we didn't want the font) 1296 * then we don't enter the main body. 1297 * So we should never get more than 'fontCount' entries. 1298 */ 1299 if (includeFallbacks) { 1300 fcFontArr = 1301 (*env)->NewObjectArray(env, fontCount, fcFontClass, NULL); 1302 if (IS_NULL(fcFontArr)) { 1303 free(family); 1304 free(fullname); 1305 free(styleStr); 1306 free(file); 1307 (*FcPatternDestroy)(pattern); 1308 (*FcFontSetDestroy)(fontset); 1309 closeFontConfig(libfontconfig, JNI_FALSE); 1310 return; 1311 } 1312 (*env)->SetObjectField(env,fcCompFontObj, fcAllFontsID, fcFontArr); 1313 } 1314 fn=0; 1315 1316 for (j=0;j<nfonts;j++) { 1317 if (family[j] != NULL) { 1318 jobject fcFont = 1319 (*env)->NewObject(env, fcFontClass, fcFontCons); 1320 if (IS_NULL(fcFont)) break; 1321 jstr = (*env)->NewStringUTF(env, (const char*)family[j]); 1322 if (IS_NULL(jstr)) break; 1323 (*env)->SetObjectField(env, fcFont, familyNameID, jstr); 1324 if (file[j] != NULL) { 1325 jstr = (*env)->NewStringUTF(env, (const char*)file[j]); 1326 if (IS_NULL(jstr)) break; 1327 (*env)->SetObjectField(env, fcFont, fontFileID, jstr); 1328 } 1329 if (styleStr[j] != NULL) { 1330 jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]); 1331 if (IS_NULL(jstr)) break; 1332 (*env)->SetObjectField(env, fcFont, styleNameID, jstr); 1333 } 1334 if (fullname[j] != NULL) { 1335 jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]); 1336 if (IS_NULL(jstr)) break; 1337 (*env)->SetObjectField(env, fcFont, fullNameID, jstr); 1338 } 1339 if (fn==0) { 1340 (*env)->SetObjectField(env, fcCompFontObj, 1341 fcFirstFontID, fcFont); 1342 } 1343 if (includeFallbacks) { 1344 (*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont); 1345 } else { 1346 break; 1347 } 1348 } 1349 } 1350 (*FcFontSetDestroy)(fontset); 1351 (*FcPatternDestroy)(pattern); 1352 free(family); 1353 free(styleStr); 1354 free(fullname); 1355 free(file); 1356 } 1357 1358 /* release resources and close the ".so" */ 1359 1360 if (locale) { 1361 (*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale); 1362 } 1363 closeFontConfig(libfontconfig, JNI_TRUE); 1364 }