Skip to main content

mln_style/property/
set.rs

1//! A property set's values at each stage: as the style sets them
2//! (`Transitionable`), transitioning from earlier values (`Unevaluated`), and
3//! evaluated for a frame (`PossiblyEvaluated`, an [`Evaluated`] per
4//! property).
5//!
6//! `properties!` declares a set: one named field per
7//! property, with its type, its default and how it evaluates.
8
9use std::fmt::Debug;
10use std::time::Duration;
11
12use crate::expression::{Dependency, global_state_refs_intersect};
13use crate::interpolate::Interpolate;
14use crate::property::evaluated::{Evaluated, Evaluation};
15use crate::property::evaluation::PropertyEvaluationParameters;
16use crate::property::transition::{GlobalStateChange, TransitionOptions, TransitionParameters};
17use crate::property::value::StyleValue;
18use crate::time::Instant;
19
20/// A value, and the values it transitions from.
21#[derive(Clone, Debug, Default)]
22pub struct Transitioning<V> {
23    /// The value transitioned from, until the transition ends: evaluating
24    /// after that drops it.
25    prior: Option<Box<Transitioning<V>>>,
26    /// When the transition from `prior` was set up; `None` without a clock,
27    /// where it has already ended.
28    start: Option<Instant>,
29    /// How long after `start` the transition begins.
30    delay: Duration,
31    /// How long it lasts once it has begun.
32    duration: Duration,
33    value: V,
34}
35
36/// Where a transition stands at a time.
37enum Stage {
38    /// Its delay hasn't passed yet.
39    Delayed,
40    /// It is this share of the way from the prior value, in [0, 1).
41    Running(f64),
42    Ended,
43}
44
45impl<V: StyleValue> Transitioning<V> {
46    #[must_use]
47    pub fn new(value: V) -> Self {
48        Transitioning { prior: None, start: None, delay: Duration::ZERO, duration: Duration::ZERO, value }
49    }
50
51    /// A transition from `prior` that starts at `now`, or one that has
52    /// already ended for `None`.
53    pub(crate) fn with_prior(
54        value: V,
55        prior: Transitioning<V>,
56        transition: &TransitionOptions,
57        now: Option<Instant>,
58    ) -> Self {
59        Transitioning {
60            prior: if transition.is_defined() { Some(Box::new(prior)) } else { None },
61            start: now,
62            delay: transition.delay.unwrap_or_default(),
63            duration: transition.duration.unwrap_or_default(),
64            value,
65        }
66    }
67
68    fn stage(&self, now: Option<Instant>) -> Stage {
69        let (Some(now), Some(start)) = (now, self.start) else {
70            return Stage::Ended;
71        };
72        let Some(running) = now.saturating_duration_since(start).checked_sub(self.delay) else {
73            return Stage::Delayed;
74        };
75        if running < self.duration { Stage::Running(running.div_duration_f64(self.duration)) } else { Stage::Ended }
76    }
77
78    /// The value at `parameters.now`, as `evaluation` says, with `default`
79    /// for an undefined value. A transition that has ended, or that ends in
80    /// a data-driven value, drops the value it came from.
81    ///
82    /// Not inlined: the property sets call these once
83    /// per property, so inlining copies them into every call.
84    #[inline(never)]
85    pub fn evaluate(
86        &mut self,
87        evaluation: Evaluation<V::Type>,
88        parameters: &PropertyEvaluationParameters,
89        default: V::Type,
90    ) -> Evaluated<V::Type>
91    where
92        V::Type: Interpolate,
93    {
94        let final_value = self.value.evaluate(evaluation, parameters, default.clone());
95        let stage = self.stage(parameters.now);
96        let Some(prior) = self.prior.as_deref_mut() else {
97            return final_value;
98        };
99        if matches!(stage, Stage::Ended) || self.value.is_data_driven() {
100            // Transitions to data-driven properties are not supported: snap
101            // to the data-driven value, so that layout sees the expression
102            // and populates vertex buffers from it.
103            self.prior = None;
104            return final_value;
105        }
106        let prior_value = prior.evaluate(evaluation, parameters, default);
107        match stage {
108            Stage::Running(t) => prior_value.interpolate(&final_value, ease_cubic_in_out(t)),
109            Stage::Delayed | Stage::Ended => prior_value,
110        }
111    }
112
113    pub fn has_transition(&self) -> bool {
114        self.prior.is_some()
115    }
116
117    pub fn is_undefined(&self) -> bool {
118        self.value.is_undefined()
119    }
120
121    /// Keeps every step that reads a key changed by `change` on the state from
122    /// before it, so that a new transition starts from what this one shows.
123    pub(crate) fn retain_prior_global_state(&mut self, change: &GlobalStateChange) {
124        self.value.retain_prior_global_state(change);
125        if let Some(prior) = &mut self.prior {
126            prior.retain_prior_global_state(change);
127        }
128    }
129
130    pub fn value(&self) -> &V {
131        &self.value
132    }
133
134    /// Evaluates the value if it may have changed since `previous`, or
135    /// copies `previous`.
136    #[inline(never)]
137    pub fn evaluate_with_previous(
138        &mut self,
139        evaluation: Evaluation<V::Type>,
140        parameters: &PropertyEvaluationParameters,
141        default: V::Type,
142        previous: &Evaluated<V::Type>,
143    ) -> Evaluated<V::Type>
144    where
145        V::Type: Interpolate,
146    {
147        let dependencies = self.value.dependencies();
148        let need_evaluate = parameters.layer_changed
149            || parameters.has_crossfade
150            || self.has_transition()
151            || (parameters.zoom_changed && dependencies.intersects(Dependency::ZOOM))
152            || (parameters.global_state_changed
153                && dependencies.intersects(Dependency::GLOBAL_STATE)
154                && global_state_refs_intersect(
155                    self.value.global_state_refs(),
156                    parameters.changed_global_state_keys.as_deref(),
157                ));
158        if need_evaluate { self.evaluate(evaluation, parameters, default) } else { previous.clone() }
159    }
160}
161
162/// A value as the style sets it, and how it transitions.
163#[derive(Clone, Debug, Default, PartialEq, mln_wire::Wire)]
164pub struct Transitionable<V> {
165    pub value: V,
166    pub(crate) options: TransitionOptions,
167}
168
169impl<V: StyleValue> Transitionable<V> {
170    /// A value that transitions with the style's default transition.
171    #[must_use]
172    pub fn new(value: V) -> Self {
173        Transitionable { value, options: TransitionOptions::default() }
174    }
175
176    /// The value, in transition from `prior` as `params` and the value's
177    /// own options say.
178    #[inline(never)]
179    pub fn transition(&self, params: &TransitionParameters, mut prior: Transitioning<V>) -> Transitioning<V> {
180        if let Some(change) = &params.global_state_change {
181            prior.retain_prior_global_state(change);
182            if change.state_only && !self.value.transitions_on_global_state_change(Some(&change.changed_keys)) {
183                return prior;
184            }
185        }
186        Transitioning::with_prior(
187            self.value.clone(),
188            prior,
189            &self.options.reverse_merge(&params.transition),
190            params.now,
191        )
192    }
193}
194
195/// The style value type of a property kind.
196#[doc(hidden)]
197#[macro_export]
198macro_rules! __property_value {
199    (color_ramp, $t:ty) => {
200        $crate::property::color_ramp::ColorRampPropertyValue
201    };
202    ($kind:ident, $t:ty) => {
203        $crate::property::value::PropertyValue<$t>
204    };
205}
206
207/// How a property kind evaluates.
208#[doc(hidden)]
209#[macro_export]
210macro_rules! __property_evaluation {
211    (integer_zoom) => {
212        $crate::property::evaluated::Evaluated::integer_zoom
213    };
214    (cross_faded) => {
215        $crate::property::evaluated::Evaluated::cross_faded
216    };
217    (cross_faded_data_driven) => {
218        $crate::property::evaluated::Evaluated::cross_faded_per_feature
219    };
220    (cross_faded_or_data_driven) => {
221        $crate::property::evaluated::Evaluated::cross_faded_or_per_feature
222    };
223    ($kind:ident) => {
224        $crate::property::evaluated::Evaluated::plain
225    };
226}
227
228/// Whether a property kind may differ by feature.
229#[doc(hidden)]
230#[macro_export]
231macro_rules! __property_data_driven {
232    (data_driven) => {
233        true
234    };
235    (integer_zoom) => {
236        true
237    };
238    (cross_faded_data_driven) => {
239        true
240    };
241    (cross_faded_or_data_driven) => {
242        true
243    };
244    ($kind:ident) => {
245        false
246    };
247}
248
249/// Declares a property set as `mod $module`, one named field per property,
250/// followed by the paths its types and defaults need:
251///
252/// ```ignore
253/// properties!(
254///     pub mod FillPaintProperties: paint {
255///         fill_antialias: constant bool = true,
256///         fill_color: data_driven Color = Color::black(),
257///     }
258///     use crate::color::Color;
259/// );
260/// ```
261///
262/// A property's kind is its style-spec property type: `constant`,
263/// `data_driven`, `cross_faded`, `cross_faded_data_driven` or `color_ramp`,
264/// or `integer_zoom` for a data-driven property that evaluates at the integer
265/// zoom below. `cross_faded_data_driven` is the patterns' kind;
266/// `cross_faded_or_data_driven`, `line-dasharray`'s, cross-fades a value that
267/// doesn't read the feature as `cross_faded` does. A `paint` set transitions; a `layout` set doesn't.
268///
269/// A paint set's `derived { field: kind T = default => source, }` values
270/// aren't style properties: each takes `source`'s value, and transitions with
271/// the style's default transition.
272#[macro_export]
273macro_rules! properties {
274    ($(#[$meta:meta])* $vis:vis mod $module:ident: paint {
275        $($field:ident: $kind:ident $t:ty = $default:expr),+ $(,)?
276    } $(derived {
277        $($dfield:ident: $dkind:ident $dt:ty = $ddefault:expr => $source:ident),+ $(,)?
278    })? $(use $($import:path),+;)?) => {
279        $(#[$meta])*
280        $vis mod $module {
281            $($(use $import;)+)?
282            use $crate::property::value::StyleValue as _;
283
284            /// The spec's default of each property.
285            pub struct Defaults;
286
287            impl Defaults {
288                $(#[must_use]
289                pub fn $field() -> $t {
290                    $default
291                })+
292                $($(#[must_use]
293                pub fn $dfield() -> $dt {
294                    $ddefault
295                })+)?
296            }
297
298            #[derive(Clone, Debug, Default, PartialEq, $crate::__private::mln_wire::Wire)]
299            #[wire(crate = $crate::__private::mln_wire, default)]
300            pub struct Transitionable {
301                $(pub $field: $crate::property::set::Transitionable<$crate::__property_value!($kind, $t)>,)+
302            }
303
304            #[derive(Clone, Debug, Default)]
305            pub struct Unevaluated {
306                $(pub $field: $crate::property::set::Transitioning<$crate::__property_value!($kind, $t)>,)+
307                $($(pub $dfield: $crate::property::set::Transitioning<$crate::__property_value!($dkind, $dt)>,)+)?
308            }
309
310            #[derive(Clone, Debug, Default, $crate::__private::mln_wire::Wire)]
311            #[wire(crate = $crate::__private::mln_wire, default)]
312            pub struct PossiblyEvaluated {
313                $(pub $field: $crate::property::evaluated::Evaluated<$t>,)+
314                $($(pub $dfield: $crate::property::evaluated::Evaluated<$dt>,)+)?
315            }
316
317            impl Transitionable {
318                pub fn transitioned(
319                    &self,
320                    parameters: &$crate::property::transition::TransitionParameters,
321                    prior: Unevaluated,
322                ) -> Unevaluated {
323                    Unevaluated {
324                        $($field: self.$field.transition(parameters, prior.$field),)+
325                        $($($dfield: $crate::property::set::Transitionable::new(self.$source.value.clone())
326                            .transition(parameters, prior.$dfield),)+)?
327                    }
328                }
329
330                pub fn untransitioned(&self) -> Unevaluated {
331                    Unevaluated {
332                        $($field: $crate::property::set::Transitioning::new(self.$field.value.clone()),)+
333                        $($($dfield: $crate::property::set::Transitioning::new(self.$source.value.clone()),)+)?
334                    }
335                }
336
337                pub fn has_data_driven_property_difference(&self, other: &Transitionable) -> bool {
338                    false $(|| self.$field.value.has_data_driven_property_difference(&other.$field.value))+
339                }
340
341                pub fn dependencies(&self) -> $crate::expression::Dependency {
342                    let mut result = $crate::expression::Dependency::NONE;
343                    $(result |= self.$field.value.dependencies();)+
344                    result
345                }
346            }
347
348            impl $crate::layer::PaintSet for Transitionable {
349                type Evaluated = PossiblyEvaluated;
350                type Unevaluated = Unevaluated;
351
352                fn untransitioned(&self) -> Unevaluated {
353                    Transitionable::untransitioned(self)
354                }
355
356                fn transitioned(
357                    &self,
358                    parameters: &$crate::property::transition::TransitionParameters,
359                    prior: Unevaluated,
360                ) -> Unevaluated {
361                    Transitionable::transitioned(self, parameters, prior)
362                }
363
364                fn evaluate_with_previous(
365                    unevaluated: &mut Unevaluated,
366                    parameters: &$crate::property::evaluation::PropertyEvaluationParameters,
367                    previous: &PossiblyEvaluated,
368                ) -> PossiblyEvaluated {
369                    unevaluated.evaluate_with_previous(parameters, previous)
370                }
371
372                fn has_transition(unevaluated: &Unevaluated) -> bool {
373                    unevaluated.has_transition()
374                }
375
376                fn dependencies(unevaluated: &Unevaluated) -> $crate::expression::Dependency {
377                    unevaluated.dependencies()
378                }
379
380                fn evaluated_dependencies(evaluated: &PossiblyEvaluated) -> $crate::expression::Dependency {
381                    evaluated.dependencies()
382                }
383
384                fn constants_mask(evaluated: &PossiblyEvaluated) -> u64 {
385                    evaluated.constants_mask()
386                }
387
388                fn collect_evaluated_global_state_refs(
389                    evaluated: &PossiblyEvaluated,
390                    refs: &mut $crate::collections::VecSet<String>,
391                ) {
392                    evaluated.collect_global_state_refs(refs);
393                }
394            }
395
396            impl Unevaluated {
397                pub fn has_transition(&self) -> bool {
398                    false $(|| self.$field.has_transition())+ $($(|| self.$dfield.has_transition())+)?
399                }
400
401                pub fn evaluate(
402                    &mut self,
403                    parameters: &$crate::property::evaluation::PropertyEvaluationParameters,
404                ) -> PossiblyEvaluated {
405                    PossiblyEvaluated {
406                        $($field: self.$field.evaluate(
407                            $crate::__property_evaluation!($kind),
408                            parameters,
409                            Defaults::$field(),
410                        ),)+
411                        $($($dfield: self.$dfield.evaluate(
412                            $crate::__property_evaluation!($dkind),
413                            parameters,
414                            Defaults::$dfield(),
415                        ),)+)?
416                    }
417                }
418
419                /// Evaluates each property, or copies its value from
420                /// `previous` where it can't have changed.
421                pub fn evaluate_with_previous(
422                    &mut self,
423                    parameters: &$crate::property::evaluation::PropertyEvaluationParameters,
424                    previous: &PossiblyEvaluated,
425                ) -> PossiblyEvaluated {
426                    PossiblyEvaluated {
427                        $($field: self.$field.evaluate_with_previous(
428                            $crate::__property_evaluation!($kind),
429                            parameters,
430                            Defaults::$field(),
431                            &previous.$field,
432                        ),)+
433                        $($($dfield: self.$dfield.evaluate_with_previous(
434                            $crate::__property_evaluation!($dkind),
435                            parameters,
436                            Defaults::$dfield(),
437                            &previous.$dfield,
438                        ),)+)?
439                    }
440                }
441
442                /// The combined dependencies of the expressions.
443                pub fn dependencies(&self) -> $crate::expression::Dependency {
444                    let mut result = $crate::expression::Dependency::NONE;
445                    $(result |= self.$field.value().dependencies();)+
446                    $($(result |= self.$dfield.value().dependencies();)+)?
447                    result
448                }
449
450                pub fn collect_global_state_refs(&self, refs: &mut $crate::collections::VecSet<String>) {
451                    $(if let Some(property_refs) = self.$field.value().global_state_refs() {
452                        refs.extend(property_refs.iter().cloned());
453                    })+
454                    $($(if let Some(property_refs) = self.$dfield.value().global_state_refs() {
455                        refs.extend(property_refs.iter().cloned());
456                    })+)?
457                }
458            }
459
460            $crate::__possibly_evaluated_methods!($($field: $kind),+ $($(, $dfield: $dkind)+)?);
461        }
462    };
463    ($(#[$meta:meta])* $vis:vis mod $module:ident: layout {
464        $($field:ident: $kind:ident $t:ty = $default:expr),+ $(,)?
465    } $(use $($import:path),+;)?) => {
466        $(#[$meta])*
467        $vis mod $module {
468            $($(use $import;)+)?
469            use $crate::property::value::StyleValue as _;
470
471            /// The spec's default of each property.
472            pub struct Defaults;
473
474            impl Defaults {
475                $(#[must_use]
476                pub fn $field() -> $t {
477                    $default
478                })+
479            }
480
481            #[derive(Clone, Debug, Default, PartialEq, $crate::__private::mln_wire::Wire)]
482            #[wire(crate = $crate::__private::mln_wire, default)]
483            pub struct Unevaluated {
484                $(pub $field: $crate::__property_value!($kind, $t),)+
485            }
486
487            #[derive(Clone, Debug, Default, $crate::__private::mln_wire::Wire)]
488            #[wire(crate = $crate::__private::mln_wire, default)]
489            pub struct PossiblyEvaluated {
490                $(pub $field: $crate::property::evaluated::Evaluated<$t>,)+
491            }
492
493            /// The value of each property for one feature.
494            #[derive(Clone, Debug, Default)]
495            pub struct Values {
496                $(pub $field: $t,)+
497            }
498
499            impl Unevaluated {
500                pub fn evaluate(
501                    &self,
502                    parameters: &$crate::property::evaluation::PropertyEvaluationParameters,
503                ) -> PossiblyEvaluated {
504                    PossiblyEvaluated {
505                        $($field: self.$field.evaluate(
506                            $crate::__property_evaluation!($kind),
507                            parameters,
508                            Defaults::$field(),
509                        ),)+
510                    }
511                }
512
513                /// Bit i is set when the i-th data-driven property is a
514                /// constant.
515                pub fn constants_mask(&self) -> u64 {
516                    let mut result = 0u64;
517                    let mut _index = 0u32;
518                    $(if $crate::__property_data_driven!($kind) {
519                        if self.$field.is_constant() {
520                            result |= 1u64 << _index;
521                        }
522                        _index += 1;
523                    })+
524                    result
525                }
526
527                /// The combined dependencies of the expressions.
528                pub fn dependencies(&self) -> $crate::expression::Dependency {
529                    let mut result = $crate::expression::Dependency::NONE;
530                    $(result |= self.$field.dependencies();)+
531                    result
532                }
533
534                pub fn collect_global_state_refs(&self, refs: &mut $crate::collections::VecSet<String>) {
535                    $(if let Some(property_refs) = self.$field.global_state_refs() {
536                        refs.extend(property_refs.iter().cloned());
537                    })+
538                }
539            }
540
541            impl $crate::layer::LayoutSet for Unevaluated {
542                fn dependencies(&self) -> $crate::expression::Dependency {
543                    self.dependencies()
544                }
545
546                fn collect_global_state_refs(&self, refs: &mut $crate::collections::VecSet<String>) {
547                    Unevaluated::collect_global_state_refs(self, refs);
548                }
549            }
550
551            impl PossiblyEvaluated {
552                /// The value of each property for the feature in `context`.
553                pub fn evaluate_all(&self, context: &$crate::expression::EvaluationContext<'_>) -> Values {
554                    Values {
555                        $($field: self.$field.evaluate(context),)+
556                    }
557                }
558            }
559
560            $crate::__possibly_evaluated_methods!($($field: $kind),+);
561        }
562    };
563}
564
565/// Declares a layer type's property sets, as [`properties!`] does, and its
566/// property table: `PROPERTIES`, with the paint properties and then the
567/// layout properties in the order the sets list them, and `visit`, which
568/// calls a visitor with the field of a slot of the table. A type's
569/// [`StyleLayerType`](crate::layer::StyleLayerType) impl names both:
570///
571/// ```ignore
572/// mln_style::layer_properties!(
573///     /// The paint properties of an `org.example.dot` layer.
574///     pub mod paint: paint {
575///         dot_radius: data_driven f32 = 5.0,
576///         dot_color: data_driven Color = Color::black(),
577///     }
578///     use mln_style::color::Color;
579/// );
580///
581/// impl StyleLayerType for Dot {
582///     const PROPERTIES: &'static PropertyTable = &PROPERTIES;
583///     type PaintProperties = paint::Transitionable;
584///     type LayoutProperties = NoProperties;
585///     fn visit_property<V: PropertyVisitor<LayerStyle<Self>>>(slot: usize, visitor: V) -> Option<V::Output> {
586///         visit(slot, visitor)
587///     }
588///     // ...
589/// }
590/// ```
591///
592/// The paint set comes first and the layout set second; a type may have
593/// either alone. A property's style-spec name is its field's, with each `_`
594/// a `-`, and a data-driven kind makes it data-driven in the table. The
595/// sets can't have `derived` values.
596#[macro_export]
597macro_rules! layer_properties {
598    ($(#[$pmeta:meta])* $pvis:vis mod $paint:ident: paint {
599        $($pfield:ident: $pkind:ident $pt:ty = $pdefault:expr),+ $(,)?
600    } $(#[$lmeta:meta])* $lvis:vis mod $layout:ident: layout {
601        $($lfield:ident: $lkind:ident $lt:ty = $ldefault:expr),+ $(,)?
602    } $(use $($import:path),+;)?) => {
603        // Each set imports the paths both sets need.
604        $crate::properties!(
605            $(#[$pmeta])*
606            #[allow(unused_imports, reason = "the imports are those of both sets")]
607            $pvis mod $paint: paint { $($pfield: $pkind $pt = $pdefault,)+ } $(use $($import),+;)?
608        );
609        $crate::properties!(
610            $(#[$lmeta])*
611            #[allow(unused_imports, reason = "the imports are those of both sets")]
612            $lvis mod $layout: layout { $($lfield: $lkind $lt = $ldefault,)+ } $(use $($import),+;)?
613        );
614        $crate::__property_table!(
615            paint $paint { $($pfield $pkind)+ }
616            layout $layout { $($lfield $lkind)+ }
617        );
618    };
619    ($(#[$pmeta:meta])* $pvis:vis mod $paint:ident: paint {
620        $($pfield:ident: $pkind:ident $pt:ty = $pdefault:expr),+ $(,)?
621    } $(use $($import:path),+;)?) => {
622        $crate::properties!(
623            $(#[$pmeta])* $pvis mod $paint: paint { $($pfield: $pkind $pt = $pdefault,)+ } $(use $($import),+;)?
624        );
625        $crate::__property_table!(paint $paint { $($pfield $pkind)+ } layout);
626    };
627    ($(#[$lmeta:meta])* $lvis:vis mod $layout:ident: layout {
628        $($lfield:ident: $lkind:ident $lt:ty = $ldefault:expr),+ $(,)?
629    } $(use $($import:path),+;)?) => {
630        $crate::properties!(
631            $(#[$lmeta])* $lvis mod $layout: layout { $($lfield: $lkind $lt = $ldefault,)+ } $(use $($import),+;)?
632        );
633        $crate::__property_table!(paint layout $layout { $($lfield $lkind)+ });
634    };
635}
636
637/// `layer_properties!`'s `PROPERTIES` and `visit`, for the paint set
638/// `$paint` and the layout set `$layout`, either of which may be missing.
639#[doc(hidden)]
640#[macro_export]
641macro_rules! __property_table {
642    (
643        paint $($paint:ident { $($pfield:ident $pkind:ident)+ })?
644        layout $($layout:ident { $($lfield:ident $lkind:ident)+ })?
645    ) => {
646        /// The type's properties, by slot.
647        pub(crate) static PROPERTIES: $crate::property::table::PropertyTable = {
648            const COUNT: usize =
649                <[&str]>::len(&[$($(::core::stringify!($pfield),)+)? $($(::core::stringify!($lfield),)+)?]);
650            static INFO: [$crate::property::table::PropertyInfo; COUNT] = [
651                $($($crate::__property_info!(paint $pfield $pkind),)+)?
652                $($($crate::__property_info!(layout $lfield $lkind),)+)?
653            ];
654            static BY_NAME: [u8; COUNT] = $crate::__private::slots_by_name(&INFO);
655            $crate::property::table::PropertyTable::new(&INFO, &BY_NAME)
656        };
657
658        /// Calls `visitor` with the field of the property in `slot` of
659        /// `PROPERTIES`.
660        pub(crate) fn visit<T, V>(slot: usize, visitor: V) -> ::core::option::Option<V::Output>
661        where
662            T: $crate::layer::StyleLayerType<
663                $(PaintProperties = $paint::Transitionable,)?
664                $(LayoutProperties = $layout::Unevaluated,)?
665            >,
666            V: $crate::property::table::PropertyVisitor<$crate::layer::LayerStyle<T>>,
667        {
668            let mut _next = 0;
669            $($(
670                if slot == _next {
671                    return ::core::option::Option::Some(visitor.paint(|s| &s.paint.$pfield, |s| &mut s.paint.$pfield));
672                }
673                _next += 1;
674            )+)?
675            $($(
676                if slot == _next {
677                    return ::core::option::Option::Some(visitor.layout(|s| &s.layout.$lfield, |s| &mut s.layout.$lfield));
678                }
679                _next += 1;
680            )+)?
681            ::core::option::Option::None
682        }
683    };
684}
685
686/// The table entry of the property in the field `$field` of a `$scope`
687/// set, of the kind `$kind`.
688#[doc(hidden)]
689#[macro_export]
690macro_rules! __property_info {
691    ($scope:ident $field:ident $kind:ident) => {{
692        const FIELD: &str = ::core::stringify!($field);
693        const NAME: [u8; FIELD.len()] = $crate::__private::property_name(FIELD);
694        const INFO: $crate::property::table::PropertyInfo =
695            // Replacing ASCII bytes keeps the field's UTF-8 valid.
696            $crate::property::table::PropertyInfo::$scope(match ::core::str::from_utf8(&NAME) {
697                ::core::result::Result::Ok(name) => name,
698                ::core::result::Result::Err(_) => FIELD,
699            });
700        $crate::__property_info!(@flags INFO $kind)
701    }};
702    (@flags $info:ident data_driven) => {
703        $info.data_driven()
704    };
705    (@flags $info:ident integer_zoom) => {
706        $info.data_driven()
707    };
708    (@flags $info:ident cross_faded_data_driven) => {
709        $info.data_driven().cross_faded()
710    };
711    (@flags $info:ident cross_faded_or_data_driven) => {
712        $info.data_driven().cross_faded()
713    };
714    (@flags $info:ident $kind:ident) => {
715        $info
716    };
717}
718
719/// The methods of a set's `PossiblyEvaluated`.
720#[doc(hidden)]
721#[macro_export]
722macro_rules! __possibly_evaluated_methods {
723    ($($field:ident: $kind:ident),+) => {
724        impl PossiblyEvaluated {
725            /// The dependencies of the values that still differ by feature.
726            pub fn dependencies(&self) -> $crate::expression::Dependency {
727                let mut result = $crate::expression::Dependency::NONE;
728                $(result |= self.$field.dependencies();)+
729                result
730            }
731
732            pub fn collect_global_state_refs(&self, refs: &mut $crate::collections::VecSet<String>) {
733                $(if let Some(property_refs) = self.$field.global_state_refs() {
734                    refs.extend(property_refs.iter().cloned());
735                })+
736            }
737
738            /// Bit i is set when the i-th data-driven property is the same
739            /// for every feature.
740            pub fn constants_mask(&self) -> u64 {
741                let mut result = 0u64;
742                let mut _index = 0u32;
743                $(if $crate::__property_data_driven!($kind) {
744                    if self.$field.is_constant() {
745                        result |= 1u64 << _index;
746                    }
747                    _index += 1;
748                })+
749                result
750            }
751        }
752    };
753}
754
755/// What an overridable property's override reads: whether a formatted
756/// section sets a text color.
757pub trait OverridableSection {
758    fn has_text_color(&self) -> bool;
759}
760
761impl OverridableSection for crate::expression::formatted::FormattedSection {
762    fn has_text_color(&self) -> bool {
763        self.text_color.is_some()
764    }
765}
766
767impl OverridableSection for crate::expression::forms::format::FormatExpressionSection {
768    fn has_text_color(&self) -> bool {
769        self.text_color.is_some()
770    }
771}
772
773/// How far a transition that has run `t` of its duration has come, as the
774/// style spec eases transitions: slow at both ends, halfway at the middle.
775fn ease_cubic_in_out(t: f64) -> f64 {
776    match t {
777        ..=0.0 => 0.0,
778        1.0.. => 1.0,
779        _ if t < 0.5 => 4.0 * t * t * t,
780        _ => 1.0 - 4.0 * (1.0 - t).powi(3),
781    }
782}