1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 21:00:03 -04:00

Make TextStyle !Copy

This commit is contained in:
Emil Ernerfeldt
2022-01-22 18:02:02 +01:00
parent 0df77d9074
commit 4f98f26fda
21 changed files with 126 additions and 92 deletions

View File

@@ -270,8 +270,8 @@ impl Font {
}
#[inline(always)]
pub fn text_style(&self) -> TextStyle {
self.text_style
pub fn text_style(&self) -> &TextStyle {
&self.text_style
}
#[inline(always)]

View File

@@ -11,7 +11,7 @@ use crate::{
// TODO: rename
/// One of a few categories of styles of text, e.g. body, button or heading.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum TextStyle {
@@ -37,7 +37,7 @@ impl TextStyle {
TextStyle::Monospace,
]
.iter()
.copied()
.cloned()
}
}
@@ -270,13 +270,13 @@ impl Fonts {
/// Width of this character in points.
#[inline]
pub fn glyph_width(&self, text_style: TextStyle, c: char) -> f32 {
pub fn glyph_width(&self, text_style: &TextStyle, c: char) -> f32 {
self.lock().fonts.glyph_width(text_style, c)
}
/// Height of one row of text. In points
#[inline]
pub fn row_height(&self, text_style: TextStyle) -> f32 {
pub fn row_height(&self, text_style: &TextStyle) -> f32 {
self.lock().fonts.row_height(text_style)
}
@@ -398,17 +398,17 @@ impl FontsImpl {
let fonts = definitions
.family_and_size
.iter()
.map(|(&text_style, &(family, scale_in_points))| {
let fonts = &definitions.fonts_for_family.get(&family);
.map(|(text_style, (family, scale_in_points))| {
let fonts = &definitions.fonts_for_family.get(family);
let fonts = fonts.unwrap_or_else(|| {
panic!("FontFamily::{:?} is not bound to any fonts", family)
});
let fonts: Vec<Arc<FontImpl>> = fonts
.iter()
.map(|font_name| font_impl_cache.font_impl(font_name, scale_in_points))
.map(|font_name| font_impl_cache.font_impl(font_name, *scale_in_points))
.collect();
(text_style, Font::new(text_style, fonts))
(text_style.clone(), Font::new(text_style.clone(), fonts))
})
.collect();
@@ -430,18 +430,18 @@ impl FontsImpl {
}
#[inline]
pub fn font_mut(&mut self, text_style: TextStyle) -> &mut Font {
self.fonts.get_mut(&text_style).unwrap()
pub fn font_mut(&mut self, text_style: &TextStyle) -> &mut Font {
self.fonts.get_mut(text_style).unwrap()
}
/// Width of this character in points.
fn glyph_width(&mut self, text_style: TextStyle, c: char) -> f32 {
fn glyph_width(&mut self, text_style: &TextStyle, c: char) -> f32 {
self.font_mut(text_style).glyph_width(c)
}
/// Height of one row of text. In points
fn row_height(&self, text_style: TextStyle) -> f32 {
self.fonts[&text_style].row_height()
fn row_height(&mut self, text_style: &TextStyle) -> f32 {
self.font_mut(text_style).row_height()
}
}

View File

@@ -86,7 +86,7 @@ fn layout_section(
byte_range,
format,
} = section;
let font = fonts.font_mut(format.style);
let font = fonts.font_mut(&format.style);
let font_height = font.row_height();
let mut paragraph = out_paragraphs.last_mut().unwrap();

View File

@@ -156,7 +156,7 @@ impl LayoutJob {
pub fn font_height(&self, fonts: &crate::Fonts) -> f32 {
let mut max_height = 0.0_f32;
for section in &self.sections {
max_height = max_height.max(fonts.row_height(section.format.style));
max_height = max_height.max(fonts.row_height(&section.format.style));
}
max_height
}
@@ -213,7 +213,7 @@ impl std::hash::Hash for LayoutSection {
// ----------------------------------------------------------------------------
#[derive(Copy, Clone, Debug, Hash, PartialEq)]
#[derive(Clone, Debug, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TextFormat {
pub style: TextStyle,