diff --git a/changelog/unreleased/2026-11-28-headless-font-smoothing.md b/changelog/unreleased/2026-11-28-headless-font-smoothing.md new file mode 100644 index 0000000000..b470c63259 --- /dev/null +++ b/changelog/unreleased/2026-11-28-headless-font-smoothing.md @@ -0,0 +1 @@ +- **Tooling: deterministic headless `FontSmoothing::On` and `--font-smoothing MODE` CLI flag.** Headless runs (`--snap`, `--frames`, `--video`, `--strip`, `--desktop`, `--snap-on-patch`, `App::headless_runtime`) no longer downgrade `FontSmoothing::On` to `Off` when the host Mac has `AppleFontSmoothing = 0` (globally or in `-currentHost` / ByHost preferences), matching how headless isolates theme, scale, scroll bars and accessibility preferences from the machine's user settings. Added `--font-smoothing MODE` (`on`, `off`, `windfoil`, `windfoil-chromium`, `windfoil-rgb`, `slug`, `hinted-gpu`, `hinted`, `direct-write`, `clear-type`) to override an app's font smoothing from the command line without rebuilding. diff --git a/crates/rune-text/src/lib.rs b/crates/rune-text/src/lib.rs index 2778c3cd6b..1fd8693c66 100644 --- a/crates/rune-text/src/lib.rs +++ b/crates/rune-text/src/lib.rs @@ -391,6 +391,9 @@ pub struct TextSystem { pub smoothing: FontSmoothing, /// The mode the app asked for ([`Self::set_font_smoothing`]). requested: FontSmoothing, + /// Whether `FontSmoothing::On` honours the host macOS `AppleFontSmoothing` user setting + /// (`true` for live windows; `false` for deterministic headless runs and explicit overrides). + respect_user_smoothing: bool, /// The `trak` table of the first font of a family slot at a weight, upright or italic. primary_traks: FxHashMap<(Family, u32, bool), Option>>, /// The hidden-axis coordinates (`hidden_axes`) of the same first font. @@ -537,6 +540,7 @@ impl TextSystem { traks: Traks::new(), smoothing: FontSmoothing::default(), requested: FontSmoothing::default(), + respect_user_smoothing: true, primary_traks: Default::default(), primary_axes: Default::default(), apple: Default::default(), @@ -705,11 +709,42 @@ impl TextSystem { pub fn set_font_smoothing(&mut self, mode: FontSmoothing) { self.requested = mode; self.smoothing = mode.drawn(self.lcd_ok()); - self.apple.set_pipeline(self.smoothing.pipeline()); + self.apple.set_pipeline(self.effective_pipeline(self.smoothing)); self.atlas.lcd = self.smoothing.lcd(); self.atlas.lcd_wanted = mode.lcd(); } + fn effective_pipeline(&self, mode: FontSmoothing) -> ApplePipeline { + if self.respect_user_smoothing { + mode.pipeline() + } else { + mode.pipeline_with_user_smoothing(true) + } + } + + /// Controls whether [`FontSmoothing::On`] honours the host macOS `AppleFontSmoothing` user + /// setting (`true` by default, matching live AppKit/SwiftUI windows; `false` for + /// deterministic headless snapshots and explicit `--font-smoothing` overrides so `On` + /// always dilates regardless of the machine's `AppleFontSmoothing` preference). Re-applies + /// the active mode's pipeline immediately and returns whether it changed. + #[doc(hidden)] + pub fn set_respect_user_font_smoothing(&mut self, respect: bool) -> bool { + if self.respect_user_smoothing == respect { + return false; + } + self.respect_user_smoothing = respect; + let next = self.effective_pipeline(self.smoothing); + let changed = self.apple.pipeline() != next; + self.apple.set_pipeline(next); + changed + } + + /// Whether [`FontSmoothing::On`] honours the host macOS `AppleFontSmoothing` preference. + #[doc(hidden)] + pub fn respect_user_font_smoothing(&self) -> bool { + self.respect_user_smoothing + } + /// Whether subpixel text draws here now: the renderer draws outlines and blends by channel, /// and for ClearType, DirectWrite is there. fn lcd_ok(&mut self) -> bool { diff --git a/crates/rune-text/src/raster/coretext_match.rs b/crates/rune-text/src/raster/coretext_match.rs index add2904812..314513e43b 100644 --- a/crates/rune-text/src/raster/coretext_match.rs +++ b/crates/rune-text/src/raster/coretext_match.rs @@ -147,8 +147,14 @@ impl FontSmoothing { /// `On` draws as `Off` when the user turned smoothing off (`AppleFontSmoothing` 0), as AppKit /// and SwiftUI then do. pub fn pipeline(self) -> ApplePipeline { + self.pipeline_with_user_smoothing(smoothing_allowed_by_user()) + } + + /// [`Self::pipeline`] with macOS's `AppleFontSmoothing` user setting supplied explicitly + /// (`true` ignores a host `AppleFontSmoothing = 0`, as deterministic headless snapshots do). + pub(crate) fn pipeline_with_user_smoothing(self, user_smoothing_allowed: bool) -> ApplePipeline { match self { - FontSmoothing::On if !smoothing_allowed_by_user() => ApplePipeline::UNSMOOTHED, + FontSmoothing::On if !user_smoothing_allowed => ApplePipeline::UNSMOOTHED, FontSmoothing::On | FontSmoothing::Windfoil => ApplePipeline::SMOOTHED, _ => ApplePipeline::UNSMOOTHED, } diff --git a/crates/rune-text/src/raster/coretext_match_tests.rs b/crates/rune-text/src/raster/coretext_match_tests.rs index aaa87650ed..9e482dccfd 100644 --- a/crates/rune-text/src/raster/coretext_match_tests.rs +++ b/crates/rune-text/src/raster/coretext_match_tests.rs @@ -109,3 +109,31 @@ fn off_draws_coverage_as_is_on_apple_platforms() { assert_eq!(text.coverage_exponent([0.9, 0.9, 0.9, 1.0]), 1.0, "a window on this Mac draws Off as is"); } } + +#[test] +fn pipeline_with_user_smoothing_controls_on_without_changing_other_modes() { + assert_eq!(FontSmoothing::On.pipeline_with_user_smoothing(true), ApplePipeline::SMOOTHED); + assert_eq!(FontSmoothing::On.pipeline_with_user_smoothing(false), ApplePipeline::UNSMOOTHED); + assert_eq!(FontSmoothing::Off.pipeline_with_user_smoothing(true), ApplePipeline::UNSMOOTHED); + assert_eq!(FontSmoothing::Off.pipeline_with_user_smoothing(false), ApplePipeline::UNSMOOTHED); + assert_eq!(FontSmoothing::Windfoil.pipeline_with_user_smoothing(true), ApplePipeline::SMOOTHED); + assert_eq!(FontSmoothing::Windfoil.pipeline_with_user_smoothing(false), ApplePipeline::SMOOTHED); +} + +#[test] +fn ignoring_user_font_smoothing_keeps_on_smoothed_and_off_unsmoothed() { + let mut text = TextSystem::default(); + assert!(text.respect_user_font_smoothing()); + text.set_font_smoothing(FontSmoothing::On); + // Simulate a host whose AppleFontSmoothing = 0 downgraded On's pipeline to UNSMOOTHED. + text.set_apple_pipeline(ApplePipeline::UNSMOOTHED); + assert!(text.set_respect_user_font_smoothing(false), "disabling user preference restores SMOOTHED immediately"); + assert!(!text.respect_user_font_smoothing()); + assert_eq!(text.apple_pipeline(), ApplePipeline::SMOOTHED); + assert!(!text.set_respect_user_font_smoothing(false), "setting the same flag is a no-op"); + + text.set_font_smoothing(FontSmoothing::Off); + assert_eq!(text.apple_pipeline(), ApplePipeline::UNSMOOTHED); + text.set_font_smoothing(FontSmoothing::On); + assert_eq!(text.apple_pipeline(), ApplePipeline::SMOOTHED); +} diff --git a/crates/rune/src/app/config.rs b/crates/rune/src/app/config.rs index 577f0729b3..4f8ef25f7f 100644 --- a/crates/rune/src/app/config.rs +++ b/crates/rune/src/app/config.rs @@ -50,6 +50,7 @@ pub(crate) struct RuntimeConfig { pub(crate) tab_visits_controls: bool, pub(crate) srgb_blend: bool, pub(crate) font_smoothing: crate::FontSmoothing, + pub(crate) respect_user_font_smoothing: bool, pub(crate) emoji_family: Option, pub(crate) max_native_materials: usize, pub(crate) reveal_fps: f32, @@ -86,6 +87,7 @@ impl Clone for RuntimeConfig { tab_visits_controls: self.tab_visits_controls, srgb_blend: self.srgb_blend, font_smoothing: self.font_smoothing, + respect_user_font_smoothing: self.respect_user_font_smoothing, emoji_family: self.emoji_family.clone(), max_native_materials: self.max_native_materials, reveal_fps: self.reveal_fps, @@ -110,6 +112,7 @@ impl Default for RuntimeConfig { tab_visits_controls: false, srgb_blend: true, font_smoothing: crate::FontSmoothing::default(), + respect_user_font_smoothing: true, emoji_family: None, max_native_materials: crate::MAX_NATIVE_MATERIALS, reveal_fps: 0.0, @@ -126,6 +129,7 @@ pub(crate) struct CoreConfig { pub(crate) tab_visits_controls: bool, pub(crate) srgb_blend: bool, pub(crate) font_smoothing: crate::FontSmoothing, + pub(crate) respect_user_font_smoothing: bool, pub(crate) emoji_family: Option, pub(crate) max_native_materials: usize, pub(crate) wheel_glide: bool, @@ -141,6 +145,7 @@ impl crate::runtime::RuntimeCore { tab_visits_controls, srgb_blend, font_smoothing, + respect_user_font_smoothing, emoji_family, max_native_materials, wheel_glide, @@ -151,6 +156,7 @@ impl crate::runtime::RuntimeCore { self.set_media_keys(media_keys); self.set_tab_visits_controls(tab_visits_controls); self.set_srgb_blend(srgb_blend); + self.set_respect_user_font_smoothing(respect_user_font_smoothing); self.set_font_smoothing(font_smoothing); if let Some(emoji) = emoji_family { self.set_emoji_family(Some(&emoji)); @@ -177,6 +183,7 @@ impl Runtime { tab_visits_controls, srgb_blend, font_smoothing, + respect_user_font_smoothing, emoji_family, max_native_materials, reveal_fps, @@ -197,6 +204,7 @@ impl Runtime { tab_visits_controls, srgb_blend, font_smoothing, + respect_user_font_smoothing, emoji_family, max_native_materials, wheel_glide, @@ -410,6 +418,7 @@ mod tests { assert!(cfg.srgb_blend); assert!(!cfg.modifier_keys && !cfg.media_keys && !cfg.tab_visits_controls); assert_eq!(cfg.font_smoothing, FontSmoothing::default()); + assert!(cfg.respect_user_font_smoothing); assert_eq!(cfg.emoji_family, None); assert_eq!(cfg.max_native_materials, crate::MAX_NATIVE_MATERIALS); assert_eq!(cfg.reveal_fps, 0.0); diff --git a/crates/rune/src/hot.rs b/crates/rune/src/hot.rs index c0e6368737..5c43683ca0 100644 --- a/crates/rune/src/hot.rs +++ b/crates/rune/src/hot.rs @@ -298,8 +298,10 @@ fn control_input_core(core: &mut RuntimeCore, app: &mut dyn AppDriver, command: /// `{"id":3,"pause":true}`, `{"id":4,"step":0.05}`, or `{"id":5,"quit":true}`. /// Every request and patch publishes a PNG + JSON tree, then a flushed completion marker. #[inline] -fn headless(spec: &AppSpec, state: S, args: Args, runtime: crate::app::RuntimeConfig) { +fn headless(spec: &AppSpec, state: S, args: Args, mut runtime: crate::app::RuntimeConfig) { assert!(enabled(), "--snap-on-patch requires --hot or RUNE_HOT=1 (use scripts/rune-dev)"); + runtime.respect_user_font_smoothing = false; + if let Some(mode) = args.font_smoothing { runtime.font_smoothing = mode; } let size = args.size.unwrap_or(spec.size); let scale = args.scale.unwrap_or(2.0); let (wake, sleep) = std::sync::mpsc::channel(); diff --git a/crates/rune/src/playground/testing.rs b/crates/rune/src/playground/testing.rs index c68c7c4bbf..b3c4e9b375 100644 --- a/crates/rune/src/playground/testing.rs +++ b/crates/rune/src/playground/testing.rs @@ -14,7 +14,8 @@ impl App { let playground = self.playground.take(); let (chrome, titlebar, borderless) = (self.chrome, self.titlebar, self.window_options.borderless); crate::credentials::note_headless_runtime(); - let (spec, state, _, runtime) = self.into_parts(None); + let (spec, state, _, mut runtime) = self.into_parts(None); + runtime.respect_user_font_smoothing = false; let mut rt = crate::tooling::runtime_for_configured(&spec, state, size, env, std::sync::Arc::new(|| {}), Some(runtime)); if let Some(playground) = playground.filter(Playground::is_enabled) { rt.attach_playground(Live::new(playground, &spec, false)); diff --git a/crates/rune/src/runtime/paint/blend.rs b/crates/rune/src/runtime/paint/blend.rs index fa13def267..de5490448d 100644 --- a/crates/rune/src/runtime/paint/blend.rs +++ b/crates/rune/src/runtime/paint/blend.rs @@ -68,6 +68,17 @@ impl RuntimeCore { self.text.font_smoothing() } + pub(crate) fn set_respect_user_font_smoothing(&mut self, respect: bool) { + if self.text.set_respect_user_font_smoothing(respect) { + self.commit.dirty = true; + } + } + + #[cfg(test)] + pub(crate) fn respect_user_font_smoothing(&self) -> bool { + self.text.respect_user_font_smoothing() + } + pub fn clear_color(&self) -> [f32; 4] { if self.transparent { if !self.host.backdrop_opaque_fallback { diff --git a/crates/rune/src/text/pixel_compare.rs b/crates/rune/src/text/pixel_compare.rs index 39e66f20f1..6d99f4222e 100644 --- a/crates/rune/src/text/pixel_compare.rs +++ b/crates/rune/src/text/pixel_compare.rs @@ -396,7 +396,9 @@ pub struct Harness { impl Harness { pub fn new() -> Result { - Ok(Harness { gpu: Gpu::try_new(Gpu::instance(), None)?, text: text_system()?, renderer: None }) + let mut text = text_system()?; + text.set_respect_user_font_smoothing(false); + Ok(Harness { gpu: Gpu::try_new(Gpu::instance(), None)?, text, renderer: None }) } /// `cases` in `pair`'s colours on a `size` (points) canvas at `scale`. diff --git a/crates/rune/src/tooling/args.rs b/crates/rune/src/tooling/args.rs index 5f1af12a9a..3fe1b03fac 100644 --- a/crates/rune/src/tooling/args.rs +++ b/crates/rune/src/tooling/args.rs @@ -15,6 +15,7 @@ pub struct Args { pub scale: Option, pub theme: Option, pub scroll_bars: Option, + pub font_smoothing: Option, pub script: Option, pub frames: Option, pub start: f64, @@ -219,6 +220,7 @@ impl Args { Some(FlagId::Scale) => a.scale = Some(parse_scale(&val(&mut i)).unwrap_or_else(|e| die(&e))), Some(FlagId::Theme) => a.theme = Some(val(&mut i)), Some(FlagId::ScrollBars) => a.scroll_bars = Some(parse_scroll_bars(&val(&mut i)).unwrap_or_else(|e| die(&e))), + Some(FlagId::FontSmoothing) => a.font_smoothing = Some(parse_font_smoothing(&val(&mut i)).unwrap_or_else(|e| die(&e))), Some(FlagId::Script) => a.script = Some(val(&mut i).into()), Some(FlagId::Frames) => a.frames = Some(num(&val(&mut i)) as usize), Some(FlagId::Start) => { @@ -370,6 +372,8 @@ pub(super) const HELP: &str = "usage: [window] --size WxH viewport in points --scale N device scale (default 2) --theme dark|light system appearance --scroll-bars MODE scroll bars: when-scrolling (headless default), always, automatic + --font-smoothing MODE font smoothing: on, off, windfoil, windfoil-chromium, windfoil-rgb, + slug, hinted-gpu, hinted, direct-write, clear-type --script FILE.json timestamped input, replayed headless (move/down/up/tap/drag, keys, {\"focus\":false}); a key with \"repeat\":true is a held key's auto-repeat; {\"type\":\"mark\",\"name\":N} marks a time; @@ -456,3 +460,22 @@ pub(crate) fn parse_scroll_bars(v: &str) -> Result { _ => Err(format!("--scroll-bars is when-scrolling, always or automatic: {v}")), } } + +pub(crate) fn parse_font_smoothing(v: &str) -> Result { + use crate::FontSmoothing; + match v.trim().to_ascii_lowercase().as_str() { + "on" => Ok(FontSmoothing::On), + "off" => Ok(FontSmoothing::Off), + "windfoil" => Ok(FontSmoothing::Windfoil), + "windfoil-chromium" | "windfoilchromium" => Ok(FontSmoothing::WindfoilChromium), + "windfoil-rgb" | "windfoilrgb" => Ok(FontSmoothing::WindfoilRgb), + "slug" => Ok(FontSmoothing::Slug), + "hinted-gpu" | "hintedgpu" => Ok(FontSmoothing::HintedGpu), + "hinted" => Ok(FontSmoothing::Hinted), + "direct-write" | "directwrite" => Ok(FontSmoothing::DirectWrite), + "clear-type" | "cleartype" => Ok(FontSmoothing::ClearType), + _ => Err(format!( + "--font-smoothing is on, off, windfoil, windfoil-chromium, windfoil-rgb, slug, hinted-gpu, hinted, direct-write or clear-type: {v}" + )), + } +} diff --git a/crates/rune/src/tooling/args_tests.rs b/crates/rune/src/tooling/args_tests.rs index ee6574e234..0078ed36a1 100644 --- a/crates/rune/src/tooling/args_tests.rs +++ b/crates/rune/src/tooling/args_tests.rs @@ -155,6 +155,7 @@ fn flags_and_parser_stay_synchronized() { "--scale" => (vec![], Some("2")), "--theme" => (vec![], Some("dark")), "--scroll-bars" => (vec![], Some("always")), + "--font-smoothing" => (vec![], Some("off")), "--script" => (vec![], Some("script.json")), "--frames" | "--every" | "--cols" | "--cpu-boot-threads" => (vec![], Some("4")), "--fps" => (vec![], Some("60")), @@ -290,3 +291,41 @@ fn flags_empty_without_dev_tooling() { #[cfg(not(any(feature = "tooling", feature = "hot", debug_assertions)))] assert!(flags().is_empty()); } + +#[test] +fn font_smoothing_flag_parses_all_modes_and_rejects_unknown() { + use super::args::parse_font_smoothing; + use crate::FontSmoothing; + + let cases = [ + ("on", FontSmoothing::On), + ("On", FontSmoothing::On), + ("off", FontSmoothing::Off), + ("OFF", FontSmoothing::Off), + ("windfoil", FontSmoothing::Windfoil), + ("windfoil-chromium", FontSmoothing::WindfoilChromium), + ("WindfoilChromium", FontSmoothing::WindfoilChromium), + ("windfoil-rgb", FontSmoothing::WindfoilRgb), + ("WindfoilRgb", FontSmoothing::WindfoilRgb), + ("slug", FontSmoothing::Slug), + ("hinted-gpu", FontSmoothing::HintedGpu), + ("HintedGpu", FontSmoothing::HintedGpu), + ("hinted", FontSmoothing::Hinted), + ("direct-write", FontSmoothing::DirectWrite), + ("DirectWrite", FontSmoothing::DirectWrite), + ("clear-type", FontSmoothing::ClearType), + ("ClearType", FontSmoothing::ClearType), + ]; + for (raw, expected) in cases { + assert_eq!(parse_font_smoothing(raw), Ok(expected), "parse {raw:?}"); + let args = Args::parse_from(vec_s(&["--font-smoothing", raw])); + assert_eq!(args.font_smoothing, Some(expected), "argv {raw:?}"); + } + // Every FontSmoothing::ALL variant is reachable by its Debug name. + for mode in FontSmoothing::ALL { + let name = format!("{mode:?}"); + assert_eq!(parse_font_smoothing(&name), Ok(mode), "Debug name {name}"); + } + assert!(parse_font_smoothing("unknown").is_err()); + assert!(parse_font_smoothing("").is_err()); +} diff --git a/crates/rune/src/tooling/flags.rs b/crates/rune/src/tooling/flags.rs index a119c9e93d..cdb24d5dfc 100644 --- a/crates/rune/src/tooling/flags.rs +++ b/crates/rune/src/tooling/flags.rs @@ -191,6 +191,11 @@ define_flags! { value: FlagValue::Required("MODE"), description: "scroll bar mode: when-scrolling, always, or automatic", }, + FontSmoothing => { + name: "--font-smoothing", + value: FlagValue::Required("MODE"), + description: "font smoothing mode: on, off, windfoil, windfoil-chromium, windfoil-rgb, slug, hinted-gpu, hinted, direct-write, or clear-type", + }, Script => { name: "--script", value: FlagValue::Required("FILE.json"), diff --git a/crates/rune/src/tooling/headless.rs b/crates/rune/src/tooling/headless.rs index 87adb9e076..df65019665 100644 --- a/crates/rune/src/tooling/headless.rs +++ b/crates/rune/src/tooling/headless.rs @@ -212,7 +212,9 @@ fn try_headless_configured(mut app: crate::app::App, args: Args, let wake = HeadlessWake::default(); let (spec, mut rt, shared, shortcuts, menus, state_ms) = { let _startup = observability.scope_stages(None); - let (spec, state, shared, runtime) = app.into_parts(args.scene.as_deref()); + let (spec, state, shared, mut runtime) = app.into_parts(args.scene.as_deref()); + runtime.respect_user_font_smoothing = false; + if let Some(mode) = args.font_smoothing { runtime.font_smoothing = mode; } shared.core.connect(wake.waker()); let shortcuts = crate::shortcuts::Guard::attach(&shared, wake.waker(), true, false); let menus = crate::native_menu::MenuGuard::attach(&shared, true); diff --git a/crates/rune/src/tooling/headless/app_tests.rs b/crates/rune/src/tooling/headless/app_tests.rs index 980df1dd5e..301e3f4c7e 100644 --- a/crates/rune/src/tooling/headless/app_tests.rs +++ b/crates/rune/src/tooling/headless/app_tests.rs @@ -137,3 +137,101 @@ fn headless_run_quit_request_defers_commits_dialog_and_quits_when_allowed() { assert_eq!(plain.run_quit_request(), crate::QuitResponse::Quit); assert_eq!(plain.rt.state.quits, 1); } + +#[test] +fn headless_runtime_ignores_user_font_smoothing_and_distinguishes_on_and_off() { + use crate::{Color, FontSmoothing, text}; + + let spec = || { + AppSpec::new("smoothing", (240.0, 80.0), || (), |_: &(), _: &mut Cx<()>| { + el().w_full().h_full().fill(crate::color::hex("#141518").unwrap()).child( + text("Hamburgefonstiv 12345").size(14.0, 400.0).color(Color::WHITE), + ) + }) + }; + + let mut on = App::new(spec()).font_smoothing(FontSmoothing::On).headless_runtime((240.0, 80.0), Env::new(true, 2.0)); + let mut off = App::new(spec()).font_smoothing(FontSmoothing::Off).headless_runtime((240.0, 80.0), Env::new(true, 2.0)); + assert!(!on.core().respect_user_font_smoothing()); + assert!(!off.core().respect_user_font_smoothing()); + assert_eq!(on.font_smoothing(), FontSmoothing::On); + assert_eq!(off.font_smoothing(), FontSmoothing::Off); + assert_eq!(on.text.apple_pipeline(), crate::text::ApplePipeline::SMOOTHED); + assert_eq!(off.text.apple_pipeline(), crate::text::ApplePipeline::UNSMOOTHED); + + on.frame(0.0); + off.frame(0.0); + let ink = |rt: &Runtime<()>| -> u64 { + rt.instances.iter() + .filter(|i| i.params[1] == crate::render::KIND_GLYPH) + .map(|i| { + let page = rt.text.atlas.page_data(i.params[2] as usize).unwrap(); + let (ux, uy) = (i.uv[0] as u32, i.uv[1] as u32); + let (w, h) = (i.rect[2] as u32, i.rect[3] as u32); + let mut sum = 0u64; + for y in 0..h { + for x in 0..w { + sum += page[((uy + y) * crate::text::ATLAS_SIZE + ux + x) as usize] as u64; + } + } + sum + }) + .sum() + }; + assert!(ink(&off) < ink(&on), "On must dilate more than Off in headless runtime: off={} on={}", ink(&off), ink(&on)); +} + +#[test] +fn headless_snap_font_smoothing_on_and_off_differ_and_cli_flag_overrides_app() { + use crate::{Color, FontSmoothing, text}; + let _turn = crate::tooling::headless_run::test_turn(); + + let spec = || { + AppSpec::new("smoothing-snap", (240.0, 80.0), || (), |_: &(), _: &mut Cx<()>| { + el().w_full().h_full().fill(crate::color::hex("#141518").unwrap()).child( + text("Hamburgefonstiv 12345").size(14.0, 400.0).color(Color::WHITE), + ) + }) + }; + + let dir = std::env::temp_dir().join(format!("rune-smoothing-snap-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let app_on = dir.join("app_on.png"); + let app_off = dir.join("app_off.png"); + let cli_on = dir.join("cli_on.png"); + let cli_off = dir.join("cli_off.png"); + + let snap = |mode: FontSmoothing, extra: &[&str], out: &PathBuf| { + let mut argv: Vec = vec![ + "--size".into(), "240x80".into(), + "--scale".into(), "2".into(), + "--theme".into(), "dark".into(), + "--snap".into(), out.to_string_lossy().into_owned(), + ]; + argv.extend(extra.iter().map(|s| (*s).into())); + super::super::try_headless(App::new(spec()).font_smoothing(mode), crate::tooling::Args::parse_from(argv)) + .map_err(|e| e.message()) + }; + + let res_on = snap(FontSmoothing::On, &[], &app_on); + if let Err(err) = &res_on { + if err.contains("adapter") || err.contains("GPU") { + let _ = std::fs::remove_dir_all(&dir); + return; + } + } + res_on.expect("snap On"); + snap(FontSmoothing::Off, &[], &app_off).expect("snap Off"); + snap(FontSmoothing::Off, &["--font-smoothing", "on"], &cli_on).expect("snap --font-smoothing on"); + snap(FontSmoothing::On, &["--font-smoothing", "off"], &cli_off).expect("snap --font-smoothing off"); + + let bytes_app_on = std::fs::read(&app_on).unwrap(); + let bytes_app_off = std::fs::read(&app_off).unwrap(); + let bytes_cli_on = std::fs::read(&cli_on).unwrap(); + let bytes_cli_off = std::fs::read(&cli_off).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + + assert_ne!(bytes_app_on, bytes_app_off, "FontSmoothing::On and Off must not render identical headless PNGs"); + assert_eq!(bytes_cli_on, bytes_app_on, "--font-smoothing on overrides App::font_smoothing(Off)"); + assert_eq!(bytes_cli_off, bytes_app_off, "--font-smoothing off overrides App::font_smoothing(On)"); +} diff --git a/crates/rune/src/tooling/virtual_desktop/window.rs b/crates/rune/src/tooling/virtual_desktop/window.rs index 0ff661916c..07ed1e0f5e 100644 --- a/crates/rune/src/tooling/virtual_desktop/window.rs +++ b/crates/rune/src/tooling/virtual_desktop/window.rs @@ -293,7 +293,9 @@ impl Window { let (chrome, titlebar) = (app.chrome, app.titlebar); let (playground, attach_playground) = (app.playground.take(), app.attach_playground); crate::credentials::note_headless_runtime(); - let (spec, state, shared, runtime) = app.into_parts(own.scene.as_deref()); + let (spec, state, shared, mut runtime) = app.into_parts(own.scene.as_deref()); + runtime.respect_user_font_smoothing = false; + if let Some(mode) = own.font_smoothing.or(args.font_smoothing) { runtime.font_smoothing = mode; } let shortcuts = crate::shortcuts::Guard::attach(&shared, Arc::new(|| {}), true, false); let menus = crate::native_menu::MenuGuard::attach(&shared, true); let size = own.size.unwrap_or(spec.size); diff --git a/crates/rune/src/window/host.rs b/crates/rune/src/window/host.rs index 79215e8576..257a0de5ba 100644 --- a/crates/rune/src/window/host.rs +++ b/crates/rune/src/window/host.rs @@ -290,6 +290,10 @@ impl Host { if let Some(hook) = app.on_displays.take() { cx.on_displays.push(hook); } + if let Some(mode) = args.font_smoothing { + app = app.font_smoothing(mode); + app.runtime.respect_user_font_smoothing = false; + } let (prepared, native_handle, slot, gpu_pool, wayland) = HostWaker::prepare(&key, handle, &mut app.overlays, cx, app.standard_menus); let (on_window, on_ready, on_frame_encoded, on_frame_drawn) = (