diff --git a/src/graphics/text.c b/src/graphics/text.c index ea5e3e10de..e602813c86 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -419,8 +419,9 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_ return y - y_offset; } -int text_measure_multiline(const uint8_t *str, int box_width, font_t font) +int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width) { + *largest_width = 0; int has_more_characters = 1; int guard = 0; int num_lines = 0; @@ -429,7 +430,7 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font) break; } int current_width = 0; - while (has_more_characters && current_width < box_width) { + while (has_more_characters) { int word_num_chars; int word_width = get_word_width(str, font, &word_num_chars); current_width += word_width; @@ -437,6 +438,7 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font) if (current_width == 0) { has_more_characters = 0; } + break; } else { str += word_num_chars; if (!*str) { @@ -447,6 +449,9 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font) } } } + if (current_width > *largest_width) { + *largest_width = current_width; + } num_lines += 1; } return num_lines; diff --git a/src/graphics/text.h b/src/graphics/text.h index 449a2ea012..503155f28d 100644 --- a/src/graphics/text.h +++ b/src/graphics/text.h @@ -36,6 +36,6 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_ /** * @return Number of lines required to draw the text */ -int text_measure_multiline(const uint8_t *str, int box_width, font_t font); +int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width); #endif // GRAPHICS_TEXT_H diff --git a/src/graphics/tooltip.c b/src/graphics/tooltip.c index 567c155e2f..516c4db5d4 100644 --- a/src/graphics/tooltip.c +++ b/src/graphics/tooltip.c @@ -132,12 +132,14 @@ static void draw_button_tooltip(tooltip_context *c) const uint8_t *text = get_tooltip_text(c); int width = 200; - int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN); + int largest_width; + int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width); if (lines > 2) { width = 300; - lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN); + lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width); } int height = 16 * lines + 10; + width = largest_width + 16; int x, y; if (c->mouse_x < screen_dialog_offset_x() + width + 100) { @@ -191,19 +193,21 @@ static void draw_button_tooltip(tooltip_context *c) graphics_draw_rect(x, y, width, height, COLOR_BLACK); graphics_fill_rect(x + 1, y + 1, width - 2, height - 2, COLOR_WHITE); - text_draw_multiline(text, x + 5, y + 7, width - 5, FONT_SMALL_PLAIN, COLOR_TOOLTIP); + text_draw_multiline(text, x + 8, y + 8, width - 15, FONT_SMALL_PLAIN, COLOR_TOOLTIP); } static void draw_overlay_tooltip(tooltip_context *c) { const uint8_t *text = get_tooltip_text(c); int width = 200; - int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN); + int largest_width; + int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width); if (lines > 2) { width = 300; - lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN); + lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width); } int height = 16 * lines + 10; + width = largest_width + 16; int x, y; if (c->mouse_x < width + 20) { @@ -223,7 +227,7 @@ static void draw_overlay_tooltip(tooltip_context *c) graphics_draw_rect(x, y, width, height, COLOR_BLACK); graphics_fill_rect(x + 1, y + 1, width - 2, height - 2, COLOR_WHITE); - text_draw_multiline(text, x + 5, y + 7, width - 5, FONT_SMALL_PLAIN, COLOR_TOOLTIP); + text_draw_multiline(text, x + 8, y + 8, width - 15, FONT_SMALL_PLAIN, COLOR_TOOLTIP); } static void draw_senate_tooltip(tooltip_context *c)