Printf TTF: correct calculation of wchar_t array required for string conversion (replaced strlen(string) with mbstowcs(NULL, string, 0) as indicate in mbstowcs manual page example)

This commit is contained in:
nebiun 2021-08-17 15:47:16 +02:00
parent ff74ddad90
commit da151aaa21

View file

@ -1,5 +1,5 @@
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
Copyright (c) 2009-2020 The GRRLIB Team Copyright (c) 2009-2021 The GRRLIB Team
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -48,8 +48,7 @@ static inline void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 c
for ( j = top, q = 0; j < y_max; j++, q++ ) { for ( j = top, q = 0; j < y_max; j++, q++ ) {
GX_Begin(GX_POINTS, GX_VTXFMT0, 1); GX_Begin(GX_POINTS, GX_VTXFMT0, 1);
GX_Position3f32(i, j, 0); GX_Position3f32(i, j, 0);
GX_Color4u8(cR, cG, cB, GX_Color4u8(cR, cG, cB, bitmap->buffer[ q * bitmap->width + p ]);
bitmap->buffer[ q * bitmap->width + p ]);
GX_End(); GX_End();
} }
} }
@ -199,7 +198,14 @@ static u32 _PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string,
return 0; return 0;
} }
u32 penX = 0; u32 penX = 0;
size_t length = strlen(string) + 1; size_t length;
length = mbstowcs(NULL, string, 0);
if(length == (size_t) -1) {
return 0;
}
length++;
if(length > _utf32_len) { if(length > _utf32_len) {
if(_utf32 != NULL) { if(_utf32 != NULL) {
free(_utf32); free(_utf32);
@ -210,9 +216,8 @@ static u32 _PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string,
_utf32_len = length; _utf32_len = length;
} }
if (_utf32 != NULL) { if (_utf32 != NULL) {
length = mbstowcs(_utf32, string, length); if(mbstowcs(_utf32, string, length) > 0) {
if (length > 0) { _utf32[length-1] = L'\0';
*(_utf32+length) = L'\0';
penX = _PrintfTTFW(x, y, myFont, _utf32, fontSize, color, noprint); penX = _PrintfTTFW(x, y, myFont, _utf32, fontSize, color, noprint);
} }
} }