# HG changeset patch # User Gleb Mazovetskiy # Date 1577496838 0 # Sat Dec 28 01:33:58 2019 +0000 # Branch SDL-1.2 # Node ID 01df91d4636111239570a192e81009fae97410d2 # Parent 7d82f2e166f053f3197cdace6f525b8f3e90657a Add a family of functions for opening fonts for non-square displays FreeType natively supports font rendering for displays with non square pixels. This commit adds the following 4 functions: TTF_OpenFontDPI TTF_OpenFontRWDPI TTF_OpenFontIndexDPI TTF_OpenFontIndexRWDPI They are equivalent to the functions without the DPI suffix but also accept 2 additional arguments, horizontal and vertical DPI resolution respectively. diff --git a/SDL_ttf.c b/SDL_ttf.c --- a/SDL_ttf.c +++ b/SDL_ttf.c @@ -371,7 +371,7 @@ static unsigned long RWread( return SDL_RWread( src, buffer, 1, (int)count ); } -TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long index ) +TTF_Font* TTF_OpenFontIndexRWDPI( SDL_RWops *src, int freesrc, int ptsize, long index, unsigned int horz_resolution, unsigned int vert_resolution ) { TTF_Font* font; FT_Error error; @@ -463,8 +463,7 @@ TTF_Font* TTF_OpenFontIndexRW( SDL_RWops /* 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( font->face, 0, ptsize * 64, 0, 0 ); + error = FT_Set_Char_Size( font->face, 0, ptsize * 64, horz_resolution, vert_resolution ); if( error ) { TTF_SetFTError( "Couldn't set font size", error ); TTF_CloseFont( font ); @@ -539,19 +538,35 @@ TTF_Font* TTF_OpenFontIndexRW( SDL_RWops return font; } +TTF_Font* TTF_OpenFontRWDPI(SDL_RWops *src, int freesrc, int ptsize, unsigned int horz_resolution, unsigned int vert_resolution) { + return TTF_OpenFontIndexRWDPI(src, freesrc, ptsize, 0, 0, 0); +} + TTF_Font* TTF_OpenFontRW( SDL_RWops *src, int freesrc, int ptsize ) { return TTF_OpenFontIndexRW(src, freesrc, ptsize, 0); } -TTF_Font* TTF_OpenFontIndex( const char *file, int ptsize, long index ) -{ +TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long index ) { + return TTF_OpenFontIndexRWDPI(src, freesrc, ptsize, index, 0, 0); +} + +TTF_Font* TTF_OpenFontIndexDPI(const char *file, int ptsize, long index, unsigned int horz_resolution, unsigned int vert_resolution) { SDL_RWops *rw = SDL_RWFromFile(file, "rb"); if ( rw == NULL ) { TTF_SetError(SDL_GetError()); return NULL; } - return TTF_OpenFontIndexRW(rw, 1, ptsize, index); + return TTF_OpenFontIndexRWDPI(rw, 1, ptsize, index, horz_resolution, vert_resolution); +} + +TTF_Font* TTF_OpenFontIndex( const char *file, int ptsize, long index ) +{ + return TTF_OpenFontIndexDPI(file, ptsize, index, 0, 0); +} + +TTF_Font* TTF_OpenFontDPI(const char *file, int ptsize, unsigned int horz_resolution, unsigned int vert_resolution) { + return TTF_OpenFontIndexDPI(file, ptsize, 0, horz_resolution, vert_resolution); } TTF_Font* TTF_OpenFont( const char *file, int ptsize ) diff --git a/SDL_ttf.h b/SDL_ttf.h --- a/SDL_ttf.h +++ b/SDL_ttf.h @@ -88,6 +88,13 @@ extern DECLSPEC TTF_Font * SDLCALL TTF_O 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); +/* Opens a font using the given horizontal and vertical target resolutions (in DPI). + * DPI scaling only applies to scalable fonts (e.g. TrueType). */ +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontDPI(const char *file, int ptsize, unsigned int horz_resolution, unsigned int vert_resolution); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRWDPI(SDL_RWops *src, int freesrc, int ptsize, unsigned int horz_resolution, unsigned int vert_resolution); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexDPI(const char *file, int ptsize, long index, unsigned int horz_resolution, unsigned int vert_resolution); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRWDPI(SDL_RWops *src, int freesrc, int ptsize, long index, unsigned int horz_resolution, unsigned int vert_resolution); + /* Set and retrieve the font style */ #define TTF_STYLE_NORMAL 0x00 #define TTF_STYLE_BOLD 0x01