Allow using multiple ttf files with separate styles of same family #220
41
src/font.cpp
41
src/font.cpp
|
@ -49,21 +49,13 @@ BUNDLED_FONT_DECL(liberation)
|
||||||
#define BNDL_F_L(f) BUNDLED_FONT_L(f)
|
#define BNDL_F_L(f) BUNDLED_FONT_L(f)
|
||||||
|
|
||||||
typedef std::pair<std::string, int> FontKey;
|
typedef std::pair<std::string, int> FontKey;
|
||||||
|
typedef BoostHash<int, std::string> FontSet;
|
||||||
|
|
||||||
static SDL_RWops *openBundledFont()
|
static SDL_RWops *openBundledFont()
|
||||||
{
|
{
|
||||||
return SDL_RWFromConstMem(BNDL_F_D(BUNDLED_FONT), BNDL_F_L(BUNDLED_FONT));
|
return SDL_RWFromConstMem(BNDL_F_D(BUNDLED_FONT), BNDL_F_L(BUNDLED_FONT));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FontSet
|
|
||||||
{
|
|
||||||
/* 'Regular' style */
|
|
||||||
std::string regular;
|
|
||||||
|
|
||||||
/* Any other styles (used in case no 'Regular' exists) */
|
|
||||||
std::string other;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SharedFontStatePrivate
|
struct SharedFontStatePrivate
|
||||||
{
|
{
|
||||||
/* Maps: font family name, To: substituted family name,
|
/* Maps: font family name, To: substituted family name,
|
||||||
|
@ -117,20 +109,18 @@ void SharedFontState::initFontSetCB(SDL_RWops &ops,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string family = TTF_FontFaceFamilyName(font);
|
std::string family = TTF_FontFaceFamilyName(font);
|
||||||
std::string style = TTF_FontFaceStyleName(font);
|
int style = TTF_GetFontStyle(font);
|
||||||
|
|
||||||
TTF_CloseFont(font);
|
TTF_CloseFont(font);
|
||||||
|
|
||||||
FontSet &set = p->sets[family];
|
FontSet &set = p->sets[family];
|
||||||
|
|
||||||
if (style == "Regular")
|
set[style] = filename;
|
||||||
set.regular = filename;
|
|
||||||
else
|
|
||||||
set.other = filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_TTF_Font *SharedFontState::getFont(std::string family,
|
_TTF_Font *SharedFontState::getFont(std::string family,
|
||||||
int size)
|
int size,
|
||||||
|
int style)
|
||||||
{
|
{
|
||||||
/* Check for substitutions */
|
/* Check for substitutions */
|
||||||
if (p->subs.contains(family))
|
if (p->subs.contains(family))
|
||||||
|
@ -139,7 +129,7 @@ _TTF_Font *SharedFontState::getFont(std::string family,
|
||||||
/* Find out if the font asset exists */
|
/* Find out if the font asset exists */
|
||||||
const FontSet &req = p->sets[family];
|
const FontSet &req = p->sets[family];
|
||||||
|
|
||||||
if (req.regular.empty() && req.other.empty())
|
if (req.cbegin() == req.cend())
|
||||||
{
|
{
|
||||||
/* Doesn't exist; use built-in font */
|
/* Doesn't exist; use built-in font */
|
||||||
family = "";
|
family = "";
|
||||||
|
@ -162,10 +152,10 @@ _TTF_Font *SharedFontState::getFont(std::string family,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Use 'other' path as alternative in case
|
/* Use the style path as default with the
|
||||||
* we have no 'regular' styled font asset */
|
* first key we have as alternative */
|
||||||
const char *path = !req.regular.empty()
|
const char *path = req.value(req.contains(style)
|
||||||
? req.regular.c_str() : req.other.c_str();
|
? style : req.cbegin()->first).c_str();
|
||||||
|
|
||||||
ops = SDL_AllocRW();
|
ops = SDL_AllocRW();
|
||||||
shState->fileSystem().openReadRaw(*ops, path, true);
|
shState->fileSystem().openReadRaw(*ops, path, true);
|
||||||
|
@ -192,7 +182,7 @@ bool SharedFontState::fontPresent(std::string family) const
|
||||||
|
|
||||||
const FontSet &set = p->sets[family];
|
const FontSet &set = p->sets[family];
|
||||||
|
|
||||||
return !(set.regular.empty() && set.other.empty());
|
return !(set.cbegin() == set.cend());
|
||||||
}
|
}
|
||||||
|
|
||||||
_TTF_Font *SharedFontState::openBundled(int size)
|
_TTF_Font *SharedFontState::openBundled(int size)
|
||||||
|
@ -443,10 +433,6 @@ void Font::initDefaults(const SharedFontState &sfs)
|
||||||
|
|
||||||
_TTF_Font *Font::getSdlFont()
|
_TTF_Font *Font::getSdlFont()
|
||||||
{
|
{
|
||||||
if (!p->sdlFont)
|
|
||||||
p->sdlFont = shState->fontState().getFont(p->name.c_str(),
|
|
||||||
p->size);
|
|
||||||
|
|
||||||
int style = TTF_STYLE_NORMAL;
|
int style = TTF_STYLE_NORMAL;
|
||||||
|
|
||||||
if (p->bold)
|
if (p->bold)
|
||||||
|
@ -455,6 +441,11 @@ _TTF_Font *Font::getSdlFont()
|
||||||
if (p->italic)
|
if (p->italic)
|
||||||
style |= TTF_STYLE_ITALIC;
|
style |= TTF_STYLE_ITALIC;
|
||||||
|
|
||||||
|
if (!p->sdlFont)
|
||||||
|
p->sdlFont = shState->fontState().getFont(p->name.c_str(),
|
||||||
|
p->size,
|
||||||
|
style);
|
||||||
|
|
||||||
TTF_SetFontStyle(p->sdlFont, style);
|
TTF_SetFontStyle(p->sdlFont, style);
|
||||||
|
|
||||||
return p->sdlFont;
|
return p->sdlFont;
|
||||||
|
|
|
@ -48,7 +48,8 @@ public:
|
||||||
const std::string &filename);
|
const std::string &filename);
|
||||||
|
|
||||||
_TTF_Font *getFont(std::string family,
|
_TTF_Font *getFont(std::string family,
|
||||||
int size);
|
int size,
|
||||||
|
int style);
|
||||||
|
|
||||||
bool fontPresent(std::string family) const;
|
bool fontPresent(std::string family) const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue