diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-07 10:50:39 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-07 11:07:00 +0200 |
| commit | 3d52387eea321e94c13b61666f7a758052b20c5d (patch) | |
| tree | 5c55c51ca7e4b53dee61d280c39b7f664b8b9d6b /src/library/structure | |
| parent | 20b4d590b3efbd9b7a44fd6d3a658e7b84d21b99 (diff) | |
Rework style chains
Diffstat (limited to 'src/library/structure')
| -rw-r--r-- | src/library/structure/heading.rs | 42 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 12 | ||||
| -rw-r--r-- | src/library/structure/table.rs | 7 |
3 files changed, 33 insertions, 28 deletions
diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index 7d3273f5..7b00c643 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -1,5 +1,5 @@ use crate::library::prelude::*; -use crate::library::text::{FontFamily, TextNode}; +use crate::library::text::{FontFamily, FontSize, TextNode, Toggle}; /// A section heading. #[derive(Debug, Hash)] @@ -14,25 +14,34 @@ pub struct HeadingNode { #[node(showable)] impl HeadingNode { /// The heading's font family. Just the normal text family if `auto`. + #[property(referenced)] pub const FAMILY: Leveled<Smart<FontFamily>> = Leveled::Value(Smart::Auto); /// The color of text in the heading. Just the normal text color if `auto`. + #[property(referenced)] pub const FILL: Leveled<Smart<Paint>> = Leveled::Value(Smart::Auto); /// The size of text in the heading. - pub const SIZE: Leveled<Linear> = Leveled::Mapping(|level| { + #[property(referenced)] + pub const SIZE: Leveled<FontSize> = Leveled::Mapping(|level| { let upscale = (1.6 - 0.1 * level as f64).max(0.75); - Relative::new(upscale).into() + FontSize(Relative::new(upscale).into()) }); /// Whether text in the heading is strengthend. + #[property(referenced)] pub const STRONG: Leveled<bool> = Leveled::Value(true); /// Whether text in the heading is emphasized. + #[property(referenced)] pub const EMPH: Leveled<bool> = Leveled::Value(false); /// Whether the heading is underlined. + #[property(referenced)] pub const UNDERLINE: Leveled<bool> = Leveled::Value(false); /// The extra padding above the heading. + #[property(referenced)] pub const ABOVE: Leveled<Length> = Leveled::Value(Length::zero()); /// The extra padding below the heading. + #[property(referenced)] pub const BELOW: Leveled<Length> = Leveled::Value(Length::zero()); /// Whether the heading is block-level. + #[property(referenced)] pub const BLOCK: Leveled<bool> = Leveled::Value(true); fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> { @@ -47,16 +56,17 @@ impl Show for HeadingNode { fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { macro_rules! resolve { ($key:expr) => { - styles.get_cloned($key).resolve(ctx, self.level)? + styles.get($key).resolve(ctx, self.level)? }; } - // Resolve the user recipe. + let args = [ + Value::Int(self.level as i64), + Value::Content(self.body.clone()), + ]; + let mut body = styles - .show(self, ctx, [ - Value::Int(self.level as i64), - Value::Content(self.body.clone()), - ])? + .show::<Self, _>(ctx, args)? .unwrap_or_else(|| self.body.clone()); let mut map = StyleMap::new(); @@ -71,11 +81,11 @@ impl Show for HeadingNode { } if resolve!(Self::STRONG) { - map.set(TextNode::STRONG, true); + map.set(TextNode::STRONG, Toggle); } if resolve!(Self::EMPH) { - map.set(TextNode::EMPH, true); + map.set(TextNode::EMPH, Toggle); } let mut seq = vec![]; @@ -116,15 +126,15 @@ pub enum Leveled<T> { Func(Func, Span), } -impl<T: Cast> Leveled<T> { +impl<T: Cast + Clone> Leveled<T> { /// Resolve the value based on the level. - pub fn resolve(self, ctx: &mut Context, level: usize) -> TypResult<T> { + pub fn resolve(&self, ctx: &mut Context, level: usize) -> TypResult<T> { Ok(match self { - Self::Value(value) => value, + Self::Value(value) => value.clone(), Self::Mapping(mapping) => mapping(level), Self::Func(func, span) => { - let args = Args::from_values(span, [Value::Int(level as i64)]); - func.call(ctx, args)?.cast().at(span)? + let args = Args::from_values(*span, [Value::Int(level as i64)]); + func.call(ctx, args)?.cast().at(*span)? } }) } diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 414f601e..1b22e166 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -31,6 +31,7 @@ pub type EnumNode = ListNode<ORDERED>; #[node(showable)] impl<const L: ListKind> ListNode<L> { /// How the list is labelled. + #[property(referenced)] pub const LABEL: Label = Label::Default; /// The spacing between the list items of a non-wide list. pub const SPACING: Linear = Linear::zero(); @@ -58,17 +59,14 @@ impl<const L: ListKind> ListNode<L> { impl<const L: ListKind> Show for ListNode<L> { fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - let content = if let Some(content) = styles.show( - self, - ctx, - self.items.iter().map(|item| Value::Content((*item.body).clone())), - )? { + let args = self.items.iter().map(|item| Value::Content((*item.body).clone())); + let content = if let Some(content) = styles.show::<Self, _>(ctx, args)? { content } else { let mut children = vec![]; let mut number = self.start; - let label = styles.get_ref(Self::LABEL); + let label = styles.get(Self::LABEL); for item in &self.items { number = item.number.unwrap_or(number); @@ -79,7 +77,7 @@ impl<const L: ListKind> Show for ListNode<L> { number += 1; } - let em = styles.get(TextNode::SIZE).abs; + let em = styles.get(TextNode::SIZE); let leading = styles.get(ParNode::LEADING); let spacing = if self.wide { styles.get(ParNode::SPACING) diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index 0e455ead..64785006 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -55,11 +55,8 @@ impl TableNode { impl Show for TableNode { fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - if let Some(content) = styles.show( - self, - ctx, - self.children.iter().map(|child| Value::Content(child.clone())), - )? { + let args = self.children.iter().map(|child| Value::Content(child.clone())); + if let Some(content) = styles.show::<Self, _>(ctx, args)? { return Ok(content); } |
