Skip to content

Commit

Permalink
Fix textWidth in draw_button & report strings out of area
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorais-ledger committed May 30, 2024
1 parent b5d8b6e commit e756322
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib_nbgl/src/nbgl_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,21 +386,32 @@ static void draw_button(nbgl_button_t *obj, nbgl_obj_t *prevObj, bool computePos
// draw the text (right of the icon, with 8 pixels between them)
if (text != NULL) {
nbgl_area_t rectArea;
textWidth = nbgl_getTextWidth(obj->fontId, text);
// Compute available with & height to display the text
rectArea.x0 = obj->obj.area.x0;
rectArea.y0 = obj->obj.area.y0;
// Only one line of text
rectArea.height = nbgl_getFontHeight(obj->fontId);
rectArea.y0 += (obj->obj.area.height - rectArea.height) / 2;
rectArea.width = obj->obj.area.width;
if (obj->icon != NULL) {
rectArea.x0 = obj->obj.area.x0 + obj->obj.area.width / 2
- (textWidth + obj->icon->width + 8) / 2 + obj->icon->width + 8;
rectArea.x0 += obj->icon->width + 8;
rectArea.width -= obj->icon->width + 8;
}
else {
rectArea.x0 = obj->obj.area.x0 + (obj->obj.area.width - textWidth) / 2;
// Compute the width & number of characters displayed on first line
uint16_t textLen;
nbgl_getTextMaxLenAndWidth(obj->fontId, text, rectArea.width, &textLen, &textWidth, true);

#ifdef BUILD_SCREENSHOTS
store_string_infos(text, obj->fontId, &rectArea, true, 1, 1, false);
#endif // BUILD_SCREENSHOTS

// Center the text, horizontally
if (textWidth < rectArea.width) {
rectArea.x0 += (rectArea.width - textWidth) / 2;
}
LOG_DEBUG(OBJ_LOGGER, "draw_button(), text = %s\n", text);
rectArea.y0
= obj->obj.area.y0 + (obj->obj.area.height - nbgl_getFontHeight(obj->fontId)) / 2;
rectArea.width = textWidth;
rectArea.height = nbgl_getFontHeight(obj->fontId);
rectArea.backgroundColor = obj->innerColor;
nbgl_drawText(&rectArea, text, nbgl_getTextLength(text), obj->fontId, obj->foregroundColor);
nbgl_drawText(&rectArea, text, textLen, obj->fontId, obj->foregroundColor);
}
// draw the icon, if any
if (obj->icon != NULL) {
Expand Down

0 comments on commit e756322

Please sign in to comment.