1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

rwlib+rwengine: add string conversion independent of font

This commit is contained in:
Anonymous Maarten 2018-08-05 23:19:02 +02:00
parent 6d4b69b742
commit 37fcebee40
10 changed files with 31 additions and 37 deletions

View File

@ -86,7 +86,7 @@ void Payphone::tick(float dt) {
if (!message.empty()) {
const auto& text =
ScreenText::format(engine->data->texts.text(message), FONT_ARIAL);
ScreenText::format(engine->data->texts.text(message));
engine->state->text.clear<ScreenTextType::HighPriority>();
engine->state->text.addText<ScreenTextType::HighPriority>(

View File

@ -143,8 +143,8 @@ public:
}
template <class... Args>
static GameString format(GameString format, font_t font, Args&&... args) {
static auto kReplacementMarker = GameStringUtil::fromString("~1~", font);
static GameString format(GameString format, Args&&... args) {
static auto kReplacementMarker = GameStringUtil::fromStringCommon("~1~");
const std::array<GameString, sizeof...(args)> vals = {{args...}};
size_t x = 0, val = 0;
// We're only looking for numerical replacement markers

View File

@ -404,7 +404,6 @@ bool CollectablePickup::onPlayerTouch() {
auto text = ScreenText::format(
engine->data->texts.text(gxtEntry),
FONT_PRICEDOWN,
GameStringUtil::fromString(
std::to_string(state->playerInfo.hiddenPackagesCollected), FONT_PRICEDOWN),
GameStringUtil::fromString(

View File

@ -5378,7 +5378,6 @@ void opcode_01e3(const ScriptArguments& args, const ScriptString gxtEntry, const
auto str =
ScreenText::format(
script::gxt(args, gxtEntry),
FONT_PRICEDOWN,
GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN));
args.getState()->text.addText<ScreenTextType::Big>(
ScreenTextEntry::makeBig(
@ -9763,7 +9762,6 @@ void opcode_036d(const ScriptArguments& args, const ScriptString gxtEntry, const
auto str =
ScreenText::format(script::gxt(args, gxtEntry),
FONT_PRICEDOWN,
GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN),
GameStringUtil::fromString(std::to_string(arg3), FONT_PRICEDOWN));

View File

@ -197,11 +197,9 @@ static const FontMap::gschar_unicode_map_t map_gta3_font_2_priv = {
{0xb1, UNICODE_ACUTE_ACCENT},
};
const FontMap map_gta3_font0({map_gta3_font_common, map_gta3_font_0_priv});
const FontMap map_gta3_font1({map_gta3_font_common, map_gta3_font_1_priv});
const FontMap map_gta3_font2({map_gta3_font_common, map_gta3_font_2_priv});
const FontMap fontmap_gta3_font_common({map_gta3_font_common});
const std::array<FontMap, 3> maps_gta3_font = {
const std::array<FontMap, 3> fontmaps_gta3_font = {
FontMap({map_gta3_font_common, map_gta3_font_0_priv}),
FontMap({map_gta3_font_common, map_gta3_font_1_priv}),
FontMap({map_gta3_font_common, map_gta3_font_2_priv}),

View File

@ -6,23 +6,13 @@
#include <array>
/**
* Font mapping of font style 0: Pager
* Commong font mapping of all fonts.
*/
extern const FontMap map_gta3_font0;
/**
* Font mapping of font style 1: Pricedown
*/
extern const FontMap map_gta3_font1;
/**
* Font mapping of font style 2: Arial
*/
extern const FontMap map_gta3_font2;
extern const FontMap fontmap_gta3_font_common;
/**
* Array of all font mappings.
*/
extern const std::array<FontMap, 3> maps_gta3_font;
extern const std::array<FontMap, 3> fontmaps_gta3_font;
#endif

View File

@ -7,9 +7,13 @@
GameString GameStringUtil::fromString(const std::string& str, font_t font) {
RW_ASSERT(font < FONTS_COUNT);
return maps_gta3_font[font].to_GameString(str);
return fontmaps_gta3_font[font].to_GameString(str);
}
GameString GameStringUtil::fromStringCommon(const std::string& str) {
return fontmap_gta3_font_common.to_GameString(str);
}
std::string GameStringUtil::toString(const GameString& str, font_t font) {
return maps_gta3_font[font].to_string(str);
return fontmaps_gta3_font[font].to_string(str);
}

View File

@ -36,20 +36,25 @@ static const font_t FONTS_COUNT = 3;
namespace GameStringUtil {
/**
* @brief fromString Converts a string to a GameString
* @brief fromString Converts a std::string to a GameString, depending on the font
*
* Encoding of GameStrings depends on the font, only simple ASCII chars will map
* well
* Encoding of GameStrings depends on the font. Unknown chars are converted to a "unknown GameStringChar" (such as '?').
*/
GameString fromString(const std::string& str, font_t font);
/**
* @brief fromString Converts a string to a GameString
* @brief fromString Converts a GameString to a std::string, depending on the font
*
* Encoding of GameStrings depends on the font, only simple ASCII chars will map
* well
* Encoding of GameStrings depends on the font. Unknown GameStringChar's are converted to a UNICODE_REPLACEMENT_CHARACTER utf-8 sequence.
*/
std::string toString(const GameString& str, font_t font);
/**
* @brief fromString Converts a string to a GameString, independent on the font (only characthers known to all fonts are converted)
*
* Encoding of GameStrings does not depend on the font. Unknown chars are converted to a "unknown GameStringChar" (such as '?').
*/
GameString fromStringCommon(const std::string& str);
}
/**

View File

@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(utf8_iterator_ranged_for_loop) {
}
BOOST_AUTO_TEST_CASE(GameStringChar_simple) {
for (const auto &fontmap : maps_gta3_font) {
for (const auto &fontmap : fontmaps_gta3_font) {
auto c = fontmap.to_GameStringChar('x');
BOOST_CHECK_EQUAL(c, GameStringChar('x'));
auto u = fontmap.to_unicode('x');
@ -188,7 +188,7 @@ BOOST_AUTO_TEST_CASE(GameStringChar_simple) {
BOOST_AUTO_TEST_CASE(GameString_simple) {
std::string s = "Hello world";
for (const auto &fontmap : maps_gta3_font) {
for (const auto &fontmap : fontmaps_gta3_font) {
auto gs = fontmap.to_GameString(s);
BOOST_CHECK_EQUAL(s.size(), gs.length());
for (size_t i = 0; i < s.size(); ++i) {

View File

@ -109,16 +109,16 @@ BOOST_AUTO_TEST_CASE(format_test) {
const auto codeStr2 = T("~1~Hello ~1~ world~1~");
const auto codeStr3 = T("Hello world~1~");
auto f1 = ScreenText::format(codeStr1, FONT_PRICEDOWN, T("r"));
auto f1 = ScreenText::format(codeStr1, T("r"));
BOOST_CHECK_EQUAL(f1, T("Hello r world"));
auto f2 = ScreenText::format(codeStr2, FONT_PRICEDOWN, T("k"), T("h"));
auto f2 = ScreenText::format(codeStr2, T("k"), T("h"));
BOOST_CHECK_EQUAL(f2, T("kHello h world~1~"));
auto f3 = ScreenText::format(codeStr3, FONT_PRICEDOWN, T("x"));
auto f3 = ScreenText::format(codeStr3, T("x"));
BOOST_CHECK_EQUAL(f3, T("Hello worldx"));
auto f4 = ScreenText::format(codeStr3, FONT_PRICEDOWN, T("x"), T("k"));
auto f4 = ScreenText::format(codeStr3, T("x"), T("k"));
BOOST_CHECK_EQUAL(f4, T("Hello worldx"));
}