The GRRLIB_LoadTTF function is now returning NULL if it fails to load the font.

This commit is contained in:
Crayon2000 2015-09-20 21:11:56 -04:00
parent 30c060518c
commit 080dd9b7fd
5 changed files with 30 additions and 23 deletions

View file

@ -87,7 +87,7 @@ void GRRLIB_FreeBMF (GRRLIB_bytemapFont *bmf) {
u16 i;
for (i=0; i<256; i++) {
if(bmf->charDef[i].data) {
if (bmf->charDef[i].data) {
free(bmf->charDef[i].data);
}
}

View file

@ -116,7 +116,7 @@ int GRRLIB_Init (void) {
GX_SetDispCopyGamma(GX_GM_1_0);
if(rmode->fbWidth <= 0){ printf("GRRLIB " GRRLIB_VER_STRING); }
if (rmode->fbWidth <= 0) { printf("GRRLIB " GRRLIB_VER_STRING); }
// Setup the vertex descriptor
GX_ClearVtxDesc(); // clear all the vertex descriptors

View file

@ -81,7 +81,9 @@ GRRLIB_texImg* GRRLIB_LoadTextureFromFile(const char *filename) {
unsigned char *data;
// return NULL it load fails
if (GRRLIB_LoadFile(filename, &data) <= 0) return NULL;
if (GRRLIB_LoadFile(filename, &data) <= 0) {
return NULL;
}
// Convert to texture
tex = GRRLIB_LoadTexture(data);

View file

@ -119,7 +119,7 @@ GRRLIB_texImg* GRRLIB_CreateEmptyTexture (const uint w, const uint h)
{
GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
if (my_texture != NULL) {
my_texture->data = memalign(32, h * w * 4);
my_texture->w = w;
my_texture->h = h;
@ -159,15 +159,15 @@ GRRLIB_texImg* GRRLIB_LoadTexturePNG (const u8 *my_png) {
IMGCTX ctx;
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
if (my_texture != NULL) {
ctx = PNGU_SelectImageFromBuffer(my_png);
PNGU_GetImageProperties(ctx, &imgProp);
my_texture->data = PNGU_DecodeTo4x4RGBA8(ctx, imgProp.imgWidth, imgProp.imgHeight, &width, &height, NULL);
if(my_texture->data != NULL) {
if (my_texture->data != NULL) {
my_texture->w = width;
my_texture->h = height;
GRRLIB_SetHandle( my_texture, 0, 0 );
if(imgProp.imgWidth != width || imgProp.imgHeight != height) {
if (imgProp.imgWidth != width || imgProp.imgHeight != height) {
// PNGU has resized the texture
memset(my_texture->data, 0, (my_texture->h * my_texture->w) << 2);
}
@ -212,7 +212,7 @@ GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp) {
s32 y, x, i;
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
if (my_texture != NULL) {
// Fill file header structure
MyBitmapFileHeader.bfType = (my_bmp[0] | my_bmp[1]<<8);
MyBitmapFileHeader.bfSize = (my_bmp[2] | my_bmp[3]<<8 | my_bmp[4]<<16 | my_bmp[5]<<24);
@ -233,7 +233,7 @@ GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp) {
MyBitmapHeader.biClrImportant = (my_bmp[50] | my_bmp[51]<<8 | my_bmp[52]<<16 | my_bmp[53]<<24);
my_texture->data = memalign(32, MyBitmapHeader.biWidth * MyBitmapHeader.biHeight * 4);
if(my_texture->data != NULL && MyBitmapFileHeader.bfType == 0x4D42) {
if (my_texture->data != NULL && MyBitmapFileHeader.bfType == 0x4D42) {
my_texture->w = MyBitmapHeader.biWidth;
my_texture->h = MyBitmapHeader.biHeight;
switch(MyBitmapHeader.biBitCount) {
@ -370,15 +370,16 @@ GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const int my_size) {
GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
unsigned int i;
if(my_texture == NULL)
if (my_texture == NULL) {
return NULL;
}
jpeg_create_decompress(&cinfo);
cinfo.err = jpeg_std_error(&jerr);
cinfo.progress = NULL;
jpeg_mem_src(&cinfo, (unsigned char *)my_jpg, my_size);
jpeg_read_header(&cinfo, TRUE);
if(cinfo.jpeg_color_space == JCS_GRAYSCALE) {
if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
cinfo.out_color_space = JCS_RGB;
}
jpeg_start_decompress(&cinfo);

View file

@ -54,13 +54,15 @@ void GRRLIB_ExitTTF (void) {
* Load a TTF from a buffer.
* @param file_base Buffer with TTF data. You must not deallocate the memory before calling GRRLIB_FreeTTF.
* @param file_size Size of the TTF buffer.
* @return A handle to a given TTF font object.
* @return A handle to a given TTF font object or NULL if it fails to load the font.
* @see GRRLIB_FreeTTF
*/
GRRLIB_ttfFont* GRRLIB_LoadTTF (const u8* file_base, s32 file_size) {
FT_Face Face;
if (FT_New_Memory_Face(ftLibrary, file_base, file_size, 0, &Face)) {
return NULL;
}
GRRLIB_ttfFont* myFont = (GRRLIB_ttfFont*)malloc(sizeof(GRRLIB_ttfFont));
FT_New_Memory_Face(ftLibrary, file_base, file_size, 0, &Face);
myFont->kerning = FT_HAS_KERNING(Face);
/*
if (FT_Set_Pixel_Sizes(Face, 0, fontSize)) {
@ -76,7 +78,7 @@ GRRLIB_ttfFont* GRRLIB_LoadTTF (const u8* file_base, s32 file_size) {
* @param myFont A TTF.
*/
void GRRLIB_FreeTTF (GRRLIB_ttfFont *myFont) {
if(myFont) {
if (myFont != NULL) {
FT_Done_Face(myFont->face);
free(myFont);
myFont = NULL;
@ -93,14 +95,15 @@ void GRRLIB_FreeTTF (GRRLIB_ttfFont *myFont) {
* @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) {
if(myFont == NULL || string == NULL)
if (myFont == NULL || string == NULL) {
return;
}
size_t length = strlen(string) + 1;
wchar_t *utf32 = (wchar_t*)malloc(length * sizeof(wchar_t));
if(utf32) {
if (utf32) {
length = mbstowcs(utf32, string, length);
if(length > 0) {
if (length > 0) {
utf32[length] = L'\0';
GRRLIB_PrintfTTFW(x, y, myFont, utf32, fontSize, color);
}
@ -119,8 +122,9 @@ void GRRLIB_PrintfTTF(int x, int y, GRRLIB_ttfFont *myFont, const char *string,
* @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;
}
FT_Face Face = (FT_Face)myFont->face;
int penX = 0;
@ -190,7 +194,7 @@ static void DrawBitmap(FT_Bitmap *bitmap, int offset, int top, const u8 cR, cons
* @return The width of a text in pixel.
*/
unsigned int GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigned int fontSize) {
if(myFont == NULL || string == NULL) {
if (myFont == NULL || string == NULL) {
return 0;
}
unsigned int penX;
@ -214,7 +218,7 @@ unsigned int GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *string, unsigne
* @return The width of a text in pixel.
*/
unsigned int GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsigned int fontSize) {
if(myFont == NULL || utf32 == NULL) {
if (myFont == NULL || utf32 == NULL) {
return 0;
}
@ -223,19 +227,19 @@ unsigned int GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *utf32, unsi
FT_UInt glyphIndex;
FT_UInt previousGlyph = 0;
if(FT_Set_Pixel_Sizes(myFont->face, 0, fontSize)) {
if (FT_Set_Pixel_Sizes(myFont->face, 0, fontSize)) {
FT_Set_Pixel_Sizes(myFont->face, 0, 12);
}
while(*utf32) {
glyphIndex = FT_Get_Char_Index(myFont->face, *utf32++);
if(myFont->kerning && previousGlyph && glyphIndex) {
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)) {
if (FT_Load_Glyph(Face, glyphIndex, FT_LOAD_RENDER)) {
continue;
}