agg_gui/font_settings.rs
1//! System-wide font / text rendering settings.
2//!
3//! Mirrors the `theme::current_visuals` / `theme::set_visuals` pattern and
4//! the scrollbar-style globals (`current_scroll_style` / `set_scroll_style`).
5//! Widgets that care about rendering style (`Label`, `Button`, `TextField`,
6//! ...) should consult these at **layout/paint time** so changes made by
7//! the System window propagate without a widget-tree rebuild.
8//!
9//! # Convention
10//!
11//! Each setting has:
12//! - an **override** stored in a thread-local cell (`None` or `false` by default),
13//! - a getter (e.g. [`current_system_font`], [`lcd_enabled`]),
14//! - a setter (e.g. [`set_system_font`], [`set_lcd_enabled`]).
15//!
16//! Widgets pick between the global override and their own per-instance value
17//! — analogous to how a `ScrollView` takes the global scroll style unless
18//! the caller wired an explicit one.
19
20use std::cell::RefCell;
21use std::sync::atomic::{AtomicU64, Ordering};
22use std::sync::Arc;
23
24use crate::text::Font;
25
26// ---------------------------------------------------------------------------
27// Typography epoch
28// ---------------------------------------------------------------------------
29//
30// Bumped every time any typography-style global (font, size scale,
31// LCD, hinting, gamma, width, interval, faux weight/italic, primary
32// weight) changes. Backbuffered widgets (`Label`, `TextField`, …)
33// compare this epoch against the one they rasterised at and
34// self-invalidate on mismatch — same trick we use for theme epoch.
35// Without this, dragging a slider in the System window would leave
36// pre-existing `Label` caches showing the old style until something
37// else invalidated them.
38
39static TYPOGRAPHY_EPOCH: AtomicU64 = AtomicU64::new(1);
40
41/// Current typography epoch. Widget render paths read this each frame.
42pub fn current_typography_epoch() -> u64 {
43 TYPOGRAPHY_EPOCH.load(Ordering::Relaxed)
44}
45
46/// Internal helper: called by every setter in this module after it
47/// writes. Keeps the epoch in lock-step with the globals.
48fn bump_typography_epoch() {
49 TYPOGRAPHY_EPOCH.fetch_add(1, Ordering::Relaxed);
50}
51
52// ---------------------------------------------------------------------------
53// Thread-local storage
54// ---------------------------------------------------------------------------
55
56thread_local! {
57 /// System-wide font override. `None` means "widgets keep whatever font
58 /// they were constructed with".
59 static SYSTEM_FONT: RefCell<Option<Arc<Font>>> = RefCell::new(None);
60 /// System-wide font size multiplier — applied to every widget's own
61 /// `font_size` at paint/layout time. `1.0` = unchanged. Acts like
62 /// egui's `pixels_per_point` for typography: shrink or enlarge ALL
63 /// text while preserving the relative hierarchy (body stays smaller
64 /// than headings, etc.).
65 static FONT_SIZE_SCALE: RefCell<f64> = RefCell::new(1.0);
66 /// System-wide LCD-subpixel override. When `Some(true|false)`, text-
67 /// rendering widgets honour it directly. When `None` (the default),
68 /// [`lcd_enabled`] derives the effective value from
69 /// [`crate::device_scale`]: LCD is enabled at standard DPI (scale ≤
70 /// 1.25) and disabled at HiDPI, because LCD subpixel rendering only
71 /// pays off when subpixels are roughly the size of a glyph stem; at
72 /// 2× scale the AA halo is already wide enough that grayscale wins on
73 /// chroma fringing while looking identical otherwise. An explicit
74 /// [`set_lcd_enabled`] overrides the auto-derivation; apps that just
75 /// want the default should never need to call it.
76 static LCD_ENABLED: RefCell<Option<bool>> = const { RefCell::new(None) };
77 /// System-wide hinting toggle — forwarded to the font engine when the
78 /// engine supports it. `ttf-parser` does NOT run a hinting interpreter,
79 /// so what we do is **Y-axis-only baseline hinting**: snap the glyph
80 /// origin's Y coordinate to the pixel grid before rasterisation,
81 /// matching the `(y + 0.5).floor()` convention from the AGG C++
82 /// `truetype_test_02_win` demo. This preserves horizontal subpixel
83 /// positioning (critical for LCD) while giving sharper vertical
84 /// metrics — the pragmatic compromise used by the agg-rust reference.
85 static HINTING_ENABLED: RefCell<bool> = RefCell::new(false);
86
87 // ── Typography-style parameters (drive the TrueType LCD Subpixel demo
88 // and, once the render pipeline is wired up, every text paint
89 // globally). Ranges mirror the agg-rust `truetype_test` demo so
90 // numbers stay comparable against the reference implementation.
91
92 /// Gamma correction applied post-raster. 1.0 = off (linear output).
93 /// Range 0.5..=2.5.
94 static GAMMA: RefCell<f64> = RefCell::new(1.0);
95 /// Horizontal glyph width scale. 1.0 = native widths.
96 /// Range 0.75..=1.25.
97 static WIDTH: RefCell<f64> = RefCell::new(1.0);
98 /// Extra letter-spacing as a fraction of em. 0.0 = unchanged.
99 /// Range -0.2..=0.2.
100 static INTERVAL: RefCell<f64> = RefCell::new(0.0);
101 /// Synthetic boldness via outline contour offset.
102 /// Range -1.0..=1.0; 0.0 = unchanged, positive = heavier, negative = lighter.
103 static FAUX_WEIGHT: RefCell<f64> = RefCell::new(0.0);
104 /// Synthetic italic slant expressed as a horizontal-shear factor.
105 /// Range -1.0..=1.0; 0.0 = upright.
106 static FAUX_ITALIC: RefCell<f64> = RefCell::new(0.0);
107 /// LCD primary-weight (the pixel coverage weight of the own-channel
108 /// vs the neighbouring channels in the 3-tap distribution LUT).
109 /// Range 0.0..=1.0; default 1/3 gives a neutral LUT.
110 static PRIMARY_WEIGHT: RefCell<f64> = RefCell::new(1.0 / 3.0);
111}
112
113// ---------------------------------------------------------------------------
114// Font
115// ---------------------------------------------------------------------------
116
117/// Current system font override, if set. Widgets should prefer this over
118/// their own `self.font` when the override is `Some(_)` so user changes in
119/// the System window propagate live.
120pub fn current_system_font() -> Option<Arc<Font>> {
121 SYSTEM_FONT.with(|c| c.borrow().clone())
122}
123
124/// Replace the system font override. Pass `None` to clear and fall back
125/// to per-widget fonts.
126pub fn set_system_font(font: Option<Arc<Font>>) {
127 SYSTEM_FONT.with(|c| *c.borrow_mut() = font);
128 bump_typography_epoch();
129}
130
131// ---------------------------------------------------------------------------
132// Font size scale
133// ---------------------------------------------------------------------------
134
135/// Current font size multiplier. Widgets reading a `self.font_size`
136/// should consult this (via e.g. `Label::active_font_size`) so a single
137/// slider in the System window can grow or shrink all text uniformly.
138pub fn current_font_size_scale() -> f64 {
139 FONT_SIZE_SCALE.with(|c| *c.borrow())
140}
141
142/// Set the system font-size multiplier. Clamped to a sensible range so
143/// typos or edge-case inputs can't hide every label or fry the layout.
144pub fn set_font_size_scale(scale: f64) {
145 let clamped = scale.clamp(0.5, 3.0);
146 FONT_SIZE_SCALE.with(|c| *c.borrow_mut() = clamped);
147 bump_typography_epoch();
148}
149
150// ---------------------------------------------------------------------------
151// LCD subpixel toggle
152// ---------------------------------------------------------------------------
153
154/// Whether widgets should rasterise text through the LCD subpixel path.
155///
156/// Whether widgets should rasterise text through the LCD subpixel path.
157///
158/// **Hard cap first:** LCD is NEVER used above standard density. The gate
159/// keys on the *effective* scale ([`crate::ux_scale::effective_scale`] =
160/// device DPR × UX zoom), because LCD subpixel rendering only pays off when
161/// individual physical pixels are large enough to resolve the R/G/B
162/// sub-stripes — and "small pixels" come from a high UX zoom (mobile /
163/// accessibility) just as much as from a HiDPI panel. Above `1.25×` it is
164/// pure overhead with no visible benefit, so we force the cheaper grayscale
165/// path *regardless of any explicit override*. This also keeps
166/// CPU-backbuffered widgets (the menu bar) off the LCD blit at high scale,
167/// where they would otherwise shrink.
168///
169/// At standard density the explicit override set via [`set_lcd_enabled`]
170/// wins if present; otherwise LCD defaults on. So platform shells generally
171/// don't need to call [`set_lcd_enabled`] at all.
172pub fn lcd_enabled() -> bool {
173 // Never LCD at high effective density — overrides included.
174 if crate::ux_scale::effective_scale() > 1.25 {
175 return false;
176 }
177 if let Some(explicit) = LCD_ENABLED.with(|c| *c.borrow()) {
178 return explicit;
179 }
180 true
181}
182
183/// Pin LCD subpixel rendering to a specific value, overriding the
184/// device-scale-derived default. System-window toggles use this; apps
185/// that just want sensible default behaviour should not call it.
186pub fn set_lcd_enabled(on: bool) {
187 LCD_ENABLED.with(|c| *c.borrow_mut() = Some(on));
188 bump_typography_epoch();
189}
190
191/// Drop any explicit override and return to device-scale-derived auto.
192/// Counterpart to [`set_lcd_enabled`]; used by tests and by System-
193/// window "reset to default" affordances.
194pub fn clear_lcd_enabled_override() {
195 LCD_ENABLED.with(|c| *c.borrow_mut() = None);
196 bump_typography_epoch();
197}
198
199// ---------------------------------------------------------------------------
200// Hinting toggle
201// ---------------------------------------------------------------------------
202
203pub fn hinting_enabled() -> bool {
204 HINTING_ENABLED.with(|c| *c.borrow())
205}
206
207pub fn set_hinting_enabled(on: bool) {
208 HINTING_ENABLED.with(|c| *c.borrow_mut() = on);
209 bump_typography_epoch();
210}
211
212// ---------------------------------------------------------------------------
213// Typography-style parameters
214// ---------------------------------------------------------------------------
215//
216// All six follow the same shape: an immutable thread-local, a getter,
217// and a clamping setter. The clamp ranges mirror the agg-rust
218// `truetype_test` demo so results stay numerically comparable. Callers
219// (System window widgets + the TrueType LCD Subpixel demo) bind to
220// these via `Rc<Cell<f64>>` mirrors owned by `SystemCells`; the global
221// is the source-of-truth for rendering, the cell is the source-of-truth
222// for UI widgets and disk persistence.
223
224pub fn current_gamma() -> f64 {
225 GAMMA.with(|c| *c.borrow())
226}
227pub fn set_gamma(v: f64) {
228 let clamped = v.clamp(0.5, 2.5);
229 GAMMA.with(|c| *c.borrow_mut() = clamped);
230 bump_typography_epoch();
231}
232
233pub fn current_width() -> f64 {
234 WIDTH.with(|c| *c.borrow())
235}
236pub fn set_width(v: f64) {
237 let clamped = v.clamp(0.75, 1.25);
238 WIDTH.with(|c| *c.borrow_mut() = clamped);
239 bump_typography_epoch();
240}
241
242pub fn current_interval() -> f64 {
243 INTERVAL.with(|c| *c.borrow())
244}
245pub fn set_interval(v: f64) {
246 let clamped = v.clamp(-0.2, 0.2);
247 INTERVAL.with(|c| *c.borrow_mut() = clamped);
248 bump_typography_epoch();
249}
250
251pub fn current_faux_weight() -> f64 {
252 FAUX_WEIGHT.with(|c| *c.borrow())
253}
254pub fn set_faux_weight(v: f64) {
255 let clamped = v.clamp(-1.0, 1.0);
256 FAUX_WEIGHT.with(|c| *c.borrow_mut() = clamped);
257 bump_typography_epoch();
258}
259
260pub fn current_faux_italic() -> f64 {
261 FAUX_ITALIC.with(|c| *c.borrow())
262}
263pub fn set_faux_italic(v: f64) {
264 let clamped = v.clamp(-1.0, 1.0);
265 FAUX_ITALIC.with(|c| *c.borrow_mut() = clamped);
266 bump_typography_epoch();
267}
268
269pub fn current_primary_weight() -> f64 {
270 PRIMARY_WEIGHT.with(|c| *c.borrow())
271}
272pub fn set_primary_weight(v: f64) {
273 let clamped = v.clamp(0.0, 1.0);
274 PRIMARY_WEIGHT.with(|c| *c.borrow_mut() = clamped);
275 bump_typography_epoch();
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281
282 #[test]
283 fn test_lcd_flag_explicit_override_round_trips() {
284 // Reset to known state — thread-locals can leak across tests reusing
285 // the same worker thread. Standard density so the high-scale cap in
286 // `lcd_enabled` doesn't mask the override under test.
287 crate::device_scale::set_device_scale(1.0);
288 crate::ux_scale::set_ux_scale(1.0);
289 set_lcd_enabled(false);
290 assert!(!lcd_enabled());
291 set_lcd_enabled(true);
292 assert!(lcd_enabled());
293 clear_lcd_enabled_override();
294 }
295
296 #[test]
297 fn test_lcd_flag_auto_derives_from_device_scale_when_no_override() {
298 use crate::device_scale::set_device_scale;
299 clear_lcd_enabled_override();
300 set_device_scale(1.0);
301 assert!(lcd_enabled(), "standard DPI should default to LCD on");
302 set_device_scale(2.0);
303 assert!(!lcd_enabled(), "HiDPI should default to LCD off");
304 // Restore to a sane state for sibling tests.
305 set_device_scale(1.0);
306 }
307
308 #[test]
309 fn test_lcd_auto_disabled_at_high_effective_scale_from_ux_zoom() {
310 // LCD subpixel rendering is pointless overhead once the on-screen
311 // pixel density is high — and "high density" can come from the UX
312 // zoom (mobile / accessibility) just as much as from the device DPR.
313 // A device at 1.0 DPR with ux_scale 1.7 renders everything at 1.7×;
314 // LCD must auto-disable there, exactly as it does at 1.7× DPR.
315 // Regression: the auto-derivation keyed on `device_scale` alone, so a
316 // ux-zoomed standard-DPI display kept LCD on, which routed the
317 // CPU-backbuffered menu bar through the LCD blit and rendered it tiny.
318 use crate::device_scale::set_device_scale;
319 use crate::ux_scale::set_ux_scale;
320 clear_lcd_enabled_override();
321 set_device_scale(1.0);
322 set_ux_scale(1.0);
323 assert!(lcd_enabled(), "standard DPI + no zoom should default to LCD on");
324 set_ux_scale(1.7);
325 assert!(
326 !lcd_enabled(),
327 "high effective scale via ux zoom should default to LCD off"
328 );
329 // The high-scale gate is a HARD CAP: it wins even over an explicit
330 // override, so "force LCD on" can't reintroduce the overhead (and the
331 // tiny-menu blit) at high density.
332 set_lcd_enabled(true);
333 assert!(
334 !lcd_enabled(),
335 "explicit LCD-on override must still be capped off at high effective scale"
336 );
337 // ...but at standard density the explicit override is honoured.
338 set_ux_scale(1.0);
339 assert!(lcd_enabled(), "override LCD-on must apply at standard density");
340 // Restore sane state for sibling tests.
341 clear_lcd_enabled_override();
342 set_ux_scale(1.0);
343 set_device_scale(1.0);
344 }
345
346 #[test]
347 fn test_hinting_flag_default_off() {
348 set_hinting_enabled(false);
349 assert!(!hinting_enabled());
350 set_hinting_enabled(true);
351 assert!(hinting_enabled());
352 set_hinting_enabled(false);
353 }
354
355 #[test]
356 fn test_system_font_default_none() {
357 // Reset first — other tests may have set it.
358 set_system_font(None);
359 assert!(current_system_font().is_none());
360 }
361}