skrifa/
setting.rs

1//! Definitions for specifying variations and typographic features.
2
3use super::Tag;
4use core::str::FromStr;
5
6/// Setting defined by a selector tag and an associated value.
7///
8/// This type is a generic container for properties that can be activated
9/// or defined by a `(Tag, T)` pair where the tag selects the target
10/// setting and the generic value of type `T` specifies the value for that
11/// setting.
12///
13/// ## Usage
14/// Current usage is for specifying variation axis settings (similar to the
15/// CSS property [font-variation-settings](https://developer.mozilla.org/en-US/docs/Web/CSS/font-variation-settings)).
16/// See [`VariationSetting`].
17///
18/// In the future, this will likely also be used for specifying feature settings
19/// (analogous to the CSS property [font-feature-settings](https://developer.mozilla.org/en-US/docs/Web/CSS/font-feature-settings))
20/// for selecting OpenType [features](https://learn.microsoft.com/en-us/typography/opentype/spec/featuretags).
21#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
22pub struct Setting<T> {
23    /// Tag that specifies the target setting.
24    pub selector: Tag,
25    /// The desired value for the setting.
26    pub value: T,
27}
28
29impl<T> Setting<T> {
30    /// Creates a new setting from the given selector tag and its associated
31    /// value.
32    pub fn new(selector: Tag, value: T) -> Self {
33        Self { selector, value }
34    }
35}
36
37// This is provided so that &[VariationSetting] can be passed to the
38// variation_settings() method of ScalerBuilder.
39impl<T: Copy> From<&'_ Setting<T>> for Setting<T> {
40    fn from(value: &'_ Setting<T>) -> Self {
41        *value
42    }
43}
44
45impl<T> From<(Tag, T)> for Setting<T> {
46    fn from(s: (Tag, T)) -> Self {
47        Self {
48            selector: s.0,
49            value: s.1,
50        }
51    }
52}
53
54impl<T: Copy> From<&(Tag, T)> for Setting<T> {
55    fn from(s: &(Tag, T)) -> Self {
56        Self {
57            selector: s.0,
58            value: s.1,
59        }
60    }
61}
62
63impl<T> From<(&str, T)> for Setting<T> {
64    fn from(s: (&str, T)) -> Self {
65        Self {
66            selector: Tag::from_str(s.0).unwrap_or_default(),
67            value: s.1,
68        }
69    }
70}
71
72impl<T: Copy> From<&(&str, T)> for Setting<T> {
73    fn from(s: &(&str, T)) -> Self {
74        Self {
75            selector: Tag::from_str(s.0).unwrap_or_default(),
76            value: s.1,
77        }
78    }
79}
80
81/// Type for specifying a variation axis setting in user coordinates.
82///
83/// The `selector` field should contain a tag that corresponds to a
84/// variation axis while the `value` field specifies the desired position
85/// on the axis in user coordinates (i.e. within the range defined by
86/// the minimum and maximum values of the axis).
87///
88/// # Example
89/// ```
90/// use skrifa::{Tag, setting::VariationSetting};
91///
92/// // For convenience, a conversion from (&str, f32) is provided.
93/// let slightly_bolder: VariationSetting = ("wght", 720.0).into();
94///
95/// assert_eq!(slightly_bolder, VariationSetting::new(Tag::new(b"wght"), 720.0));
96/// ```
97pub type VariationSetting = Setting<f32>;