UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_font.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 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
35static int numFonts = 0;
37
38static 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
52static 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
68bool 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
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:
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
145const 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
157const 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
166int 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
177void UI_InitFonts (void)
178{
179 Com_Printf("...registering %i fonts\n", numFonts);
180 for (int i = 0; i < numFonts; i++)
182}
183
185{
186 numFonts = 0;
187}
void R_FontRegister(const char *name, int size, const char *path, const char *style)
Definition r_font.cpp:744
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition common.cpp:440
void Com_Error(int code, const char *fmt,...)
Definition common.cpp:459
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
#define ERR_FATAL
Definition common.h:210
#define DEBUG_CLIENT
Definition defines.h:59
int FS_CheckFile(const char *fmt,...)
Just returns the filelength and -1 if the file wasn't found.
Definition files.cpp:298
voidpf void uLong size
Definition ioapi.h:42
#define Mem_PoolStrDup(in, pool, tagNum)
Definition mem.h:50
#define Mem_PoolStrDupTo(in, out, pool, tagNum)
Definition mem.h:49
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
Shared parsing functions.
static int numFonts
Definition r_font.cpp:81
static font_t fonts[MAX_FONTS]
Definition r_font.cpp:82
#define MAX_FONTS
Definition r_font.cpp:35
font_t * R_GetFont(const char *name)
Searches the array of available fonts (see fonts.ufo).
Definition r_font.cpp:208
QGL_EXTERN int GLboolean GLfloat * v
Definition r_gl.h:120
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
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
int Com_EParseValue(void *base, const char *token, valueTypes_t type, int ofs, size_t size)
Definition scripts.cpp:964
T & Com_GetValue(void *const object, value_t const *const value)
Definition scripts.h:174
@ V_TRANSLATION_STRING
Definition scripts.h:59
@ V_HUNK_STRING
Definition scripts.h:69
@ V_NULL
Definition scripts.h:49
@ V_INT
Definition scripts.h:52
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition scripts.h:34
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
int height
Definition r_font.h:36
char * style
Definition ui_font.h:32
char * path
Definition ui_font.h:33
int size
Definition ui_font.h:31
char * name
Definition ui_font.h:30
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
char * font
Definition ui_nodes.h:122
void UI_FontShutdown(void)
Definition ui_font.cpp:184
int UI_FontGetHeight(const char *fontID)
Definition ui_font.cpp:166
bool UI_ParseFont(const char *name, const char **text)
Definition ui_font.cpp:68
static void UI_RegisterFont(const uiFont_t *font)
Registers a new TTF font.
Definition ui_font.cpp:52
void UI_InitFonts(void)
after a video restart we have to reinitialize the fonts
Definition ui_font.cpp:177
const uiFont_t * UI_GetFontByID(const char *name)
Return the font for a specific id.
Definition ui_font.cpp:157
static const value_t fontValues[]
Definition ui_font.cpp:38
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition ui_font.cpp:145
Internal data use by the UI package.
memPool_t * ui_sysPool
Definition ui_main.cpp:42
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)