Skip to content

Commit 9611ca1

Browse files
committed
cleanup
1 parent 8edfabf commit 9611ca1

File tree

6 files changed

+38
-52
lines changed

6 files changed

+38
-52
lines changed

doc/lua_api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ Accepted formats are:
279279
images: .png, .jpg, .tga
280280
sounds: .ogg vorbis
281281
models: .x, .b3d, .obj, (since version 5.10:) .gltf, .glb
282-
fonts: .ttf (since version 5.?, see notes below) <!-- TODO before merge -->
282+
fonts: .ttf (since version 5.11, see notes below)
283283

284284
Other formats won't be sent to the client (e.g. you can store .blend files
285285
in a folder for convenience, without the risk that such files are transferred)

src/client/fontengine.cpp

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ void FontEngine::readSettings()
155155
m_default_bold = g_settings->getBool("font_bold");
156156
m_default_italic = g_settings->getBool("font_italic");
157157

158-
clearCache();
159-
updateCache();
160-
updateSkin();
158+
refresh();
161159
}
162160

163161
void FontEngine::updateSkin()
@@ -175,25 +173,25 @@ void FontEngine::updateCache()
175173
getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
176174
}
177175

176+
void FontEngine::refresh() {
177+
clearCache();
178+
updateCache();
179+
updateSkin();
180+
}
181+
178182
void FontEngine::setMediaFont(const std::string &name, const std::string &data)
179183
{
180184
std::string copy = data;
181185
irr_ptr<gui::SGUITTFace> face(gui::SGUITTFace::createFace(std::move(copy)));
182186
m_media_faces.emplace(name, face);
183-
// HACK dedup this
184-
clearCache();
185-
updateCache();
186-
updateSkin();
187+
refresh();
187188
}
188189

189190
void FontEngine::clearMediaFonts()
190191
{
191192
RecursiveMutexAutoLock l(m_font_mutex);
192193
m_media_faces.clear();
193-
// HACK dedup this
194-
clearCache();
195-
updateCache();
196-
updateSkin();
194+
refresh();
197195
}
198196

199197
gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
@@ -246,26 +244,29 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
246244
: (setting_suffix.empty() ? "" : setting_suffix.substr(1));
247245
if (media_name == "") media_name = "regular";
248246

249-
auto it = m_media_faces.find(media_name);
250-
if (it != m_media_faces.end()) {
251-
auto *face = it->second.get();
252-
face->grab();
247+
auto createFont = [&](gui::SGUITTFace *face) -> gui::CGUITTFont* {
253248
auto *font = gui::CGUITTFont::createTTFont(m_env,
254249
face, size, true, true, font_shadow,
255250
font_shadow_alpha);
256-
257-
if (!font) {
258-
errorstream << "FontEngine: Cannot load media font '" << media_name <<
259-
"'. Falling back to client settings." << std::endl;
260-
}
261251

262-
// HACK this tidbit is duplicated
252+
if (!font) return nullptr;
253+
263254
if (spec.mode != _FM_Fallback) {
264255
FontSpec spec2(spec);
265256
spec2.mode = _FM_Fallback;
266257
font->setFallback(getFont(spec2, true));
267258
}
259+
268260
return font;
261+
};
262+
263+
auto it = m_media_faces.find(media_name);
264+
if (it != m_media_faces.end()) {
265+
auto *face = it->second.get();
266+
if (auto *font = createFont(face))
267+
return font;
268+
errorstream << "FontEngine: Cannot load media font '" << media_name <<
269+
"'. Falling back to client settings." << std::endl;
269270
}
270271

271272
for (const std::string &font_path : fallback_settings) {
@@ -274,25 +275,11 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
274275

275276
// Grab the face.
276277
auto *face = irr::gui::SGUITTFace::loadFace(font_path);
277-
gui::CGUITTFont *font = nullptr;
278-
if (face) {
279-
font = gui::CGUITTFont::createTTFont(m_env,
280-
face, size, true, true, font_shadow,
281-
font_shadow_alpha);
282-
}
278+
if (auto *font = face ? createFont(face) : nullptr)
279+
return font;
283280

284-
if (!font) {
285-
errorstream << "FontEngine: Cannot load '" << font_path <<
286-
"'. Trying to fall back to another path." << std::endl;
287-
continue;
288-
}
289-
290-
if (spec.mode != _FM_Fallback) {
291-
FontSpec spec2(spec);
292-
spec2.mode = _FM_Fallback;
293-
font->setFallback(getFont(spec2, true));
294-
}
295-
return font;
281+
errorstream << "FontEngine: Cannot load '" << font_path <<
282+
"'. Trying to fall back to another path." << std::endl;
296283
}
297284
return nullptr;
298285
}

