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