From 37fcebee404b96a6f1d133b6246e276fb5ff7bbf Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sun, 5 Aug 2018 23:19:02 +0200 Subject: [PATCH] rwlib+rwengine: add string conversion independent of font --- rwengine/src/engine/Payphone.cpp | 2 +- rwengine/src/engine/ScreenText.hpp | 4 ++-- rwengine/src/objects/PickupObject.cpp | 1 - rwengine/src/script/modules/GTA3ModuleImpl.inl | 2 -- rwlib/source/fonts/FontMapGta3.cpp | 6 ++---- rwlib/source/fonts/FontMapGta3.hpp | 16 +++------------- rwlib/source/fonts/GameTexts.cpp | 8 ++++++-- rwlib/source/fonts/GameTexts.hpp | 17 +++++++++++------ tests/test_StringEncoding.cpp | 4 ++-- tests/test_Text.cpp | 8 ++++---- 10 files changed, 31 insertions(+), 37 deletions(-) diff --git a/rwengine/src/engine/Payphone.cpp b/rwengine/src/engine/Payphone.cpp index 37d157da..f78d194f 100644 --- a/rwengine/src/engine/Payphone.cpp +++ b/rwengine/src/engine/Payphone.cpp @@ -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(); engine->state->text.addText( diff --git a/rwengine/src/engine/ScreenText.hpp b/rwengine/src/engine/ScreenText.hpp index d24a9c80..8afb5c30 100644 --- a/rwengine/src/engine/ScreenText.hpp +++ b/rwengine/src/engine/ScreenText.hpp @@ -143,8 +143,8 @@ public: } template - 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 vals = {{args...}}; size_t x = 0, val = 0; // We're only looking for numerical replacement markers diff --git a/rwengine/src/objects/PickupObject.cpp b/rwengine/src/objects/PickupObject.cpp index e244dd85..23188fee 100644 --- a/rwengine/src/objects/PickupObject.cpp +++ b/rwengine/src/objects/PickupObject.cpp @@ -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( diff --git a/rwengine/src/script/modules/GTA3ModuleImpl.inl b/rwengine/src/script/modules/GTA3ModuleImpl.inl index a3f7bfbc..8276df15 100644 --- a/rwengine/src/script/modules/GTA3ModuleImpl.inl +++ b/rwengine/src/script/modules/GTA3ModuleImpl.inl @@ -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( 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)); diff --git a/rwlib/source/fonts/FontMapGta3.cpp b/rwlib/source/fonts/FontMapGta3.cpp index 382b41de..e3fb7957 100644 --- a/rwlib/source/fonts/FontMapGta3.cpp +++ b/rwlib/source/fonts/FontMapGta3.cpp @@ -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 maps_gta3_font = { +const std::array 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}), diff --git a/rwlib/source/fonts/FontMapGta3.hpp b/rwlib/source/fonts/FontMapGta3.hpp index c18707b1..caaf77fe 100644 --- a/rwlib/source/fonts/FontMapGta3.hpp +++ b/rwlib/source/fonts/FontMapGta3.hpp @@ -6,23 +6,13 @@ #include /** - * 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 maps_gta3_font; +extern const std::array fontmaps_gta3_font; #endif diff --git a/rwlib/source/fonts/GameTexts.cpp b/rwlib/source/fonts/GameTexts.cpp index 89173dea..ef782004 100644 --- a/rwlib/source/fonts/GameTexts.cpp +++ b/rwlib/source/fonts/GameTexts.cpp @@ -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); } diff --git a/rwlib/source/fonts/GameTexts.hpp b/rwlib/source/fonts/GameTexts.hpp index 7ee16ec3..8583e789 100644 --- a/rwlib/source/fonts/GameTexts.hpp +++ b/rwlib/source/fonts/GameTexts.hpp @@ -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); } /** diff --git a/tests/test_StringEncoding.cpp b/tests/test_StringEncoding.cpp index 7fea681b..4de76aea 100644 --- a/tests/test_StringEncoding.cpp +++ b/tests/test_StringEncoding.cpp @@ -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) { diff --git a/tests/test_Text.cpp b/tests/test_Text.cpp index 18fd03c2..062febe5 100644 --- a/tests/test_Text.cpp +++ b/tests/test_Text.cpp @@ -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")); }