diff --git a/Cargo.lock b/Cargo.lock index bf4b460c7..90d62aef3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2711,6 +2711,7 @@ dependencies = [ "craft-font-catalog", "craft-font-manager", "craft-font-store", + "craft-fonts-build", "craft-host", "craft-i18n", "craft-theme", diff --git a/crates/font-manager/Cargo.toml b/crates/font-manager/Cargo.toml index 60f0bb878..acfc70058 100644 --- a/crates/font-manager/Cargo.toml +++ b/crates/font-manager/Cargo.toml @@ -46,6 +46,10 @@ log = { version = "0.4", optional = true } # Variation axes and named instances of a face (`axes`), on every feature set. skrifa = { version = "0.44", default-features = false, features = ["std"] } +[build-dependencies] +# The Arabic sample story's face, from the craft fonts set when `CRAFT_FONTS_DIR` names one. +craft-fonts-build = { workspace = true } + [dev-dependencies] craft-font-manager = { path = ".", features = ["suite"] } diff --git a/crates/font-manager/build.rs b/crates/font-manager/build.rs new file mode 100644 index 000000000..37fbbfc44 --- /dev/null +++ b/crates/font-manager/build.rs @@ -0,0 +1,10 @@ +fn main() { + println!("cargo::rerun-if-changed=build.rs"); + // The Arabic sample story's face from the craft fonts set: only for the stories, and never required, so an app + // build with `CRAFT_FONTS_REQUIRED=1` doesn't fail over it. + let stories = std::env::var_os("CARGO_FEATURE_SUITE").is_some(); + let written = craft_fonts_build::Embed::documents(&["Noto Sans Arabic"]).filter(move |_| stories).required(false).write("story_fonts.rs"); + if let Err(e) = written { + println!("cargo::error={e}"); + } +} diff --git a/crates/font-manager/src/picker/stories.rs b/crates/font-manager/src/picker/stories.rs index 5886003aa..6c501947c 100644 --- a/crates/font-manager/src/picker/stories.rs +++ b/crates/font-manager/src/picker/stories.rs @@ -1,10 +1,13 @@ //! Stories of `FontPicker`: the closed field, the list open with recents and a typed query, and the list open with -//! the class column and a filter on. They draw from the two faces egui ships in an in-memory catalog. +//! the class column and a filter on. They draw from the two faces egui ships in an in-memory catalog, with Inter where a +//! story needs a family of two styles and Noto Sans Arabic, from the craft fonts set when the build has +//! `CRAFT_FONTS_DIR`, for the Arabic sample. use std::sync::{Arc, OnceLock}; use craft_behavior::Mixed; use craft_ui::contract::{Bind, Show as _, Validity}; +use craft_ui::fonts::EmbeddedFace; use craft_ui::runtime::env::{Label, Message}; use craft_ui::testing::focus::AccessActionKind; use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, story}; @@ -35,19 +38,32 @@ fn demo(family: &str, settings: FontPickerSettings, filters: FontFilters) -> Dem Demo { family: family.to_owned(), settings, filters } } -/// The story catalog with Inter Regular and SemiBold, a family of two styles. +/// The craft fonts set's Noto Sans Arabic; empty when the build has no `CRAFT_FONTS_DIR`. +static STORY_FONTS: &[EmbeddedFace] = include!(concat!(env!("OUT_DIR"), "/story_fonts.rs")); + +/// A new story catalog with Inter Regular and SemiBold, a family of two styles, and the faces `more`. +fn with_inter(more: &[&'static [u8]]) -> Arc { + let catalog = story_catalog(); + let inter = [ + include_bytes!("../../../ui-contract/fonts/inter/Inter-Regular.ttf").as_slice(), + include_bytes!("../../../ui-contract/fonts/inter/Inter-SemiBold.ttf").as_slice(), + ]; + for bytes in inter.iter().chain(more) { + let _ = catalog.add_bytes(bytes.to_vec(), Source::Bundled, &Limits::default()); + } + catalog +} + +/// The story catalog with Inter Regular and SemiBold. fn styled_catalog() -> Arc { static CATALOG: OnceLock> = OnceLock::new(); - Arc::clone(CATALOG.get_or_init(|| { - let catalog = story_catalog(); - for bytes in [ - include_bytes!("../../../ui-contract/fonts/inter/Inter-Regular.ttf").as_slice(), - include_bytes!("../../../ui-contract/fonts/inter/Inter-SemiBold.ttf").as_slice(), - ] { - let _ = catalog.add_bytes(bytes.to_vec(), Source::Bundled, &Limits::default()); - } - catalog - })) + Arc::clone(CATALOG.get_or_init(|| with_inter(&[]))) +} + +/// The styled catalog with Noto Sans Arabic, which draws both samples, when the build has the craft fonts set. +fn arabic_catalog() -> Arc { + static CATALOG: OnceLock> = OnceLock::new(); + Arc::clone(CATALOG.get_or_init(|| with_inter(&STORY_FONTS.iter().map(|face| face.bytes).collect::>()))) } /// What a story adds to the picker besides its state. @@ -166,11 +182,12 @@ fn font_picker_banner_text(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { show_with(ui, data, demo("Ubuntu", FontPickerSettings::default(), FontFilters::default()), &catalog(), &extras) } -/// Large samples: a face with Arabic letters gets the second sample after the Latin one. +/// Large samples: a face with Arabic letters (Noto Sans Arabic, when the build has the craft fonts set) gets the second +/// sample after the Latin one. #[story(FontPicker<'static>, covers = [Open], drive = Drive::Custom(open_list))] fn font_picker_arabic_sample(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let settings = FontPickerSettings { preview: crate::picker::FontPreviewSize::Large, ..FontPickerSettings::default() }; - show_with(ui, data, demo("Inter", settings, FontFilters::default()), &styled_catalog(), &Extras::default()) + show_with(ui, data, demo("Inter", settings, FontFilters::default()), &arabic_catalog(), &Extras::default()) } /// Settings whose recents are the families `recent`.