[A] Add function ft_set_u8strwid_func
to set custom function to compute width of utf8 strings
This commit is contained in:
27
lib/fort.c
27
lib/fort.c
@@ -1821,6 +1821,12 @@ FT_INTERNAL
|
||||
int buffer_printf(f_string_buffer_t *buffer, size_t buffer_row, f_conv_context_t *cntx, size_t cod_width,
|
||||
const char *content_style_tag, const char *reset_content_style_tag);
|
||||
|
||||
#ifdef FT_HAVE_UTF8
|
||||
FT_INTERNAL
|
||||
void buffer_set_u8strwid_func(int (*u8strwid)(const void *beg, const void *end, size_t *width));
|
||||
#endif /* FT_HAVE_UTF8 */
|
||||
|
||||
|
||||
#endif /* STRING_BUFFER_H */
|
||||
|
||||
/********************************************************
|
||||
@@ -3528,6 +3534,12 @@ const void *ft_to_u8string(const ft_table_t *table)
|
||||
{
|
||||
return (const void *)ft_to_string_impl(table, UTF8_BUF);
|
||||
}
|
||||
|
||||
void ft_set_u8strwid_func(int (*u8strwid)(const void *beg, const void *end, size_t *width))
|
||||
{
|
||||
buffer_set_u8strwid_func(u8strwid);
|
||||
}
|
||||
|
||||
#endif /* FT_HAVE_UTF8 */
|
||||
|
||||
/********************************************************
|
||||
@@ -6300,9 +6312,24 @@ size_t string_buffer_raw_capacity(const f_string_buffer_t *buffer)
|
||||
}
|
||||
|
||||
#ifdef FT_HAVE_UTF8
|
||||
/* User provided function to compute utf8 string visible width */
|
||||
static int (*_custom_u8strwid)(const void *beg, const void *end, size_t *width) = NULL;
|
||||
|
||||
FT_INTERNAL
|
||||
void buffer_set_u8strwid_func(int (*u8strwid)(const void *beg, const void *end, size_t *width))
|
||||
{
|
||||
_custom_u8strwid = u8strwid;
|
||||
}
|
||||
|
||||
static
|
||||
size_t utf8_width(const void *beg, const void *end)
|
||||
{
|
||||
if (_custom_u8strwid) {
|
||||
size_t width = 0;
|
||||
if (!_custom_u8strwid(beg, end, &width))
|
||||
return width;
|
||||
}
|
||||
|
||||
size_t sz = (size_t)((const char *)end - (const char *)beg);
|
||||
char *tmp = (char *)F_MALLOC(sizeof(char) * (sz + 1));
|
||||
// @todo: add check to tmp
|
||||
|
21
lib/fort.h
21
lib/fort.h
@@ -915,6 +915,27 @@ int ft_u8printf_ln(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_
|
||||
|
||||
const void *ft_to_u8string(const ft_table_t *table);
|
||||
|
||||
/**
|
||||
* Set custom function to compute visible width of utf8 string.
|
||||
*
|
||||
* libfort internally has a very simple logic to compute visible width of utf8
|
||||
* strings. It considers that each codepoint will occupy one position on the
|
||||
* terminal in case of monowidth font (some east asians wide and fullwidth
|
||||
* characters (see http://www.unicode.org/reports/tr11/tr11-33.html) will occupy
|
||||
* 2 positions). This logic is very simple and covers wide range of cases. But
|
||||
* obviously there a lot of cases when it is not sufficient. In such cases user
|
||||
* should use some external libraries and provide an appropriate function to
|
||||
* libfort.
|
||||
*
|
||||
* @param u8strwid
|
||||
* User provided function to evaluate width of utf8 string ( beg - start of
|
||||
* utf8 string, end - end of utf8 string (not included), width - pointer to
|
||||
* the result). If function succeed it should return 0, otherwise some non-
|
||||
* zero value. If function returns nonzero value libfort fallbacks to default
|
||||
* internal algorithm.
|
||||
*/
|
||||
void ft_set_u8strwid_func(int (*u8strwid)(const void *beg, const void *end, size_t *width));
|
||||
|
||||
#endif /* FT_HAVE_UTF8 */
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user