1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
use std::any::{Any, TypeId};
use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
// Possible optimizations:
// - Ref-count map for cheaper cloning and smaller footprint
// - Store map in `Option` to make empty maps non-allocating
// - Store small properties inline
/// A map of style properties.
#[derive(Default, Clone, Hash)]
pub struct Styles {
map: Vec<(StyleId, Entry)>,
}
impl Styles {
/// Create a new, empty style map.
pub fn new() -> Self {
Self { map: vec![] }
}
/// Create a style map with a single property-value pair.
pub fn one<P: Property>(key: P, value: P::Value) -> Self {
let mut styles = Self::new();
styles.set(key, value);
styles
}
/// Whether this map contains no styles.
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Set the value for a style property.
pub fn set<P: Property>(&mut self, key: P, value: P::Value) {
let id = StyleId::of::<P>();
for pair in &mut self.map {
if pair.0 == id {
let prev = pair.1.downcast::<P::Value>().unwrap();
let folded = P::combine(value, prev.clone());
pair.1 = Entry::new(key, folded);
return;
}
}
self.map.push((id, Entry::new(key, value)));
}
/// Toggle a boolean style property.
pub fn toggle<P: Property<Value = bool>>(&mut self, key: P) {
let id = StyleId::of::<P>();
for (i, pair) in self.map.iter_mut().enumerate() {
if pair.0 == id {
self.map.swap_remove(i);
return;
}
}
self.map.push((id, Entry::new(key, true)));
}
/// Get the value of a copyable style property.
///
/// Returns the property's default value if the map does not contain an
/// entry for it.
pub fn get<P: Property>(&self, key: P) -> P::Value
where
P::Value: Copy,
{
self.get_direct(key).copied().unwrap_or_else(P::default)
}
/// Get a reference to a style property.
///
/// Returns a reference to the property's default value if the map does not
/// contain an entry for it.
pub fn get_ref<P: Property>(&self, key: P) -> &P::Value {
self.get_direct(key).unwrap_or_else(|| P::default_ref())
}
/// Get a reference to a style directly in this map (no default value).
pub fn get_direct<P: Property>(&self, _: P) -> Option<&P::Value> {
self.map
.iter()
.find(|pair| pair.0 == StyleId::of::<P>())
.and_then(|pair| pair.1.downcast())
}
/// Apply styles from `outer` in-place.
///
/// Properties from `self` take precedence over the ones from `outer`.
pub fn apply(&mut self, outer: &Self) {
'outer: for pair in &outer.map {
for (id, entry) in &mut self.map {
if pair.0 == *id {
entry.apply(&pair.1);
continue 'outer;
}
}
self.map.push(pair.clone());
}
}
/// Create new styles combining `self` with `outer`.
///
/// Properties from `self` take precedence over the ones from `outer`.
pub fn chain(&self, outer: &Self) -> Self {
let mut styles = self.clone();
styles.apply(outer);
styles
}
/// Keep only those styles that are also in `other`.
pub fn intersect(&mut self, other: &Self) {
self.map.retain(|a| other.map.iter().any(|b| a == b));
}
/// Keep only those styles that are not also in `other`.
pub fn erase(&mut self, other: &Self) {
self.map.retain(|a| other.map.iter().all(|b| a != b));
}
/// Whether two style maps are equal when filtered down to the given
/// properties.
pub fn compatible<F>(&self, other: &Self, filter: F) -> bool
where
F: Fn(StyleId) -> bool,
{
// TODO(set): Filtered length + one direction equal should suffice.
let f = |e: &&(StyleId, Entry)| filter(e.0);
self.map.iter().filter(f).all(|pair| other.map.contains(pair))
&& other.map.iter().filter(f).all(|pair| self.map.contains(pair))
}
}
impl Debug for Styles {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
for pair in &self.map {
writeln!(f, "{:#?}", pair.1)?;
}
Ok(())
} else {
f.write_str("Styles ")?;
f.debug_set().entries(self.map.iter().map(|pair| &pair.1)).finish()
}
}
}
impl PartialEq for Styles {
fn eq(&self, other: &Self) -> bool {
self.compatible(other, |_| true)
}
}
/// An entry for a single style property.
#[derive(Clone)]
pub(crate) struct Entry(Rc<dyn Bounds>);
impl Entry {
fn new<P: Property>(key: P, value: P::Value) -> Self {
Self(Rc::new((key, value)))
}
fn downcast<T: 'static>(&self) -> Option<&T> {
self.0.as_any().downcast_ref()
}
fn apply(&mut self, outer: &Self) {
*self = self.0.combine(outer);
}
}
impl Debug for Entry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq for Entry {
fn eq(&self, other: &Self) -> bool {
self.0.dyn_eq(other)
}
}
impl Hash for Entry {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.0.hash64());
}
}
trait Bounds: 'static {
fn as_any(&self) -> &dyn Any;
fn fmt(&self, f: &mut Formatter) -> fmt::Result;
fn dyn_eq(&self, other: &Entry) -> bool;
fn hash64(&self) -> u64;
fn combine(&self, outer: &Entry) -> Entry;
}
impl<P> Bounds for (P, P::Value)
where
P: Property,
P::Value: Debug + Hash + PartialEq + 'static,
{
fn as_any(&self) -> &dyn Any {
&self.1
}
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "#[{} = {:?}]", P::NAME, self.1)
} else {
write!(f, "{}: {:?}", P::NAME, self.1)
}
}
fn dyn_eq(&self, other: &Entry) -> bool {
if let Some(other) = other.downcast::<P::Value>() {
&self.1 == other
} else {
false
}
}
fn hash64(&self) -> u64 {
// No need to hash the TypeId since there's only one
// valid value type per property.
fxhash::hash64(&self.1)
}
fn combine(&self, outer: &Entry) -> Entry {
let outer = outer.downcast::<P::Value>().unwrap();
let combined = P::combine(self.1.clone(), outer.clone());
Entry::new(self.0, combined)
}
}
/// Style property keys.
///
/// This trait is not intended to be implemented manually, but rather through
/// the `properties!` macro.
pub trait Property: Copy + 'static {
/// The type of this property, for example, this could be
/// [`Length`](crate::geom::Length) for a `WIDTH` property.
type Value: Debug + Clone + Hash + PartialEq + 'static;
/// The name of the property, used for debug printing.
const NAME: &'static str;
/// Combine the property with an outer value.
fn combine(inner: Self::Value, outer: Self::Value) -> Self::Value;
/// The default value of the property.
fn default() -> Self::Value;
/// A static reference to the default value of the property.
///
/// This is automatically implemented through lazy-initialization in the
/// `properties!` macro. This way, expensive defaults don't need to be
/// recreated all the time.
fn default_ref() -> &'static Self::Value;
}
/// A unique identifier for a style property.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct StyleId(TypeId);
impl StyleId {
/// The style id of the property.
pub fn of<P: Property>() -> Self {
Self(TypeId::of::<P>())
}
}
/// Generate the property keys for a node.
macro_rules! properties {
($node:ty, $(
$(#[doc = $doc:expr])*
$(#[fold($combine:expr)])?
$name:ident: $type:ty = $default:expr
),* $(,)?) => {
// TODO(set): Fix possible name clash.
mod properties {
use std::marker::PhantomData;
use $crate::eval::{Property, StyleId};
use super::*;
$(#[allow(non_snake_case)] mod $name {
use once_cell::sync::Lazy;
use super::*;
pub struct Key<T>(pub PhantomData<T>);
impl<T> Copy for Key<T> {}
impl<T> Clone for Key<T> {
fn clone(&self) -> Self {
*self
}
}
impl Property for Key<$type> {
type Value = $type;
const NAME: &'static str = concat!(
stringify!($node), "::", stringify!($name)
);
#[allow(unused_mut, unused_variables)]
fn combine(mut inner: Self::Value, outer: Self::Value) -> Self::Value {
$(
let combine: fn(Self::Value, Self::Value) -> Self::Value = $combine;
inner = combine(inner, outer);
)?
inner
}
fn default() -> Self::Value {
$default
}
fn default_ref() -> &'static Self::Value {
static LAZY: Lazy<$type> = Lazy::new(|| $default);
&*LAZY
}
}
})*
impl $node {
/// Check whether the property with the given type id belongs to
/// `Self`.
pub fn has_property(id: StyleId) -> bool {
false || $(id == StyleId::of::<$name::Key<$type>>())||*
}
$($(#[doc = $doc])* pub const $name: $name::Key<$type>
= $name::Key(PhantomData);)*
}
}
};
}
/// Set a style property to a value if the value is `Some`.
macro_rules! set {
($styles:expr, $target:expr => $value:expr) => {
if let Some(v) = $value {
$styles.set($target, v);
}
};
}
|