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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
use std::any::{Any, TypeId};
use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
// TODO(style): 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
/// An item with associated styles.
#[derive(PartialEq, Clone, Hash)]
pub struct Styled<T> {
/// The item to apply styles to.
pub item: T,
/// The associated style map.
pub map: StyleMap,
}
impl<T> Styled<T> {
/// Create a new instance from an item and a style map.
pub fn new(item: T, map: StyleMap) -> Self {
Self { item, map }
}
/// Create a new instance with empty style map.
pub fn bare(item: T) -> Self {
Self { item, map: StyleMap::new() }
}
/// Map the item with `f`.
pub fn map<F, U>(self, f: F) -> Styled<U>
where
F: FnOnce(T) -> U,
{
Styled { item: f(self.item), map: self.map }
}
}
impl<T: Debug> Debug for Styled<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.map.fmt(f)?;
self.item.fmt(f)
}
}
/// A map of style properties.
#[derive(Default, Clone, Hash)]
pub struct StyleMap(Vec<Entry>);
impl StyleMap {
/// Create a new, empty style map.
pub fn new() -> Self {
Self(vec![])
}
/// Whether this map contains no styles.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Create a style map from a single property-value pair.
pub fn with<P: Property>(key: P, value: P::Value) -> Self {
let mut styles = Self::new();
styles.set(key, value);
styles
}
/// Set the value for a style property.
pub fn set<P: Property>(&mut self, key: P, value: P::Value) {
for entry in &mut self.0 {
if entry.is::<P>() {
let prev = entry.downcast::<P>().unwrap();
let folded = P::fold(value, prev.clone());
*entry = Entry::new(key, folded);
return;
}
}
self.0.push(Entry::new(key, value));
}
/// Set a value for a style property if it is `Some(_)`.
pub fn set_opt<P: Property>(&mut self, key: P, value: Option<P::Value>) {
if let Some(value) = value {
self.set(key, value);
}
}
/// Toggle a boolean style property, removing it if it exists and inserting
/// it with `true` if it doesn't.
pub fn toggle<P: Property<Value = bool>>(&mut self, key: P) {
for (i, entry) in self.0.iter_mut().enumerate() {
if entry.is::<P>() {
self.0.swap_remove(i);
return;
}
}
self.0.push(Entry::new(key, true));
}
/// Mark all contained properties as _scoped_. This means that they only
/// apply to the first descendant node (of their type) in the hierarchy and
/// not its children, too. This is used by class constructors.
pub fn scoped(mut self) -> Self {
for entry in &mut self.0 {
entry.scoped = true;
}
self
}
/// Make `self` the first link of the style chain `outer`.
///
/// The resulting style chain contains styles from `self` as well as
/// `outer`. The ones from `self` take precedence over the ones from
/// `outer`. For folded properties `self` contributes the inner value.
pub fn chain<'a>(&'a self, outer: &'a StyleChain<'a>) -> StyleChain<'a> {
if self.is_empty() {
*outer
} else {
StyleChain {
first: Link::Map(self),
outer: Some(outer),
}
}
}
/// Apply styles from `outer` in-place. The resulting style map is
/// equivalent to the style chain created by
/// `self.chain(StyleChain::new(outer))`.
///
/// This is useful in the evaluation phase while building nodes and their
/// style maps, whereas `chain` would be used during layouting to combine
/// immutable style maps from different levels of the hierarchy.
pub fn apply(&mut self, outer: &Self) {
for outer in &outer.0 {
if let Some(inner) = self.0.iter_mut().find(|inner| inner.is_same(outer)) {
*inner = inner.fold(outer);
continue;
}
self.0.push(outer.clone());
}
}
/// Subtract `other` from `self` in-place, keeping only styles that are in
/// `self` but not in `other`.
pub fn erase(&mut self, other: &Self) {
self.0.retain(|x| !other.0.contains(x));
}
/// Intersect `self` with `other` in-place, keeping only styles that are
/// both in `self` and `other`.
pub fn intersect(&mut self, other: &Self) {
self.0.retain(|x| other.0.contains(x));
}
/// Whether two style maps are equal when filtered down to properties of the
/// node `T`.
pub fn compatible<T: 'static>(&self, other: &Self) -> bool {
let f = |entry: &&Entry| entry.is_of::<T>();
self.0.iter().filter(f).count() == other.0.iter().filter(f).count()
&& self.0.iter().filter(f).all(|x| other.0.contains(x))
}
}
impl Debug for StyleMap {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
for entry in &self.0 {
writeln!(f, "{:#?}", entry)?;
}
Ok(())
}
}
impl PartialEq for StyleMap {
fn eq(&self, other: &Self) -> bool {
self.0.len() == other.0.len() && self.0.iter().all(|x| other.0.contains(x))
}
}
/// A chain of style maps, similar to a linked list.
///
/// A style chain allows to conceptually merge (and fold) properties from
/// multiple style maps in a node hierarchy in a non-allocating way. Rather than
/// eagerly merging the maps, each access walks the hierarchy from the innermost
/// to the outermost map, trying to find a match and then folding it with
/// matches further up the chain.
#[derive(Clone, Copy, Hash)]
pub struct StyleChain<'a> {
/// The first map in the chain.
first: Link<'a>,
/// The remaining maps in the chain.
outer: Option<&'a Self>,
}
/// The two kinds of links in the chain.
#[derive(Clone, Copy, Hash)]
enum Link<'a> {
Map(&'a StyleMap),
Barrier(TypeId),
}
impl<'a> StyleChain<'a> {
/// Start a new style chain with a root map.
pub fn new(map: &'a StyleMap) -> Self {
Self { first: Link::Map(map), outer: None }
}
/// Get the (folded) value of a copyable style property.
///
/// This is the method you should reach for first. If it doesn't work
/// because your property is not copyable, use `get_ref`. If that doesn't
/// work either because your property needs folding, use `get_cloned`.
///
/// Returns the property's default value if no map in the chain contains an
/// entry for it.
pub fn get<P: Property>(self, key: P) -> P::Value
where
P::Value: Copy,
{
self.get_impl(key, 0)
}
/// Get a reference to a style property's value.
///
/// This is naturally only possible for properties that don't need folding.
/// Prefer `get` if possible or resort to `get_cloned` for non-`Copy`
/// properties that need folding.
///
/// Returns a lazily-initialized reference to the property's default value
/// if no map in the chain contains an entry for it.
pub fn get_ref<P: Property>(self, key: P) -> &'a P::Value
where
P: Nonfolding,
{
self.get_ref_impl(key, 0)
}
/// Get the (folded) value of any style property.
///
/// While this works for all properties, you should prefer `get` or
/// `get_ref` where possible. This is only needed for non-`Copy` properties
/// that need folding.
///
/// Returns the property's default value if no map in the chain contains an
/// entry for it.
pub fn get_cloned<P: Property>(self, key: P) -> P::Value {
self.get_impl(key, 0)
}
/// Insert a barrier into the style chain.
///
/// Barriers interact with [scoped](StyleMap::scoped) styles: A scoped style
/// can still be read through a single barrier (the one of the node it
/// _should_ apply to), but a second barrier will make it invisible.
pub fn barred<'b>(&'b self, node: TypeId) -> StyleChain<'b> {
if self.needs_barrier(node) {
StyleChain {
first: Link::Barrier(node),
outer: Some(self),
}
} else {
*self
}
}
}
impl<'a> StyleChain<'a> {
fn get_impl<P: Property>(self, key: P, depth: usize) -> P::Value {
let (value, depth) = self.process(key, depth);
if let Some(value) = value.cloned() {
if P::FOLDABLE {
if let Some(outer) = self.outer {
P::fold(value, outer.get_cloned(key))
} else {
P::fold(value, P::default())
}
} else {
value
}
} else if let Some(outer) = self.outer {
outer.get_impl(key, depth)
} else {
P::default()
}
}
fn get_ref_impl<P: Property>(self, key: P, depth: usize) -> &'a P::Value
where
P: Nonfolding,
{
let (value, depth) = self.process(key, depth);
if let Some(value) = value {
value
} else if let Some(outer) = self.outer {
outer.get_ref_impl(key, depth)
} else {
P::default_ref()
}
}
fn process<P: Property>(self, _: P, depth: usize) -> (Option<&'a P::Value>, usize) {
match self.first {
Link::Map(map) => (
map.0
.iter()
.find(|entry| entry.is::<P>() && (!entry.scoped || depth <= 1))
.and_then(|entry| entry.downcast::<P>()),
depth,
),
Link::Barrier(node) => (None, depth + (P::node_id() == node) as usize),
}
}
fn needs_barrier(self, node: TypeId) -> bool {
if let Link::Map(map) = self.first {
if map.0.iter().any(|entry| entry.is_of_same(node)) {
return true;
}
}
self.outer.map_or(false, |outer| outer.needs_barrier(node))
}
}
impl Debug for StyleChain<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.first.fmt(f)?;
if let Some(outer) = self.outer {
outer.fmt(f)?;
}
Ok(())
}
}
impl Debug for Link<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Map(map) => map.fmt(f),
Self::Barrier(id) => writeln!(f, "Barrier({:?})", id),
}
}
}
/// An entry for a single style property.
#[derive(Clone)]
struct Entry {
p: Rc<dyn Bounds>,
scoped: bool,
}
impl Entry {
fn new<P: Property>(key: P, value: P::Value) -> Self {
Self { p: Rc::new((key, value)), scoped: false }
}
fn is<P: Property>(&self) -> bool {
self.p.style_id() == TypeId::of::<P>()
}
fn is_same(&self, other: &Self) -> bool {
self.p.style_id() == other.p.style_id()
}
fn is_of<T: 'static>(&self) -> bool {
self.p.node_id() == TypeId::of::<T>()
}
fn is_of_same(&self, node: TypeId) -> bool {
self.p.node_id() == node
}
fn downcast<P: Property>(&self) -> Option<&P::Value> {
self.p.as_any().downcast_ref()
}
fn fold(&self, outer: &Self) -> Self {
self.p.fold(outer)
}
}
impl Debug for Entry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.p.dyn_fmt(f)
}
}
impl PartialEq for Entry {
fn eq(&self, other: &Self) -> bool {
self.p.dyn_eq(other)
}
}
impl Hash for Entry {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.p.hash64());
}
}
/// Style property keys.
///
/// This trait is not intended to be implemented manually, but rather through
/// the `#[properties]` proc-macro.
pub trait Property: Copy + 'static {
/// The type of value that is returned when getting this property from a
/// style map. For example, this could be [`Length`](crate::geom::Length)
/// for a `WIDTH` property.
type Value: Debug + Clone + PartialEq + Hash + 'static;
/// The name of the property, used for debug printing.
const NAME: &'static str;
/// Whether the property needs folding.
const FOLDABLE: bool = false;
/// The type id of the node this property belongs to.
fn node_id() -> TypeId;
/// 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;
/// Fold the property with an outer value.
///
/// For example, this would fold a relative font size with an outer
/// absolute font size.
#[allow(unused_variables)]
fn fold(inner: Self::Value, outer: Self::Value) -> Self::Value {
inner
}
}
/// Marker trait that indicates that a property doesn't need folding.
pub trait Nonfolding {}
/// This trait is implemented for pairs of zero-sized property keys and their
/// value types below. Although it is zero-sized, the property `P` must be part
/// of the implementing type so that we can use it in the methods (it must be a
/// constrained type parameter).
trait Bounds: 'static {
fn as_any(&self) -> &dyn Any;
fn dyn_fmt(&self, f: &mut Formatter) -> fmt::Result;
fn dyn_eq(&self, other: &Entry) -> bool;
fn hash64(&self) -> u64;
fn node_id(&self) -> TypeId;
fn style_id(&self) -> TypeId;
fn fold(&self, outer: &Entry) -> Entry;
}
impl<P: Property> Bounds for (P, P::Value) {
fn as_any(&self) -> &dyn Any {
&self.1
}
fn dyn_fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "#[{} = {:?}]", P::NAME, self.1)
}
fn dyn_eq(&self, other: &Entry) -> bool {
self.style_id() == other.p.style_id()
&& if let Some(other) = other.downcast::<P>() {
&self.1 == other
} else {
false
}
}
fn hash64(&self) -> u64 {
let mut state = fxhash::FxHasher64::default();
self.style_id().hash(&mut state);
self.1.hash(&mut state);
state.finish()
}
fn node_id(&self) -> TypeId {
P::node_id()
}
fn style_id(&self) -> TypeId {
TypeId::of::<P>()
}
fn fold(&self, outer: &Entry) -> Entry {
let outer = outer.downcast::<P>().unwrap();
let combined = P::fold(self.1.clone(), outer.clone());
Entry::new(self.0, combined)
}
}
|