Skip to content

Commit 753d87c

Browse files
committed
Put font data into Arc to reduce memory consumption.
And additionally make cloning `FontDefinitions` cheaper.
1 parent fba2dc8 commit 753d87c

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

crates/egui/src/context.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use epaint::{
99
pos2,
1010
stats::PaintStats,
1111
tessellator,
12-
text::{FontInsert, FontPriority, Fonts},
12+
text::{FontData, FontInsert, FontPriority, Fonts},
1313
util::OrderedFloat,
1414
vec2, ClippedPrimitive, ClippedShape, Color32, ImageData, ImageDelta, Pos2, Rect,
1515
TessellationOptions, TextureAtlas, TextureId, Vec2,
@@ -597,7 +597,9 @@ impl ContextImpl {
597597
FontPriority::Lowest => fam.push(font.name.clone()),
598598
}
599599
}
600-
self.font_definitions.font_data.insert(font.name, font.data);
600+
self.font_definitions
601+
.font_data
602+
.insert(font.name, Arc::new(font.data));
601603
}
602604

603605
#[cfg(feature = "log")]
@@ -2940,7 +2942,13 @@ impl Context {
29402942

29412943
for (name, data) in &mut font_definitions.font_data {
29422944
ui.collapsing(name, |ui| {
2943-
if data.tweak.ui(ui).changed() {
2945+
let mut tweak = data.tweak;
2946+
if tweak.ui(ui).changed() {
2947+
*data = Arc::new(FontData {
2948+
font: data.font.clone(),
2949+
index: data.index,
2950+
tweak,
2951+
});
29442952
changed = true;
29452953
}
29462954
});

crates/epaint/src/text/fonts.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ fn ab_glyph_font_from_font_data(name: &str, data: &FontData) -> ab_glyph::FontAr
224224
///
225225
/// // Install my own font (maybe supporting non-latin characters):
226226
/// fonts.font_data.insert("my_font".to_owned(),
227-
/// FontData::from_static(include_bytes!("../../../epaint_default_fonts/fonts/Ubuntu-Light.ttf"))); // .ttf and .otf supported
227+
/// std::sync::Arc::new(
228+
/// // .ttf and .otf supported
229+
/// FontData::from_static(include_bytes!("../../../epaint_default_fonts/fonts/Ubuntu-Light.ttf")))
230+
/// );
228231
///
229232
/// // Put my font first (highest priority):
230233
/// fonts.families.get_mut(&FontFamily::Proportional).unwrap()
@@ -243,7 +246,7 @@ pub struct FontDefinitions {
243246
/// List of font names and their definitions.
244247
///
245248
/// `epaint` has built-in-default for these, but you can override them if you like.
246-
pub font_data: BTreeMap<String, FontData>,
249+
pub font_data: BTreeMap<String, Arc<FontData>>,
247250

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

315318
let mut families = BTreeMap::new();
316319

317-
font_data.insert("Hack".to_owned(), FontData::from_static(HACK_REGULAR));
320+
font_data.insert(
321+
"Hack".to_owned(),
322+
Arc::new(FontData::from_static(HACK_REGULAR)),
323+
);
318324

319325
// Some good looking emojis. Use as first priority:
320326
font_data.insert(
321327
"NotoEmoji-Regular".to_owned(),
322-
FontData::from_static(NOTO_EMOJI_REGULAR).tweak(FontTweak {
328+
Arc::new(FontData::from_static(NOTO_EMOJI_REGULAR).tweak(FontTweak {
323329
scale: 0.81, // Make smaller
324330
..Default::default()
325-
}),
331+
})),
326332
);
327333

328334
font_data.insert(
329335
"Ubuntu-Light".to_owned(),
330-
FontData::from_static(UBUNTU_LIGHT),
336+
Arc::new(FontData::from_static(UBUNTU_LIGHT)),
331337
);
332338

333339
// Bigger emojis, and more. <http://jslegers.github.io/emoji-icon-font/>:
334340
font_data.insert(
335341
"emoji-icon-font".to_owned(),
336-
FontData::from_static(EMOJI_ICON).tweak(FontTweak {
342+
Arc::new(FontData::from_static(EMOJI_ICON).tweak(FontTweak {
337343
scale: 0.90, // Make smaller
338344
..Default::default()
339-
}),
345+
})),
340346
);
341347

342348
families.insert(
@@ -795,7 +801,7 @@ impl FontImplCache {
795801
pub fn new(
796802
atlas: Arc<Mutex<TextureAtlas>>,
797803
pixels_per_point: f32,
798-
font_data: &BTreeMap<String, FontData>,
804+
font_data: &BTreeMap<String, Arc<FontData>>,
799805
) -> Self {
800806
let ab_glyph_fonts = font_data
801807
.iter()

examples/custom_font/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ fn replace_fonts(ctx: &egui::Context) {
4848
// .ttf and .otf files supported.
4949
fonts.font_data.insert(
5050
"my_font".to_owned(),
51-
egui::FontData::from_static(include_bytes!(
51+
std::sync::Arc::new(egui::FontData::from_static(include_bytes!(
5252
"../../../crates/epaint_default_fonts/fonts/Hack-Regular.ttf"
53-
)),
53+
))),
5454
);
5555

5656
// Put my font first (highest priority) for proportional text:

0 commit comments

Comments
 (0)