Skip to content

Commit

Permalink
Add TTF_GetScript to retreive the script to which unicode character b…
Browse files Browse the repository at this point in the history
…elongs
  • Loading branch information
João Paulo committed May 27, 2024
1 parent 3a51231 commit 624d5ac
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
46 changes: 46 additions & 0 deletions SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,52 @@ int TTF_SetFontScriptName(TTF_Font *font, const char *script)
#endif
}

extern DECLSPEC int SDLCALL TTF_GetScript(Uint32 ch, char* script)
{
#if TTF_USE_HARFBUZZ

hb_buffer_t* hb_buffer = hb_buffer_create();

if (hb_buffer == NULL)
{
TTF_SetError("Cannot create harfbuzz buffer");
return -1;
}

if (script == NULL || SDL_strlen(script) != 4)
{
return -1;
}

hb_unicode_funcs_t* hb_unicode_functions = hb_buffer_get_unicode_funcs(hb_buffer);

if (hb_unicode_functions == NULL)
{
TTF_SetError("Cannot get harfbuzz unicode funcs");

hb_buffer_destroy(hb_buffer);
return -1;
}

hb_buffer_clear_contents(hb_buffer);
hb_buffer_set_content_type(hb_buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);

uint8_t const untagged_script[4] = { HB_UNTAG(hb_unicode_script(hb_unicode_functions, ch)) };

script[0] = (char)untagged_script[0];
script[1] = (char)untagged_script[1];
script[2] = (char)untagged_script[2];
script[3] = (char)untagged_script[3];

hb_buffer_destroy(hb_buffer);
return 0;

#else
TTF_SetError("Unsupported");
return -1;
#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
15 changes: 15 additions & 0 deletions SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,21 @@ extern DECLSPEC int SDLCALL TTF_SetFontDirection(TTF_Font *font, TTF_Direction d
*/
extern DECLSPEC int SDLCALL TTF_SetFontScriptName(TTF_Font *font, const char *script);

/**
* Retrieves the script to which unicode character belongs.
*
* The supplied script value must be exactly four characters.
*
* If SDL_ttf was not built with HarfBuzz support, this function returns -1.
*
* \param ch the character code to check.
* \param script the script output string.
* \returns 0 on success, or -1 on error.
*
* \since This function is available since SDL_ttf ?.
*/
extern DECLSPEC int SDLCALL TTF_GetScript(Uint32 ch, char* script);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
Expand Down

0 comments on commit 624d5ac

Please sign in to comment.