1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Add support for turkish glyphs

Improve UTF-8 parsing by the way.
This commit is contained in:
Mathieu Schroeter 2019-01-25 17:30:22 +01:00
parent a732fad20f
commit adb44c5f05
4 changed files with 87 additions and 54 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -981,7 +981,7 @@ DoInit (int argc, char * argv[], bool & exit)
} }
totalDim.x = DIMTEXTX * 16; totalDim.x = DIMTEXTX * 16;
totalDim.y = DIMTEXTY * 9 * 3; totalDim.y = DIMTEXTY * 14 * 3;
iconDim.x = DIMTEXTX; iconDim.x = DIMTEXTX;
iconDim.y = DIMTEXTY; iconDim.y = DIMTEXTY;
if (!g_pPixmap->Cache (CHTEXT, "image/text.png", totalDim, iconDim)) if (!g_pPixmap->Cache (CHTEXT, "image/text.png", totalDim, iconDim))
@ -991,7 +991,7 @@ DoInit (int argc, char * argv[], bool & exit)
} }
totalDim.x = DIMLITTLEX * 16; totalDim.x = DIMLITTLEX * 16;
totalDim.y = DIMLITTLEY * 9; totalDim.y = DIMLITTLEY * 14;
iconDim.x = DIMLITTLEX; iconDim.x = DIMLITTLEX;
iconDim.y = DIMLITTLEY; iconDim.y = DIMLITTLEY;
if (!g_pPixmap->Cache (CHLITTLE, "image/little.png", totalDim, iconDim)) if (!g_pPixmap->Cache (CHLITTLE, "image/little.png", totalDim, iconDim))

View File

