(svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
authorrubidium
Fri, 17 Nov 2006 22:15:55 +0000
changeset 5117 a4a7fd847b77
parent 5116 2a33a74925c5
child 5118 5a56a0f12206
(svn r7196) -Feature: use fontconfig so one can set the font family name in openttd.cfg instead of the full path to the font.
Makefile
configure
fontcache.c
makefiledir/Makefile.config_writer
makefiledir/Makefile.libdetection
--- a/Makefile	Fri Nov 17 19:31:44 2006 +0000
+++ b/Makefile	Fri Nov 17 22:15:55 2006 +0000
@@ -241,6 +241,12 @@
 	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 @@
 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
--- a/configure	Fri Nov 17 19:31:44 2006 +0000
+++ b/configure	Fri Nov 17 22:15:55 2006 +0000
@@ -10,39 +10,41 @@
 	echo " $0 --your_options"
 	echo ""
 	echo "Params:"
-	echo " --debug            Create debug-release                [no]"
-	echo " --profile          Create profile-release              [no]"
-	echo " --dedicated        Make a dedicated build              [no]"
-	echo " --revision         Set the revision of the compilation [detected]"
-	echo " --target-cc        Sets the target-compiler            [\$CC]"
-	echo " --target-cxx       Sets the C++ target-compiler        []"
-	echo " --host-cc          Sets the host-compiler              [\$CC]"
-	echo " --host-cxx         Sets the C++ host-compiler          []"
-	echo " --os               Sets the OS. Listens to:            [detected]"
-	echo "                       UNIX, OSX, FREEBSD, MORPHOS"
-	echo "                       BEOS, SUNOS, CYGWIN, MINGW"
-	echo " --windres          Sets the windres (Windows)          [windres]"
-	echo " --force-le         Force LE platform                   [no]"
-	echo " --force-be         Force BE platform                   [no]"
+	echo " --debug             Create debug-release                [no]"
+	echo " --profile           Create profile-release              [no]"
+	echo " --dedicated         Make a dedicated build              [no]"
+	echo " --revision          Set the revision of the compilation [detected]"
+	echo " --target-cc         Sets the target-compiler            [\$CC]"
+	echo " --target-cxx        Sets the C++ target-compiler        []"
+	echo " --host-cc           Sets the host-compiler              [\$CC]"
+	echo " --host-cxx          Sets the C++ host-compiler          []"
+	echo " --os                Sets the OS. Listens to:            [detected]"
+	echo "                        UNIX, OSX, FREEBSD, MORPHOS"
+	echo "                        BEOS, SUNOS, CYGWIN, MINGW"
+	echo " --windres           Sets the windres (Windows)          [windres]"
+	echo " --force-le          Force LE platform                   [no]"
+	echo " --force-be          Force BE platform                   [no]"
 	echo ""
 	echo "Params that can be used with --with or --without"
 	echo "   (e.g.: --without-static disables static (default))"
-	echo " static             Do you want a static build?         [no]"
-	echo " directmusic        Do you want direct-music?           [no]"
-	echo " zlib               Do you want zlib-support?           [yes]"
-	echo " sdl                Do you want SDL-support?            [yes]"
-	echo " png                Do you want PNG-support?            [yes]"
-	echo " iconv              Do you want iconv-support?          [no]"
-	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 " static              Do you want a static build?         [no]"
+	echo " directmusic         Do you want direct-music?           [no]"
+	echo " zlib                Do you want zlib-support?           [yes]"
+	echo " sdl                 Do you want SDL-support?            [yes]"
+	echo " png                 Do you want PNG-support?            [yes]"
+	echo " iconv               Do you want iconv-support?          [no]"
+	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 " --with-iconv       Set the path to your iconv headers  []"
+	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 @@
 		--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 @@
 		--freetype-config)
 			ITEM="FREETYPE_CONFIG"
 			;;
+		--fontconfig-config=*)
+			handle FONTCONFIG_CONFIG "$n"
+			;;
+		--fontconfig-config)
+			ITEM="FONTCONFIG_CONFIG"
+			;;
 
 		--*=*)
 			echo -n "Unknown switch "
--- a/fontcache.c	Fri Nov 17 19:31:44 2006 +0000
+++ b/fontcache.c	Fri Nov 17 22:15:55 2006 +0000
@@ -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 @@
 };
 
 
+/**
+ * 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 @@
 	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);
--- a/makefiledir/Makefile.config_writer	Fri Nov 17 19:31:44 2006 +0000
+++ b/makefiledir/Makefile.config_writer	Fri Nov 17 22:15:55 2006 +0000
@@ -74,6 +74,7 @@
 	$(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 @@
 	$(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))
--- a/makefiledir/Makefile.libdetection	Fri Nov 17 19:31:44 2006 +0000
+++ b/makefiledir/Makefile.libdetection	Fri Nov 17 22:15:55 2006 +0000
@@ -69,6 +69,9 @@
 # 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 @@
 # 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