Skip to content

Commit

Permalink
Restore UTF-8 for cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
BLooperZ committed Sep 30, 2024
1 parent e84a5b4 commit 612b8b4
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Source/engine/render/text_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,16 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,

std::u32string text32 = ConvertUtf8ToUtf32(text);
std::u32string_view remaining = text32;

std::string rem;
uint32_t bytesDrawn = 0;

size_t lineStart = 0;
size_t currPos = 0;
std::u32string line;

const auto maybeDrawCursor = [&]() {
if (opts.cursorPosition == static_cast<int>(text.size() - remaining.size())) {
if (opts.cursorPosition == static_cast<int>(text.size() - rem.size())) {
Point position = characterPosition;
MaybeWrap(position, 2, rightMargin, position.x, opts.lineHeight);
if (GetAnimationFrame(2, 500) != 0) {
Expand All @@ -430,7 +432,8 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
line = ConvertLogicalToVisual(text32.substr(lineStart, currPos - lineStart));

for (uint16_t j = lineStart; j < currPos; j++) {
remaining = text32.substr(j + 1);
remaining = text32.substr(j);
rem = ConvertUtf32ToUtf8(remaining);

char32_t cnext = line[j - lineStart];

Expand All @@ -447,11 +450,13 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
const uint8_t frame = cnext & 0xFF;
const ClxSprite glyph = (*currentFont.sprite)[frame];
const int w = glyph.width();
const auto byteIndex = static_cast<int>(text.size() - ConvertUtf32ToUtf8(text32.substr(j)).size());
const auto byteIndex = static_cast<int>(text.size() - rem.size());

// Draw highlight
if (byteIndex >= opts.highlightRange.begin && byteIndex < opts.highlightRange.end) {
const bool lastInRange = static_cast<int>(byteIndex + 1) == opts.highlightRange.end;
size_t cpLen;
DecodeFirstUtf8CodePoint(rem, &cpLen);
const bool lastInRange = static_cast<int>(byteIndex + cpLen) == opts.highlightRange.end;
FillRect(out, characterPosition.x, characterPosition.y,
glyph.width() + (lastInRange ? 0 : opts.spacing), glyph.height(),
opts.highlightColor);
Expand All @@ -461,12 +466,15 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
maybeDrawCursor();
characterPosition.x += w + opts.spacing;
}
remaining = text32.substr(currPos);
rem = ConvertUtf32ToUtf8(remaining);
maybeDrawCursor();
};

int16_t lineX = characterPosition.x;


for (currPos = 0; currPos < remaining.size() && text32[currPos] != U'\0'; currPos++) {
for (currPos = 0; currPos < text32.size() && text32[currPos] != U'\0'; currPos++) {
char32_t next = text32[currPos];
bytesDrawn += ConvertUtf32ToUtf8(text32.substr(currPos, currPos + 1)).size();

Expand Down Expand Up @@ -510,7 +518,6 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
}

drawSingleLine();
maybeDrawCursor();
return bytesDrawn;
}

Expand Down

0 comments on commit 612b8b4

Please sign in to comment.