mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-08-04 12:56:25 +00:00
trezor.ui: introduced minwidth to display.text functions; more optimizations
This commit is contained in:
parent
47f5cba439
commit
63e999ab84
@ -39,13 +39,13 @@ static void display_header(int icon, const char *text)
|
|||||||
display_icon(8, 4, 24, 24, toi_icon_wipe, sizeof(toi_icon_wipe), COLOR_BLACK, COLOR_BL_ORANGE);
|
display_icon(8, 4, 24, 24, toi_icon_wipe, sizeof(toi_icon_wipe), COLOR_BLACK, COLOR_BL_ORANGE);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
display_text(8 + 24 + 8, 23, text, -1, FONT_BOLD, COLOR_BLACK, COLOR_BL_ORANGE);
|
display_text(8 + 24 + 8, 23, text, -1, FONT_BOLD, COLOR_BLACK, COLOR_BL_ORANGE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void display_footer(const char *text, uint16_t color, int bottom)
|
static void display_footer(const char *text, uint16_t color, int bottom)
|
||||||
{
|
{
|
||||||
display_bar(0, DISPLAY_RESY - bottom - 24, DISPLAY_RESX, bottom + 24, COLOR_BLACK);
|
display_bar(0, DISPLAY_RESY - bottom - 24, DISPLAY_RESX, bottom + 24, COLOR_BLACK);
|
||||||
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - bottom, text, -1, FONT_BOLD, color, COLOR_BLACK);
|
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - bottom, text, -1, FONT_BOLD, color, COLOR_BLACK, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void display_done(int restart)
|
static void display_done(int restart)
|
||||||
@ -73,7 +73,7 @@ static void display_welcome(secbool firmware_present)
|
|||||||
display_clear();
|
display_clear();
|
||||||
if (secfalse == firmware_present) {
|
if (secfalse == firmware_present) {
|
||||||
display_icon((DISPLAY_RESX - 124) / 2, (DISPLAY_RESY - 40 - 180) / 2, 124, 180, toi_icon_lock, sizeof(toi_icon_lock), COLOR_WHITE, COLOR_BLACK);
|
display_icon((DISPLAY_RESX - 124) / 2, (DISPLAY_RESY - 40 - 180) / 2, 124, 180, toi_icon_lock, sizeof(toi_icon_lock), COLOR_WHITE, COLOR_BLACK);
|
||||||
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 20, "Go to trezor.io/start", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK);
|
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 20, "Go to trezor.io/start", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK, 0);
|
||||||
}
|
}
|
||||||
if (sectrue == firmware_present) {
|
if (sectrue == firmware_present) {
|
||||||
display_header(ICON_TOOLS, "TREZOR Bootloader");
|
display_header(ICON_TOOLS, "TREZOR Bootloader");
|
||||||
@ -99,7 +99,7 @@ static void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_
|
|||||||
uint32_t datalen = *(uint32_t *)(vimg + 8);
|
uint32_t datalen = *(uint32_t *)(vimg + 8);
|
||||||
display_image((DISPLAY_RESX - w) / 2, 32, w, h, vimg + 12, datalen);
|
display_image((DISPLAY_RESX - w) / 2, 32, w, h, vimg + 12, datalen);
|
||||||
if (vstr && vstr_len) {
|
if (vstr && vstr_len) {
|
||||||
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 48, vstr, vstr_len, FONT_BOLD, COLOR_WHITE, background);
|
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 48, vstr, vstr_len, FONT_BOLD, COLOR_WHITE, background, 0);
|
||||||
}
|
}
|
||||||
char ver_str[32];
|
char ver_str[32];
|
||||||
mini_snprintf(ver_str, sizeof(ver_str), "%d.%d.%d.%d",
|
mini_snprintf(ver_str, sizeof(ver_str), "%d.%d.%d.%d",
|
||||||
@ -108,7 +108,7 @@ static void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_
|
|||||||
(int)((fw_version >> 16) & 0xFF),
|
(int)((fw_version >> 16) & 0xFF),
|
||||||
(int)((fw_version >> 24) & 0xFF)
|
(int)((fw_version >> 24) & 0xFF)
|
||||||
);
|
);
|
||||||
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 25, ver_str, -1, FONT_BOLD, COLOR_GRAY128, background);
|
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 25, ver_str, -1, FONT_BOLD, COLOR_GRAY128, background, 0);
|
||||||
display_refresh();
|
display_refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,32 +411,17 @@ void display_printf(const char *fmt, ...)
|
|||||||
|
|
||||||
#endif // TREZOR_PRINT_DISABLE
|
#endif // TREZOR_PRINT_DISABLE
|
||||||
|
|
||||||
// first two bytes are width and height of the glyph
|
static void display_text_render(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor)
|
||||||
// third, fourth and fifth bytes are advance, bearingX and bearingY of the horizontal metrics of the glyph
|
|
||||||
// rest is packed 4-bit glyph data
|
|
||||||
void display_text(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor)
|
|
||||||
{
|
{
|
||||||
uint16_t colortable[16];
|
|
||||||
set_color_table(colortable, fgcolor, bgcolor);
|
|
||||||
|
|
||||||
// determine text length if not provided
|
// determine text length if not provided
|
||||||
if (textlen < 0) {
|
if (textlen < 0) {
|
||||||
textlen = strlen(text);
|
textlen = strlen(text);
|
||||||
}
|
}
|
||||||
y += DISPLAY_OFFSET[1];
|
|
||||||
|
|
||||||
// render background bars
|
uint16_t colortable[16];
|
||||||
int px = x + DISPLAY_OFFSET[0];
|
set_color_table(colortable, fgcolor, bgcolor);
|
||||||
for (int i = 0; i < textlen; i++) {
|
|
||||||
const uint8_t *g = get_glyph(font, (uint8_t)text[i]);
|
|
||||||
if (!g) continue;
|
|
||||||
const int8_t adv = g[2]; // advance
|
|
||||||
display_bar(px - 2, y - 18, adv + 3, 23, bgcolor);
|
|
||||||
px += adv;
|
|
||||||
}
|
|
||||||
|
|
||||||
// render glyphs
|
// render glyphs
|
||||||
px = x + DISPLAY_OFFSET[0];
|
|
||||||
for (int i = 0; i < textlen; i++) {
|
for (int i = 0; i < textlen; i++) {
|
||||||
const uint8_t *g = get_glyph(font, (uint8_t)text[i]);
|
const uint8_t *g = get_glyph(font, (uint8_t)text[i]);
|
||||||
if (!g) continue;
|
if (!g) continue;
|
||||||
@ -446,7 +431,7 @@ void display_text(int x, int y, const char *text, int textlen, uint8_t font, uin
|
|||||||
const int8_t bearX = g[3]; // bearingX
|
const int8_t bearX = g[3]; // bearingX
|
||||||
const int8_t bearY = g[4]; // bearingY
|
const int8_t bearY = g[4]; // bearingY
|
||||||
if (w && h) {
|
if (w && h) {
|
||||||
const int sx = px + bearX;
|
const int sx = x + bearX;
|
||||||
const int sy = y - bearY;
|
const int sy = y - bearY;
|
||||||
int x0, y0, x1, y1;
|
int x0, y0, x1, y1;
|
||||||
clamp_coords(sx, sy, w, h, &x0, &y0, &x1, &y1);
|
clamp_coords(sx, sy, w, h, &x0, &y0, &x1, &y1);
|
||||||
@ -468,20 +453,40 @@ void display_text(int x, int y, const char *text, int textlen, uint8_t font, uin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
px += adv;
|
x += adv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_text_center(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor)
|
#define max(x, y) (((x) > (y)) ? (x) : (y))
|
||||||
|
|
||||||
|
void display_text(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth)
|
||||||
{
|
{
|
||||||
|
x += DISPLAY_OFFSET[0];
|
||||||
|
y += DISPLAY_OFFSET[1];
|
||||||
int w = display_text_width(text, textlen, font);
|
int w = display_text_width(text, textlen, font);
|
||||||
display_text(x - w / 2, y, text, textlen, font, fgcolor, bgcolor);
|
int barwidth = max(w, minwidth);
|
||||||
|
display_bar(x - 1, y - 18, barwidth + 2, 23, bgcolor);
|
||||||
|
display_text_render(x, y, text, textlen, font, fgcolor, bgcolor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor)
|
void display_text_center(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth)
|
||||||
{
|
{
|
||||||
|
x += DISPLAY_OFFSET[0];
|
||||||
|
y += DISPLAY_OFFSET[1];
|
||||||
int w = display_text_width(text, textlen, font);
|
int w = display_text_width(text, textlen, font);
|
||||||
display_text(x - w, y, text, textlen, font, fgcolor, bgcolor);
|
int barwidth = max(w, minwidth);
|
||||||
|
display_bar(x - barwidth / 2 - 1, y - 18, barwidth + 2, 23, bgcolor);
|
||||||
|
display_text_render(x - w / 2, y, text, textlen, font, fgcolor, bgcolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth)
|
||||||
|
{
|
||||||
|
x += DISPLAY_OFFSET[0];
|
||||||
|
y += DISPLAY_OFFSET[1];
|
||||||
|
int w = display_text_width(text, textlen, font);
|
||||||
|
int barwidth = max(w, minwidth);
|
||||||
|
display_bar(x - barwidth - 1, y - 18, barwidth + 2, 23, bgcolor);
|
||||||
|
display_text_render(x - w, y, text, textlen, font, fgcolor, bgcolor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute the width of the text (in pixels)
|
// compute the width of the text (in pixels)
|
||||||
@ -495,7 +500,8 @@ int display_text_width(const char *text, int textlen, uint8_t font)
|
|||||||
for (int i = 0; i < textlen; i++) {
|
for (int i = 0; i < textlen; i++) {
|
||||||
const uint8_t *g = get_glyph(font, (uint8_t)text[i]);
|
const uint8_t *g = get_glyph(font, (uint8_t)text[i]);
|
||||||
if (!g) continue;
|
if (!g) continue;
|
||||||
w += g[2];
|
const int8_t adv = g[2]; // advance
|
||||||
|
w += adv;
|
||||||
}
|
}
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
@ -72,9 +72,9 @@ void display_print(const char *text, int textlen);
|
|||||||
void display_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
|
void display_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void display_text(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);
|
void display_text(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth);
|
||||||
void display_text_center(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);
|
void display_text_center(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth);
|
||||||
void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor);
|
void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor, int minwidth);
|
||||||
int display_text_width(const char *text, int textlen, uint8_t font);
|
int display_text_width(const char *text, int textlen, uint8_t font);
|
||||||
|
|
||||||
void display_qrcode(int x, int y, const char *data, int datalen, uint8_t scale);
|
void display_qrcode(int x, int y, const char *data, int datalen, uint8_t scale);
|
||||||
|
@ -178,7 +178,7 @@ STATIC mp_obj_t mod_trezorui_Display_print(mp_obj_t self, mp_obj_t text) {
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorui_Display_print_obj, mod_trezorui_Display_print);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorui_Display_print_obj, mod_trezorui_Display_print);
|
||||||
|
|
||||||
/// def text(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
|
/// def text(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None:
|
||||||
/// '''
|
/// '''
|
||||||
/// Renders left-aligned text at position (x,y) where x is left position and y is baseline.
|
/// Renders left-aligned text at position (x,y) where x is left position and y is baseline.
|
||||||
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
||||||
@ -191,14 +191,13 @@ STATIC mp_obj_t mod_trezorui_Display_text(size_t n_args, const mp_obj_t *args) {
|
|||||||
mp_int_t font = mp_obj_get_int(args[4]);
|
mp_int_t font = mp_obj_get_int(args[4]);
|
||||||
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
||||||
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
||||||
if (text.len > 0) {
|
mp_int_t minwidth = (n_args > 7) ? mp_obj_get_int(args[7]) : 0;
|
||||||
display_text(x, y, text.buf, text.len, font, fgcolor, bgcolor);
|
display_text(x, y, text.buf, text.len, font, fgcolor, bgcolor, minwidth);
|
||||||
}
|
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_obj, 7, 7, mod_trezorui_Display_text);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_obj, 7, 8, mod_trezorui_Display_text);
|
||||||
|
|
||||||
/// def text_center(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
|
/// def text_center(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None:
|
||||||
/// '''
|
/// '''
|
||||||
/// Renders text centered at position (x,y) where x is text center and y is baseline.
|
/// Renders text centered at position (x,y) where x is text center and y is baseline.
|
||||||
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
||||||
@ -211,14 +210,13 @@ STATIC mp_obj_t mod_trezorui_Display_text_center(size_t n_args, const mp_obj_t *
|
|||||||
mp_int_t font = mp_obj_get_int(args[4]);
|
mp_int_t font = mp_obj_get_int(args[4]);
|
||||||
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
||||||
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
||||||
if (text.len > 0) {
|
mp_int_t minwidth = (n_args > 7) ? mp_obj_get_int(args[7]) : 0;
|
||||||
display_text_center(x, y, text.buf, text.len, font, fgcolor, bgcolor);
|
display_text_center(x, y, text.buf, text.len, font, fgcolor, bgcolor, minwidth);
|
||||||
}
|
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_center_obj, 7, 7, mod_trezorui_Display_text_center);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_center_obj, 7, 8, mod_trezorui_Display_text_center);
|
||||||
|
|
||||||
/// def text_right(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None:
|
/// def text_right(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None:
|
||||||
/// '''
|
/// '''
|
||||||
/// Renders right-aligned text at position (x,y) where x is right position and y is baseline.
|
/// Renders right-aligned text at position (x,y) where x is right position and y is baseline.
|
||||||
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
/// Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background.
|
||||||
@ -231,12 +229,11 @@ STATIC mp_obj_t mod_trezorui_Display_text_right(size_t n_args, const mp_obj_t *a
|
|||||||
mp_int_t font = mp_obj_get_int(args[4]);
|
mp_int_t font = mp_obj_get_int(args[4]);
|
||||||
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
mp_int_t fgcolor = mp_obj_get_int(args[5]);
|
||||||
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
mp_int_t bgcolor = mp_obj_get_int(args[6]);
|
||||||
if (text.len > 0) {
|
mp_int_t minwidth = (n_args > 7) ? mp_obj_get_int(args[7]) : 0;
|
||||||
display_text_right(x, y, text.buf, text.len, font, fgcolor, bgcolor);
|
display_text_right(x, y, text.buf, text.len, font, fgcolor, bgcolor, minwidth);
|
||||||
}
|
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_right_obj, 7, 7, mod_trezorui_Display_text_right);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorui_Display_text_right_obj, 7, 8, mod_trezorui_Display_text_right);
|
||||||
|
|
||||||
/// def text_width(self, text: str, font: int) -> int:
|
/// def text_width(self, text: str, font: int) -> int:
|
||||||
/// '''
|
/// '''
|
||||||
@ -246,10 +243,7 @@ STATIC mp_obj_t mod_trezorui_Display_text_width(mp_obj_t self, mp_obj_t text, mp
|
|||||||
mp_buffer_info_t txt;
|
mp_buffer_info_t txt;
|
||||||
mp_get_buffer_raise(text, &txt, MP_BUFFER_READ);
|
mp_get_buffer_raise(text, &txt, MP_BUFFER_READ);
|
||||||
mp_int_t f = mp_obj_get_int(font);
|
mp_int_t f = mp_obj_get_int(font);
|
||||||
uint32_t w = 0;
|
int w = display_text_width(txt.buf, txt.len, f);
|
||||||
if (txt.len > 0) {
|
|
||||||
w = display_text_width(txt.buf, txt.len, f);
|
|
||||||
}
|
|
||||||
return MP_OBJ_NEW_SMALL_INT(w);
|
return MP_OBJ_NEW_SMALL_INT(w);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorui_Display_text_width_obj, mod_trezorui_Display_text_width);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorui_Display_text_width_obj, mod_trezorui_Display_text_width);
|
||||||
|
@ -310,7 +310,7 @@ int main(void)
|
|||||||
// format: TREZOR2-YYMMDD
|
// format: TREZOR2-YYMMDD
|
||||||
if (sectrue == flash_otp_read(0, 0, (uint8_t *)dom, 32) && 0 == memcmp(dom, "TREZOR2-", 8) && dom[31] == 0) {
|
if (sectrue == flash_otp_read(0, 0, (uint8_t *)dom, 32) && 0 == memcmp(dom, "TREZOR2-", 8) && dom[31] == 0) {
|
||||||
display_qrcode(DISPLAY_RESX / 2, DISPLAY_RESY / 2, dom, strlen(dom), 4);
|
display_qrcode(DISPLAY_RESX / 2, DISPLAY_RESY / 2, dom, strlen(dom), 4);
|
||||||
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 30, dom + 8, -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK);
|
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 30, dom + 8, -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
display_fade(0, BACKLIGHT_NORMAL, 1000);
|
display_fade(0, BACKLIGHT_NORMAL, 1000);
|
||||||
|
Loading…
Reference in New Issue
Block a user