Skip to content

Commit 52f1b99

Browse files
committed
Allow using multiple ttf files with separate styles
This is done by keeping a BoostHash of all the styles we have and their corresponding paths, and passing the style when we want to get the font.
1 parent 9dcfb66 commit 52f1b99

2 files changed

Lines changed: 18 additions & 26 deletions

File tree

src/font.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,13 @@ BUNDLED_FONT_DECL(liberation)
4949
#define BNDL_F_L(f) BUNDLED_FONT_L(f)
5050

5151
typedef std::pair<std::string, int> FontKey;
52+
typedef BoostHash<int, std::string> FontSet;
5253

5354
static SDL_RWops *openBundledFont()
5455
{
5556
return SDL_RWFromConstMem(BNDL_F_D(BUNDLED_FONT), BNDL_F_L(BUNDLED_FONT));
5657
}
5758

58-
struct FontSet
59-
{
60-
/* 'Regular' style */
61-
std::string regular;
62-
63-
/* Any other styles (used in case no 'Regular' exists) */
64-
std::string other;
65-
};
66-
6759
struct SharedFontStatePrivate
6860
{
6961
/* Maps: font family name, To: substituted family name,
@@ -117,20 +109,18 @@ void SharedFontState::initFontSetCB(SDL_RWops &ops,
117109
return;
118110

119111
std::string family = TTF_FontFaceFamilyName(font);
120-
std::string style = TTF_FontFaceStyleName(font);
112+
int style = TTF_GetFontStyle(font);
121113

122114
TTF_CloseFont(font);
123115

124116
FontSet &set = p->sets[family];
125117

126-
if (style == "Regular")
127-
set.regular = filename;
128-
else
129-
set.other = filename;
118+
set[style] = filename;
130119
}
131120

132121
_TTF_Font *SharedFontState::getFont(std::string family,
133-
int size)
122+
int size,
123+
int style)
134124
{
135125
/* Check for substitutions */
136126
if (p->subs.contains(family))
@@ -139,7 +129,7 @@ _TTF_Font *SharedFontState::getFont(std::string family,
139129
/* Find out if the font asset exists */
140130
const FontSet &req = p->sets[family];
141131

142-
if (req.regular.empty() && req.other.empty())
132+
if (req.cbegin() == req.cend())
143133
{
144134
/* Doesn't exist; use built-in font */
145135
family = "";
@@ -162,10 +152,10 @@ _TTF_Font *SharedFontState::getFont(std::string family,
162152
}
163153
else
164154
{
165-
/* Use 'other' path as alternative in case
166-
* we have no 'regular' styled font asset */
167-
const char *path = !req.regular.empty()
168-
? req.regular.c_str() : req.other.c_str();
155+
/* Use the style path as default with the
156+
* first key we have as alternative */
157+
const char *path = req.value(req.contains(style)
158+
? style : req.cbegin()->first).c_str();
169159

170160
ops = SDL_AllocRW();
171161
shState->fileSystem().openReadRaw(*ops, path, true);
@@ -192,7 +182,7 @@ bool SharedFontState::fontPresent(std::string family) const
192182

193183
const FontSet &set = p->sets[family];
194184

195-
return !(set.regular.empty() && set.other.empty());
185+
return !(set.cbegin() == set.cend());
196186
}
197187

198188
_TTF_Font *SharedFontState::openBundled(int size)
@@ -443,10 +433,6 @@ void Font::initDefaults(const SharedFontState &sfs)
443433

444434
_TTF_Font *Font::getSdlFont()
445435
{
446-
if (!p->sdlFont)
447-
p->sdlFont = shState->fontState().getFont(p->name.c_str(),
448-
p->size);
449-
450436
int style = TTF_STYLE_NORMAL;
451437

452438
if (p->bold)
@@ -455,6 +441,11 @@ _TTF_Font *Font::getSdlFont()
455441
if (p->italic)
456442
style |= TTF_STYLE_ITALIC;
457443

444+
if (!p->sdlFont)
445+
p->sdlFont = shState->fontState().getFont(p->name.c_str(),
446+
p->size,
447+
style);
448+
458449
TTF_SetFontStyle(p->sdlFont, style);
459450

460451
return p->sdlFont;

src/font.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class SharedFontState
4848
const std::string &filename);
4949

5050
_TTF_Font *getFont(std::string family,
51-
int size);
51+
int size,
52+
int style);
5253

5354
bool fontPresent(std::string family) const;
5455

0 commit comments

Comments
 (0)