diff --git a/src/lv_misc/lv_mem.c b/src/lv_misc/lv_mem.c index 7f2037371..5e510efbf 100644 --- a/src/lv_misc/lv_mem.c +++ b/src/lv_misc/lv_mem.c @@ -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); } } diff --git a/src/lv_misc/lv_txt_ap.c b/src/lv_misc/lv_txt_ap.c index 6b0325fbe..4db491e01 100644 --- a/src/lv_misc/lv_txt_ap.c +++ b/src/lv_misc/lv_txt_ap.c @@ -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>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=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(itext == 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. diff --git a/src/lv_widgets/lv_label.h b/src/lv_widgets/lv_label.h index 0b676eb4d..dc1f425c4 100644 --- a/src/lv_widgets/lv_label.h +++ b/src/lv_widgets/lv_label.h @@ -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.