1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-08-02 11:58:32 +00:00

WIP - tweak get_glyph

This commit is contained in:
matejcik 2024-01-15 10:13:01 +01:00
parent 03ceecba15
commit 751b876397
4 changed files with 14 additions and 36 deletions

View File

@ -1,7 +1 @@
typedef struct {
const uint8_t* ptr;
uint32_t len;
} PointerData;
// TODO: Theoretically, the `len` is not used by the client and does not have to be sent
PointerData get_utf8_glyph(uint16_t char_code, int font);
const uint8_t *get_utf8_glyph(uint16_t char_code, int font);

View File

@ -72,7 +72,7 @@ pub fn char_width(ch: char, font: i32) -> i16 {
text_width(encoding, font)
}
pub fn get_char_glyph(ch: u8, font: i32) -> *const u8 {
pub fn get_char_glyph(ch: u16, font: i32) -> *const u8 {
unsafe { ffi::font_get_glyph(font, ch) }
}

View File

@ -144,22 +144,10 @@ impl Font {
display::text_width(text, self.into())
}
/// Supports UTF8 characters
fn get_glyph_from_char(self, c: char) -> Option<Glyph> {
let mut buffer = [0; 4];
let bytes = c.encode_utf8(&mut buffer);
for byte in bytes.bytes() {
if let Some(glyph) = self.get_glyph(byte) {
return Some(glyph);
}
}
None
}
/// Supports UTF8 characters
fn get_first_glyph_from_text(self, text: &str) -> Option<Glyph> {
if let Some(c) = text.chars().next() {
self.get_glyph_from_char(c)
Some(self.get_glyph(c))
} else {
None
}
@ -168,7 +156,7 @@ impl Font {
/// Supports UTF8 characters
fn get_last_glyph_from_text(self, text: &str) -> Option<Glyph> {
if let Some(c) = text.chars().next_back() {
self.get_glyph_from_char(c)
Some(self.get_glyph(c))
} else {
None
}
@ -236,27 +224,23 @@ impl Font {
constant::LINE_SPACE + self.text_height()
}
pub fn get_glyph(self, char_byte: u8) -> Option<Glyph> {
let gl_data = display::get_char_glyph(char_byte, self.into());
pub fn get_glyph(self, ch: char) -> Glyph {
let gl_data = display::get_char_glyph(ch as u16, self.into());
if gl_data.is_null() {
return None;
}
ensure!(!gl_data.is_null(), "Failed to load glyph");
// SAFETY: Glyph::load is valid for data returned by get_char_glyph
unsafe { Some(Glyph::load(gl_data)) }
unsafe { Glyph::load(gl_data) }
}
pub fn display_text(self, text: &str, baseline: Point, fg_color: Color, bg_color: Color) {
let colortable = get_color_table(fg_color, bg_color);
let mut adv_total = 0;
for c in text.bytes() {
let g = self.get_glyph(c);
if let Some(gly) = g {
for c in text.chars() {
let gly = self.get_glyph(c);
let adv = gly.print(baseline + Offset::new(adv_total, 0), colortable);
adv_total += adv;
}
}
}
/// Get the length of the longest suffix from a given `text`
/// that will fit into the area `width` pixels wide.

View File

@ -400,8 +400,8 @@ impl<T: AsRef<str>> TextOverlay<T> {
for g in self
.text
.as_ref()
.bytes()
.filter_map(|c| self.font.get_glyph(c))
.chars()
.map(|c| self.font.get_glyph(c))
{
let top = self.max_height - self.baseline - g.bearing_y;
let char_area = Rect::new(