More font handling cleanup

Group all fields we need to determine a physical font into one FontRequest
structure and use that throughout.

That way adding more fields will require less changes.
This commit is contained in:
Simon Hausmann 2020-11-24 09:00:07 +01:00
parent 05204a3185
commit f0289192b7
5 changed files with 42 additions and 63 deletions

View file

@ -529,8 +529,7 @@ impl Item for Text {
) -> HighLevelRenderingPrimitive {
HighLevelRenderingPrimitive::Text {
text: Self::FIELD_OFFSETS.text.apply_pin(self).get(),
font_family: Self::FIELD_OFFSETS.font_family.apply_pin(self).get(),
font_size: Text::font_pixel_size(self, window),
font_request: self.font_request(window),
}
}
@ -562,16 +561,12 @@ impl Item for Text {
}
fn layouting_info(self: Pin<&Self>, window: &ComponentWindow) -> LayoutInfo {
let font_family = Self::FIELD_OFFSETS.font_family.apply_pin(self).get();
let font_size = Text::font_pixel_size(self, window);
let text = Self::FIELD_OFFSETS.text.apply_pin(self).get();
crate::font::FONT_CACHE.with(|fc| {
let font = fc.find_font(&font_family, font_size);
let width = font.text_width(&text);
let height = font.height();
LayoutInfo { min_width: width, min_height: height, ..LayoutInfo::default() }
})
let font = self.font(window);
let width = font.text_width(&text);
let height = font.height();
LayoutInfo { min_width: width, min_height: height, ..LayoutInfo::default() }
}
fn input_event(
@ -596,17 +591,6 @@ impl ItemConsts for Text {
Text::FIELD_OFFSETS.cached_rendering_data.as_unpinned_projection();
}
impl Text {
fn font_pixel_size(self: Pin<&Self>, window: &ComponentWindow) -> f32 {
let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get();
if font_size == 0.0 {
DEFAULT_FONT_SIZE * window.scale_factor()
} else {
font_size
}
}
}
impl HasFont for Pin<&Text> {
fn font_family(&self) -> SharedString {
<Self as core::ops::Deref>::Target::FIELD_OFFSETS.font_family.apply_pin(*self).get()
@ -1140,8 +1124,7 @@ impl Item for TextInput {
) -> HighLevelRenderingPrimitive {
HighLevelRenderingPrimitive::Text {
text: Self::FIELD_OFFSETS.text.apply_pin(self).get(),
font_family: Self::FIELD_OFFSETS.font_family.apply_pin(self).get(),
font_size: TextInput::font_pixel_size(self, window),
font_request: self.font_request(window),
}
}
@ -1369,17 +1352,6 @@ impl Item for TextInput {
}
}
impl TextInput {
fn font_pixel_size(self: Pin<&Self>, window: &ComponentWindow) -> f32 {
let font_size = Self::FIELD_OFFSETS.font_size.apply_pin(self).get();
if font_size == 0.0 {
DEFAULT_FONT_SIZE * window.scale_factor()
} else {
font_size
}
}
}
impl ItemConsts for TextInput {
const cached_rendering_data_offset: const_field_offset::FieldOffset<
TextInput,