diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-13 23:19:44 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-13 23:19:44 +0100 |
| commit | 272a4c228976466e9fa6cc100ad89f93dc5cc371 (patch) | |
| tree | ad02a6e57b07da061432d58ff0ca46d6777bdb97 /src/library | |
| parent | 1b53e27f270e3c040ee095573af9a5243980191a (diff) | |
Unbounded pages 🌌
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/insert.rs | 20 | ||||
| -rw-r--r-- | src/library/layout.rs | 13 |
2 files changed, 21 insertions, 12 deletions
diff --git a/src/library/insert.rs b/src/library/insert.rs index 8d60927a..51cbbf52 100644 --- a/src/library/insert.rs +++ b/src/library/insert.rs @@ -55,8 +55,11 @@ struct NodeImage { impl Layout for NodeImage { fn layout(&self, _: &mut LayoutContext, areas: &Areas) -> Layouted { - let Area { rem, full } = areas.current; - let pixel_ratio = (self.dimensions.0 as f64) / (self.dimensions.1 as f64); + let Areas { current, full, .. } = areas; + + let pixel_width = self.dimensions.0 as f64; + let pixel_height = self.dimensions.1 as f64; + let pixel_ratio = pixel_width / pixel_height; let width = self.width.map(|w| w.resolve(full.width)); let height = self.height.map(|w| w.resolve(full.height)); @@ -66,12 +69,15 @@ impl Layout for NodeImage { (Some(width), None) => Size::new(width, width / pixel_ratio), (None, Some(height)) => Size::new(height * pixel_ratio, height), (None, None) => { - let ratio = rem.width / rem.height; - if ratio < pixel_ratio { - Size::new(rem.width, rem.width / pixel_ratio) - } else { + let ratio = current.width / current.height; + if ratio < pixel_ratio && current.width.is_finite() { + Size::new(current.width, current.width / pixel_ratio) + } else if current.height.is_finite() { // TODO: Fix issue with line spacing. - Size::new(rem.height * pixel_ratio, rem.height) + Size::new(current.height * pixel_ratio, current.height) + } else { + // Totally unbounded area, we have to make up something. + Size::new(Length::pt(pixel_width), Length::pt(pixel_height)) } } }; diff --git a/src/library/layout.rs b/src/library/layout.rs index 59c1954b..0e04c507 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -197,13 +197,12 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { let children = ctx.end_content_group(); let fill_if = |c| if c { Expansion::Fill } else { Expansion::Fit }; - let expansion = - Spec::new(fill_if(width.is_some()), fill_if(height.is_some())).switch(dirs); + let expand = Spec::new(fill_if(width.is_some()), fill_if(height.is_some())); ctx.push(NodeFixed { width, height, - child: NodeStack { dirs, align, expansion, children }.into(), + child: NodeStack { dirs, align, expand, children }.into(), }); ctx.state = snapshot; @@ -271,6 +270,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value { if let Some(paper) = Paper::from_name(&name.v) { ctx.state.page.class = paper.class; ctx.state.page.size = paper.size(); + ctx.state.page.expand = Spec::uniform(Expansion::Fill); } else { ctx.diag(error!(name.span, "invalid paper name")); } @@ -279,11 +279,13 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value { if let Some(width) = args.get(ctx, "width") { ctx.state.page.class = PaperClass::Custom; ctx.state.page.size.width = width; + ctx.state.page.expand.horizontal = Expansion::Fill; } if let Some(height) = args.get(ctx, "height") { ctx.state.page.class = PaperClass::Custom; ctx.state.page.size.height = height; + ctx.state.page.expand.vertical = Expansion::Fill; } if let Some(margins) = args.get(ctx, "margins") { @@ -307,8 +309,9 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value { } if args.get(ctx, "flip").unwrap_or(false) { - let size = &mut ctx.state.page.size; - std::mem::swap(&mut size.width, &mut size.height); + let page = &mut ctx.state.page; + std::mem::swap(&mut page.size.width, &mut page.size.height); + std::mem::swap(&mut page.expand.horizontal, &mut page.expand.vertical); } let main = args.get(ctx, "main-dir"); |
