What changed, and why it matters
This commit is a performance optimization for drawing text on the BitBox02 hardware wallet screen. It makes text rendering faster by reducing repeated work and changing how tabs are displayed (now a single space width instead of double). The changes are accompanied by new unit tests. There is no clear security vulnerability here, but any change to low-level screen drawing code carries a small risk of visual glitches or unexpected behavior.
Review the optimized glyph drawing path for off-by-one errors, clipping correctness, and behavior with unusual glyph metrics. Run the new unit tests and verify on-device text rendering still matches expectations, especially for password/mask fonts and tabs. No immediate security response is indicated.
Security signals we found
Low-level rendering code changed, which always warrants careful review for memory safety and correctness
Tab rendering behavior changed from blank double-width to visible single-width space
New unit tests added to verify glyph rendering edge cases and cmap validation
Evidence from the diff
The patch refactors the BitBox02 firmware’s text renderer in src/ui/ugui/ugui.c and src/ui/fonts/lvgl_font_compat.c. It validates the selected LVGL character map only once, reuses the resolved glyph descriptor for measuring and drawing, and renders glyphs in one pass inside their character cell when possible, falling back to the general renderer for overhanging glyphs. It also changes tab handling from a blank double-width space to a visible single-width space glyph. The commit adds unit tests covering tab behavior, packed glyph rows, overhanging glyphs, single glyph resolution per string character, and cmap validation. No security-relevant weakness is evident in the diff.
Changed components
src/ui/fonts/lvgl_font_compat.csrc/ui/ugui/ugui.ctest/unit-test/test_ugui.cInspect captured patch +285 / −40
### src/ui/fonts/lvgl_font_compat.c
@@ -14,13 +14,6 @@ static bool _font_dsc_supported(const lv_font_fmt_txt_dsc_t* font_dsc)
return false;
}
- for (uint16_t i = 0; i < font_dsc->cmap_num; i++) {
- const lv_font_fmt_txt_cmap_t* cmap = &font_dsc->cmaps[i];
- if (cmap->type != LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY || cmap->unicode_list != NULL ||
- cmap->glyph_id_ofs_list != NULL || cmap->list_length != 0) {
- return false;
- }
- }
return true;
}
@@ -34,6 +27,10 @@ static uint32_t _get_glyph_id(const lv_font_fmt_txt_dsc_t* font_dsc, uint32_t co
const lv_font_fmt_txt_cmap_t* cmap = &font_dsc->cmaps[i];
uint32_t relative_codepoint = codepoint - cmap->range_start;
if (relative_codepoint < cmap->range_length) {
+ if (cmap->type != LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY || cmap->unicode_list != NULL ||
+ cmap->glyph_id_ofs_list != NULL || cmap->list_length != 0) {
+ return 0;
+ }
return cmap->glyph_id_start + relative_codepoint;
}
}
@@ -51,8 +48,7 @@ __attribute__((weak)) bool lv_font_get_glyph_dsc_fmt_txt(
return false;
}
- bool is_tab = unicode_letter == '\t';
- if (is_tab) {
+ if (unicode_letter == '\t') {
unicode_letter = ' ';
}
@@ -80,10 +76,6 @@ __attribute__((weak)) bool lv_font_get_glyph_dsc_fmt_txt(
dsc_out->is_placeholder = false;
dsc_out->gid.index = glyph_id;
- if (is_tab) {
- dsc_out->adv_w *= 2;
- }
-
return true;
}
### src/ui/ugui/ugui.c
@@ -169,45 +169,82 @@ static bool _UG_GlyphPixelSet(
return ((bitmap[bit_index / 8] >> (7 - (bit_index % 8))) & 0x01) != 0;
}
-static void _UG_PutCodepoint( uint32_t codepoint, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc,
- const UG_FONT *font)
-{
- lv_font_glyph_dsc_t glyph_dsc;
- if (!_UG_GetGlyph(font, codepoint, &glyph_dsc)) {
+// line_height must come from the selected font, even when the glyph uses a fallback font.
+static void _UG_DrawGlyph(
+ lv_font_glyph_dsc_t* glyph_dsc, UG_S32 line_height,
+ UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc)
+{
+ const lv_font_t* resolved_font = glyph_dsc->resolved_font;
+ if (resolved_font == NULL || glyph_dsc->format != LV_FONT_GLYPH_FORMAT_A1 ||
+ glyph_dsc->stride != 0) {
return;
}
- const lv_font_t* resolved_font = glyph_dsc.resolved_font;
- if (resolved_font == NULL || glyph_dsc.format != LV_FONT_GLYPH_FORMAT_A1 ||
- glyph_dsc.stride != 0) {
+
+ if (x + glyph_dsc->adv_w < 0 || x > gui->x_dim) {
return;
}
- if (x + glyph_dsc.adv_w < 0 || x > gui->x_dim) {
+ const uint8_t* bitmap = NULL;
+ if (glyph_dsc->box_w != 0 && glyph_dsc->box_h != 0 &&
+ resolved_font->get_glyph_bitmap != NULL) {
+ bitmap = (const uint8_t*)resolved_font->get_glyph_bitmap(glyph_dsc, NULL);
+ }
+ UG_S16 glyph_y =
+ line_height - resolved_font->base_line - glyph_dsc->box_h - glyph_dsc->ofs_y;
+ if (bitmap != NULL && glyph_dsc->ofs_x >= 0 && glyph_y >= 0 &&
+ glyph_dsc->ofs_x + glyph_dsc->box_w <= glyph_dsc->adv_w &&
+ glyph_y + glyph_dsc->box_h <= line_height) {
+ const int advance = glyph_dsc->adv_w;
+ const int left = glyph_dsc->ofs_x;
+ const int right = left + glyph_dsc->box_w;
+ const int bottom = glyph_y + glyph_dsc->box_h;
+ int row = 0;
+ for (; row < glyph_y; row++) {
+ for (int col = 0; col < advance; col++) {
+ _UG_PSet(x + col, y + row, bc);
+ }
+ }
+ unsigned bits = 0;
+ unsigned mask = 0;
+ for (; row < bottom; row++) {
+ int col = 0;
+ for (; col < left; col++) {
+ _UG_PSet(x + col, y + row, bc);
+ }
+ for (; col < right; col++) {
+ if (mask == 0) {
+ bits = *bitmap++;
+ mask = 0x80;
+ }
+ _UG_PSet(x + col, y + row, bits & mask ? fc : bc);
+ mask >>= 1;
+ }
+ for (; col < advance; col++) {
+ _UG_PSet(x + col, y + row, bc);
+ }
+ }
+ for (; row < line_height; row++) {
+ for (int col = 0; col < advance; col++) {
+ _UG_PSet(x + col, y + row, bc);
+ }
+ }
return;
}
- for (UG_S16 row = 0; row < font->line_height; row++) {
- for (UG_U16 col = 0; col < glyph_dsc.adv_w; col++) {
+ for (UG_S16 row = 0; row < line_height; row++) {
+ for (UG_U16 col = 0; col < glyph_dsc->adv_w; col++) {
_UG_PSet(x + col, y + row, bc);
}
}
- if (codepoint == '\t' || glyph_dsc.box_w == 0 || glyph_dsc.box_h == 0 ||
- resolved_font->get_glyph_bitmap == NULL) {
- return;
- }
-
- const uint8_t* bitmap = (const uint8_t*)resolved_font->get_glyph_bitmap(&glyph_dsc, NULL);
if (bitmap == NULL) {
return;
}
- UG_S16 glyph_y =
- font->line_height - resolved_font->base_line - glyph_dsc.box_h - glyph_dsc.ofs_y;
- for (UG_U16 row = 0; row < glyph_dsc.box_h; row++) {
- for (UG_U16 col = 0; col < glyph_dsc.box_w; col++) {
- UG_S16 xo = x + glyph_dsc.ofs_x + col;
+ for (UG_U16 row = 0; row < glyph_dsc->box_h; row++) {
+ for (UG_U16 col = 0; col < glyph_dsc->box_w; col++) {
+ UG_S16 xo = x + glyph_dsc->ofs_x + col;
UG_S16 yo = y + glyph_y + row;
- if (_UG_GlyphPixelSet(bitmap, &glyph_dsc, col, row)) {
+ if (_UG_GlyphPixelSet(bitmap, glyph_dsc, col, row)) {
_UG_PSet(xo, yo, fc);
}
}
@@ -221,6 +258,7 @@ static void _UG_PutString( UG_S16 x, UG_S16 y, UG_S16 *xout, UG_S16 *yout, const
UG_S16 xp, yp;
UG_U16 cw;
+ lv_font_glyph_dsc_t glyph;
uint32_t codepoint;
UG_S16 max_x = x;
@@ -234,7 +272,7 @@ static void _UG_PutString( UG_S16 x, UG_S16 y, UG_S16 *xout, UG_S16 *yout, const
if (cursor == NULL) {
Abort("Invalid UTF-8 string");
}
- if (codepoint != '\n' && !UG_GetCharWidth(gui->font, codepoint, &cw)) {
+ if (codepoint != '\n' && !_UG_GetGlyph(gui->font, codepoint, &glyph)) {
continue;
}
if ( codepoint == '\n' ) {
@@ -247,14 +285,15 @@ static void _UG_PutString( UG_S16 x, UG_S16 y, UG_S16 *xout, UG_S16 *yout, const
continue;
}
+ cw = glyph.adv_w;
if ( autobreak == 1 && xp + cw > gui->x_dim - 1 ) {
xp = x;
yp += gui->font->line_height + gui->char_v_space;
}
if (!calconly) {
- _UG_PutCodepoint(
- codepoint, xp, yp, gui->fore_color, gui->back_color, gui->font);
+ _UG_DrawGlyph(
+ &glyph, gui->font->line_height, xp, yp, gui->fore_color, gui->back_color);
}
xp += cw + gui->char_h_space;
@@ -866,7 +905,10 @@ void UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc )
{
ASSERT(gui != NULL);
- _UG_PutCodepoint((UG_U8)chr, x, y, fc, bc, gui->font);
+ lv_font_glyph_dsc_t glyph;
+ if (_UG_GetGlyph(gui->font, (UG_U8)chr, &glyph)) {
+ _UG_DrawGlyph(&glyph, gui->font->line_height, x, y, fc, bc);
+ }
}
void UG_SetForecolor( UG_COLOR c )
### test/unit-test/test_ugui.c
@@ -321,6 +321,41 @@ static void _set_glyph_pixel(UG_S16 x, UG_S16 y, UG_COLOR color)
_glyph_pixels[y][x] = color == C_WHITE;
}
+static void _test_ugui_tab_as_space(void** state)
+{
+ (void)state;
+ const UG_FONT* fonts[] = {
+ &font_regular_9,
+ &font_regular_11,
+ &font_regular_12,
+ &font_monogram_16,
+ &font_password_9,
+ &font_password_12,
+ };
+ for (size_t i = 0; i < sizeof(fonts) / sizeof(*fonts); i++) {
+ UG_Init(&gui, _set_glyph_pixel, fonts[i], 32, 32);
+ UG_FontSetHSpace(0);
+ UG_U16 space_width = 0;
+ assert_true(UG_GetCharWidth(fonts[i], ' ', &space_width));
+ UG_S16 width = 0;
+ UG_MeasureStringNoBreak(&width, NULL, "\t");
+ assert_int_equal(width, space_width);
+
+ uint8_t expected[32][32];
+ memset(_glyph_pixels, 0x55, sizeof(_glyph_pixels));
+ UG_PutChar(' ', 8, 8, C_WHITE, C_BLACK);
+ memcpy(expected, _glyph_pixels, sizeof(expected));
+
+ memset(_glyph_pixels, 0x55, sizeof(_glyph_pixels));
+ UG_PutStringNoBreak(8, 8, "\t");
+ assert_memory_equal(_glyph_pixels, expected, sizeof(expected));
+
+ memset(_glyph_pixels, 0x55, sizeof(_glyph_pixels));
+ UG_PutChar('\t', 8, 8, C_WHITE, C_BLACK);
+ assert_memory_equal(_glyph_pixels, expected, sizeof(expected));
+ }
+}
+
static uint32_t _hash_bytes(uint32_t hash, const uint8_t* bytes, size_t len)
{
for (size_t i = 0; i < len; i++) {
@@ -364,6 +399,177 @@ static void _test_ugui_ascii_font_pixels_and_metrics(void** state)
}
}
+static void _draw_test_glyph(void* ctx)
+{
+ (void)ctx;
+ UG_PutStringNoBreak(2, 3, "A");
+}
+
+static void _test_ugui_packed_glyph_rows(void** state)
+{
+ (void)state;
+ static const uint8_t bitmap_1[] = {0xa0};
+ static const uint8_t bitmap_7[] = {0x82, 0x88, 0xa0};
+ static const uint8_t bitmap_8[] = {0x81, 0x42, 0x24};
+ static const uint8_t bitmap_9[] = {0x80, 0xa0, 0x88, 0x80};
+ static const uint8_t bitmap_15[] = {0x80, 0x02, 0x80, 0x08, 0x80, 0x20};
+ static const uint8_t bitmap_16[] = {0x80, 0x01, 0x40, 0x02, 0x20, 0x04};
+ static const uint8_t bitmap_17[] = {0x80, 0x00, 0xa0, 0x00, 0x88, 0x00, 0x80};
+ static const struct {
+ const uint8_t* bitmap;
+ const char* rows[3];
+ } cases[] = {
+ {bitmap_1, {"#", ".", "#"}},
+ {bitmap_7, {"#.....#", ".#...#.", "..#.#.."}},
+ {bitmap_8, {"#......#", ".#....#.", "..#..#.."}},
+ {bitmap_9, {"#.......#", ".#.....#.", "..#...#.."}},
+ {bitmap_15, {"#.............#", ".#...........#.", "..#.........#.."}},
+ {bitmap_16, {"#..............#", ".#............#.", "..#..........#.."}},
+ {bitmap_17, {"#...............#", ".#.............#.", "..#...........#.."}},
+ };
+ static const lv_font_fmt_txt_cmap_t cmap = {
+ .range_start = 'A',
+ .range_length = 1,
+ .glyph_id_start = 1,
+ .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY,
+ };
+ static lv_font_fmt_txt_glyph_dsc_t glyphs[2];
+ static lv_font_fmt_txt_dsc_t font_dsc = {
+ .glyph_dsc = glyphs,
+ .cmaps = &cmap,
+ .cmap_num = 1,
+ .bpp = 1,
+ };
+ static const UG_FONT font = {
+ .get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt,
+ .get_glyph_bitmap = lv_font_get_bitmap_fmt_txt,
+ .line_height = 5,
+ .dsc = &font_dsc,
+ };
+ for (size_t i = 0; i < sizeof(cases) / sizeof(*cases); i++) {
+ const int width = strlen(cases[i].rows[0]);
+ font_dsc.glyph_bitmap = cases[i].bitmap;
+ glyphs[1] = (lv_font_fmt_txt_glyph_dsc_t){
+ .adv_w = (width + 2) * 16,
+ .box_w = width,
+ .box_h = 3,
+ .ofs_x = 1,
+ .ofs_y = 1,
+ };
+ for (int inverted = 0; inverted < 2; inverted++) {
+ for (int rotated = 0; rotated < 2; rotated++) {
+ UG_Init(&gui, _set_glyph_pixel, &font, 32, 32);
+ UG_SetForecolor(inverted ? C_BLACK : C_WHITE);
+ UG_SetBackcolor(inverted ? C_WHITE : C_BLACK);
+ memset(_glyph_pixels, 0x55, sizeof(_glyph_pixels));
+ if (rotated) {
+ UG_RenderRotated180(0, 0, 32, 32, _draw_test_glyph, NULL);
+ } else {
+ _draw_test_glyph(NULL);
+ }
+ for (int y = 0; y < 32; y++) {
+ for (int x = 0; x < 32; x++) {
+ uint8_t expected = 0x55;
+ if (x >= 2 && x < width + 4 && y >= 3 && y < 8) {
+ expected = inverted;
+ if (x >= 3 && x < width + 3 && y >= 4 && y < 7 &&
+ cases[i].rows[y - 4][x - 3] == '#') {
+ expected = !inverted;
+ }
+ }
+ assert_int_equal(
+ _glyph_pixels[rotated ? 31 - y : y][rotated ? 31 - x : x], expected);
+ }
+ }
+ }
+ }
+ }
+}
+
+static void _test_ugui_overhanging_glyph(void** state)
+{
+ (void)state;
+ static const uint8_t bitmap[] = {0xaa, 0x80};
+ static const lv_font_fmt_txt_glyph_dsc_t glyphs[] = {
+ {0},
+ {.adv_w = 32, .box_w = 3, .box_h = 3, .ofs_x = -1},
+ };
+ static const lv_font_fmt_txt_cmap_t cmap = {
+ .range_start = 'A',
+ .range_length = 1,
+ .glyph_id_start = 1,
+ .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY,
+ };
+ static const lv_font_fmt_txt_dsc_t font_dsc = {
+ .glyph_bitmap = bitmap,
+ .glyph_dsc = glyphs,
+ .cmaps = &cmap,
+ .cmap_num = 1,
+ .bpp = 1,
+ };
+ static const UG_FONT font = {
+ .get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt,
+ .get_glyph_bitmap = lv_font_get_bitmap_fmt_txt,
+ .line_height = 2,
+ .dsc = &font_dsc,
+ };
+ UG_Init(&gui, _set_glyph_pixel, &font, 32, 32);
+ for (int background = 0; background < 2; background++) {
+ uint8_t expected[32][32];
+ memset(expected, background, sizeof(expected));
+ expected[3][2] = expected[3][3] = expected[4][2] = expected[4][3] = 0;
+ expected[2][1] = expected[2][3] = expected[3][2] = expected[4][1] = expected[4][3] = 1;
+ memset(_glyph_pixels, background, sizeof(_glyph_pixels));
+ _draw_test_glyph(NULL);
+ assert_memory_equal(_glyph_pixels, expected, sizeof(expected));
+ }
+}
+
+static void _test_ugui_put_string_resolves_glyph_once(void** state)
+{
+ (void)state;
+ static const UG_FONT font = {
+ .get_glyph_dsc = _capture_glyph_dsc,
+ .line_height = 1,
+ };
+ UG_Init(&gui, _set_pixel, &font, 128, 64);
+ expect_value(_capture_glyph_dsc, codepoint, 'A');
+ UG_PutStringNoBreak(0, 0, "A");
+}
+
+static void _test_ugui_lvgl_cmap_validation(void** state)
+{
+ (void)state;
+ static const uint8_t bitmap[] = {0x80};
+ static const lv_font_fmt_txt_glyph_dsc_t glyphs[] = {{0}, {.adv_w = 16}};
+ static const lv_font_fmt_txt_cmap_t cmaps[] = {
+ {.range_start = 'A',
+ .range_length = 1,
+ .glyph_id_start = 1,
+ .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_FULL},
+ {.range_start = 'B',
+ .range_length = 1,
+ .glyph_id_start = 1,
+ .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY},
+ };
+ static const lv_font_fmt_txt_dsc_t font_dsc = {
+ .glyph_bitmap = bitmap,
+ .glyph_dsc = glyphs,
+ .cmaps = cmaps,
+ .cmap_num = 2,
+ .bpp = 1,
+ };
+ static const UG_FONT font = {
+ .get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt,
+ .get_glyph_bitmap = lv_font_get_bitmap_fmt_txt,
+ .dsc = &font_dsc,
+ };
+ UG_U16 width = 0;
+ assert_false(UG_GetCharWidth(&font, 'A', &width));
+ assert_true(UG_GetCharWidth(&font, 'B', &width));
+ assert_int_equal(width, 1);
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -374,9 +580,14 @@ int main(void)
cmocka_unit_test(_test_ugui_word_wrap_invalid_utf8),
cmocka_unit_test(_test_ugui_lvgl_font),
cmocka_unit_test(_test_ugui_lvgl_font_fallback),
+ cmocka_unit_test(_test_ugui_tab_as_space),
cmocka_unit_test(_test_ugui_fonts_use_supported_subset),
cmocka_unit_test(_test_ugui_fonts_exclude_ambiguous_characters),
cmocka_unit_test(_test_ugui_ascii_font_pixels_and_metrics),
+ cmocka_unit_test(_test_ugui_packed_glyph_rows),
+ cmocka_unit_test(_test_ugui_overhanging_glyph),
+ cmocka_unit_test(_test_ugui_put_string_resolves_glyph_once),
+ cmocka_unit_test(_test_ugui_lvgl_cmap_validation),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.