From f2de65f636cdc7bbf662b4a5482f1385ce2a0bb9 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 6 Sep 2026 08:27:50 +0200 Subject: [PATCH] =?UTF-8?q?Render=20any=20glyph=20and=20colored=20emoji=20?= =?UTF-8?q?on=20native=20=F0=9F=9A=80=20(#8512)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add full emoji support to eframe on native! Also supports any glyph that your system fonts support. Test string: ✨ We support ไทย, 한국어, and 🦀 🚀 🤖/🦾/📷/🎞️ Chain, in order, all merged into `emilk/emojis`: * https://github.com/emilk/egui/pull/8491 * https://github.com/emilk/egui/pull/8492 * https://github.com/emilk/egui/pull/8493 * https://github.com/emilk/egui/pull/8508 * https://github.com/emilk/egui/pull/8509 * https://github.com/emilk/egui/pull/8510 * https://github.com/emilk/egui/pull/8511 --------- Co-authored-by: Claude Fable 5.1 --- .github/workflows/labels.yml | 2 +- Cargo.lock | 106 ++++- Cargo.toml | 8 + crates/eframe/Cargo.toml | 7 + crates/eframe/src/epi.rs | 11 + crates/eframe/src/native/epi_integration.rs | 5 + crates/eframe/src/web/canvas_glyphs.rs | 16 +- crates/egui/Cargo.toml | 8 + crates/egui/src/context.rs | 62 +-- crates/egui/src/input_state/mod.rs | 4 +- crates/egui/src/lib.rs | 52 +-- crates/egui/src/memory/mod.rs | 6 +- crates/egui/src/memory/theme.rs | 4 +- crates/egui/src/style.rs | 2 +- crates/egui/src/widgets/image.rs | 2 +- crates/egui_demo_app/Cargo.toml | 2 + crates/egui_demo_app/src/apps/image_viewer.rs | 2 +- crates/egui_demo_app/src/backend_panel.rs | 6 +- crates/egui_demo_app/src/wrap_app.rs | 6 +- .../egui_demo_app/tests/snapshots/clock.png | 4 +- .../tests/snapshots/custom3d.png | 4 +- .../tests/snapshots/easymarkeditor.png | 4 +- .../tests/snapshots/imageviewer.png | 4 +- crates/egui_demo_app/tests/test_demo_app.rs | 2 +- crates/egui_demo_lib/Cargo.toml | 3 +- crates/egui_demo_lib/src/demo/about.rs | 5 +- crates/egui_demo_lib/src/demo/code_editor.rs | 2 +- crates/egui_demo_lib/src/demo/code_example.rs | 2 +- .../src/demo/demo_app_windows.rs | 3 +- .../egui_demo_lib/src/demo/extra_viewport.rs | 2 +- crates/egui_demo_lib/src/demo/font_book.rs | 126 ++++-- .../src/demo/misc_demo_window.rs | 2 +- crates/egui_demo_lib/src/demo/modals.rs | 2 +- crates/egui_demo_lib/src/demo/paint_bezier.rs | 2 +- crates/egui_demo_lib/src/demo/painting.rs | 2 +- crates/egui_demo_lib/src/demo/panels.rs | 10 +- crates/egui_demo_lib/src/demo/password.rs | 2 +- crates/egui_demo_lib/src/demo/scrolling.rs | 6 +- crates/egui_demo_lib/src/demo/sliders.rs | 2 +- crates/egui_demo_lib/src/demo/table_demo.rs | 2 +- .../src/demo/tests/window_resize_test.rs | 12 +- crates/egui_demo_lib/src/demo/text_edit.rs | 2 +- crates/egui_demo_lib/src/demo/text_layout.rs | 2 +- crates/egui_demo_lib/src/demo/tooltips.rs | 2 +- .../egui_demo_lib/src/demo/widget_gallery.rs | 2 +- .../egui_demo_lib/src/demo/window_options.rs | 4 +- .../tests/snapshots/demos/Bézier Curve.png | 4 +- .../tests/snapshots/demos/Code Editor.png | 4 +- .../tests/snapshots/demos/Code Example.png | 4 +- .../tests/snapshots/demos/Extra Viewport.png | 4 +- .../tests/snapshots/demos/Font Book.png | 4 +- .../tests/snapshots/demos/Misc Demos.png | 4 +- .../tests/snapshots/demos/Modals.png | 4 +- .../tests/snapshots/demos/Sliders.png | 4 +- .../tests/snapshots/demos/Text Layout.png | 4 +- .../tests/snapshots/demos/TextEdit.png | 4 +- .../tests/snapshots/demos/Window Options.png | 4 +- .../tests/snapshots/modals_1.png | 4 +- .../tests/snapshots/modals_2.png | 4 +- .../tests/snapshots/modals_3.png | 4 +- ...rop_should_prevent_focusing_lower_area.png | 4 +- crates/egui_extras/src/syntax_highlighting.rs | 4 +- crates/egui_kittest/Cargo.toml | 3 +- crates/egui_system_fonts/CHANGELOG.md | 9 + crates/egui_system_fonts/Cargo.toml | 37 ++ crates/egui_system_fonts/README.md | 16 + crates/egui_system_fonts/src/lib.rs | 261 +++++++++++ crates/epaint/Cargo.toml | 13 + crates/epaint/src/lib.rs | 6 + crates/epaint/src/text/face_store.rs | 41 +- crates/epaint/src/text/family.rs | 193 +++++---- crates/epaint/src/text/font_data.rs | 86 ++-- crates/epaint/src/text/font_definitions.rs | 125 ++++-- crates/epaint/src/text/font_face.rs | 229 +++++++++- crates/epaint/src/text/font_provider.rs | 362 ++++++++++++++++ crates/epaint/src/text/fonts.rs | 149 ++++--- crates/epaint/src/text/glyph_atlas.rs | 90 ++-- crates/epaint/src/text/glyph_rasterizer.rs | 69 +-- crates/epaint/src/text/mod.rs | 8 +- crates/epaint/src/text/text_layout.rs | 410 ++++++------------ crates/epaint/src/text/text_layout_types.rs | 4 +- crates/epaint/src/text/unicode.rs | 15 + crates/epaint_default_fonts/Cargo.toml | 9 + .../epaint_default_fonts/fonts/egui-icons.ttf | Bin 0 -> 3484 bytes .../epaint_default_fonts/fonts/egui-icons.txt | 23 + crates/epaint_default_fonts/src/lib.rs | 18 + .../src/special_emojis.rs | 47 ++ deny.toml | 2 + scripts/generate_changelog.py | 1 + scripts/publish_crates.sh | 1 + tests/egui_tests/Cargo.toml | 6 +- 91 files changed, 1980 insertions(+), 843 deletions(-) create mode 100644 crates/egui_system_fonts/CHANGELOG.md create mode 100644 crates/egui_system_fonts/Cargo.toml create mode 100644 crates/egui_system_fonts/README.md create mode 100644 crates/egui_system_fonts/src/lib.rs create mode 100644 crates/epaint/src/text/font_provider.rs create mode 100644 crates/epaint_default_fonts/fonts/egui-icons.ttf create mode 100644 crates/epaint_default_fonts/fonts/egui-icons.txt create mode 100644 crates/epaint_default_fonts/src/special_emojis.rs diff --git a/.github/workflows/labels.yml b/.github/workflows/labels.yml index c6300731..14d2ad2c 100644 --- a/.github/workflows/labels.yml +++ b/.github/workflows/labels.yml @@ -30,4 +30,4 @@ jobs: with: mode: minimum count: 1 - labels: "CI, dependencies, docs and examples, ecolor, eframe, egui_extras, egui_glow, egui_inspection, egui_kittest, egui-wgpu, egui-winit, egui, emath, epaint, epaint_default_fonts, exclude from changelog, typo" + labels: "CI, dependencies, docs and examples, ecolor, eframe, egui_extras, egui_glow, egui_inspection, egui_kittest, egui_system_fonts, egui-wgpu, egui-winit, egui, emath, epaint, epaint_default_fonts, exclude from changelog, typo" diff --git a/Cargo.lock b/Cargo.lock index aa73170c..e6a35449 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1256,6 +1256,7 @@ dependencies = [ "egui-winit", "egui_glow", "egui_inspection", + "egui_system_fonts", "glow", "glutin", "glutin-winit", @@ -1461,6 +1462,20 @@ dependencies = [ "wgpu", ] +[[package]] +name = "egui_system_fonts" +version = "0.36.1" +dependencies = [ + "egui", + "epaint", + "fontique", + "log", + "poll-promise", + "skrifa", + "sys-locale", + "unicode-script", +] + [[package]] name = "egui_tests" version = "0.36.1" @@ -1804,7 +1819,7 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" dependencies = [ - "roxmltree", + "roxmltree 0.20.0", ] [[package]] @@ -1820,6 +1835,28 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "fontique" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6688bc1294fe7117d788937b6c53480169b29c566954af490830d4c09da9516a" +dependencies = [ + "hashbrown 0.17.1", + "linebender_resource_handle", + "memmap2", + "objc2 0.6.4", + "objc2-core-foundation", + "objc2-core-text", + "objc2-foundation 0.3.2", + "parlance", + "read-fonts", + "roxmltree 0.21.1", + "smallvec", + "windows", + "windows-core", + "yeslogic-fontconfig-sys", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -2003,6 +2040,7 @@ dependencies = [ "hashbrown 0.17.1", "log", "peniko", + "png 0.18.1", "skrifa", "smallvec", "vello_common", @@ -2334,7 +2372,7 @@ dependencies = [ "gif", "image-webp", "num-traits", - "png", + "png 0.17.16", "tiff", "zune-core", "zune-jpeg", @@ -3059,6 +3097,16 @@ dependencies = [ "objc2-foundation 0.2.2", ] +[[package]] +name = "objc2-core-text" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" +dependencies = [ + "bitflags 2.13.1", + "objc2-core-foundation", +] + [[package]] name = "objc2-encode" version = "4.1.0" @@ -3351,6 +3399,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "parlance" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b6937eda350acc1a5d05872c3cbf99fe78619c269096e2be3d4a350058639d5" + [[package]] name = "peniko" version = "0.6.1" @@ -3548,6 +3602,19 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags 2.13.1", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide", +] + [[package]] name = "poll-promise" version = "0.3.0" @@ -4039,6 +4106,15 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" +[[package]] +name = "roxmltree" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1964b10c76125c36f8afe190065a4bf9a87bf324842c05701330bba9f1cacbb" +dependencies = [ + "memchr", +] + [[package]] name = "rustc-demangle" version = "0.1.27" @@ -4547,6 +4623,15 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "sys-locale" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4" +dependencies = [ + "libc", +] + [[package]] name = "tempfile" version = "3.27.0" @@ -4710,7 +4795,7 @@ dependencies = [ "bytemuck", "cfg-if", "log", - "png", + "png 0.17.16", "tiny-skia-path", ] @@ -5037,7 +5122,7 @@ dependencies = [ "kurbo 0.11.3", "log", "pico-args", - "roxmltree", + "roxmltree 0.20.0", "rustybuzz", "simplecss", "siphasher", @@ -5090,6 +5175,7 @@ dependencies = [ "guillotiere", "log", "peniko", + "png 0.18.1", "smallvec", "thiserror 2.0.18", ] @@ -5103,6 +5189,7 @@ dependencies = [ "bytemuck", "glifo", "hashbrown 0.17.1", + "png 0.18.1", "vello_common", ] @@ -5913,6 +6000,17 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "yeslogic-fontconfig-sys" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8b8abf912b9a29ff112e1671c97c33636903d13a69712037190e6805af4f76" +dependencies = [ + "dlib", + "once_cell", + "pkg-config", +] + [[package]] name = "yoke" version = "0.8.3" diff --git a/Cargo.toml b/Cargo.toml index 4c12814f..569d802c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "crates/egui_glow", "crates/egui_inspection", "crates/egui_kittest", + "crates/egui_system_fonts", "crates/egui-wgpu", "crates/egui-winit", "crates/egui", @@ -68,6 +69,7 @@ egui_demo_lib = { version = "0.36.1", path = "crates/egui_demo_lib", default-fea egui_glow = { version = "0.36.1", path = "crates/egui_glow", default-features = false } egui_inspection = { version = "0.36.1", path = "crates/egui_inspection", default-features = false } egui_kittest = { version = "0.36.1", path = "crates/egui_kittest", default-features = false } +egui_system_fonts = { version = "0.36.1", path = "crates/egui_system_fonts", default-features = false } eframe = { version = "0.36.1", path = "crates/eframe", default-features = false } accesskit = "0.24.1" @@ -92,6 +94,10 @@ ehttp = { version = "0.7.1", default-features = false } enum-map = "2.7" # Can't update to 3.1: its `enum-map-derive` moved to syn 3, duplicating the syn 2 that most other proc-macro crates still use env_logger = { version = "0.11.8", default-features = false } # 0.11.9+ pulls env_filter 1.x/2.x, duplicating the 0.1.x that android_logger needs font-types = { version = "0.12.2", default-features = false, features = ["std"] } +fontique = { version = "0.11.1", default-features = false, features = [ + "system", + "fontconfig-dlopen", # Load libfontconfig at runtime, so builds don't need its headers +] } glow = "0.17.0" # Can't update to 0.18: wgpu 30's wgpu-hal pins glow 0.17, so bumping splits it into two versions glutin = { version = "0.32.3", default-features = false } glutin-winit = { version = "0.5.0", default-features = false } @@ -138,6 +144,7 @@ smallvec = "1.15" smithay-clipboard = "0.7.2" # 0.7.3 pulls smithay-client-toolkit 0.20 (calloop 0.14), duplicating the 0.19/0.13 that winit 0.30 needs static_assertions = "1.1" syntect = { version = "5.3", default-features = false } +sys-locale = "0.3.2" tempfile = "3.27" thiserror = "2.0" # Held at 2.0.18 in `Cargo.lock`: `thiserror-impl` 2.0.19 moved to syn 3, duplicating the syn 2 that most other proc-macro crates still use tokio = "1.53" @@ -146,6 +153,7 @@ type-map = "0.5.1" unicode_names2 = { version = "3.1", default-features = false } unicode-general-category = "1.1" unicode-properties = { version = "0.1.4", default-features = false, features = ["emoji"] } +unicode-script = "0.5.8" unicode-segmentation = "1.13" vello_cpu = { version = "0.2.0", default-features = false, features = [ "std", diff --git a/crates/eframe/Cargo.toml b/crates/eframe/Cargo.toml index 8fc50154..2b03bb17 100644 --- a/crates/eframe/Cargo.toml +++ b/crates/eframe/Cargo.toml @@ -29,6 +29,7 @@ default = [ "accesskit", "default_fonts", "links", + "system_fonts", "wayland", # Required for Linux support (including CI!) "web_screen_reader", "wgpu", @@ -80,6 +81,11 @@ wayland = [ "glutin-winit?/wayland", ] +## Load system fonts on demand for characters that the installed fonts lack (native only). +## +## Turn it off at runtime with [`NativeOptions::system_font_fallback`]. +system_fonts = ["dep:egui_system_fonts"] + ## Enable screen reader support (requires `ctx.options_mut(|o| o.screen_reader = true);`) on web. ## ## For other platforms, use the `accesskit` feature instead. @@ -160,6 +166,7 @@ image = { workspace = true, features = ["png"] } # Needed for app icon winit = { workspace = true, default-features = false, features = ["rwh_06"] } # optional native: +egui_system_fonts = { workspace = true, optional = true } egui-wgpu = { workspace = true, optional = true, features = [ "winit", "capture", diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index a971097a..74eedd02 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -378,6 +378,15 @@ pub struct NativeOptions { /// persisted (only if the "persistence" feature is enabled). pub persist_window: bool, + /// Load system fonts on demand for characters that the installed fonts lack, + /// e.g. CJK, Arabic, or Devanagari (only if the `system_fonts` feature is enabled). + /// + /// Turn this off if you bundle fonts that cover everything your app shows, + /// or if you need identical text rendering on all machines. + /// + /// Default: `true`. + pub system_font_fallback: bool, + /// The folder where `eframe` will store the app state. If not set, eframe will use a default /// data storage path for each target system. pub persistence_path: Option, @@ -462,6 +471,8 @@ impl Default for NativeOptions { persist_window: true, + system_font_fallback: true, + persistence_path: None, dithering: true, diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index b8122563..a170d384 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -217,6 +217,11 @@ impl EpiIntegration { Some(icon), ); + #[cfg(feature = "system_fonts")] + if native_options.system_font_fallback { + egui_ctx.add_font_provider(Arc::new(egui_system_fonts::SystemFontProvider::new())); + } + Self { frame, last_auto_save: Instant::now(), diff --git a/crates/eframe/src/web/canvas_glyphs.rs b/crates/eframe/src/web/canvas_glyphs.rs index e2ae6ea2..e78608c7 100644 --- a/crates/eframe/src/web/canvas_glyphs.rs +++ b/crates/eframe/src/web/canvas_glyphs.rs @@ -8,8 +8,8 @@ use core::cell::RefCell; use std::collections::HashMap; use egui::{ - ColorImage, GlyphRasterizer, GlyphRasterizerRequest, MAX_GLYPH_SIZE, RasterizedGlyph, - has_emoji_presentation, vec2, + ColorImage, GlyphBitmap, GlyphRasterizer, GlyphRasterizerRequest, MAX_GLYPH_SIZE, + RasterizedGlyph, has_emoji_presentation, vec2, }; use wasm_bindgen::JsCast as _; use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement}; @@ -108,12 +108,14 @@ impl CanvasGlyphs { } = white; Some(RasterizedGlyph { - image: ColorImage::from_rgba_unmultiplied([width as _, height as _], &rgba), - // The pen sits at `(PADDING + left, PADDING + ascent)` in the image, - // so the image's top-left is this far from the pen: - offset_px: vec2(-(left + PADDING) as f32, -(ascent + PADDING) as f32), + bitmap: GlyphBitmap { + image: ColorImage::from_rgba_unmultiplied([width as _, height as _], &rgba), + // The pen sits at `(PADDING + left, PADDING + ascent)` in the image, + // so the image's top-left is this far from the pen: + offset_px: vec2(-(left + PADDING) as f32, -(ascent + PADDING) as f32), + is_color, + }, advance_px: advance as f32, - is_color, }) } diff --git a/crates/egui/Cargo.toml b/crates/egui/Cargo.toml index 0bbd8494..f7d40de3 100644 --- a/crates/egui/Cargo.toml +++ b/crates/egui/Cargo.toml @@ -54,6 +54,14 @@ experimental = [] ## [`mint`](https://docs.rs/mint) enables interoperability with other math libraries such as [`glam`](https://docs.rs/glam) and [`nalgebra`](https://docs.rs/nalgebra). mint = ["epaint/mint"] +## Bundle the monochrome emoji fonts `NotoEmoji-Regular` and `emoji-icon-font`, +## and put them in the default [`FontDefinitions`]. +## +## They add about 1 MB to the binary, and are only needed if you want the same +## (monochrome) emoji on every platform, instead of the platform's own color emoji +## (see [`GlyphRasterizer`] and [`FontProvider`]). +monochrome_emoji_fonts = ["default_fonts", "epaint/monochrome_emoji_fonts"] + ## Enable persistence of memory (window positions etc). persistence = ["serde", "epaint/serde", "ron"] diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 1bb8f422..60d9c977 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -16,13 +16,12 @@ use epaint::{ }; use crate::{ - Align2, CursorIcon, DeferredViewportUiCallback, FontDefinitions, GlyphRasterizer, GlyphSource, - GlyphSourcePreference, Grid, Id, ImmediateViewport, ImmediateViewportRendererCallback, Key, - KeyboardShortcut, Label, LayerId, Memory, ModifierNames, Modifiers, NumExt as _, Order, - Painter, RawInput, Response, RichText, SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, - TextureHandle, TextureOptions, Ui, UiBuilder, ViewportBuilder, ViewportCommand, ViewportId, - ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportOutput, Visuals, Widget as _, WidgetRect, - WidgetText, + Align2, CursorIcon, DeferredViewportUiCallback, FontDefinitions, FontProvider, GlyphRasterizer, + Grid, Id, ImmediateViewport, ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, + LayerId, Memory, ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, + RichText, SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, + Ui, UiBuilder, ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair, + ViewportIdSet, ViewportOutput, Visuals, Widget as _, WidgetRect, WidgetText, animation_manager::AnimationManager, containers::{self, area::AreaState}, data::output::PlatformOutput, @@ -377,7 +376,7 @@ struct ContextImpl { fonts: Option, font_definitions: FontDefinitions, glyph_rasterizer: Option, - glyph_source_preference: Option, + font_providers: Vec>, memory: Memory, animation_manager: AnimationManager, @@ -594,14 +593,11 @@ impl ContextImpl { is_new = true; profiling::scope!("Fonts::new"); - let mut fonts = Fonts::new(text_options, self.font_definitions.clone()); + let mut fonts = Fonts::new(text_options, self.font_definitions.clone()) + .with_font_providers(self.font_providers.clone()); if let Some(glyph_rasterizer) = &self.glyph_rasterizer { fonts = fonts.with_glyph_rasterizer(glyph_rasterizer.clone()); } - if let Some(prefer) = &self.glyph_source_preference { - let prefer = Arc::clone(prefer); - fonts = fonts.with_glyph_source_preference(move |cluster| prefer(cluster)); - } fonts }); @@ -774,8 +770,8 @@ impl Default for Context { impl Context { /// Set the platform glyph rasterizer, e.g. the browser on web. /// - /// It is used for grapheme clusters no installed font can render, - /// and for clusters where the [`GlyphSourcePreference`] says so (color emoji by default). + /// It is used for grapheme clusters that no installed font can render, + /// and that no [`FontProvider`] has a font for. /// /// `eframe` sets this on web. Pass `None` to only use the installed fonts. pub fn set_glyph_rasterizer(&self, glyph_rasterizer: Option) { @@ -785,19 +781,25 @@ impl Context { }); } - /// Decide where to look first for the glyphs of each grapheme cluster: - /// the fonts in the [`FontDefinitions`], or what the platform offers (e.g. the browser on web). + /// Add a [`FontProvider`], asked for fonts for characters that no installed font has. /// - /// By default, color emoji come from the platform, and everything else from the fonts. - /// Use `|_| GlyphSource::Fonts` to only use the platform for characters no installed font has. + /// Fonts it finds are appended to the fallback chain of the requested font family. + /// Providers are asked in the order they were added. /// - /// See [`GlyphSourcePreference`]. - pub fn set_glyph_source_preference( - &self, - prefer: impl Fn(&str) -> GlyphSource + Send + Sync + 'static, - ) { + /// `eframe` adds a system font provider on native (see its `system_fonts` feature). + pub fn add_font_provider(&self, font_provider: Arc) { self.write(|ctx| { - ctx.glyph_source_preference = Some(Arc::new(prefer)); + ctx.font_providers.push(font_provider); + ctx.fonts = None; + }); + } + + /// Replace all [`FontProvider`]s. See [`Self::add_font_provider`]. + /// + /// Pass an empty list to only use the installed fonts. + pub fn set_font_providers(&self, font_providers: Vec>) { + self.write(|ctx| { + ctx.font_providers = font_providers; ctx.fonts = None; }); } @@ -2196,6 +2198,8 @@ impl Context { /// /// The new fonts will become active at the start of the next pass. /// This will overwrite the existing fonts. + /// + /// These fonts will be used before any system fallback. pub fn set_fonts(&self, font_definitions: FontDefinitions) { profiling::function_scope!(); @@ -2212,13 +2216,15 @@ impl Context { } } - /// Tell `egui` which fonts to use. + /// Add an additional font to `egui`. /// /// 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. + /// + /// This font will be used before any system fallback. pub fn add_font(&self, new_font: FontInsert) { profiling::function_scope!(); @@ -3561,13 +3567,13 @@ impl Context { paint_stats.ui(ui); }); - CollapsingHeader::new("🖼 Textures") + CollapsingHeader::new("🖼️ Textures") .default_open(false) .show(ui, |ui| { self.texture_ui(ui); }); - CollapsingHeader::new("🖼 Image loaders") + CollapsingHeader::new("🖼️ Image loaders") .default_open(false) .show(ui, |ui| { self.loaders_ui(ui); diff --git a/crates/egui/src/input_state/mod.rs b/crates/egui/src/input_state/mod.rs index 854c1fea..e8f33a44 100644 --- a/crates/egui/src/input_state/mod.rs +++ b/crates/egui/src/input_state/mod.rs @@ -1603,7 +1603,7 @@ impl InputState { ui.collapsing("Raw Input", |ui| raw.ui(ui)); - crate::containers::CollapsingHeader::new("🖱 Pointer") + crate::containers::CollapsingHeader::new("🖱️ Pointer") .default_open(false) .show(ui, |ui| { pointer.ui(ui); @@ -1615,7 +1615,7 @@ impl InputState { }); } - crate::containers::CollapsingHeader::new("⬍ Scroll") + crate::containers::CollapsingHeader::new("↕️ Scroll") .default_open(false) .show(ui, |ui| { wheel.ui(ui); diff --git a/crates/egui/src/lib.rs b/crates/egui/src/lib.rs index d1c37248..85ad77c7 100644 --- a/crates/egui/src/lib.rs +++ b/crates/egui/src/lib.rs @@ -453,9 +453,9 @@ pub use epaint::{ ClippedPrimitive, ColorImage, CornerRadius, Direction, ImageData, Margin, Mesh, PaintCallback, PaintCallbackInfo, Shadow, Shape, Stroke, StrokeKind, TextureHandle, TextureId, mutex, text::{ - FontData, FontDefinitions, FontFamily, FontId, FontTweak, GlyphRasterizer, - GlyphRasterizerRequest, GlyphSource, GlyphSourcePreference, MAX_GLYPH_SIZE, - RasterizedGlyph, default_glyph_source, has_emoji_presentation, + FallbackRequest, FontData, FontDefinitions, FontFamily, FontId, FontInsert, FontPriority, + FontProvider, FontTweak, GlyphBitmap, GlyphRasterizer, GlyphRasterizerRequest, + InsertFontFamily, MAX_GLYPH_SIZE, RasterizedGlyph, has_emoji_presentation, }, textures::{TextureFilter, TextureOptions, TextureWrapMode, TexturesDelta}, }; @@ -511,7 +511,7 @@ pub use self::{ pub fn warn_if_debug_build(ui: &mut crate::Ui) { if cfg!(debug_assertions) { ui.label( - RichText::new("⚠ Debug build ⚠") + RichText::new("⚠️ Debug build ⚠️") .small() .color(ui.visuals().warn_fg_color), ) @@ -585,46 +585,10 @@ macro_rules! github_link_file { /// The minus character: pub(crate) const MINUS_CHAR_STR: &str = "−"; -/// The default egui fonts supports around 1216 emojis in total. -/// Here are some of the most useful: -/// ∞⊗⎗⎘⎙⏏⏴⏵⏶⏷ -/// ⏩⏪⏭⏮⏸⏹⏺■▶📾🔀🔁🔃 -/// ☀☁★☆☐☑☜☝☞☟⛃⛶✔ -/// ↺↻⟲⟳⬅➡⬆⬇⬈⬉⬊⬋⬌⬍⮨⮩⮪⮫ -/// ♡ -/// 📅📆 -/// 📈📉📊 -/// 📋📌📎📤📥🔆 -/// 🔈🔉🔊🔍🔎🔗🔘 -/// 🕓🖧🖩🖮🖱🖴🖵🖼🗀🗁🗋🗐🗑🗙🚫❓ -/// -/// NOTE: In egui all emojis are monochrome! -/// -/// You can explore them all in the Font Book in [the online demo](https://www.egui.rs/#demo). -/// -/// In addition, egui supports a few special emojis that are not part of the unicode standard. -/// This module contains some of them: -pub mod special_emojis { - /// Tux, the Linux penguin. - pub const OS_LINUX: char = '🐧'; - - /// The Windows logo. - pub const OS_WINDOWS: char = ''; - - /// The Android logo. - pub const OS_ANDROID: char = ''; - - /// The Apple logo. - pub const OS_APPLE: char = ''; - - /// The Github logo. - pub const GITHUB: char = ''; - - /// The word `git`. - pub const GIT: char = ''; - - // I really would like to have ferris here. -} +/// A few special emojis that are not part of the unicode standard, +/// plus a list of the emojis in the default fonts. +#[cfg(feature = "default_fonts")] +pub use epaint::special_emojis; /// The different types of built-in widgets in egui #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/egui/src/memory/mod.rs b/crates/egui/src/memory/mod.rs index 7618eacc..c27a08d9 100644 --- a/crates/egui/src/memory/mod.rs +++ b/crates/egui/src/memory/mod.rs @@ -410,7 +410,7 @@ impl Options { use crate::Widget as _; use crate::containers::CollapsingHeader; - CollapsingHeader::new("⚙ Options") + CollapsingHeader::new("⚙️ Options") .default_open(false) .show(ui, |ui| { ui.horizontal(|ui| { @@ -452,7 +452,7 @@ impl Options { style.ui(ui); }); - CollapsingHeader::new("✒ Painting") + CollapsingHeader::new("✒️ Painting") .default_open(false) .show(ui, |ui| { tessellation_options.ui(ui); @@ -461,7 +461,7 @@ impl Options { }); }); - CollapsingHeader::new("🖱 Input") + CollapsingHeader::new("🖱️ Input") .default_open(false) .show(ui, |ui| { input_options.ui(ui); diff --git a/crates/egui/src/memory/theme.rs b/crates/egui/src/memory/theme.rs index 4641966e..d1c5e1a9 100644 --- a/crates/egui/src/memory/theme.rs +++ b/crates/egui/src/memory/theme.rs @@ -42,7 +42,7 @@ impl Theme { #![expect(clippy::collapsible_else_if)] if self == Self::Dark { if ui - .add(Button::new("☀").frame(false)) + .add(Button::new("☀️").frame(false)) .on_hover_text("Switch to light mode") .clicked() { @@ -90,7 +90,7 @@ impl ThemePreference { pub fn icon(self) -> &'static str { match self { Self::Dark => "🌙", - Self::Light => "☀", + Self::Light => "☀️", Self::System => "💻", } } diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index ae467656..0a1dc1c8 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -1901,7 +1901,7 @@ impl Style { ui.collapsing("🔠 Text styles", |ui| text_styles_ui(ui, text_styles)); ui.collapsing("📏 Spacing", |ui| spacing.ui(ui)); - ui.collapsing("☝ Interaction", |ui| interaction.ui(ui)); + ui.collapsing("☝️ Interaction", |ui| interaction.ui(ui)); ui.collapsing("🎨 Visuals", |ui| visuals.ui(ui)); ui.collapsing("🔄 Scroll animation", |ui| scroll_animation.ui(ui)); diff --git a/crates/egui/src/widgets/image.rs b/crates/egui/src/widgets/image.rs index 9bce8c4d..a9675070 100644 --- a/crates/egui/src/widgets/image.rs +++ b/crates/egui/src/widgets/image.rs @@ -683,7 +683,7 @@ pub fn paint_texture_load_result( ..Default::default() }; job.append( - "⚠", + "⚠️", 0.0, TextFormat::simple(font_id.clone(), ui.visuals().error_fg_color), ); diff --git a/crates/egui_demo_app/Cargo.toml b/crates/egui_demo_app/Cargo.toml index d024b998..1827d0dc 100644 --- a/crates/egui_demo_app/Cargo.toml +++ b/crates/egui_demo_app/Cargo.toml @@ -86,4 +86,6 @@ wasm-bindgen-futures.workspace = true web-sys.workspace = true [dev-dependencies] +# `monochrome_emoji_fonts` so that the snapshots keep their emoji: +egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } egui_kittest = { workspace = true, features = ["eframe", "snapshot", "wgpu"] } diff --git a/crates/egui_demo_app/src/apps/image_viewer.rs b/crates/egui_demo_app/src/apps/image_viewer.rs index 1fdb69c2..65fcc4b2 100644 --- a/crates/egui_demo_app/src/apps/image_viewer.rs +++ b/crates/egui_demo_app/src/apps/image_viewer.rs @@ -55,7 +55,7 @@ impl crate::DemoApp for ImageViewer { let label = ui.label("URI:"); ui.text_edit_singleline(&mut self.uri_edit_text) .labelled_by(label.id); - if ui.small_button("✔").clicked() { + if ui.small_button("✔️").clicked() { ui.ctx().forget_image(&self.current_uri); self.uri_edit_text = self.uri_edit_text.trim().to_owned(); self.current_uri = self.uri_edit_text.clone(); diff --git a/crates/egui_demo_app/src/backend_panel.rs b/crates/egui_demo_app/src/backend_panel.rs index 0fb7eee0..eaa34adb 100644 --- a/crates/egui_demo_app/src/backend_panel.rs +++ b/crates/egui_demo_app/src/backend_panel.rs @@ -328,7 +328,7 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) { { let mut fullscreen = ui.input(|i| i.viewport().fullscreen.unwrap_or(false)); if ui - .checkbox(&mut fullscreen, "🗖 Fullscreen (F11)") + .checkbox(&mut fullscreen, "🔳 Fullscreen (F11)") .on_hover_text("Fullscreen the window") .changed() { @@ -349,12 +349,12 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) { ui.selectable_value( &mut size, Some(egui::vec2(1280.0, 720.0)), - "🖥 Desktop 720p", + "🖥️ Desktop 720p", ); ui.selectable_value( &mut size, Some(egui::vec2(1920.0, 1080.0)), - "🖥 Desktop 1080p", + "🖥️ Desktop 1080p", ); }); diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index e5448ff8..b27fa4a0 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -226,13 +226,13 @@ impl WrapApp { ), #[cfg(feature = "easymark")] ( - "🖹 EasyMark editor", + "📝 EasyMark editor", Anchor::EasyMarkEditor, &mut self.state.easy_mark_editor as &mut dyn DemoApp, ), #[cfg(feature = "http")] ( - "⬇ HTTP", + "⬇️ HTTP", Anchor::Http, &mut self.state.http as &mut dyn DemoApp, ), @@ -243,7 +243,7 @@ impl WrapApp { ), #[cfg(feature = "image_viewer")] ( - "🖼 Image Viewer", + "🖼️ Image Viewer", Anchor::ImageViewer, &mut self.state.image_viewer as &mut dyn DemoApp, ), diff --git a/crates/egui_demo_app/tests/snapshots/clock.png b/crates/egui_demo_app/tests/snapshots/clock.png index 8786c9d4..ef03bad4 100644 --- a/crates/egui_demo_app/tests/snapshots/clock.png +++ b/crates/egui_demo_app/tests/snapshots/clock.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7311e37b40ecf968be90d7f36ed5ee7316f2b46b1d308a6c2d922e179a1ac5ba -size 335131 +oid sha256:44dc55002e51c8596a05fdc3e82c5249739db92ed9acf41b015d7e538b085bb8 +size 335296 diff --git a/crates/egui_demo_app/tests/snapshots/custom3d.png b/crates/egui_demo_app/tests/snapshots/custom3d.png index ac2f499e..303ac718 100644 --- a/crates/egui_demo_app/tests/snapshots/custom3d.png +++ b/crates/egui_demo_app/tests/snapshots/custom3d.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a11b9020bfd5c6f786c35399c463c41bdd97b68465bf11527dc76879ae105efe -size 93101 +oid sha256:ec2b816a26cb6f193183db87ff9a38bc1e6330c2c9ac9ff958b076813c3b8124 +size 93379 diff --git a/crates/egui_demo_app/tests/snapshots/easymarkeditor.png b/crates/egui_demo_app/tests/snapshots/easymarkeditor.png index daf86564..48842fcd 100644 --- a/crates/egui_demo_app/tests/snapshots/easymarkeditor.png +++ b/crates/egui_demo_app/tests/snapshots/easymarkeditor.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e11107b7e87e542ec8c7a18be35000db35830dfb01f88e2d2d579fab514f47e4 -size 168933 +oid sha256:9635c77760d0dc61fa43d3312b78e8293ce5d265266256c4fbbf2a53811a29fd +size 169102 diff --git a/crates/egui_demo_app/tests/snapshots/imageviewer.png b/crates/egui_demo_app/tests/snapshots/imageviewer.png index 31556258..a4c4b4c7 100644 --- a/crates/egui_demo_app/tests/snapshots/imageviewer.png +++ b/crates/egui_demo_app/tests/snapshots/imageviewer.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:935f309ffb3244a58c8dfbf7120498c52361cf9befcbca85d668afe080d8537e -size 101599 +oid sha256:a0aa0dc62ae2ebefc2e920361c39fa51202ac5354346386baa1222c34ee96a39 +size 101872 diff --git a/crates/egui_demo_app/tests/test_demo_app.rs b/crates/egui_demo_app/tests/test_demo_app.rs index e083c845..ddb7bb15 100644 --- a/crates/egui_demo_app/tests/test_demo_app.rs +++ b/crates/egui_demo_app/tests/test_demo_app.rs @@ -61,7 +61,7 @@ fn test_demo_app() { .get_by_role_and_label(Role::TextInput, "URI:") .type_text("file://../eframe/data/icon.png"); - harness.get_by_role_and_label(Role::Button, "✔").click(); + harness.get_by_role_and_label(Role::Button, "✔️").click(); // Wait for the image to load harness.try_run_realtime().ok(); diff --git a/crates/egui_demo_lib/Cargo.toml b/crates/egui_demo_lib/Cargo.toml index b5d500b1..9a1f7935 100644 --- a/crates/egui_demo_lib/Cargo.toml +++ b/crates/egui_demo_lib/Cargo.toml @@ -54,7 +54,8 @@ serde = { workspace = true, optional = true } [dev-dependencies] criterion.workspace = true -egui = { workspace = true, features = ["default_fonts"] } +# `monochrome_emoji_fonts` so that the snapshots of the demos keep their emoji: +egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } egui_extras = { workspace = true, features = ["image", "svg"] } egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] } image = { workspace = true, features = ["png"] } diff --git a/crates/egui_demo_lib/src/demo/about.rs b/crates/egui_demo_lib/src/demo/about.rs index 227aff42..3811d1c5 100644 --- a/crates/egui_demo_lib/src/demo/about.rs +++ b/crates/egui_demo_lib/src/demo/about.rs @@ -38,9 +38,8 @@ impl crate::View for About { }); ui.label(format!( - "egui is an immediate mode GUI library written in Rust. egui runs natively on {}{}{}, and \ + "egui is an immediate mode GUI library written in Rust. egui runs natively on {OS_APPLE}{OS_LINUX}{OS_WINDOWS}, and \ on the web it is compiled to WebAssembly and rendered with WebGL or WebGPU.{}", - OS_APPLE, OS_LINUX, OS_WINDOWS, if cfg!(target_arch = "wasm32") { " Everything you see is rendered as textured triangles. There is no DOM, HTML, JS or CSS. Just Rust." } else {""} @@ -115,5 +114,5 @@ fn links(ui: &mut egui::Ui) { "@ernerfeldt.bsky.social", "https://bsky.app/profile/ernerfeldt.bsky.social", ); - ui.hyperlink_to("📓 egui documentation", "https://docs.rs/egui/"); + ui.hyperlink_to("egui documentation", "https://docs.rs/egui/"); } diff --git a/crates/egui_demo_lib/src/demo/code_editor.rs b/crates/egui_demo_lib/src/demo/code_editor.rs index c79cdfd4..5a37249c 100644 --- a/crates/egui_demo_lib/src/demo/code_editor.rs +++ b/crates/egui_demo_lib/src/demo/code_editor.rs @@ -23,7 +23,7 @@ fn main() {\n\ impl crate::Demo for CodeEditor { fn name(&self) -> &'static str { - "🖮 Code Editor" + "⌨️ Code Editor" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/code_example.rs b/crates/egui_demo_lib/src/demo/code_example.rs index 7dbcd885..14947680 100644 --- a/crates/egui_demo_lib/src/demo/code_example.rs +++ b/crates/egui_demo_lib/src/demo/code_example.rs @@ -102,7 +102,7 @@ impl CodeExample { impl crate::Demo for CodeExample { fn name(&self) -> &'static str { - "🖮 Code Example" + "⌨️ Code Example" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/demo_app_windows.rs b/crates/egui_demo_lib/src/demo/demo_app_windows.rs index fc15aac1..88d3aead 100644 --- a/crates/egui_demo_lib/src/demo/demo_app_windows.rs +++ b/crates/egui_demo_lib/src/demo/demo_app_windows.rs @@ -331,6 +331,7 @@ impl DemoWindows { if ui.button("Organize windows").clicked() { ui.memory_mut(|mem| mem.reset_areas()); } + ui.add_space(4.0); }); }); } @@ -445,7 +446,7 @@ mod tests { fn remove_leading_emoji(full_name: &str) -> &str { if let Some((start, name)) = full_name.split_once(' ') - && start.len() <= 4 + && start.len() <= 7 // An emoji, plus an optional variation selector && start.bytes().next().is_some_and(|byte| byte >= 128) { return name; diff --git a/crates/egui_demo_lib/src/demo/extra_viewport.rs b/crates/egui_demo_lib/src/demo/extra_viewport.rs index ab3eac67..863d167b 100644 --- a/crates/egui_demo_lib/src/demo/extra_viewport.rs +++ b/crates/egui_demo_lib/src/demo/extra_viewport.rs @@ -7,7 +7,7 @@ impl crate::Demo for ExtraViewport { } fn name(&self) -> &'static str { - "🗖 Extra Viewport" + "🖥️ Extra Viewport" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/font_book.rs b/crates/egui_demo_lib/src/demo/font_book.rs index 07939778..a22a3e4e 100644 --- a/crates/egui_demo_lib/src/demo/font_book.rs +++ b/crates/egui_demo_lib/src/demo/font_book.rs @@ -45,20 +45,14 @@ impl crate::View for FontBook { ui.add(crate::egui_github_link_file!()); }); - ui.label(format!( - "The selected font supports {} characters.", - self.available_glyphs - .get(&self.font_id.family) - .map(|map| map.len()) - .unwrap_or_default() - )); - ui.horizontal_wrapped(|ui| { ui.spacing_mut().item_spacing.x = 0.0; - ui.label("You can add more characters by installing additional fonts with "); + ui.label("On web, the browser draws missing glyphs instead. "); + ui.label("On native, eframe falls back to looking for a matching system font. "); + ui.label("Bundle fonts with "); ui.add(egui::Hyperlink::from_label_and_url( - egui::RichText::new("Context::set_fonts").text_style(egui::TextStyle::Monospace), - "https://docs.rs/egui/latest/egui/struct.Context.html#method.set_fonts", + egui::RichText::new("Context::add_font").text_style(egui::TextStyle::Monospace), + "https://docs.rs/egui/latest/egui/struct.Context.html#method.add_font", )); ui.label("."); }); @@ -67,6 +61,26 @@ impl crate::View for FontBook { egui::introspection::font_id_ui(ui, &mut self.font_id); + let font_id = self.font_id.clone(); + let available_glyphs = self + .available_glyphs + .entry(font_id.family.clone()) + .or_insert_with(|| available_characters(ui, &font_id.family)); + + let fonts_in_family = fonts_in_family(ui, &font_id.family); + + egui::CollapsingHeader::new(format!( + "The selected family has {} characters, from {} fonts", + available_glyphs.len(), + fonts_in_family.len() + )) + .show(ui, |ui| { + ui.label("In fallback order:"); + for name in fonts_in_family { + ui.label(egui::RichText::new(name).text_style(egui::TextStyle::Monospace)); + } + }); + ui.horizontal(|ui| { ui.label("Filter:"); ui.add(egui::TextEdit::singleline(&mut self.filter).desired_width(120.0)); @@ -77,43 +91,83 @@ impl crate::View for FontBook { }); let filter = &self.filter; - let available_glyphs = self - .available_glyphs - .entry(self.font_id.family.clone()) - .or_insert_with(|| available_characters(ui, &self.font_id.family)); + + let matching_glyphs: Vec<(char, &GlyphInfo)> = available_glyphs + .iter() + .filter(|(chr, glyph_info)| { + filter.is_empty() || glyph_info.name.contains(filter) || *filter == chr.to_string() + }) + .map(|(&chr, glyph_info)| (chr, glyph_info)) + .collect(); ui.separator(); - egui::ScrollArea::vertical().show(ui, |ui| { - ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing = egui::Vec2::splat(2.0); + // Each glyph gets a fixed-size cell so we can calculate what is visible, + // and only paint those glyphs. + let spacing = egui::Vec2::splat(2.0); + let cell_size = egui::Vec2::splat((1.5 * font_id.size).round()); - for (&chr, glyph_info) in available_glyphs.iter() { - if filter.is_empty() - || glyph_info.name.contains(filter) - || *filter == chr.to_string() - { - let button = egui::Button::new( - egui::RichText::new(chr.to_string()).font(self.font_id.clone()), - ) - .frame(false); + let num_columns = ((ui.available_width() + spacing.x) / (cell_size.x + spacing.x)).floor(); + let num_columns = (num_columns as usize).max(1); + let num_rows = matching_glyphs.len().div_ceil(num_columns); - let tooltip_ui = |ui: &mut egui::Ui| { - let font_id = self.font_id.clone(); + egui::ScrollArea::vertical().auto_shrink(false).show_rows( + ui, + cell_size.y + spacing.y, + num_rows, + |ui, rows| { + ui.spacing_mut().item_spacing = spacing; - char_info_ui(ui, chr, glyph_info, font_id); - }; + for row in rows { + ui.horizontal(|ui| { + let start = row * num_columns; + let end = (start + num_columns).min(matching_glyphs.len()); + for &(chr, glyph_info) in &matching_glyphs[start..end] { + let button = + egui::Button::new(egui::RichText::new(chr).font(font_id.clone())) + .frame(false); - if ui.add(button).on_hover_ui(tooltip_ui).clicked() { - ui.copy_text(chr.to_string()); + let tooltip_ui = |ui: &mut egui::Ui| { + char_info_ui(ui, chr, glyph_info, font_id.clone()); + }; + + if ui + .add_sized(cell_size, button) + .on_hover_ui(tooltip_ui) + .clicked() + { + ui.copy_text(chr.to_string()); + } } - } + }); } - }); - }); + }, + ); } } +fn fonts_in_family(ui: &egui::Ui, family: &egui::FontFamily) -> Vec { + ui.fonts(|fonts| { + let mut names = fonts + .definitions() + .families + .get(family) + .cloned() + .unwrap_or_default(); + + // Fonts found by a `FontProvider`, e.g. among the system fonts: + names.extend( + fonts + .discovered_fonts() + .iter() + .filter(|font| font.families.iter().any(|insert| &insert.family == family)) + .map(|font| font.name.clone()), + ); + + names + }) +} + fn char_info_ui(ui: &mut egui::Ui, chr: char, glyph_info: &GlyphInfo, font_id: egui::FontId) { let resp = ui.label(egui::RichText::new(chr.to_string()).font(font_id)); diff --git a/crates/egui_demo_lib/src/demo/misc_demo_window.rs b/crates/egui_demo_lib/src/demo/misc_demo_window.rs index 8b91fbc6..24b0ae25 100644 --- a/crates/egui_demo_lib/src/demo/misc_demo_window.rs +++ b/crates/egui_demo_lib/src/demo/misc_demo_window.rs @@ -237,7 +237,7 @@ fn label_ui(ui: &mut egui::Ui) { let _ = ui.small_button("this button"); ui.label("."); - ui.label("The default font supports all latin and cyrillic characters (ИÅđ…), common math symbols (∫√∞²⅓…), and many emojis (💓🌟🖩…).") + ui.label("The default font supports all latin and cyrillic characters (ИÅđ…), common math symbols (∫√∞²⅓…), and many emojis (💓🌟📐…).") .on_hover_text("There is currently no support for right-to-left languages."); ui.label("See the 🔤 Font Book for more!"); diff --git a/crates/egui_demo_lib/src/demo/modals.rs b/crates/egui_demo_lib/src/demo/modals.rs index ad249f59..bdd52b4a 100644 --- a/crates/egui_demo_lib/src/demo/modals.rs +++ b/crates/egui_demo_lib/src/demo/modals.rs @@ -29,7 +29,7 @@ impl Modals { impl crate::Demo for Modals { fn name(&self) -> &'static str { - "🗖 Modals" + "💭 Modals" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/paint_bezier.rs b/crates/egui_demo_lib/src/demo/paint_bezier.rs index 22ed86de..552d8274 100644 --- a/crates/egui_demo_lib/src/demo/paint_bezier.rs +++ b/crates/egui_demo_lib/src/demo/paint_bezier.rs @@ -162,7 +162,7 @@ impl PaintBezier { impl crate::Demo for PaintBezier { fn name(&self) -> &'static str { - ") Bézier Curve" + "〰️ Bézier Curve" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/painting.rs b/crates/egui_demo_lib/src/demo/painting.rs index 0915febd..351f4ae9 100644 --- a/crates/egui_demo_lib/src/demo/painting.rs +++ b/crates/egui_demo_lib/src/demo/painting.rs @@ -74,7 +74,7 @@ impl Painting { impl crate::Demo for Painting { fn name(&self) -> &'static str { - "🖊 Painting" + "🖊️ Painting" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/panels.rs b/crates/egui_demo_lib/src/demo/panels.rs index 00d19efd..d4b05254 100644 --- a/crates/egui_demo_lib/src/demo/panels.rs +++ b/crates/egui_demo_lib/src/demo/panels.rs @@ -20,7 +20,7 @@ impl Default for Panels { impl crate::Demo for Panels { fn name(&self) -> &'static str { - "🗖 Panels" + "📰 Panels" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { @@ -122,10 +122,10 @@ impl crate::View for Panels { ui.horizontal(|ui| { ui.label("Panel toggles:"); - ui.toggle_value(left, "⬅"); - ui.toggle_value(top, "⬆"); - ui.toggle_value(bottom, "⬇"); - ui.toggle_value(right, "➡"); + ui.toggle_value(left, "⬅️"); + ui.toggle_value(top, "⬆️"); + ui.toggle_value(bottom, "⬇️"); + ui.toggle_value(right, "➡️"); }); ui.separator(); diff --git a/crates/egui_demo_lib/src/demo/password.rs b/crates/egui_demo_lib/src/demo/password.rs index 04b3c6f3..9d23394d 100644 --- a/crates/egui_demo_lib/src/demo/password.rs +++ b/crates/egui_demo_lib/src/demo/password.rs @@ -27,7 +27,7 @@ pub fn password_ui(ui: &mut egui::Ui, password: &mut String) -> egui::Response { let result = ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { // Toggle the `show_plaintext` bool with a button: let response = ui - .selectable_label(show_plaintext, "👁") + .selectable_label(show_plaintext, "👁️") .on_hover_text("Show/hide password"); if response.clicked() { diff --git a/crates/egui_demo_lib/src/demo/scrolling.rs b/crates/egui_demo_lib/src/demo/scrolling.rs index d9eb7192..88cbc701 100644 --- a/crates/egui_demo_lib/src/demo/scrolling.rs +++ b/crates/egui_demo_lib/src/demo/scrolling.rs @@ -27,7 +27,7 @@ pub struct Scrolling { impl crate::Demo for Scrolling { fn name(&self) -> &'static str { - "↕ Scrolling" + "↕️ Scrolling" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { @@ -301,10 +301,10 @@ impl crate::View for ScrollTo { .speed(1.0) .suffix("px") .ui(ui); - if ui.button("⬇").clicked() { + if ui.button("⬇️").clicked() { scroll_delta = Some(self.delta * Vec2::UP); // scroll down (move contents up) } - if ui.button("⬆").clicked() { + if ui.button("⬆️").clicked() { scroll_delta = Some(self.delta * Vec2::DOWN); // scroll up (move contents down) } }); diff --git a/crates/egui_demo_lib/src/demo/sliders.rs b/crates/egui_demo_lib/src/demo/sliders.rs index d2371caf..f9d33a4d 100644 --- a/crates/egui_demo_lib/src/demo/sliders.rs +++ b/crates/egui_demo_lib/src/demo/sliders.rs @@ -40,7 +40,7 @@ impl Default for Sliders { impl crate::Demo for Sliders { fn name(&self) -> &'static str { - "⬌ Sliders" + "↔️ Sliders" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/table_demo.rs b/crates/egui_demo_lib/src/demo/table_demo.rs index 27a2c367..f5b43dbf 100644 --- a/crates/egui_demo_lib/src/demo/table_demo.rs +++ b/crates/egui_demo_lib/src/demo/table_demo.rs @@ -186,7 +186,7 @@ impl TableDemo { }, |ui| { self.reversed ^= - ui.button(if self.reversed { "⬆" } else { "⬇" }).clicked(); + ui.button(if self.reversed { "⬆️" } else { "⬇️" }).clicked(); }, ); }); diff --git a/crates/egui_demo_lib/src/demo/tests/window_resize_test.rs b/crates/egui_demo_lib/src/demo/tests/window_resize_test.rs index c44221ef..8c81323c 100644 --- a/crates/egui_demo_lib/src/demo/tests/window_resize_test.rs +++ b/crates/egui_demo_lib/src/demo/tests/window_resize_test.rs @@ -18,7 +18,7 @@ impl crate::Demo for WindowResizeTest { fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { use egui::{Resize, ScrollArea, TextEdit, Window}; - Window::new("↔ auto-sized") + Window::new("↔️ auto-sized") .open(open) .auto_sized() .constrain_to(ui.available_rect_before_wrap()) @@ -31,7 +31,7 @@ impl crate::Demo for WindowResizeTest { ui.heading("Resize the above area!"); }); - Window::new("↔ resizable + scroll") + Window::new("↔️ resizable + scroll") .open(open) .vscroll(true) .resizable(true) @@ -45,7 +45,7 @@ impl crate::Demo for WindowResizeTest { lorem_ipsum(ui, crate::LOREM_IPSUM_LONG); }); - Window::new("↔ resizable + embedded scroll") + Window::new("↔️ resizable + embedded scroll") .open(open) .vscroll(false) .resizable(true) @@ -63,7 +63,7 @@ impl crate::Demo for WindowResizeTest { // ui.heading("Some additional text here, that should also be visible"); // this works, but messes with the resizing a bit }); - Window::new("↔ resizable without scroll") + Window::new("↔️ resizable without scroll") .open(open) .vscroll(false) .resizable(true) @@ -75,7 +75,7 @@ impl crate::Demo for WindowResizeTest { lorem_ipsum(ui, crate::LOREM_IPSUM); }); - Window::new("↔ resizable with TextEdit") + Window::new("↔️ resizable with TextEdit") .open(open) .vscroll(false) .resizable(true) @@ -86,7 +86,7 @@ impl crate::Demo for WindowResizeTest { ui.add_sized(ui.available_size(), TextEdit::multiline(&mut self.text)); }); - Window::new("↔ freely resized") + Window::new("↔️ freely resized") .open(open) .vscroll(false) .resizable(true) diff --git a/crates/egui_demo_lib/src/demo/text_edit.rs b/crates/egui_demo_lib/src/demo/text_edit.rs index 143268fe..77590c60 100644 --- a/crates/egui_demo_lib/src/demo/text_edit.rs +++ b/crates/egui_demo_lib/src/demo/text_edit.rs @@ -22,7 +22,7 @@ impl Default for TextEditDemo { impl crate::Demo for TextEditDemo { fn name(&self) -> &'static str { - "🖹 TextEdit" + "✏️ TextEdit" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/text_layout.rs b/crates/egui_demo_lib/src/demo/text_layout.rs index 656d5a6d..03bde6db 100644 --- a/crates/egui_demo_lib/src/demo/text_layout.rs +++ b/crates/egui_demo_lib/src/demo/text_layout.rs @@ -29,7 +29,7 @@ impl Default for TextLayoutDemo { impl crate::Demo for TextLayoutDemo { fn name(&self) -> &'static str { - "🖹 Text Layout" + "📄 Text Layout" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/tooltips.rs b/crates/egui_demo_lib/src/demo/tooltips.rs index 008a30a6..1e600b95 100644 --- a/crates/egui_demo_lib/src/demo/tooltips.rs +++ b/crates/egui_demo_lib/src/demo/tooltips.rs @@ -12,7 +12,7 @@ impl Default for Tooltips { impl crate::Demo for Tooltips { fn name(&self) -> &'static str { - "🗖 Tooltips" + "💬 Tooltips" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/widget_gallery.rs b/crates/egui_demo_lib/src/demo/widget_gallery.rs index ec5d5f3e..85ae966d 100644 --- a/crates/egui_demo_lib/src/demo/widget_gallery.rs +++ b/crates/egui_demo_lib/src/demo/widget_gallery.rs @@ -61,7 +61,7 @@ impl WidgetGallery { impl crate::Demo for WidgetGallery { fn name(&self) -> &'static str { - "🗄 Widget Gallery" + "🗄️ Widget Gallery" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/src/demo/window_options.rs b/crates/egui_demo_lib/src/demo/window_options.rs index 6a45778e..4c0baa6b 100644 --- a/crates/egui_demo_lib/src/demo/window_options.rs +++ b/crates/egui_demo_lib/src/demo/window_options.rs @@ -23,7 +23,7 @@ pub struct WindowOptions { impl Default for WindowOptions { fn default() -> Self { Self { - title: "🗖 Window Options".to_owned(), + title: "🔲 Window Options".to_owned(), title_bar: true, closable: true, collapsible: true, @@ -42,7 +42,7 @@ impl Default for WindowOptions { impl crate::Demo for WindowOptions { fn name(&self) -> &'static str { - "🗖 Window Options" + "🔲 Window Options" } fn show(&mut self, ui: &mut egui::Ui, open: &mut bool) { diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Bézier Curve.png b/crates/egui_demo_lib/tests/snapshots/demos/Bézier Curve.png index 92726ea0..1b9a9a02 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Bézier Curve.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Bézier Curve.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1bb3195aa5eb1fa7da6f6bb52eec745704e10eb82efb68d5f328f9bfa09fc2d4 -size 32538 +oid sha256:9b64acdb5117adfd9a72abd7d209aa2b735027910237a69192719fe11283f519 +size 32503 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Code Editor.png b/crates/egui_demo_lib/tests/snapshots/demos/Code Editor.png index 30c75f12..d947aca6 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Code Editor.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Code Editor.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b6351ddb148941f73a182e676a3be4ac5d7165c618324459d0061dbf93f213d9 -size 27023 +oid sha256:97cb0c0062e85631336514332fa9563a9f4f8a4702fb599070d37d992efae05b +size 27214 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png b/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png index 17f557c8..28aa3d86 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Code Example.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6df3c0a298d48a5f2e1b36207909e20ea545a72a97461a3ae0792d21e0554f93 -size 76567 +oid sha256:b6ff3c48abecb3776f1d4909f93adae57ed03b0d7082158b38925def8c222d90 +size 76865 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Extra Viewport.png b/crates/egui_demo_lib/tests/snapshots/demos/Extra Viewport.png index 99850aa4..b3169021 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Extra Viewport.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Extra Viewport.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:04f66c848c6f24607d19b809f411a9287ad45067b18da40cf48a77ff65ae9d91 -size 10875 +oid sha256:b8a6ab24302cac6ec80ac9b30354df8db96322a6f5131224eca98ccf75404b95 +size 11104 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Font Book.png b/crates/egui_demo_lib/tests/snapshots/demos/Font Book.png index 09067c97..cf240a08 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Font Book.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Font Book.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:611b18575d2768d40b89f28427bd987d90393e886da2a2c4e09ea6503f85acdd -size 100303 +oid sha256:008a00f0130f3559788c5945fa3b72465d81847f43c5b1072d46f942986d0e9a +size 55640 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Misc Demos.png b/crates/egui_demo_lib/tests/snapshots/demos/Misc Demos.png index f96f4a8f..26e7106d 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Misc Demos.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Misc Demos.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a825dc9c62979fbb8d1ca3d441b4d9e7dbbd234b994901026f35fe7d591ff196 -size 60217 +oid sha256:c6469a1442ab031184ff19b2236ca4e041979205f47c0bb8dbf9b495c07b61de +size 60286 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Modals.png b/crates/egui_demo_lib/tests/snapshots/demos/Modals.png index 32dc226c..9ce02d52 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Modals.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Modals.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:58d56dce5f1e9766dbfdca4ea98fb71f7062b23a0ee4b08ce7f76056513aa9a5 -size 33440 +oid sha256:3d89411d616afdb30e98051786689a97a61a76e8978547d9eada1dee5519d711 +size 33923 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Sliders.png b/crates/egui_demo_lib/tests/snapshots/demos/Sliders.png index 0f2836dd..714be271 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Sliders.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Sliders.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ad81eeb593663cb26295e7fc51987b85a7084b32504e26364f54f9c2129cb790 -size 118048 +oid sha256:0dae980eb1c4fd51be7cd817b8d07a66b51a3abd7e58f5a84df221ce2184e977 +size 118965 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Text Layout.png b/crates/egui_demo_lib/tests/snapshots/demos/Text Layout.png index 793f77bd..5682d242 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Text Layout.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Text Layout.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:121c6d5ba129439d2d747da8e505cfff04a023c28d9a0e3aca324d77de1ef418 -size 79186 +oid sha256:9692a9e2f64a6f0288e86bd9e248a6dea54c2384d0c159d05542490ffb6738ba +size 79979 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png b/crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png index d5513b0e..d2234ff8 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7ad81306e153798d72724e37f23a4037bf986f98e0ecbe3e8d294d23a92ef77c -size 29975 +oid sha256:910d6d4ed7faf6376ac29380a880e25526e10421dd155664c4cd19e82dc5f669 +size 30622 diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Window Options.png b/crates/egui_demo_lib/tests/snapshots/demos/Window Options.png index c5c4ead8..dff69a93 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Window Options.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Window Options.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7bbfacbbc740729ce8729f4507c6cc155a0dcda51dd4442d3db332cf2f6fd9f1 -size 42790 +oid sha256:793b495d10b63537be6331746dead0c280efe244e37786d102b72b30126ccf81 +size 43008 diff --git a/crates/egui_demo_lib/tests/snapshots/modals_1.png b/crates/egui_demo_lib/tests/snapshots/modals_1.png index b2d76916..b4a0bd15 100644 --- a/crates/egui_demo_lib/tests/snapshots/modals_1.png +++ b/crates/egui_demo_lib/tests/snapshots/modals_1.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:01493c64acb62a24d19c69579a8f8cb8e080a60747792bbbb98e1cccdfe9214f -size 47175 +oid sha256:56ba6ff19a46375c89f94f7cf0a359fbde397ba619d67358539bd0b7382a2088 +size 47860 diff --git a/crates/egui_demo_lib/tests/snapshots/modals_2.png b/crates/egui_demo_lib/tests/snapshots/modals_2.png index 70d6b813..36bbafdc 100644 --- a/crates/egui_demo_lib/tests/snapshots/modals_2.png +++ b/crates/egui_demo_lib/tests/snapshots/modals_2.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2718c333123388c8751f461382867b0640ce7a02c220db311c06ff634e6178c1 -size 48620 +oid sha256:871bfe66576d6836784bbda58f884562721934b81791fb407d276c5c1cffc772 +size 48888 diff --git a/crates/egui_demo_lib/tests/snapshots/modals_3.png b/crates/egui_demo_lib/tests/snapshots/modals_3.png index cd460f8a..e7660d36 100644 --- a/crates/egui_demo_lib/tests/snapshots/modals_3.png +++ b/crates/egui_demo_lib/tests/snapshots/modals_3.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a41a61cce2cda5fb9372324f915ffe2b47f68ead5e26b471ea29dfb5208695a7 -size 44600 +oid sha256:b64b2263c92680d3cb90d3e82783dfc69b8602d1e2155c778d5804dd9bd2c1bc +size 44841 diff --git a/crates/egui_demo_lib/tests/snapshots/modals_backdrop_should_prevent_focusing_lower_area.png b/crates/egui_demo_lib/tests/snapshots/modals_backdrop_should_prevent_focusing_lower_area.png index 44c5f4d1..2758c752 100644 --- a/crates/egui_demo_lib/tests/snapshots/modals_backdrop_should_prevent_focusing_lower_area.png +++ b/crates/egui_demo_lib/tests/snapshots/modals_backdrop_should_prevent_focusing_lower_area.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bff8f0531d5cf92e0afe857a04705599d30e77d526091b03296e381b45f02170 -size 44488 +oid sha256:84d488c88126883a6ef8252f22d20c3d90c76c348941b207c9b2674479da6d38 +size 44756 diff --git a/crates/egui_extras/src/syntax_highlighting.rs b/crates/egui_extras/src/syntax_highlighting.rs index f45b68f2..74ce75e1 100644 --- a/crates/egui_extras/src/syntax_highlighting.rs +++ b/crates/egui_extras/src/syntax_highlighting.rs @@ -367,7 +367,7 @@ impl CodeTheme { ui.selectable_value(&mut self.dark_mode, true, "🌙 Dark theme") .on_hover_text("Use the dark mode theme"); - ui.selectable_value(&mut self.dark_mode, false, "☀ Light theme") + ui.selectable_value(&mut self.dark_mode, false, "☀️ Light theme") .on_hover_text("Use the light mode theme"); }); let current_theme_is_dark = self.is_dark(); @@ -436,7 +436,7 @@ impl CodeTheme { ui.selectable_value(&mut self.dark_mode, true, "🌙 Dark theme") .on_hover_text("Use the dark mode theme"); - ui.selectable_value(&mut self.dark_mode, false, "☀ Light theme") + ui.selectable_value(&mut self.dark_mode, false, "☀️ Light theme") .on_hover_text("Use the light mode theme"); }); ui.scope(|ui| { diff --git a/crates/egui_kittest/Cargo.toml b/crates/egui_kittest/Cargo.toml index a770438c..15a02d29 100644 --- a/crates/egui_kittest/Cargo.toml +++ b/crates/egui_kittest/Cargo.toml @@ -60,7 +60,8 @@ open = { workspace = true, optional = true } tempfile = { workspace = true, optional = true } [dev-dependencies] -egui = { workspace = true, features = ["default_fonts"] } +# `monochrome_emoji_fonts` so that the snapshots keep their emoji: +egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } image = { workspace = true, features = ["png"] } egui_extras = { workspace = true, features = ["image", "http"] } diff --git a/crates/egui_system_fonts/CHANGELOG.md b/crates/egui_system_fonts/CHANGELOG.md new file mode 100644 index 00000000..064e8034 --- /dev/null +++ b/crates/egui_system_fonts/CHANGELOG.md @@ -0,0 +1,9 @@ +# Changelog for egui_system_fonts +All notable changes to the `egui_system_fonts` crate will be noted in this file. + + +This file is updated upon each release. +Changes since the last release can be found at or by running the `scripts/generate_changelog.py` script. + + +## Unreleased - Initial release diff --git a/crates/egui_system_fonts/Cargo.toml b/crates/egui_system_fonts/Cargo.toml new file mode 100644 index 00000000..c1dc9b75 --- /dev/null +++ b/crates/egui_system_fonts/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "egui_system_fonts" +version.workspace = true +authors = ["Emil Ernerfeldt "] +description = "Load system fonts on demand for characters the egui fonts lack" +edition.workspace = true +rust-version.workspace = true +homepage = "https://github.com/emilk/egui/tree/main/crates/egui_system_fonts" +license.workspace = true +readme = "README.md" +repository = "https://github.com/emilk/egui/tree/main/crates/egui_system_fonts" +categories = ["gui"] +keywords = ["egui", "gui", "fonts", "text"] +include = ["../../LICENSE-APACHE", "../../LICENSE-MIT", "**/*.rs", "Cargo.toml"] + +[lints] +workspace = true + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--generate-link-to-definition"] + +[lib] + + +[dependencies] +epaint = { workspace = true, features = ["color_fonts"] } + +fontique.workspace = true +log.workspace = true +poll-promise.workspace = true +skrifa.workspace = true +sys-locale.workspace = true +unicode-script.workspace = true + +[dev-dependencies] +egui.workspace = true diff --git a/crates/egui_system_fonts/README.md b/crates/egui_system_fonts/README.md new file mode 100644 index 00000000..00b34c58 --- /dev/null +++ b/crates/egui_system_fonts/README.md @@ -0,0 +1,16 @@ +# egui_system_fonts + +[![Latest version](https://img.shields.io/crates/v/egui_system_fonts.svg)](https://crates.io/crates/egui_system_fonts) +[![Documentation](https://docs.rs/egui_system_fonts/badge.svg)](https://docs.rs/egui_system_fonts) + +Load system fonts on demand for characters that the fonts installed in [egui](https://github.com/emilk/egui) lack. + +The default egui fonts cover Latin, Greek, and Cyrillic. When egui is asked to render e.g. Japanese, Arabic, or Devanagari, it draws `◻`. `egui_system_fonts` provides a [`SystemFontProvider`] that asks the operating system for a font that covers the character, using the same font fallback as native apps (CoreText, DirectWrite, fontconfig, or Android's `fonts.xml`, via [`fontique`](https://crates.io/crates/fontique)). The font file is memory-mapped and installed as a fallback font in egui. + +`eframe` installs it on native by default (feature `system_fonts`). Other integrations can add it themselves: + +```rust +egui_ctx.add_font_provider(std::sync::Arc::new(egui_system_fonts::SystemFontProvider::new())); +``` + +System emoji fonts are skipped for now, since they contain bitmaps or color glyphs that epaint cannot render yet. diff --git a/crates/egui_system_fonts/src/lib.rs b/crates/egui_system_fonts/src/lib.rs new file mode 100644 index 00000000..243d273a --- /dev/null +++ b/crates/egui_system_fonts/src/lib.rs @@ -0,0 +1,261 @@ +//! Load system fonts on demand for characters that the fonts installed in egui lack. +//! +//! [`SystemFontProvider`] is an [`epaint::text::FontProvider`] backed by [`fontique`], +//! which uses the operating system's own font fallback (CoreText, DirectWrite, fontconfig, +//! or Android's `fonts.xml`) to find an installed font for a script and locale. +//! +//! ```no_run +//! # let egui_ctx = egui::Context::default(); +//! egui_ctx.add_font_provider(std::sync::Arc::new(egui_system_fonts::SystemFontProvider::new())); +//! ``` + +use epaint::{ + mutex::Mutex, + text::{ + FallbackRequest, FontData, FontFamily, FontInsert, FontPriority, FontProvider, + InsertFontFamily, + }, +}; +use fontique::{ + Collection, CollectionOptions, FallbackKey, GenericFamily, Language, QueryFamily, QueryFont, + QueryStatus, Script, SourceCache, SourceCacheOptions, +}; +use poll_promise::Promise; + +struct SystemFonts { + collection: Collection, + source_cache: SourceCache, +} + +/// Finds installed system fonts for characters that the egui fonts lack. +/// +/// Uses the operating system's own font fallback via [`fontique`], +/// so e.g. 日本語 gets a Japanese font and العربية an Arabic font. +/// The font files are memory-mapped, not copied. +/// +/// Enumerating the system fonts can take hundreds of milliseconds, +/// so [`Self::new`] starts doing that on a background thread. +/// A lookup before it is done blocks until it is. +pub struct SystemFontProvider { + /// Loaded on a background thread by [`Self::new`]. + fonts: Mutex>, + locale: Option, +} + +impl Default for SystemFontProvider { + fn default() -> Self { + Self::new() + } +} + +impl SystemFontProvider { + /// Start enumerating the system fonts on a background thread. + /// + /// Uses the system locale to pick between fonts (see [`Self::with_locale`]). + pub fn new() -> Self { + Self { + fonts: Mutex::new(Promise::spawn_thread( + "egui_system_fonts", + load_system_fonts, + )), + locale: sys_locale::get_locale().and_then(|locale| parse_locale(&locale)), + } + } + + /// The locale used to pick between fonts for the same script, + /// e.g. between a Japanese and a Simplified Chinese font for CJK ideographs. + /// + /// A BCP-47 language tag like `"ja"` or `"zh-Hans"`, or `None` for the platform default. + /// + /// Default: the system locale. + #[inline] + pub fn with_locale(mut self, locale: Option<&str>) -> Self { + self.locale = locale.and_then(parse_locale); + self + } +} + +fn parse_locale(locale: &str) -> Option { + match Language::parse(locale) { + Ok(language) => Some(language), + Err(err) => { + log::warn!("Failed to parse locale {locale:?}: {err:?}"); + None + } + } +} + +fn load_system_fonts() -> SystemFonts { + let start = std::time::Instant::now(); + let collection = Collection::new(CollectionOptions { + shared: false, + system_fonts: true, + }); + log::debug!("Enumerated system fonts in {:?}", start.elapsed()); + SystemFonts { + collection, + source_cache: SourceCache::new(SourceCacheOptions::default()), + } +} + +impl FontProvider for SystemFontProvider { + fn font_for(&self, request: &FallbackRequest<'_>) -> Option { + let FallbackRequest { cluster, family } = request; + let base_char = cluster.chars().next()?; + + // Blocks if the background thread is not done enumerating the system fonts yet: + let mut fonts = self.fonts.lock(); + let SystemFonts { + collection, + source_cache, + } = fonts.block_until_ready_mut(); + + let generic = match family { + FontFamily::Monospace => GenericFamily::Monospace, + FontFamily::Proportional | FontFamily::Name(_) => GenericFamily::SansSerif, + }; + + let mut query = collection.query(source_cache); + if let Some(script) = script_of(base_char) { + query.set_families([QueryFamily::Generic(generic)]); + query.set_fallbacks(FallbackKey::new(script, self.locale.as_ref())); + } else { + // Punctuation, symbols, emoji, etc. belong to no script, so there is no fallback for them. + // Try the generic families instead. + query.set_families( + [ + generic, + GenericFamily::SystemUi, + GenericFamily::Emoji, + GenericFamily::Math, + GenericFamily::Serif, + GenericFamily::Monospace, + ] + .map(QueryFamily::Generic), + ); + } + + let mut found = None; + query.matches_with(|font| { + if can_render(font, base_char) { + found = Some(font.clone()); + QueryStatus::Stop + } else { + QueryStatus::Continue + } + }); + drop(query); + let font = found?; + + let family_name = collection.family_name(font.family.0).unwrap_or("unknown"); + let name = format!("system:{family_name}:{}:{}", font.family.1, font.index); + log::debug!("Using {name:?} for {base_char:?}"); + + let (blob, _blob_id) = font.blob.into_raw_parts(); + Some(FontInsert { + name, + data: FontData::from_blob(blob, font.index), + families: vec![InsertFontFamily { + family: (*family).clone(), + priority: FontPriority::Lowest, + }], + }) + } +} + +/// The script of a character, or `None` for characters shared between scripts (punctuation, symbols, …). +fn script_of(c: char) -> Option