diff --git a/CMakeLists.txt b/CMakeLists.txt index f61abfc..1f2ab37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,24 +126,26 @@ if(TARGET SDL2::SDL2main) list(PREPEND BMH_SDL_TARGET SDL2::SDL2main) endif() -# App sources share objects only within identical compile environments. -# MSVC consumers currently differ in target/source /utf-8 flags; keep that path. -set(BMH_APP_SESSION_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/app/career_session.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/app/window_actions.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/app/desktop_actions.cpp") -set(BMH_APP_SESSION_SDL_SOURCE "${BMH_APP_SESSION_SOURCE}") -set(BMH_APP_CONTROLLER_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/app/controller.cpp") -if(NOT MSVC) - add_library(bmh_app_session OBJECT src/app/career_session.cpp src/app/window_actions.cpp src/app/desktop_actions.cpp) - target_link_libraries(bmh_app_session PRIVATE bmhui) - add_library(bmh_app_session_sdl OBJECT src/app/career_session.cpp src/app/window_actions.cpp src/app/desktop_actions.cpp) - target_link_libraries(bmh_app_session_sdl PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET}) - add_library(bmh_app_controller_sdl OBJECT src/app/controller.cpp) - target_link_libraries(bmh_app_controller_sdl PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET}) - set(BMH_APP_SESSION_SOURCE "$") - set(BMH_APP_SESSION_SDL_SOURCE "$") - set(BMH_APP_CONTROLLER_SOURCE "$") +# The app sources (career session, window/desktop actions, controller) are compiled once per +# compile environment and shared as objects by the game and by about forty tests and tools. +# On MSVC they are compiled with /utf-8 like the game itself: their German messages are UTF-8 +# string literals. (Until 1.0.12 the MSVC build compiled these sources again for every +# consumer, which cost most of the build time; controller.cpp then got /utf-8 only from +# targets that set it.) +add_library(bmh_app_session OBJECT src/app/career_session.cpp src/app/window_actions.cpp src/app/desktop_actions.cpp) +target_link_libraries(bmh_app_session PRIVATE bmhui) +add_library(bmh_app_session_sdl OBJECT src/app/career_session.cpp src/app/window_actions.cpp src/app/desktop_actions.cpp) +target_link_libraries(bmh_app_session_sdl PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET}) +add_library(bmh_app_controller_sdl OBJECT src/app/controller.cpp) +target_link_libraries(bmh_app_controller_sdl PRIVATE bmhui openbmh_audio ${BMH_SDL_TARGET}) +if(MSVC) + target_compile_options(bmh_app_session PRIVATE /utf-8) + target_compile_options(bmh_app_session_sdl PRIVATE /utf-8) + target_compile_options(bmh_app_controller_sdl PRIVATE /utf-8) endif() +set(BMH_APP_SESSION_SOURCE "$") +set(BMH_APP_SESSION_SDL_SOURCE "$") +set(BMH_APP_CONTROLLER_SOURCE "$") # (before the first subdirectory: tests added there are only registered once testing is on) enable_testing() @@ -211,6 +213,7 @@ add_library(bmhcore STATIC src/formats/vga.cpp src/formats/font.cpp src/formats/textd.cpp + src/formats/text_encoding.cpp src/formats/fbm.cpp src/formats/le.cpp src/formats/diamond_image.cpp @@ -479,9 +482,6 @@ set_target_properties(openbmh_play PROPERTIES WIN32_EXECUTABLE TRUE OUTPUT_NAME target_include_directories(openbmh_play PRIVATE "${CMAKE_BINARY_DIR}/generated") if(MSVC) target_compile_options(openbmh_play PRIVATE /utf-8) - # App sources also compile in headless tests/helpers without target /utf-8. - set_source_files_properties(src/app/career_session.cpp src/app/window_actions.cpp - src/app/desktop_actions.cpp PROPERTIES COMPILE_OPTIONS /utf-8) endif() # Development helper: drives the career session like the UI, without a window diff --git a/src/formats/text_encoding.cpp b/src/formats/text_encoding.cpp new file mode 100644 index 0000000..85085d49 --- /dev/null +++ b/src/formats/text_encoding.cpp @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Text encoding helpers: see text_encoding.hpp. +#include "formats/text_encoding.hpp" + +#include +#include +#include + +#include "formats/binary.hpp" + +namespace bmh { +namespace { + +// The seven German letters and the ISO 646-DE bytes that stand for them, in the same order. +constexpr std::array german = {"Ä", "Ö", "Ü", "ä", "ö", "ü", "ß"}; +constexpr std::string_view original_german = "[\\]{|}~"; + +} // namespace + +std::string json_quote(std::string_view value) { + std::ostringstream s; s << '"'; + for (unsigned char c : value) { + if (c == '"' || c == '\\') s << '\\' << c; + else if (c < 32) s << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c) << std::dec; + else s << c; + } + s << '"'; return s.str(); +} + +std::string original_to_utf8(std::string_view value) { + std::string out; + for (char c : value) { + auto i = original_german.find(c); + if (i != std::string_view::npos) out += german[i]; + else if (static_cast(c) < 128) out += c; + else out += "�"; // Unsupported original byte remains visibly unknown. + } + return out; +} + +std::string utf8_to_original(std::string_view value) { + std::string out; + for (size_t i = 0; i < value.size();) { + bool found = false; + for (size_t j = 0; j < german.size(); ++j) + if (value.substr(i, german[j].size()) == german[j]) { + out += original_german[j]; i += german[j].size(); found = true; break; + } + if (found) continue; + const unsigned char c = value[i++]; + if (c >= 128) throw FormatError("manager name: unsupported UTF-8 character"); + out += char(c); + } + return out; +} + +} // namespace bmh diff --git a/src/formats/text_encoding.hpp b/src/formats/text_encoding.hpp new file mode 100644 index 0000000..df3b480 --- /dev/null +++ b/src/formats/text_encoding.hpp @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +// Text encoding of the original game and of the JSON the front ends emit. +// +// The original stores German text in ISO 646-DE: the seven bytes [ \ ] { | } ~ stand for +// Ä Ö Ü ä ö ü ß, everything else is ASCII. The engine and the screens keep text in that original +// encoding (the bitmap fonts are indexed by it); UTF-8 is used only where text leaves the program +// (JSON state, console, window titles) or enters it (typed names). +// +// These functions lived in ui/manager.hpp until 1.0.12, although nearly every screen, the console +// client and the app use them; ui/manager.hpp still makes them visible as bmh::ui::... . + +#include +#include + +namespace bmh { + +// A JSON string literal (with the quotes) for UTF-8 text: escapes '"', '\' and control characters. +std::string json_quote(std::string_view utf8); + +// Original (ISO 646-DE) bytes -> UTF-8. A byte above 127 is not part of the original encoding +// and becomes U+FFFD, so that it stays visible. +std::string original_to_utf8(std::string_view original); + +// UTF-8 -> original bytes; throws FormatError for a character the original cannot store. +std::string utf8_to_original(std::string_view utf8); + +} // namespace bmh diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index 11e0c54..63312c0 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -38,48 +38,8 @@ void border(Image& out, int x, int y) { rect(out, x + 38, y - 1, 1, 40, 0x40); } -constexpr std::array german = {"Ä", "Ö", "Ü", "ä", "ö", "ü", "ß"}; -constexpr std::string_view original_german = "[\\]{|}~"; - } // namespace -std::string json_quote(std::string_view value) { - std::ostringstream s; s << '"'; - for (unsigned char c : value) { - if (c == '"' || c == '\\') s << '\\' << c; - else if (c < 32) s << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c) << std::dec; - else s << c; - } - s << '"'; return s.str(); -} - -std::string original_to_utf8(std::string_view value) { - std::string out; - for (char c : value) { - auto i = original_german.find(c); - if (i != std::string_view::npos) out += german[i]; - else if (static_cast(c) < 128) out += c; - else out += "�"; // Unsupported original byte remains visibly unknown. - } - return out; -} - -std::string utf8_to_original(std::string_view value) { - std::string out; - for (size_t i = 0; i < value.size();) { - bool found = false; - for (size_t j = 0; j < german.size(); ++j) - if (value.substr(i, german[j].size()) == german[j]) { - out += original_german[j]; i += german[j].size(); found = true; break; - } - if (found) continue; - const unsigned char c = value[i++]; - if (c >= 128) throw FormatError("manager name: unsupported UTF-8 character"); - out += char(c); - } - return out; -} - SelectionContext load_selection_context(const std::filesystem::path& path, uint64_t revision) { std::ifstream input(path); if (!input) throw FormatError("cannot read selection context: " + path.string()); diff --git a/src/ui/manager.hpp b/src/ui/manager.hpp index cc2842c..b7b89d8 100644 --- a/src/ui/manager.hpp +++ b/src/ui/manager.hpp @@ -13,6 +13,7 @@ #include "formats/font.hpp" #include "formats/le.hpp" #include "formats/m3d.hpp" +#include "formats/text_encoding.hpp" #include "formats/textd.hpp" #include "ui/setup.hpp" @@ -100,10 +101,12 @@ private: int group_ = 0, hover_ = -1; }; -// JSON emits UTF-8; rendering/model preserve the original encoding. -std::string json_quote(std::string_view utf8); -std::string original_to_utf8(std::string_view original); -std::string utf8_to_original(std::string_view utf8); +// JSON emits UTF-8; rendering/model preserve the original encoding. The three functions are +// defined in formats/text_encoding.hpp (namespace bmh); the screens keep using them as +// bmh::ui::json_quote etc. +using ::bmh::json_quote; +using ::bmh::original_to_utf8; +using ::bmh::utf8_to_original; std::string manager_json(const ManagerChoice& choice); } // namespace bmh::ui