Permalink
Browse files

Merge pull request #1478 from TheCycoONE/unicode_fixes

Chinese and memory leak fixes
  • Loading branch information...
Alberth289346 committed Oct 2, 2018
2 parents 02896f9 + 0c61dca commit f16a51011a611c1a66ba91bea8fa640739905ccf
Showing with 23 additions and 7 deletions.
  1. +23 −7 CorsixTH/Src/th_gfx_font.cpp
@@ -333,9 +333,10 @@ freetype_font::freetype_font()
freetype_font::~freetype_font()
{
for(cached_text* pEntry = cache;
pEntry != cache + (1 << cache_size_log2); ++pEntry)
{
for( cached_text* pEntry = cache;
pEntry != cache + (1 << cache_size_log2); ++pEntry) {
delete[] pEntry->message;
delete[] pEntry->data;
free_texture(pEntry);
}
if(font_face != nullptr)
@@ -505,6 +506,17 @@ struct codepoint_glyph
FT_UInt index;
};
// Determine if the character code is a suitable Chinese/Japanese/Korean
// character for a line break.
bool isCjkBreakCharacter(int charcode) {
return (charcode == 0x3000 || // Ideographic space
charcode == 0x3002 || // Ideographic full stop
charcode == 0xff0c || // Fullwidth comma
charcode == 0xff0d || // Fullwidth hyphen-minus
charcode == 0xff1b || // Fullwidth semicolon
charcode == 0xff1f); //Fullwidth question mark
}
text_layout freetype_font::draw_text_wrapped(render_target* pCanvas, const char* sMessage,
size_t iMessageLength, int iX, int iY,
int iWidth, int iMaxRows, int iSkipRows, text_alignment eAlign) const
@@ -652,8 +664,12 @@ text_layout freetype_font::draw_text_wrapped(render_target* pCanvas, const char*
}
// Determine if a line can be broken at the current position.
if(iCode == ' ')
if (iCode == ' ') {
sLineBreakPosition = sOldMessage;
} else if (isCjkBreakCharacter(iCode)) {
// break after this codepoint (cjk codepoints are 3 bytes in utf-8)
sLineBreakPosition = sOldMessage + 3;
}
// Save (unless we are skipping lines) and advance the pen.
if(iHandledRows >= iSkipRows)
@@ -701,7 +717,7 @@ text_layout freetype_font::draw_text_wrapped(render_target* pCanvas, const char*
// Calculate the line height and baseline position.
FT_Pos iLineHeight = 0;
FT_Pos iBaselinePos = 0;
for(const char* s = itr->first; s != itr->second; )
for(const char* s = itr->first; s < itr->second; )
{
codepoint_glyph& oGlyph = mapGlyphs[next_utf8_codepoint(s)];
FT_Pos iBearingY = oGlyph.metrics.horiBearingY;
@@ -719,7 +735,7 @@ text_layout freetype_font::draw_text_wrapped(render_target* pCanvas, const char*
iNormalLineHeight = std::max(iNormalLineHeight, iLineHeight);
// Apply the character position changes.
for(const char* s = itr->first; s != itr->second; next_utf8_codepoint(s))
for(const char* s = itr->first; s < itr->second; next_utf8_codepoint(s))
{
FT_Vector& ftvPos = vCharPositions[s - sMessage];
ftvPos.x += iAlignDelta;
@@ -766,7 +782,7 @@ text_layout freetype_font::draw_text_wrapped(render_target* pCanvas, const char*
itr != itrEnd && iDrawnLines < iMaxRows + iSkipRows; ++itr)
{
iDrawnLines++;
for(const char* s = itr->first; s != itr->second; )
for(const char* s = itr->first; s < itr->second; )
{
FT_Vector& ftvPos = vCharPositions[s - sMessage];
unsigned int iCode = next_utf8_codepoint(s);

0 comments on commit f16a510

Please sign in to comment.