From 5e3907c7f8b163e34e2648042babe8ccab20f31f Mon Sep 17 00:00:00 2001 From: Ancurio Date: Tue, 28 Sep 2021 10:37:09 +0200 Subject: [PATCH] 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. --- binding-mri/binding-mri.cpp | 15 +++++++++++++++ src/font.cpp | 15 +++++++++++++++ src/font.h | 2 ++ 3 files changed, 32 insertions(+) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 5a60668..d166771 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -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()) diff --git a/src/font.cpp b/src/font.cpp index 97ebc76..2d2e71d 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -27,6 +27,7 @@ #include "boost-hash.h" #include "util.h" #include "config.h" +#include "debugwriter.h" #include #include @@ -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 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 &names, std::string &out, const SharedFontState &sfs) diff --git a/src/font.h b/src/font.h index 2ddf96b..5341974 100644 --- a/src/font.h +++ b/src/font.h @@ -54,6 +54,8 @@ public: static _TTF_Font *openBundled(int size); + void setDefaultFontFamily(const std::string &family); + private: SharedFontStatePrivate *p; };