Font: Overhaul font asset discovery

Previously, any font names requested by RGSS would be translated
directly to filenames by lowercasing and replacing spaces with
underscores (and finally doing some extension substitution).
To make this whole thing work smoother as well as get closer to
how font discovery is done in VX, we now scan the "Fonts/" folder
at startup and index all present font assets by their family name;
now, if an "Open Sans" font is present in "Fonts/", it will be
used regardless of filename.

Font assets with "Regular" style are preferred, but in their
absence, mkxp will make use of any other style it can find for
the respective family. This is not the exact same behavior as
VX, but it should cover 95% of use cases.

Previously, one could substitute fonts via filenames, ie. to
substitute "Arial" with "Open Sans", one would just rename
"OpenSans.ttf" to "arial.ttf" and put it in "Fonts/". With the
above change, this is no longer possible. As an alternative, one
can now explicitly specify font family substitutions via mkxp.conf;
eg. for the above case, one would add

fontSub=Arial>Open Sans

to the configuration file. Multiple such rules can be specified.

In the process, I also added the ability to provide
'Font.(default_)name' with an array of font families to search
for the first existing one instead of a plain string.
This makes the behavior closer to RMXP; however, it doesn't
work 100% the same: when a reference to the 'Font.name' array is
held and additional strings are added to it without re-assignig
the array to 'Font.name', those will be ignored.
This commit is contained in:
Jonas Kulla 2014-04-11 13:37:14 +02:00
parent 31626c9acc
commit 1ef6e04520
13 changed files with 443 additions and 185 deletions

View file

@ -52,12 +52,14 @@ RB_METHOD(bitmapInitialize)
setPrivateData(self, b);
/* Wrap properties */
Font *font = new Font();
b->setFont(font);
font->setColor(new Color(*font->getColor()));
VALUE fontKlass = rb_const_get(rb_cObject, rb_intern("Font"));
VALUE fontObj = rb_obj_alloc(fontKlass);
rb_obj_call_init(fontObj, 0, 0);
VALUE fontProp = wrapProperty(self, font, "font", FontType);
wrapProperty(fontProp, font->getColor(), "color", ColorType);
Font *font = getPrivateData<Font>(fontObj);
b->setFont(font);
rb_iv_set(self, "font", fontObj);
return self;
}

View file

@ -23,6 +23,9 @@
#include "binding-util.h"
#include "binding-types.h"
#include "exception.h"
#include "sharedstate.h"
#include <string.h>
DEF_TYPE(Font);
@ -36,14 +39,16 @@ RB_METHOD(fontDoesExist)
return rb_bool_new(Font::doesExist(name));
}
RB_METHOD(FontSetName);
RB_METHOD(fontInitialize)
{
const char *name = 0;
VALUE name = Qnil;
int size = 0;
rb_get_args(argc, argv, "|zi", &name, &size RB_ARG_END);
rb_get_args(argc, argv, "|oi", &name, &size RB_ARG_END);
Font *f = new Font(name, size);
Font *f = new Font(0, size);
setPrivateData(self, f);
@ -51,6 +56,13 @@ RB_METHOD(fontInitialize)
f->setColor(new Color(*f->getColor()));
wrapProperty(self, f->getColor(), "color", ColorType);
if (NIL_P(name))
name = rb_iv_get(rb_obj_class(self), "default_name");
/* Going over the 'name=' function automatically causes
* a possbile name array to be re-verified for existing fonts */
FontSetName(1, &name, self);
return self;
}
@ -77,21 +89,63 @@ RB_METHOD(FontGetName)
{
RB_UNUSED_PARAM;
Font *f = getPrivateData<Font>(self);
return rb_iv_get(self, "name");
}
return rb_str_new_cstr(f->getName());
static void
fontSetNameHelper(VALUE self, int argc, VALUE *argv,
const char *nameIv, char *outBuf, size_t outLen)
{
rb_check_argc(argc, 1);
VALUE arg = argv[0];
int type = rb_type(arg);
// Fixme: in RGSS3, specifying "" (and only that) as font name results in
// no text being drawn (everything else is substituted with Arial I think)
strncpy(outBuf, "", outLen);
if (type == RUBY_T_STRING)
{
strncpy(outBuf, RSTRING_PTR(arg), outLen);
}
else if (type == RUBY_T_ARRAY)
{
for (long i = 0; i < RARRAY_LEN(arg); ++i)
{
VALUE str = rb_ary_entry(arg, i);
/* Non-string objects are tolerated (ignored) */
if (rb_type(str) != RUBY_T_STRING)
continue;
const char *family = RSTRING_PTR(str);
/* We only set the core Font object's name attribute
* to the actually existing font name */
if (!shState->fontState().fontPresent(family))
continue;
strncpy(outBuf, family, outLen);
}
}
/* RMXP doesn't even care if the argument type is
* something other than string/array. Whatever... */
rb_iv_set(self, nameIv, arg);
}
RB_METHOD(FontSetName)
{
Font *f = getPrivateData<Font>(self);
VALUE name;
rb_get_args(argc, argv, "S", &name RB_ARG_END);
char result[256];
fontSetNameHelper(self, argc, argv, "default_name",
result, sizeof(result));
f->setName(RSTRING_PTR(name));
f->setName(result);
return name;
return argv[0];
}
#undef DEF_PROP_CHK_DISP
@ -124,18 +178,19 @@ DEF_KLASS_PROP(Font, bool, DefaultItalic, "b", rb_bool_new)
RB_METHOD(FontGetDefaultName)
{
RB_UNUSED_PARAM;
return rb_str_new_cstr(Font::getDefaultName());
return rb_iv_get(self, "default_name");
}
RB_METHOD(FontSetDefaultName)
{
RB_UNUSED_PARAM;
VALUE nameObj;
rb_get_args(argc, argv, "S", &nameObj RB_ARG_END);
char result[256];
fontSetNameHelper(self, argc, argv, "default_name",
result, sizeof(result));
Font::setDefaultName(RSTRING_PTR(nameObj));
Font::setDefaultName(result);
return nameObj;
return argv[0];
}
RB_METHOD(FontGetDefaultColor)
@ -174,6 +229,7 @@ fontBindingInit()
Font::setDefaultColor(new Color(*Font::getDefaultColor()));
wrapProperty(klass, Font::getDefaultColor(), "default_color", ColorType);
rb_iv_set(klass, "default_name", rb_str_new_cstr(Font::getDefaultName()));
INIT_KLASS_PROP_BIND(Font, DefaultName, "default_name");
INIT_KLASS_PROP_BIND(Font, DefaultSize, "default_size");