fix: draw non-null terminated strings

This commit is contained in:
Raphael Coeffic
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent 1acea8fc88
commit 34652c4afa
+10 -3
View File
@@ -18,6 +18,8 @@
*/ */
#include <math.h> #include <math.h>
#include <string.h>
#include "bitmapbuffer.h" #include "bitmapbuffer.h"
#include "libopenui_depends.h" #include "libopenui_depends.h"
#include "libopenui_helpers.h" #include "libopenui_helpers.h"
@@ -1068,6 +1070,11 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
if (!s) return x; if (!s) return x;
MOVE_OFFSET(); MOVE_OFFSET();
// LVGL does not handle non-null terminated strings
static char buffer[256];
strncpy(buffer, s, len);
buffer[len] = '\0';
int height = getFontHeight(flags); int height = getFontHeight(flags);
if (y + height <= ymin || y >= ymax) { if (y + height <= ymin || y >= ymax) {
@@ -1094,7 +1101,7 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
} }
lv_point_t p; lv_point_t p;
lv_txt_get_size(&p, s, font, label_draw_dsc.letter_space, lv_txt_get_size(&p, buffer, font, label_draw_dsc.letter_space,
label_draw_dsc.line_space, LV_COORD_MAX, 0); label_draw_dsc.line_space, LV_COORD_MAX, 0);
lv_coord_t lv_x = (lv_coord_t)x; lv_coord_t lv_x = (lv_coord_t)x;
@@ -1118,12 +1125,12 @@ coord_t BitmapBuffer::drawSizedText(coord_t x, coord_t y, const char *s,
} }
if (draw_ctx) { if (draw_ctx) {
lv_draw_label(draw_ctx, &label_draw_dsc, &coords, s, nullptr); lv_draw_label(draw_ctx, &label_draw_dsc, &coords, buffer, nullptr);
} }
#if !defined(BOOT) #if !defined(BOOT)
else if (canvas) { else if (canvas) {
lv_canvas_draw_text(canvas, coords.x1, coords.y1, coords.x2 - coords.x1 + 1, lv_canvas_draw_text(canvas, coords.x1, coords.y1, coords.x2 - coords.x1 + 1,
&label_draw_dsc, s); &label_draw_dsc, buffer);
} }
#endif #endif