@ -34,56 +34,65 @@
* \param[in] c - The character (incremented if 0xC3 or 0xC4 or 0xC5 UTF-8). * \param[in] c - The character (incremented if 0xC3 or 0xC4 or 0xC5 UTF-8).
* \returns the offset. * \returns the offset.
*/ */
static Sint32 static Uint8
GetOffset (const char *& c) GetOffset (const char *& c)
{ {
/* clang-format off */ /* clang-format off */
static const unsigned char table_accents[] = { static const unsigned char table_c3[] = {
/* ü à â é è ë ê ï */ /* ü à â é è ë ê ï */
0xBC, 0xA0, 0xA2, 0xA9, 0xA8, 0xAB, 0xAA, 0xAF, /* UTF-8 */ 0xBC, 0xA0, 0xA2, 0xA9, 0xA8, 0xAB, 0xAA, 0xAF,
/* î ô ù û ä ö ç */ /* î ô ù û ä ö ç ò */
0xAE, 0xB4, 0xB9, 0xBB, 0xA4, 0xB6, 0xA7, /* UTF-8 */ 0xAE, 0xB4, 0xB9, 0xBB, 0xA4, 0xB6, 0xA7, 0xB2,
/* ì ó Ç Ö Ü */
0xAC, 0xB3, 0x87, 0x96, 0x9C,
}; };
static const unsigned char table_extended[] = { static const unsigned char table_c4[] = {
/* Italian */ /* ę ć ą Ğ ğ İ ı */
/* ò ì */ 0x99, 0x87, 0x85, 0x9E, 0x9F, 0xB0, 0xB1,
0xB2, 0xAC, /* UTF-8 */ };
/* Polish */
/* ń ó ę ć ź ż */ static const unsigned char table_c5[] = {
0x84, 0xB3, 0x99, 0x87, 0xBA, 0xBC, /* UTF-8 */ /* ń ź ż ł ś Ş ş */
/* ą ł ś */ 0x84, 0xBA, 0xBC, 0x82, 0x9B, 0x9E, 0x9F,
0x85, 0x82, 0x9B, /* UTF-8 */
}; };
/* clang-format on */ /* clang-format on */
if (static_cast<unsigned char> (*c) == 0xC3) int offset = 0;
c++; int inc = 0;
if (static_cast<unsigned char> (*c) == 0xC4) size_t size = 0;
c++; const unsigned char * table = nullptr;
if (static_cast<unsigned char> (*c) == 0xC5)
c++;
if (GetLocale () != "pl") switch (static_cast<unsigned char> (*c))
{ {
// Do not use the 'standard' accents table with Polish locale case 0xC3:
// This is required because we check only last byte of UTF-8 and some offset = 128;
// characters overlap table = table_c3;
// TODO: In the future, this ugly hack should be replaced with proper UTF-8 size = countof (table_c3);
// parsing inc = 1;
for (unsigned int i = 0; i < countof (table_accents); ++i) break;
if ((unsigned char) *c == table_accents[i]) case 0xC4:
return 15 + i; offset = 128 + 32;
table = table_c4;
size = countof (table_c4);
inc = 1;
break;
case 0xC5:
offset = 128 + 64;
table = table_c5;
size = countof (table_c5);
inc = 1;
break;
} }
for (unsigned int i = 0; i < countof (table_extended); ++i) for (size_t i = 0; i < size; ++i)
if ((unsigned char) *c == table_extended[i]) if (static_cast<unsigned char> (*(c + inc)) == table[i])
return 127 + i; return offset + i;
if (*c < 0) if (*(c + inc) < 0)
return 1; // square return 1; // square
return *c; return *(c + inc);
} }
/** /**
@ -93,41 +102,50 @@ GetOffset (const char *& c)
* \param[in] font - The font used (little or normal). * \param[in] font - The font used (little or normal).
* \returns the length. * \returns the length.
*/ */
Sint32 Uint8
GetCharWidth (const char *& c, Sint32 font) GetCharWidth (const char *& c, Sint32 font)
{ {
// clang-format off // clang-format off
static const unsigned char table_width[] = static const Uint8 table_width[] =
{ {
9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0,
9, 9, 8, 8, 8, 8, 5, 5, 8, 8, 8, 9, 8, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10,
5, 6, 9, 13, 11, 12, 12, 6, 6, 6, 12, 12, 5, 9, 6, 9, 5, 6, 9, 13, 11, 12, 12, 6, 6, 6, 12, 12, 5, 9, 6, 9,
8, 8, 9, 9, 8, 9, 8, 8, 9, 9, 6, 6, 8, 9, 10, 11, 8, 8, 9, 9, 8, 9, 8, 8, 9, 9, 6, 6, 8, 9, 10, 11,
12, 8, 9, 9, 9, 8, 8, 8, 9, 4, 8, 9, 8, 10, 9, 9, 12, 8, 9, 9, 9, 8, 8, 8, 9, 4, 8, 9, 8, 10, 9, 9,
8, 9, 8, 9, 10, 8, 9, 11, 9, 8, 10, 7, 10, 7, 13, 13, 8, 9, 8, 9, 10, 8, 9, 11, 9, 8, 10, 7, 10, 7, 13, 13,
9, 9, 8, 8, 8, 8, 6, 8, 8, 4, 6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6, 6, 3, 5, 3, 6, 8, 6, 8, 7, 6, 6, 6, 8, 12, 7, 6, 6, 3, 5, 3, 6, 8,
4, 6, 6, 6, 6, 6, 4, 6, 6, 2, 3, 5, 2, 10, 6, 6, 4, 6, 6, 6, 6, 6, 4, 6, 6, 2, 3, 5, 2, 10, 6, 6,
6, 6, 3, 5, 3, 6, 6, 8, 6, 6, 5, 4, 6, 4, 7, 6, 6, 6, 3, 5, 3, 6, 6, 8, 6, 6, 5, 4, 6, 4, 7, 0,
3, 6, 6, 6, 5, 5, 5, 7, 4, 5 7, 6, 6, 6, 6, 6, 6, 3, 3, 6, 6, 6, 6, 6, 6, 6,
3, 6, 7, 8, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 5, 7, 8, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 5, 5, 4, 5, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}; };
// clang-format on // clang-format on
if (font == FONTLITTLE) if (font == FONTLITTLE)
return table_width_little[GetOffset (c)]; return table_width_little[GetOffset (c)];
else return table_width[GetOffset (c)] - 1;
return table_width[GetOffset (c)] - 1;
} }
/** /**
@ -145,17 +163,21 @@ DrawText (CPixmap * pPixmap, Point pos, const char * pText, Sint32 font)
while (*pText != '\0') while (*pText != '\0')
{ {
rank = GetOffset (pText); rank = GetOffset (pText);
auto inc = rank > 127;
if (font != FONTLITTLE) if (font != FONTLITTLE)
{ {
rank += (128 + 16) * font; rank += 224 * font;
pPixmap->DrawIcon (-1, CHTEXT, rank, pos); pPixmap->DrawIcon (-1, CHTEXT, rank, pos);
} }
else else
pPixmap->DrawIcon (-1, CHLITTLE, rank, pos); pPixmap->DrawIcon (-1, CHLITTLE, rank, pos);
pos.x += GetCharWidth (pText, font); pos.x += GetCharWidth (pText, font);
if (inc)
pText++;
pText++; pText++;
} }
} }
@ -172,11 +194,16 @@ DrawTextPente (
rel = 0; rel = 0;
while (*pText != 0) while (*pText != 0)
{ {
rank = GetOffset (pText); rank = GetOffset (pText);
rank += (128 + 16) * font; auto inc = rank > 127;
rank += 224 * font;
pPixmap->DrawIcon (-1, CHTEXT, rank, pos); pPixmap->DrawIcon (-1, CHTEXT, rank, pos);
lg = GetCharWidth (pText, font); lg = GetCharWidth (pText, font);
if (inc)
pText++;
pText++; pText++;
rel += lg; rel += lg;
pos.x += lg; pos.x += lg;
@ -347,7 +374,13 @@ GetTextWidth (const char * pText, Sint32 font)
while (*pText != 0) while (*pText != 0)
{ {
auto rank = GetOffset (pText);
auto inc = rank > 127;
width += GetCharWidth (pText, font); width += GetCharWidth (pText, font);
if (inc)
pText++;
pText++; pText++;
} }