mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Minor changes to columns
This commit is contained in:
parent
37328f11ed
commit
7f7e14d95f
@ -43,18 +43,19 @@ impl Layout for ColumnsNode {
|
|||||||
return self.child.layout(ctx, regions);
|
return self.child.layout(ctx, regions);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All gutters in the document. (Can be different because the relative
|
// Gutter width for each region. (Can be different because the relative
|
||||||
// component is calculated seperately for each region.)
|
// component is calculated seperately for each region.)
|
||||||
let mut gutters = vec![];
|
let mut gutters = vec![];
|
||||||
// Sizes of all columns resulting from `region.current` and
|
|
||||||
// `region.backlog`.
|
// Sizes of all columns resulting from `region.current`,
|
||||||
|
// `region.backlog` and `regions.last`.
|
||||||
let mut sizes = vec![];
|
let mut sizes = vec![];
|
||||||
|
|
||||||
let columns = self.columns.get();
|
let columns = self.columns.get();
|
||||||
|
|
||||||
for (current, base) in std::iter::once((regions.current, regions.base))
|
for (current, base) in regions
|
||||||
.chain(regions.backlog.as_slice().iter().map(|&s| (s, s)))
|
.iter()
|
||||||
.chain(regions.last.iter().map(|&s| (s, s)))
|
.take(1 + regions.backlog.len() + if regions.last.is_some() { 1 } else { 0 })
|
||||||
{
|
{
|
||||||
let gutter = self.gutter.resolve(base.x);
|
let gutter = self.gutter.resolve(base.x);
|
||||||
gutters.push(gutter);
|
gutters.push(gutter);
|
||||||
@ -68,9 +69,11 @@ impl Layout for ColumnsNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let first = sizes.remove(0);
|
let first = sizes.remove(0);
|
||||||
let mut pod =
|
let mut pod = Regions::one(
|
||||||
Regions::one(first, Size::new(first.x, regions.base.y), regions.expand);
|
first,
|
||||||
pod.expand.x = true;
|
Size::new(first.x, regions.base.y),
|
||||||
|
Spec::new(true, regions.expand.y),
|
||||||
|
);
|
||||||
|
|
||||||
// Retrieve elements for the last region from the vectors.
|
// Retrieve elements for the last region from the vectors.
|
||||||
let last_gutter = if regions.last.is_some() {
|
let last_gutter = if regions.last.is_some() {
|
||||||
@ -84,24 +87,18 @@ impl Layout for ColumnsNode {
|
|||||||
|
|
||||||
pod.backlog = sizes.into_iter();
|
pod.backlog = sizes.into_iter();
|
||||||
|
|
||||||
let frames = self.child.layout(ctx, &pod);
|
let mut frames = self.child.layout(ctx, &pod).into_iter();
|
||||||
|
|
||||||
let dir = ctx.styles.get(ParNode::DIR);
|
let dir = ctx.styles.get(ParNode::DIR);
|
||||||
|
|
||||||
let to = |cursor: Length, width: Length, regions: &Regions| {
|
let mut finished = vec![];
|
||||||
if dir.is_positive() {
|
|
||||||
cursor
|
|
||||||
} else {
|
|
||||||
regions.current.x - cursor - width
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut cursor = Length::zero();
|
|
||||||
|
|
||||||
let mut frames = frames.into_iter();
|
|
||||||
let mut res = vec![];
|
|
||||||
let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize;
|
let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize;
|
||||||
|
|
||||||
for (i, (current, base)) in regions.iter().take(total_regions).enumerate() {
|
for ((current, base), gutter) in regions
|
||||||
|
.iter()
|
||||||
|
.take(total_regions)
|
||||||
|
.zip(gutters.into_iter().chain(last_gutter.into_iter().cycle()))
|
||||||
|
{
|
||||||
// The height should be the parent height if the node shall expand.
|
// The height should be the parent height if the node shall expand.
|
||||||
// Otherwise its the maximum column height for the frame. In that
|
// Otherwise its the maximum column height for the frame. In that
|
||||||
// case, the frame is first created with zero height and then
|
// case, the frame is first created with zero height and then
|
||||||
@ -109,6 +106,8 @@ impl Layout for ColumnsNode {
|
|||||||
let mut height = if regions.expand.y { current.y } else { Length::zero() };
|
let mut height = if regions.expand.y { current.y } else { Length::zero() };
|
||||||
let mut frame = Frame::new(Spec::new(regions.current.x, height));
|
let mut frame = Frame::new(Spec::new(regions.current.x, height));
|
||||||
|
|
||||||
|
let mut cursor = Length::zero();
|
||||||
|
|
||||||
for _ in 0 .. columns {
|
for _ in 0 .. columns {
|
||||||
let child_frame = match frames.next() {
|
let child_frame = match frames.next() {
|
||||||
Some(frame) => frame.item,
|
Some(frame) => frame.item,
|
||||||
@ -118,16 +117,19 @@ impl Layout for ColumnsNode {
|
|||||||
let width = child_frame.size.x;
|
let width = child_frame.size.x;
|
||||||
|
|
||||||
if !regions.expand.y {
|
if !regions.expand.y {
|
||||||
height = height.max(child_frame.size.y);
|
height.set_max(child_frame.size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.push_frame(
|
frame.push_frame(
|
||||||
Point::new(to(cursor, width, ®ions), Length::zero()),
|
Point::with_x(if dir.is_positive() {
|
||||||
|
cursor
|
||||||
|
} else {
|
||||||
|
regions.current.x - cursor - width
|
||||||
|
}),
|
||||||
child_frame,
|
child_frame,
|
||||||
);
|
);
|
||||||
|
|
||||||
cursor += width
|
cursor += width + gutter;
|
||||||
+ gutters.get(i).copied().unwrap_or_else(|| last_gutter.unwrap());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.size.y = height;
|
frame.size.y = height;
|
||||||
@ -135,10 +137,9 @@ impl Layout for ColumnsNode {
|
|||||||
let mut cts = Constraints::new(regions.expand);
|
let mut cts = Constraints::new(regions.expand);
|
||||||
cts.base = base.map(Some);
|
cts.base = base.map(Some);
|
||||||
cts.exact = current.map(Some);
|
cts.exact = current.map(Some);
|
||||||
res.push(frame.constrain(cts));
|
finished.push(frame.constrain(cts));
|
||||||
cursor = Length::zero();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
finished
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ D
|
|||||||
Arbitrary horizontal growth.
|
Arbitrary horizontal growth.
|
||||||
|
|
||||||
---
|
---
|
||||||
// Test columns in an infinitely wide frame.
|
// Test columns in an infinitely high frame.
|
||||||
#set page(width: 7.05cm, columns: 2)
|
#set page(width: 7.05cm, columns: 2)
|
||||||
|
|
||||||
There can be as much content as you want in the left column
|
There can be as much content as you want in the left column
|
||||||
@ -102,7 +102,3 @@ This is a normal page. Very normal.
|
|||||||
// Test a page with zero columns.
|
// Test a page with zero columns.
|
||||||
// Error: 49-50 must be positive
|
// Error: 49-50 must be positive
|
||||||
#set page(height: auto, width: 7.05cm, columns: 0)
|
#set page(height: auto, width: 7.05cm, columns: 0)
|
||||||
|
|
||||||
This makes less sense.
|
|
||||||
|
|
||||||
// colbreak in auto stroke box on sized page that should be higher than box
|
|
Loading…
x
Reference in New Issue
Block a user