SharedFontState: Add 'defaultFontFamily' property

Some scripts may end up ignoring RGSS-side defaults and just
requesting invalid / empty family names; in this case
the internal default family will be substituted.
This commit is contained in:
Ancurio 2021-09-28 10:37:09 +02:00
parent 3d6b7d3b75
commit 5e3907c7f8
3 changed files with 32 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include "debugwriter.h"
#include "graphics.h"
#include "audio.h"
#include "font.h"
#include "boost-hash.h"
#include "steamshim/steamshim_child.h"
@ -362,6 +363,18 @@ RB_METHOD(_steamAchievementUnlock)
return Qnil;
}
RB_METHOD(_mkxp_set_default_font_family)
{
RB_UNUSED_PARAM;
const char *family;
rb_get_args(argc, argv, "z", &family RB_ARG_END);
shState->fontState().setDefaultFontFamily(family);
return Qnil;
}
static VALUE newStringUTF8(const char *string, long length)
{
return rb_enc_str_new(string, length, rb_utf8_encoding());
@ -643,6 +656,8 @@ static void mriBindingExecute()
_rb_define_module_function(rb_mKernel, "_steam_achievement_unlock",
_steamAchievementUnlock);
_rb_define_module_function(rb_mKernel, "_mkxp_set_default_font_family",
_mkxp_set_default_font_family);
std::string &customScript = conf.customScript;
if (!customScript.empty())

View File

@ -27,6 +27,7 @@
#include "boost-hash.h"
#include "util.h"
#include "config.h"
#include "debugwriter.h"
#include <string>
#include <utility>
@ -77,6 +78,10 @@ struct SharedFontStatePrivate
/* Pool of already opened fonts; once opened, they are reused
* and never closed until the termination of the program */
BoostHash<FontKey, TTF_Font*> pool;
/* Internal default font family that is used anytime an
* empty/invalid family is requested */
std::string defaultFamily;
};
SharedFontState::SharedFontState(const Config &conf)
@ -132,6 +137,11 @@ void SharedFontState::initFontSetCB(SDL_RWops &ops,
_TTF_Font *SharedFontState::getFont(std::string family,
int size)
{
if (family.empty()) {
Debug() << "Invalid font family" << family << "requested, substituting internal default";
family = p->defaultFamily;
}
/* Check for substitutions */
if (p->subs.contains(family))
family = p->subs[family];
@ -202,6 +212,11 @@ _TTF_Font *SharedFontState::openBundled(int size)
return TTF_OpenFontRW(ops, 1, size);
}
void SharedFontState::setDefaultFontFamily(const std::string &family)
{
p->defaultFamily = family;
}
void pickExistingFontName(const std::vector<std::string> &names,
std::string &out,
const SharedFontState &sfs)

View File

@ -54,6 +54,8 @@ public:
static _TTF_Font *openBundled(int size);
void setDefaultFontFamily(const std::string &family);
private:
SharedFontStatePrivate *p;
};