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