From 39447c977e65bf127d5346d61f5c358a0c04abad Mon Sep 17 00:00:00 2001
From: jsalmon3 <>
Date: Sat, 27 Mar 2004 19:17:54 +0000
Subject: [PATCH] Removed hardcoded font names

---
 src/game/campaign.cpp |   4 +-
 src/include/font.h    |  16 +++--
 src/ui/script_ui.cpp  |   8 +--
 src/video/font.cpp    | 134 ++++++++++++++++++++++++------------------
 4 files changed, 89 insertions(+), 73 deletions(-)

diff --git a/src/game/campaign.cpp b/src/game/campaign.cpp
index 37da3dc9c..c288ad4c4 100644
--- a/src/game/campaign.cpp
+++ b/src/game/campaign.cpp
@@ -236,7 +236,7 @@ local void ParseShowPicture(lua_State* l, CampaignChapter* chapter)
 
 				if (!strcmp(value, "font")) {
 					lua_rawgeti(l, -1, k + 1);
-					(*text)->Font = CclFontByIdentifier(LuaToString(l, -1));
+					(*text)->Font = FontByIdent(LuaToString(l, -1));
 					lua_pop(l, 1);
 				} else if (!strcmp(value, "x")) {
 					lua_rawgeti(l, -1, k + 1);
@@ -573,7 +573,7 @@ global void SaveCampaign(CLFile* file)
 				ch->Data.Picture.DisplayTime);
 			for (text = ch->Data.Picture.Text; text; text = text->Next) {
 				CLprintf(file, "      \"text\", {\n");
-				CLprintf(file, "        \"font\", \"%s\",\n", FontNames[text->Font]);
+				CLprintf(file, "        \"font\", \"%s\",\n", FontName(text->Font));
 				CLprintf(file, "        \"x\", %d,\n", text->X);
 				CLprintf(file, "        \"y\", %d,\n", text->Y);
 				CLprintf(file, "        \"width\", %d,\n", text->Width);
diff --git a/src/include/font.h b/src/include/font.h
index 3c78b6149..1b79baf18 100644
--- a/src/include/font.h
+++ b/src/include/font.h
@@ -103,6 +103,7 @@ typedef struct _color_font_ {
 
 /**
 **  Font selector for the font functions.
+**  FIXME: should be removed
 */
 enum _game_font_ {
 	SmallFont,       /// Small font used in stats
@@ -110,15 +111,10 @@ enum _game_font_ {
 	LargeFont,       /// Large font used in menus
 	SmallTitleFont,  /// Small font used in episoden titles
 	LargeTitleFont,  /// Large font used in episoden titles
-	User1Font,       /// User font 1
-	User2Font,       /// User font 2
-	User3Font,       /// User font 3
-	User4Font,       /// User font 4
-	User5Font,       /// User font 5
-	// ... more to come or not
-	MaxFonts,        /// Number of fonts supported
 };
 
+#define MaxFonts 10  /// Number of fonts supported
+
 /**
 **  Color selector for the font functions.
 */
@@ -177,8 +173,10 @@ extern void FontsCclRegister(void);
 extern void CleanFonts(void);
 	/// Check if font is loaded
 extern int IsFontLoaded(unsigned font);
-	/// Font symbol to id
-extern int CclFontByIdentifier(const char* type);
+	/// Find font by identifier
+extern int FontByIdent(const char* ident);
+	// Find the name of a font.
+extern const char* FontName(int font);
 
 //@}
 
diff --git a/src/ui/script_ui.cpp b/src/ui/script_ui.cpp
index 85e858a25..323d6a10f 100644
--- a/src/ui/script_ui.cpp
+++ b/src/ui/script_ui.cpp
@@ -312,7 +312,7 @@ local int CclSetTitleScreens(lua_State* l)
 						if (!strcmp(value, "Text")) {
 							TitleScreens[j]->Labels[k]->Text = strdup(LuaToString(l, -1));
 						} else if (!strcmp(value, "Font")) {
-							TitleScreens[j]->Labels[k]->Font = CclFontByIdentifier(LuaToString(l, -1));
+							TitleScreens[j]->Labels[k]->Font = FontByIdent(LuaToString(l, -1));
 						} else if (!strcmp(value, "Pos")) {
 							if (!lua_istable(l, -1) || luaL_getn(l, -1) != 2) {
 								LuaError(l, "incorrect argument");
@@ -696,7 +696,7 @@ local void CclParseInfoText(lua_State* l, InfoText* text)
 			lua_pop(l, 1);
 		} else if (!strcmp(value, "font")) {
 			lua_rawgeti(l, -1, j + 1);
-			text->Font = CclFontByIdentifier(LuaToString(l, -1));
+			text->Font = FontByIdent(LuaToString(l, -1));
 			lua_pop(l, 1);
 		} else if (!strcmp(value, "pos")) {
 			lua_rawgeti(l, -1, j + 1);
@@ -1884,7 +1884,7 @@ local int CclDefineUI(lua_State* l)
 					lua_pop(l, 1);
 				} else if (!strcmp(value, "font")) {
 					lua_rawgeti(l, j + 1, k + 1);
-					ui->StatusLineFont = CclFontByIdentifier(LuaToString(l, -1));
+					ui->StatusLineFont = FontByIdent(LuaToString(l, -1));
 					lua_pop(l, 1);
 				} else {
 					LuaError(l, "Unsupported tag: %s" _C_ value);
@@ -2519,7 +2519,7 @@ local int CclDefineMenuItem(lua_State* l)
 				}
 			}
 		} else if (!strcmp(value, "font")) {
-			item->font = CclFontByIdentifier(LuaToString(l, j + 1));
+			item->font = FontByIdent(LuaToString(l, j + 1));
 		} else if (!strcmp(value, "init")) {
 			if (!lua_isstring(l, j + 1) && !lua_isnil(l, j + 1)) {
 				LuaError(l, "incorrect argument");
diff --git a/src/video/font.cpp b/src/video/font.cpp
index bc8cdf264..4b7a46208 100644
--- a/src/video/font.cpp
+++ b/src/video/font.cpp
@@ -63,6 +63,15 @@ local FontColorMapping* FontColor;
 	/// Font color mappings
 local FontColorMapping* FontColorMappings;
 
+	/// Font mapping
+typedef struct _font_mapping_ {
+	char* Ident;                            /// Font name
+	int Font;                               /// Ident number
+	struct _font_mapping_* Next;            /// Next pointer
+} FontMapping;
+
+local FontMapping* FontMappings;
+
 /**
 **  Fonts table
 **
@@ -93,28 +102,10 @@ local int FontBitmapWidths[MaxFonts];
 local int CurrentFont;
 #endif
 
-/**
-**  FIXME: should use the names of the real fonts.
-*/
-global char* FontNames[] = {
-	"small",
-	"game",
-	"large",
-	"small-title",
-	"large-title",
-	"user1",
-	"user2",
-	"user3",
-	"user4",
-	"user5",
-};
-
 /*----------------------------------------------------------------------------
 --  Functions
 ----------------------------------------------------------------------------*/
 
-// FIXME: should use RLE encoded fonts, not color key fonts.
-
 /**
 **  Draw character with current color.
 **
@@ -188,7 +179,7 @@ local FontColorMapping* GetFontColorMapping(char* color)
 		}
 		fcm = fcm->Next;
 	}
-	fprintf(stderr, "Font mapping not found: '%s'\n", color);
+	fprintf(stderr, "Font color mapping not found: '%s'\n", color);
 	ExitFatal(1);
 	return NULL;
 }
@@ -250,8 +241,8 @@ global int VideoTextLength(unsigned font, const unsigned char* text)
 			}
 		}
 		if (!isformat) {
-		width += widths[*s - 32] + 1;
-	}
+			width += widths[*s - 32] + 1;
+		}
 	}
 	return width;
 }
@@ -714,46 +705,56 @@ global void LoadFonts(void)
 	}
 }
 
+/**
+**  Find font by identifier.
+**
+**  @param ident  Font identifier
+**
+**  @return       Integer as font identifier.
+*/
+global int FontByIdent(const char* ident)
+{
+	FontMapping* fm;
+
+	fm = FontMappings;
+	while (fm) {
+		if (!strcmp(fm->Ident, ident)) {
+			return fm->Font;
+		}
+		fm = fm->Next;
+	}
+	fprintf(stderr, "Font not found: '%s'", ident);
+	ExitFatal(1);
+	return 0;
+}
+
+/**
+**  Find the name of a font.
+**
+**  @param font  Font identifier.
+**
+**  @return      Name of the font.
+*/
+global const char* FontName(int font)
+{
+	FontMapping* fm;
+
+	fm = FontMappings;
+	while (fm) {
+		if (fm->Font == font) {
+			return fm->Ident;
+		}
+		fm = fm->Next;
+	}
+	fprintf(stderr, "Font not found: %d", font);
+	ExitFatal(1);
+	return NULL;
+}
+
 /*----------------------------------------------------------------------------
 --  CCL
 ----------------------------------------------------------------------------*/
 
-/**
-**  Font symbol to id.
-**
-**  @param type  Type of the font (game,small,...)
-**
-**  @return      Integer as font identifier.
-*/
-global int CclFontByIdentifier(const char* type)
-{
-	if (!strcmp(type, "game")) {
-		return GameFont;
-	} else if (!strcmp(type, "small")) {
-		return SmallFont;
-	} else if (!strcmp(type, "large")) {
-		return LargeFont;
-	} else if (!strcmp(type, "small-title")) {
-		return SmallTitleFont;
-	} else if (!strcmp(type, "large-title")) {
-		return LargeTitleFont;
-	} else if (!strcmp(type, "user1")) {
-		return User1Font;
-	} else if (!strcmp(type, "user2")) {
-		return User2Font;
-	} else if (!strcmp(type, "user3")) {
-		return User3Font;
-	} else if (!strcmp(type, "user4")) {
-		return User4Font;
-	} else if (!strcmp(type, "user5")) {
-		return User5Font;
-	} else {
-		fprintf(stderr, "Unsupported font tag: %s", type);
-		exit(1);
-	}
-	return 0;
-}
-
 /**
 **  Define the used fonts.
 **
@@ -766,6 +767,8 @@ local int CclDefineFont(lua_State* l)
 	int w;
 	int h;
 	char* file;
+	FontMapping** fm;
+	const char* str;
 
 	if (lua_gettop(l) != 1 || !lua_istable(l, 1)) {
 		LuaError(l, "incorrect argument");
@@ -777,7 +780,22 @@ local int CclDefineFont(lua_State* l)
 	while (lua_next(l, 1)) {
 		value = LuaToString(l, -2);
 		if (!strcmp(value, "Name")) {
-			i = CclFontByIdentifier(LuaToString(l, -1));
+			str = LuaToString(l, -1);
+			fm = &FontMappings;
+			i = 0;
+			while (*fm) {
+				if (!strcmp((*fm)->Ident, str)) {
+					break;
+				}
+				fm = &(*fm)->Next;
+				++i;
+			}
+			if (!*fm) {
+				*fm = malloc(sizeof(**fm));
+				(*fm)->Ident = strdup(str);
+				(*fm)->Font = i;
+				(*fm)->Next = NULL;
+			}
 		} else if (!strcmp(value, "File")) {
 			file = strdup(LuaToString(l, -1));
 		} else if (!strcmp(value, "Size")) {