1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 11:52:40 +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()) { if (!message.empty()) {
const auto& text = 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.clear<ScreenTextType::HighPriority>();
engine->state->text.addText<ScreenTextType::HighPriority>( engine->state->text.addText<ScreenTextType::HighPriority>(

View File

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

View File

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

View File

@ -5378,7 +5378,6 @@ void opcode_01e3(const ScriptArguments& args, const ScriptString gxtEntry, const
auto str = auto str =
ScreenText::format( ScreenText::format(
script::gxt(args, gxtEntry), script::gxt(args, gxtEntry),
FONT_PRICEDOWN,
GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN)); GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN));
args.getState()->text.addText<ScreenTextType::Big>( args.getState()->text.addText<ScreenTextType::Big>(
ScreenTextEntry::makeBig( ScreenTextEntry::makeBig(
@ -9763,7 +9762,6 @@ void opcode_036d(const ScriptArguments& args, const ScriptString gxtEntry, const
auto str = auto str =
ScreenText::format(script::gxt(args, gxtEntry), ScreenText::format(script::gxt(args, gxtEntry),
FONT_PRICEDOWN,
GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN), GameStringUtil::fromString(std::to_string(arg2), FONT_PRICEDOWN),
GameStringUtil::fromString(std::to_string(arg3), 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}, {0xb1, UNICODE_ACUTE_ACCENT},
}; };
const FontMap map_gta3_font0({map_gta3_font_common, map_gta3_font_0_priv}); const FontMap fontmap_gta3_font_common({map_gta3_font_common});
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 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_0_priv}),
FontMap({map_gta3_font_common, map_gta3_font_1_priv}), FontMap({map_gta3_font_common, map_gta3_font_1_priv}),
FontMap({map_gta3_font_common, map_gta3_font_2_priv}), FontMap({map_gta3_font_common, map_gta3_font_2_priv}),

View File

@ -6,23 +6,13 @@
#include <array> #include <array>
/** /**
* Font mapping of font style 0: Pager * Commong font mapping of all fonts.
*/ */
extern const FontMap map_gta3_font0; extern const FontMap fontmap_gta3_font_common;
/**
* 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;
/** /**
* Array of all font mappings. * Array of all font mappings.
*/ */
extern const std::array<FontMap, 3> maps_gta3_font; extern const std::array<FontMap, 3> fontmaps_gta3_font;
#endif #endif

View File

@ -7,9 +7,13 @@
GameString GameStringUtil::fromString(const std::string& str, font_t font) { GameString GameStringUtil::fromString(const std::string& str, font_t font) {
RW_ASSERT(font < FONTS_COUNT); 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) { 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 { 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 * Encoding of GameStrings depends on the font. Unknown chars are converted to a "unknown GameStringChar" (such as '?').
* well
*/ */
GameString fromString(const std::string& str, font_t font); 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 * Encoding of GameStrings depends on the font. Unknown GameStringChar's are converted to a UNICODE_REPLACEMENT_CHARACTER utf-8 sequence.
* well
*/ */
std::string toString(const GameString& str, font_t font); 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) { 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'); auto c = fontmap.to_GameStringChar('x');
BOOST_CHECK_EQUAL(c, GameStringChar('x')); BOOST_CHECK_EQUAL(c, GameStringChar('x'));
auto u = fontmap.to_unicode('x'); auto u = fontmap.to_unicode('x');
@ -188,7 +188,7 @@ BOOST_AUTO_TEST_CASE(GameStringChar_simple) {
BOOST_AUTO_TEST_CASE(GameString_simple) { BOOST_AUTO_TEST_CASE(GameString_simple) {
std::string s = "Hello world"; 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); auto gs = fontmap.to_GameString(s);
BOOST_CHECK_EQUAL(s.size(), gs.length()); BOOST_CHECK_EQUAL(s.size(), gs.length());
for (size_t i = 0; i < s.size(); ++i) { 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 codeStr2 = T("~1~Hello ~1~ world~1~");
const auto codeStr3 = T("Hello 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")); 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~")); 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")); 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")); BOOST_CHECK_EQUAL(f4, T("Hello worldx"));
} }