Skip to content

Commit

Permalink
Added optional DPI-scaling of fonts
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
DavidLudwig committed Dec 28, 2019
1 parent d2cfe8c commit 593fb06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
47 changes: 37 additions & 10 deletions SDL_ttf.c
Expand Up @@ -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;
Expand Down Expand Up @@ -1511,23 +1511,25 @@ TTF_Font* TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long inde
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;
}
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;
Expand Down Expand Up @@ -1567,6 +1569,11 @@ int TTF_SetFontSize(TTF_Font *font, int ptsize)
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)
{
Expand Down Expand Up @@ -1640,18 +1647,38 @@ static int TTF_initFontMetrics(TTF_Font *font)
return 0;
}

TTF_Font* TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize)
TTF_Font* TTF_OpenFontDPIRW( SDL_RWops *src, int freesrc, int ptsize, unsigned int hdpi, unsigned int vdpi )
{
return TTF_OpenFontIndexRW(src, freesrc, ptsize, 0);
return TTF_OpenFontIndexDPIRW(src, freesrc, ptsize, 0, hdpi, vdpi);
}

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_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) {
if ( rw == NULL ) {
return NULL;
}
return TTF_OpenFontIndexRW(rw, 1, ptsize, index);
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)
{
return TTF_OpenFontIndexDPI(file, ptsize, index, 0, 0);
}

TTF_Font* TTF_OpenFont(const char *file, int ptsize)
Expand Down
8 changes: 8 additions & 0 deletions SDL_ttf.h
Expand Up @@ -111,8 +111,16 @@ 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);

/* 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
Expand Down

0 comments on commit 593fb06

Please sign in to comment.