src/client/fontengine.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ class FontEngine
121121
/** update internal parameters from settings */
122122
void readSettings();
123123

124+
/**
125+
* @brief Set a media-provided font.
126+
* Name should currently be one of
127+
* {regular, bold, italic_bold, mono, mono_bold, mono_bold_italic},
128+
* but arbitrary names are to be supported in the future.
129+
*/
124130
void setMediaFont(const std::string &name, const std::string &data);
125131

126132
void clearMediaFonts();
@@ -137,9 +143,11 @@ class FontEngine
137143
/** update current minetest skin with font changes */
138144
void updateSkin();
139145

140-
/** clean cache */
141146
void clearCache();
142147

148+
/** refresh after fonts have been changed */
149+
void refresh();
150+
143151
/** pointer to irrlicht gui environment */
144152
gui::IGUIEnvironment* m_env = nullptr;
145153

src/irrlicht_changes/CGUITTFont.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*/
3333

3434
#include <cstdlib>
35-
#include <freetype/freetype.h>
3635
#include <iostream>
3736
#include <optional>
3837
#include "irr_ptr.h"
@@ -261,8 +260,6 @@ void SGUITTGlyph::unload()
261260

262261
//////////////////////
263262

264-
// TODO constructor which takes bogus filename and data
265-
266263
CGUITTFont* CGUITTFont::createTTFont(IGUIEnvironment *env,
267264
SGUITTFace *face, u32 size, bool antialias,
268265
bool transparency, u32 shadow, u32 shadow_alpha)

src/irrlicht_changes/CGUITTFont.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Copyright (c) 2009-2010 John Norman
44
Copyright (c) 2016 Nathanaëlle Courant
55
Copyright (c) 2023 Caleb Butler
6+
Copyright (c) 2024 Luanti contributors
67
78
This software is provided 'as-is', without any express or implied
89
warranty. In no event will the authors be held liable for any
@@ -32,24 +33,18 @@
3233

3334
#pragma once
3435

35-
#include <map>
36-
3736
#include <ft2build.h>
3837
#include FT_FREETYPE_H
3938

4039
#include "IGUIEnvironment.h"
4140
#include "IGUIFont.h"
4241
#include "IVideoDriver.h"
4342
#include "IrrlichtDevice.h"
44-
#include "SMesh.h"
4543
#include "util/enriched_string.h"
4644
#include "util/basic_macros.h"
4745

48-
// TODO these should be in the C++ when the struct is properly split into C++ & header
46+
#include <map>
4947
#include <optional>
50-
#include "irr_ptr.h"
51-
#include "log.h"
52-
#include "filesys.h"
5348

5449
namespace irr
5550
{

src/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2542,7 +2542,6 @@ bool Server::addMediaFile(const std::string &filename,
25422542
// Translation file formats
25432543
".tr", ".po", ".mo",
25442544
// Fonts
2545-
// TODO throw a warning and ignore file if name is not one of the few recognized ones?
25462545
".ttf",
25472546
NULL
25482547
};

0 commit comments

Comments
 (0)