Skip to content

Commit

Permalink
refactor(core): use font_id_t instead of plain int
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
obrusvit committed Sep 19, 2024
1 parent 923b016 commit 1fce051
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 212 deletions.
18 changes: 10 additions & 8 deletions core/embed/lib/display_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void display_bar(int x, int y, int w, int h, uint16_t c) {
display_pixeldata_dirty();
}

void display_text_render_buffer(const char *text, int textlen, int font,
void display_text_render_buffer(const char *text, int textlen, font_id_t font,
buffer_text_t *buffer, int text_offset) {
// determine text length if not provided
if (textlen < 0) {
Expand Down Expand Up @@ -159,7 +159,8 @@ void display_text_render_buffer(const char *text, int textlen, int font,

#ifdef FRAMEBUFFER
static void display_text_render(int x, int y, const char *text, int textlen,
int font, uint16_t fgcolor, uint16_t bgcolor) {
font_id_t font, uint16_t fgcolor,
uint16_t bgcolor) {
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
Expand Down Expand Up @@ -226,7 +227,8 @@ static void display_text_render(int x, int y, const char *text, int textlen,

#else
static void display_text_render(int x, int y, const char *text, int textlen,
int font, uint16_t fgcolor, uint16_t bgcolor) {
font_id_t font, uint16_t fgcolor,
uint16_t bgcolor) {
// determine text length if not provided
if (textlen < 0) {
textlen = strlen(text);
Expand Down Expand Up @@ -282,23 +284,23 @@ static void display_text_render(int x, int y, const char *text, int textlen,
}
#endif

void display_text(int x, int y, const char *text, int textlen, int font,
void display_text(int x, int y, const char *text, int textlen, font_id_t font,
uint16_t fgcolor, uint16_t bgcolor) {
x += DISPLAY_OFFSET.x;
y += DISPLAY_OFFSET.y;
display_text_render(x, y, text, textlen, font, fgcolor, bgcolor);
}

void display_text_center(int x, int y, const char *text, int textlen, int font,
uint16_t fgcolor, uint16_t bgcolor) {
void display_text_center(int x, int y, const char *text, int textlen,
font_id_t font, uint16_t fgcolor, uint16_t bgcolor) {
x += DISPLAY_OFFSET.x;
y += DISPLAY_OFFSET.y;
int w = font_text_width(font, text, textlen);
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, int font,
uint16_t fgcolor, uint16_t bgcolor) {
void display_text_right(int x, int y, const char *text, int textlen,
font_id_t font, uint16_t fgcolor, uint16_t bgcolor) {
x += DISPLAY_OFFSET.x;
y += DISPLAY_OFFSET.y;
int w = font_text_width(font, text, textlen);
Expand Down
12 changes: 6 additions & 6 deletions core/embed/lib/display_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ void display_clear(void);

void display_bar(int x, int y, int w, int h, uint16_t c);

void display_text(int x, int y, const char *text, int textlen, int font,
void display_text(int x, int y, const char *text, int textlen, font_id_t font,
uint16_t fgcolor, uint16_t bgcolor);
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);
void display_text_render_buffer(const char *text, int textlen, int font,
void display_text_center(int x, int y, const char *text, int textlen,
font_id_t font, uint16_t fgcolor, uint16_t bgcolor);
void display_text_right(int x, int y, const char *text, int textlen,
font_id_t font, uint16_t fgcolor, uint16_t bgcolor);
void display_text_render_buffer(const char *text, int textlen, font_id_t font,
buffer_text_t *buffer, int text_offset);

void display_qrcode(int x, int y, const char *data, uint8_t scale);
Expand Down
14 changes: 7 additions & 7 deletions core/embed/lib/fonts/fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ static const font_info_t *get_font_info(font_id_t font_id) {
return font_registry[font_id];
}

int font_height(int font_id) {
int font_height(font_id_t font_id) {
const font_info_t *font_info = get_font_info(font_id);
return font_info ? font_info->height : 0;
}

int font_max_height(int font) {
int font_max_height(font_id_t font) {
const font_info_t *font_info = get_font_info(font);
return font_info ? font_info->max_height : 0;
}

int font_baseline(int font) {
int font_baseline(font_id_t font) {
const font_info_t *font_info = get_font_info(font);
return font_info ? font_info->baseline : 0;
}

const uint8_t *font_get_glyph(int font, uint16_t c) {
const uint8_t *font_get_glyph(font_id_t font, uint16_t c) {
#ifdef TRANSLATIONS
// found UTF8 character
// it is not hardcoded in firmware fonts, it must be extracted from the
Expand All @@ -146,12 +146,12 @@ const uint8_t *font_get_glyph(int font, uint16_t c) {
return font_nonprintable_glyph(font);
}

const uint8_t *font_nonprintable_glyph(int font) {
const uint8_t *font_nonprintable_glyph(font_id_t font) {
const font_info_t *font_info = get_font_info(font);
return font_info ? font_info->glyph_nonprintable : NULL;
}

font_glyph_iter_t font_glyph_iter_init(const int font, const uint8_t *text,
font_glyph_iter_t font_glyph_iter_init(font_id_t font, const uint8_t *text,
const int len) {
return (font_glyph_iter_t){
.font = font,
Expand Down Expand Up @@ -222,7 +222,7 @@ bool font_next_glyph(font_glyph_iter_t *iter, const uint8_t **out) {
}

// compute the width of the text (in pixels)
int font_text_width(int font, const char *text, int textlen) {
int font_text_width(font_id_t font, const char *text, int textlen) {
int width = 0;
// determine text length if not provided
if (textlen < 0) {
Expand Down
14 changes: 7 additions & 7 deletions core/embed/lib/fonts/fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
TREZOR_FONT_MONO_ENABLE ? FONT_MONO_MAX_HEIGHT : 0)
// clang-format on

int font_height(int font);
int font_max_height(int font);
int font_baseline(int font);
const uint8_t *font_get_glyph(int font, uint16_t c);
const uint8_t *font_nonprintable_glyph(int font);
int font_height(font_id_t font);
int font_max_height(font_id_t font);
int font_baseline(font_id_t font);
const uint8_t *font_get_glyph(font_id_t font, const uint16_t c);
const uint8_t *font_nonprintable_glyph(font_id_t font);

font_glyph_iter_t font_glyph_iter_init(const int font, const uint8_t *text,
font_glyph_iter_t font_glyph_iter_init(font_id_t font, const uint8_t *text,
const int len);
bool font_next_glyph(font_glyph_iter_t *iter, const uint8_t **out);
int font_text_width(int font, const char *text, int textlen);
int font_text_width(font_id_t font, const char *text, int textlen);

#endif //_FONTS_H
2 changes: 1 addition & 1 deletion core/embed/lib/fonts/fonts_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef enum {

/// Font glyph iterator structure
typedef struct {
const int font;
const font_id_t font;
const uint8_t* text;
int remaining;
} font_glyph_iter_t;
Expand Down
6 changes: 3 additions & 3 deletions core/embed/lib/gfx_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void display_bar(int x, int y, int w, int h, uint16_t c) {
gfx_draw_bar(gfx_rect_wh(x, y, w, h), c);
}

void display_text(int x, int y, const char* text, int textlen, int font,
void display_text(int x, int y, const char* text, int textlen, font_id_t font,
uint16_t fg_color, uint16_t bg_color) {
gfx_text_attr_t attr = {
.font = font,
Expand All @@ -309,8 +309,8 @@ void display_text(int x, int y, const char* text, int textlen, int font,
gfx_draw_text(gfx_offset(x, y), text, maxlen, &attr);
}

void display_text_center(int x, int y, const char* text, int textlen, int font,
uint16_t fg_color, uint16_t bg_color) {
void display_text_center(int x, int y, const char* text, int textlen,
font_id_t font, uint16_t fg_color, uint16_t bg_color) {
gfx_text_attr_t attr = {
.font = font,
.fg_color = fg_color,
Expand Down
4 changes: 3 additions & 1 deletion core/embed/rust/librust_fonts.h
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
const uint8_t *get_utf8_glyph(uint16_t char_code, int font);
#include "fonts/fonts_types.h"

const uint8_t *get_utf8_glyph(uint16_t char_code, font_id_t font);
7 changes: 2 additions & 5 deletions core/embed/rust/src/translations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub const DEFAULT_LANGUAGE: &str = "en-US";
/// Returned pointer will only point to valid font data for as long as
/// the flash content is not invalidated by `erase()` or `write()`.
#[no_mangle]
pub unsafe extern "C" fn get_utf8_glyph(codepoint: cty::uint16_t, font: cty::c_int) -> *const u8 {
// C will send a negative number
let font_abs = font.unsigned_abs() as u16;

pub unsafe extern "C" fn get_utf8_glyph(codepoint: cty::uint16_t, font: cty::c_uint) -> *const u8 {
// SAFETY: Reference is discarded at the end of the function.
// We do return a _pointer_ to the same memory location, but the pointer is
// always valid.
Expand All @@ -27,7 +24,7 @@ pub unsafe extern "C" fn get_utf8_glyph(codepoint: cty::uint16_t, font: cty::c_i
let Some(tr) = translations.as_ref() else {
return core::ptr::null();
};
if let Some(glyph) = tr.font(font_abs).and_then(|t| t.get(codepoint)) {
if let Some(glyph) = tr.font(font as u16).and_then(|t| t.get(codepoint)) {
glyph.as_ptr()
} else {
core::ptr::null()
Expand Down
16 changes: 8 additions & 8 deletions core/embed/rust/src/trezorhal/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn backlight(val: i32) -> i32 {
unsafe { ffi::display_backlight(val) }
}

pub fn text(baseline_x: i16, baseline_y: i16, text: &str, font: i32, fgcolor: u16, bgcolor: u16) {
pub fn text(baseline_x: i16, baseline_y: i16, text: &str, font: u32, fgcolor: u16, bgcolor: u16) {
unsafe {
ffi::display_text(
baseline_x.into(),
Expand All @@ -37,7 +37,7 @@ pub fn text(baseline_x: i16, baseline_y: i16, text: &str, font: i32, fgcolor: u1
}
}

pub fn text_into_buffer(text: &str, font: i32, buffer: &mut BufferText, x_offset: i16) {
pub fn text_into_buffer(text: &str, font: u32, buffer: &mut BufferText, x_offset: i16) {
unsafe {
ffi::display_text_render_buffer(
text.as_ptr() as _,
Expand All @@ -49,33 +49,33 @@ pub fn text_into_buffer(text: &str, font: i32, buffer: &mut BufferText, x_offset
}
}

pub fn text_width(text: &str, font: i32) -> i16 {
pub fn text_width(text: &str, font: u32) -> i16 {
unsafe {
ffi::font_text_width(font, text.as_ptr() as _, text.len() as _)
.try_into()
.unwrap_or(i16::MAX)
}
}

pub fn char_width(ch: char, font: i32) -> i16 {
pub fn char_width(ch: char, font: u32) -> i16 {
let mut buf = [0u8; 4];
let encoding = ch.encode_utf8(&mut buf);
text_width(encoding, font)
}

pub fn get_char_glyph(ch: u16, font: i32) -> *const u8 {
pub fn get_char_glyph(ch: u16, font: u32) -> *const u8 {
unsafe { ffi::font_get_glyph(font, ch) }
}

pub fn text_height(font: i32) -> i16 {
pub fn text_height(font: u32) -> i16 {
unsafe { ffi::font_height(font).try_into().unwrap_or(i16::MAX) }
}

pub fn text_max_height(font: i32) -> i16 {
pub fn text_max_height(font: u32) -> i16 {
unsafe { ffi::font_max_height(font).try_into().unwrap_or(i16::MAX) }
}

pub fn text_baseline(font: i32) -> i16 {
pub fn text_baseline(font: u32) -> i16 {
unsafe { ffi::font_baseline(font).try_into().unwrap_or(i16::MAX) }
}

Expand Down
6 changes: 3 additions & 3 deletions core/embed/rust/src/ui/display/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ pub enum Font {
SUB,
}

impl From<Font> for i32 {
fn from(font: Font) -> i32 {
font as i32
impl From<Font> for u32 {
fn from(font: Font) -> u32 {
font as u32
}
}

Expand Down
3 changes: 1 addition & 2 deletions core/embed/rust/src/ui/model_mercury/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,7 @@ pub fn get_chunkified_text_style(character_length: usize) -> &'static TextStyle

/// Convert Python-side numeric id to a `TextStyle`.
pub fn textstyle_number(num: i32) -> &'static TextStyle {
let font = Font::from_i32(-num);
match font {
match Font::from_i32(num) {
Some(Font::DEMIBOLD) => &TEXT_DEMIBOLD,
Some(Font::BOLD) => &TEXT_BOLD,
Some(Font::MONO) => &TEXT_MONO,
Expand Down
3 changes: 1 addition & 2 deletions core/embed/rust/src/ui/model_tr/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub const MONO_CHUNKS: Chunks = Chunks::new(4, 4);

/// Convert Python-side numeric id to a `TextStyle`.
pub fn textstyle_number(num: i32) -> &'static TextStyle {
let font = Font::from_i32(-num);
match font {
match Font::from_i32(num) {
Some(Font::BOLD) => &TEXT_BOLD,
Some(Font::DEMIBOLD) => &TEXT_BOLD,
Some(Font::NORMAL) => &TEXT_NORMAL,
Expand Down
3 changes: 1 addition & 2 deletions core/embed/rust/src/ui/model_tt/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,7 @@ pub fn get_chunkified_text_style(character_length: usize) -> &'static TextStyle

/// Convert Python-side numeric id to a `TextStyle`.
pub fn textstyle_number(num: i32) -> &'static TextStyle {
let font = Font::from_i32(-num);
match font {
match Font::from_i32(num) {
Some(Font::DEMIBOLD) => &TEXT_DEMIBOLD,
Some(Font::BOLD_UPPER) => &TEXT_BOLD,
Some(Font::MONO) => &TEXT_MONO,
Expand Down
5 changes: 3 additions & 2 deletions core/embed/trezorhal/xdisplay_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#ifndef TREZORHAL_DISPLAY_LEGACY_H
#define TREZORHAL_DISPLAY_LEGACY_H

#include <buffers.h>
#include <stdint.h>
#include "buffers.h"
#include "fonts/fonts_types.h"

// These declarationscode emulates will be removed after the
// final cleanup of display drivers. They are here just to simplify
Expand All @@ -48,7 +49,7 @@ void display_pixeldata(uint16_t c);
uint32_t* display_get_fb_addr(void);

void display_clear(void);
void display_text_render_buffer(const char* text, int textlen, int font,
void display_text_render_buffer(const char* text, int textlen, font_id_t font,
buffer_text_t* buffer, int text_offset);

#define PIXELDATA(c) display_pixeldata(c)
Expand Down
42 changes: 21 additions & 21 deletions core/translations/cs.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"fonts": {
"##Safe3": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_cs.json",
"0_FONT_NORMAL": "font_pixeloperator_regular_8_cs.json",
"1_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_cs.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_cs.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_cs.json",
"4_FONT_BIG": "font_unifont_regular_16_cs.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_cs.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_cs.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_cs.json",
"8_FONT_SUB": null
"3_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_cs.json",
"4_FONT_DEMIBOLD": "font_unifont_bold_16_cs.json",
"5_FONT_MONO": "font_pixeloperatormono_regular_8_cs.json",
"6_FONT_BIG": "font_unifont_regular_16_cs.json",
"7_FONT_SUB": null
},
"T2T1": {
"1_FONT_NORMAL": "font_tthoves_regular_21_cs.json",
"0_FONT_NORMAL": "font_tthoves_regular_21_cs.json",
"1_FONT_NORMAL_UPPER": null,
"2_FONT_BOLD": null,
"3_FONT_MONO": "font_robotomono_medium_20_cs.json",
"4_FONT_BIG": null,
"5_FONT_DEMIBOLD": "font_tthoves_demibold_21_cs.json",
"6_FONT_NORMAL_UPPER": null,
"7_FONT_BOLD_UPPER": "font_tthoves_bold_17_upper_cs.json",
"8_FONT_SUB": null
"3_FONT_BOLD_UPPER": "font_tthoves_bold_17_upper_cs.json",
"4_FONT_DEMIBOLD": "font_tthoves_demibold_21_cs.json",
"5_FONT_MONO": "font_robotomono_medium_20_cs.json",
"6_FONT_BIG": null,
"7_FONT_SUB": null
},
"T3T1": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_cs.json",
"0_FONT_NORMAL": "font_ttsatoshi_demibold_21_cs.json",
"1_FONT_NORMAL_UPPER": null,
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_cs.json",
"3_FONT_MONO": "font_robotomono_medium_21_cs.json",
"4_FONT_BIG": null,
"5_FONT_DEMIBOLD": "font_ttsatoshi_demibold_21_cs.json",
"6_FONT_NORMAL_UPPER": null,
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_cs.json"
"3_FONT_BOLD_UPPER": null,
"4_FONT_DEMIBOLD": "font_ttsatoshi_demibold_21_cs.json",
"5_FONT_MONO": "font_robotomono_medium_21_cs.json",
"6_FONT_BIG": null,
"7_FONT_SUB": "font_ttsatoshi_demibold_18_cs.json"
}
},
"header": {
Expand Down
Loading

0 comments on commit 1fce051

Please sign in to comment.