00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "ui_main.h"
00026 #include "ui_internal.h"
00027 #include "ui_font.h"
00028 #include "ui_parse.h"
00029
00030 #include "../client.h"
00031 #include "../../shared/parse.h"
00032 #include "../renderer/r_font.h"
00033
00034 #define MAX_FONTS 16
00035 static int numFonts = 0;
00036 static uiFont_t fonts[MAX_FONTS];
00037
00038 static const value_t fontValues[] = {
00039 {"font", V_TRANSLATION_STRING, offsetof(uiFont_t, path), 0},
00040 {"size", V_INT, offsetof(uiFont_t, size), MEMBER_SIZEOF(uiFont_t, size)},
00041 {"style", V_CLIENT_HUNK_STRING, offsetof(uiFont_t, style), 0},
00042
00043 {NULL, V_NULL, 0, 0}
00044 };
00045
00052 static void UI_RegisterFont (const uiFont_t *font)
00053 {
00054 const char *path = _(font->path);
00055
00056 if (!path)
00057 Com_Error(ERR_FATAL, "...font without path (font %s)", font->name);
00058
00059 if (FS_CheckFile("%s", path) == -1)
00060 Com_Error(ERR_FATAL, "...font file %s does not exist (font %s)", path, font->name);
00061
00062 R_FontRegister(font->name, font->size, path, font->style);
00063 }
00064
00068 void UI_ParseFont (const char *name, const char **text)
00069 {
00070 uiFont_t *font;
00071 const char *errhead = "UI_ParseFont: unexpected end of file (font";
00072 const char *token;
00073 const value_t *v = NULL;
00074
00075
00076 if (UI_GetFontByID(name)) {
00077 Com_Printf("UI_ParseFont: font \"%s\" with same name found, second ignored\n", name);
00078 return;
00079 }
00080
00081 if (numFonts >= MAX_FONTS) {
00082 Com_Printf("UI_ParseFont: Max fonts reached\n");
00083 return;
00084 }
00085
00086
00087 font = &fonts[numFonts];
00088 memset(font, 0, sizeof(*font));
00089
00090 font->name = Mem_PoolStrDup(name, ui_sysPool, 0);
00091
00092 Com_DPrintf(DEBUG_CLIENT, "...found font %s (%i)\n", font->name, numFonts);
00093
00094
00095 token = Com_Parse(text);
00096
00097 if (!*text || *token != '{') {
00098 Com_Printf("UI_ParseFont: font \"%s\" without body ignored\n", name);
00099 return;
00100 }
00101
00102 numFonts++;
00103
00104 do {
00105
00106 token = Com_EParse(text, errhead, name);
00107 if (!*text)
00108 break;
00109 if (*token == '}')
00110 break;
00111
00112 for (v = fontValues; v->string; v++)
00113 if (!strncmp(token, v->string, sizeof(v->string))) {
00114
00115 token = Com_EParse(text, errhead, name);
00116 if (!*text)
00117 return;
00118
00119 switch (v->type) {
00120 case V_TRANSLATION_STRING:
00121 token++;
00122 case V_CLIENT_HUNK_STRING:
00123 Mem_PoolStrDupTo(token, (char**) ((char*)font + (int)v->ofs), ui_sysPool, 0);
00124 break;
00125 default:
00126 Com_EParseValue(font, token, v->type, v->ofs, v->size);
00127 break;
00128 }
00129 break;
00130 }
00131
00132 if (!v->string)
00133 Com_Printf("UI_ParseFont: unknown token \"%s\" ignored (font %s)\n", token, name);
00134 } while (*text);
00135
00136 UI_RegisterFont(font);
00137 }
00138
00144 const char *UI_GetFontFromNode (const uiNode_t *const node)
00145 {
00146 if (node && node->font) {
00147 return UI_GetReferenceString(node, node->font);
00148 }
00149 return "f_small";
00150 }
00151
00152
00156 const uiFont_t *UI_GetFontByID (const char *name)
00157 {
00158 int i;
00159
00160 for (i = 0; i < numFonts; i++)
00161 if (!strcmp(fonts[i].name, name))
00162 return &fonts[i];
00163
00164 return NULL;
00165 }
00166
00167 int UI_FontGetHeight (const char *fontID)
00168 {
00169 font_t *font = R_GetFont(fontID);
00170 if (font != NULL)
00171 return font->height;
00172 return -1;
00173 }
00174
00178 void UI_InitFonts (void)
00179 {
00180 int i;
00181
00182 Com_Printf("...registering %i fonts\n", numFonts);
00183 for (i = 0; i < numFonts; i++)
00184 UI_RegisterFont(&fonts[i]);
00185 }