Skip to content
Open
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
2 changes: 1 addition & 1 deletion eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default = ["default_fonts", "glow"]

# detect dark mode system preference
dark-light = ["dep:dark-light"]

system_fonts = ["egui/system_fonts"]
# 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 = ["egui/default_fonts"]
Expand Down
2 changes: 1 addition & 1 deletion egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ deadlock_detection = ["epaint/deadlock_detection"]
# 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 = ["epaint/default_fonts"]

system_fonts = ["epaint/system_fonts"]
# Enable additional checks if debug assertions are enabled (debug builds).
extra_debug_asserts = ["epaint/extra_debug_asserts"]
# Always enable additional checks.
Expand Down
5 changes: 5 additions & 0 deletions epaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ mint = ["emath/mint"]
# implement serde on most types.
serde = ["dep:serde", "ahash/serde", "emath/serde"]

# load font from disk automatically according to user input
system_fonts = ["skia-safe"]

[dependencies]
emath = { version = "0.18.0", path = "../emath" }

Expand All @@ -65,6 +68,8 @@ cint = { version = "0.3.1", optional = true }
color-hex = { version = "0.2.0", optional = true }
serde = { version = "1", optional = true, features = ["derive", "rc"] }

skia-safe = {version = "0.49.1", optional = true, features = ["gl"]}

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
backtrace = { version = "0.3", optional = true }
Expand Down
12 changes: 12 additions & 0 deletions epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ impl Font {
self.glyph_info(c).1.advance_width
}

pub fn has_glyph_info_and_cache(&mut self, c: char) -> bool {
if let Some(_font_index_glyph_info) = self.glyph_info_cache.get(&c) {
return true;
}

self.glyph_info_no_cache_or_fallback(c).is_some()
}

pub fn push_font_impl(&mut self, new_font_impl: Arc<FontImpl>) {
self.fonts.push(new_font_impl);
}

/// `\n` will (intentionally) show up as the replacement character.
fn glyph_info(&mut self, c: char) -> (FontIndex, GlyphInfo) {
if let Some(font_index_glyph_info) = self.glyph_info_cache.get(&c) {
Expand Down
68 changes: 65 additions & 3 deletions epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::{
mutex::{Mutex, MutexGuard},
text::{
Expand All @@ -10,6 +7,8 @@ use crate::{
TextureAtlas,
};
use emath::NumExt as _;
use std::collections::BTreeMap;
use std::sync::Arc;

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -243,6 +242,25 @@ pub struct FontDefinitions {
pub families: BTreeMap<FontFamily, Vec<String>>,
}

impl FontDefinitions {
/// query a font which contains the character
#[cfg(feature = "system_fonts")]
pub fn query_font_for_character(c: char) -> Option<(Vec<u8>, String)> {
use skia_safe::{FontMgr, FontStyle};

let font_mgr = FontMgr::new();

if let Some(typeface) =
font_mgr.match_family_style_character("", FontStyle::normal(), &[], c as i32)
{
if let Some((buf, _index)) = typeface.to_font_data() {
return Some((buf, typeface.family_name()));
}
}
None
}
}

impl Default for FontDefinitions {
fn default() -> Self {
#[allow(unused)]
Expand Down Expand Up @@ -598,6 +616,50 @@ impl FontsImpl {
fn row_height(&mut self, font_id: &FontId) -> f32 {
self.font(font_id).row_height()
}

#[cfg(feature = "system_fonts")]
pub fn ensure_correct_fonts_for_text(&mut self, text: &str, main_font_id: &FontId) {
let FontId { size, family: _ } = main_font_id;
let scale_in_pixels = self.font_impl_cache.scale_as_pixels(*size);

let mut font_impl_manager = self.font(main_font_id);
for c in text.chars() {
if font_impl_manager.has_glyph_info_and_cache(c) {
continue;
}
if let Some((buf, new_font_name)) = FontDefinitions::query_font_for_character(c) {
// update FontData
let font_data = self
.definitions
.font_data
.entry(new_font_name.clone())
.or_insert_with(|| FontData::from_owned(buf));

self.definitions
.families
.entry(FontFamily::Monospace)
.or_default()
.push(new_font_name.clone());
self.definitions
.families
.entry(FontFamily::Proportional)
.or_default()
.push(new_font_name.clone());
// update fonts_impl_cache
let ab_glyph = ab_glyph_font_from_font_data(&new_font_name, font_data);
let tweak = font_data.tweak;
self.font_impl_cache
.ab_glyph_fonts
.insert(new_font_name.clone(), (tweak, ab_glyph));
// update fonts_impl_cache
let new_font_impl = self
.font_impl_cache
.font_impl(scale_in_pixels, &new_font_name);
font_impl_manager = self.font(main_font_id);
font_impl_manager.push_font_impl(new_font_impl);
}
}
}
}

// ----------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ fn layout_section(
byte_range,
format,
} = section;

// load font from system while we can't recognize with fonts in memory while layout
#[cfg(feature = "system_fonts")]
fonts.ensure_correct_fonts_for_text(&job.text, &format.font_id);

let font = fonts.font(&format.font_id);
let font_height = font.row_height();

Expand Down