a lot of very bad code that but line wrapping works

This commit is contained in:
DeaTh-G 2025-01-19 19:58:49 +01:00
parent 5b41bb061c
commit bddf9efff6
2 changed files with 82 additions and 1 deletions

View file

@ -273,6 +273,79 @@ std::string Truncate(const std::string& input, size_t maxLength, bool useEllipsi
return input; return input;
} }
std::pair<std::string, std::map<std::string, std::string>> RemoveRubyAnnotations(const char* input) {
std::string output;
std::map<std::string, std::string> rubyMap;
std::string currentMain, currentRuby;
size_t idx = 0;
while (input[idx] != '\0')
{
if (input[idx] == '[')
{
idx++;
currentMain.clear();
currentRuby.clear();
while (input[idx] != ':' && input[idx] != ']' && input[idx] != '\0')
{
currentMain += input[idx++];
}
if (input[idx] == ':')
{
idx++;
while (input[idx] != ']' && input[idx] != '\0')
{
currentRuby += input[idx++];
}
}
if (input[idx] == ']')
{
idx++;
}
if (!currentMain.empty() && !currentRuby.empty())
{
rubyMap[currentMain] = currentRuby;
}
output += currentMain;
}
else
{
output += input[idx++];
}
}
return { output, rubyMap };
}
std::string ReAddRubyAnnotations(const std::string& wrappedText, const std::map<std::string, std::string>& rubyMap) {
std::string annotatedText;
size_t idx = 0;
size_t length = wrappedText.length();
while (idx < length) {
bool matched = false;
for (const auto& [mainText, rubyText] : rubyMap)
{
if (wrappedText.substr(idx, mainText.length()) == mainText)
{
annotatedText += "[" + mainText + ":" + rubyText + "]";
idx += mainText.length();
matched = true;
break;
}
}
if (!matched)
{
annotatedText += wrappedText[idx++];
}
}
return annotatedText;
}
std::vector<std::string> Split(const char* strStart, const ImFont *font, float fontSize, float maxWidth) std::vector<std::string> Split(const char* strStart, const ImFont *font, float fontSize, float maxWidth)
{ {
if (!strStart) if (!strStart)
@ -463,10 +536,16 @@ ImVec2 MeasureCentredParagraph(const ImFont* font, float fontSize, float maxWidt
void DrawCentredParagraph(const ImFont* font, float fontSize, float maxWidth, const ImVec2& centre, float lineMargin, const char* text, std::function<void(const char*, ImVec2)> drawMethod) void DrawCentredParagraph(const ImFont* font, float fontSize, float maxWidth, const ImVec2& centre, float lineMargin, const char* text, std::function<void(const char*, ImVec2)> drawMethod)
{ {
auto lines = Split(text, font, fontSize, maxWidth); const auto& input = RemoveRubyAnnotations(text);
auto lines = Split(input.first.c_str(), font, fontSize, maxWidth);
auto paragraphSize = MeasureCentredParagraph(font, fontSize, lineMargin, lines); auto paragraphSize = MeasureCentredParagraph(font, fontSize, lineMargin, lines);
auto offsetY = 0.0f; auto offsetY = 0.0f;
for (auto& line : lines)
{
line = ReAddRubyAnnotations(line, input.second);
}
const auto& annotatedLines = CalculateAnnotatedParagraph(lines); const auto& annotatedLines = CalculateAnnotatedParagraph(lines);
for (const auto& annotatedLine : annotatedLines.lines) for (const auto& annotatedLine : annotatedLines.lines)

View file

@ -57,6 +57,8 @@ void DrawTextWithOutline(const ImFont* font, float fontSize, const ImVec2& pos,
void DrawTextWithShadow(const ImFont* font, float fontSize, const ImVec2& pos, ImU32 colour, const char* text, float offset = 2.0f, float radius = 1.0f, ImU32 shadowColour = IM_COL32(0, 0, 0, 255)); void DrawTextWithShadow(const ImFont* font, float fontSize, const ImVec2& pos, ImU32 colour, const char* text, float offset = 2.0f, float radius = 1.0f, ImU32 shadowColour = IM_COL32(0, 0, 0, 255));
float CalcWidestTextSize(const ImFont* font, float fontSize, std::span<std::string> strs); float CalcWidestTextSize(const ImFont* font, float fontSize, std::span<std::string> strs);
std::string Truncate(const std::string& input, size_t maxLength, bool useEllipsis = true, bool usePrefixEllipsis = false); std::string Truncate(const std::string& input, size_t maxLength, bool useEllipsis = true, bool usePrefixEllipsis = false);
std::pair<std::string, std::map<std::string, std::string>> RemoveRubyAnnotations(const char* input);
std::string ReAddRubyAnnotations(const std::string& wrappedText, const std::map<std::string, std::string>& rubyMap);
std::vector<std::string> Split(const char* strStart, const ImFont* font, float fontSize, float maxWidth); std::vector<std::string> Split(const char* strStart, const ImFont* font, float fontSize, float maxWidth);
Paragraph CalculateAnnotatedParagraph(const std::vector<std::string>& lines); Paragraph CalculateAnnotatedParagraph(const std::vector<std::string>& lines);
std::vector<std::string> RemoveAnnotationFromParagraph(const std::vector<std::string>& lines); std::vector<std::string> RemoveAnnotationFromParagraph(const std::vector<std::string>& lines);