oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/footnote.rs
6.8 KiB, 6 runs
created by r1870400018:63260, 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/footnote.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: footnote and footnote.entry. The footnote's show is its superscript number, linked |
| 5 | // to the entry; page layout collects each page's footnotes, builds their entries with [`entry`] and sets |
| 6 | // them below the separator, the entry's show linking its number back. The entry's location is the |
| 7 | // footnote's own location's variant one ([`entry_location`]), as Typst's `loc.variant(1)`, so page |
| 8 | // layout records the entry's position under that location. |
| 9 | |
| 10 | use crate::diag::DiagnosticKind; |
| 11 | use crate::eval::content::{ |
| 12 | Content, |
| 13 | ElemKind, |
| 14 | FieldDefault, |
| 15 | FieldSpec, |
| 16 | FieldType, |
| 17 | }; |
| 18 | use crate::eval::lib::model::common::{ |
| 19 | self, |
| 20 | expect, |
| 21 | expect_cast, |
| 22 | to_content, |
| 23 | CastErr, |
| 24 | K, |
| 25 | }; |
| 26 | use crate::eval::lib::model::{ |
| 27 | link, |
| 28 | lookup, |
| 29 | }; |
| 30 | use crate::eval::locate::Location; |
| 31 | use crate::eval::styles::{ |
| 32 | StyleChain, |
| 33 | Styles, |
| 34 | }; |
| 35 | use crate::eval::value::{ |
| 36 | Length, |
| 37 | Ratio, |
| 38 | Relative, |
| 39 | Type, |
| 40 | Value, |
| 41 | }; |
| 42 | use crate::eval::Engine; |
| 43 | use crate::syntax::Span; |
| 44 | |
| 45 | use oxedyne_fe2o3_core::prelude::*; |
| 46 | |
| 47 | const ANY: FieldType = FieldType::Any; |
| 48 | const LENGTH: FieldType = FieldType::Of(Type::Length); |
| 49 | |
| 50 | static FOOTNOTE: [FieldSpec; 2] = [ |
| 51 | FieldSpec::named("numbering", ANY, FieldDefault::Str("1")), |
| 52 | FieldSpec::required("body", ANY), |
| 53 | ]; |
| 54 | |
| 55 | static ENTRY: [FieldSpec; 5] = [ |
| 56 | FieldSpec::required("note", ANY), |
| 57 | FieldSpec::named("separator", ANY, FieldDefault::Computed), |
| 58 | FieldSpec::named("clearance", LENGTH, FieldDefault::Em(1.0)), |
| 59 | FieldSpec::named("gap", LENGTH, FieldDefault::Em(0.5)), |
| 60 | FieldSpec::named("indent", LENGTH, FieldDefault::Em(1.0)), |
| 61 | ]; |
| 62 | |
| 63 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 64 | match kind { |
| 65 | ElemKind::Footnote => &FOOTNOTE, |
| 66 | ElemKind::FootnoteEntry => &ENTRY, |
| 67 | _ => &[], |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// The separator: a line 30% wide, stroked at 0.05em. |
| 72 | pub fn default_value(kind: ElemKind, name: &str) -> Option<Value> { |
| 73 | match (kind, name) { |
| 74 | (ElemKind::FootnoteEntry, "separator") => { |
| 75 | let line = ElemKind::Line; |
| 76 | let mut fields = Vec::new(); |
| 77 | if let Some(id) = line.field_id("length") { |
| 78 | fields.push((id, Value::Relative(Relative { abs: Length::zero(), rel: Ratio(0.3) }))); |
| 79 | } |
| 80 | if let Some(id) = line.field_id("stroke") { |
| 81 | fields.push((id, common::em(0.05))); |
| 82 | } |
| 83 | Some(Value::Content(Content::new(line, fields, Span::detached()))) |
| 84 | } |
| 85 | _ => None, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn cast(kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 90 | match (kind, name) { |
| 91 | (ElemKind::Footnote, "numbering") => expect_cast(v, &[K::Str, K::Func]), |
| 92 | (ElemKind::Footnote, "body") => match v { |
| 93 | Value::Label(_) => Ok(v), |
| 94 | other => expect(&other, &[K::Content, K::Label]).map(|_| to_content(other)), |
| 95 | }, |
| 96 | (ElemKind::FootnoteEntry, "note") => { |
| 97 | ok!(expect(&v, &[K::Content])); |
| 98 | match to_content(v) { |
| 99 | Value::Content(c) if c.is(ElemKind::Footnote) => Ok(Value::Content(c)), |
| 100 | Value::Content(c) => match common::raw_elem(ElemKind::Footnote, Span::detached(), |
| 101 | vec![("body", Value::Content(c))]) |
| 102 | { |
| 103 | Ok(f) => Ok(Value::Content(f)), |
| 104 | Err(e) => Err(CastErr::Value(e.to_string())), |
| 105 | }, |
| 106 | other => Ok(other), |
| 107 | } |
| 108 | } |
| 109 | (ElemKind::FootnoteEntry, "separator") => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 110 | (ElemKind::FootnoteEntry, _) => expect(&v, &[K::Length]).map(|_| v), |
| 111 | _ => Ok(v), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Where a footnote's entry is recorded: the footnote's location, variant one. |
| 116 | pub fn entry_location(loc: Location) -> Location { |
| 117 | Location(loc.0.rotate_left(1) ^ 0x9e37_79b9_7f4a_7c15) |
| 118 | } |
| 119 | |
| 120 | /// The entry page layout sets for a footnote. |
| 121 | pub fn entry(note: &Content) -> Outcome<Content> { |
| 122 | common::raw_elem(ElemKind::FootnoteEntry, note.span(), vec![("note", Value::Content(note.clone()))]) |
| 123 | } |
| 124 | |
| 125 | /// Does the footnote repeat another's mark (`footnote(<label>)`) rather than carry a note? |
| 126 | pub fn is_ref(note: &Content) -> bool { matches!(note.field("body"), Some(Value::Label(_))) } |
| 127 | |
| 128 | /// The location of the footnote that carries the note: its own, or the one its label names. |
| 129 | fn declaration(engine: &mut Engine, note: &Content, depth: usize) -> Outcome<Location> { |
| 130 | let span = note.span(); |
| 131 | match note.field("body") { |
| 132 | Some(Value::Label(l)) => { |
| 133 | let l = l.clone(); |
| 134 | let target = res!(lookup::label(engine, &l, span)); |
| 135 | if !target.is(ElemKind::Footnote) { |
| 136 | return Err(engine.error(DiagnosticKind::Type, span, "referenced element should be a footnote")); |
| 137 | } |
| 138 | if target.location() == note.location() || depth > 64 { |
| 139 | return Err(engine.error(DiagnosticKind::Type, span, "footnote cannot reference itself")); |
| 140 | } |
| 141 | declaration(engine, &target, depth + 1) |
| 142 | } |
| 143 | _ => match note.location() { |
| 144 | Some(l) => Ok(l), |
| 145 | None => Err(engine.error(DiagnosticKind::Type, span, "footnote must have a location")), |
| 146 | }, |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | pub fn show_set_entry() -> Outcome<Styles> { |
| 151 | common::props(vec![ |
| 152 | (ElemKind::Par, "leading", common::em(0.5)), |
| 153 | (ElemKind::Text, "size", common::em(0.85)), |
| 154 | ]) |
| 155 | } |
| 156 | |
| 157 | pub fn show(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 158 | let span = elem.span(); |
| 159 | match elem.kind() { |
| 160 | Some(ElemKind::FootnoteEntry) => { |
| 161 | let note = match elem.field("note") { |
| 162 | Some(Value::Content(c)) => c.clone(), |
| 163 | _ => return Err(engine.error(DiagnosticKind::Type, span, "missing argument: note")), |
| 164 | }; |
| 165 | let dest = match note.location() { |
| 166 | Some(l) => l, |
| 167 | None => return Err(engine.error_hint(DiagnosticKind::Type, span, "footnote entry must have a location", |
| 168 | "try using a query or a show rule to customize the footnote instead")), |
| 169 | }; |
| 170 | let numbering = note.field("numbering").cloned().unwrap_or_else(|| Value::str("1")); |
| 171 | let num = res!(lookup::display_counter(engine, &lookup::elem_counter(ElemKind::Footnote), |
| 172 | dest, &numbering, span)); |
| 173 | let linked = res!(link::to_location(num, dest)); |
| 174 | let sup = res!(common::build(engine, ElemKind::Super, span, vec![Value::Content(linked)], Vec::new())); |
| 175 | let indent = res!(common::get(elem, styles, "indent")); |
| 176 | let lead = res!(common::h(engine, indent, false)); |
| 177 | let gap = res!(common::h(engine, common::em(0.05), true)); |
| 178 | Ok(Some(common::seq(vec![lead, sup, gap, common::body(¬e, "body")]))) |
| 179 | } |
| 180 | _ => { |
| 181 | let loc = res!(declaration(engine, elem, 0)); |
| 182 | let numbering = res!(common::get(elem, styles, "numbering")); |
| 183 | let num = res!(lookup::display_counter(engine, &lookup::elem_counter(ElemKind::Footnote), |
| 184 | loc, &numbering, span)); |
| 185 | let linked = res!(link::to_location(num, entry_location(loc))); |
| 186 | let sup = res!(common::build(engine, ElemKind::Super, span, vec![Value::Content(linked)], Vec::new())); |
| 187 | // Zero-width weak spacing: a space before the mark is dropped. |
| 188 | let hole = res!(common::h(engine, common::pt(0.0), true)); |
| 189 | Ok(Some(common::seq(vec![hole, sup]))) |
| 190 | } |
| 191 | } |
| 192 | } |