Skip to content

Commit

Permalink
Add TTF_SetFontLanguage to set language BCP47 code, (see #308)
Browse files Browse the repository at this point in the history
  • Loading branch information
1bsyl authored and slouken committed Jan 15, 2024
1 parent 7da9d2a commit c4daf98
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/SDL3_ttf/SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,19 @@ extern DECLSPEC int SDLCALL TTF_SetFontDirection(TTF_Font *font, TTF_Direction d
*/
extern DECLSPEC int SDLCALL TTF_SetFontScriptName(TTF_Font *font, const char *script);

/**
* Set language to be used for text shaping by a font.
*
* If SDL_ttf was not built with HarfBuzz support, this function returns -1.
*
* \param font the font to specify a language for.
* \param language_bcp47 a null-terminated string containing the desired language's BCP47 code. Or null to reset the value.
* \returns 0 on success, or -1 on error.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern DECLSPEC int TTF_SetFontLanguage(TTF_Font *font, const char *language_bcp47);

/**
* Query whether a font is scalable or not.
*
Expand Down
21 changes: 21 additions & 0 deletions src/SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ struct _TTF_Font {
hb_script_t hb_script;
/* If HB_DIRECTION_INVALID, use global default direction */
hb_direction_t hb_direction;
hb_language_t hb_language;
#endif
int render_sdf;

Expand Down Expand Up @@ -1867,6 +1868,7 @@ TTF_Font* TTF_OpenFontIndexDPIRW(SDL_RWops *src, SDL_bool freesrc, int ptsize, l
/* By default the script / direction are inherited from global variables */
font->hb_script = HB_SCRIPT_INVALID;
font->hb_direction = HB_DIRECTION_INVALID;
font->hb_language = hb_language_from_string("", -1);
#endif

if (TTF_SetFontSizeDPI(font, ptsize, hdpi, vdpi) < 0) {
Expand Down Expand Up @@ -3111,6 +3113,24 @@ int TTF_SetFontScriptName(TTF_Font *font, const char *script)
#endif
}

int TTF_SetFontLanguage(TTF_Font *font, const char *language_bcp47)
{
#if TTF_USE_HARFBUZZ
TTF_CHECK_POINTER(font, -1);

if (language_bcp47 == NULL) {
font->hb_language = hb_language_from_string("", -1);
} else {
font->hb_language = hb_language_from_string(language_bcp47, -1);
}
return 0;
#else
(void) font;
(void) language_bcp47;
return TTF_SetError("Unsupported");
#endif
}

static int TTF_Size_Internal(TTF_Font *font,
const char *text, const str_type_t str_type,
int *w, int *h, int *xstart, int *ystart,
Expand Down Expand Up @@ -3200,6 +3220,7 @@ static int TTF_Size_Internal(TTF_Font *font,
}

/* Set global configuration */
hb_buffer_set_language(hb_buffer, font->hb_language);
hb_buffer_set_direction(hb_buffer, hb_direction);
hb_buffer_set_script(hb_buffer, hb_script);

Expand Down
1 change: 1 addition & 0 deletions src/SDL_ttf.sym
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ SDL3_ttf_0.0.0 {
TTF_SetFontDirection;
TTF_SetFontHinting;
TTF_SetFontKerning;
TTF_SetFontLanguage;
TTF_SetFontOutline;
TTF_SetFontSDF;
TTF_SetFontScriptName;
Expand Down

0 comments on commit c4daf98

Please sign in to comment.