Skip to content

Commit

Permalink
Adjust utf-32 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
BLooperZ committed Aug 28, 2024
1 parent 3fcf4eb commit 4ee21d7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Source/engine/render/text_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,9 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
}
};

std::u32string_view remaining32 = ConvertLogicalToVisual(ConvertUtf8ToUtf32(remaining));
remaining = ConvertUtf32ToUtf8(remaining32);
std::u32string remaining32 = ConvertLogicalToVisual(ConvertUtf8ToUtf32(remaining));
std::string line = ConvertUtf32ToUtf8(remaining32);
remaining = line;

for (; !remaining.empty() && remaining[0] != '\0'
&& (next = DecodeFirstUtf8CodePoint(remaining, &cpLen)) != Utf8DecodeError;
Expand Down
5 changes: 3 additions & 2 deletions Source/utils/unicode-bidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace devilution {

std::u32string ConvertLogicalToVisual(std::u32string_view input)
{
FriBidiChar *logical = new FriBidiChar[input.size()];
FriBidiChar *visual = new FriBidiChar[input.size()];
// return std::u32string(input);
FriBidiChar *logical = new FriBidiChar[input.size()];
FriBidiChar *visual = new FriBidiChar[input.size()];

for (size_t i = 0; i < input.size(); i++) {
logical[i] = input[i];
Expand Down
87 changes: 87 additions & 0 deletions test/utf8_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gtest/gtest.h>

#include "utils/utf8.hpp"
#include "utils/unicode-bidi.hpp"

namespace devilution {
namespace {
Expand Down Expand Up @@ -81,5 +82,91 @@ TEST(Utf8CodeUnits, BasicLatin)
EXPECT_FALSE(IsBasicLatin('\xFF')) << "Multibyte Utf8 code units are not Basic Latin symbols";
}

TEST(ConvertUtf8ToUtf32Test, EmptyString)
{
std::string_view input;
auto result = ConvertUtf8ToUtf32(input);
EXPECT_TRUE(result.empty());
}

TEST(ConvertUtf8ToUtf32Test, BasicLatin)
{
std::string_view input = "Hello, world!";
auto result = ConvertUtf8ToUtf32(input);
EXPECT_EQ(result, U"Hello, world!");
}

TEST(ConvertUtf8ToUtf32Test, MultibyteUtf8)
{
std::string_view input = "こんにちは、世界!";
auto result = ConvertUtf8ToUtf32(input);
EXPECT_EQ(result, U"こんにちは、世界!");
}

TEST(ConvertUtf32ToUtf8Test, EmptyString)
{
std::u32string_view input;
auto result = ConvertUtf32ToUtf8(input);
EXPECT_TRUE(result.empty());
}

TEST(ConvertUtf32ToUtf8Test, BasicLatin)
{
std::u32string_view input = U"Hello, world!";
auto result = ConvertUtf32ToUtf8(input);
EXPECT_EQ(result, "Hello, world!");
}

TEST(ConvertUtf32ToUtf8Test, MultibyteUtf8)
{
std::u32string_view input = U"こんにちは、世界!";
auto result = ConvertUtf32ToUtf8(input);
EXPECT_EQ(result, "こんにちは、世界!");
}

TEST(ConvertUtf32ToUtf8Test, Inverse)
{
std::u32string_view input = U"こんにちは、世界!";
auto utf8 = ConvertUtf32ToUtf8(input);
auto utf32 = ConvertUtf8ToUtf32(utf8);
EXPECT_EQ(input, utf32);
}

TEST(ConvertUtf32ToUtf8Test, InverseInverse)
{
std::string_view input = "こんにちは、世界!";
auto utf32 = ConvertUtf8ToUtf32(input);
auto utf8 = ConvertUtf32ToUtf8(utf32);
EXPECT_EQ(input, utf8);
}

TEST(ConvertLogicalToVisualTest, EmptyString)
{
std::u32string_view input;
auto result = ConvertLogicalToVisual(input);
EXPECT_TRUE(result.empty());
}

TEST(ConvertLogicalToVisualTest, BasicLatin)
{
std::u32string_view input = U"Hello, world!";
auto result = ConvertLogicalToVisual(input);
EXPECT_EQ(result, U"Hello, world!");
}

TEST(ConvertLogicalToVisualTest, Hebrew)
{
std::u32string_view input = U"שלום, עולם!";
auto result = ConvertLogicalToVisual(input);
EXPECT_EQ(result, U"!םלוע ,םולש");
}

// TEST(ConvertLogicalToVisualTest, MultiLineString)
// {
// std::u32string_view input = U"שלום\nכיתה א!";
// auto result = ConvertLogicalToVisual(input);
// EXPECT_EQ(ConvertUtf32ToUtf8(result), "םולש\n!א התיכ");
// }

} // namespace
} // namespace devilution

0 comments on commit 4ee21d7

Please sign in to comment.