Make percentages for h and v relative to area instead of font size

This commit is contained in:
Laurenz 2021-08-17 00:17:28 +02:00
parent 9462fb17b3
commit 9a798ce6f6
7 changed files with 22 additions and 19 deletions

View File

@ -93,7 +93,7 @@ impl ExecContext {
} }
/// Push spacing into the active paragraph or stack depending on the `axis`. /// Push spacing into the active paragraph or stack depending on the `axis`.
pub fn push_spacing(&mut self, axis: GenAxis, amount: Length) { pub fn push_spacing(&mut self, axis: GenAxis, amount: Linear) {
match axis { match axis {
GenAxis::Main => { GenAxis::Main => {
self.stack.finish_par(&self.state); self.stack.finish_par(&self.state);
@ -114,7 +114,7 @@ impl ExecContext {
pub fn parbreak(&mut self) { pub fn parbreak(&mut self) {
let amount = self.state.par_spacing(); let amount = self.state.par_spacing();
self.stack.finish_par(&self.state); self.stack.finish_par(&self.state);
self.stack.push_soft(StackChild::Spacing(amount)); self.stack.push_soft(StackChild::Spacing(amount.into()));
} }
/// Apply a forced page break. /// Apply a forced page break.

View File

@ -119,7 +119,7 @@ fn exec_item(ctx: &mut ExecContext, label: EcoString, body: &SyntaxTree, map: &E
aspect: None, aspect: None,
children: vec![ children: vec![
StackChild::Any(label.into(), Gen::default()), StackChild::Any(label.into(), Gen::default()),
StackChild::Spacing(ctx.state.font.size / 2.0), StackChild::Spacing((ctx.state.font.size / 2.0).into()),
StackChild::Any(body.into(), Gen::default()), StackChild::Any(body.into(), Gen::default()),
], ],
}); });

View File

@ -27,7 +27,7 @@ pub struct ParNode {
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub enum ParChild { pub enum ParChild {
/// Spacing between other nodes. /// Spacing between other nodes.
Spacing(Length), Spacing(Linear),
/// A run of text and how to align it in its line. /// A run of text and how to align it in its line.
Text(EcoString, Align, Rc<FontState>), Text(EcoString, Align, Rc<FontState>),
/// Any child node and how to align it in its line. /// Any child node and how to align it in its line.
@ -137,7 +137,8 @@ impl<'a> ParLayouter<'a> {
for (range, child) in par.ranges().zip(&par.children) { for (range, child) in par.ranges().zip(&par.children) {
match *child { match *child {
ParChild::Spacing(amount) => { ParChild::Spacing(amount) => {
items.push(ParItem::Spacing(amount)); let resolved = amount.resolve(regions.current.width);
items.push(ParItem::Spacing(resolved));
ranges.push(range); ranges.push(range);
} }
ParChild::Text(_, align, ref state) => { ParChild::Text(_, align, ref state) => {

View File

@ -24,7 +24,7 @@ pub struct StackNode {
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub enum StackChild { pub enum StackChild {
/// Spacing between other nodes. /// Spacing between other nodes.
Spacing(Length), Spacing(Linear),
/// Any child node and how to align it in the stack. /// Any child node and how to align it in the stack.
Any(LayoutNode, Gen<Align>), Any(LayoutNode, Gen<Align>),
} }
@ -125,11 +125,15 @@ impl<'a> StackLayouter<'a> {
} }
/// Add main-axis spacing into the current region. /// Add main-axis spacing into the current region.
fn space(&mut self, amount: Length) { fn space(&mut self, amount: Linear) {
// Resolve the linear.
let full = self.full.get(self.main);
let resolved = amount.resolve(full);
// Cap the spacing to the remaining available space. This action does // Cap the spacing to the remaining available space. This action does
// not directly affect the constraints because of the cap. // not directly affect the constraints because of the cap.
let remaining = self.regions.current.get_mut(self.main); let remaining = self.regions.current.get_mut(self.main);
let capped = amount.min(*remaining); let capped = resolved.min(*remaining);
// Grow our size and shrink the available space in the region. // Grow our size and shrink the available space in the region.
self.used.main += capped; self.used.main += capped;

View File

@ -80,20 +80,17 @@ pub fn pagebreak(_: &mut EvalContext, _: &mut Arguments) -> TypResult<Value> {
/// `h`: Horizontal spacing. /// `h`: Horizontal spacing.
pub fn h(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { pub fn h(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
spacing_impl(args, GenAxis::Cross) let spacing = args.expect("spacing")?;
Ok(Value::template(move |ctx| {
ctx.push_spacing(GenAxis::Cross, spacing);
}))
} }
/// `v`: Vertical spacing. /// `v`: Vertical spacing.
pub fn v(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { pub fn v(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
spacing_impl(args, GenAxis::Main) let spacing = args.expect("spacing")?;
}
fn spacing_impl(args: &mut Arguments, axis: GenAxis) -> TypResult<Value> {
let spacing = args.expect::<Linear>("spacing")?;
Ok(Value::template(move |ctx| { Ok(Value::template(move |ctx| {
// TODO: Should this really always be font-size relative? ctx.push_spacing(GenAxis::Main, spacing);
let amount = spacing.resolve(ctx.state.font.size);
ctx.push_spacing(axis, amount);
})) }))
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -10,8 +10,9 @@ Inv #h(0pt) isible
// Multiple spacings in a row. // Multiple spacings in a row.
Add #h(10pt) #h(10pt) up Add #h(10pt) #h(10pt) up
// Relative to font size. // Relative to area.
Relative #h(100%) spacing #let x = 25% - 4pt
| #h(x) | #h(x) | #h(x) | #h(x) |
--- ---
// Missing spacing. // Missing spacing.