diff --git a/applications/debug/example_custom_font/example_custom_font.c b/applications/debug/example_custom_font/example_custom_font.c index 8d85f23c6..334aa8aa8 100644 --- a/applications/debug/example_custom_font/example_custom_font.c +++ b/applications/debug/example_custom_font/example_custom_font.c @@ -72,9 +72,9 @@ static void app_draw_callback(Canvas* canvas, void* ctx) { canvas_set_custom_u8g2_font(canvas, u8g2_font_4x6_t_cyrillic); - canvas_draw_str(canvas, 0, 6, "This is a tiny custom font"); - canvas_draw_str(canvas, 0, 12, "012345.?! ,:;\"\'@#$%"); - canvas_draw_str(canvas, 0, 18, "И немного юникода"); + canvas_draw_utf8_str(canvas, 0, 6, "This is a tiny custom font"); + canvas_draw_utf8_str(canvas, 0, 12, "012345.?! ,:;\"\'@#$%"); + canvas_draw_utf8_str(canvas, 0, 18, "И немного юникода"); } static void app_input_callback(InputEvent* input_event, void* ctx) { diff --git a/applications/services/gui/canvas.c b/applications/services/gui/canvas.c index f8f33da9b..3cd35e402 100644 --- a/applications/services/gui/canvas.c +++ b/applications/services/gui/canvas.c @@ -154,6 +154,14 @@ void canvas_set_custom_u8g2_font(Canvas* canvas, const uint8_t* font) { } void canvas_draw_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str) { + furi_assert(canvas); + if(!str) return; + x += canvas->offset_x; + y += canvas->offset_y; + u8g2_DrawStr(&canvas->fb, x, y, str); +} + +void canvas_draw_utf8_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str) { furi_assert(canvas); if(!str) return; x += canvas->offset_x; @@ -173,6 +181,49 @@ void canvas_draw_str_aligned( x += canvas->offset_x; y += canvas->offset_y; + switch(horizontal) { + case AlignLeft: + break; + case AlignRight: + x -= u8g2_GetStrWidth(&canvas->fb, str); + break; + case AlignCenter: + x -= (u8g2_GetStrWidth(&canvas->fb, str) / 2); + break; + default: + furi_crash(); + break; + } + + switch(vertical) { + case AlignTop: + y += u8g2_GetAscent(&canvas->fb); + break; + case AlignBottom: + break; + case AlignCenter: + y += (u8g2_GetAscent(&canvas->fb) / 2); + break; + default: + furi_crash(); + break; + } + + u8g2_DrawStr(&canvas->fb, x, y, str); +} + +void canvas_draw_utf8_str_aligned( + Canvas* canvas, + uint8_t x, + uint8_t y, + Align horizontal, + Align vertical, + const char* str) { + furi_assert(canvas); + if(!str) return; + x += canvas->offset_x; + y += canvas->offset_y; + switch(horizontal) { case AlignLeft: break; @@ -205,6 +256,12 @@ void canvas_draw_str_aligned( } uint16_t canvas_string_width(Canvas* canvas, const char* str) { + furi_assert(canvas); + if(!str) return 0; + return u8g2_GetStrWidth(&canvas->fb, str); +} + +uint16_t canvas_utf8_string_width(Canvas* canvas, const char* str) { furi_assert(canvas); if(!str) return 0; return u8g2_GetUTF8Width(&canvas->fb, str); diff --git a/applications/services/gui/canvas.h b/applications/services/gui/canvas.h index bda730ff2..afed58548 100644 --- a/applications/services/gui/canvas.h +++ b/applications/services/gui/canvas.h @@ -187,6 +187,15 @@ void canvas_set_custom_u8g2_font(Canvas* canvas, const uint8_t* font); */ void canvas_draw_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str); +/** Draw UTF8 string at position of baseline defined by x, y. + * + * @param canvas Canvas instance + * @param x anchor point x coordinate + * @param y anchor point y coordinate + * @param str C-string + */ +void canvas_draw_utf8_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str); + /** Draw aligned string defined by x, y. * * Align calculated from position of baseline, string width and ascent (height @@ -207,6 +216,26 @@ void canvas_draw_str_aligned( Align vertical, const char* str); +/** Draw aligned UTF8 string defined by x, y. + * + * Align calculated from position of baseline, string width and ascent (height + * of the glyphs above the baseline) + * + * @param canvas Canvas instance + * @param x anchor point x coordinate + * @param y anchor point y coordinate + * @param horizontal horizontal alignment + * @param vertical vertical alignment + * @param str C-string + */ +void canvas_draw_utf8_str_aligned( + Canvas* canvas, + uint8_t x, + uint8_t y, + Align horizontal, + Align vertical, + const char* str); + /** Get string width * * @param canvas Canvas instance @@ -216,6 +245,15 @@ void canvas_draw_str_aligned( */ uint16_t canvas_string_width(Canvas* canvas, const char* str); +/** Get UTF8 string width + * + * @param canvas Canvas instance + * @param str C-string + * + * @return width in pixels. + */ +uint16_t canvas_utf8_string_width(Canvas* canvas, const char* str); + /** Get glyph width * * @param canvas Canvas instance diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index 3ab58f58d..afce83fe5 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -735,6 +735,8 @@ Function,+,canvas_draw_rframe,void,"Canvas*, uint8_t, uint8_t, uint8_t, uint8_t, Function,+,canvas_draw_str,void,"Canvas*, uint8_t, uint8_t, const char*" Function,+,canvas_draw_str_aligned,void,"Canvas*, uint8_t, uint8_t, Align, Align, const char*" Function,+,canvas_draw_triangle,void,"Canvas*, uint8_t, uint8_t, uint8_t, uint8_t, CanvasDirection" +Function,+,canvas_draw_utf8_str,void,"Canvas*, uint8_t, uint8_t, const char*" +Function,+,canvas_draw_utf8_str_aligned,void,"Canvas*, uint8_t, uint8_t, Align, Align, const char*" Function,+,canvas_draw_xbm,void,"Canvas*, uint8_t, uint8_t, uint8_t, uint8_t, const uint8_t*" Function,+,canvas_get_font_params,const CanvasFontParameters*,"const Canvas*, Font" Function,+,canvas_glyph_width,uint8_t,"Canvas*, uint16_t" @@ -747,6 +749,7 @@ Function,+,canvas_set_custom_u8g2_font,void,"Canvas*, const uint8_t*" Function,+,canvas_set_font,void,"Canvas*, Font" Function,+,canvas_set_font_direction,void,"Canvas*, CanvasDirection" Function,+,canvas_string_width,uint16_t,"Canvas*, const char*" +Function,+,canvas_utf8_string_width,uint16_t,"Canvas*, const char*" Function,+,canvas_width,uint8_t,const Canvas* Function,-,cbrt,double,double Function,-,cbrtf,float,float