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