# HG changeset patch # User David Ludwig # Date 1577521232 -10800 # 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 72 DPI. Doing so allows fonts to be scaled linearly (at runtime), whereas point-sizes often do not scale linearly. New functions added to SDL_ttf to facilitate this change: TTF_OpenFontDPI TTF_OpenFontIndexDPI TTF_OpenFontDPIRW TTF_OpenFontIndexDPIRW TTF_SetFontSizeDPI To each DPI-enabled call are added two parameters at 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 @@ -1378,7 +1378,7 @@ static unsigned long RWread( 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; @@ -1511,7 +1511,7 @@ TTF_Font* TTF_OpenFontIndexRW(SDL_RWops hb_ft_font_set_load_flags(font->hb_font, FT_LOAD_DEFAULT | font->ft_load_target); #endif - if (TTF_SetFontSize(font, ptsize) < 0) { + if (TTF_SetFontSizeDPI(font, ptsize, hdpi, vdpi) < 0) { TTF_SetFTError("Couldn't set font size", error); TTF_CloseFont(font); return NULL; @@ -1519,15 +1519,17 @@ TTF_Font* TTF_OpenFontIndexRW(SDL_RWops return font; } -int TTF_SetFontSize(TTF_Font *font, int ptsize) +int TTF_SetFontSizeDPI(TTF_Font *font, int ptsize, unsigned int hdpi, unsigned int vdpi) { 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); + /* Set the character size using the provided DPI. If a zero DPI + * is provided, then the other DPI setting will be used. If both + * are zero, then Freetype's default 72 DPI will be used. */ + error = FT_Set_Char_Size(face, 0, ptsize * 64, hdpi, vdpi); if (error) { TTF_SetFTError("Couldn't set font size", error); return -1; @@ -1567,6 +1569,11 @@ int TTF_SetFontSize(TTF_Font *font, int return 0; } +int TTF_SetFontSize(TTF_Font *font, int ptsize) +{ + return TTF_SetFontSizeDPI(font, ptsize, 0, 0); +} + /* Update font parameter depending on a style change */ static int TTF_initFontMetrics(TTF_Font *font) { @@ -1640,18 +1647,38 @@ static int TTF_initFontMetrics(TTF_Font return 0; } +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 @@ -111,8 +111,16 @@ 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 hdpi, unsigned int vdpi); +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_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 font size dynamically */ extern DECLSPEC int SDLCALL TTF_SetFontSize(TTF_Font *font, int ptsize); +extern DECLSPEC int SDLCALL TTF_SetFontSizeDPI(TTF_Font *font, int ptsize, unsigned int hdpi, unsigned int vdpi); /* Set and retrieve the font style */ #define TTF_STYLE_NORMAL 0x00