From 84580bba7b96cec68fa428bd8d9c97419fdf559b Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 12:26:15 +0000 Subject: [PATCH] fwb2-fix: R108 FontAxis and NamedInstance without the ui feature --- crates/font-manager/src/axes.rs | 228 +----------------- crates/font-manager/src/axes/sliders.rs | 225 +++++++++++++++++ .../src/axes/{ => sliders}/stories.rs | 3 +- crates/font-manager/src/lib.rs | 1 - 4 files changed, 233 insertions(+), 224 deletions(-) create mode 100644 crates/font-manager/src/axes/sliders.rs rename crates/font-manager/src/axes/{ => sliders}/stories.rs (93%) diff --git a/crates/font-manager/src/axes.rs b/crates/font-manager/src/axes.rs index ba1ac4cfc..0e122f0cc 100644 --- a/crates/font-manager/src/axes.rs +++ b/crates/font-manager/src/axes.rs @@ -1,35 +1,11 @@ //! Variable font axes (F14.09): the axes and named instances of a face, and the row of sliders -//! that edits them (Photoshop's Properties › Character axis sliders; FigCraft's axis panel). +//! that edits them (Photoshop's Properties › Character axis sliders; FigCraft's axis panel). The data types +//! are always available; reading a font and the sliders are on `ui`. -#[cfg(feature = "screen-testing")] -mod stories; - -use std::sync::Arc; - -use craft_behavior::{EditEvent, EditEvents, Mixed}; -use craft_ui::contract::{Bind, Entry, NoValue, Outcome, Pattern, Props, Show as _, ShowContainer as _}; -use craft_ui::form::{Field, FieldGroup, SliderField}; -use craft_ui::pickers::Select; -use craft_ui::runtime::env::{Env, Label}; -use egui::{Response, Ui}; - -use crate::picker::{read_bind, write_event}; -use skrifa::raw::types::Tag; -use skrifa::string::StringId; -use skrifa::{FontRef, MetadataProvider as _}; - -craft_ui::words! { - /// The words of the axis sliders, English by default. - #[non_exhaustive] - pub struct AxisSlidersWords { - weight: "axisSliders.weight" = "Weight"; - width: "axisSliders.width" = "Width"; - slant: "axisSliders.slant" = "Slant"; - italic: "axisSliders.italic" = "Italic"; - style: "axisSliders.style" = "Style"; - custom: "axisSliders.custom" = "Custom"; - } -} +#[cfg(feature = "ui")] +mod sliders; +#[cfg(feature = "ui")] +pub use sliders::*; /// One variation axis of a face. #[derive(Clone, Debug, PartialEq)] @@ -80,35 +56,6 @@ impl NamedInstance { } } -/// The axes of the face at `index` in `data`, in font order; empty for a static font. -pub fn read_axes(data: &[u8], index: u32) -> Vec { - let Ok(font) = FontRef::from_index(data, index) else { return Vec::new() }; - font.axes() - .iter() - .map(|axis| { - let label = name(&font, axis.name_id()).unwrap_or_default(); - FontAxis::new(axis.tag().to_be_bytes(), label, axis.min_value(), axis.default_value(), axis.max_value()).hidden(axis.is_hidden()) - }) - .collect() -} - -/// The named instances of the face at `index` in `data`, in font order. -pub fn read_named_instances(data: &[u8], index: u32) -> Vec { - let Ok(font) = FontRef::from_index(data, index) else { return Vec::new() }; - let tags: Vec<[u8; 4]> = font.axes().iter().map(|axis| axis.tag().to_be_bytes()).collect(); - font.named_instances() - .iter() - .map(|instance| { - let label = name(&font, instance.subfamily_name_id()).unwrap_or_default(); - NamedInstance::new(label, tags.iter().copied().zip(instance.user_coords()).collect()) - }) - .collect() -} - -fn name(font: &FontRef, id: StringId) -> Option { - font.localized_strings(id).english_or_first().map(|s| s.to_string()).filter(|s| !s.is_empty()) -} - /// How close two coordinates are to count as equal: well under the 0.01 the sliders step by. const SAME: f32 = 0.005; @@ -121,166 +68,3 @@ pub fn matching_instance<'a>(instances: &'a [NamedInstance], values: &[([u8; 4], && instance.coordinates.iter().all(|(tag, at)| values.iter().any(|(t, v)| t == tag && (v - at).abs() <= SAME)) }) } - -/// The label of an axis: its name when the font gives one, else the registered tag's -/// (`Weight`, `Width`, `Slant`, `Italic`), else the tag itself. -pub fn axis_label(axis: &FontAxis) -> String { - label_in(axis, &AxisSlidersWords::default()) -} - -fn label_in(axis: &FontAxis, labels: &AxisSlidersWords) -> String { - if !axis.name.is_empty() { - return axis.name.clone(); - } - match &axis.tag { - b"wght" => labels.weight.as_str().to_owned(), - b"wdth" => labels.width.as_str().to_owned(), - b"slnt" => labels.slant.as_str().to_owned(), - b"ital" => labels.italic.as_str().to_owned(), - tag => Tag::new(tag).to_string(), - } -} - -/// The value an axis shows: the stored one, else what the style draws, else the axis default, -/// within the axis range. -fn value_of(values: &[([u8; 4], f32)], axis: &FontAxis, drawn: Drawn) -> f32 { - let default = if axis.default.is_finite() { axis.default } else { axis.min }; - let stored = values.iter().find(|(t, _)| *t == axis.tag).map(|(_, v)| *v).filter(|v| v.is_finite()); - let styled = match &axis.tag { - b"wght" => drawn.weight.filter(|w| w.is_finite()), - b"ital" if drawn.weight.is_some() && drawn.italic => Some(axis.max).filter(|m| m.is_finite()), - _ => None, - }; - let (lo, hi) = if axis.min <= axis.max { (axis.min, axis.max) } else { (axis.max, axis.min) }; - stored.or(styled).unwrap_or(default).clamp(lo, hi) -} - -fn set_value(values: &mut AxisValues, tag: [u8; 4], value: f32) { - match values.iter_mut().find(|(t, _)| *t == tag) { - Some(slot) => slot.1 = value, - None => values.push((tag, value)), - } -} - -/// The axes the sliders show: not hidden, not `opsz`, with a finite, non-empty range. -fn shown_axes(axes: &[FontAxis]) -> impl Iterator { - axes.iter().filter(|a| !a.hidden && a.tag != *b"opsz" && a.min.is_finite() && a.max.is_finite() && a.min < a.max) -} - -/// What changed in the sliders this pass. -#[derive(Debug)] -#[non_exhaustive] -pub struct AxisSlidersOutput { - /// The sliders' response. - pub response: Response, - /// The edits of this pass on the values, in order: a slider drag is one gesture (begin, updates, commit); a key - /// step, a typed value and an instance pick are one set. The binding has already taken the values the events - /// carry. - pub edit: EditEvents, - /// A named instance was picked from the list. - pub instance: Option, -} - -impl Outcome for AxisSlidersOutput { - type Value = NoValue; - - fn response(&self) -> &Response { - &self.response - } -} - -/// The values of the axes: (tag, value) for every axis that has one. -pub type AxisValues = Vec<([u8; 4], f32)>; - -/// The style the sliders' unset axes show. -#[derive(Clone, Copy, Default)] -struct Drawn { - weight: Option, - italic: bool, -} - -/// One slider field per visible axis (label, slider, number field with the axis's range), plus a `Select` of named -/// instances above them when the face has any. Hidden axes, `opsz` (the text layout sets the optical size from the -/// font size) and axes with an empty or non-finite range are left out. Values are the current coordinates; an axis -/// without one shows what the layout draws (its style's weight and italic; else its default). Axes whose range is 10 wide -/// or more step by whole units, the others by hundredths. A drag is one edit; an instance sets every axis in one -/// edit. -#[must_use = "show the sliders with `show`"] -#[derive(Pattern, Props)] -pub struct AxisSliders<'a> { - label: Label, - axes: &'a [FontAxis], - values: Bind<'a, AxisValues>, - /// The named instances of the face, listed above the sliders. - #[prop] - instances: &'a [NamedInstance], - /// The style's weight, which an unset `wght` axis shows (an unset axis shows its default without it). - #[prop(quantity = "font weight, the `wght` coordinate")] - weight: Option, - /// Whether the style is italic: an unset `ital` axis then shows its maximum, once a weight is given. - #[prop] - italic: bool, -} - -impl<'a> AxisSliders<'a> { - /// Sliders named `label` over the `axes` of a face, editing the coordinates `values`. - pub fn new(label: impl Into