mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-12 17:38:13 +00:00
fixup! refactor(core): safe iface for get_glyph_data
This commit is contained in:
parent
cadcede0b7
commit
77b3d79e01
@ -227,7 +227,12 @@ impl From<Font> for i32 {
|
|||||||
impl Font {
|
impl Font {
|
||||||
/// Supports UTF8 characters
|
/// Supports UTF8 characters
|
||||||
pub fn text_width(self, text: &str) -> i16 {
|
pub fn text_width(self, text: &str) -> i16 {
|
||||||
text.chars().fold(0, |acc, c| acc + self.char_width(c))
|
self.with_glyph_data(|data| {
|
||||||
|
text.chars().fold(0, |acc, c| {
|
||||||
|
let char_width = data.get_glyph(c).adv;
|
||||||
|
acc + char_width
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Width of the text that is visible.
|
/// Width of the text that is visible.
|
||||||
@ -350,17 +355,20 @@ impl Font {
|
|||||||
pub fn longest_prefix(self, width: i16, text: &str) -> &str {
|
pub fn longest_prefix(self, width: i16, text: &str) -> &str {
|
||||||
let mut prev_word_boundary = 0;
|
let mut prev_word_boundary = 0;
|
||||||
let mut text_width = 0;
|
let mut text_width = 0;
|
||||||
for (i, c) in text.char_indices() {
|
self.with_glyph_data(|data| {
|
||||||
let c_width = self.char_width(c);
|
for (i, c) in text.char_indices() {
|
||||||
if text_width + c_width > width {
|
let char_width = data.get_glyph(c).adv;
|
||||||
// Another character would not fit => split at the previous word boundary
|
let c_width = char_width;
|
||||||
return &text[0..prev_word_boundary];
|
if text_width + c_width > width {
|
||||||
|
// Another character would not fit => split at the previous word boundary
|
||||||
|
return &text[0..prev_word_boundary];
|
||||||
|
}
|
||||||
|
if c == ' ' {
|
||||||
|
prev_word_boundary = i;
|
||||||
|
}
|
||||||
|
text_width += c_width;
|
||||||
}
|
}
|
||||||
if c == ' ' {
|
});
|
||||||
prev_word_boundary = i;
|
|
||||||
}
|
|
||||||
text_width += c_width;
|
|
||||||
}
|
|
||||||
|
|
||||||
text // the whole text fits
|
text // the whole text fits
|
||||||
}
|
}
|
||||||
@ -369,14 +377,17 @@ impl Font {
|
|||||||
/// that will fit into the area `width` pixels wide.
|
/// that will fit into the area `width` pixels wide.
|
||||||
pub fn longest_suffix(self, width: i16, text: &str) -> usize {
|
pub fn longest_suffix(self, width: i16, text: &str) -> usize {
|
||||||
let mut text_width = 0;
|
let mut text_width = 0;
|
||||||
for (chars_from_right, c) in text.chars().rev().enumerate() {
|
|
||||||
let c_width = self.char_width(c);
|
self.with_glyph_data(|data| {
|
||||||
if text_width + c_width > width {
|
for (chars_from_right, c) in text.chars().rev().enumerate() {
|
||||||
// Another character cannot be fitted, we're done.
|
let char_width = data.get_glyph(c).adv;
|
||||||
return chars_from_right;
|
if text_width + char_width > width {
|
||||||
|
// Another character cannot be fitted, we're done.
|
||||||
|
return chars_from_right;
|
||||||
|
}
|
||||||
|
text_width += char_width;
|
||||||
}
|
}
|
||||||
text_width += c_width;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
text.len() // it fits in its entirety
|
text.len() // it fits in its entirety
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user