mirror of
https://github.com/GRRLIB/GRRLIB.git
synced 2024-11-23 07:22:23 +00:00
Update GRRLIB_ttf.c
Changed functions ordering (called after calling) Code optimizations. Change malloc strategy for PrintfTTF and WidthTTF.
This commit is contained in:
parent
aa41f4f814
commit
ff74ddad90
1 changed files with 111 additions and 117 deletions
|
@ -27,10 +27,33 @@ THE SOFTWARE.
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
|
|
||||||
static FT_Library ftLibrary; /**< A handle to a FreeType library instance. */
|
static FT_Library ftLibrary; /**< A handle to a FreeType library instance. */
|
||||||
|
static wchar_t *_utf32 = NULL;
|
||||||
|
static u32 _utf32_len = 0;
|
||||||
|
|
||||||
// Static function prototypes
|
/**
|
||||||
static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, const u8 cG, const u8 cB);
|
* Draw a character on the screen.
|
||||||
|
* @param bitmap Bitmap to draw.
|
||||||
|
* @param offset x-coordinate offset.
|
||||||
|
* @param top y-coordinate.
|
||||||
|
* @param cR Red component of the colour.
|
||||||
|
* @param cG Green component of the colour.
|
||||||
|
* @param cB Blue component of the colour.
|
||||||
|
*/
|
||||||
|
static inline void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, const u8 cG, const u8 cB) {
|
||||||
|
FT_Int i, j, p, q;
|
||||||
|
FT_Int x_max = offset + bitmap->width;
|
||||||
|
FT_Int y_max = top + bitmap->rows;
|
||||||
|
|
||||||
|
for ( i = offset, p = 0; i < x_max; i++, p++ ) {
|
||||||
|
for ( j = top, q = 0; j < y_max; j++, q++ ) {
|
||||||
|
GX_Begin(GX_POINTS, GX_VTXFMT0, 1);
|
||||||
|
GX_Position3f32(i, j, 0);
|
||||||
|
GX_Color4u8(cR, cG, cB,
|
||||||
|
bitmap->buffer[ q * bitmap->width + p ]);
|
||||||
|
GX_End();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize FreeType library.
|
* Initialize FreeType library.
|
||||||
|
@ -48,6 +71,11 @@ int GRRLIB_InitTTF () {
|
||||||
*/
|
*/
|
||||||
void GRRLIB_ExitTTF (void) {
|
void GRRLIB_ExitTTF (void) {
|
||||||
FT_Done_FreeType(ftLibrary);
|
FT_Done_FreeType(ftLibrary);
|
||||||
|
if(_utf32 != NULL) {
|
||||||
|
free(_utf32);
|
||||||
|
_utf32 = NULL;
|
||||||
|
_utf32_len = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,44 +115,13 @@ void GRRLIB_FreeTTF (GRRLIB_ttfFont *myFont) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print function for TTF font.
|
* Common code for GRRLIB_PrintfTTFW() and GRRLIB_WidthTTFW()
|
||||||
* @param x Specifies the x-coordinate of the upper-left corner of the text.
|
|
||||||
* @param y Specifies the y-coordinate of the upper-left corner of the text.
|
|
||||||
* @param myFont A TTF.
|
|
||||||
* @param string Text to draw.
|
|
||||||
* @param fontSize Size of the font.
|
|
||||||
* @param color Text color in RGBA format.
|
|
||||||
*/
|
*/
|
||||||
void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize, const u32 color) {
|
static u32 _PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize,
|
||||||
if (myFont == NULL || string == NULL) {
|
const u32 color, bool noprint)
|
||||||
return;
|
{
|
||||||
}
|
|
||||||
|
|
||||||
size_t length = strlen(string) + 1;
|
|
||||||
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
|
|
||||||
if (utf32 != NULL) {
|
|
||||||
length = mbstowcs(utf32, string, length);
|
|
||||||
if (length > 0) {
|
|
||||||
utf32[length] = L'\0';
|
|
||||||
GRRLIB_PrintfTTFW(x, y, myFont, utf32, fontSize, color);
|
|
||||||
}
|
|
||||||
free(utf32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print function for TTF font.
|
|
||||||
* @author wplaat and DrTwox
|
|
||||||
* @param x Specifies the x-coordinate of the upper-left corner of the text.
|
|
||||||
* @param y Specifies the y-coordinate of the upper-left corner of the text.
|
|
||||||
* @param myFont A TTF.
|
|
||||||
* @param utf32 Text to draw.
|
|
||||||
* @param fontSize Size of the font.
|
|
||||||
* @param color Text color in RGBA format.
|
|
||||||
*/
|
|
||||||
void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize, const u32 color) {
|
|
||||||
if (myFont == NULL || utf32 == NULL) {
|
if (myFont == NULL || utf32 == NULL) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
FT_Face Face = (FT_Face)myFont->face;
|
FT_Face Face = (FT_Face)myFont->face;
|
||||||
|
@ -152,65 +149,34 @@ void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf3
|
||||||
if (FT_Load_Glyph(myFont->face, glyphIndex, FT_LOAD_RENDER) != 0) {
|
if (FT_Load_Glyph(myFont->face, glyphIndex, FT_LOAD_RENDER) != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if(!noprint) {
|
||||||
DrawBitmap(&slot->bitmap,
|
DrawBitmap(&slot->bitmap,
|
||||||
penX + slot->bitmap_left + x,
|
penX + slot->bitmap_left + x,
|
||||||
penY - slot->bitmap_top + y,
|
penY - slot->bitmap_top + y,
|
||||||
cR, cG, cB);
|
cR, cG, cB);
|
||||||
|
}
|
||||||
penX += slot->advance.x >> 6;
|
penX += slot->advance.x >> 6;
|
||||||
previousGlyph = glyphIndex;
|
previousGlyph = glyphIndex;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Draw a character on the screen.
|
|
||||||
* @param bitmap Bitmap to draw.
|
|
||||||
* @param offset x-coordinate offset.
|
|
||||||
* @param top y-coordinate.
|
|
||||||
* @param cR Red component of the colour.
|
|
||||||
* @param cG Green component of the colour.
|
|
||||||
* @param cB Blue component of the colour.
|
|
||||||
*/
|
|
||||||
static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, const u8 cG, const u8 cB) {
|
|
||||||
FT_Int i, j, p, q;
|
|
||||||
FT_Int x_max = offset + bitmap->width;
|
|
||||||
FT_Int y_max = top + bitmap->rows;
|
|
||||||
|
|
||||||
for ( i = offset, p = 0; i < x_max; i++, p++ ) {
|
|
||||||
for ( j = top, q = 0; j < y_max; j++, q++ ) {
|
|
||||||
GX_Begin(GX_POINTS, GX_VTXFMT0, 1);
|
|
||||||
GX_Position3f32(i, j, 0);
|
|
||||||
GX_Color4u8(cR, cG, cB,
|
|
||||||
bitmap->buffer[ q * bitmap->width + p ]);
|
|
||||||
GX_End();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the width of a text in pixel.
|
|
||||||
* @param myFont A TTF.
|
|
||||||
* @param string The text to check.
|
|
||||||
* @param fontSize The size of the font.
|
|
||||||
* @return The width of a text in pixel.
|
|
||||||
*/
|
|
||||||
u32 GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize) {
|
|
||||||
if (myFont == NULL || string == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
u32 penX;
|
|
||||||
size_t length = strlen(string) + 1;
|
|
||||||
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
|
|
||||||
length = mbstowcs(utf32, string, length);
|
|
||||||
utf32[length] = L'\0';
|
|
||||||
|
|
||||||
penX = GRRLIB_WidthTTFW(myFont, utf32, fontSize);
|
|
||||||
|
|
||||||
free(utf32);
|
|
||||||
|
|
||||||
return penX;
|
return penX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print function for TTF font.
|
||||||
|
* @author wplaat and DrTwox
|
||||||
|
* @param x Specifies the x-coordinate of the upper-left corner of the text.
|
||||||
|
* @param y Specifies the y-coordinate of the upper-left corner of the text.
|
||||||
|
* @param myFont A TTF.
|
||||||
|
* @param utf32 Text to draw.
|
||||||
|
* @param fontSize Size of the font.
|
||||||
|
* @param color Text color in RGBA format.
|
||||||
|
*/
|
||||||
|
void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize, const u32 color)
|
||||||
|
{
|
||||||
|
_PrintfTTFW(x, y, myFont, utf32, fontSize, color, false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the width of a text in pixel.
|
* Get the width of a text in pixel.
|
||||||
* @param myFont A TTF.
|
* @param myFont A TTF.
|
||||||
|
@ -218,35 +184,63 @@ u32 GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fon
|
||||||
* @param fontSize The size of the font.
|
* @param fontSize The size of the font.
|
||||||
* @return The width of a text in pixel.
|
* @return The width of a text in pixel.
|
||||||
*/
|
*/
|
||||||
u32 GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize) {
|
u32 GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize)
|
||||||
if (myFont == NULL || utf32 == NULL) {
|
{
|
||||||
|
return _PrintfTTFW(0, 0, myFont, utf32, fontSize, 0x00000000, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common code for GRRLIB_PrintfTTF() and GRRLIB_WidthTTF()
|
||||||
|
*/
|
||||||
|
static u32 _PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize,
|
||||||
|
const u32 color, bool noprint)
|
||||||
|
{
|
||||||
|
if (myFont == NULL || string == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
FT_Face Face = (FT_Face)myFont->face;
|
|
||||||
u32 penX = 0;
|
u32 penX = 0;
|
||||||
FT_UInt glyphIndex;
|
size_t length = strlen(string) + 1;
|
||||||
FT_UInt previousGlyph = 0;
|
if(length > _utf32_len) {
|
||||||
|
if(_utf32 != NULL) {
|
||||||
if (FT_Set_Pixel_Sizes(myFont->face, 0, fontSize) != 0) {
|
free(_utf32);
|
||||||
FT_Set_Pixel_Sizes(myFont->face, 0, 12);
|
_utf32 = NULL;
|
||||||
|
_utf32_len = 0;
|
||||||
}
|
}
|
||||||
|
_utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
|
||||||
while(*utf32) {
|
_utf32_len = length;
|
||||||
glyphIndex = FT_Get_Char_Index(myFont->face, *utf32++);
|
|
||||||
|
|
||||||
if (myFont->kerning && previousGlyph && glyphIndex) {
|
|
||||||
FT_Vector delta;
|
|
||||||
FT_Get_Kerning(Face, previousGlyph, glyphIndex, FT_KERNING_DEFAULT, &delta);
|
|
||||||
penX += delta.x >> 6;
|
|
||||||
}
|
}
|
||||||
if (FT_Load_Glyph(Face, glyphIndex, FT_LOAD_RENDER) != 0) {
|
if (_utf32 != NULL) {
|
||||||
continue;
|
length = mbstowcs(_utf32, string, length);
|
||||||
|
if (length > 0) {
|
||||||
|
*(_utf32+length) = L'\0';
|
||||||
|
penX = _PrintfTTFW(x, y, myFont, _utf32, fontSize, color, noprint);
|
||||||
}
|
}
|
||||||
|
|
||||||
penX += Face->glyph->advance.x >> 6;
|
|
||||||
previousGlyph = glyphIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return penX;
|
return penX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print function for TTF font.
|
||||||
|
* @param x Specifies the x-coordinate of the upper-left corner of the text.
|
||||||
|
* @param y Specifies the y-coordinate of the upper-left corner of the text.
|
||||||
|
* @param myFont A TTF.
|
||||||
|
* @param string Text to draw.
|
||||||
|
* @param fontSize Size of the font.
|
||||||
|
* @param color Text color in RGBA format.
|
||||||
|
*/
|
||||||
|
void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize, const u32 color)
|
||||||
|
{
|
||||||
|
_PrintfTTF(x, y, myFont, string, fontSize, color, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the width of a text in pixel.
|
||||||
|
* @param myFont A TTF.
|
||||||
|
* @param string The text to check.
|
||||||
|
* @param fontSize The size of the font.
|
||||||
|
* @return The width of a text in pixel.
|
||||||
|
*/
|
||||||
|
u32 GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize)
|
||||||
|
{
|
||||||
|
return _PrintfTTF(0, 0, myFont, string, fontSize, 0x00000000, true);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue