1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-08-02 20:08:31 +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 *get_utf8_glyph(uint16_t char_code, int font);
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);

View File

@ -72,7 +72,7 @@ pub fn char_width(ch: char, font: i32) -> i16 {
text_width(encoding, font) 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) } unsafe { ffi::font_get_glyph(font, ch) }
} }

View File

@ -144,22 +144,10 @@ impl Font {
display::text_width(text, self.into()) 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 /// Supports UTF8 characters
fn get_first_glyph_from_text(self, text: &str) -> Option<Glyph> { fn get_first_glyph_from_text(self, text: &str) -> Option<Glyph> {
if let Some(c) = text.chars().next() { if let Some(c) = text.chars().next() {
self.get_glyph_from_char(c) Some(self.get_glyph(c))
} else { } else {
None None
} }
@ -168,7 +156,7 @@ impl Font {
/// Supports UTF8 characters /// Supports UTF8 characters
fn get_last_glyph_from_text(self, text: &str) -> Option<Glyph> { fn get_last_glyph_from_text(self, text: &str) -> Option<Glyph> {
if let Some(c) = text.chars().next_back() { if let Some(c) = text.chars().next_back() {
self.get_glyph_from_char(c) Some(self.get_glyph(c))
} else { } else {
None None
} }
@ -236,25 +224,21 @@ impl Font {
constant::LINE_SPACE + self.text_height() constant::LINE_SPACE + self.text_height()
} }
pub fn get_glyph(self, char_byte: u8) -> Option<Glyph> { pub fn get_glyph(self, ch: char) -> Glyph {
let gl_data = display::get_char_glyph(char_byte, self.into()); let gl_data = display::get_char_glyph(ch as u16, self.into());
if gl_data.is_null() { ensure!(!gl_data.is_null(), "Failed to load glyph");
return None;
}
// SAFETY: Glyph::load is valid for data returned by get_char_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) { 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 colortable = get_color_table(fg_color, bg_color);
let mut adv_total = 0; let mut adv_total = 0;
for c in text.bytes() { for c in text.chars() {
let g = self.get_glyph(c); let gly = self.get_glyph(c);
if let Some(gly) = g { let adv = gly.print(baseline + Offset::new(adv_total, 0), colortable);
let adv = gly.print(baseline + Offset::new(adv_total, 0), colortable); adv_total += adv;
adv_total += adv;
}
} }
} }

View File

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