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