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

Context::add_font #5228

Merged
merged 14 commits into from
Oct 23, 2024
59 changes: 58 additions & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use epaint::{
pos2,
stats::PaintStats,
tessellator,
text::Fonts,
text::{FontInsert, Fonts},
util::OrderedFloat,
vec2, ClippedPrimitive, ClippedShape, Color32, ImageData, ImageDelta, Pos2, Rect,
TessellationOptions, TextureAtlas, TextureId, Vec2,
Expand Down Expand Up @@ -582,6 +582,29 @@ impl ContextImpl {
log::trace!("Loading new font definitions");
}

if !self.memory.add_fonts.is_empty() {
let fonts = self.memory.add_fonts.drain(..);
for font in fonts {
self.fonts.clear();
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved
self.font_definitions
.font_data
.insert(font.name.clone(), font.data);
let fam = self
.font_definitions
.families
.entry(font.family)
.or_default();
if font.family_append {
fam.push(font.name);
} else {
fam.insert(0, font.name);
};
}

#[cfg(feature = "log")]
log::trace!("Loading new font definitions");
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved
}

let mut is_new = false;

let fonts = self
Expand Down Expand Up @@ -1727,6 +1750,7 @@ impl Context {
/// but you can call this to install additional fonts that support e.g. korean characters.
///
/// The new fonts will become active at the start of the next pass.
/// This will overwrite the existing fonts
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
crate::profile_function!();

Expand All @@ -1748,6 +1772,39 @@ impl Context {
}
}

/// Tell `egui` which fonts to use.
///
/// The default `egui` fonts only support latin and cyrillic alphabets,
/// but you can call this to install additional fonts that support e.g. korean characters.
///
/// The new font will become active at the start of the next pass.
/// This will keep the existing fonts
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved
pub fn add_font(&self, new_font: FontInsert) {
crate::profile_function!();

let pixels_per_point = self.pixels_per_point();

let mut update_fonts = true;

self.read(|ctx| {
if let Some(current_fonts) = ctx.fonts.get(&pixels_per_point.into()) {
if current_fonts
.lock()
.fonts
.definitions()
.font_data
.contains_key(&new_font.name)
{
update_fonts = false; // no need to update
}
}
});

if update_fonts {
self.memory_mut(|mem| mem.add_fonts.push(new_font));
}
}

/// Does the OS use dark or light mode?
/// This is used when the theme preference is set to [`crate::ThemePreference::System`].
pub fn system_theme(&self) -> Option<Theme> {
Expand Down
5 changes: 5 additions & 0 deletions crates/egui/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub struct Memory {
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>,

/// add new font that will be applied at the start of the next frame
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) add_fonts: Vec<epaint::text::FontInsert>,

// Current active viewport
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) viewport_id: ViewportId,
Expand Down Expand Up @@ -119,6 +123,7 @@ impl Default for Memory {
layer_transforms: Default::default(),
popup: Default::default(),
everything_is_visible: Default::default(),
add_fonts: Default::default(),
};
slf.interactions.entry(slf.viewport_id).or_default();
slf.areas.entry(slf.viewport_id).or_default();
Expand Down
28 changes: 28 additions & 0 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,34 @@ pub struct FontDefinitions {
pub families: BTreeMap<FontFamily, Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct FontInsert {
/// Font name
pub name: String,

/// A `.ttf` or `.otf` file and a font face index.
pub data: FontData,

/// Sets the font family
pub family: FontFamily,
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved

/// If append is true & there are multiple fonts for that font family the font will be added as fallback.
/// This decides if the font is added to the front or the back.
/// So the first font is the primary, and then comes a list of fallbacks in order of priority.
pub family_append: bool,
frederik-uni marked this conversation as resolved.
Show resolved Hide resolved
}

impl FontInsert {
pub fn new(name: &str, data: FontData, family: FontFamily, primary_font: bool) -> Self {
Self {
name: name.to_owned(),
data,
family,
family_append: !primary_font,
}
}
}

impl Default for FontDefinitions {
/// Specifies the default fonts if the feature `default_fonts` is enabled,
/// otherwise this is the same as [`Self::empty`].
Expand Down
4 changes: 3 additions & 1 deletion crates/epaint/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ mod text_layout_types;
pub const TAB_SIZE: usize = 4;

pub use {
fonts::{FontData, FontDefinitions, FontFamily, FontId, FontTweak, Fonts, FontsImpl},
fonts::{
FontData, FontDefinitions, FontFamily, FontId, FontInsert, FontTweak, Fonts, FontsImpl,
},
text_layout::layout,
text_layout_types::*,
};
Expand Down
Loading