17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 05:09:20 +02:00

Text displays: accept characters above 127, in windows-1250 or utf-8 (#198)

* Text displays: accept characters above 127, in windows-1250 or utf-8

A text display (sub-model of type "text") looks up the sub-model of each character
in a 256 entry table, and indexed it with a plain char. With char being signed, any
character above 127 gave a negative index: reading the text went outside the table,
and so did filling the table if a character sub-model had such a name. In effect the
displays could only show ASCII, and a text with national characters could crash.

The lookup is now keyed by unicode code point. Both the displayed text and the names
of the character sub-models are decoded as utf-8 if they are valid utf-8, and as
windows-1250 otherwise, so legacy (ansi) sceneries and models work next to utf-8
ones, and the two can be mixed. A short ansi text can be valid utf-8 by accident
(two upper case accented letters in a row will do); when both readings are possible
the one which finds more of its characters in the model is used.

The rest is unchanged: the first character of the text still isn't displayed, the
characters are still drawn in the translucent pass only. A missing text pointer no
longer gets dereferenced.

* Text displays: prepare the characters once for each text

The sequence of character sub-models was cached for the last text only, so
displays of several instances drawn in turn decoded their texts again on
every draw. It is now kept for each text the display shows; the texts of the
instances don't change, so a draw costs a single lookup.

Also the comments added in the renderers are now in English.

* Text displays: move the text decoding to utilities

utf8_to_utf32() and win1250_to_utf32(), with the windows-1250 table, now sit
next to win1250_to_utf8(). The existing function maps only the Polish letters
and passes other bytes above 127 through, so it can't serve the displays.

* Text displays: address the SonarCloud findings

std::array instead of the C-style arrays, the utf-8 sequence length worked
out in a function of its own instead of nested conditional operators,
std::byte for the bit operations, std::ranges::count_if and contains().
The character loop of the renderers moved to Render_Alpha_text(), which
takes the nesting back to the limit, and the static pasText is accessed
through the class.
This commit is contained in:
pawelw
2026-10-03 12:05:32 +02:00
committed by GitHub
parent 06fc3915f4
commit 51521a135a
8 changed files with 187 additions and 45 deletions

View File

@@ -17,6 +17,7 @@ Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include "rendering/renderer.h"
#include "utilities/Timer.h"
#include "simulation/simulation.h"
@@ -55,8 +56,6 @@ TSubModel::~TSubModel()
SafeDelete(Child);
delete fMatrix; // własny transform trzeba usunąć (zawsze jeden)
}
delete[] smLetter; // używany tylko roboczo dla TP_TEXT, do przyspieszenia
// wyświetlania
};
void TSubModel::Name_Material(std::string const &Name)
@@ -892,6 +891,67 @@ void TSubModel::InitialRotate(bool doit)
Next->InitialRotate(doit);
};
// text display (TP_TEXT): returns sub-models of the characters to draw for provided text, in drawing order.
// a character is matched with the first character of a child sub-model name. both the text and the names can be
// encoded either in utf-8 or in windows-1250, so the legacy (ansi) scenery and model files work along the utf-8 ones
std::vector<TSubModel *> const &TSubModel::text_letters(std::string const &Text)
{
// the sequence is prepared once for each text the display shows. the texts of the model instances don't change,
// so drawing a display afterwards costs a single lookup, however many instances and passes take turns
auto const prepared{m_textlayouts.find(Text)};
if (prepared != m_textlayouts.end())
{
return prepared->second;
}
std::u32string characters;
if (false == m_lettersready)
{
for (auto *letter = Child; letter != nullptr; letter = letter->Next)
{
if (letter->pName.empty())
{
continue;
}
if (false == utf8_to_utf32(letter->pName, characters))
{
win1250_to_utf32(letter->pName, characters);
}
if (characters.front() != 0)
{
m_letters[characters.front()] = letter;
}
}
m_lettersready = true;
}
// the text is taken for utf-8 if it's valid as such, and for windows-1250 otherwise. a short ansi text can pass
// for utf-8 by accident (two upper case accented letters in a row are enough for this to happen),
// so if both readings are possible the one which finds more of its characters in the model wins
auto const known = [this](std::u32string const &Characters) {
return std::ranges::count_if(Characters, [this](char32_t const Character) { return m_letters.contains(Character); });
};
std::u32string ansi;
win1250_to_utf32(Text, ansi);
if (false == utf8_to_utf32(Text, characters) || known(ansi) > known(characters))
{
characters = ansi;
}
auto &textletters{m_textlayouts[Text]};
// NOTE: the first character of the text isn't displayed. the displays always worked this way, and the existing
// content relies on it
for (std::size_t idx = 1; idx < characters.size(); ++idx)
{
auto const lookup{m_letters.find(characters[idx])};
if (lookup != m_letters.end())
{
textletters.emplace_back(lookup->second);
}
}
return textletters;
}
void TSubModel::ChildAdd(TSubModel *SubModel)
{ // dodanie submodelu potemnego (uzależnionego)
// Ra: zmiana kolejności, żeby kolejne móc renderować po aktualnym (było