mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
Stop wrapping FontsImpl in an Arc<Mutex<_>>
We never need to clone it, and none of its methods took `self` as mutable, meaning it wasn't making use of the semantic difference between the two. This API is about to get reworked, and simplifying it is a first step.
This commit is contained in:
@@ -299,7 +299,7 @@ impl Shape {
|
||||
|
||||
#[expect(clippy::needless_pass_by_value)]
|
||||
pub fn text(
|
||||
fonts: &Fonts,
|
||||
fonts: &mut Fonts,
|
||||
pos: Pos2,
|
||||
anchor: Align2,
|
||||
text: impl ToString,
|
||||
|
||||
@@ -181,7 +181,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn text_bounding_box_under_rotation() {
|
||||
let fonts = Fonts::new(
|
||||
let mut fonts = Fonts::new(
|
||||
1.0,
|
||||
1024,
|
||||
AlphaFromCoverage::default(),
|
||||
@@ -190,7 +190,7 @@ mod tests {
|
||||
let font = FontId::monospace(12.0);
|
||||
|
||||
let mut t = crate::Shape::text(
|
||||
&fonts,
|
||||
&mut fonts,
|
||||
Pos2::ZERO,
|
||||
emath::Align2::CENTER_CENTER,
|
||||
"testing123",
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
AlphaFromCoverage, TextureAtlas,
|
||||
mutex::{Mutex, MutexGuard},
|
||||
mutex::Mutex,
|
||||
text::{
|
||||
Galley, LayoutJob, LayoutSection,
|
||||
font::{Font, FontImpl},
|
||||
@@ -418,8 +418,10 @@ impl FontDefinitions {
|
||||
/// If you are using `egui`, use `egui::Context::set_fonts` and `egui::Context::fonts`.
|
||||
///
|
||||
/// You need to call [`Self::begin_pass`] and [`Self::font_image_delta`] once every frame.
|
||||
#[derive(Clone)]
|
||||
pub struct Fonts(Arc<Mutex<FontsAndCache>>);
|
||||
pub struct Fonts {
|
||||
pub fonts: FontsImpl,
|
||||
galley_cache: GalleyCache,
|
||||
}
|
||||
|
||||
impl Fonts {
|
||||
/// Create a new [`Fonts`] for text layout.
|
||||
@@ -433,7 +435,7 @@ impl Fonts {
|
||||
text_alpha_from_coverage: AlphaFromCoverage,
|
||||
definitions: FontDefinitions,
|
||||
) -> Self {
|
||||
let fonts_and_cache = FontsAndCache {
|
||||
Self {
|
||||
fonts: FontsImpl::new(
|
||||
pixels_per_point,
|
||||
max_texture_side,
|
||||
@@ -441,8 +443,7 @@ impl Fonts {
|
||||
definitions,
|
||||
),
|
||||
galley_cache: Default::default(),
|
||||
};
|
||||
Self(Arc::new(Mutex::new(fonts_and_cache)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Call at the start of each frame with the latest known
|
||||
@@ -453,27 +454,25 @@ impl Fonts {
|
||||
/// This function will react to changes in `pixels_per_point`, `max_texture_side`, and `text_alpha_from_coverage`,
|
||||
/// as well as notice when the font atlas is getting full, and handle that.
|
||||
pub fn begin_pass(
|
||||
&self,
|
||||
&mut self,
|
||||
pixels_per_point: f32,
|
||||
max_texture_side: usize,
|
||||
text_alpha_from_coverage: AlphaFromCoverage,
|
||||
) {
|
||||
let mut fonts_and_cache = self.0.lock();
|
||||
|
||||
let pixels_per_point_changed = fonts_and_cache.fonts.pixels_per_point != pixels_per_point;
|
||||
let max_texture_side_changed = fonts_and_cache.fonts.max_texture_side != max_texture_side;
|
||||
let pixels_per_point_changed = self.fonts.pixels_per_point != pixels_per_point;
|
||||
let max_texture_side_changed = self.fonts.max_texture_side != max_texture_side;
|
||||
let text_alpha_from_coverage_changed =
|
||||
fonts_and_cache.fonts.atlas.lock().text_alpha_from_coverage != text_alpha_from_coverage;
|
||||
let font_atlas_almost_full = fonts_and_cache.fonts.atlas.lock().fill_ratio() > 0.8;
|
||||
self.fonts.atlas.lock().text_alpha_from_coverage != text_alpha_from_coverage;
|
||||
let font_atlas_almost_full = self.fonts.atlas.lock().fill_ratio() > 0.8;
|
||||
let needs_recreate = pixels_per_point_changed
|
||||
|| max_texture_side_changed
|
||||
|| text_alpha_from_coverage_changed
|
||||
|| font_atlas_almost_full;
|
||||
|
||||
if needs_recreate {
|
||||
let definitions = fonts_and_cache.fonts.definitions.clone();
|
||||
let definitions = self.fonts.definitions.clone();
|
||||
|
||||
*fonts_and_cache = FontsAndCache {
|
||||
*self = Self {
|
||||
fonts: FontsImpl::new(
|
||||
pixels_per_point,
|
||||
max_texture_side,
|
||||
@@ -484,83 +483,70 @@ impl Fonts {
|
||||
};
|
||||
}
|
||||
|
||||
fonts_and_cache.galley_cache.flush_cache();
|
||||
self.galley_cache.flush_cache();
|
||||
}
|
||||
|
||||
/// Call at the end of each frame (before painting) to get the change to the font texture since last call.
|
||||
pub fn font_image_delta(&self) -> Option<crate::ImageDelta> {
|
||||
self.lock().fonts.atlas.lock().take_delta()
|
||||
}
|
||||
|
||||
/// Access the underlying [`FontsAndCache`].
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
pub fn lock(&self) -> MutexGuard<'_, FontsAndCache> {
|
||||
self.0.lock()
|
||||
self.fonts.atlas.lock().take_delta()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pixels_per_point(&self) -> f32 {
|
||||
self.lock().fonts.pixels_per_point
|
||||
self.fonts.pixels_per_point
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn max_texture_side(&self) -> usize {
|
||||
self.lock().fonts.max_texture_side
|
||||
self.fonts.max_texture_side
|
||||
}
|
||||
|
||||
/// The font atlas.
|
||||
/// Pass this to [`crate::Tessellator`].
|
||||
pub fn texture_atlas(&self) -> Arc<Mutex<TextureAtlas>> {
|
||||
self.lock().fonts.atlas.clone()
|
||||
self.fonts.atlas.clone()
|
||||
}
|
||||
|
||||
/// The full font atlas image.
|
||||
#[inline]
|
||||
pub fn image(&self) -> crate::ColorImage {
|
||||
self.lock().fonts.atlas.lock().image().clone()
|
||||
self.fonts.atlas.lock().image().clone()
|
||||
}
|
||||
|
||||
/// Current size of the font image.
|
||||
/// Pass this to [`crate::Tessellator`].
|
||||
pub fn font_image_size(&self) -> [usize; 2] {
|
||||
self.lock().fonts.atlas.lock().size()
|
||||
self.fonts.atlas.lock().size()
|
||||
}
|
||||
|
||||
/// Width of this character in points.
|
||||
#[inline]
|
||||
pub fn glyph_width(&self, font_id: &FontId, c: char) -> f32 {
|
||||
self.lock().fonts.glyph_width(font_id, c)
|
||||
pub fn glyph_width(&mut self, font_id: &FontId, c: char) -> f32 {
|
||||
self.fonts.glyph_width(font_id, c)
|
||||
}
|
||||
|
||||
/// Can we display this glyph?
|
||||
#[inline]
|
||||
pub fn has_glyph(&self, font_id: &FontId, c: char) -> bool {
|
||||
self.lock().fonts.has_glyph(font_id, c)
|
||||
pub fn has_glyph(&mut self, font_id: &FontId, c: char) -> bool {
|
||||
self.fonts.has_glyph(font_id, c)
|
||||
}
|
||||
|
||||
/// Can we display all the glyphs in this text?
|
||||
pub fn has_glyphs(&self, font_id: &FontId, s: &str) -> bool {
|
||||
self.lock().fonts.has_glyphs(font_id, s)
|
||||
pub fn has_glyphs(&mut self, font_id: &FontId, s: &str) -> bool {
|
||||
self.fonts.has_glyphs(font_id, s)
|
||||
}
|
||||
|
||||
/// Height of one row of text in points.
|
||||
///
|
||||
/// Returns a value rounded to [`emath::GUI_ROUNDING`].
|
||||
#[inline]
|
||||
pub fn row_height(&self, font_id: &FontId) -> f32 {
|
||||
self.lock().fonts.row_height(font_id)
|
||||
pub fn row_height(&mut self, font_id: &FontId) -> f32 {
|
||||
self.fonts.row_height(font_id)
|
||||
}
|
||||
|
||||
/// List of all known font families.
|
||||
pub fn families(&self) -> Vec<FontFamily> {
|
||||
self.lock()
|
||||
.fonts
|
||||
.definitions
|
||||
.families
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect()
|
||||
self.fonts.definitions.families.keys().cloned().collect()
|
||||
}
|
||||
|
||||
/// Layout some text.
|
||||
@@ -571,12 +557,14 @@ impl Fonts {
|
||||
///
|
||||
/// The implementation uses memoization so repeated calls are cheap.
|
||||
#[inline]
|
||||
pub fn layout_job(&self, job: LayoutJob) -> Arc<Galley> {
|
||||
self.lock().layout_job(job)
|
||||
pub fn layout_job(&mut self, job: LayoutJob) -> Arc<Galley> {
|
||||
let allow_split_paragraphs = true; // Optimization for editing text with many paragraphs.
|
||||
self.galley_cache
|
||||
.layout(&mut self.fonts, job, allow_split_paragraphs)
|
||||
}
|
||||
|
||||
pub fn num_galleys_in_cache(&self) -> usize {
|
||||
self.lock().galley_cache.num_galleys_in_cache()
|
||||
self.galley_cache.num_galleys_in_cache()
|
||||
}
|
||||
|
||||
/// How full is the font atlas?
|
||||
@@ -584,14 +572,14 @@ impl Fonts {
|
||||
/// This increases as new fonts and/or glyphs are used,
|
||||
/// but can also decrease in a call to [`Self::begin_pass`].
|
||||
pub fn font_atlas_fill_ratio(&self) -> f32 {
|
||||
self.lock().fonts.atlas.lock().fill_ratio()
|
||||
self.fonts.atlas.lock().fill_ratio()
|
||||
}
|
||||
|
||||
/// Will wrap text at the given width and line break at `\n`.
|
||||
///
|
||||
/// The implementation uses memoization so repeated calls are cheap.
|
||||
pub fn layout(
|
||||
&self,
|
||||
&mut self,
|
||||
text: String,
|
||||
font_id: FontId,
|
||||
color: crate::Color32,
|
||||
@@ -605,7 +593,7 @@ impl Fonts {
|
||||
///
|
||||
/// The implementation uses memoization so repeated calls are cheap.
|
||||
pub fn layout_no_wrap(
|
||||
&self,
|
||||
&mut self,
|
||||
text: String,
|
||||
font_id: FontId,
|
||||
color: crate::Color32,
|
||||
@@ -618,7 +606,7 @@ impl Fonts {
|
||||
///
|
||||
/// The implementation uses memoization so repeated calls are cheap.
|
||||
pub fn layout_delayed_color(
|
||||
&self,
|
||||
&mut self,
|
||||
text: String,
|
||||
font_id: FontId,
|
||||
wrap_width: f32,
|
||||
@@ -629,19 +617,6 @@ impl Fonts {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
pub struct FontsAndCache {
|
||||
pub fonts: FontsImpl,
|
||||
galley_cache: GalleyCache,
|
||||
}
|
||||
|
||||
impl FontsAndCache {
|
||||
fn layout_job(&mut self, job: LayoutJob) -> Arc<Galley> {
|
||||
let allow_split_paragraphs = true; // Optimization for editing text with many paragraphs.
|
||||
self.galley_cache
|
||||
.layout(&mut self.fonts, job, allow_split_paragraphs)
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// The collection of fonts used by `epaint`.
|
||||
|
||||
@@ -184,7 +184,7 @@ impl LayoutJob {
|
||||
/// The height of the tallest font used in the job.
|
||||
///
|
||||
/// Returns a value rounded to [`emath::GUI_ROUNDING`].
|
||||
pub fn font_height(&self, fonts: &crate::Fonts) -> f32 {
|
||||
pub fn font_height(&self, fonts: &mut crate::Fonts) -> f32 {
|
||||
let mut max_height = 0.0_f32;
|
||||
for section in &self.sections {
|
||||
max_height = max_height.max(fonts.row_height(§ion.format.font_id));
|
||||
|
||||
Reference in New Issue
Block a user