Skip to content

Commit

Permalink
fix(core/rust): correctly process and render TOIF icons
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Oct 6, 2021
1 parent 9d8ed08 commit c5c660b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
36 changes: 36 additions & 0 deletions core/embed/rust/src/trezorhal/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,24 @@ extern "C" {
fgcolor: cty::uint16_t,
bgcolor: cty::uint16_t,
);
fn display_toif_info(
data: *const cty::uint8_t,
len: cty::uint32_t,
out_w: *mut cty::uint16_t,
out_h: *mut cty::uint16_t,
out_grayscale: *mut bool,
) -> bool;
}

const WIDTH: i32 = 240;
const HEIGHT: i32 = 240;

pub struct ToifInfo {
pub width: u16,
pub height: u16,
pub grayscale: bool,
}

pub fn width() -> i32 {
WIDTH
}
Expand Down Expand Up @@ -92,3 +105,26 @@ pub fn icon(x: i32, y: i32, w: i32, h: i32, data: &[u8], fgcolor: u16, bgcolor:
)
}
}

pub fn toif_info(data: &[u8]) -> Result<ToifInfo, ()> {
let mut width: cty::uint16_t = 0;
let mut height: cty::uint16_t = 0;
let mut grayscale: bool = false;
if unsafe {
display_toif_info(
data.as_ptr() as _,
data.len() as _,
&mut width,
&mut height,
&mut grayscale,
)
} {
Ok(ToifInfo {
width,
height,
grayscale,
})
} else {
Err(())
}
}
9 changes: 6 additions & 3 deletions core/embed/rust/src/ui/component/model_tt/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ impl Component for Button {
);
}
ButtonContent::Icon(icon) => {
let size = Offset::uniform(theme::ICON_SIZE);
let area = Rect::from_center_and_size(self.area.center(), size);
display::icon(area, icon, style.text_color, style.button_color);
display::icon(
self.area.center(),
icon,
style.text_color,
style.button_color,
);
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions core/embed/rust/src/ui/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ pub fn rounded_rect(r: Rect, fg_color: Color, bg_color: Color, radius: u8) {
);
}

pub fn icon(r: Rect, data: &[u8], fg_color: Color, bg_color: Color) {
pub fn icon(center: Point, data: &[u8], fg_color: Color, bg_color: Color) {
let toif_info = display::toif_info(data).unwrap();
assert!(toif_info.grayscale);

let r = Rect::from_center_and_size(
center,
Offset::new(toif_info.width as _, toif_info.height as _),
);
display::icon(
r.x0,
r.y0,
r.width(),
r.height(),
data,
&data[12..], // skip TOIF header
fg_color.into(),
bg_color.into(),
);
Expand Down

0 comments on commit c5c660b

Please sign in to comment.