oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/heading.rs
9.0 KiB, 6 runs
created by r1870400018:63262, 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-library `model/heading.rs`, version 0.15.1). |
| 3 | // Modified for Austenite: the element's fields, defaults and default show, ported to Hematite's types, IR and error handling. |
| 4 | // U5 owns this file: heading and title. |
| 5 | |
| 6 | use crate::diag::DiagnosticKind; |
| 7 | use crate::eval::content::{ |
| 8 | Content, |
| 9 | ElemKind, |
| 10 | FieldDefault, |
| 11 | FieldSpec, |
| 12 | FieldType, |
| 13 | }; |
| 14 | use crate::eval::lib::model::common::{ |
| 15 | self, |
| 16 | alignment_name, |
| 17 | expect, |
| 18 | expect_cast, |
| 19 | natural, |
| 20 | to_content, |
| 21 | CastErr, |
| 22 | K, |
| 23 | }; |
| 24 | use crate::eval::lib::model::lookup; |
| 25 | use crate::eval::styles::{ |
| 26 | StyleChain, |
| 27 | Styles, |
| 28 | }; |
| 29 | use crate::eval::args::Args; |
| 30 | use crate::eval::value::{ |
| 31 | Type, |
| 32 | Value, |
| 33 | }; |
| 34 | use crate::eval::Engine; |
| 35 | use crate::flow::{ |
| 36 | self, |
| 37 | Region, |
| 38 | }; |
| 39 | use crate::ir::Sp; |
| 40 | |
| 41 | use oxedyne_fe2o3_core::prelude::*; |
| 42 | |
| 43 | const ANY: FieldType = FieldType::Any; |
| 44 | |
| 45 | static HEADING: [FieldSpec; 9] = [ |
| 46 | FieldSpec::named("level", FieldType::OneOf(&[Type::Int, Type::Auto]), FieldDefault::Auto), |
| 47 | FieldSpec::named("depth", FieldType::Of(Type::Int), FieldDefault::Int(1)), |
| 48 | FieldSpec::named("offset", FieldType::Of(Type::Int), FieldDefault::Int(0)), |
| 49 | FieldSpec::named("numbering", ANY, FieldDefault::None), |
| 50 | FieldSpec::named("supplement", ANY, FieldDefault::Auto), |
| 51 | FieldSpec::named("outlined", FieldType::Of(Type::Bool), FieldDefault::Bool(true)), |
| 52 | FieldSpec::named("bookmarked", FieldType::OneOf(&[Type::Bool, Type::Auto]), FieldDefault::Auto), |
| 53 | FieldSpec::named("hanging-indent", FieldType::OneOf(&[Type::Length, Type::Auto]), FieldDefault::Auto), |
| 54 | FieldSpec::required("body", ANY), |
| 55 | ]; |
| 56 | |
| 57 | static TITLE: [FieldSpec; 1] = [ |
| 58 | FieldSpec::named("body", ANY, FieldDefault::Auto).positional(), |
| 59 | ]; |
| 60 | |
| 61 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 62 | match kind { |
| 63 | ElemKind::Heading => &HEADING, |
| 64 | ElemKind::Title => &TITLE, |
| 65 | _ => &[], |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn cast(kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 70 | match (kind, name) { |
| 71 | (ElemKind::Heading, "level") => match v { |
| 72 | Value::Auto => Ok(v), |
| 73 | Value::Int(_) => natural(&v, false).map(|_| v), |
| 74 | other => Err(CastErr::Type(common::mismatch("integer or auto", &other))), |
| 75 | }, |
| 76 | (ElemKind::Heading, "depth") => natural(&v, false).map(|_| v), |
| 77 | (ElemKind::Heading, "offset") => natural(&v, true).map(|_| v), |
| 78 | (ElemKind::Heading, "numbering") => expect_cast(v, &[K::Str, K::Func, K::None]), |
| 79 | (ElemKind::Heading, "supplement") => supplement(v), |
| 80 | (ElemKind::Heading, "outlined") => expect(&v, &[K::Bool]).map(|_| v), |
| 81 | (ElemKind::Heading, "bookmarked") => expect(&v, &[K::Bool, K::Auto]).map(|_| v), |
| 82 | (ElemKind::Heading, "hanging-indent") => expect(&v, &[K::Length, K::Auto]).map(|_| v), |
| 83 | (ElemKind::Heading, "body") => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 84 | (ElemKind::Title, "body") => match v { |
| 85 | Value::Auto => Ok(v), |
| 86 | other => expect(&other, &[K::Content]).map(|_| to_content(other)), |
| 87 | }, |
| 88 | _ => Ok(v), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// A supplement: content, a function of the element, `none` or `auto`. |
| 93 | pub fn supplement(v: Value) -> Result<Value, CastErr> { |
| 94 | match v { |
| 95 | Value::Func(_) | Value::None | Value::Auto => Ok(v), |
| 96 | other => expect(&other, &[K::Content, K::Func, K::None, K::Auto]).map(|_| to_content(other)), |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// The level a heading has: set outright, or its depth plus the offset in force. |
| 101 | pub fn level(elem: &Content, styles: &StyleChain) -> Outcome<i64> { |
| 102 | match res!(common::get(elem, styles, "level")) { |
| 103 | Value::Int(l) => Ok(l), |
| 104 | _ => { |
| 105 | let depth = match res!(common::get(elem, styles, "depth")) { Value::Int(d) => d, _ => 1 }; |
| 106 | let offset = match res!(common::get(elem, styles, "offset")) { Value::Int(o) => o, _ => 0 }; |
| 107 | Ok((depth + offset).max(1)) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// Resolves a supplement for `target`: `auto` is the local word for `key`, a function is called with |
| 113 | /// the target, `none` is nothing. |
| 114 | pub fn resolve_supplement( |
| 115 | engine: &mut Engine, |
| 116 | value: &Value, |
| 117 | styles: &StyleChain, |
| 118 | key: &str, |
| 119 | target: &Content, |
| 120 | ) |
| 121 | -> Outcome<Option<Content>> |
| 122 | { |
| 123 | match value { |
| 124 | Value::Auto => Ok(Some(common::text(common::local(styles, key)))), |
| 125 | Value::None => Ok(None), |
| 126 | Value::Func(f) => { |
| 127 | let mut args = Args::new(target.span()); |
| 128 | args.push(target.span(), Value::Content(target.clone())); |
| 129 | let saved = std::mem::replace(&mut engine.context, crate::eval::Context { |
| 130 | location: None, |
| 131 | styles: Some(styles.clone()), |
| 132 | }); |
| 133 | let out = engine.call_func(f, args); |
| 134 | engine.context = saved; |
| 135 | Ok(Some(res!(common::shown(engine, res!(out), target.span())))) |
| 136 | } |
| 137 | other => Ok(Some(common::display(other.clone()))), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn synthesise(engine: &mut Engine, elem: &mut Content, styles: &StyleChain) -> Outcome<()> { |
| 142 | let sup = res!(common::get(elem, styles, "supplement")); |
| 143 | let target = elem.clone(); |
| 144 | let resolved = res!(resolve_supplement(engine, &sup, styles, "heading", &target)) |
| 145 | .unwrap_or_else(Content::empty); |
| 146 | let lvl = res!(level(elem, styles)); |
| 147 | elem.set(res!(common::fid(ElemKind::Heading, "level")), Value::Int(lvl)); |
| 148 | elem.set(res!(common::fid(ElemKind::Heading, "supplement")), Value::Content(resolved)); |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | /// A heading's size by level (1.4em, 1.2em, then 1em), bold, with space above and below scaled so it |
| 153 | /// reads the same at every level, and kept with what follows. |
| 154 | pub fn show_set(elem: &Content, styles: &StyleChain) -> Outcome<Styles> { |
| 155 | let lvl = res!(level(elem, styles)); |
| 156 | let scale = match lvl { |
| 157 | 1 => 1.4, |
| 158 | 2 => 1.2, |
| 159 | _ => 1.0, |
| 160 | }; |
| 161 | let above = if lvl == 1 { 1.8 } else { 1.44 } / scale; |
| 162 | let below = 0.75 / scale; |
| 163 | common::props(vec![ |
| 164 | (ElemKind::Text, "size", common::em(scale)), |
| 165 | (ElemKind::Text, "weight", Value::str("bold")), |
| 166 | (ElemKind::Block, "above", common::em(above)), |
| 167 | (ElemKind::Block, "below", common::em(below)), |
| 168 | (ElemKind::Block, "sticky", Value::Bool(true)), |
| 169 | ]) |
| 170 | } |
| 171 | |
| 172 | pub fn show_set_title() -> Outcome<Styles> { |
| 173 | common::props(vec![ |
| 174 | (ElemKind::Text, "size", common::em(1.7)), |
| 175 | (ElemKind::Text, "weight", Value::str("bold")), |
| 176 | (ElemKind::Block, "above", common::em(1.125)), |
| 177 | (ElemKind::Block, "below", common::em(0.75)), |
| 178 | (ElemKind::Block, "sticky", Value::Bool(true)), |
| 179 | ]) |
| 180 | } |
| 181 | |
| 182 | const SPACING_TO_NUMBERING: f64 = 0.3; // em, between a heading's number and its body |
| 183 | |
| 184 | pub fn show(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 185 | match elem.kind() { |
| 186 | Some(ElemKind::Title) => show_title(engine, elem, styles), |
| 187 | _ => show_heading(engine, elem, styles), |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | fn show_heading(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 192 | let span = elem.span(); |
| 193 | let mut realised = common::body(elem, "body"); |
| 194 | let hanging = res!(common::get(elem, styles, "hanging-indent")); |
| 195 | let mut indent = match &hanging { |
| 196 | Value::Length(_) => hanging.clone(), |
| 197 | _ => common::pt(0.0), |
| 198 | }; |
| 199 | let numbering = res!(common::get(elem, styles, "numbering")); |
| 200 | if !numbering.is_none() { |
| 201 | let loc = match elem.location() { |
| 202 | Some(l) => l, |
| 203 | None => return Err(engine.error(DiagnosticKind::Type, span, "heading must have a location to be numbered")), |
| 204 | }; |
| 205 | let numbers = common::spanned(res!(lookup::display_counter( |
| 206 | engine, &lookup::elem_counter(ElemKind::Heading), loc, &numbering, span)), span); |
| 207 | let align = common::style_or(styles, ElemKind::Align, "alignment", Value::Auto); |
| 208 | if common::is_auto(&hanging) && starts(&align, styles) { |
| 209 | let pod = Region { |
| 210 | width: Sp(i32::MAX), |
| 211 | height: Sp(i32::MAX), |
| 212 | base: (Sp(i32::MAX), Sp(i32::MAX)), |
| 213 | expand_x: false, |
| 214 | expand_y: false, |
| 215 | }; |
| 216 | let size = res!(flow::measure(engine, &numbers, styles, pod)); |
| 217 | let gap = common::resolve_pt(styles, &common::em(SPACING_TO_NUMBERING)); |
| 218 | indent = common::pt(size.width.to_pt() + gap); |
| 219 | } |
| 220 | let spacing = res!(common::h(engine, common::em(SPACING_TO_NUMBERING), true)); |
| 221 | realised = common::seq(vec![numbers, spacing, realised]); |
| 222 | } |
| 223 | if common::resolve_pt(styles, &indent) != 0.0 { |
| 224 | let pull = res!(common::h(engine, common::neg(&indent), false)); |
| 225 | let body = common::seq(vec![pull, realised]); |
| 226 | let inset = common::start_side(styles, indent); |
| 227 | return Ok(Some(res!(common::block(engine, body, span, vec![("inset", inset)])))); |
| 228 | } |
| 229 | Ok(Some(res!(common::block(engine, realised, span, Vec::new())))) |
| 230 | } |
| 231 | |
| 232 | /// Is the horizontal alignment in force the text's start: `start`, or the side the direction starts? |
| 233 | fn starts(align: &Value, styles: &StyleChain) -> bool { |
| 234 | let name = match align { |
| 235 | Value::Alignment(a) if a.x.is_some() => alignment_name(&crate::eval::value::Alignment { x: a.x, y: None }), |
| 236 | _ => return true, |
| 237 | }; |
| 238 | match name.as_str() { |
| 239 | "start" => true, |
| 240 | "left" => !common::is_rtl(styles), |
| 241 | "right" => common::is_rtl(styles), |
| 242 | _ => false, |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | fn show_title(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 247 | let span = elem.span(); |
| 248 | let body = match res!(common::get(elem, styles, "body")) { |
| 249 | Value::Auto => match res!(common::style(styles, ElemKind::Document, "title")) { |
| 250 | Value::None => return Err(engine.error_hint(DiagnosticKind::Type, span, "document title was not set", |
| 251 | "set the title with `set document(title: [...])`")), |
| 252 | v => common::display(v), |
| 253 | }, |
| 254 | v => common::display(v), |
| 255 | }; |
| 256 | Ok(Some(res!(common::block(engine, body, span, Vec::new())))) |
| 257 | } |