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