Skip to content

Commit

Permalink
Fixed bug 2487 - adding TTF_SetFontSize() to set font size dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
1bsyl committed Jan 31, 2019
1 parent 303fee0 commit 9a6863a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
31 changes: 22 additions & 9 deletions SDL_ttf.c
Expand Up @@ -255,6 +255,8 @@ static SDL_INLINE int Find_GlyphByIndex(TTF_Font *font, FT_UInt idx,
const int want_bitmap, const int want_pixmap, const int want_subpixel,
int translation, c_glyph **out_glyph, TTF_Image **out_image);

static void Flush_Cache(TTF_Font *font);

#if defined(USE_DUFFS_LOOP)

/* 4-times unrolled loop */
Expand Down Expand Up @@ -1451,23 +1453,34 @@ TTF_Font* TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long inde
return NULL;
}

if (TTF_SetFontSize(font, ptsize) < 0) {
TTF_SetFTError("Couldn't set font size", error);
TTF_CloseFont(font);
return NULL;
}
return font;
}

int TTF_SetFontSize(TTF_Font *font, int ptsize)
{
FT_Face face = font->face;
FT_Error error;

/* Make sure that our font face is scalable (global metrics) */
if (FT_IS_SCALABLE(face)) {
/* Set the character size and use default DPI (72) */
error = FT_Set_Char_Size(face, 0, ptsize * 64, 0, 0);
if (error) {
TTF_SetFTError("Couldn't set font size", error);
TTF_CloseFont(font);
return NULL;
return -1;
}
} else {
/* Non-scalable font case. ptsize determines which family
* or series of fonts to grab from the non-scalable format.
* It is not the point size of the font. */
if (face->num_fixed_sizes <= 0) {
TTF_SetError("Couldn't select size : no num_fixed_sizes");
TTF_CloseFont(font);
return NULL;
return -1;
}

/* within [0; num_fixed_sizes - 1] */
Expand All @@ -1477,18 +1490,18 @@ TTF_Font* TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long inde
error = FT_Select_Size(face, ptsize);
if (error) {
TTF_SetFTError("Couldn't select size", error);
TTF_CloseFont(font);
return NULL;
return -1;
}
}

if (TTF_initFontMetrics(font) < 0) {
TTF_SetError("Cannot initialize metrics");
TTF_CloseFont(font);
return NULL;
return -1;
}

return font;
Flush_Cache(font);

return 0;
}

/* Update font parameter depending on a style change */
Expand Down
3 changes: 3 additions & 0 deletions SDL_ttf.h
Expand Up @@ -109,6 +109,9 @@ extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndex(const char *file, int ptsiz
extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize);
extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index);

/* Set font size dynamically */
extern DECLSPEC int SDLCALL TTF_SetFontSize(TTF_Font *font, int ptsize);

/* Set and retrieve the font style */
#define TTF_STYLE_NORMAL 0x00
#define TTF_STYLE_BOLD 0x01
Expand Down

0 comments on commit 9a6863a

Please sign in to comment.