Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put font data into Arc to reduce memory consumption #5276

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,9 @@ impl ContextImpl {
FontPriority::Lowest => fam.push(font.name.clone()),
}
}
self.font_definitions.font_data.insert(font.name, font.data);
self.font_definitions
.font_data
.insert(font.name, Arc::new(font.data));
}

#[cfg(feature = "log")]
Expand Down Expand Up @@ -2940,7 +2942,9 @@ impl Context {

for (name, data) in &mut font_definitions.font_data {
ui.collapsing(name, |ui| {
if data.tweak.ui(ui).changed() {
let mut tweak = data.tweak;
if tweak.ui(ui).changed() {
Arc::make_mut(data).tweak = tweak;
changed = true;
}
});
Expand Down
27 changes: 17 additions & 10 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ fn ab_glyph_font_from_font_data(name: &str, data: &FontData) -> ab_glyph::FontAr
///
/// // Install my own font (maybe supporting non-latin characters):
/// fonts.font_data.insert("my_font".to_owned(),
/// FontData::from_static(include_bytes!("../../../epaint_default_fonts/fonts/Ubuntu-Light.ttf"))); // .ttf and .otf supported
/// std::sync::Arc::new(
/// // .ttf and .otf supported
/// FontData::from_static(include_bytes!("../../../epaint_default_fonts/fonts/Ubuntu-Light.ttf"))
/// )
/// );
///
/// // Put my font first (highest priority):
/// fonts.families.get_mut(&FontFamily::Proportional).unwrap()
Expand All @@ -243,7 +247,7 @@ pub struct FontDefinitions {
/// List of font names and their definitions.
///
/// `epaint` has built-in-default for these, but you can override them if you like.
pub font_data: BTreeMap<String, FontData>,
pub font_data: BTreeMap<String, Arc<FontData>>,

/// Which fonts (names) to use for each [`FontFamily`].
///
Expand Down Expand Up @@ -310,33 +314,36 @@ impl Default for FontDefinitions {
/// otherwise this is the same as [`Self::empty`].
#[cfg(feature = "default_fonts")]
fn default() -> Self {
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
let mut font_data: BTreeMap<String, Arc<FontData>> = BTreeMap::new();

let mut families = BTreeMap::new();

font_data.insert("Hack".to_owned(), FontData::from_static(HACK_REGULAR));
font_data.insert(
"Hack".to_owned(),
Arc::new(FontData::from_static(HACK_REGULAR)),
);

// Some good looking emojis. Use as first priority:
font_data.insert(
"NotoEmoji-Regular".to_owned(),
FontData::from_static(NOTO_EMOJI_REGULAR).tweak(FontTweak {
Arc::new(FontData::from_static(NOTO_EMOJI_REGULAR).tweak(FontTweak {
scale: 0.81, // Make smaller
..Default::default()
}),
})),
);

font_data.insert(
"Ubuntu-Light".to_owned(),
FontData::from_static(UBUNTU_LIGHT),
Arc::new(FontData::from_static(UBUNTU_LIGHT)),
);

// Bigger emojis, and more. <http://jslegers.github.io/emoji-icon-font/>:
font_data.insert(
"emoji-icon-font".to_owned(),
FontData::from_static(EMOJI_ICON).tweak(FontTweak {
Arc::new(FontData::from_static(EMOJI_ICON).tweak(FontTweak {
scale: 0.90, // Make smaller
..Default::default()
}),
})),
);

families.insert(
Expand Down Expand Up @@ -795,7 +802,7 @@ impl FontImplCache {
pub fn new(
atlas: Arc<Mutex<TextureAtlas>>,
pixels_per_point: f32,
font_data: &BTreeMap<String, FontData>,
font_data: &BTreeMap<String, Arc<FontData>>,
) -> Self {
let ab_glyph_fonts = font_data
.iter()
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_font/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ fn replace_fonts(ctx: &egui::Context) {
// .ttf and .otf files supported.
fonts.font_data.insert(
"my_font".to_owned(),
egui::FontData::from_static(include_bytes!(
std::sync::Arc::new(egui::FontData::from_static(include_bytes!(
"../../../crates/epaint_default_fonts/fonts/Hack-Regular.ttf"
)),
))),
);

// Put my font first (highest priority) for proportional text:
Expand Down
Loading