1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

integrate Arabic processing in to lv_label

This commit is contained in:
Gabor Kiss-Vamosi 2020-04-17 10:30:11 +02:00
parent bab1d87f2d
commit 9de260d648
5 changed files with 68 additions and 122 deletions

View File

@ -312,7 +312,7 @@ void * lv_mem_realloc(void * data_p, size_t new_size)
if(data_p != NULL) {
/*Copy the old data to the new. Use the smaller size*/
if(old_size != 0) {
memcpy(new_p, data_p, LV_MATH_MIN(new_size, old_size));
lv_memcpy(new_p, data_p, LV_MATH_MIN(new_size, old_size));
lv_mem_free(data_p);
}
}

View File

@ -128,7 +128,8 @@ uint32_t lv_txt_ap_calc_bytes_cnt(const char * txt){
}
void lv_txt_ap_proc(const char * txt,char * txt_out){
void lv_txt_ap_proc(const char * txt,char * txt_out)
{
uint32_t txt_length=0;
uint32_t index_current,idx_next,idx_previous,i,j;
uint32_t * ch_enc;
@ -136,13 +137,15 @@ void lv_txt_ap_proc(const char * txt,char * txt_out){
txt_length = lv_txt_get_encoded_length(txt);
ch_enc = (uint32_t *)lv_mem_alloc(sizeof(uint32_t) * txt_length);
ch_enc = (uint32_t *)lv_mem_alloc(sizeof(uint32_t) * (txt_length + 1));
i=0;
j=0;
while(j<txt_length)
ch_enc[j++] = lv_txt_encoded_next(txt, &i);
ch_enc[j] = 0;
i=0;
idx_previous = LV_UNDEF_ARABIC_PERSIAN_CHARS;
while(i<txt_length){
@ -171,27 +174,23 @@ void lv_txt_ap_proc(const char * txt,char * txt_out){
i++;
}
#if LV_USE_REVERSE_ARABIC_PERSIAN_CHARS == 1
lv_ap_normalize_chars(ch_enc,txt_length);
#endif
txt_out_temp = txt_out;
i=0;
while(i<txt_length){
if((*ch_enc) <=0x7F){
*(txt_out_temp++) = *ch_enc;
}else if((*ch_enc) <=0x7FFF){
*(txt_out_temp++) = 0xC0 | ((*ch_enc>>6) & 0x3F);
*(txt_out_temp++) = 0x80 | (*ch_enc & 0x3F);
if((ch_enc[i]) <=0x7F){
*(txt_out_temp++) = ch_enc[i];
}else if(ch_enc[i] <=0x7FFF){
*(txt_out_temp++) = 0xC0 | ((ch_enc[i]>>6) & 0x3F);
*(txt_out_temp++) = 0x80 | (ch_enc[i] & 0x3F);
}else {
*(txt_out_temp++) = 0xE0 | ((*ch_enc>>12) & 0x3F);
*(txt_out_temp++) = 0x80 | ((*ch_enc>>6) & 0x3F);
*(txt_out_temp++) = 0x80 | (*ch_enc & 0x3F);
*(txt_out_temp++) = 0xE0 | ((ch_enc[i]>>12) & 0x3F);
*(txt_out_temp++) = 0x80 | ((ch_enc[i]>>6) & 0x3F);
*(txt_out_temp++) = 0x80 | (ch_enc[i] & 0x3F);
}
ch_enc++;
i++;
}
*(txt_out_temp++)=0;
*(txt_out_temp) = '\0';
lv_mem_free(ch_enc);
}
/**********************
@ -206,58 +205,4 @@ static uint32_t lv_ap_get_char_index(uint16_t c){
return LV_UNDEF_ARABIC_PERSIAN_CHARS;
}
#if LV_USE_REVERSE_ARABIC_PERSIAN_CHARS == 1
static void lv_ap_normalize_chars(uint32_t * str,uint16_t count){
uint16_t t,u,i=0,j,is_digit_domin=0,digit_domin_s=0,is_ascii_domin=0,ascii_domin_s=0;
while(i<count){
if(str[i]>=0x06F0 && str[i]<=0x06F9){
if(is_digit_domin==0)
digit_domin_s = i;
is_digit_domin = 1;
}else if(is_digit_domin){
for(j=0;j<(i-digit_domin_s)/2;j++){
t = str[j+digit_domin_s];
str[j+digit_domin_s] = str[i-j-1];
str[i-j-1] = t;
}
is_digit_domin = 0;
}
if((str[i] & 0x80) == 0){
if(is_ascii_domin==0)
ascii_domin_s = i;
is_ascii_domin = 1;
}else if(is_ascii_domin){
for(j=0;j<(i-ascii_domin_s)/2;j++){
u = str[j+ascii_domin_s];
str[j+ascii_domin_s] = str[i-j-1];
str[i-j-1] = u;
}
is_ascii_domin = 0;
}
i++;
}
if(is_ascii_domin){
for(j=0;j<(count-ascii_domin_s)/2;j++){
u = str[j+ascii_domin_s];
str[j+ascii_domin_s] = str[i-j-1];
str[i-j-1] = u;
}
}
if(is_digit_domin){
for(j=0;j<(count-digit_domin_s)/2;j++){
t = str[j+digit_domin_s];
str[j+digit_domin_s] = str[i-j-1];
str[i-j-1] = t;
}
}
i=0;
while(i<count/2){
t = str[i];
str[i] = str[count-i-1];
str[count-i-1] = t;
i++;
}
}
#endif
#endif

View File

@ -24,7 +24,7 @@ extern "C" {
* DEFINES
*********************/
#define LV_UNDEF_ARABIC_PERSIAN_CHARS (~(0UL))
#define LV_UNDEF_ARABIC_PERSIAN_CHARS (UINT32_MAX)
#define LV_AP_ALPHABET_BASE_CODE 0x0622
#define LV_AP_END_CHARS_LIST {0,0,0,0,0,{0,0}}
/**********************

View File

@ -196,25 +196,48 @@ void lv_label_set_text(lv_obj_t * label, const char * text)
if(ext->text == text) {
/*If set its own text then reallocate it (maybe its size changed)*/
#if LV_USE_ARABIC_PERSIAN_CHARS
/*Get the size of the text and process it*/
size_t len = lv_txt_ap_calc_bytes_cnt(text);
ext->text = lv_mem_realloc(ext->text, len);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) return;
lv_txt_ap_proc(ext->text, ext->text);
#else
ext->text = lv_mem_realloc(ext->text, strlen(ext->text) + 1);
#endif
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) return;
}
else {
/*Allocate space for the new text*/
size_t len = strlen(text) + 1;
/*Free the old text*/
if(ext->text != NULL && ext->static_txt == 0) {
lv_mem_free(ext->text);
ext->text = NULL;
}
ext->text = lv_mem_alloc(len);
#if LV_USE_ARABIC_PERSIAN_CHARS
/*Get the size of the text and process it*/
size_t len = lv_txt_ap_calc_bytes_cnt(text);
ext->text = lv_mem_alloc(len);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) return;
lv_txt_ap_proc(text, ext->text);
#else
/*Get the size of the text*/
size_t len = strlen(text) + 1;
/*Allocate space for the new text*/
ext->text = lv_mem_alloc(len);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) return;
strcpy(ext->text, text);
#endif
/*Now the text is dynamically allocated*/
ext->static_txt = 0;
@ -248,15 +271,38 @@ void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...)
ext->text = NULL;
}
va_list ap, ap2;
va_start(ap, fmt);
va_copy(ap2, ap);
/*Allocate space for the new text by using trick from C99 standard section 7.19.6.12 */
uint32_t len = lv_vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
#if LV_USE_ARABIC_PERSIAN_CHARS
/*Put together the text according to the format string*/
char * raw_txt = lv_mem_buf_get(len + 1);
LV_ASSERT_MEM(raw_txt);
if(raw_txt == NULL) {
va_end(ap2);
return;
}
lv_vsnprintf(raw_txt, len + 1, fmt, ap2);
/*Get the size of the Arabic text and process it*/
size_t len_ap = lv_txt_ap_calc_bytes_cnt(raw_txt);
ext->text = lv_mem_alloc(len_ap + 1);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) {
va_end(ap2);
return;
}
lv_txt_ap_proc(raw_txt, ext->text);
lv_mem_buf_release(raw_txt);
#else
ext->text = lv_mem_alloc(len + 1);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) {
@ -266,6 +312,7 @@ void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...)
ext->text[len - 1] = 0; /* Ensure NULL termination */
lv_vsnprintf(ext->text, len + 1, fmt, ap2);
#endif
va_end(ap2);
ext->static_txt = 0; /*Now the text is dynamically allocated*/
@ -273,43 +320,6 @@ void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...)
lv_label_refr_text(label);
}
/**
* Set a new text for a label from a character array. The array don't has to be '\0' terminated.
* Memory will be allocated to store the array by the label.
* @param label pointer to a label object
* @param array array of characters or NULL to refresh the label
* @param size the size of 'array' in bytes
*/
void lv_label_set_array_text(lv_obj_t * label, const char * array, uint16_t size)
{
LV_ASSERT_OBJ(label, LV_OBJX_NAME);
lv_obj_invalidate(label);
lv_label_ext_t * ext = lv_obj_get_ext_attr(label);
/*If trying to set its own text or the array is NULL then refresh */
if(array == ext->text || array == NULL) {
lv_label_refr_text(label);
return;
}
/*Allocate space for the new text*/
if(ext->text != NULL && ext->static_txt == 0) {
lv_mem_free(ext->text);
ext->text = NULL;
}
ext->text = lv_mem_alloc(size + 1);
LV_ASSERT_MEM(ext->text);
if(ext->text == NULL) return;
memcpy(ext->text, array, size);
ext->text[size] = '\0';
ext->static_txt = 0; /*Now the text is dynamically allocated*/
lv_label_refr_text(label);
}
/**
* Set a static text. It will not be saved by the label so the 'text' variable
* has to be 'alive' while the label exist.

View File

@ -135,15 +135,6 @@ void lv_label_set_text(lv_obj_t * label, const char * text);
*/
void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...);
/**
* Set a new text for a label from a character array. The array don't has to be '\0' terminated.
* Memory will be allocated to store the array by the label.
* @param label pointer to a label object
* @param array array of characters or NULL to refresh the label
* @param size the size of 'array' in bytes
*/
void lv_label_set_array_text(lv_obj_t * label, const char * array, uint16_t size);
/**
* Set a static text. It will not be saved by the label so the 'text' variable
* has to be 'alive' while the label exist.