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