summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2022-06-08 12:30:10 +0200
committerMartin Haug <mhaug@live.de>2022-06-08 12:30:10 +0200
commit6d8b65c4b24206a1482ea143791d7a1c410a4313 (patch)
treee11046d9b92928969b904a63cc6f59540b95d0a7 /src/library
parent4640585fbdf72df993dbed46799844aa78996cce (diff)
More consistent role application
Diffstat (limited to 'src/library')
-rw-r--r--src/library/graphics/shape.rs9
-rw-r--r--src/library/layout/flow.rs2
-rw-r--r--src/library/layout/page.rs3
-rw-r--r--src/library/layout/stack.rs10
-rw-r--r--src/library/structure/heading.rs9
-rw-r--r--src/library/structure/list.rs20
-rw-r--r--src/library/structure/table.rs7
-rw-r--r--src/library/text/par.rs9
-rw-r--r--src/library/text/raw.rs4
9 files changed, 35 insertions, 38 deletions
diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs
index 8070231e..e972a3d5 100644
--- a/src/library/graphics/shape.rs
+++ b/src/library/graphics/shape.rs
@@ -91,11 +91,14 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> {
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 0ba84b09..f779c8b1 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 d524839b..8bd507c4 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 7bad01d9..9c2cbccd 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 285793dd..a376bf09 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 563426b4..1c0e251f 100644
--- a/src/library/structure/list.rs
+++ b/src/library/structure/list.rs
@@ -77,12 +77,9 @@ impl<const L: ListKind> ListNode<L> {
impl<const L: ListKind> Show for ListNode<L> {
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<const L: ListKind> Show for ListNode<L> {
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<const L: ListKind> Show for ListNode<L> {
}
}
- 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 60115612..118e48ca 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 53bb798f..0fec11ad 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 4d73b11b..31db811a 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<Content> {
- 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))
}
}