2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** font.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "font.h"
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "filesystem.h"
|
|
|
|
#include "exception.h"
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "boost-hash.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-10 10:50:04 +00:00
|
|
|
#include "../liberation.ttf.xxd"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_ttf.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
#define BUNDLED_FONT liberation
|
|
|
|
|
|
|
|
#define BUNDLED_FONT_D(f) assets_## f ##_ttf
|
|
|
|
#define BUNDLED_FONT_L(f) assets_## f ##_ttf_len
|
|
|
|
|
|
|
|
// Go fuck yourself CPP
|
|
|
|
#define BNDL_F_D(f) BUNDLED_FONT_D(f)
|
|
|
|
#define BNDL_F_L(f) BUNDLED_FONT_L(f)
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
typedef std::pair<std::string, int> FontKey;
|
|
|
|
|
|
|
|
static void strToLower(std::string &str)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < str.size(); ++i)
|
|
|
|
str[i] = tolower(str[i]);
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
struct FontPoolPrivate
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
BoostHash<FontKey, TTF_Font*> hash;
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FontPool::FontPool()
|
|
|
|
{
|
|
|
|
p = new FontPoolPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPool::~FontPool()
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
BoostHash<FontKey, TTF_Font*>::const_iterator iter;
|
|
|
|
for (iter = p->hash.cbegin(); iter != p->hash.cend(); ++iter)
|
|
|
|
TTF_CloseFont(iter->second);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_RWops *openBundledFont()
|
|
|
|
{
|
|
|
|
return SDL_RWFromConstMem(BNDL_F_D(BUNDLED_FONT), BNDL_F_L(BUNDLED_FONT));
|
|
|
|
}
|
|
|
|
|
|
|
|
_TTF_Font *FontPool::request(const char *filename,
|
2013-12-11 19:46:54 +00:00
|
|
|
int size)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
// FIXME Find out how font path resolution is done in VX/Ace
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string nameKey(filename);
|
|
|
|
strToLower(nameKey);
|
|
|
|
strReplace(nameKey, ' ', '_');
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
bool useBundled = false;
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string path = std::string("Fonts/") + nameKey;
|
|
|
|
if (!shState->fileSystem().exists(path.c_str(), FileSystem::Font))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
/* Use the same name key for the bundled font
|
|
|
|
* even when it resulted from multiple different
|
|
|
|
* font name requests. The space at the front is
|
|
|
|
* to prevent collisions (spaces are normally
|
|
|
|
* replaced with '_' */
|
2013-09-01 14:27:21 +00:00
|
|
|
useBundled = true;
|
|
|
|
nameKey = " bundled";
|
|
|
|
}
|
|
|
|
|
|
|
|
FontKey key(nameKey, size);
|
|
|
|
|
|
|
|
TTF_Font *font = p->hash.value(key, 0);
|
|
|
|
|
|
|
|
if (font)
|
|
|
|
return font;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
/* Not in hash, open */
|
2013-09-01 14:27:21 +00:00
|
|
|
SDL_RWops *ops;
|
|
|
|
|
|
|
|
if (useBundled)
|
|
|
|
{
|
|
|
|
ops = openBundledFont();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ops = SDL_AllocRW();
|
2013-12-11 19:46:54 +00:00
|
|
|
shState->fileSystem().openRead(*ops, path.c_str(), FileSystem::Font, true);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME 0.9 is guesswork at this point
|
|
|
|
font = TTF_OpenFontRW(ops, 1, (float) size * .90);
|
|
|
|
|
|
|
|
if (!font)
|
|
|
|
throw Exception(Exception::SDLError, "SDL: %s", SDL_GetError());
|
|
|
|
|
|
|
|
p->hash.insert(key, font);
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct FontPrivate
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string name;
|
2013-09-01 14:27:21 +00:00
|
|
|
int size;
|
|
|
|
bool bold;
|
|
|
|
bool italic;
|
|
|
|
Color *color;
|
|
|
|
|
|
|
|
Color colorTmp;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
static std::string defaultName;
|
2013-09-01 14:27:21 +00:00
|
|
|
static int defaultSize;
|
|
|
|
static bool defaultBold;
|
|
|
|
static bool defaultItalic;
|
|
|
|
static Color *defaultColor;
|
|
|
|
|
|
|
|
static Color defaultColorTmp;
|
|
|
|
|
|
|
|
TTF_Font *sdlFont;
|
|
|
|
|
|
|
|
FontPrivate(const char *name = 0,
|
|
|
|
int size = 0)
|
2013-12-11 19:46:54 +00:00
|
|
|
: name(name ? std::string(name) : defaultName),
|
2013-09-01 14:27:21 +00:00
|
|
|
size(size ? size : defaultSize),
|
|
|
|
bold(defaultBold),
|
|
|
|
italic(defaultItalic),
|
|
|
|
color(&colorTmp),
|
|
|
|
colorTmp(*defaultColor)
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
sdlFont = shState->fontPool().request(this->name.c_str(),
|
|
|
|
this->size);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
2013-10-31 09:09:57 +00:00
|
|
|
|
|
|
|
FontPrivate(const FontPrivate &other)
|
|
|
|
: name(other.name),
|
|
|
|
size(other.size),
|
|
|
|
bold(other.bold),
|
|
|
|
italic(other.italic),
|
|
|
|
color(&colorTmp),
|
|
|
|
colorTmp(*other.color),
|
|
|
|
sdlFont(other.sdlFont)
|
|
|
|
{}
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string FontPrivate::defaultName = "Arial";
|
|
|
|
int FontPrivate::defaultSize = 22;
|
|
|
|
bool FontPrivate::defaultBold = false;
|
|
|
|
bool FontPrivate::defaultItalic = false;
|
|
|
|
Color *FontPrivate::defaultColor = &FontPrivate::defaultColorTmp;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Color FontPrivate::defaultColorTmp(255, 255, 255, 255);
|
|
|
|
|
|
|
|
bool Font::doesExist(const char *name)
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string path = std::string("fonts/") + std::string(name);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
return shState->fileSystem().exists(path.c_str(), FileSystem::Font);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::Font(const char *name,
|
|
|
|
int size)
|
|
|
|
{
|
|
|
|
p = new FontPrivate(name, size);
|
|
|
|
}
|
|
|
|
|
2013-10-31 09:09:57 +00:00
|
|
|
Font::Font(const Font &other)
|
|
|
|
{
|
|
|
|
p = new FontPrivate(*other.p);
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
Font::~Font()
|
|
|
|
{
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Font::getName() const
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
return p->name.c_str();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setName(const char *value)
|
|
|
|
{
|
|
|
|
p->name = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setSize(int value)
|
|
|
|
{
|
|
|
|
if (p->size == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p->size = value;
|
2013-12-11 19:46:54 +00:00
|
|
|
p->sdlFont = shState->fontPool().request(p->name.c_str(), value);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHK_DISP
|
|
|
|
#define CHK_DISP
|
|
|
|
|
|
|
|
DEF_ATTR_RD_SIMPLE(Font, Size, int, p->size)
|
|
|
|
DEF_ATTR_SIMPLE(Font, Bold, bool, p->bold)
|
|
|
|
DEF_ATTR_SIMPLE(Font, Italic, bool, p->italic)
|
|
|
|
DEF_ATTR_SIMPLE(Font, Color, Color*, p->color)
|
|
|
|
|
|
|
|
DEF_ATTR_SIMPLE_STATIC(Font, DefaultSize, int, FontPrivate::defaultSize)
|
|
|
|
DEF_ATTR_SIMPLE_STATIC(Font, DefaultBold, bool, FontPrivate::defaultBold)
|
|
|
|
DEF_ATTR_SIMPLE_STATIC(Font, DefaultItalic, bool, FontPrivate::defaultItalic)
|
|
|
|
DEF_ATTR_SIMPLE_STATIC(Font, DefaultColor, Color*, FontPrivate::defaultColor)
|
|
|
|
|
|
|
|
const char *Font::getDefaultName()
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
return FontPrivate::defaultName.c_str();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setDefaultName(const char *value)
|
|
|
|
{
|
|
|
|
FontPrivate::defaultName = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_TTF_Font *Font::getSdlFont()
|
|
|
|
{
|
|
|
|
int style = TTF_STYLE_NORMAL;
|
|
|
|
|
|
|
|
if (p->bold)
|
|
|
|
style |= TTF_STYLE_BOLD;
|
|
|
|
|
|
|
|
if (p->italic)
|
|
|
|
style |= TTF_STYLE_ITALIC;
|
|
|
|
|
|
|
|
TTF_SetFontStyle(p->sdlFont, style);
|
|
|
|
|
|
|
|
return p->sdlFont;
|
|
|
|
}
|