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