Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TTF_GetScript to retreive the script to which unicode character belongs #312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,46 @@ int TTF_SetFontScriptName(TTF_Font *font, const char *script)
#endif
}

int TTF_GetScriptName(Uint32 ch, char *script, size_t script_size)
{
#if TTF_USE_HARFBUZZ

TTF_CHECK_POINTER(script, -1);
if (script_size < 5) {
return TTF_SetError("Insufficient script buffer size");
}

hb_buffer_t* hb_buffer = hb_buffer_create();

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

hb_unicode_funcs_t* hb_unicode_functions = hb_buffer_get_unicode_funcs(hb_buffer);

if (hb_unicode_functions == NULL) {
hb_buffer_destroy(hb_buffer);
return TTF_SetError("Cannot get harfbuzz unicode funcs");
}

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];
sezero marked this conversation as resolved.
Show resolved Hide resolved
script[4] = '\0';

hb_buffer_destroy(hb_buffer);
return 0;

j9hnny97 marked this conversation as resolved.
Show resolved Hide resolved
#else
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
18 changes: 18 additions & 0 deletions SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,24 @@ extern DECLSPEC int SDLCALL TTF_SetFontDirection(TTF_Font *font, TTF_Direction d
*/
extern DECLSPEC int SDLCALL TTF_SetFontScriptName(TTF_Font *font, const char *script);

/**
* Query the script to which unicode character belongs.
*
* The supplied script pointer should be able to hold four characters and
* the null-terminator.
*
* If SDL_ttf was not built with HarfBuzz support, this function returns -1.
*
* \param ch the character to check.
* \param script on return, filled in with the the script as a null-terminated
* string of exactly 4 characters
* \param script_size size of the script buffer, must be at least 5 (see above.)
* \returns 0 on success, or -1 on error.
*
* \since This function is available since SDL_ttf 2.23.0.
*/
extern DECLSPEC int SDLCALL TTF_GetScriptName(Uint32 ch, char *script, size_t script_size);

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