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