diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-06-08 19:31:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-08 19:31:07 +0200 |
| commit | cd5a14bc240b21f8f538ff7fc1d116d23611d6c5 (patch) | |
| tree | f4b8a7e8a941f8653d5061d2f7acc0bfd8d92b13 /src/library | |
| parent | b905048d4bb497252028df6d21b525fefb6b64c3 (diff) | |
| parent | 72d3f3fffabe6872eb7839585bea925b89aac6a4 (diff) | |
Merge pull request #75 from typst/semantics
Frame Role and PDF outline
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/graphics/hide.rs | 6 | ||||
| -rw-r--r-- | src/library/graphics/shape.rs | 4 | ||||
| -rw-r--r-- | src/library/layout/flow.rs | 7 | ||||
| -rw-r--r-- | src/library/layout/grid.rs | 16 | ||||
| -rw-r--r-- | src/library/layout/page.rs | 26 | ||||
| -rw-r--r-- | src/library/layout/stack.rs | 7 | ||||
| -rw-r--r-- | src/library/math/rex.rs | 1 | ||||
| -rw-r--r-- | src/library/structure/heading.rs | 5 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 16 | ||||
| -rw-r--r-- | src/library/structure/table.rs | 9 | ||||
| -rw-r--r-- | src/library/text/par.rs | 9 | ||||
| -rw-r--r-- | src/library/text/raw.rs | 2 |
12 files changed, 87 insertions, 21 deletions
diff --git a/src/library/graphics/hide.rs b/src/library/graphics/hide.rs index 28afe320..4ba5e023 100644 --- a/src/library/graphics/hide.rs +++ b/src/library/graphics/hide.rs @@ -22,7 +22,11 @@ impl Layout for HideNode { // Clear the frames. for frame in &mut frames { - *frame = Arc::new(Frame { elements: vec![], ..**frame }); + *frame = Arc::new({ + let mut empty = Frame::new(frame.size); + empty.baseline = frame.baseline; + empty + }); } Ok(frames) diff --git a/src/library/graphics/shape.rs b/src/library/graphics/shape.rs index 9da8d8df..82eb2d9d 100644 --- a/src/library/graphics/shape.rs +++ b/src/library/graphics/shape.rs @@ -93,6 +93,10 @@ impl<const S: ShapeKind> Layout for ShapeNode<S> { let mut pod = Regions::one(regions.first, regions.base, regions.expand); frames = child.layout(ctx, &pod, styles)?; + for frame in frames.iter_mut() { + 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 6193a68f..f779c8b1 100644 --- a/src/library/layout/flow.rs +++ b/src/library/layout/flow.rs @@ -182,7 +182,12 @@ impl FlowLayouter { 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; self.used.y += size.y; diff --git a/src/library/layout/grid.rs b/src/library/layout/grid.rs index 4cad9de6..2d6eb259 100644 --- a/src/library/layout/grid.rs +++ b/src/library/layout/grid.rs @@ -450,6 +450,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row with fixed height and return its frame. fn layout_single_row(&mut self, height: Length, y: usize) -> TypResult<Frame> { let mut output = Frame::new(Size::new(self.used.x, height)); + let mut pos = Point::zero(); for (x, &rcol) in self.rcols.iter().enumerate() { @@ -464,6 +465,14 @@ impl<'a> GridLayouter<'a> { let pod = Regions::one(size, base, Spec::splat(true)); let frame = node.layout(self.ctx, &pod, self.styles)?.remove(0); + match frame.role() { + Some(Role::ListLabel | Role::ListItemBody) => { + output.apply_role(Role::ListItem) + } + Some(Role::TableCell) => output.apply_role(Role::TableRow), + _ => {} + } + output.push_frame(pos, frame); } @@ -505,6 +514,13 @@ impl<'a> GridLayouter<'a> { // Push the layouted frames into the individual output frames. let frames = node.layout(self.ctx, &pod, self.styles)?; for (output, frame) in outputs.iter_mut().zip(frames) { + match frame.role() { + Some(Role::ListLabel | Role::ListItemBody) => { + output.apply_role(Role::ListItem) + } + Some(Role::TableCell) => output.apply_role(Role::TableRow), + _ => {} + } output.push_frame(pos, frame); } } diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index 8435e510..115a1923 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -110,16 +110,28 @@ impl PageNode { let pad = padding.resolve(styles).relative_to(size); let pw = size.x - pad.left - pad.right; let py = size.y - pad.bottom; - for (marginal, pos, area) in [ - (header, Point::with_x(pad.left), Size::new(pw, pad.top)), - (footer, Point::new(pad.left, py), Size::new(pw, pad.bottom)), - (foreground, Point::zero(), size), - (background, Point::zero(), size), + for (role, marginal, pos, area) in [ + ( + Role::Header, + header, + Point::with_x(pad.left), + Size::new(pw, pad.top), + ), + ( + Role::Footer, + footer, + Point::new(pad.left, py), + Size::new(pw, pad.bottom), + ), + (Role::Foreground, foreground, Point::zero(), size), + (Role::Background, background, Point::zero(), size), ] { if let Some(content) = marginal.resolve(ctx, page)? { let pod = Regions::one(area, area, Spec::splat(true)); - let sub = content.layout(ctx, &pod, styles)?.remove(0); - if std::ptr::eq(marginal, background) { + let mut sub = content.layout(ctx, &pod, styles)?.remove(0); + Arc::make_mut(&mut sub).apply_role(role); + + if role == Role::Background { Arc::make_mut(frame).prepend_frame(pos, sub); } else { Arc::make_mut(frame).push_frame(pos, sub); diff --git a/src/library/layout/stack.rs b/src/library/layout/stack.rs index 828ff8e3..9c2cbccd 100644 --- a/src/library/layout/stack.rs +++ b/src/library/layout/stack.rs @@ -194,7 +194,12 @@ impl<'a> StackLayouter<'a> { 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/math/rex.rs b/src/library/math/rex.rs index 0268fb9c..f839a9e8 100644 --- a/src/library/math/rex.rs +++ b/src/library/math/rex.rs @@ -66,6 +66,7 @@ impl Layout for RexNode { let mut backend = FrameBackend { frame: { let mut frame = Frame::new(size); + frame.apply_role(Role::Formula); frame.baseline = Some(baseline); frame }, diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index a0973b90..af2b3626 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -65,7 +65,8 @@ impl HeadingNode { impl Show for HeadingNode { fn unguard(&self, sel: Selector) -> ShowNode { - Self { body: self.body.unguard(sel), ..*self }.pack() + let body = self.body.unguard(sel).role(Role::Heading(self.level.get())); + Self { body, ..*self }.pack() } fn encode(&self, _: StyleChain) -> Dict { @@ -114,7 +115,7 @@ impl Show for HeadingNode { realized = realized.underlined(); } - realized = realized.styled_with_map(map); + realized = realized.styled_with_map(map).role(Role::Heading(self.level.get())); realized = realized.spaced( resolve!(Self::ABOVE).resolve(styles), resolve!(Self::BELOW).resolve(styles), diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 84603eb3..015ef520 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -78,7 +78,7 @@ impl<const L: ListKind> Show for ListNode<L> { fn unguard(&self, sel: Selector) -> ShowNode { Self { items: self.items.map(|item| ListItem { - body: Box::new(item.body.unguard(sel)), + body: Box::new(item.body.unguard(sel).role(Role::ListItemBody)), ..*item }), ..*self @@ -108,9 +108,15 @@ impl<const L: ListKind> Show for ListNode<L> { for (item, map) in self.items.iter() { number = item.number.unwrap_or(number); + cells.push(LayoutNode::default()); - cells - .push(label.resolve(ctx, L, number)?.styled_with_map(map.clone()).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; @@ -155,7 +161,9 @@ impl<const L: ListKind> Show for ListNode<L> { } } - Ok(realized.spaced(above, below)) + Ok(realized + .role(Role::List { ordered: L == ORDERED }) + .spaced(above, below)) } } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index cd70db30..0f74fc96 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -52,7 +52,11 @@ impl Show for TableNode { Self { tracks: self.tracks.clone(), gutter: self.gutter.clone(), - cells: self.cells.iter().map(|cell| cell.unguard(sel)).collect(), + cells: self + .cells + .iter() + .map(|cell| cell.unguard(sel).role(Role::TableCell)) + .collect(), } .pack() } @@ -100,7 +104,8 @@ impl Show for TableNode { tracks: self.tracks.clone(), gutter: self.gutter.clone(), cells, - })) + }) + .role(Role::Table)) } fn finalize( diff --git a/src/library/text/par.rs b/src/library/text/par.rs index 695d8066..41246b00 100644 --- a/src/library/text/par.rs +++ b/src/library/text/par.rs @@ -551,11 +551,14 @@ 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 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)); @@ -1063,6 +1066,7 @@ fn stack( let mut finished = vec![]; let mut first = true; let mut output = Frame::new(Size::with_x(width)); + output.apply_role(Role::Paragraph); // Stack the lines into one frame per region. for line in lines { @@ -1072,6 +1076,7 @@ fn stack( while !regions.first.y.fits(height) && !regions.in_last() { finished.push(Arc::new(output)); output = Frame::new(Size::with_x(width)); + output.apply_role(Role::Paragraph); regions.next(); first = true; } diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index a24d2170..31db811a 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -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)) } } |
