mkxp-freebird/binding-mri/font-binding.cpp

329 lines
7.2 KiB
C++
Raw Normal View History

2013-09-01 14:27:21 +00:00
/*
** font-binding.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"
#include "binding-util.h"
#include "binding-types.h"
#include "exception.h"
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.
2014-04-11 11:37:14 +00:00
#include "sharedstate.h"
#include <string.h>
2013-09-01 14:27:21 +00:00
static void
collectStrings(VALUE obj, std::vector<std::string> &out)
{
if (RB_TYPE_P(obj, RUBY_T_STRING))
{
out.push_back(RSTRING_PTR(obj));
}
else if (RB_TYPE_P(obj, RUBY_T_ARRAY))
{
for (long i = 0; i < RARRAY_LEN(obj); ++i)
{
VALUE str = rb_ary_entry(obj, i);
/* Non-string objects are tolerated (ignored) */
if (!RB_TYPE_P(str, RUBY_T_STRING))
continue;
out.push_back(RSTRING_PTR(str));
}
}
}
2013-09-01 14:27:21 +00:00
DEF_TYPE(Font);
RB_METHOD(fontDoesExist)
{
RB_UNUSED_PARAM;
const char *name = 0;
VALUE nameObj;
rb_get_args(argc, argv, "o", &nameObj RB_ARG_END);
if (RB_TYPE_P(nameObj, RUBY_T_STRING))
name = rb_string_value_cstr(&nameObj);
2013-09-01 14:27:21 +00:00
return rb_bool_new(Font::doesExist(name));
}
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.
2014-04-11 11:37:14 +00:00
RB_METHOD(FontSetName);
2013-09-01 14:27:21 +00:00
RB_METHOD(fontInitialize)
{
VALUE namesObj = Qnil;
2013-09-01 14:27:21 +00:00
int size = 0;
rb_get_args(argc, argv, "|oi", &namesObj, &size RB_ARG_END);
Font *f;
2013-09-01 14:27:21 +00:00
if (NIL_P(namesObj))
{
namesObj = rb_iv_get(rb_obj_class(self), "default_name");
f = new Font(0, size);
}
else
{
std::vector<std::string> names;
collectStrings(namesObj, names);
f = new Font(&names, size);
}
/* This is semantically wrong; the new Font object should take
* a dup'ed object here in case of an array. Ditto for the setters.
* However the same bug/behavior exists in all RM versions. */
rb_iv_set(self, "name", namesObj);
2013-09-01 14:27:21 +00:00
setPrivateData(self, f);
2013-09-01 14:27:21 +00:00
/* Wrap property objects */
f->initDynAttribs();
wrapProperty(self, &f->getColor(), "color", ColorType);
2013-09-01 14:27:21 +00:00
if (rgssVer >= 3)
wrapProperty(self, &f->getOutColor(), "out_color", ColorType);
2013-09-01 14:27:21 +00:00
return self;
}
RB_METHOD(fontInitializeCopy)
{
VALUE origObj;
rb_get_args(argc, argv, "o", &origObj RB_ARG_END);
if (!OBJ_INIT_COPY(self, origObj))
return self;
Font *orig = getPrivateData<Font>(origObj);
Font *f = new Font(*orig);
setPrivateData(self, f);
/* Wrap property objects */
f->initDynAttribs();
wrapProperty(self, &f->getColor(), "color", ColorType);
if (rgssVer >= 3)
wrapProperty(self, &f->getOutColor(), "out_color", ColorType);
return self;
}
2013-09-01 14:27:21 +00:00
RB_METHOD(FontGetName)
{
RB_UNUSED_PARAM;
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.
2014-04-11 11:37:14 +00:00
return rb_iv_get(self, "name");
}
2013-09-01 14:27:21 +00:00
RB_METHOD(FontSetName)
{
Font *f = getPrivateData<Font>(self);
rb_check_argc(argc, 1);
std::vector<std::string> namesObj;
collectStrings(argv[0], namesObj);
2013-09-01 14:27:21 +00:00
f->setName(namesObj);
rb_iv_set(self, "name", argv[0]);
2013-09-01 14:27:21 +00:00
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.
2014-04-11 11:37:14 +00:00
return argv[0];
2013-09-01 14:27:21 +00:00
}
template<class C>
static void checkDisposed(VALUE) {}
DEF_PROP_OBJ_VAL(Font, Color, Color, "color")
DEF_PROP_OBJ_VAL(Font, Color, OutColor, "out_color")
2013-09-01 14:27:21 +00:00
DEF_PROP_I(Font, Size)
2013-09-01 14:27:21 +00:00
DEF_PROP_B(Font, Bold)
DEF_PROP_B(Font, Italic)
DEF_PROP_B(Font, Shadow)
DEF_PROP_B(Font, Outline)
2013-09-01 14:27:21 +00:00
#define DEF_KLASS_PROP(Klass, type, PropName, param_t_s, value_fun) \
RB_METHOD(Klass##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
return value_fun(Klass::get##PropName()); \
} \
RB_METHOD(Klass##Set##PropName) \
{ \
RB_UNUSED_PARAM; \
type value; \
rb_get_args(argc, argv, param_t_s, &value RB_ARG_END); \
2013-09-01 14:27:21 +00:00
Klass::set##PropName(value); \
return value_fun(value); \
}
DEF_KLASS_PROP(Font, int, DefaultSize, "i", rb_fix_new)
DEF_KLASS_PROP(Font, bool, DefaultBold, "b", rb_bool_new)
DEF_KLASS_PROP(Font, bool, DefaultItalic, "b", rb_bool_new)
DEF_KLASS_PROP(Font, bool, DefaultShadow, "b", rb_bool_new)
DEF_KLASS_PROP(Font, bool, DefaultOutline, "b", rb_bool_new)
RB_METHOD(FontGetDefaultOutColor)
{
RB_UNUSED_PARAM;
return rb_iv_get(self, "default_out_color");
}
RB_METHOD(FontSetDefaultOutColor)
{
RB_UNUSED_PARAM;
VALUE colorObj;
rb_get_args(argc, argv, "o", &colorObj RB_ARG_END);
Color *c = getPrivateDataCheck<Color>(colorObj, ColorType);
Font::setDefaultOutColor(*c);
return colorObj;
}
2013-09-01 14:27:21 +00:00
RB_METHOD(FontGetDefaultName)
{
RB_UNUSED_PARAM;
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.
2014-04-11 11:37:14 +00:00
return rb_iv_get(self, "default_name");
2013-09-01 14:27:21 +00:00
}
RB_METHOD(FontSetDefaultName)
{
RB_UNUSED_PARAM;
2013-09-01 14:27:21 +00:00
rb_check_argc(argc, 1);
std::vector<std::string> namesObj;
collectStrings(argv[0], namesObj);
Font::setDefaultName(namesObj, shState->fontState());
rb_iv_set(self, "default_name", argv[0]);
2013-09-01 14:27:21 +00:00
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.
2014-04-11 11:37:14 +00:00
return argv[0];
2013-09-01 14:27:21 +00:00
}
RB_METHOD(FontGetDefaultColor)
{
RB_UNUSED_PARAM;
return rb_iv_get(self, "default_color");
}
RB_METHOD(FontSetDefaultColor)
{
RB_UNUSED_PARAM;
2013-09-01 14:27:21 +00:00
VALUE colorObj;
rb_get_args(argc, argv, "o", &colorObj RB_ARG_END);
2013-09-01 14:27:21 +00:00
Color *c = getPrivateDataCheck<Color>(colorObj, ColorType);
Font::setDefaultColor(*c);
2013-09-01 14:27:21 +00:00
return colorObj;
}
#define INIT_KLASS_PROP_BIND(Klass, PropName, prop_name_s) \
{ \
rb_define_class_method(klass, prop_name_s, Klass##Get##PropName); \
rb_define_class_method(klass, prop_name_s "=", Klass##Set##PropName); \
}
void
fontBindingInit()
{
VALUE klass = rb_define_class("Font", rb_cObject);
rb_define_alloc_func(klass, classAllocate<&FontType>);
2013-09-01 14:27:21 +00:00
Font::initDefaultDynAttribs();
wrapProperty(klass, &Font::getDefaultColor(), "default_color", ColorType);
/* Initialize default names */
const std::vector<std::string> &defNames = Font::getInitialDefaultNames();
VALUE defNamesObj;
if (defNames.size() == 1)
{
defNamesObj = rb_str_new_cstr(defNames[0].c_str());
}
else
{
defNamesObj = rb_ary_new2(defNames.size());
for (size_t i = 0; i < defNames.size(); ++i)
rb_ary_push(defNamesObj, rb_str_new_cstr(defNames[i].c_str()));
}
rb_iv_set(klass, "default_name", defNamesObj);
2013-09-01 14:27:21 +00:00
if (rgssVer >= 3)
wrapProperty(klass, &Font::getDefaultOutColor(), "default_out_color", ColorType);
2013-09-01 14:27:21 +00:00
INIT_KLASS_PROP_BIND(Font, DefaultName, "default_name");
INIT_KLASS_PROP_BIND(Font, DefaultSize, "default_size");
INIT_KLASS_PROP_BIND(Font, DefaultBold, "default_bold");
INIT_KLASS_PROP_BIND(Font, DefaultItalic, "default_italic");
INIT_KLASS_PROP_BIND(Font, DefaultColor, "default_color");
if (rgssVer >= 2)
{
INIT_KLASS_PROP_BIND(Font, DefaultShadow, "default_shadow");
}
if (rgssVer >= 3)
{
INIT_KLASS_PROP_BIND(Font, DefaultOutline, "default_outline");
INIT_KLASS_PROP_BIND(Font, DefaultOutColor, "default_out_color");
}
2013-09-01 14:27:21 +00:00
rb_define_class_method(klass, "exist?", fontDoesExist);
_rb_define_method(klass, "initialize", fontInitialize);
_rb_define_method(klass, "initialize_copy", fontInitializeCopy);
2013-09-01 14:27:21 +00:00
INIT_PROP_BIND(Font, Name, "name");
INIT_PROP_BIND(Font, Size, "size");
INIT_PROP_BIND(Font, Bold, "bold");
INIT_PROP_BIND(Font, Italic, "italic");
INIT_PROP_BIND(Font, Color, "color");
if (rgssVer >= 2)
{
INIT_PROP_BIND(Font, Shadow, "shadow");
}
if (rgssVer >= 3)
{
INIT_PROP_BIND(Font, Outline, "outline");
INIT_PROP_BIND(Font, OutColor, "out_color");
}
2013-09-01 14:27:21 +00:00
}