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