Skip to main content

agg_gui/widgets/
label.rs

1//! `Label` — static text display widget.
2//!
3//! Labels are non-interactive by design (`hit_test` always returns `false`
4//! and `on_event` always returns `Ignored`).  This makes them safe to use as
5//! transparent overlay children inside interactive parents like `Button` — the
6//! parent retains full hit-test and focus ownership.
7//!
8//! # Backbuffer
9//!
10//! When `buffered` is `true` AND the active `DrawCtx` supports image blitting
11//! (`ctx.has_image_blit()` returns `true`, i.e. the software `GfxCtx` path),
12//! the label pre-renders its glyphs into an offscreen `Framebuffer` on the
13//! first `paint()` call — or whenever `text`, `font_size`, `color`, or `bounds`
14//! change — and blits the cached pixels every subsequent frame via
15//! `ctx.draw_image_rgba()`.  No font shaping or rasterisation occurs on cache
16//! hits.
17//!
18//! On the GL path (`has_image_blit()` → false) the label falls back to the
19//! direct `fill_text()` call; the GL path's `GlyphCache` provides equivalent
20//! glyph-level savings there.
21
22use std::sync::Arc;
23
24use crate::color::Color;
25use crate::draw_ctx::DrawCtx;
26use crate::event::{Event, EventResult};
27use crate::geometry::{Point, Rect, Size};
28use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
29use crate::text::Font;
30use crate::widget::Widget;
31
32/// Break `text` into lines that each fit within `max_width` pixels at the given
33/// font size.  Explicit `\n` characters always produce a new line.  Returns at
34/// least one entry (possibly an empty string for blank text).
35fn wrap_text(font: &Arc<Font>, text: &str, font_size: f64, max_width: f64) -> Vec<String> {
36    use crate::text::measure_text_metrics;
37    let mut result = Vec::new();
38    for paragraph in text.split('\n') {
39        if paragraph.trim().is_empty() {
40            // Preserve explicit blank lines.
41            result.push(String::new());
42            continue;
43        }
44        let mut current: String = String::new();
45        for word in paragraph.split_whitespace() {
46            if current.is_empty() {
47                current.push_str(word);
48            } else {
49                let candidate = format!("{current} {word}");
50                let w = measure_text_metrics(font, &candidate, font_size).width;
51                if w <= max_width {
52                    current = candidate;
53                } else {
54                    result.push(std::mem::replace(&mut current, word.to_string()));
55                }
56            }
57        }
58        if !current.is_empty() {
59            result.push(current);
60        }
61    }
62    if result.is_empty() {
63        result.push(String::new());
64    }
65    result
66}
67
68/// Horizontal alignment for `Label` text.
69#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
70pub enum LabelAlign {
71    #[default]
72    Left,
73    Center,
74    Right,
75}
76
77/// A non-interactive text widget.
78///
79/// Used directly as a standalone label, and as a child of composite widgets
80/// such as [`Button`] and (in the future) `Checkbox`, `RadioGroup`, etc.
81///
82/// When no explicit color is set via [`with_color`](Label::with_color), the
83/// label reads its text color from the active [`Visuals`](crate::theme::Visuals)
84/// at paint time (`ctx.visuals().text_color`), so it automatically adapts to
85/// dark / light mode switches.
86pub struct Label {
87    bounds: Rect,
88    children: Vec<Box<dyn Widget>>, // always empty
89    base: WidgetBase,
90    text: String,
91    font: Arc<Font>,
92    font_size: f64,
93    /// `None` → use a theme colour at paint time (`text_dim` when `dim`,
94    /// else `text_color`).
95    /// `Some(c)` → explicit override (e.g. accent-coloured text).
96    color: Option<Color>,
97    /// When `true` and no explicit `color` is set, follow the theme's
98    /// dimmed/secondary text colour (`visuals().text_dim`) for hints and
99    /// captions.  Resolves at paint time like the `text_color` default, so
100    /// it tracks live dark/light switches.
101    dim: bool,
102    align: LabelAlign,
103    /// When `true` (the default), this Label owns a CPU backbuffer
104    /// that's re-rasterised on dirty and blitted every frame.  Set to
105    /// `false` only for text that changes every frame (e.g. live
106    /// counters) where caching adds overhead with no benefit — those
107    /// go through `ctx.fill_text` direct every paint.
108    pub buffered: bool,
109    /// Per-widget CPU bitmap cache.  Populated by `paint_subtree` when
110    /// `buffered = true`; invalidated by Label's setters (text, color,
111    /// align, etc.) so the next paint re-rasterises.
112    cache: crate::widget::BackbufferCache,
113    /// When `true`, long lines are broken at word boundaries to fit
114    /// `available.width`.  The label height expands to fit all lines.
115    /// Disabled by default; enable with `.with_wrap(true)`.
116    wrap: bool,
117    /// When `true`, this Label ignores the system-wide font override
118    /// (`font_settings::current_system_font`) and always renders with
119    /// the specific `self.font` passed to `Label::new`.  Used by font
120    /// preview widgets (ComboBox item labels in the System window's
121    /// font selector) where each entry must render in its OWN face
122    /// regardless of the current global font choice.
123    ignore_system_font: bool,
124    /// Per-instance LCD preference: `Some(true)` always LCD, `Some(false)`
125    /// always grayscale, `None` defers to the global
126    /// `font_settings::lcd_enabled()`.  Exposed on every widget via
127    /// `Widget::lcd_preference`; Label is the only widget that reads it
128    /// today.
129    lcd_pref: Option<bool>,
130
131    // ── Layout measurement cache ──────────────────────────────────────────────
132    /// Cached text advance width from last `measure_advance()` call.
133    /// Avoids calling `rustybuzz::shape()` every frame — only re-measures
134    /// when `text` or `font_size` changes.
135    layout_text: String,
136    layout_font_size: f64,
137    layout_width: f64,
138    /// Pointer identity of the [`Font`] used for the last measurement.  If
139    /// the system-wide font override (see
140    /// [`font_settings::current_system_font`](crate::font_settings::current_system_font))
141    /// is swapped, pointer identity changes and we re-measure to pick up
142    /// the new font's glyph metrics.
143    layout_font_ptr: *const Font,
144    /// Width used for the last word-wrap computation.
145    wrap_at_width: f64,
146    /// Lines produced by the last word-wrap computation.
147    wrapped_lines: Vec<String>,
148}
149
150impl Label {
151    pub fn new(text: impl Into<String>, font: Arc<Font>) -> Self {
152        Self {
153            bounds: Rect::default(),
154            children: Vec::new(),
155            base: WidgetBase::new(),
156            text: text.into(),
157            font,
158            font_size: 14.0,
159            color: None, // resolved from ctx.visuals() at paint time
160            dim: false,
161            align: LabelAlign::Left,
162            // Default: backbuffer only when grayscale.  Rationale:
163            //   - Grayscale on GL direct-to-surface goes through
164            //     tessellated glyph outlines, which are visibly thinner
165            //     than AGG's subpixel-accurate scanline coverage.
166            //     Routing grayscale through a software backbuffer gives
167            //     AGG-quality rasterisation blitted as a texture.
168            //   - LCD on GL direct-to-surface uses dual-source blend on
169            //     the cached LCD mask — identical quality to AGG.
170            //     Adding a backbuffer here would force the sub-ctx into
171            //     `Rgba` mode (Label has no opaque bg for `LcdCoverage`)
172            //     and lose the subpixel result.
173            // `buffered` stores the user's opt-out; the actual decision
174            // happens in `backbuffer_cache_mut` based on the global
175            // LCD flag.
176            buffered: true,
177            cache: crate::widget::BackbufferCache::new(),
178            wrap: false,
179            ignore_system_font: false,
180            lcd_pref: None,
181            layout_text: String::new(),
182            layout_font_size: 0.0,
183            layout_width: 0.0,
184            layout_font_ptr: std::ptr::null(),
185            wrap_at_width: -1.0,
186            wrapped_lines: Vec::new(),
187        }
188    }
189
190    // ── builder methods ───────────────────────────────────────────────────────
191
192    pub fn with_font_size(mut self, size: f64) -> Self {
193        self.font_size = size;
194        self
195    }
196    /// Override the label colour.  Pass an explicit `Color` to always use that
197    /// colour regardless of the active theme.  Omit this call to follow the
198    /// theme's `text_color` automatically.
199    pub fn with_color(mut self, color: Color) -> Self {
200        self.color = Some(color);
201        self
202    }
203    /// Follow the theme's *dimmed* text colour (`visuals().text_dim`)
204    /// instead of the primary `text_color` — for secondary / hint /
205    /// caption text.  Has no effect when an explicit
206    /// [`with_color`](Label::with_color) is also set.  Like the default
207    /// `text_color` path this resolves at paint time, so it stays readable
208    /// across live dark/light theme switches (unlike a hard-coded grey).
209    pub fn with_dim(mut self, dim: bool) -> Self {
210        self.dim = dim;
211        self
212    }
213    pub fn with_align(mut self, align: LabelAlign) -> Self {
214        self.align = align;
215        self
216    }
217    pub fn with_has_backbuffer(mut self, v: bool) -> Self {
218        self.buffered = v;
219        self
220    }
221    /// Enable or disable word-wrapping.  When `true`, long lines are broken at
222    /// word boundaries to fit the available width; the label height expands to
223    /// accommodate all lines.  Newlines in the text are always honoured.
224    pub fn with_wrap(mut self, wrap: bool) -> Self {
225        self.wrap = wrap;
226        self
227    }
228
229    /// Opt OUT of the system-wide font override for this Label.  The
230    /// Label will render with `self.font` (passed to `Label::new`)
231    /// regardless of what `font_settings::set_system_font` is pointing
232    /// at.  Useful for font-preview UI — each entry in a font picker
233    /// dropdown needs its OWN face, not the currently selected one.
234    /// Pin this label's LCD setting: `Some(true)` always LCD, `Some(false)`
235    /// always grayscale, `None` (default) defers to the global toggle.
236    pub fn with_lcd(mut self, pref: Option<bool>) -> Self {
237        self.lcd_pref = pref;
238        self
239    }
240
241    pub fn with_ignore_system_font(mut self, ignore: bool) -> Self {
242        self.ignore_system_font = ignore;
243        self
244    }
245
246    pub fn with_margin(mut self, m: Insets) -> Self {
247        self.base.margin = m;
248        self
249    }
250    pub fn with_h_anchor(mut self, h: HAnchor) -> Self {
251        self.base.h_anchor = h;
252        self
253    }
254    pub fn with_v_anchor(mut self, v: VAnchor) -> Self {
255        self.base.v_anchor = v;
256        self
257    }
258    pub fn with_min_size(mut self, s: Size) -> Self {
259        self.base.min_size = s;
260        self
261    }
262    pub fn with_max_size(mut self, s: Size) -> Self {
263        self.base.max_size = s;
264        self
265    }
266
267    // ── getter methods ────────────────────────────────────────────────────────
268
269    /// Return the current label text as a `&str`.
270    pub fn text_str(&self) -> &str {
271        &self.text
272    }
273
274    /// Resolve the font used for THIS layout/paint.  Prefers the system-wide
275    /// font override (set by the System window / `font_settings::set_system_font`)
276    /// so swapping the system font live flows through every widget; falls
277    /// back to the per-instance font otherwise.  Scrollbar-style pattern.
278    fn active_font(&self) -> Arc<Font> {
279        if self.ignore_system_font {
280            Arc::clone(&self.font)
281        } else {
282            crate::font_settings::current_system_font().unwrap_or_else(|| Arc::clone(&self.font))
283        }
284    }
285
286    /// Per-instance font size multiplied by the system-wide
287    /// [`font_settings::current_font_size_scale`].  Label's font-preview
288    /// UI (combo-box items flagged `ignore_system_font`) ALSO ignores
289    /// the scale — a font picker must show every entry at the same
290    /// reference size or comparing faces becomes useless.
291    fn active_font_size(&self) -> f64 {
292        if self.ignore_system_font {
293            self.font_size
294        } else {
295            self.font_size * crate::font_settings::current_font_size_scale()
296        }
297    }
298
299    // ── setter methods (for post-construction mutation) ───────────────────────
300
301    pub fn set_font_size(&mut self, size: f64) {
302        if (self.font_size - size).abs() > 1e-9 {
303            self.font_size = size;
304            self.cache.invalidate();
305        }
306    }
307
308    pub fn set_text(&mut self, text: impl Into<String>) {
309        let text = text.into();
310        if text != self.text {
311            self.text = text;
312            self.cache.invalidate();
313        }
314    }
315    pub fn set_color(&mut self, color: Color) {
316        if self.color != Some(color) {
317            self.color = Some(color);
318            self.cache.invalidate();
319        }
320    }
321    pub fn clear_color(&mut self) {
322        if self.color.is_some() {
323            self.color = None;
324            self.cache.invalidate();
325        }
326    }
327    pub fn set_align(&mut self, align: LabelAlign) {
328        if self.align != align {
329            self.align = align;
330            self.cache.invalidate();
331        }
332    }
333}
334
335impl Widget for Label {
336    fn type_name(&self) -> &'static str {
337        "Label"
338    }
339    fn bounds(&self) -> Rect {
340        self.bounds
341    }
342    fn set_bounds(&mut self, b: Rect) {
343        // Only invalidate on SIZE change — position doesn't affect
344        // cached bitmap (painted at local origin, blitted at parent's
345        // choice of translation).  Framework also invalidates via
346        // `cache.width != w || cache.height != h` in
347        // `paint_subtree_backbuffered`, so this is defence in depth.
348        if self.bounds.width != b.width || self.bounds.height != b.height {
349            self.cache.invalidate();
350        }
351        self.bounds = b;
352    }
353    fn children(&self) -> &[Box<dyn Widget>] {
354        &self.children
355    }
356    fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
357        &mut self.children
358    }
359
360    fn lcd_preference(&self) -> Option<bool> {
361        self.lcd_pref
362    }
363
364    fn set_label_color(&mut self, color: Color) {
365        self.set_color(color);
366    }
367
368    fn set_label_text(&mut self, text: &str) {
369        self.set_text(text);
370    }
371
372    fn backbuffer_cache_mut(&mut self) -> Option<&mut crate::widget::BackbufferCache> {
373        // Cache always when `buffered`.  Mode is chosen by
374        // `backbuffer_mode` below — LCD on → per-channel LcdCoverage
375        // buffer, LCD off → Rgba buffer.  Per-channel alpha means
376        // unpainted pixels stay `alpha = 0` and blit leaves parent
377        // unchanged there, so no scroll-stale cache problem (that
378        // was a dead end from the seed-from-parent approach we ripped
379        // out).
380        if self.buffered {
381            Some(&mut self.cache)
382        } else {
383            None
384        }
385    }
386
387    fn backbuffer_mode(&self) -> crate::widget::BackbufferMode {
388        // Dispatching on the global LCD flag means toggling the
389        // setting automatically rebuilds every cached label in the
390        // right format — `paint_subtree_backbuffered` detects the
391        // mode flip via `cache.lcd_alpha.is_some()` vs the requested
392        // mode and forces a re-raster.
393        if crate::font_settings::lcd_enabled() {
394            crate::widget::BackbufferMode::LcdCoverage
395        } else {
396            crate::widget::BackbufferMode::Rgba
397        }
398    }
399
400    /// Labels are never independently hittable.  This lets their interactive
401    /// parent (e.g., Button) retain full hit-test and focus ownership even
402    /// when the label fills the parent's entire bounds.
403    fn hit_test(&self, _: Point) -> bool {
404        false
405    }
406
407    fn layout(&mut self, available: Size) -> Size {
408        // Resolve the effective font + size ONCE per layout so this call
409        // and the paint that follows agree on glyph metrics even if the
410        // system scale is mid-transition.
411        let font = self.active_font();
412        let size = self.active_font_size();
413        let line_h = size * 1.5;
414
415        // Drop the pre-rasterized bitmap the moment we notice a font or size
416        // swap — unconditionally, before any other branching.  Without this
417        // a buffered Label (the default) keeps blitting glyphs drawn with
418        // the previous typeface / point size until a bounds change or a
419        // text edit happens to invalidate the cache.  DragValue hits this
420        // hardest: its `value_label` often measures the same width for two
421        // different fonts ("14.0" in Arial vs the default is identical
422        // within a pixel), so the size-based invalidation in `set_bounds`
423        // never fires and the stale bitmap lingers until the user hovers
424        // (which triggers some other layout-affecting update).
425        let font_changed = Arc::as_ptr(&font) != self.layout_font_ptr;
426        let size_changed = (self.layout_font_size - size).abs() > 0.01;
427        if font_changed || size_changed {
428            self.cache.invalidate();
429        }
430
431        if self.wrap && available.width > 0.0 {
432            let text_changed = self.layout_text != self.text || size_changed;
433            let width_changed = (self.wrap_at_width - available.width).abs() > 1.0;
434            if text_changed || width_changed || font_changed {
435                self.wrapped_lines = wrap_text(&font, &self.text, size, available.width);
436                self.wrap_at_width = available.width;
437                self.layout_text = self.text.clone();
438                self.layout_font_size = size;
439                self.layout_font_ptr = Arc::as_ptr(&font);
440                // Text changes also need a bitmap rebuild.
441                if text_changed {
442                    self.cache.invalidate();
443                }
444            }
445            let total_h = self.wrapped_lines.len() as f64 * line_h;
446            Size::new(available.width, total_h)
447        } else {
448            // Single-line path: tight bounds matching rendered text width.
449            if self.layout_text != self.text || size_changed || font_changed {
450                let metrics = crate::text::measure_text_metrics(&font, &self.text, size);
451                self.layout_width = metrics.width;
452                self.layout_text = self.text.clone();
453                self.layout_font_size = size;
454                self.layout_font_ptr = Arc::as_ptr(&font);
455            }
456            Size::new(self.layout_width.min(available.width), line_h)
457        }
458    }
459
460    fn paint(&mut self, ctx: &mut dyn DrawCtx) {
461        let w = self.bounds.width;
462        let h = self.bounds.height;
463
464        // Resolve the font to use THIS PAINT: prefer the system-wide override
465        // (set by the System window) so font changes propagate live; fall
466        // back to the per-instance font otherwise.  The same resolution runs
467        // in `layout()` so the two stages agree on metrics.
468        let font = self.active_font();
469        let size = self.active_font_size();
470
471        ctx.set_font(Arc::clone(&font));
472        ctx.set_font_size(size);
473        // If no explicit colour was set, follow the active theme — the
474        // dimmed secondary colour for hint labels, otherwise body text.
475        let color = self.color.unwrap_or_else(|| {
476            let v = ctx.visuals();
477            if self.dim {
478                v.text_dim
479            } else {
480                v.text_color
481            }
482        });
483
484        let is_wrapped = self.wrap && !self.wrapped_lines.is_empty();
485
486        // Clip text rendering to the label's bounds.  `Label::layout`
487        // clamps its returned width to `available.width`, so a long
488        // label inside a narrow parent gets bounds narrower than the
489        // text's natural width.  The backbuffered path (grayscale cache)
490        // implicitly clips at the bitmap's edges; the direct-paint path
491        // (LCD mode) would otherwise draw glyphs past the bounds.  An
492        // explicit clip makes both modes behave identically — text
493        // never escapes the label's rect.
494        ctx.save();
495        ctx.clip_rect(0.0, 0.0, w, h);
496
497        // Labels always paint through `ctx.fill_text` — the backend
498        // decides LCD vs grayscale AA internally based on
499        // `font_settings::lcd_enabled()` and whether it can composite
500        // per-channel coverage.  No backbuffer, no LCD-specific logic
501        // lives here.  Label is just a widget that draws text.
502        ctx.set_fill_color(color);
503        if is_wrapped {
504            let line_h = size * 1.5;
505            let total_h = self.wrapped_lines.len() as f64 * line_h;
506            for (i, line) in self.wrapped_lines.iter().enumerate() {
507                if line.is_empty() {
508                    continue;
509                }
510                if let Some(m) = ctx.measure_text(line) {
511                    let line_center_y = total_h - (i as f64 + 0.5) * line_h;
512                    let ty = line_center_y - line_h * 0.5 + m.centered_baseline_y(line_h);
513                    let tx = match self.align {
514                        LabelAlign::Left => 0.0,
515                        LabelAlign::Center => (w - m.width) * 0.5,
516                        LabelAlign::Right => w - m.width,
517                    };
518                    ctx.fill_text(line, tx, ty);
519                }
520            }
521        } else if let Some(m) = ctx.measure_text(&self.text) {
522            let ty = m.centered_baseline_y(h);
523            let tx = match self.align {
524                LabelAlign::Left => 0.0,
525                LabelAlign::Center => (w - m.width) * 0.5,
526                LabelAlign::Right => w - m.width,
527            };
528            ctx.fill_text(&self.text, tx, ty);
529        }
530
531        ctx.restore();
532    }
533
534    fn margin(&self) -> Insets {
535        self.base.margin
536    }
537    fn widget_base(&self) -> Option<&WidgetBase> {
538        Some(&self.base)
539    }
540    fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
541        Some(&mut self.base)
542    }
543    fn h_anchor(&self) -> HAnchor {
544        self.base.h_anchor
545    }
546    fn v_anchor(&self) -> VAnchor {
547        self.base.v_anchor
548    }
549    fn min_size(&self) -> Size {
550        self.base.min_size
551    }
552    fn max_size(&self) -> Size {
553        self.base.max_size
554    }
555
556    fn measure_min_height(&self, available_w: f64) -> f64 {
557        // Wrapped: count lines at the supplied width.  Non-wrapped:
558        // a single line tall.  Used by ancestor `Window::tight_content_fit`
559        // to compute a content-bound for height.
560        let font = self.active_font();
561        let size = self.active_font_size();
562        let line_h = size * 1.5;
563        if self.wrap && available_w > 0.0 {
564            let lines = wrap_text(&font, &self.text, size, available_w);
565            (lines.len().max(1) as f64) * line_h
566        } else {
567            line_h
568        }
569    }
570
571    fn on_event(&mut self, _: &Event) -> EventResult {
572        EventResult::Ignored
573    }
574
575    fn properties(&self) -> Vec<(&'static str, String)> {
576        vec![
577            ("text", self.text.clone()),
578            ("font_size", format!("{:.1}", self.font_size)),
579            ("align", format!("{:?}", self.align)),
580            (
581                "has_backbuffer",
582                if self.buffered { "true" } else { "false" }.to_string(),
583            ),
584        ]
585    }
586}