From 6d8b65c4b24206a1482ea143791d7a1c410a4313 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Wed, 8 Jun 2022 12:30:10 +0200 Subject: [PATCH] More consistent role application --- src/frame.rs | 4 +++- src/library/graphics/shape.rs | 9 ++++++--- src/library/layout/flow.rs | 2 +- src/library/layout/page.rs | 3 ++- src/library/layout/stack.rs | 10 ++++++---- src/library/structure/heading.rs | 9 ++------- src/library/structure/list.rs | 20 +++++++++----------- src/library/structure/table.rs | 7 ++----- src/library/text/par.rs | 9 +++++---- src/library/text/raw.rs | 4 ++-- src/model/content.rs | 5 +++++ 11 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/frame.rs b/src/frame.rs index 8b14b2b16..0a8a20548 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -448,7 +448,9 @@ pub enum Role { } impl Role { - fn is_weak(&self) -> bool { + /// Whether the role describes a generic element and is not very + /// descriptive. + pub fn is_weak(self) -> bool { match self { Self::Paragraph | Self::GenericBlock | Self::GenericInline => true, _ => false, diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs index 8070231e4..e972a3d52 100644 --- a/src/library/graphics/shape.rs +++ b/src/library/graphics/shape.rs @@ -91,11 +91,14 @@ impl Layout for ShapeNode { let child = child.clone().padded(inset.map(|side| side.map(RawLength::from))); let mut pod = Regions::one(regions.first, regions.base, regions.expand); - let role_map = StyleMap::with_role(Role::GenericBlock); - let styles = role_map.chain(&styles); - frames = child.layout(ctx, &pod, styles)?; + for frame in frames.iter_mut() { + if frame.role().map_or(true, Role::is_weak) { + Arc::make_mut(frame).apply_role(Role::GenericBlock); + } + } + // Relayout with full expansion into square region to make sure // the result is really a square or circle. if is_quadratic(S) { diff --git a/src/library/layout/flow.rs b/src/library/layout/flow.rs index 0ba84b091..f779c8b14 100644 --- a/src/library/layout/flow.rs +++ b/src/library/layout/flow.rs @@ -184,7 +184,7 @@ impl FlowLayouter { let len = frames.len(); for (i, mut frame) in frames.into_iter().enumerate() { // Set the generic block role. - if frame.role().is_none() { + if frame.role().map_or(true, Role::is_weak) { Arc::make_mut(&mut frame).apply_role(Role::GenericBlock); } diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index d524839b9..8bd507c43 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -130,7 +130,8 @@ impl PageNode { let pod = Regions::one(area, area, Spec::splat(true)); let role_map = StyleMap::with_role(role); let styles = role_map.chain(&styles); - let sub = content.layout(ctx, &pod, styles)?.remove(0); + let mut sub = content.layout(ctx, &pod, styles)?.remove(0); + Arc::make_mut(&mut sub).apply_role(role); if std::ptr::eq(marginal, background) { Arc::make_mut(frame).prepend_frame(pos, sub); diff --git a/src/library/layout/stack.rs b/src/library/layout/stack.rs index 7bad01d99..9c2cbccd4 100644 --- a/src/library/layout/stack.rs +++ b/src/library/layout/stack.rs @@ -192,12 +192,14 @@ impl<'a> StackLayouter<'a> { self.dir.start().into() }); - let role_map = StyleMap::with_role(Role::GenericBlock); - let styles = role_map.chain(&styles); - let frames = node.layout(ctx, &self.regions, styles)?; let len = frames.len(); - for (i, frame) in frames.into_iter().enumerate() { + for (i, mut frame) in frames.into_iter().enumerate() { + // Set the generic block role. + if frame.role().map_or(true, Role::is_weak) { + Arc::make_mut(&mut frame).apply_role(Role::GenericBlock); + } + // Grow our size, shrink the region and save the frame for later. let size = frame.size.to_gen(self.axis); self.used.main += size.main; diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index 285793dd0..a376bf095 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -66,13 +66,8 @@ impl HeadingNode { impl Show for HeadingNode { fn unguard(&self, sel: Selector) -> ShowNode { - let mut map = StyleMap::with_role(Role::Heading(self.level.get())); - map.push(StyleEntry::Unguard(sel)); - Self { - body: self.body.clone().styled_with_map(map), - ..*self - } - .pack() + let body = self.body.unguard(sel).role(Role::Heading(self.level.get())); + Self { body, ..*self }.pack() } fn encode(&self, _: StyleChain) -> Dict { diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 563426b4e..1c0e251f7 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -77,12 +77,9 @@ impl ListNode { impl Show for ListNode { fn unguard(&self, sel: Selector) -> ShowNode { - let mut map = StyleMap::with_role(Role::ListItemBody); - map.push(StyleEntry::Unguard(sel)); - Self { items: self.items.map(|item| ListItem { - body: Box::new(item.body.clone().styled_with_map(map.clone())), + body: Box::new(item.body.unguard(sel).role(Role::ListItemBody)), ..*item }), ..*self @@ -113,11 +110,14 @@ impl Show for ListNode { for (item, map) in self.items.iter() { number = item.number.unwrap_or(number); - let mut label_map = map.clone(); - label_map.push(StyleEntry::Role(Role::ListLabel)); - cells.push(LayoutNode::default()); - cells.push(label.resolve(ctx, L, number)?.styled_with_map(label_map).pack()); + cells.push( + label + .resolve(ctx, L, number)? + .styled_with_map(map.clone()) + .role(Role::ListLabel) + .pack(), + ); cells.push(LayoutNode::default()); cells.push((*item.body).clone().styled_with_map(map.clone()).pack()); number += 1; @@ -163,9 +163,7 @@ impl Show for ListNode { } } - Ok(realized - .styled_with_map(StyleMap::with_role(Role::List(L == ORDERED))) - .spaced(above, below)) + Ok(realized.role(Role::List(L == ORDERED)).spaced(above, below)) } } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index 601156126..118e48ca3 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -50,16 +50,13 @@ impl TableNode { impl Show for TableNode { fn unguard(&self, sel: Selector) -> ShowNode { - let mut map = StyleMap::with_role(Role::TableCell); - map.push(StyleEntry::Unguard(sel)); - Self { tracks: self.tracks.clone(), gutter: self.gutter.clone(), cells: self .cells .iter() - .map(|cell| cell.clone().styled_with_map(map.clone())) + .map(|cell| cell.unguard(sel).role(Role::TableCell)) .collect(), } .pack() @@ -110,7 +107,7 @@ impl Show for TableNode { cells, semantic: GridSemantics::Table, }) - .styled_with_map(StyleMap::with_role(Role::Table))) + .role(Role::Table)) } fn finalize( diff --git a/src/library/text/par.rs b/src/library/text/par.rs index 53bb798f5..0fec11ad8 100644 --- a/src/library/text/par.rs +++ b/src/library/text/par.rs @@ -551,14 +551,15 @@ fn prepare<'a>( } else { let size = Size::new(regions.first.x, regions.base.y); let pod = Regions::one(size, regions.base, Spec::splat(false)); - let role_map = StyleMap::with_role(Role::GenericInline); - let styles = role_map.chain(&styles); let mut frame = node.layout(ctx, &pod, styles)?.remove(0); let shift = styles.get(TextNode::BASELINE); - if !shift.is_zero() { - Arc::make_mut(&mut frame).translate(Point::with_y(shift)); + if !shift.is_zero() || frame.role().map_or(true, Role::is_weak) { + let frame = Arc::make_mut(&mut frame); + + frame.translate(Point::with_y(shift)); + frame.apply_role(Role::GenericInline); } items.push(Item::Frame(frame)); diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index 4d73b11bf..31db811a4 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -113,7 +113,7 @@ impl Show for RawNode { styles: StyleChain, mut realized: Content, ) -> TypResult { - let mut map = StyleMap::with_role(Role::Code); + let mut map = StyleMap::new(); map.set_family(styles.get(Self::FAMILY).clone(), styles); map.set(TextNode::OVERHANG, false); map.set(TextNode::HYPHENATE, Smart::Custom(Hyphenate(false))); @@ -123,7 +123,7 @@ impl Show for RawNode { realized = realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)); } - Ok(realized.styled_with_map(map)) + Ok(realized.styled_with_map(map).role(Role::Code)) } } diff --git a/src/model/content.rs b/src/model/content.rs index 21bf83695..2b9eb182a 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -204,6 +204,11 @@ impl Content { Self::Styled(Arc::new((self, styles))) } + /// Assign a role to this content by adding a style map. + pub fn role(self, role: Role) -> Self { + self.styled_with_map(StyleMap::with_role(role)) + } + /// Reenable the show rule identified by the selector. pub fn unguard(&self, sel: Selector) -> Self { self.clone().styled_with_entry(StyleEntry::Unguard(sel))