@@ -49,21 +49,13 @@ BUNDLED_FONT_DECL(liberation)
4949#define BNDL_F_L (f ) BUNDLED_FONT_L(f)
5050
5151typedef std::pair<std::string, int > FontKey;
52+ typedef BoostHash<int , std::string> FontSet;
5253
5354static 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-
6759struct 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 ;
0 commit comments