oxedyne/fe2o3/fe2o3_austenite/src/flow/decorate.rs
7.8 KiB, 1 run
created by r1870400018:63520, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Original work copyright (c) the Typst project authors (typst-layout `pages/{run,finalize}.rs`, version 0.15.1). |
| 3 | // Modified for Austenite: ported to Hematite's types, IR and error handling. |
| 4 | // U9 owns this file: page furniture. The header, footer, background and foreground are evaluated once per |
| 5 | // page per pass, laid out beside the body, with `here()` at the page; they sit outside the body flow, so |
| 6 | // they never feed back into pagination. |
| 7 | // |
| 8 | // A port of Typst 0.15.1's page-run marginals. Each is laid out with the page's own styles, expanding to |
| 9 | // its area and aligned in it: the header to the bottom of the strip above the body (the top margin less |
| 10 | // `header-ascent`), the footer to the top of the strip below it (the bottom margin less `footer-descent`), |
| 11 | // and the background and foreground to the centre of the whole page. An `auto` header or footer holds the |
| 12 | // page number when `numbering` is set, a `counter(page).display(numbering)` aligned by `number-align`, |
| 13 | // whose `y` picks the header or the footer; `none` clears the slot. The frames are placed by |
| 14 | // [`crate::driver::place_page`] in Typst's order (background, header, body, footer, foreground), so the |
| 15 | // located elements of the furniture are recorded in that order and the introspector reads them as it does. |
| 16 | // |
| 17 | // Bleed is not drawn: a page with a bleed warns, and its marginals are laid to the trimmed page. |
| 18 | |
| 19 | use crate::eval::args::Args; |
| 20 | use crate::eval::content::{ |
| 21 | Content, |
| 22 | ElemKind, |
| 23 | }; |
| 24 | use crate::eval::func::{ |
| 25 | Func, |
| 26 | NativeFunc, |
| 27 | }; |
| 28 | use crate::eval::intro::Counter; |
| 29 | use crate::eval::lib::intro::IntroFn; |
| 30 | use crate::eval::lib::numbering::Pattern; |
| 31 | use crate::eval::styles::{ |
| 32 | Property, |
| 33 | Style, |
| 34 | StyleChain, |
| 35 | Styles, |
| 36 | }; |
| 37 | use crate::eval::value::{ |
| 38 | Alignment, |
| 39 | HAlign, |
| 40 | VAlign, |
| 41 | Value, |
| 42 | }; |
| 43 | use crate::eval::Engine; |
| 44 | use crate::flow::block::{ |
| 45 | self, |
| 46 | Frame, |
| 47 | Regions, |
| 48 | }; |
| 49 | use crate::flow::page::{ |
| 50 | page_field, |
| 51 | rel, |
| 52 | }; |
| 53 | use crate::flow::{ |
| 54 | RunSetup, |
| 55 | Slot, |
| 56 | }; |
| 57 | use crate::syntax::Span; |
| 58 | |
| 59 | use oxedyne_fe2o3_core::prelude::*; |
| 60 | |
| 61 | use std::sync::Arc; |
| 62 | |
| 63 | // Where Typst has the default `number-align` (`center + bottom`) and the marginals' own alignments. |
| 64 | const NUMBER_ALIGN: Alignment = Alignment { x: Some(HAlign::Center), y: Some(VAlign::Bottom) }; |
| 65 | const HEADER_ALIGN: Alignment = Alignment { x: None, y: Some(VAlign::Bottom) }; |
| 66 | const FOOTER_ALIGN: Alignment = Alignment { x: None, y: Some(VAlign::Top) }; |
| 67 | const MIDDLE: Alignment = Alignment { x: Some(HAlign::Center), y: Some(VAlign::Horizon) }; |
| 68 | |
| 69 | /// A page's laid-out furniture. Each frame is as large as its area, so a footer stands at the page's foot |
| 70 | /// by `foot_h`, its area's height, whatever it holds. |
| 71 | #[derive(Debug, Default)] |
| 72 | pub struct Marginals { |
| 73 | pub background: Option<Frame>, |
| 74 | pub header: Option<Frame>, |
| 75 | pub footer: Option<Frame>, |
| 76 | pub foreground: Option<Frame>, |
| 77 | pub foot_h: f64, // the footer area's height: the bottom margin less `footer-descent` |
| 78 | } |
| 79 | |
| 80 | impl Marginals { |
| 81 | /// Does the page have no furniture? |
| 82 | pub fn is_empty(&self) -> bool { |
| 83 | self.background.is_none() && self.header.is_none() && self.footer.is_none() && self.foreground.is_none() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Lays out a page's furniture. `inner` is the size of the page's body frame, inside the margins, which the |
| 88 | /// header and footer span and the background and foreground exceed by the margins. The page number, where |
| 89 | /// there is one, is read in a context at the footer's own location, from the introspector the previous pass |
| 90 | /// built, and the reads are recorded for the fixpoint like any other. |
| 91 | pub fn decorate_page(engine: &mut Engine, setup: &RunSetup, inner: (f64, f64)) -> Outcome<Marginals> { |
| 92 | let styles = &setup.styles; |
| 93 | let geom = &setup.geom; |
| 94 | let (top, bottom, left, right) = (geom.top.to_pt(), geom.bottom.to_pt(), geom.inside.to_pt(), geom.outside.to_pt()); |
| 95 | let ascent = res!(strip(styles, "header-ascent", top)); |
| 96 | let descent = res!(strip(styles, "footer-descent", bottom)); |
| 97 | let align = match res!(page_field(styles, "number-align")) { |
| 98 | Some(Value::Alignment(a)) => a, |
| 99 | _ => NUMBER_ALIGN, |
| 100 | }; |
| 101 | let numbered = res!(numbering_marginal(&setup.numbering, align)); |
| 102 | // `number-align`'s vertical part picks the header or the footer to carry the number. |
| 103 | let (header, footer) = match align.y { |
| 104 | Some(VAlign::Top) => (pick(&setup.header, numbered), pick(&setup.footer, None)), |
| 105 | _ => (pick(&setup.header, None), pick(&setup.footer, numbered)), |
| 106 | }; |
| 107 | let head_h = (top - ascent).max(0.0); |
| 108 | let foot_h = (bottom - descent).max(0.0); |
| 109 | let (full_w, full_h) = (inner.0 + left + right, inner.1 + top + bottom); |
| 110 | let mut out = Marginals { foot_h, ..Marginals::default() }; |
| 111 | if let Some(c) = &header { |
| 112 | out.header = Some(res!(marginal(engine, c, HEADER_ALIGN, styles, inner.0, head_h))); |
| 113 | } |
| 114 | if let Some(c) = &footer { |
| 115 | out.footer = Some(res!(marginal(engine, c, FOOTER_ALIGN, styles, inner.0, foot_h))); |
| 116 | } |
| 117 | if let Some(c) = &setup.background { |
| 118 | out.background = Some(res!(marginal(engine, c, MIDDLE, styles, full_w, full_h))); |
| 119 | } |
| 120 | if let Some(c) = &setup.foreground { |
| 121 | out.foreground = Some(res!(marginal(engine, c, MIDDLE, styles, full_w, full_h))); |
| 122 | } |
| 123 | Ok(out) |
| 124 | } |
| 125 | |
| 126 | /// A margin strip's depth: the `header-ascent` or `footer-descent` taken from its margin. |
| 127 | fn strip(styles: &StyleChain, field: &str, margin: f64) -> Outcome<f64> { |
| 128 | Ok(match res!(page_field(styles, field)) { |
| 129 | Some(v) => rel(styles, &v).map(|r| r.relative_to(margin)).unwrap_or(0.3 * margin), |
| 130 | None => 0.3 * margin, |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | /// The content a slot holds: its own, none, or the page number's when it is left `auto`. |
| 135 | fn pick(slot: &Slot, auto: Option<Content>) -> Option<Content> { |
| 136 | match slot { |
| 137 | Slot::Auto => auto, |
| 138 | Slot::Off => None, |
| 139 | Slot::On(c) => Some(c.clone()), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// The page number as Typst builds it: `align(x)[#context counter(page).display(numbering, both:)]`, |
| 144 | /// `both` when the pattern has two counting symbols (or the numbering is a function), so `"1 / 1"` shows |
| 145 | /// the page and the total. A numbering that is not a pattern or a function has no marginal. |
| 146 | fn numbering_marginal(numbering: &Value, align: Alignment) -> Outcome<Option<Content>> { |
| 147 | let both = match numbering { |
| 148 | Value::Str(s) => Pattern::parse(s).map(|p| p.pieces.len() >= 2).unwrap_or(false), |
| 149 | Value::Func(_) => true, |
| 150 | _ => return Ok(None), |
| 151 | }; |
| 152 | let span = Span::detached(); |
| 153 | let mut args = Args::new(span); |
| 154 | args.push(span, Value::Counter(Arc::new(Counter::page()))); |
| 155 | args.push(span, numbering.clone()); |
| 156 | args.push_named(span, "both", Value::Bool(both)); |
| 157 | let show = Func::Native(NativeFunc::Intro(IntroFn::CounterDisplay)).with(args); |
| 158 | let func = res!(ElemKind::Context.field_id("func") |
| 159 | .ok_or_else(|| err!("The context element has no func field."; Bug))); |
| 160 | let context = Content::new(ElemKind::Context, vec![(func, Value::Func(show))], span); |
| 161 | let body = res!(ElemKind::Align.field_id("body") |
| 162 | .ok_or_else(|| err!("The align element has no body field."; Bug))); |
| 163 | let at = res!(ElemKind::Align.field_id("alignment") |
| 164 | .ok_or_else(|| err!("The align element has no alignment field."; Bug))); |
| 165 | // Only the horizontal part of `number-align` aligns the number; the vertical part chose the slot. |
| 166 | Ok(Some(match align.x { |
| 167 | Some(x) => Content::new(ElemKind::Align, vec![ |
| 168 | (body, Value::Content(context)), |
| 169 | (at, Value::Alignment(Alignment { x: Some(x), y: None })), |
| 170 | ], span), |
| 171 | None => context, |
| 172 | })) |
| 173 | } |
| 174 | |
| 175 | /// One marginal laid out as Typst's `layout_marginal`: its content aligned by a property set on it, into a |
| 176 | /// region of `w` by `h` that it expands to fill. |
| 177 | fn marginal( |
| 178 | engine: &mut Engine, |
| 179 | content: &Content, |
| 180 | align: Alignment, |
| 181 | styles: &StyleChain, |
| 182 | w: f64, |
| 183 | h: f64, |
| 184 | ) |
| 185 | -> Outcome<Frame> |
| 186 | { |
| 187 | let at = res!(ElemKind::Align.field_id("alignment") |
| 188 | .ok_or_else(|| err!("The align element has no alignment field."; Bug))); |
| 189 | let set = Style::Property(Property::new(ElemKind::Align, at, Value::Alignment(align), content.span())); |
| 190 | let aligned = content.clone().styled(Styles::from_style(set)); |
| 191 | block::layout_frame(engine, &aligned, styles, Regions::one(w, h, true, true)) |
| 192 | } |