UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_font.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "ui_main.h"
26 #include "ui_internal.h"
27 #include "ui_font.h"
28 #include "ui_parse.h"
29 
30 #include "../cl_shared.h"
31 #include "../../shared/parse.h"
32 #include "../renderer/r_font.h"
33 
34 #define MAX_FONTS 16
35 static int numFonts = 0;
37 
38 static const value_t fontValues[] = {
39  {"font", V_TRANSLATION_STRING, offsetof(uiFont_t, path), 0},
40  {"size", V_INT, offsetof(uiFont_t, size), MEMBER_SIZEOF(uiFont_t, size)},
41  {"style", V_HUNK_STRING, offsetof(uiFont_t, style), 0},
42 
43  {nullptr, V_NULL, 0, 0}
44 };
45 
52 static void UI_RegisterFont (const uiFont_t* font)
53 {
54  const char* path = _(font->path);
55 
56  if (!path)
57  Com_Error(ERR_FATAL, "...font without path (font %s)", font->name);
58 
59  if (FS_CheckFile("%s", path) == -1)
60  Com_Error(ERR_FATAL, "...font file %s does not exist (font %s)", path, font->name);
61 
62  R_FontRegister(font->name, font->size, path, font->style);
63 }
64 
68 bool UI_ParseFont (const char* name, const char** text)
69 {
70  const char* errhead = "UI_ParseFont: unexpected end of file (font";
71 
72  /* search for font with same name */
73  if (UI_GetFontByID(name)) {
74  Com_Printf("UI_ParseFont: font \"%s\" with same name found, second ignored\n", name);
75  return false;
76  }
77 
78  if (numFonts >= MAX_FONTS) {
79  Com_Printf("UI_ParseFont: Max fonts reached\n");
80  return false;
81  }
82 
83  /* initialize the UI */
84  uiFont_t* font = &fonts[numFonts];
85  OBJZERO(*font);
86 
87  font->name = Mem_PoolStrDup(name, ui_sysPool, 0);
88 
89  Com_DPrintf(DEBUG_CLIENT, "...found font %s (%i)\n", font->name, numFonts);
90 
91  /* get it's body */
92  const char* token = Com_Parse(text);
93 
94  if (!*text || *token != '{') {
95  Com_Printf("UI_ParseFont: font \"%s\" without body ignored\n", name);
96  return false;
97  }
98 
99  numFonts++;
100 
101  do {
102  /* get the name type */
103  token = Com_EParse(text, errhead, name);
104  if (!*text)
105  return false;
106  if (*token == '}')
107  break;
108 
109  const value_t* v;
110  for (v = fontValues; v->string; v++)
111  if (Q_streq(token, v->string)) {
112  /* found a definition */
113  token = Com_EParse(text, errhead, name);
114  if (!*text)
115  return false;
116 
117  switch (v->type) {
119  /* Translation strings begin with _, skip that and process like regular strings */
120  token++;
121  /* fall through */
122  case V_HUNK_STRING:
123  Mem_PoolStrDupTo(token, &Com_GetValue<char*>(font, v), ui_sysPool, 0);
124  break;
125  default:
126  Com_EParseValue(font, token, v->type, v->ofs, v->size);
127  break;
128  }
129  break;
130  }
131 
132  if (!v->string)
133  Com_Printf("UI_ParseFont: unknown token \"%s\" ignored (font %s)\n", token, name);
134  } while (*text);
135 
136  UI_RegisterFont(font);
137  return true;
138 }
139 
145 const char* UI_GetFontFromNode (const uiNode_t* const node)
146 {
147  if (node && node->font) {
148  return UI_GetReferenceString(node, node->font);
149  }
150  return "f_small";
151 }
152 
153 
157 const uiFont_t* UI_GetFontByID (const char* name)
158 {
159  for (int i = 0; i < numFonts; i++)
160  if (Q_streq(fonts[i].name, name))
161  return &fonts[i];
162 
163  return nullptr;
164 }
165 
166 int UI_FontGetHeight (const char* fontID)
167 {
168  font_t* font = R_GetFont(fontID);
169  if (font != nullptr)
170  return font->height;
171  return -1;
172 }
173 
177 void UI_InitFonts (void)
178 {
179  Com_Printf("...registering %i fonts\n", numFonts);
180  for (int i = 0; i < numFonts; i++)
181  UI_RegisterFont(&fonts[i]);
182 }
183 
184 void UI_FontShutdown (void)
185 {
186  numFonts = 0;
187 }
int FS_CheckFile(const char *fmt,...)
Just returns the filelength and -1 if the file wasn't found.
Definition: files.cpp:298
int size
Definition: ui_font.h:31
#define _(String)
Definition: cl_shared.h:43
static int numFonts
Definition: ui_font.cpp:35
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void UI_FontShutdown(void)
Definition: ui_font.cpp:184
valueTypes_t type
Definition: scripts.h:170
#define ERR_FATAL
Definition: common.h:210
#define Mem_PoolStrDupTo(in, out, pool, tagNum)
Definition: mem.h:49
Internal data use by the UI package.
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
font_t * R_GetFont(const char *name)
Searches the array of available fonts (see fonts.ufo)
Definition: r_font.cpp:210
#define DEBUG_CLIENT
Definition: defines.h:59
GLsizei size
Definition: r_gl.h:152
char * font
Definition: ui_nodes.h:122
#define OBJZERO(obj)
Definition: shared.h:178
char * path
Definition: ui_font.h:33
const char * Com_EParse(const char **text, const char *errhead, const char *errinfo, char *target, size_t size)
Parsing function that prints an error message when there is no text in the buffer.
Definition: scripts.cpp:277
static void UI_RegisterFont(const uiFont_t *font)
Registers a new TTF font.
Definition: ui_font.cpp:52
static const value_t fontValues[]
Definition: ui_font.cpp:38
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition: common.cpp:398
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
const uiFont_t * UI_GetFontByID(const char *name)
Return the font for a specific id.
Definition: ui_font.cpp:157
Definition: r_font.h:29
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
char * style
Definition: ui_font.h:32
const char * string
Definition: scripts.h:169
size_t ofs
Definition: scripts.h:171
int Com_EParseValue(void *base, const char *token, valueTypes_t type, int ofs, size_t size)
Definition: scripts.cpp:978
const char * Com_Parse(const char *data_p[], char *target, size_t size, bool replaceWhitespaces)
Parse a token out of a string.
Definition: parse.cpp:107
Definition: scripts.h:49
char * name
Definition: ui_font.h:30
QGL_EXTERN GLint i
Definition: r_gl.h:113
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition: ui_font.cpp:145
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
int height
Definition: r_font.h:36
int UI_FontGetHeight(const char *fontID)
Definition: ui_font.cpp:166
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition: scripts.h:34
size_t size
Definition: scripts.h:172
static uiFont_t fonts[MAX_FONTS]
Definition: ui_font.cpp:36
Definition: scripts.h:52
void R_FontRegister(const char *name, int size, const char *path, const char *style)
Definition: r_font.cpp:750
memPool_t * ui_sysPool
Definition: ui_main.cpp:42
#define Q_streq(a, b)
Definition: shared.h:136
#define Mem_PoolStrDup(in, pool, tagNum)
Definition: mem.h:50
#define MAX_FONTS
Definition: ui_font.cpp:34
QGL_EXTERN int GLboolean GLfloat * v
Definition: r_gl.h:120
bool UI_ParseFont(const char *name, const char **text)
Definition: ui_font.cpp:68
void UI_InitFonts(void)
after a video restart we have to reinitialize the fonts
Definition: ui_font.cpp:177