(svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.

This commit is contained in:
rubidium 2006-11-17 22:15:55 +00:00
parent f2cda44d2e
commit bd129cf6bf
5 changed files with 138 additions and 28 deletions

View File

@ -241,6 +241,12 @@ $(error WITH_FREETYPE can't be used when FREETYPE_CONFIG is not set. Edit Makefi
endif
endif
ifdef WITH_FONTCONFIG
ifndef FONTCONFIG_CONFIG
$(error WITH_FONTCONFIG can't be used when FONTOCNFIG_CONFIG is not set. Edit Makefile.config to correct this)
endif
endif
##############################################################################
#
# Compiler configuration
@ -508,6 +514,15 @@ CFLAGS += $(CCFLAGS_FREETYPE)
LIBS += $(LDFLAGS_FREETYPE)
endif
# fontconfig config
ifdef WITH_FONTCONFIG
CDEFS += -DWITH_FONTCONFIG
CCFLAGS_FONTCONFIG := $(shell $(FONTCONFIG_CONFIG) --cflags)
LDFLAGS_FONTCONFIG := $(shell $(FONTCONFIG_CONFIG) --libs)
CFLAGS += $(CCFLAGS_FONTCONFIG)
LIBS += $(LDFLAGS_FONTCONFIG)
endif
# iconv is enabled defaultly on OSX >= 10.3
ifdef OSX
ifndef JAGUAR

14
configure vendored
View File

@ -36,12 +36,14 @@ function showhelp() {
echo " network Do you want network-support? [yes]"
echo " cocoa Do you want cocoa-support? (MacOSX) [no]"
echo " freetype Do you want freetype-support? [yes]"
echo " fontconfig Do you want fontconfig-support? [yes]"
echo ""
echo "Params used to configure external libs:"
echo " --static-zlib-path Set the path to your static zlib []"
echo " --sdl-config Where is your sdl-config [sdl-config]"
echo " --libpng-config Where is your libpng-config [libpng-config]"
echo " --freetype-config Where is your freetype-config [freetype-config]"
echo " --fontconfig-config Where is your fontconfig-config [pkg-config fontconfig]"
echo " --with-iconv Set the path to your iconv headers []"
echo " "
}
@ -189,6 +191,12 @@ do
--without-freetype)
PARAM="$PARAM WITH_FREETYPE="
;;
--with-fontconfig)
PARAM="$PARAM WITH_FONTCONFIG=1"
;;
--without-fontconfig)
PARAM="$PARAM WITH_FONTCONFIG="
;;
--static-zlib-path=*)
handle STATIC_ZLIB_PATH "$n"
;;
@ -213,6 +221,12 @@ do
--freetype-config)
ITEM="FREETYPE_CONFIG"
;;
--fontconfig-config=*)
handle FONTCONFIG_CONFIG "$n"
;;
--fontconfig-config)
ITEM="FONTCONFIG_CONFIG"
;;
--*=*)
echo -n "Unknown switch "

View File

@ -18,6 +18,10 @@
#include FT_FREETYPE_H
#include FT_GLYPH_H
#ifdef WITH_FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
static FT_Library _library = NULL;
static FT_Face _face_small = NULL;
static FT_Face _face_medium = NULL;
@ -31,6 +35,12 @@ enum {
};
/**
* Loads the freetype font.
* First type to load the fontname as if it were a path. If that fails,
* try to resolve the filename of the font using fontconfig, where the
* format is 'font family name' or 'font family name, font style'.
*/
static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
{
FT_Error error;
@ -38,6 +48,69 @@ static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *t
if (strlen(font_name) == 0) return;
error = FT_New_Face(_library, font_name, 0, face);
#ifdef WITH_FONTCONFIG
/* Failed to load the font, so try it with fontconfig */
if (error != FT_Err_Ok) {
if (!FcInit()) {
ShowInfoF("Unable to load font configuration");
} else {
FcPattern *match;
FcPattern *pat;
FcFontSet *fs;
FcResult result;
char *font_style;
char *font_family;
/* Split & strip the font's style */
font_family = strdup(font_name);
font_style = strchr(font_family, ',');
if (font_style != NULL) {
font_style[0] = '\0';
font_style++;
while (*font_style == ' ' || *font_style == '\t') font_style++;
}
/* Resolve the name and populate the information structure */
pat = FcNameParse((FcChar8*)font_family);
if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
FcConfigSubstitute(0, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
fs = FcFontSetCreate();
match = FcFontMatch(0, pat, &result);
if (fs != NULL && match != NULL) {
int i;
FcChar8 *family;
FcChar8 *style;
FcChar8 *file;
FcFontSetAdd(fs, match);
for (i = 0; error != FT_Err_Ok && i < fs->nfont; i++) {
/* Try the new filename */
if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch) {
/* The correct style? */
if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
/* Font config takes the best shot, which, if the family name is spelled
* wrongly a 'random' font, so check whether the family name is the
* same as the supplied name */
if (strcasecmp(font_family, (char*)family) == 0) {
error = FT_New_Face(_library, (char *)file, 0, face);
}
}
}
}
free(font_family);
FcPatternDestroy(pat);
FcFontSetDestroy(fs);
FcFini();
}
}
#endif
if (error == FT_Err_Ok) {
/* Attempt to select the unicode character map */
error = FT_Select_Charmap(*face, ft_encoding_unicode);

View File

@ -74,6 +74,7 @@ $(MAKE_CONFIG):
$(call CONFIG_LINE,STATIC_ZLIB_PATH:=$(STATIC_ZLIB_PATH))
$(call CONFIG_LINE,WITH_COCOA:=$(WITH_COCOA))
$(call CONFIG_LINE,WITH_FREETYPE:=$(WITH_FREETYPE))
$(call CONFIG_LINE,WITH_FONTCONFIG:=$(WITH_FONTCONFIG))
$(call CONFIG_LINE,)
$(call CONFIG_LINE,\# OS flags)
@ -102,6 +103,7 @@ $(MAKE_CONFIG):
$(call CONFIG_LINE,SDL_CONFIG:=$(SDL_CONFIG))
$(call CONFIG_LINE,LIBPNG_CONFIG:=$(LIBPNG_CONFIG))
$(call CONFIG_LINE,FREETYPE_CONFIG:=$(FREETYPE_CONFIG))
$(call CONFIG_LINE,FONTCONFIG_CONFIG:=$(FONTCONFIG_CONFIG))
$(call CONFIG_LINE,BEOS_NET_SERVER:=$(BEOS_NET_SERVER))
$(call CONFIG_LINE,CONFIG_INCLUDED:=yes)
$(call CONFIG_LINE,PATH_SET:=$(PATH_SET))

View File

@ -69,6 +69,9 @@ LIBPNG_CONFIG :=libpng-config
# set freetype-config to the default value
FREETYPE_CONFIG:=freetype-config
# set pkg-config to the default value
FONTCONFIG_CONFIG:=pkg-config fontconfig
# Networking, enabled by default
WITH_NETWORK:=1
@ -81,6 +84,9 @@ WITH_PNG:=$(shell $(LIBPNG_CONFIG) --version 2>/dev/null)
# Freetype detection
WITH_FREETYPE:=$(shell $(FREETYPE_CONFIG) --ftversion 2>/dev/null)
# fontconfig detection
WITH_FONTCONFIG:=$(shell $(FONTCONFIG_CONFIG) --modversion 2>/dev/null)
ifdef WITH_PNG
# LibPNG depends on Zlib
WITH_ZLIB:=1