Add default_fonts feature: Turn off if you use your own fonts

This commit is contained in:
Emil Ernerfeldt 2020-12-26 21:20:55 +01:00
parent 6f5fd1b9c0
commit de614153b5
3 changed files with 75 additions and 47 deletions

View file

@ -20,7 +20,7 @@ include = [
[dependencies] [dependencies]
ahash = { version = "0.6", features = ["std"], default-features = false } ahash = { version = "0.6", features = ["std"], default-features = false }
atomic_refcell = { version = "0.1", optional = true } # Used instead of parking_lot when you are always using Egui in a single thread. About as fast as parking_lot. atomic_refcell = { version = "0.1", optional = true } # Used instead of parking_lot when you are always using Egui in a single thread. About as fast as parking_lot. Panics on multi-threaded use of egui::Context.
parking_lot = { version = "0.11", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios parking_lot = { version = "0.11", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios
rusttype = "0.9" rusttype = "0.9"
serde = { version = "1", features = ["derive"], optional = true } serde = { version = "1", features = ["derive"], optional = true }
@ -30,8 +30,14 @@ serde_json = { version = "1", optional = true }
criterion = { version = "0.3", default-features = false } criterion = { version = "0.3", default-features = false }
[features] [features]
default = ["atomic_refcell"] # Using the same egui::Context from multiple threads will result in a panic. default = ["atomic_refcell", "default_fonts"]
multi_threaded = ["parking_lot"] # Only needed if you plan to use the same egui::Context from multiple threads.
# If set, egui will use `include_bytes!` to bundle some fonts.
# If you plan on specifying your own fonts you may disable this feature.
default_fonts = []
# Only needed if you plan to use the same egui::Context from multiple threads.
multi_threaded = ["parking_lot"]
[[bench]] [[bench]]
name = "benchmark" name = "benchmark"

View file

@ -142,6 +142,7 @@ type FontIndex = usize;
// TODO: rename? // TODO: rename?
/// Wrapper over multiple `FontImpl` (e.g. a primary + fallbacks for emojis) /// Wrapper over multiple `FontImpl` (e.g. a primary + fallbacks for emojis)
#[derive(Default)]
pub struct Font { pub struct Font {
fonts: Vec<Arc<FontImpl>>, fonts: Vec<Arc<FontImpl>>,
replacement_glyph: (FontIndex, GlyphInfo), replacement_glyph: (FontIndex, GlyphInfo),
@ -152,7 +153,10 @@ pub struct Font {
impl Font { impl Font {
pub fn new(fonts: Vec<Arc<FontImpl>>) -> Self { pub fn new(fonts: Vec<Arc<FontImpl>>) -> Self {
assert!(!fonts.is_empty()); if fonts.is_empty() {
return Default::default();
}
let pixels_per_point = fonts[0].pixels_per_point(); let pixels_per_point = fonts[0].pixels_per_point();
let row_height = fonts[0].row_height(); let row_height = fonts[0].row_height();
@ -239,6 +243,7 @@ impl Font {
let mut last_glyph_id = None; let mut last_glyph_id = None;
for c in text.chars() { for c in text.chars() {
if !self.fonts.is_empty() {
let (font_index, glyph_info) = self.glyph_info(c); let (font_index, glyph_info) = self.glyph_info(c);
let font_impl = &self.fonts[font_index]; let font_impl = &self.fonts[font_index];
@ -248,6 +253,7 @@ impl Font {
cursor_x_in_points += glyph_info.advance_width; cursor_x_in_points += glyph_info.advance_width;
cursor_x_in_points = self.round_to_pixel(cursor_x_in_points); cursor_x_in_points = self.round_to_pixel(cursor_x_in_points);
last_glyph_id = Some(glyph_info.id); last_glyph_id = Some(glyph_info.id);
}
x_offsets.push(cursor_x_in_points); x_offsets.push(cursor_x_in_points);
} }

View file

@ -89,7 +89,15 @@ impl Default for FontDefinitions {
impl FontDefinitions { impl FontDefinitions {
/// Default values for the fonts /// Default values for the fonts
pub fn default_with_pixels_per_point(pixels_per_point: f32) -> Self { pub fn default_with_pixels_per_point(pixels_per_point: f32) -> Self {
#[allow(unused)]
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new(); let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
let mut fonts_for_family = BTreeMap::new();
#[cfg(feature = "default_fonts")]
{
// TODO: figure out a way to make the WASM smaller despite including fonts. Zip them?
// Use size 13 for this. NOTHING ELSE: // Use size 13 for this. NOTHING ELSE:
font_data.insert( font_data.insert(
"ProggyClean".to_owned(), "ProggyClean".to_owned(),
@ -100,7 +108,7 @@ impl FontDefinitions {
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf")), std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf")),
); );
// Few, but good looking. Use as first priority: // Some good looking emojis. Use as first priority:
font_data.insert( font_data.insert(
"NotoEmoji-Regular".to_owned(), "NotoEmoji-Regular".to_owned(),
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")), std::borrow::Cow::Borrowed(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")),
@ -111,9 +119,6 @@ impl FontDefinitions {
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/emoji-icon-font.ttf")), std::borrow::Cow::Borrowed(include_bytes!("../../fonts/emoji-icon-font.ttf")),
); );
// TODO: figure out a way to make the WASM smaller despite including fonts. Zip them?
let mut fonts_for_family = BTreeMap::new();
fonts_for_family.insert( fonts_for_family.insert(
FontFamily::Monospace, FontFamily::Monospace,
vec![ vec![
@ -131,6 +136,13 @@ impl FontDefinitions {
"emoji-icon-font".to_owned(), "emoji-icon-font".to_owned(),
], ],
); );
}
#[cfg(not(feature = "default_fonts"))]
{
fonts_for_family.insert(FontFamily::Monospace, vec![]);
fonts_for_family.insert(FontFamily::VariableWidth, vec![]);
}
let mut family_and_size = BTreeMap::new(); let mut family_and_size = BTreeMap::new();
family_and_size.insert(TextStyle::Small, (FontFamily::VariableWidth, 10.0)); family_and_size.insert(TextStyle::Small, (FontFamily::VariableWidth, 10.0));
@ -196,7 +208,11 @@ impl Fonts {
.family_and_size .family_and_size
.iter() .iter()
.map(|(&text_style, &(family, scale_in_points))| { .map(|(&text_style, &(family, scale_in_points))| {
let fonts: Vec<Arc<FontImpl>> = self.definitions.fonts_for_family[&family] let fonts = &self.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() .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(); .collect();