mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Simplify decoration handling
This commit is contained in:
parent
a493b9533a
commit
19e17cc6ac
@ -26,8 +26,8 @@ pub struct Frame {
|
|||||||
pub enum FrameChild {
|
pub enum FrameChild {
|
||||||
/// A leaf node in the frame tree.
|
/// A leaf node in the frame tree.
|
||||||
Element(Element),
|
Element(Element),
|
||||||
/// An interior node with an optional index.
|
/// An interior group.
|
||||||
Frame(Option<usize>, Rc<Frame>),
|
Group(Rc<Frame>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The building block frames are composed of.
|
/// The building block frames are composed of.
|
||||||
@ -110,12 +110,7 @@ impl Frame {
|
|||||||
|
|
||||||
/// Add a frame element.
|
/// Add a frame element.
|
||||||
pub fn push_frame(&mut self, pos: Point, subframe: Rc<Self>) {
|
pub fn push_frame(&mut self, pos: Point, subframe: Rc<Self>) {
|
||||||
self.children.push((pos, FrameChild::Frame(None, subframe)))
|
self.children.push((pos, FrameChild::Group(subframe)))
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a frame element with an index of arbitrary use.
|
|
||||||
pub fn push_indexed_frame(&mut self, pos: Point, index: usize, subframe: Rc<Self>) {
|
|
||||||
self.children.push((pos, FrameChild::Frame(Some(index), subframe)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add all elements of another frame, placing them relative to the given
|
/// Add all elements of another frame, placing them relative to the given
|
||||||
@ -152,7 +147,7 @@ impl<'a> Iterator for Elements<'a> {
|
|||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let (cursor, offset, frame) = self.stack.last_mut()?;
|
let (cursor, offset, frame) = self.stack.last_mut()?;
|
||||||
match frame.children.get(*cursor) {
|
match frame.children.get(*cursor) {
|
||||||
Some((pos, FrameChild::Frame(_, f))) => {
|
Some((pos, FrameChild::Group(f))) => {
|
||||||
let new_offset = *offset + *pos;
|
let new_offset = *offset + *pos;
|
||||||
self.stack.push((0, new_offset, f.as_ref()));
|
self.stack.push((0, new_offset, f.as_ref()));
|
||||||
self.next()
|
self.next()
|
||||||
@ -194,7 +189,7 @@ impl Debug for FrameChild {
|
|||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Element(element) => element.fmt(f),
|
Self::Element(element) => element.fmt(f),
|
||||||
Self::Frame(_, frame) => frame.fmt(f),
|
Self::Group(frame) => frame.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ impl Layout for ParNode {
|
|||||||
let layouter = ParLayouter::new(self, ctx, regions, bidi);
|
let layouter = ParLayouter::new(self, ctx, regions, bidi);
|
||||||
|
|
||||||
// Find suitable linebreaks.
|
// Find suitable linebreaks.
|
||||||
layouter.layout(ctx, &self.children, regions.clone())
|
layouter.layout(ctx, regions.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,25 +131,25 @@ impl<'a> ParLayouter<'a> {
|
|||||||
let mut ranges = vec![];
|
let mut ranges = vec![];
|
||||||
|
|
||||||
// Layout the children and collect them into items.
|
// Layout the children and collect them into items.
|
||||||
for (i, (range, child)) in par.ranges().zip(&par.children).enumerate() {
|
for (range, child) in par.ranges().zip(&par.children) {
|
||||||
match *child {
|
match child {
|
||||||
ParChild::Spacing(amount) => {
|
ParChild::Spacing(amount) => {
|
||||||
let resolved = amount.resolve(regions.current.w);
|
let resolved = amount.resolve(regions.current.w);
|
||||||
items.push(ParItem::Spacing(resolved));
|
items.push(ParItem::Spacing(resolved));
|
||||||
ranges.push(range);
|
ranges.push(range);
|
||||||
}
|
}
|
||||||
ParChild::Text(_, align, ref state, _) => {
|
ParChild::Text(_, align, state, decos) => {
|
||||||
// TODO: Also split by language and script.
|
// TODO: Also split by language and script.
|
||||||
for (subrange, dir) in split_runs(&bidi, range) {
|
for (subrange, dir) in split_runs(&bidi, range) {
|
||||||
let text = &bidi.text[subrange.clone()];
|
let text = &bidi.text[subrange.clone()];
|
||||||
let shaped = shape(ctx, text, dir, state);
|
let shaped = shape(ctx, text, dir, state);
|
||||||
items.push(ParItem::Text(shaped, align, i));
|
items.push(ParItem::Text(shaped, *align, decos));
|
||||||
ranges.push(subrange);
|
ranges.push(subrange);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParChild::Any(ref node, align, _) => {
|
ParChild::Any(node, align, decos) => {
|
||||||
let frame = node.layout(ctx, regions).remove(0);
|
let frame = node.layout(ctx, regions).remove(0);
|
||||||
items.push(ParItem::Frame(frame.item, align, i));
|
items.push(ParItem::Frame(frame.item, *align, decos));
|
||||||
ranges.push(range);
|
ranges.push(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,10 +168,9 @@ impl<'a> ParLayouter<'a> {
|
|||||||
fn layout(
|
fn layout(
|
||||||
self,
|
self,
|
||||||
ctx: &mut LayoutContext,
|
ctx: &mut LayoutContext,
|
||||||
children: &[ParChild],
|
|
||||||
regions: Regions,
|
regions: Regions,
|
||||||
) -> Vec<Constrained<Rc<Frame>>> {
|
) -> Vec<Constrained<Rc<Frame>>> {
|
||||||
let mut stack = LineStack::new(self.line_spacing, children, regions);
|
let mut stack = LineStack::new(self.line_spacing, regions);
|
||||||
|
|
||||||
// The current line attempt.
|
// The current line attempt.
|
||||||
// Invariant: Always fits into `stack.regions.current`.
|
// Invariant: Always fits into `stack.regions.current`.
|
||||||
@ -290,9 +289,9 @@ enum ParItem<'a> {
|
|||||||
/// Spacing between other items.
|
/// Spacing between other items.
|
||||||
Spacing(Length),
|
Spacing(Length),
|
||||||
/// A shaped text run with consistent direction.
|
/// A shaped text run with consistent direction.
|
||||||
Text(ShapedText<'a>, Align, usize),
|
Text(ShapedText<'a>, Align, &'a [Decoration]),
|
||||||
/// A layouted child node.
|
/// A layouted child node.
|
||||||
Frame(Rc<Frame>, Align, usize),
|
Frame(Rc<Frame>, Align, &'a [Decoration]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParItem<'_> {
|
impl ParItem<'_> {
|
||||||
@ -313,113 +312,6 @@ impl ParItem<'_> {
|
|||||||
Self::Frame(frame, ..) => frame.baseline,
|
Self::Frame(frame, ..) => frame.baseline,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The index of the `ParChild` that this item belongs to.
|
|
||||||
pub fn index(&self) -> Option<usize> {
|
|
||||||
match *self {
|
|
||||||
Self::Spacing(_) => None,
|
|
||||||
Self::Text(.., index) => Some(index),
|
|
||||||
Self::Frame(.., index) => Some(index),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stacks lines on top of each other.
|
|
||||||
struct LineStack<'a> {
|
|
||||||
line_spacing: Length,
|
|
||||||
children: &'a [ParChild],
|
|
||||||
full: Size,
|
|
||||||
regions: Regions,
|
|
||||||
size: Size,
|
|
||||||
lines: Vec<LineLayout<'a>>,
|
|
||||||
finished: Vec<Constrained<Rc<Frame>>>,
|
|
||||||
constraints: Constraints,
|
|
||||||
overflowing: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> LineStack<'a> {
|
|
||||||
/// Create an empty line stack.
|
|
||||||
fn new(line_spacing: Length, children: &'a [ParChild], regions: Regions) -> Self {
|
|
||||||
Self {
|
|
||||||
line_spacing,
|
|
||||||
children,
|
|
||||||
full: regions.current,
|
|
||||||
constraints: Constraints::new(regions.expand),
|
|
||||||
regions,
|
|
||||||
size: Size::zero(),
|
|
||||||
lines: vec![],
|
|
||||||
finished: vec![],
|
|
||||||
overflowing: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Push a new line into the stack.
|
|
||||||
fn push(&mut self, line: LineLayout<'a>) {
|
|
||||||
self.regions.current.h -= line.size.h + self.line_spacing;
|
|
||||||
|
|
||||||
self.size.w.set_max(line.size.w);
|
|
||||||
self.size.h += line.size.h;
|
|
||||||
if !self.lines.is_empty() {
|
|
||||||
self.size.h += self.line_spacing;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.lines.push(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finish the frame for one region.
|
|
||||||
fn finish_region(&mut self, ctx: &LayoutContext) {
|
|
||||||
if self.regions.expand.x {
|
|
||||||
self.size.w = self.regions.current.w;
|
|
||||||
self.constraints.exact.x = Some(self.regions.current.w);
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.overflowing {
|
|
||||||
self.constraints.min.y = None;
|
|
||||||
self.constraints.max.y = None;
|
|
||||||
self.constraints.exact = self.full.to_spec().map(Some);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut output = Frame::new(self.size, self.size.h);
|
|
||||||
let mut offset = Length::zero();
|
|
||||||
let mut first = true;
|
|
||||||
|
|
||||||
for line in self.lines.drain(..) {
|
|
||||||
let frame = line.build(self.size.w);
|
|
||||||
|
|
||||||
let pos = Point::new(Length::zero(), offset);
|
|
||||||
if first {
|
|
||||||
output.baseline = pos.y + frame.baseline;
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
offset += frame.size.h + self.line_spacing;
|
|
||||||
output.merge_frame(pos, frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (_, child) in &mut output.children {
|
|
||||||
if let FrameChild::Frame(Some(frame_idx), frame) = child {
|
|
||||||
for deco in match &self.children[*frame_idx] {
|
|
||||||
ParChild::Spacing(_) => continue,
|
|
||||||
ParChild::Text(.., decos) => decos,
|
|
||||||
ParChild::Any(.., decos) => decos,
|
|
||||||
} {
|
|
||||||
deco.apply(ctx, Rc::make_mut(frame));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.finished.push(output.constrain(self.constraints));
|
|
||||||
self.regions.next();
|
|
||||||
self.full = self.regions.current;
|
|
||||||
self.constraints = Constraints::new(self.regions.expand);
|
|
||||||
self.size = Size::zero();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finish the last region and return the built frames.
|
|
||||||
fn finish(mut self, ctx: &LayoutContext) -> Vec<Constrained<Rc<Frame>>> {
|
|
||||||
self.finish_region(ctx);
|
|
||||||
self.finished
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A lightweight representation of a line that spans a specific range in a
|
/// A lightweight representation of a line that spans a specific range in a
|
||||||
@ -537,7 +429,7 @@ impl<'a> LineLayout<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Build the line's frame.
|
/// Build the line's frame.
|
||||||
fn build(&self, width: Length) -> Frame {
|
fn build(&self, ctx: &LayoutContext, width: Length) -> Frame {
|
||||||
let size = Size::new(self.size.w.max(width), self.size.h);
|
let size = Size::new(self.size.w.max(width), self.size.h);
|
||||||
let free = size.w - self.size.w;
|
let free = size.w - self.size.w;
|
||||||
|
|
||||||
@ -545,33 +437,36 @@ impl<'a> LineLayout<'a> {
|
|||||||
let mut offset = Length::zero();
|
let mut offset = Length::zero();
|
||||||
let mut ruler = Align::Start;
|
let mut ruler = Align::Start;
|
||||||
|
|
||||||
self.reordered(|item| {
|
self.reordered(ctx, |ctx, item| {
|
||||||
let frame = match *item {
|
let mut position = |frame: &Frame, align| {
|
||||||
ParItem::Spacing(amount) => {
|
// FIXME: Ruler alignment for RTL.
|
||||||
offset += amount;
|
ruler = ruler.max(align);
|
||||||
return;
|
let x = ruler.resolve(self.dir, offset .. free + offset);
|
||||||
}
|
let y = self.baseline - frame.baseline;
|
||||||
ParItem::Text(ref shaped, align, _) => {
|
offset += frame.size.w;
|
||||||
ruler = ruler.max(align);
|
Point::new(x, y)
|
||||||
Rc::new(shaped.build())
|
|
||||||
}
|
|
||||||
ParItem::Frame(ref frame, align, _) => {
|
|
||||||
ruler = ruler.max(align);
|
|
||||||
frame.clone()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: Ruler alignment for RTL.
|
match *item {
|
||||||
let pos = Point::new(
|
ParItem::Spacing(amount) => {
|
||||||
ruler.resolve(self.dir, offset .. free + offset),
|
offset += amount;
|
||||||
self.baseline - frame.baseline,
|
}
|
||||||
);
|
ParItem::Text(ref shaped, align, decos) => {
|
||||||
|
let mut frame = shaped.build();
|
||||||
offset += frame.size.w;
|
for deco in decos {
|
||||||
|
deco.apply(ctx, &mut frame);
|
||||||
match item.index() {
|
}
|
||||||
Some(idx) => output.push_indexed_frame(pos, idx, frame),
|
let pos = position(&frame, align);
|
||||||
None => output.push_frame(pos, frame),
|
output.merge_frame(pos, frame);
|
||||||
|
}
|
||||||
|
ParItem::Frame(ref frame, align, decos) => {
|
||||||
|
let mut frame = frame.clone();
|
||||||
|
for deco in decos {
|
||||||
|
deco.apply(ctx, Rc::make_mut(&mut frame));
|
||||||
|
}
|
||||||
|
let pos = position(&frame, align);
|
||||||
|
output.push_frame(pos, frame);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -579,7 +474,10 @@ impl<'a> LineLayout<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate through the line's items in visual order.
|
/// Iterate through the line's items in visual order.
|
||||||
fn reordered(&self, mut f: impl FnMut(&ParItem<'a>)) {
|
fn reordered<F>(&self, ctx: &LayoutContext, mut f: F)
|
||||||
|
where
|
||||||
|
F: FnMut(&LayoutContext, &ParItem<'a>),
|
||||||
|
{
|
||||||
// The bidi crate doesn't like empty lines.
|
// The bidi crate doesn't like empty lines.
|
||||||
if self.line.is_empty() {
|
if self.line.is_empty() {
|
||||||
return;
|
return;
|
||||||
@ -606,11 +504,11 @@ impl<'a> LineLayout<'a> {
|
|||||||
// direction.
|
// direction.
|
||||||
if levels[run.start].is_ltr() {
|
if levels[run.start].is_ltr() {
|
||||||
for item in range {
|
for item in range {
|
||||||
f(self.get(item).unwrap());
|
f(ctx, self.get(item).unwrap());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for item in range.rev() {
|
for item in range.rev() {
|
||||||
f(self.get(item).unwrap());
|
f(ctx, self.get(item).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -627,6 +525,90 @@ impl<'a> LineLayout<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stacks lines on top of each other.
|
||||||
|
struct LineStack<'a> {
|
||||||
|
line_spacing: Length,
|
||||||
|
full: Size,
|
||||||
|
regions: Regions,
|
||||||
|
size: Size,
|
||||||
|
lines: Vec<LineLayout<'a>>,
|
||||||
|
finished: Vec<Constrained<Rc<Frame>>>,
|
||||||
|
constraints: Constraints,
|
||||||
|
overflowing: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> LineStack<'a> {
|
||||||
|
/// Create an empty line stack.
|
||||||
|
fn new(line_spacing: Length, regions: Regions) -> Self {
|
||||||
|
Self {
|
||||||
|
line_spacing,
|
||||||
|
full: regions.current,
|
||||||
|
constraints: Constraints::new(regions.expand),
|
||||||
|
regions,
|
||||||
|
size: Size::zero(),
|
||||||
|
lines: vec![],
|
||||||
|
finished: vec![],
|
||||||
|
overflowing: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Push a new line into the stack.
|
||||||
|
fn push(&mut self, line: LineLayout<'a>) {
|
||||||
|
self.regions.current.h -= line.size.h + self.line_spacing;
|
||||||
|
|
||||||
|
self.size.w.set_max(line.size.w);
|
||||||
|
self.size.h += line.size.h;
|
||||||
|
if !self.lines.is_empty() {
|
||||||
|
self.size.h += self.line_spacing;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.lines.push(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finish the frame for one region.
|
||||||
|
fn finish_region(&mut self, ctx: &LayoutContext) {
|
||||||
|
if self.regions.expand.x {
|
||||||
|
self.size.w = self.regions.current.w;
|
||||||
|
self.constraints.exact.x = Some(self.regions.current.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.overflowing {
|
||||||
|
self.constraints.min.y = None;
|
||||||
|
self.constraints.max.y = None;
|
||||||
|
self.constraints.exact = self.full.to_spec().map(Some);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut output = Frame::new(self.size, self.size.h);
|
||||||
|
let mut offset = Length::zero();
|
||||||
|
let mut first = true;
|
||||||
|
|
||||||
|
for line in self.lines.drain(..) {
|
||||||
|
let frame = line.build(ctx, self.size.w);
|
||||||
|
|
||||||
|
let pos = Point::new(Length::zero(), offset);
|
||||||
|
if first {
|
||||||
|
output.baseline = pos.y + frame.baseline;
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += frame.size.h + self.line_spacing;
|
||||||
|
output.merge_frame(pos, frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.finished.push(output.constrain(self.constraints));
|
||||||
|
self.regions.next();
|
||||||
|
self.full = self.regions.current;
|
||||||
|
self.constraints = Constraints::new(self.regions.expand);
|
||||||
|
self.size = Size::zero();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finish the last region and return the built frames.
|
||||||
|
fn finish(mut self, ctx: &LayoutContext) -> Vec<Constrained<Rc<Frame>>> {
|
||||||
|
self.finish_region(ctx);
|
||||||
|
self.finished
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A decoration for a paragraph child.
|
/// A decoration for a paragraph child.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||||
pub enum Decoration {
|
pub enum Decoration {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user