Skip to content

Commit

Permalink
refactor(core): switch fonts from C to Rust
Browse files Browse the repository at this point in the history
- switch common and individual layout components to Rust fonts
- font usage changed from enum Font to pointers to FontInfo structs

[no changelog]
  • Loading branch information
obrusvit committed Feb 6, 2025
1 parent 9638c7e commit be45901
Show file tree
Hide file tree
Showing 43 changed files with 405 additions and 441 deletions.
4 changes: 0 additions & 4 deletions core/embed/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@ fn generate_trezorhal_bindings() {
.allowlist_function("gfx_mono8_blend_mono1p")
.allowlist_function("gfx_mono8_blend_mono4")
.allowlist_function("gfx_bitblt_wait")
// fonts
.allowlist_type("font_info_t")
.allowlist_function("get_font_info")
// .allowlist_function("font_get_glyph")
// uzlib
.allowlist_function("uzlib_uncompress_init")
.allowlist_function("uzlib_uncompress")
Expand Down
15 changes: 0 additions & 15 deletions core/embed/rust/src/trezorhal/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,10 @@ use ffi::{DISPLAY_RESX_, DISPLAY_RESY_};
pub const DISPLAY_RESX: u32 = DISPLAY_RESX_;
pub const DISPLAY_RESY: u32 = DISPLAY_RESY_;

pub type FontInfo = ffi::font_info_t;

pub fn backlight(val: i32) -> i32 {
unsafe { ffi::display_set_backlight(val) }
}

pub fn get_font_info(font: i32) -> Option<FontInfo> {
// SAFETY:
// - `ffi::get_font_info` returns either null (for invalid fonts) or a pointer
// to a static font_info_t struct
// - The font_info_t data is in ROM, making it immutable and static
// - The font_info_t contains pointers to static glyph data arrays also in ROM
// - All font data is generated at compile time and included in the binary
unsafe {
let font = ffi::get_font_info(font as _);
Some(*font.as_ref()?)
}
}

pub fn sync() {
// NOTE: The sync operation is not called for tests because the linker
// would otherwise report missing symbols if the tests are built with ASAN.
Expand Down
4 changes: 2 additions & 2 deletions core/embed/rust/src/ui/component/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
strutil::TString,
ui::{
component::{Component, Event, EventCtx, Never, Pad},
display::{Color, Font},
display::{font::FONT_NORMAL, Color},
geometry::{Alignment, Offset, Rect},
shape::{self, Renderer},
},
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Component for Connect {
}

fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
let font = Font::NORMAL;
let font = FONT_NORMAL;

self.bg.render(target);

Expand Down
17 changes: 11 additions & 6 deletions core/embed/rust/src/ui/component/text/op.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
strutil::TString,
ui::{
display::{Color, Font},
display::{
font::{FONT_BOLD, FONT_BOLD_UPPER, FONT_DEMIBOLD, FONT_MONO, FONT_NORMAL},
Color, Font,
},
geometry::{Alignment, Offset, Rect},
util::ResultExt,
},
Expand Down Expand Up @@ -241,24 +244,26 @@ impl<'a> OpTextLayout<'a> {

// Op-adding aggregation operations
impl<'a> OpTextLayout<'a> {
// TODO: use TextStyle instead because we do not want e.g. BOLD_UPPER in all
// layouts
pub fn text_normal(self, text: impl Into<TString<'a>>) -> Self {
self.font(Font::NORMAL).text(text.into())
self.font(FONT_NORMAL).text(text.into())
}

pub fn text_mono(self, text: impl Into<TString<'a>>) -> Self {
self.font(Font::MONO).text(text.into())
self.font(FONT_MONO).text(text.into())
}

pub fn text_bold(self, text: impl Into<TString<'a>>) -> Self {
self.font(Font::BOLD).text(text.into())
self.font(FONT_BOLD).text(text.into())
}

pub fn text_bold_upper(self, text: impl Into<TString<'a>>) -> Self {
self.font(Font::BOLD_UPPER).text(text.into())
self.font(FONT_BOLD_UPPER).text(text.into())
}

pub fn text_demibold(self, text: impl Into<TString<'a>>) -> Self {
self.font(Font::DEMIBOLD).text(text.into())
self.font(FONT_DEMIBOLD).text(text.into())
}

pub fn chunkify_text(self, chunks: Option<(Chunks, i16)>) -> Self {
Expand Down
13 changes: 8 additions & 5 deletions core/embed/rust/src/ui/component/text/paragraphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use crate::{
strutil::TString,
ui::{
component::{Component, Event, EventCtx, Never, Paginate},
display::{toif::Icon, Color, Font},
display::{
font::{FONT_NORMAL, FONT_SUB},
toif::Icon,
Color,
},
geometry::{Alignment, Dimensions, Insets, LinearPlacement, Offset, Point, Rect},
shape,
shape::Renderer,
shape::{self, Renderer},
},
};

Expand Down Expand Up @@ -626,7 +629,7 @@ where
} else {
// current and future tasks - ordinal numbers or icon on current task
if self.show_numerals {
let num_offset = Offset::new(4, Font::NORMAL.visible_text_height("1"));
let num_offset = Offset::new(4, FONT_NORMAL.visible_text_height("1"));
self.render_numeral(base + num_offset, i, l.style.text_color, target);
} else if i == current_visible {
let color = l.style.text_color;
Expand All @@ -645,7 +648,7 @@ where
) {
let numeral = uformat!("{}.", n + 1);
shape::Text::new(base_point, numeral.as_str())
.with_font(Font::SUB)
.with_font(FONT_SUB)
.with_fg(color)
.render(target);
}
Expand Down
Loading

0 comments on commit be45901

Please sign in to comment.