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