Closed KaliStudio closed 3 months ago
Problem solved
void elements_text_box(
Canvas* canvas,
uint8_t x,
uint8_t y,
uint8_t width,
uint8_t height,
Align horizontal,
Align vertical,
const char* text,
bool strip_to_dots) {
furi_assert(canvas);
ElementTextBoxLine line[ELEMENTS_MAX_LINES_NUM];
bool bold = false;
bool mono = false;
bool inverse = false;
bool inverse_present = false;
Font current_font = FontSecondary;
Font prev_font = FontSecondary;
const CanvasFontParameters* font_params = canvas_get_font_params(canvas, current_font);
// Fill line parameters
uint8_t line_leading_min = font_params->leading_min;
uint8_t line_leading_default = font_params->leading_default;
uint8_t line_height = font_params->height;
uint8_t line_descender = font_params->descender;
uint8_t line_num = 0;
uint8_t line_width = 0;
uint8_t line_len = 0;
uint8_t total_height_min = 0;
uint8_t total_height_default = 0;
uint16_t i = 0;
bool full_text_processed = false;
uint16_t dots_width = canvas_string_width(canvas, "...");
canvas_set_font(canvas, FontSecondary);
// Fill all lines
line[0].text = text;
for(i = 0; !full_text_processed; i++) {
line_len++;
// Identify line height
if(prev_font != current_font) {
font_params = canvas_get_font_params(canvas, current_font);
line_leading_min = MAX(line_leading_min, font_params->leading_min);
line_leading_default = MAX(line_leading_default, font_params->leading_default);
line_height = MAX(line_height, font_params->height);
line_descender = MAX(line_descender, font_params->descender);
prev_font = current_font;
}
// Set the font
if(text[i] == '\e' && text[i + 1]) {
i++;
line_len++;
if(text[i] == ELEMENTS_BOLD_MARKER) {
if(bold) {
current_font = FontSecondary;
} else {
current_font = FontPrimary;
}
canvas_set_font(canvas, current_font);
bold = !bold;
}
if(text[i] == ELEMENTS_MONO_MARKER) {
if(mono) {
current_font = FontSecondary;
} else {
current_font = FontKeyboard;
}
canvas_set_font(canvas, FontKeyboard);
mono = !mono;
}
if(text[i] == ELEMENTS_INVERSED_MARKER) {
inverse_present = true;
}
continue;
}
if(text[i] != '\n') {
line_width += canvas_glyph_width(canvas, text[i]);
}
// Process new line
if(text[i] == '\n' || text[i] == '\0' || line_width > width) {
if(line_width > width) {
line_width -= canvas_glyph_width(canvas, text[i--]);
line_len--;
}
if(text[i] == '\0') {
full_text_processed = true;
}
if(inverse_present) {
line_leading_min += 1;
line_leading_default += 1;
inverse_present = false;
}
line[line_num].leading_min = line_leading_min;
line[line_num].leading_default = line_leading_default;
line[line_num].height = line_height;
line[line_num].descender = line_descender;
if(total_height_min + line_leading_min > height) {
break;
}
total_height_min += line_leading_min;
total_height_default += line_leading_default;
line[line_num].len = line_len;
if(horizontal == AlignCenter) {
line[line_num].x = x + (width - line_width) / 2;
} else if(horizontal == AlignRight) {
line[line_num].x = x + (width - line_width);
} else {
line[line_num].x = x;
}
line[line_num].y = total_height_min;
line_num++;
if(text[i + 1]) {
line[line_num].text = &text[i + 1];
}
line_leading_min = font_params->leading_min;
line_height = font_params->height;
line_descender = font_params->descender;
line_width = 0;
line_len = 0;
}
}
// Set vertical alignment for all lines
if(total_height_default < height) {
if(vertical == AlignTop) {
line[0].y = y + line[0].height;
} else if(vertical == AlignCenter) {
line[0].y = y + line[0].height + (height - total_height_default) / 2;
} else if(vertical == AlignBottom) {
line[0].y = y + line[0].height + (height - total_height_default);
}
if(line_num > 1) {
for(uint8_t i = 1; i < line_num; i++) {
line[i].y = line[i - 1].y + line[i - 1].leading_default;
}
}
} else if(line_num > 1) {
uint8_t free_pixel_num = height - total_height_min;
uint8_t fill_pixel = 0;
uint8_t j = 1;
line[0].y = y + line[0].height;
while(fill_pixel < free_pixel_num) {
line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
fill_pixel++;
j = j % (line_num - 1) + 1;
}
}
// Draw line by line
canvas_set_font(canvas, FontSecondary);
bold = false;
mono = false;
inverse = false;
for (uint8_t i = 0; i < line_num; i++) {
size_t j = 0;
while (j < line[i].len) {
uint32_t unicode_value = 0;
// Process UTF-8 multibyte character
uint8_t first_byte = (uint8_t)line[i].text[j++];
int additional_bytes = 0;
// Determine the number of additional bytes
if ((first_byte & 0xE0) == 0xC0) {
additional_bytes = 1;
} else if ((first_byte & 0xF0) == 0xE0) {
additional_bytes = 2;
} else if ((first_byte & 0xF8) == 0xF0) {
additional_bytes = 3;
}
// Extract the rest of the bytes
unicode_value = (first_byte & (0xFF >> (additional_bytes + 1)));
for (int k = 0; k < additional_bytes; k++) {
unicode_value = (unicode_value << 6) | (line[i].text[j++] & 0x3F);
}
if(unicode_value == ELEMENTS_BOLD_MARKER) {
if(bold) {
current_font = FontSecondary;
} else {
current_font = FontPrimary;
}
canvas_set_font(canvas, current_font);
bold = !bold;
continue;
}
if(unicode_value == ELEMENTS_MONO_MARKER) {
if(mono) {
current_font = FontSecondary;
} else {
current_font = FontKeyboard;
}
canvas_set_font(canvas, current_font);
mono = !mono;
continue;
}
if(unicode_value == ELEMENTS_INVERSED_MARKER) {
inverse = !inverse;
continue;
}
if(inverse) {
canvas_draw_box(
canvas,
line[i].x - 1,
line[i].y - line[i].height - 1,
canvas_glyph_width(canvas, unicode_value) + 1,
line[i].height + line[i].descender + 2);
canvas_invert_color(canvas);
canvas_draw_glyph(canvas, line[i].x, line[i].y, unicode_value);
canvas_invert_color(canvas);
} else {
if ((i == line_num - 1) && strip_to_dots) {
uint8_t next_symbol_width = canvas_glyph_width(canvas, unicode_value);
if (line[i].x + next_symbol_width + dots_width > x + width) {
canvas_draw_str(canvas, line[i].x, line[i].y, "...");
break;
}
}
canvas_draw_glyph(canvas, line[i].x, line[i].y, unicode_value);
}
line[i].x += canvas_glyph_width(canvas, unicode_value);
}
}
canvas_set_font(canvas, FontSecondary);
}
Description of the feature you're suggesting.
I would like to replace canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]); by canvas_draw_str(canvas, line[i].x, line[i].y, &line[i].text[j]); in applications\services\gui\elements.c for special character support, the problem is that the characters are there, but I have duplicate text and/or bolding
Anything else?
No response