1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 13:38:12 +00:00

refactor(core/embed): move&rename display_font_xxx routines

[no changelog]
This commit is contained in:
cepetr 2024-01-17 15:29:49 +01:00 committed by cepetr
parent 3031ec5d87
commit 139701fb1a
8 changed files with 65 additions and 99 deletions

View File

@ -432,7 +432,7 @@ void display_text_center(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor) {
x += DISPLAY_OFFSET.x;
y += DISPLAY_OFFSET.y;
int w = display_text_width(text, textlen, font);
int w = font_text_width(font, text, textlen);
display_text_render(x - w / 2, y, text, textlen, font, fgcolor, bgcolor);
}
@ -440,65 +440,10 @@ void display_text_right(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor) {
x += DISPLAY_OFFSET.x;
y += DISPLAY_OFFSET.y;
int w = display_text_width(text, textlen, font);
int w = font_text_width(font, text, textlen);
display_text_render(x - w, y, text, textlen, font, fgcolor, bgcolor);
}
// compute the width of the text (in pixels)
int display_text_width(const char *text, int textlen, int font) {
int width = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
for (int i = 0; i < textlen; i++) {
const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]);
if (!g) continue;
const uint8_t adv = g[2]; // advance
width += adv;
/*
if (i != textlen - 1) {
const uint8_t adv = g[2]; // advance
width += adv;
} else { // last character
const uint8_t w = g[0]; // width
const uint8_t bearX = g[3]; // bearingX
width += (bearX + w);
}
*/
}
return width;
}
// Returns how many characters of the string can be used before exceeding
// the requested width. Tries to avoid breaking words if possible.
int display_text_split(const char *text, int textlen, int font,
int requested_width) {
int width = 0;
int lastspace = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
for (int i = 0; i < textlen; i++) {
if (text[i] == ' ') {
lastspace = i;
}
const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]);
if (!g) continue;
const uint8_t adv = g[2]; // advance
width += adv;
if (width > requested_width) {
if (lastspace > 0) {
return lastspace;
} else {
return i;
}
}
}
return textlen;
}
#ifdef TREZOR_PRODTEST
#include "qr-code-generator/qrcodegen.h"
@ -562,38 +507,3 @@ void display_fade(int start, int end, int delay) {
display_backlight(end);
#endif
}
#define UTF8_IS_CONT(ch) (((ch)&0xC0) == 0x80)
void display_utf8_substr(const char *buf_start, size_t buf_len, int char_off,
int char_len, const char **out_start, int *out_len) {
size_t i = 0;
for (; i < buf_len; i++) {
if (char_off == 0) {
break;
}
if (!UTF8_IS_CONT(buf_start[i])) {
char_off--;
}
}
size_t i_start = i;
for (; i < buf_len; i++) {
if (char_len == 0) {
break;
}
if (!UTF8_IS_CONT(buf_start[i])) {
char_len--;
}
}
for (; i < buf_len; i++) {
if (!UTF8_IS_CONT(buf_start[i])) {
break;
}
}
*out_start = buf_start + i_start;
*out_len = i - i_start;
}

View File

@ -58,9 +58,6 @@ void display_text_center(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor);
void display_text_right(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor);
int display_text_width(const char *text, int textlen, int font);
int display_text_split(const char *text, int textlen, int font,
int requested_width);
void display_text_render_buffer(const char *text, int textlen, int font,
buffer_text_t *buffer, int text_offset);

View File

@ -18,6 +18,7 @@
*/
#include "fonts.h"
#include <string.h>
static uint8_t convert_char(const uint8_t c) {
static char last_was_utf8 = 0;
@ -184,3 +185,58 @@ const uint8_t *font_get_glyph(int font, uint8_t c) {
}
return 0;
}
// compute the width of the text (in pixels)
int font_text_width(int font, const char *text, int textlen) {
int width = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
for (int i = 0; i < textlen; i++) {
const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]);
if (!g) continue;
const uint8_t adv = g[2]; // advance
width += adv;
/*
if (i != textlen - 1) {
const uint8_t adv = g[2]; // advance
width += adv;
} else { // last character
const uint8_t w = g[0]; // width
const uint8_t bearX = g[3]; // bearingX
width += (bearX + w);
}
*/
}
return width;
}
// Returns how many characters of the string can be used before exceeding
// the requested width. Tries to avoid breaking words if possible.
int font_text_split(int font, const char *text, int textlen,
int requested_width) {
int width = 0;
int lastspace = 0;
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
}
for (int i = 0; i < textlen; i++) {
if (text[i] == ' ') {
lastspace = i;
}
const uint8_t *g = font_get_glyph(font, (uint8_t)text[i]);
if (!g) continue;
const uint8_t adv = g[2]; // advance
width += adv;
if (width > requested_width) {
if (lastspace > 0) {
return lastspace;
} else {
return i;
}
}
}
return textlen;
}

View File

@ -118,5 +118,8 @@ int font_height(int font);
int font_max_height(int font);
int font_baseline(int font);
const uint8_t *font_get_glyph(int font, uint8_t c);
int font_text_width(int font, const char *text, int textlen);
int font_text_split(int font, const char *text, int textlen,
int requested_width);
#endif //_FONTS_H

View File

@ -22,7 +22,7 @@
#define UTF8_IS_CONT(ch) (((ch)&0xC0) == 0x80)
void utf8_substr(const char *buf_start, size_t buf_len, int char_off,
int char_len, const char **out_start, int *out_len) {
int char_len, const char **out_start, int *out_len) {
size_t i = 0;
for (; i < buf_len; i++) {

View File

@ -25,6 +25,6 @@
// helper for locating a substring in buffer with utf-8 string
void utf8_substr(const char *buf_start, size_t buf_len, int char_off,
int char_len, const char **out_start, int *out_len);
int char_len, const char **out_start, int *out_len);
#endif // LIB_UTF8_H

View File

@ -296,7 +296,6 @@ fn generate_trezorhal_bindings() {
.allowlist_function("display_backlight")
.allowlist_function("display_text")
.allowlist_function("display_text_render_buffer")
.allowlist_function("display_text_width")
.allowlist_function("display_pixeldata")
.allowlist_function("display_pixeldata_dirty")
.allowlist_function("display_set_window")
@ -316,6 +315,7 @@ fn generate_trezorhal_bindings() {
.allowlist_function("font_max_height")
.allowlist_function("font_baseline")
.allowlist_function("font_get_glyph")
.allowlist_function("font_text_width")
// uzlib
.allowlist_function("uzlib_uncompress_init")
.allowlist_function("uzlib_uncompress")

View File

@ -59,7 +59,7 @@ pub fn text_into_buffer(text: &str, font: i32, buffer: &mut BufferText, x_offset
pub fn text_width(text: &str, font: i32) -> i16 {
unsafe {
ffi::display_text_width(text.as_ptr() as _, text.len() as _, font)
ffi::font_text_width(font, text.as_ptr() as _, text.len() as _)
.try_into()
.unwrap_or(i16::MAX)
}