Skip to content

Commit

Permalink
- fixed crash when inserting CJK text.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Mar 10, 2019
1 parent b605ae5 commit 4532c2c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/c_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,30 @@ struct FCommandBuffer

}

unsigned CharsForCells(unsigned cells)
unsigned CharsForCells(unsigned cellin, bool *overflow)
{
unsigned chars = 0;
int cells = cellin;
while (cells > 0)
{
int w;
NewConsoleFont->GetChar(Text[chars++], CR_UNTRANSLATED, &w);
cells -= w / 9;
}
return cells == 0? chars : -chars;
*overflow = (cells < 0);
return cells;
}


void MakeStartPosGood()
{
// Make sure both values point to something valid.
if (CursorPos > Text.length()) CursorPos = Text.length();
if (StartPos > Text.length()) StartPos = Text.length();
if (CursorPos > Text.length()) CursorPos = (unsigned)Text.length();
if (StartPos > Text.length()) StartPos = (unsigned)Text.length();

CursorPosCells = CalcCellSize(CursorPos);
StartPosCells = CalcCellSize(StartPos);
unsigned LengthCells = CalcCellSize(Text.length());
unsigned LengthCells = CalcCellSize((unsigned)Text.length());

int n = StartPosCells;
unsigned cols = ConCols / active_con_scale();
Expand All @@ -288,11 +290,12 @@ struct FCommandBuffer
n = CursorPosCells;
}
StartPosCells = MAX(0, n);
StartPos = CharsForCells(StartPosCells);
if (StartPos < 0)
bool overflow;
StartPos = CharsForCells(StartPosCells, &overflow);
if (overflow)
{
// We ended up in the middle of a double cell character, so set the start to the following character.
StartPos = -StartPos + 1;
StartPos++;
StartPosCells++;
}
}
Expand Down

0 comments on commit 4532c2c

Please sign in to comment.