fontpath.c revision 426
0N/A/*
0N/A * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A#ifdef __linux__
0N/A#include <string.h>
0N/A#endif /* __linux__ */
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <strings.h>
0N/A#include <sys/types.h>
0N/A#include <sys/stat.h>
0N/A#include <sys/mman.h>
0N/A#include <fcntl.h>
0N/A#include <unistd.h>
0N/A#ifdef __solaris__
0N/A#include <sys/systeminfo.h>
0N/A#endif
0N/A
0N/A#include <jni.h>
0N/A#include <jni_util.h>
0N/A#include <sun_font_FontManager.h>
0N/A#ifndef HEADLESS
0N/A#include <X11/Xlib.h>
0N/A#include <awt.h>
0N/A#else
0N/A/* locks ought to be included from awt.h */
0N/A#define AWT_LOCK()
0N/A#define AWT_UNLOCK()
0N/A#endif /* !HEADLESS */
0N/A
0N/A#if defined(__linux__) && !defined(MAP_FAILED)
0N/A#define MAP_FAILED ((caddr_t)-1)
0N/A#endif
0N/A
0N/A#ifndef HEADLESS
0N/Aextern Display *awt_display;
0N/A#endif /* !HEADLESS */
0N/A
0N/A
0N/A#define MAXFDIRS 512 /* Max number of directories that contain fonts */
0N/A
0N/A#ifndef __linux__
0N/A/*
0N/A * This can be set in the makefile to "/usr/X11" if so desired.
0N/A */
0N/A#ifndef OPENWINHOMELIB
0N/A#define OPENWINHOMELIB "/usr/openwin/lib/"
0N/A#endif
0N/A
0N/A/* This is all known Solaris X11 directories on Solaris 8, 9 and 10.
0N/A * It is ordered to give precedence to TrueType directories.
0N/A * It is needed if fontconfig is not installed or configured properly.
0N/A */
0N/Astatic char *fullSolarisFontPath[] = {
0N/A OPENWINHOMELIB "X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/euro_fonts/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_2/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_5/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_7/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_8/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_9/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_13/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/iso_8859_15/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/ar/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/hi_IN.UTF-8/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/ja/X11/fonts/TT",
0N/A OPENWINHOMELIB "locale/ko/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/ko.UTF-8/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/KOI8-R/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/ru.ansi-1251/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/th_TH/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/zh_TW/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/zh_TW.BIG5/X11/fonts/TT",
0N/A OPENWINHOMELIB "locale/zh_HK.BIG5HK/X11/fonts/TT",
0N/A OPENWINHOMELIB "locale/zh_CN.GB18030/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/zh/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "locale/zh.GBK/X11/fonts/TrueType",
0N/A OPENWINHOMELIB "X11/fonts/Type1",
0N/A OPENWINHOMELIB "X11/fonts/Type1/sun",
0N/A OPENWINHOMELIB "X11/fonts/Type1/sun/outline",
0N/A OPENWINHOMELIB "locale/iso_8859_2/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_4/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_5/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_7/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_8/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_9/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/iso_8859_13/X11/fonts/Type1",
0N/A OPENWINHOMELIB "locale/ar/X11/fonts/Type1",
0N/A NULL, /* terminates the list */
0N/A};
0N/A
0N/A#else /* __linux */
0N/A/* All the known interesting locations we have discovered on
0N/A * various flavors of Linux
0N/A */
0N/Astatic char *fullLinuxFontPath[] = {
0N/A "/usr/X11R6/lib/X11/fonts/TrueType", /* RH 7.1+ */
0N/A "/usr/X11R6/lib/X11/fonts/truetype", /* SuSE */
0N/A "/usr/X11R6/lib/X11/fonts/tt",
0N/A "/usr/X11R6/lib/X11/fonts/TTF",
0N/A "/usr/X11R6/lib/X11/fonts/OTF", /* RH 9.0 (but empty!) */
0N/A "/usr/share/fonts/ja/TrueType", /* RH 7.2+ */
0N/A "/usr/share/fonts/truetype",
0N/A "/usr/share/fonts/ko/TrueType", /* RH 9.0 */
0N/A "/usr/share/fonts/zh_CN/TrueType", /* RH 9.0 */
0N/A "/usr/share/fonts/zh_TW/TrueType", /* RH 9.0 */
0N/A "/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType", /* Debian */
0N/A "/usr/X11R6/lib/X11/fonts/Type1",
0N/A "/usr/share/fonts/default/Type1", /* RH 9.0 */
0N/A NULL, /* terminates the list */
0N/A};
0N/A#endif
0N/A
0N/Astatic char **getFontConfigLocations();
0N/A
0N/Atypedef struct {
0N/A const char *name[MAXFDIRS];
0N/A int num;
0N/A} fDirRecord, *fDirRecordPtr;
0N/A
0N/A#ifndef HEADLESS
0N/A
0N/A/*
0N/A * Returns True if display is local, False of it's remote.
0N/A */
0N/Ajboolean isDisplayLocal(JNIEnv *env) {
0N/A static jboolean isLocal = False;
0N/A static jboolean isLocalSet = False;
0N/A jboolean ret;
0N/A
0N/A if (isLocalSet) {
0N/A return isLocal;
0N/A }
0N/A
0N/A isLocal = JNU_CallStaticMethodByName(env, NULL,
0N/A "sun/awt/X11GraphicsEnvironment",
0N/A "isDisplayLocal",
0N/A "()Z").z;
0N/A isLocalSet = True;
0N/A return isLocal;
0N/A}
0N/A
0N/Astatic void AddFontsToX11FontPath ( fDirRecord *fDirP )
0N/A{
0N/A char *onePath;
0N/A int index, nPaths;
0N/A int origNumPaths, length;
0N/A int origIndex;
0N/A int totalDirCount;
0N/A char **origFontPath;
0N/A char **tempFontPath;
0N/A int doNotAppend;
0N/A int *appendDirList;
0N/A char **newFontPath;
0N/A int err, compareLength;
0N/A char fontDirPath[512];
0N/A int dirFile;
0N/A
0N/A doNotAppend = 0;
0N/A
0N/A if ( fDirP->num == 0 ) return;
0N/A
0N/A appendDirList = malloc ( fDirP->num * sizeof ( int ));
0N/A if ( appendDirList == NULL ) {
0N/A return; /* if it fails we cannot do much */
0N/A }
0N/A
0N/A origFontPath = XGetFontPath ( awt_display, &nPaths );
0N/A
0N/A totalDirCount = nPaths;
0N/A origNumPaths = nPaths;
0N/A tempFontPath = origFontPath;
0N/A
0N/A
0N/A for (index = 0; index < fDirP->num; index++ ) {
0N/A
0N/A doNotAppend = 0;
0N/A
0N/A tempFontPath = origFontPath;
0N/A for ( origIndex = 0; origIndex < nPaths; origIndex++ ) {
0N/A
0N/A onePath = *tempFontPath;
0N/A
0N/A compareLength = strlen ( onePath );
0N/A if ( onePath[compareLength -1] == '/' )
0N/A compareLength--;
0N/A
0N/A /* there is a slash at the end of every solaris X11 font path name */
0N/A if ( strncmp ( onePath, fDirP->name[index], compareLength ) == 0 ) {
0N/A doNotAppend = 1;
0N/A break;
0N/A }
0N/A tempFontPath++;
0N/A }
0N/A
0N/A appendDirList[index] = 0;
0N/A if ( doNotAppend == 0 ) {
0N/A strcpy ( fontDirPath, fDirP->name[index] );
0N/A strcat ( fontDirPath, "/fonts.dir" );
0N/A dirFile = open ( fontDirPath, O_RDONLY, 0 );
0N/A if ( dirFile == -1 ) {
0N/A doNotAppend = 1;
0N/A } else {
0N/A close ( dirFile );
0N/A totalDirCount++;
0N/A appendDirList[index] = 1;
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A /* if no changes are required do not bother to do a setfontpath */
0N/A if ( totalDirCount == nPaths ) {
0N/A free ( ( void *) appendDirList );
0N/A XFreeFontPath ( origFontPath );
0N/A return;
0N/A }
0N/A
0N/A
0N/A newFontPath = malloc ( totalDirCount * sizeof ( char **) );
0N/A /* if it fails free things and get out */
0N/A if ( newFontPath == NULL ) {
0N/A free ( ( void *) appendDirList );
0N/A XFreeFontPath ( origFontPath );
0N/A return;
0N/A }
0N/A
0N/A for ( origIndex = 0; origIndex < nPaths; origIndex++ ) {
0N/A onePath = origFontPath[origIndex];
0N/A newFontPath[origIndex] = onePath;
0N/A }
0N/A
0N/A /* now add the other font paths */
0N/A
0N/A for (index = 0; index < fDirP->num; index++ ) {
0N/A
0N/A if ( appendDirList[index] == 1 ) {
0N/A
0N/A /* printf ( "Appending %s\n", fDirP->name[index] ); */
0N/A
0N/A onePath = malloc ( ( strlen (fDirP->name[index]) + 2 )* sizeof( char ) );
0N/A strcpy ( onePath, fDirP->name[index] );
0N/A strcat ( onePath, "/" );
0N/A newFontPath[nPaths++] = onePath;
0N/A /* printf ( "The path to be appended is %s\n", onePath ); */
0N/A }
0N/A }
0N/A
0N/A /* printf ( "The dir count = %d\n", totalDirCount ); */
0N/A free ( ( void *) appendDirList );
0N/A
0N/A XSetFontPath ( awt_display, newFontPath, totalDirCount );
0N/A
0N/A for ( index = origNumPaths; index < totalDirCount; index++ ) {
0N/A free( newFontPath[index] );
0N/A }
0N/A
0N/A free ( (void *) newFontPath );
0N/A XFreeFontPath ( origFontPath );
0N/A return;
0N/A}
0N/A#endif /* !HEADLESS */
0N/A
0N/A
0N/A#ifndef HEADLESS
0N/Astatic char **getX11FontPath ()
0N/A{
0N/A char **x11Path, **fontdirs;
0N/A int i, pos, slen, nPaths, numDirs;
0N/A
0N/A x11Path = XGetFontPath (awt_display, &nPaths);
0N/A
0N/A /* This isn't ever going to be perfect: the font path may contain
0N/A * much we aren't interested in, but the cost should be moderate
0N/A * Exclude all directories that contain the strings "Speedo","/F3/",
0N/A * "75dpi", "100dpi", "misc" or "bitmap", or don't begin with a "/",
0N/A * the last of which should exclude font servers.
0N/A * Also exclude the user specific ".gnome*" directories which
0N/A * aren't going to contain the system fonts we need.
0N/A * Hopefully we are left only with Type1 and TrueType directories.
0N/A * It doesn't matter much if there are extraneous directories, it'll just
0N/A * cost us a little wasted effort upstream.
0N/A */
0N/A fontdirs = (char**)calloc(nPaths+1, sizeof(char*));
0N/A pos = 0;
0N/A for (i=0; i < nPaths; i++) {
0N/A if (x11Path[i][0] != '/') {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], "/75dpi") != NULL) {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], "/100dpi") != NULL) {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], "/misc") != NULL) {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], "/Speedo") != NULL) {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], ".gnome") != NULL) {
0N/A continue;
0N/A }
0N/A#ifdef __solaris__
0N/A if (strstr(x11Path[i], "/F3/") != NULL) {
0N/A continue;
0N/A }
0N/A if (strstr(x11Path[i], "bitmap") != NULL) {
0N/A continue;
0N/A }
0N/A#endif
0N/A fontdirs[pos] = strdup(x11Path[i]);
0N/A slen = strlen(fontdirs[pos]);
0N/A if (slen > 0 && fontdirs[pos][slen-1] == '/') {
0N/A fontdirs[pos][slen-1] = '\0'; /* null out trailing "/" */
0N/A }
0N/A pos++;
0N/A }
0N/A
0N/A XFreeFontPath(x11Path);
0N/A if (pos == 0) {
0N/A free(fontdirs);
0N/A fontdirs = NULL;
0N/A }
0N/A return fontdirs;
0N/A}
0N/A
0N/A
0N/A#endif /* !HEADLESS */
0N/A
0N/A#ifdef __linux__
0N/A/* from awt_LoadLibrary.c */
0N/AJNIEXPORT jboolean JNICALL AWTIsHeadless();
0N/A#endif
0N/A
0N/A/* This eliminates duplicates, at a non-linear but acceptable cost
0N/A * since the lists are expected to be reasonably short, and then
0N/A * deletes references to non-existent directories, and returns
0N/A * a single path consisting of unique font directories.
0N/A */
0N/Astatic char* mergePaths(char **p1, char **p2, char **p3, jboolean noType1) {
0N/A
0N/A int len1=0, len2=0, len3=0, totalLen=0, numDirs=0,
0N/A currLen, i, j, found, pathLen=0;
0N/A char **ptr, **fontdirs;
0N/A char *fontPath = NULL;
0N/A
0N/A if (p1 != NULL) {
0N/A ptr = p1;
0N/A while (*ptr++ != NULL) len1++;
0N/A }
0N/A if (p2 != NULL) {
0N/A ptr = p2;
0N/A
0N/A while (*ptr++ != NULL) len2++;
0N/A }
0N/A if (p3 != NULL) {
0N/A ptr = p3;
0N/A while (*ptr++ != NULL) len3++;
0N/A }
0N/A totalLen = len1+len2+len3;
0N/A fontdirs = (char**)calloc(totalLen, sizeof(char*));
0N/A
0N/A for (i=0; i < len1; i++) {
0N/A if (noType1 && strstr(p1[i], "Type1") != NULL) {
0N/A continue;
0N/A }
0N/A fontdirs[numDirs++] = p1[i];
0N/A }
0N/A
0N/A currLen = numDirs; /* only compare against previous path dirs */
0N/A for (i=0; i < len2; i++) {
0N/A if (noType1 && strstr(p2[i], "Type1") != NULL) {
0N/A continue;
0N/A }
0N/A found = 0;
0N/A for (j=0; j < currLen; j++) {
0N/A if (strcmp(fontdirs[j], p2[i]) == 0) {
0N/A found = 1;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A fontdirs[numDirs++] = p2[i];
0N/A }
0N/A }
0N/A
0N/A currLen = numDirs; /* only compare against previous path dirs */
0N/A for (i=0; i < len3; i++) {
0N/A if (noType1 && strstr(p3[i], "Type1") != NULL) {
0N/A continue;
0N/A }
0N/A found = 0;
0N/A for (j=0; j < currLen; j++) {
0N/A if (strcmp(fontdirs[j], p3[i]) == 0) {
0N/A found = 1;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A fontdirs[numDirs++] = p3[i];
0N/A }
0N/A }
0N/A
0N/A /* Now fontdirs contains unique dirs and numDirs records how many.
0N/A * What we don't know is if they all exist. On reflection I think
0N/A * this isn't an issue, so for now I will return all these locations,
0N/A * converted to one string */
0N/A for (i=0; i<numDirs; i++) {
0N/A pathLen += (strlen(fontdirs[i]) + 1);
0N/A }
0N/A if (pathLen > 0 && (fontPath = malloc(pathLen))) {
0N/A *fontPath = '\0';
0N/A for (i = 0; i<numDirs; i++) {
0N/A if (i != 0) {
0N/A strcat(fontPath, ":");
0N/A }
0N/A strcat(fontPath, fontdirs[i]);
0N/A }
0N/A }
0N/A free (fontdirs);
0N/A
0N/A return fontPath;
0N/A}
0N/A
0N/A/*
0N/A * The goal of this function is to find all "system" fonts which
0N/A * are needed by the JRE to display text in supported locales etc, and
0N/A * to support APIs which allow users to enumerate all system fonts and use
0N/A * them from their Java applications.
0N/A * The preferred mechanism is now using the new "fontconfig" library
0N/A * This exists on newer versions of Linux and Solaris (S10 and above)
0N/A * The library is dynamically located. The results are merged with
0N/A * a set of "known" locations and with the X11 font path, if running in
0N/A * a local X11 environment.
0N/A * The hardwired paths are built into the JDK binary so as new font locations
0N/A * are created on a host plaform for them to be located by the JRE they will
0N/A * need to be added ito the host's font configuration database, typically
0N/A * /etc/fonts/local.conf, and to ensure that directory contains a fonts.dir
0N/A * NB: Fontconfig also depends heavily for performance on the host O/S
0N/A * maintaining up to date caches.
0N/A * This is consistent with the requirements of the desktop environments
0N/A * on these OSes.
0N/A * This also frees us from X11 APIs as JRE is required to function in
0N/A * a "headless" mode where there is no Xserver.
0N/A */
0N/Astatic char *getPlatformFontPathChars(JNIEnv *env, jboolean noType1) {
0N/A
0N/A char **fcdirs = NULL, **x11dirs = NULL, **knowndirs = NULL, *path = NULL;
0N/A
0N/A /* As of 1.5 we try to use fontconfig on both Solaris and Linux.
0N/A * If its not available NULL is returned.
0N/A */
0N/A fcdirs = getFontConfigLocations();
0N/A
0N/A#ifdef __linux__
0N/A knowndirs = fullLinuxFontPath;
0N/A#else /* IF SOLARIS */
0N/A knowndirs = fullSolarisFontPath;
0N/A#endif
0N/A
0N/A /* REMIND: this code requires to be executed when the GraphicsEnvironment
0N/A * is already initialised. That is always true, but if it were not so,
0N/A * this code could throw an exception and the fontpath would fail to
0N/A * be initialised.
0N/A */
0N/A#ifndef HEADLESS
0N/A#ifdef __linux__ /* There's no headless build on linux ... */
0N/A if (!AWTIsHeadless()) { /* .. so need to call a function to check */
0N/A#endif
0N/A AWT_LOCK();
0N/A if (isDisplayLocal(env)) {
0N/A x11dirs = getX11FontPath();
0N/A }
0N/A AWT_UNLOCK();
0N/A#ifdef __linux__
0N/A }
0N/A#endif
0N/A#endif /* !HEADLESS */
0N/A path = mergePaths(fcdirs, x11dirs, knowndirs, noType1);
0N/A if (fcdirs != NULL) {
0N/A char **p = fcdirs;
0N/A while (*p != NULL) free(*p++);
0N/A free(fcdirs);
0N/A }
0N/A
0N/A if (x11dirs != NULL) {
0N/A char **p = x11dirs;
0N/A while (*p != NULL) free(*p++);
0N/A free(x11dirs);
0N/A }
0N/A
0N/A return path;
0N/A}
0N/A
0N/AJNIEXPORT jstring JNICALL Java_sun_font_FontManager_getFontPath
0N/A(JNIEnv *env, jclass obj, jboolean noType1) {
0N/A jstring ret;
0N/A static char *ptr = NULL; /* retain result across calls */
0N/A
0N/A if (ptr == NULL) {
0N/A ptr = getPlatformFontPathChars(env, noType1);
0N/A }
0N/A ret = (*env)->NewStringUTF(env, ptr);
0N/A return ret;
0N/A}
0N/A
0N/A/*
0N/A * In general setting the font path in a remote display situation is
0N/A * problematic. But for Solaris->Solaris the paths needed by the JRE should
0N/A * also be available to the server, although we have no way to check this
0N/A * for sure.
0N/A * So set the font path if we think its safe to do so:
0N/A * All Solaris X servers at least back to 2.6 and up to Solaris 10
0N/A * define the exact same vendor string.
0N/A * The version number for Solaris 2.6 is 3600, for 2.7 is 3610 and
0N/A * for Solaris 8 6410
0N/A * we want to set the font path only for 2.8 and onwards. Earlier releases
0N/A * are unlikely to have the right fonts and can't install "all locales"
0N/A * as needed to be sure. Also Solaris 8 is the earliest release supported
0N/A * by 1.5.
0N/A */
0N/A#ifndef HEADLESS
0N/Astatic int isSunXServer() {
0N/A#ifdef __solaris__
0N/A return (strcmp("Sun Microsystems, Inc.", ServerVendor(awt_display)) == 0 &&
0N/A VendorRelease(awt_display) >= 6410);
0N/A#else
0N/A return 0;
0N/A#endif /* __solaris__ */
0N/A}
0N/A
0N/A/* Avoid re-doing work for every call to setNativeFontPath */
0N/Astatic int doSetFontPath = -1;
0N/Astatic int shouldSetXFontPath(JNIEnv *env) {
0N/A if (doSetFontPath == -1) {
0N/A doSetFontPath =
0N/A awt_display != NULL && (isDisplayLocal(env) || isSunXServer());
0N/A }
0N/A return doSetFontPath;
0N/A}
0N/A#endif /* !HEADLESS */
0N/A
0N/AJNIEXPORT void JNICALL Java_sun_font_FontManager_setNativeFontPath
0N/A(JNIEnv *env, jclass obj, jstring theString) {
0N/A#ifdef HEADLESS
0N/A return;
0N/A#else
0N/A fDirRecord fDir;
0N/A const char *theChars;
0N/A
0N/A if (awt_display == NULL) {
0N/A return;
0N/A }
0N/A AWT_LOCK();
0N/A if (shouldSetXFontPath(env)) {
0N/A theChars = (*env)->GetStringUTFChars (env, theString, 0);
0N/A fDir.num = 1;
0N/A fDir.name[0] = theChars;
0N/A /* printf ("Registering the font path here %s \n", theChars ); */
0N/A AddFontsToX11FontPath ( &fDir );
0N/A if (theChars) {
0N/A (*env)->ReleaseStringUTFChars (env,
0N/A theString, (const char*)theChars);
0N/A }
0N/A }
0N/A AWT_UNLOCK();
0N/A
0N/A#endif
0N/A}
0N/A
0N/A/* This isn't yet used on unix, the implementation is added since shared
0N/A * code calls this method in preparation for future use.
0N/A */
0N/A/* Obtain all the fontname -> filename mappings.
0N/A * This is called once and the results returned to Java code which can
0N/A * use it for lookups to reduce or avoid the need to search font files.
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_font_FontManager_populateFontFileNameMap
0N/A(JNIEnv *env, jclass obj, jobject fontToFileMap,
0N/A jobject fontToFamilyMap, jobject familyToFontListMap, jobject locale)
0N/A{
0N/A return;
0N/A}
0N/A
0N/A#include <dlfcn.h>
0N/A#ifndef __linux__ /* i.e. is solaris */
0N/A#include <link.h>
0N/A#endif
0N/A
0N/A#include "fontconfig.h"
0N/A
0N/A
0N/Astatic void* openFontConfig() {
0N/A
0N/A char *homeEnv;
0N/A static char *homeEnvStr = "HOME="; /* must be static */
0N/A void* libfontconfig = NULL;
0N/A#ifdef __solaris__
0N/A#define SYSINFOBUFSZ 8
0N/A char sysinfobuf[SYSINFOBUFSZ];
0N/A#endif
0N/A
0N/A /* Private workaround to not use fontconfig library.
0N/A * May be useful during testing/debugging
0N/A */
0N/A char *useFC = getenv("USE_J2D_FONTCONFIG");
0N/A if (useFC != NULL && !strcmp(useFC, "no")) {
0N/A return NULL;
0N/A }
0N/A
0N/A#ifdef __solaris__
0N/A /* fontconfig is likely not properly configured on S8/S9 - skip it,
0N/A * although allow user to override this behaviour with an env. variable
0N/A * ie if USE_J2D_FONTCONFIG=yes then we skip this test.
0N/A * NB "4" is the length of a string which matches our patterns.
0N/A */
0N/A if (useFC == NULL || strcmp(useFC, "yes")) {
0N/A if (sysinfo(SI_RELEASE, sysinfobuf, SYSINFOBUFSZ) == 4) {
0N/A if ((!strcmp(sysinfobuf, "5.8") || !strcmp(sysinfobuf, "5.9"))) {
0N/A return NULL;
0N/A }
0N/A }
0N/A }
0N/A#endif
0N/A /* 64 bit sparc should pick up the right version from the lib path.
0N/A * New features may be added to libfontconfig, this is expected to
0N/A * be compatible with old features, but we may need to start
0N/A * distinguishing the library version, to know whether to expect
0N/A * certain symbols - and functionality - to be available.
0N/A * Also add explicit search for .so.1 in case .so symlink doesn't exist.
0N/A */
0N/A libfontconfig = dlopen("libfontconfig.so.1", RTLD_LOCAL|RTLD_LAZY);
0N/A if (libfontconfig == NULL) {
0N/A libfontconfig = dlopen("libfontconfig.so", RTLD_LOCAL|RTLD_LAZY);
0N/A if (libfontconfig == NULL) {
0N/A return NULL;
0N/A }
0N/A }
0N/A
0N/A /* Version 1.0 of libfontconfig crashes if HOME isn't defined in
0N/A * the environment. This should generally never happen, but we can't
0N/A * control it, and can't control the version of fontconfig, so iff
0N/A * its not defined we set it to an empty value which is sufficient
0N/A * to prevent a crash. I considered unsetting it before exit, but
0N/A * it doesn't appear to work on Solaris, so I will leave it set.
0N/A */
0N/A homeEnv = getenv("HOME");
0N/A if (homeEnv == NULL) {
0N/A putenv(homeEnvStr);
0N/A }
0N/A
0N/A return libfontconfig;
0N/A}
0N/A
0N/Atypedef void* (FcFiniFuncType)();
0N/A
0N/Astatic void closeFontConfig(void* libfontconfig, jboolean fcFini) {
0N/A
0N/A /* NB FcFini is not in (eg) the Solaris 10 version of fontconfig. Its not
0N/A * clear if this means we are really leaking resources in those cases
0N/A * but it seems we should call this function when its available.
0N/A * But since the Swing GTK code may be still accessing the lib, its probably
0N/A * safest for now to just let this "leak" rather than potentially
0N/A * concurrently free global data still in use by other code.
0N/A */
0N/A#if 0
0N/A if (fcFini) { /* release resources */
0N/A FcFiniFuncType FcFini = (FcFiniFuncType)dlsym(libfontconfig, "FcFini");
0N/A
0N/A if (FcFini != NULL) {
0N/A (*FcFini)();
0N/A }
0N/A }
0N/A#endif
0N/A dlclose(libfontconfig);
0N/A}
0N/A
0N/Atypedef FcConfig* (*FcInitLoadConfigFuncType)();
0N/Atypedef FcPattern* (*FcPatternBuildFuncType)(FcPattern *orig, ...);
0N/Atypedef FcObjectSet* (*FcObjectSetFuncType)(const char *first, ...);
0N/Atypedef FcFontSet* (*FcFontListFuncType)(FcConfig *config,
0N/A FcPattern *p,
0N/A FcObjectSet *os);
0N/Atypedef FcResult (*FcPatternGetBoolFuncType)(const FcPattern *p,
0N/A const char *object,
0N/A int n,
0N/A FcBool *b);
0N/Atypedef FcResult (*FcPatternGetIntegerFuncType)(const FcPattern *p,
0N/A const char *object,
0N/A int n,
0N/A int *i);
0N/Atypedef FcResult (*FcPatternGetStringFuncType)(const FcPattern *p,
0N/A const char *object,
0N/A int n,
0N/A FcChar8 ** s);
0N/Atypedef FcChar8* (*FcStrDirnameFuncType)(const FcChar8 *file);
0N/Atypedef void (*FcPatternDestroyFuncType)(FcPattern *p);
0N/Atypedef void (*FcFontSetDestroyFuncType)(FcFontSet *s);
0N/Atypedef FcPattern* (*FcNameParseFuncType)(const FcChar8 *name);
0N/Atypedef FcBool (*FcPatternAddStringFuncType)(FcPattern *p,
0N/A const char *object,
0N/A const FcChar8 *s);
0N/Atypedef void (*FcDefaultSubstituteFuncType)(FcPattern *p);
0N/Atypedef FcBool (*FcConfigSubstituteFuncType)(FcConfig *config,
0N/A FcPattern *p,
0N/A FcMatchKind kind);
0N/Atypedef FcPattern* (*FcFontMatchFuncType)(FcConfig *config,
0N/A FcPattern *p,
0N/A FcResult *result);
0N/Atypedef FcFontSet* (*FcFontSetCreateFuncType)();
0N/Atypedef FcBool (*FcFontSetAddFuncType)(FcFontSet *s, FcPattern *font);
0N/A
426N/Atypedef FcResult (*FcPatternGetCharSetFuncType)(FcPattern *p,
426N/A const char *object,
426N/A int n,
426N/A FcCharSet **c);
426N/Atypedef FcFontSet* (*FcFontSortFuncType)(FcConfig *config,
426N/A FcPattern *p,
426N/A FcBool trim,
426N/A FcCharSet **csp,
426N/A FcResult *result);
426N/Atypedef FcCharSet* (*FcCharSetUnionFuncType)(const FcCharSet *a,
426N/A const FcCharSet *b);
426N/Atypedef FcChar32 (*FcCharSetSubtractCountFuncType)(const FcCharSet *a,
426N/A const FcCharSet *b);
426N/A
426N/Atypedef int (*FcGetVersionFuncType)();
426N/A
426N/Atypedef FcStrList* (*FcConfigGetCacheDirsFuncType)(FcConfig *config);
426N/Atypedef FcChar8* (*FcStrListNextFuncType)(FcStrList *list);
426N/Atypedef FcChar8* (*FcStrListDoneFuncType)(FcStrList *list);
0N/A
0N/Astatic char **getFontConfigLocations() {
0N/A
0N/A char **fontdirs;
0N/A int numdirs = 0;
0N/A FcInitLoadConfigFuncType FcInitLoadConfig;
0N/A FcPatternBuildFuncType FcPatternBuild;
0N/A FcObjectSetFuncType FcObjectSetBuild;
0N/A FcFontListFuncType FcFontList;
0N/A FcPatternGetStringFuncType FcPatternGetString;
0N/A FcStrDirnameFuncType FcStrDirname;
0N/A FcPatternDestroyFuncType FcPatternDestroy;
0N/A FcFontSetDestroyFuncType FcFontSetDestroy;
0N/A
0N/A FcConfig *fontconfig;
0N/A FcPattern *pattern;
0N/A FcObjectSet *objset;
0N/A FcFontSet *fontSet;
0N/A FcStrList *strList;
0N/A FcChar8 *str;
0N/A int i, f, found, len=0;
0N/A char **fontPath;
0N/A
0N/A void* libfontconfig = openFontConfig();
0N/A
0N/A if (libfontconfig == NULL) {
0N/A return NULL;
0N/A }
0N/A
0N/A FcPatternBuild =
0N/A (FcPatternBuildFuncType)dlsym(libfontconfig, "FcPatternBuild");
0N/A FcObjectSetBuild =
0N/A (FcObjectSetFuncType)dlsym(libfontconfig, "FcObjectSetBuild");
0N/A FcFontList =
0N/A (FcFontListFuncType)dlsym(libfontconfig, "FcFontList");
0N/A FcPatternGetString =
0N/A (FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
0N/A FcStrDirname =
0N/A (FcStrDirnameFuncType)dlsym(libfontconfig, "FcStrDirname");
0N/A FcPatternDestroy =
0N/A (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
0N/A FcFontSetDestroy =
0N/A (FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
0N/A
0N/A if (FcPatternBuild == NULL ||
0N/A FcObjectSetBuild == NULL ||
0N/A FcPatternGetString == NULL ||
0N/A FcFontList == NULL ||
0N/A FcStrDirname == NULL ||
0N/A FcPatternDestroy == NULL ||
0N/A FcFontSetDestroy == NULL) { /* problem with the library: return. */
0N/A closeFontConfig(libfontconfig, JNI_FALSE);
0N/A return NULL;
0N/A }
0N/A
0N/A /* Make calls into the fontconfig library to build a search for
0N/A * outline fonts, and to get the set of full file paths from the matches.
0N/A * This set is returned from the call to FcFontList(..)
0N/A * We allocate an array of char* pointers sufficient to hold all
0N/A * the matches + 1 extra which ensures there will be a NULL after all
0N/A * valid entries.
0N/A * We call FcStrDirname strip the file name from the path, and
0N/A * check if we have yet seen this directory. If not we add a pointer to
0N/A * it into our array of char*. Note that FcStrDirname returns newly
0N/A * allocated storage so we can use this in the return char** value.
0N/A * Finally we clean up, freeing allocated resources, and return the
0N/A * array of unique directories.
0N/A */
0N/A pattern = (*FcPatternBuild)(NULL, FC_OUTLINE, FcTypeBool, FcTrue, NULL);
0N/A objset = (*FcObjectSetBuild)(FC_FILE, NULL);
0N/A fontSet = (*FcFontList)(NULL, pattern, objset);
0N/A fontdirs = (char**)calloc(fontSet->nfont+1, sizeof(char*));
0N/A for (f=0; f < fontSet->nfont; f++) {
0N/A FcChar8 *file;
0N/A FcChar8 *dir;
0N/A if ((*FcPatternGetString)(fontSet->fonts[f], FC_FILE, 0, &file) ==
0N/A FcResultMatch) {
0N/A dir = (*FcStrDirname)(file);
0N/A found = 0;
0N/A for (i=0;i<numdirs; i++) {
0N/A if (strcmp(fontdirs[i], (char*)dir) == 0) {
0N/A found = 1;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A fontdirs[numdirs++] = (char*)dir;
0N/A } else {
0N/A free((char*)dir);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* Free memory and close the ".so" */
0N/A (*FcFontSetDestroy)(fontSet);
0N/A (*FcPatternDestroy)(pattern);
0N/A closeFontConfig(libfontconfig, JNI_TRUE);
0N/A return fontdirs;
0N/A}
0N/A
0N/A/* These are copied from sun.awt.SunHints.
0N/A * Consider initialising them as ints using JNI for more robustness.
0N/A */
0N/A#define TEXT_AA_OFF 1
0N/A#define TEXT_AA_ON 2
0N/A#define TEXT_AA_LCD_HRGB 4
0N/A#define TEXT_AA_LCD_HBGR 5
0N/A#define TEXT_AA_LCD_VRGB 6
0N/A#define TEXT_AA_LCD_VBGR 7
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_font_FontManager_getFontConfigAASettings
0N/A(JNIEnv *env, jclass obj, jstring localeStr, jstring fcNameStr) {
0N/A
0N/A FcNameParseFuncType FcNameParse;
0N/A FcPatternAddStringFuncType FcPatternAddString;
0N/A FcConfigSubstituteFuncType FcConfigSubstitute;
0N/A FcDefaultSubstituteFuncType FcDefaultSubstitute;
0N/A FcFontMatchFuncType FcFontMatch;
0N/A FcPatternGetBoolFuncType FcPatternGetBool;
0N/A FcPatternGetIntegerFuncType FcPatternGetInteger;
0N/A FcPatternDestroyFuncType FcPatternDestroy;
0N/A
0N/A FcPattern *pattern, *matchPattern;
0N/A FcResult result;
0N/A FcBool antialias = FcFalse;
0N/A int rgba = 0;
0N/A const char *locale=NULL, *fcName=NULL;
0N/A void* libfontconfig;
0N/A
0N/A if (fcNameStr == NULL || localeStr == NULL) {
0N/A return -1;
0N/A }
0N/A
0N/A fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
0N/A if (fcName == NULL) {
0N/A return -1;
0N/A }
0N/A locale = (*env)->GetStringUTFChars(env, localeStr, 0);
0N/A
0N/A if ((libfontconfig = openFontConfig()) == NULL) {
0N/A (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
0N/A if (locale) {
0N/A (*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale);
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
0N/A FcPatternAddString =
0N/A (FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
0N/A FcConfigSubstitute =
0N/A (FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
0N/A FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
0N/A dlsym(libfontconfig, "FcDefaultSubstitute");
0N/A FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
0N/A FcPatternGetBool = (FcPatternGetBoolFuncType)
0N/A dlsym(libfontconfig, "FcPatternGetBool");
0N/A FcPatternGetInteger = (FcPatternGetIntegerFuncType)
0N/A dlsym(libfontconfig, "FcPatternGetInteger");
0N/A FcPatternDestroy =
0N/A (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
0N/A
0N/A if (FcNameParse == NULL ||
0N/A FcPatternAddString == NULL ||
0N/A FcConfigSubstitute == NULL ||
0N/A FcDefaultSubstitute == NULL ||
0N/A FcFontMatch == NULL ||
0N/A FcPatternGetBool == NULL ||
0N/A FcPatternGetInteger == NULL ||
0N/A FcPatternDestroy == NULL) { /* problem with the library: return. */
0N/A
0N/A (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
0N/A if (locale) {
0N/A (*env)->ReleaseStringUTFChars (env, localeStr,(const char*)locale);
0N/A }
0N/A closeFontConfig(libfontconfig, JNI_FALSE);
0N/A return -1;
0N/A }
0N/A
0N/A
0N/A pattern = (*FcNameParse)((FcChar8 *)fcName);
0N/A if (locale != NULL) {
0N/A (*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
0N/A }
0N/A (*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
0N/A (*FcDefaultSubstitute)(pattern);
0N/A matchPattern = (*FcFontMatch)(NULL, pattern, &result);
0N/A /* Perhaps should call FcFontRenderPrepare() here as some pattern
0N/A * elements might change as a result of that call, but I'm not seeing
0N/A * any difference in testing.
0N/A */
0N/A if (matchPattern) {
0N/A (*FcPatternGetBool)(matchPattern, FC_ANTIALIAS, 0, &antialias);
0N/A (*FcPatternGetInteger)(matchPattern, FC_RGBA, 0, &rgba);
0N/A (*FcPatternDestroy)(matchPattern);
0N/A }
0N/A (*FcPatternDestroy)(pattern);
0N/A
0N/A (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
0N/A if (locale) {
0N/A (*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale);
0N/A }
0N/A closeFontConfig(libfontconfig, JNI_TRUE);
0N/A
0N/A if (antialias == FcFalse) {
0N/A return TEXT_AA_OFF;
0N/A } else if (rgba <= FC_RGBA_UNKNOWN || rgba >= FC_RGBA_NONE) {
0N/A return TEXT_AA_ON;
0N/A } else {
0N/A switch (rgba) {
0N/A case FC_RGBA_RGB : return TEXT_AA_LCD_HRGB;
0N/A case FC_RGBA_BGR : return TEXT_AA_LCD_HBGR;
0N/A case FC_RGBA_VRGB : return TEXT_AA_LCD_VRGB;
0N/A case FC_RGBA_VBGR : return TEXT_AA_LCD_VBGR;
0N/A default : return TEXT_AA_LCD_HRGB; // should not get here.
0N/A }
0N/A }
0N/A}
0N/A
426N/AJNIEXPORT jint JNICALL
426N/AJava_sun_font_FontManager_getFontConfigVersion
426N/A (JNIEnv *env, jclass obj) {
426N/A
426N/A void* libfontconfig;
426N/A FcGetVersionFuncType FcGetVersion;
426N/A int version = 0;
426N/A
426N/A if ((libfontconfig = openFontConfig()) == NULL) {
426N/A return 0;
426N/A }
426N/A
426N/A FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
426N/A
426N/A if (FcGetVersion == NULL) {
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A return 0;
426N/A }
426N/A version = (*FcGetVersion)();
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A
426N/A return version;
426N/A}
426N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_font_FontManager_getFontConfig
426N/A(JNIEnv *env, jclass obj, jstring localeStr, jobject fcInfoObj,
426N/A jobjectArray fcCompFontArray, jboolean includeFallbacks) {
0N/A
0N/A FcNameParseFuncType FcNameParse;
0N/A FcPatternAddStringFuncType FcPatternAddString;
0N/A FcConfigSubstituteFuncType FcConfigSubstitute;
0N/A FcDefaultSubstituteFuncType FcDefaultSubstitute;
0N/A FcFontMatchFuncType FcFontMatch;
0N/A FcPatternGetStringFuncType FcPatternGetString;
0N/A FcPatternDestroyFuncType FcPatternDestroy;
426N/A FcPatternGetCharSetFuncType FcPatternGetCharSet;
426N/A FcFontSortFuncType FcFontSort;
426N/A FcFontSetDestroyFuncType FcFontSetDestroy;
426N/A FcCharSetUnionFuncType FcCharSetUnion;
426N/A FcCharSetSubtractCountFuncType FcCharSetSubtractCount;
426N/A FcGetVersionFuncType FcGetVersion;
426N/A FcConfigGetCacheDirsFuncType FcConfigGetCacheDirs;
426N/A FcStrListNextFuncType FcStrListNext;
426N/A FcStrListDoneFuncType FcStrListDone;
0N/A
0N/A int i, arrlen;
426N/A jobject fcCompFontObj;
0N/A jstring fcNameStr, jstr;
0N/A const char *locale, *fcName;
426N/A FcPattern *pattern;
0N/A FcResult result;
0N/A void* libfontconfig;
426N/A jfieldID fcNameID, fcFirstFontID, fcAllFontsID, fcVersionID, fcCacheDirsID;
426N/A jfieldID familyNameID, styleNameID, fullNameID, fontFileID;
426N/A jmethodID fcFontCons;
426N/A char* debugMinGlyphsStr = getenv("J2D_DEBUG_MIN_GLYPHS");
0N/A
426N/A jclass fcInfoClass =
0N/A (*env)->FindClass(env, "sun/font/FontManager$FontConfigInfo");
426N/A jclass fcCompFontClass =
426N/A (*env)->FindClass(env, "sun/font/FontManager$FcCompFont");
426N/A jclass fcFontClass =
426N/A (*env)->FindClass(env, "sun/font/FontManager$FontConfigFont");
0N/A
426N/A if (fcInfoObj == NULL || fcCompFontArray == NULL || fcInfoClass == NULL ||
426N/A fcCompFontClass == NULL || fcFontClass == NULL) {
0N/A return;
0N/A }
0N/A
426N/A fcVersionID = (*env)->GetFieldID(env, fcInfoClass, "fcVersion", "I");
426N/A
426N/A fcCacheDirsID = (*env)->GetFieldID(env, fcInfoClass, "cacheDirs",
426N/A "[Ljava/lang/String;");
426N/A
426N/A fcNameID = (*env)->GetFieldID(env, fcCompFontClass,
0N/A "fcName", "Ljava/lang/String;");
426N/A fcFirstFontID =
426N/A (*env)->GetFieldID(env, fcCompFontClass, "firstFont",
426N/A "Lsun/font/FontManager$FontConfigFont;");
426N/A
426N/A fcAllFontsID =
426N/A (*env)->GetFieldID(env, fcCompFontClass, "allFonts",
426N/A "[Lsun/font/FontManager$FontConfigFont;");
426N/A
426N/A fcFontCons = (*env)->GetMethodID(env, fcFontClass, "<init>", "()V");
426N/A
426N/A familyNameID = (*env)->GetFieldID(env, fcFontClass,
0N/A "familyName", "Ljava/lang/String;");
426N/A styleNameID = (*env)->GetFieldID(env, fcFontClass,
426N/A "styleStr", "Ljava/lang/String;");
426N/A fullNameID = (*env)->GetFieldID(env, fcFontClass,
426N/A "fullName", "Ljava/lang/String;");
426N/A fontFileID = (*env)->GetFieldID(env, fcFontClass,
0N/A "fontFile", "Ljava/lang/String;");
0N/A
426N/A if (fcVersionID == NULL || fcCacheDirsID == NULL || fcNameID == NULL ||
426N/A fcFirstFontID == NULL || fcAllFontsID == NULL || fcFontCons == NULL ||
426N/A familyNameID == NULL || styleNameID == NULL || fullNameID == NULL ||
426N/A fontFileID == NULL) {
0N/A return;
0N/A }
0N/A
0N/A if ((libfontconfig = openFontConfig()) == NULL) {
0N/A return;
0N/A }
0N/A
0N/A FcNameParse = (FcNameParseFuncType)dlsym(libfontconfig, "FcNameParse");
0N/A FcPatternAddString =
0N/A (FcPatternAddStringFuncType)dlsym(libfontconfig, "FcPatternAddString");
0N/A FcConfigSubstitute =
0N/A (FcConfigSubstituteFuncType)dlsym(libfontconfig, "FcConfigSubstitute");
0N/A FcDefaultSubstitute = (FcDefaultSubstituteFuncType)
0N/A dlsym(libfontconfig, "FcDefaultSubstitute");
0N/A FcFontMatch = (FcFontMatchFuncType)dlsym(libfontconfig, "FcFontMatch");
0N/A FcPatternGetString =
0N/A (FcPatternGetStringFuncType)dlsym(libfontconfig, "FcPatternGetString");
0N/A FcPatternDestroy =
0N/A (FcPatternDestroyFuncType)dlsym(libfontconfig, "FcPatternDestroy");
426N/A FcPatternGetCharSet =
426N/A (FcPatternGetCharSetFuncType)dlsym(libfontconfig,
426N/A "FcPatternGetCharSet");
426N/A FcFontSort =
426N/A (FcFontSortFuncType)dlsym(libfontconfig, "FcFontSort");
426N/A FcFontSetDestroy =
426N/A (FcFontSetDestroyFuncType)dlsym(libfontconfig, "FcFontSetDestroy");
426N/A FcCharSetUnion =
426N/A (FcCharSetUnionFuncType)dlsym(libfontconfig, "FcCharSetUnion");
426N/A FcCharSetSubtractCount =
426N/A (FcCharSetSubtractCountFuncType)dlsym(libfontconfig,
426N/A "FcCharSetSubtractCount");
426N/A FcGetVersion = (FcGetVersionFuncType)dlsym(libfontconfig, "FcGetVersion");
0N/A
0N/A if (FcNameParse == NULL ||
0N/A FcPatternAddString == NULL ||
0N/A FcConfigSubstitute == NULL ||
0N/A FcDefaultSubstitute == NULL ||
0N/A FcFontMatch == NULL ||
0N/A FcPatternGetString == NULL ||
426N/A FcPatternDestroy == NULL ||
426N/A FcPatternGetCharSet == NULL ||
426N/A FcFontSetDestroy == NULL ||
426N/A FcCharSetUnion == NULL ||
426N/A FcGetVersion == NULL ||
426N/A FcCharSetSubtractCount == NULL) {/* problem with the library: return.*/
0N/A closeFontConfig(libfontconfig, JNI_FALSE);
0N/A return;
0N/A }
0N/A
426N/A (*env)->SetIntField(env, fcInfoObj, fcVersionID, (*FcGetVersion)());
426N/A
426N/A /* Optionally get the cache dir locations. This isn't
426N/A * available until v 2.4.x, but this is OK since on those later versions
426N/A * we can check the time stamps on the cache dirs to see if we
426N/A * are out of date. There are a couple of assumptions here. First
426N/A * that the time stamp on the directory changes when the contents are
426N/A * updated. Secondly that the locations don't change. The latter is
426N/A * most likely if a new version of fontconfig is installed, but we also
426N/A * invalidate the cache if we detect that. Arguably even that is "rare",
426N/A * and most likely is tied to an OS upgrade which gets a new file anyway.
426N/A */
426N/A FcConfigGetCacheDirs =
426N/A (FcConfigGetCacheDirsFuncType)dlsym(libfontconfig,
426N/A "FcConfigGetCacheDirs");
426N/A FcStrListNext =
426N/A (FcStrListNextFuncType)dlsym(libfontconfig, "FcStrListNext");
426N/A FcStrListDone =
426N/A (FcStrListDoneFuncType)dlsym(libfontconfig, "FcStrListDone");
426N/A if (FcStrListNext != NULL && FcStrListDone != NULL &&
426N/A FcConfigGetCacheDirs != NULL) {
426N/A
426N/A FcStrList* cacheDirs;
426N/A FcChar8* cacheDir;
426N/A int cnt = 0;
426N/A jobject cacheDirArray =
426N/A (*env)->GetObjectField(env, fcInfoObj, fcCacheDirsID);
426N/A int max = (*env)->GetArrayLength(env, cacheDirArray);
426N/A
426N/A cacheDirs = (*FcConfigGetCacheDirs)(NULL);
426N/A if (cacheDirs != NULL) {
426N/A while ((cnt < max) && (cacheDir = (*FcStrListNext)(cacheDirs))) {
426N/A jstr = (*env)->NewStringUTF(env, (const char*)cacheDir);
426N/A (*env)->SetObjectArrayElement(env, cacheDirArray, cnt++, jstr);
426N/A }
426N/A (*FcStrListDone)(cacheDirs);
426N/A }
426N/A }
426N/A
0N/A locale = (*env)->GetStringUTFChars(env, localeStr, 0);
0N/A
426N/A arrlen = (*env)->GetArrayLength(env, fcCompFontArray);
0N/A for (i=0; i<arrlen; i++) {
426N/A FcFontSet* fontset;
426N/A int fn, j, fontCount, nfonts, minGlyphs;
426N/A FcChar8 **family, **styleStr, **fullname, **file;
426N/A jarray fcFontArr;
426N/A
426N/A fcCompFontObj = (*env)->GetObjectArrayElement(env, fcCompFontArray, i);
0N/A fcNameStr =
426N/A (jstring)((*env)->GetObjectField(env, fcCompFontObj, fcNameID));
0N/A fcName = (*env)->GetStringUTFChars(env, fcNameStr, 0);
0N/A if (fcName == NULL) {
0N/A continue;
0N/A }
0N/A pattern = (*FcNameParse)((FcChar8 *)fcName);
426N/A if (pattern == NULL) {
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A return;
426N/A }
426N/A
0N/A /* locale may not usually be necessary as fontconfig appears to apply
0N/A * this anyway based on the user's environment. However we want
0N/A * to use the value of the JDK startup locale so this should take
0N/A * care of it.
0N/A */
0N/A if (locale != NULL) {
0N/A (*FcPatternAddString)(pattern, FC_LANG, (unsigned char*)locale);
0N/A }
0N/A (*FcConfigSubstitute)(NULL, pattern, FcMatchPattern);
0N/A (*FcDefaultSubstitute)(pattern);
426N/A fontset = (*FcFontSort)(NULL, pattern, FcTrue, NULL, &result);
426N/A if (fontset == NULL) {
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A return;
426N/A }
0N/A
426N/A /* fontconfig returned us "nfonts". If we are just getting the
426N/A * first font, we set nfont to zero. Otherwise we use "nfonts".
426N/A * Next create separate C arrrays of length nfonts for family file etc.
426N/A * Inspect the returned fonts and the ones we like (adds enough glyphs)
426N/A * are added to the arrays and we increment 'fontCount'.
426N/A */
426N/A if (includeFallbacks) {
426N/A nfonts = fontset->nfont;
426N/A } else {
426N/A nfonts = 1;
426N/A }
426N/A family = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
426N/A styleStr = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
426N/A fullname = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
426N/A file = (FcChar8**)calloc(nfonts, sizeof(FcChar8*));
426N/A if (family == NULL || styleStr == NULL ||
426N/A fullname == NULL || file == NULL) {
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A return;
426N/A }
426N/A fontCount = 0;
426N/A minGlyphs = 20;
426N/A if (debugMinGlyphsStr != NULL) {
426N/A int val = minGlyphs;
426N/A sscanf(debugMinGlyphsStr, "%5d", &val);
426N/A if (val >= 0 && val <= 65536) {
426N/A minGlyphs = val;
426N/A }
426N/A }
426N/A for (j=0; j<nfonts; j++) {
426N/A FcPattern *fontPattern = fontset->fonts[j];
426N/A FcChar8 *fontformat;
426N/A FcCharSet *unionCharset, *charset;
426N/A
426N/A fontformat = NULL;
426N/A (*FcPatternGetString)(fontPattern, FC_FONTFORMAT, 0, &fontformat);
426N/A if (fontformat != NULL && strcmp((char*)fontformat, "TrueType")
426N/A != 0) {
426N/A continue;
426N/A }
426N/A result = (*FcPatternGetCharSet)(fontPattern,
426N/A FC_CHARSET, 0, &charset);
426N/A if (result != FcResultMatch) {
426N/A closeFontConfig(libfontconfig, JNI_FALSE);
426N/A return;
426N/A }
0N/A
426N/A /* We don't want 20 or 30 fonts, so once we hit 10 fonts,
426N/A * then require that they really be adding value. Too many
426N/A * adversely affects load time for minimal value-add.
426N/A * This is still likely far more than we've had in the past.
426N/A */
426N/A if (nfonts==10) {
426N/A minGlyphs = 50;
426N/A }
426N/A if (j == 0) {
426N/A unionCharset = charset;
426N/A } else {
426N/A if ((*FcCharSetSubtractCount)(charset, unionCharset)
426N/A > minGlyphs) {
426N/A unionCharset = (* FcCharSetUnion)(unionCharset, charset);
426N/A } else {
426N/A continue;
426N/A }
0N/A }
426N/A
426N/A fontCount++; // found a font we will use.
426N/A (*FcPatternGetString)(fontPattern, FC_FILE, 0, &file[j]);
426N/A (*FcPatternGetString)(fontPattern, FC_FAMILY, 0, &family[j]);
426N/A (*FcPatternGetString)(fontPattern, FC_STYLE, 0, &styleStr[j]);
426N/A (*FcPatternGetString)(fontPattern, FC_FULLNAME, 0, &fullname[j]);
426N/A }
426N/A
426N/A /* Once we get here 'fontCount' is the number of returned fonts
426N/A * we actually want to use, so we create 'fcFontArr' of that length.
426N/A * The non-null entries of "family[]" etc are those fonts.
426N/A * Then loop again over all nfonts adding just those non-null ones
426N/A * to 'fcFontArr'. If its null (we didn't want the font)
426N/A * then we don't enter the main body.
426N/A * So we should never get more than 'fontCount' entries.
426N/A */
426N/A if (includeFallbacks) {
426N/A fcFontArr =
426N/A (*env)->NewObjectArray(env, fontCount, fcFontClass, NULL);
426N/A (*env)->SetObjectField(env,fcCompFontObj, fcAllFontsID, fcFontArr);
426N/A }
426N/A fn=0;
426N/A
426N/A for (j=0;j<nfonts;j++) {
426N/A if (family[j] != NULL) {
426N/A jobject fcFont =
426N/A (*env)->NewObject(env, fcFontClass, fcFontCons);
426N/A jstr = (*env)->NewStringUTF(env, (const char*)family[j]);
426N/A (*env)->SetObjectField(env, fcFont, familyNameID, jstr);
426N/A if (file[j] != NULL) {
426N/A jstr = (*env)->NewStringUTF(env, (const char*)file[j]);
426N/A (*env)->SetObjectField(env, fcFont, fontFileID, jstr);
426N/A }
426N/A if (styleStr[j] != NULL) {
426N/A jstr = (*env)->NewStringUTF(env, (const char*)styleStr[j]);
426N/A (*env)->SetObjectField(env, fcFont, styleNameID, jstr);
426N/A }
426N/A if (fullname[j] != NULL) {
426N/A jstr = (*env)->NewStringUTF(env, (const char*)fullname[j]);
426N/A (*env)->SetObjectField(env, fcFont, fullNameID, jstr);
426N/A }
426N/A if (fn==0) {
426N/A (*env)->SetObjectField(env, fcCompFontObj,
426N/A fcFirstFontID, fcFont);
426N/A }
426N/A if (includeFallbacks) {
426N/A (*env)->SetObjectArrayElement(env, fcFontArr, fn++,fcFont);
426N/A }
0N/A }
0N/A }
0N/A (*env)->ReleaseStringUTFChars (env, fcNameStr, (const char*)fcName);
426N/A (*FcFontSetDestroy)(fontset);
0N/A (*FcPatternDestroy)(pattern);
426N/A free(family);
426N/A free(styleStr);
426N/A free(fullname);
426N/A free(file);
0N/A }
0N/A
0N/A /* release resources and close the ".so" */
0N/A
0N/A if (locale) {
0N/A (*env)->ReleaseStringUTFChars (env, localeStr, (const char*)locale);
0N/A }
0N/A closeFontConfig(libfontconfig, JNI_TRUE);
0N/A}