# HG changeset patch # User David Ludwig # Date 1429382861 14400 # Sat Apr 18 14:47:41 2015 -0400 # Node ID 1a9cb59bec3fe80a526695b2cf2e6084145f8935 # Parent b773c1cd55a2483fef78f65374a60ec02f5fd08d Added optional DPI-scaling of fonts This change allows TTF_Fonts to be created with DPI resolutions other than FreeType's default (of 72 DPI). Doing so allows fonts to be scaled linearly (at runtime), whereas point-sizes often do not scale linearly. Four new functions are added to SDL_ttf to facilitate this change: SDL_OpenFontDPI SDL_OpenFontIndexDPI SDL_OpenFontDPIRW SDL_OpenFontIndexDPIRW Each DPI-enabled SDL_OpenFont* call adds two parameters to the end: 1. unsigned int hdpi: specifies the DPI resolution along the horizontal axis 2. unsigned int vdpi: specifies the DPI resolution along the vertical axis Both parameters are passed directly into FreeType, which processes them as such: * if both hdpi and vdpi are 0, then FreeType will use its default DPI of 72 * if only hdpi, or only vdpi, is 0, then FreeType will use the non-zero value for both hdpi and vdpi. For example, if hdpi is 0 and vdpi is 144, FreeType will use 144 DPI on both axes. * if both hdpi and vdpi are NOT 0, then FreeType will use the DPI resolutions as specified, along their respective axes. This addition requires no modifications to FreeType 2.x itself, and does not modify any existing SDL_ttf API (for source and binary level compatibility). diff --git a/SDL_ttf.c b/SDL_ttf.c --- a/SDL_ttf.c +++ b/SDL_ttf.c @@ -365,7 +365,7 @@ return (unsigned long)SDL_RWread( src, buffer, 1, (int)count ); } -TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long index ) +TTF_Font* TTF_OpenFontIndexDPIRW( SDL_RWops *src, int freesrc, int ptsize, long index, unsigned int hdpi, unsigned int vdpi ) { TTF_Font* font; FT_Error error; @@ -455,8 +455,11 @@ /* 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 ); + /* Set the character size and use the provided DPI. If a DPI of zero is + * provided, then the other DPI setting will be used. If both are zero, + * then Freetype's default DPI (of 72) will be used. + * */ + error = FT_Set_Char_Size( font->face, 0, ptsize * 64, hdpi, vdpi ); if ( error ) { TTF_SetFTError( "Couldn't set font size", error ); TTF_CloseFont( font ); @@ -534,18 +537,38 @@ return font; } +TTF_Font* TTF_OpenFontDPIRW( SDL_RWops *src, int freesrc, int ptsize, unsigned int hdpi, unsigned int vdpi ) +{ + return TTF_OpenFontIndexDPIRW(src, freesrc, ptsize, 0, hdpi, vdpi); +} + +TTF_Font* TTF_OpenFontIndexRW( SDL_RWops *src, int freesrc, int ptsize, long index ) +{ + return TTF_OpenFontIndexDPIRW(src, freesrc, ptsize, index, 0, 0); +} + +TTF_Font* TTF_OpenFontIndexDPI( const char *file, int ptsize, long index, unsigned int hdpi, unsigned int vdpi ) +{ + SDL_RWops *rw = SDL_RWFromFile(file, "rb"); + if ( rw == NULL ) { + return NULL; + } + return TTF_OpenFontIndexDPIRW(rw, 1, ptsize, index, hdpi, vdpi); +} + TTF_Font* TTF_OpenFontRW( SDL_RWops *src, int freesrc, int ptsize ) { return TTF_OpenFontIndexRW(src, freesrc, ptsize, 0); } +TTF_Font* TTF_OpenFontDPI( const char *file, int ptsize, unsigned int hdpi, unsigned int vdpi ) +{ + return TTF_OpenFontIndexDPI(file, ptsize, 0, hdpi, vdpi); +} + TTF_Font* TTF_OpenFontIndex( const char *file, int ptsize, long index ) { - SDL_RWops *rw = SDL_RWFromFile(file, "rb"); - if ( rw == NULL ) { - return NULL; - } - return TTF_OpenFontIndexRW(rw, 1, ptsize, index); + return TTF_OpenFontIndexDPI(file, ptsize, index, 0, 0); } 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 @@ -85,8 +85,12 @@ * is too high, the last indexed size will be the default. */ extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFont(const char *file, int ptsize); extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndex(const char *file, int ptsize, long index); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontDPI(const char *file, int ptsize, unsigned int hdpi, unsigned int vdpi); extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexDPI(const char *file, int ptsize, long index, unsigned int hdpi, unsigned int vdpi); extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontDPIRW(SDL_RWops *src, int freesrc, int ptsize, unsigned int hdpi, unsigned int vdpi); +extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexDPIRW(SDL_RWops *src, int freesrc, int ptsize, long index, unsigned int hdpi, unsigned int vdpi); /* Set and retrieve the font style */ #define TTF_STYLE_NORMAL 0x00