oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/figure.rs
13.0 KiB, 9 runs
created by r1870400018:63258, 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/figure.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: figure and figure.caption. Synthesis settles what a figure is (its kind from the |
| 5 | // first image, table or raw text in its body), what it is called (the kind's word in the language in |
| 6 | // force) and what counts it (`counter(figure.where(kind: ..))`), and hands all three to the caption, |
| 7 | // with the figure's location, so the caption can number itself. |
| 8 | |
| 9 | use crate::diag::DiagnosticKind; |
| 10 | use crate::eval::content::{ |
| 11 | Content, |
| 12 | ElemKind, |
| 13 | FieldDefault, |
| 14 | FieldSpec, |
| 15 | FieldType, |
| 16 | }; |
| 17 | use crate::eval::func::Func; |
| 18 | use crate::eval::intro::{ |
| 19 | Counter, |
| 20 | CounterKey, |
| 21 | }; |
| 22 | use crate::eval::lib::model::common::{ |
| 23 | self, |
| 24 | alignment_of, |
| 25 | choice, |
| 26 | expect, |
| 27 | expect_cast, |
| 28 | to_content, |
| 29 | CastErr, |
| 30 | K, |
| 31 | }; |
| 32 | use crate::eval::lib::model::{ |
| 33 | heading, |
| 34 | lookup, |
| 35 | }; |
| 36 | use crate::eval::select::Selector; |
| 37 | use crate::eval::styles::{ |
| 38 | StyleChain, |
| 39 | Styles, |
| 40 | }; |
| 41 | use crate::eval::value::{ |
| 42 | Alignment, |
| 43 | HAlign, |
| 44 | Type, |
| 45 | VAlign, |
| 46 | Value, |
| 47 | }; |
| 48 | use crate::eval::Engine; |
| 49 | |
| 50 | use oxedyne_fe2o3_core::prelude::*; |
| 51 | |
| 52 | const ANY: FieldType = FieldType::Any; |
| 53 | |
| 54 | static FIGURE: [FieldSpec; 11] = [ |
| 55 | FieldSpec::required("body", ANY), |
| 56 | FieldSpec::named("alt", FieldType::OneOf(&[Type::Str, Type::None]), FieldDefault::None), |
| 57 | FieldSpec::named("placement", ANY, FieldDefault::None), |
| 58 | FieldSpec::named("scope", ANY, FieldDefault::Str("column")), |
| 59 | FieldSpec::named("caption", ANY, FieldDefault::None), |
| 60 | FieldSpec::named("kind", ANY, FieldDefault::Auto), |
| 61 | FieldSpec::named("supplement", ANY, FieldDefault::Auto), |
| 62 | FieldSpec::named("numbering", ANY, FieldDefault::Str("1")), |
| 63 | FieldSpec::named("gap", FieldType::Of(Type::Length), FieldDefault::Em(0.65)), |
| 64 | FieldSpec::named("outlined", FieldType::Of(Type::Bool), FieldDefault::Bool(true)), |
| 65 | FieldSpec::named("counter", ANY, FieldDefault::None).synthesised(), |
| 66 | ]; |
| 67 | |
| 68 | static CAPTION: [FieldSpec; 8] = [ |
| 69 | FieldSpec::named("position", ANY, FieldDefault::Computed), |
| 70 | FieldSpec::named("separator", ANY, FieldDefault::Auto), |
| 71 | FieldSpec::required("body", ANY), |
| 72 | FieldSpec::named("kind", ANY, FieldDefault::None).synthesised(), |
| 73 | FieldSpec::named("supplement", ANY, FieldDefault::None).synthesised(), |
| 74 | FieldSpec::named("numbering", ANY, FieldDefault::None).synthesised(), |
| 75 | FieldSpec::named("counter", ANY, FieldDefault::None).synthesised(), |
| 76 | FieldSpec::named("figure-location", ANY, FieldDefault::None).synthesised().internal(), |
| 77 | ]; |
| 78 | |
| 79 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 80 | match kind { |
| 81 | ElemKind::Figure => &FIGURE, |
| 82 | ElemKind::FigureCaption => &CAPTION, |
| 83 | _ => &[], |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn default_value(kind: ElemKind, name: &str) -> Option<Value> { |
| 88 | match (kind, name) { |
| 89 | (ElemKind::FigureCaption, "position") => Some(Value::Alignment(Alignment { x: None, y: Some(VAlign::Bottom) })), |
| 90 | _ => None, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | pub fn cast(kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 95 | match (kind, name) { |
| 96 | (ElemKind::Figure, "body") => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 97 | (ElemKind::Figure, "alt") => expect_cast(v, &[K::Str, K::None]), |
| 98 | (ElemKind::Figure, "placement") => match v { |
| 99 | Value::None | Value::Auto => Ok(v), |
| 100 | other => alignment_of(&other, &["top", "horizon", "bottom"]).map(|_| other), |
| 101 | }, |
| 102 | (ElemKind::Figure, "scope") => choice(&v, &["column", "parent"], false, false).map(|_| v), |
| 103 | (ElemKind::Figure, "caption") => match v { |
| 104 | Value::None => Ok(v), |
| 105 | other => { |
| 106 | ok!(expect(&other, &[K::Content, K::None])); |
| 107 | caption_of(to_content(other)) |
| 108 | } |
| 109 | }, |
| 110 | (ElemKind::Figure, "kind") => match &v { |
| 111 | Value::Func(f) if f.element().is_some() => Ok(v), |
| 112 | Value::Str(_) | Value::Auto => Ok(v), |
| 113 | other => Err(CastErr::Type(common::mismatch("function, string, or auto", other))), |
| 114 | }, |
| 115 | (ElemKind::Figure, "supplement") => heading::supplement(v), |
| 116 | (ElemKind::Figure, "numbering") => expect_cast(v, &[K::Str, K::Func, K::None]), |
| 117 | (ElemKind::Figure, "gap") => expect(&v, &[K::Length]).map(|_| v), |
| 118 | (ElemKind::Figure, "outlined") => expect(&v, &[K::Bool]).map(|_| v), |
| 119 | (ElemKind::FigureCaption, "position") => alignment_of(&v, &["top", "bottom"]).map(|_| v), |
| 120 | (ElemKind::FigureCaption, "separator") => match v { |
| 121 | Value::Auto => Ok(v), |
| 122 | other => expect(&other, &[K::Content, K::Auto]).map(|_| to_content(other)), |
| 123 | }, |
| 124 | (ElemKind::FigureCaption, "body") => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 125 | _ => Ok(v), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// Content as a caption: a caption as it is, anything else as a caption's body. |
| 130 | fn caption_of(v: Value) -> Result<Value, CastErr> { |
| 131 | match v { |
| 132 | Value::Content(c) if c.is(ElemKind::FigureCaption) => Ok(Value::Content(c)), |
| 133 | Value::Content(c) => match common::raw_elem(ElemKind::FigureCaption, c.span(), vec![("body", Value::Content(c))]) { |
| 134 | Ok(cap) => Ok(Value::Content(cap)), |
| 135 | Err(e) => Err(CastErr::Value(e.to_string())), |
| 136 | }, |
| 137 | other => Ok(other), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// The elements a figure takes its kind from, Typst's `Figurable`. |
| 142 | fn figurable(k: ElemKind) -> bool { matches!(k, ElemKind::Image | ElemKind::Table | ElemKind::Raw) } |
| 143 | |
| 144 | /// The word key an element kind is named by, Typst's `LocalName`. |
| 145 | pub fn local_key(k: ElemKind) -> Option<&'static str> { |
| 146 | match k { |
| 147 | ElemKind::Image => Some("figure"), |
| 148 | ElemKind::Table => Some("table"), |
| 149 | ElemKind::Raw => Some("raw"), |
| 150 | ElemKind::Equation => Some("equation"), |
| 151 | ElemKind::Heading => Some("heading"), |
| 152 | ElemKind::Outline => Some("outline"), |
| 153 | ElemKind::Bibliography => Some("bibliography"), |
| 154 | ElemKind::Footnote => Some("footnote"), |
| 155 | _ => None, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// The figure's kind: the one given, else the first figurable element of the body (an image when it has none). |
| 160 | pub fn resolved_kind(elem: &Content, styles: &StyleChain) -> Outcome<Value> { |
| 161 | Ok(match res!(common::get(elem, styles, "kind")) { |
| 162 | Value::Auto => { |
| 163 | let body = common::body(elem, "body"); |
| 164 | let found = common::find_first(&body, |c| c.kind().map(figurable).unwrap_or(false)); |
| 165 | let k = found.and_then(|c| c.kind()).unwrap_or(ElemKind::Image); |
| 166 | Value::Func(Func::Element(k)) |
| 167 | } |
| 168 | other => other, |
| 169 | }) |
| 170 | } |
| 171 | |
| 172 | pub fn synthesise(engine: &mut Engine, elem: &mut Content, styles: &StyleChain) -> Outcome<()> { |
| 173 | let span = elem.span(); |
| 174 | let numbering = res!(common::get(elem, styles, "numbering")); |
| 175 | let body = common::body(elem, "body"); |
| 176 | let kind = res!(resolved_kind(elem, styles)); |
| 177 | let kind_elem = match &kind { |
| 178 | Value::Func(f) => f.element(), |
| 179 | _ => None, |
| 180 | }; |
| 181 | let supplement = match res!(common::get(elem, styles, "supplement")) { |
| 182 | Value::Auto => { |
| 183 | let name = kind_elem.and_then(local_key).map(|key| common::text(common::local(styles, key))); |
| 184 | if !numbering.is_none() && name.is_none() { |
| 185 | return Err(engine.error(DiagnosticKind::Type, span, "please specify the figure's supplement")); |
| 186 | } |
| 187 | Some(name.unwrap_or_else(Content::empty)) |
| 188 | } |
| 189 | Value::None => None, |
| 190 | other => { |
| 191 | // A function receives the first element of the figure's kind, or the body. |
| 192 | let target = kind_elem |
| 193 | .and_then(|k| common::find_first(&body, |c| c.is(k))) |
| 194 | .unwrap_or_else(|| body.clone()); |
| 195 | res!(heading::resolve_supplement(engine, &other, styles, "figure", &target)) |
| 196 | } |
| 197 | }; |
| 198 | let counter = res!(figure_counter(&kind)); |
| 199 | let supplement_value = match &supplement { |
| 200 | Some(c) => Value::Content(c.clone()), |
| 201 | None => Value::None, |
| 202 | }; |
| 203 | let caption = match res!(common::get(elem, styles, "caption")) { |
| 204 | Value::Content(mut cap) => { |
| 205 | res!(synthesise_caption(&mut cap, styles)); |
| 206 | for (name, v) in [ |
| 207 | ("kind", kind.clone()), |
| 208 | ("supplement", supplement_value.clone()), |
| 209 | ("numbering", numbering.clone()), |
| 210 | ("counter", Value::Counter(std::sync::Arc::new(counter.clone()))), |
| 211 | ("figure-location", elem.location().map(Value::Location).unwrap_or(Value::None)), |
| 212 | ] { |
| 213 | cap.set(res!(common::fid(ElemKind::FigureCaption, name)), v); |
| 214 | } |
| 215 | Value::Content(cap) |
| 216 | } |
| 217 | other => other, |
| 218 | }; |
| 219 | elem.set(res!(common::fid(ElemKind::Figure, "kind")), kind); |
| 220 | elem.set(res!(common::fid(ElemKind::Figure, "supplement")), supplement_value); |
| 221 | elem.set(res!(common::fid(ElemKind::Figure, "counter")), Value::Counter(std::sync::Arc::new(counter))); |
| 222 | elem.set(res!(common::fid(ElemKind::Figure, "caption")), caption); |
| 223 | Ok(()) |
| 224 | } |
| 225 | |
| 226 | /// `counter(figure.where(kind: k))`. |
| 227 | fn figure_counter(kind: &Value) -> Outcome<Counter> { |
| 228 | let id = res!(common::fid(ElemKind::Figure, "kind")); |
| 229 | Ok(Counter { key: CounterKey::Selector(Selector::Elem(ElemKind::Figure, Some(vec![(id, kind.clone())]))) }) |
| 230 | } |
| 231 | |
| 232 | /// A caption's separator: set, else the one the language in force uses. |
| 233 | pub fn synthesise_caption(elem: &mut Content, styles: &StyleChain) -> Outcome<()> { |
| 234 | let sep = res!(separator(elem, styles)); |
| 235 | elem.set(res!(common::fid(ElemKind::FigureCaption, "separator")), Value::Content(sep)); |
| 236 | Ok(()) |
| 237 | } |
| 238 | |
| 239 | fn separator(elem: &Content, styles: &StyleChain) -> Outcome<Content> { |
| 240 | match res!(common::get(elem, styles, "separator")) { |
| 241 | Value::Auto => { |
| 242 | let (lang, _) = common::lang(styles); |
| 243 | Ok(common::text(match lang.as_str() { |
| 244 | "zh" => "\u{2003}", |
| 245 | "fr" => ".\u{a0}\u{2013} ", |
| 246 | "ru" => ". ", |
| 247 | _ => ": ", |
| 248 | })) |
| 249 | } |
| 250 | v => Ok(common::display(v)), |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /// A figure does not break across pages unless a rule says so, and is centred. |
| 255 | pub fn show_set() -> Outcome<Styles> { |
| 256 | common::props(vec![ |
| 257 | (ElemKind::Block, "breakable", Value::Bool(false)), |
| 258 | (ElemKind::Align, "alignment", Value::Alignment(Alignment { x: Some(HAlign::Center), y: None })), |
| 259 | ]) |
| 260 | } |
| 261 | |
| 262 | pub fn show(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 263 | match elem.kind() { |
| 264 | Some(ElemKind::FigureCaption) => show_caption(engine, elem, styles).map(Some), |
| 265 | _ => show_figure(engine, elem, styles).map(Some), |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | fn show_figure(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Content> { |
| 270 | let span = elem.span(); |
| 271 | let mut realised = common::body(elem, "body"); |
| 272 | if let Value::Content(caption) = res!(common::get(elem, styles, "caption")) { |
| 273 | let position = res!(common::get(&caption, styles, "position")); |
| 274 | let top = matches!(position, Value::Alignment(Alignment { y: Some(VAlign::Top), .. })); |
| 275 | let gap = res!(common::get(elem, styles, "gap")); |
| 276 | let v = res!(common::build(engine, ElemKind::V, span, vec![gap], vec![("weak", Value::Bool(true))])); |
| 277 | realised = if top { |
| 278 | common::seq(vec![caption, v, realised]) |
| 279 | } else { |
| 280 | common::seq(vec![realised, v, caption]) |
| 281 | }; |
| 282 | } |
| 283 | // The body always counts as a paragraph of its own. |
| 284 | realised = common::seq(vec![realised, Content::marker(ElemKind::Parbreak, span)]); |
| 285 | realised = res!(common::block(engine, realised, span, Vec::new())); |
| 286 | let scope = res!(common::get(elem, styles, "scope")); |
| 287 | match res!(common::get(elem, styles, "placement")) { |
| 288 | Value::None => { |
| 289 | if matches!(&scope, Value::Str(s) if s.as_str() == "parent") { |
| 290 | return Err(engine.error_hint(DiagnosticKind::Type, span, |
| 291 | "parent-scoped placement is only available for floating figures", |
| 292 | "you can enable floating placement with `figure(placement: auto, ..)`")); |
| 293 | } |
| 294 | Ok(realised) |
| 295 | } |
| 296 | placement => { |
| 297 | let alignment = match placement { |
| 298 | Value::Alignment(a) => Value::Alignment(Alignment { x: Some(HAlign::Center), y: a.y }), |
| 299 | other => other, |
| 300 | }; |
| 301 | common::build(engine, ElemKind::Place, span, vec![alignment, Value::Content(realised)], vec![ |
| 302 | ("scope", scope), |
| 303 | ("float", Value::Bool(true)), |
| 304 | ]) |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | fn show_caption(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Content> { |
| 310 | let span = elem.span(); |
| 311 | let mut realised = common::body(elem, "body"); |
| 312 | let parts = ( |
| 313 | elem.field("supplement").cloned(), |
| 314 | elem.field("numbering").cloned(), |
| 315 | elem.field("counter").cloned(), |
| 316 | elem.field("figure-location").cloned(), |
| 317 | ); |
| 318 | if let (Some(Value::Content(sup)), Some(numbering), Some(Value::Counter(counter)), Some(Value::Location(loc))) |
| 319 | = parts |
| 320 | { |
| 321 | if !numbering.is_none() { |
| 322 | let numbers = res!(lookup::display_counter(engine, &counter, loc, &numbering, span)); |
| 323 | let mut seq = vec![sup.clone()]; |
| 324 | if !sup.is_empty() { |
| 325 | seq.push(common::text("\u{a0}")); |
| 326 | } |
| 327 | seq.push(numbers); |
| 328 | seq.push(res!(separator(elem, styles))); |
| 329 | seq.push(realised); |
| 330 | realised = common::seq(seq); |
| 331 | } |
| 332 | } |
| 333 | common::block(engine, realised, span, Vec::new()) |
| 334 | } |
| 335 | |
| 336 | /// Is the figure outlined: asked to be, and captioned or numbered? |
| 337 | pub fn outlined(elem: &Content) -> bool { |
| 338 | let asked = !matches!(elem.field("outlined"), Some(Value::Bool(false))); |
| 339 | let captioned = matches!(elem.field("caption"), Some(Value::Content(_))); |
| 340 | let numbered = !matches!(elem.field("numbering"), None | Some(Value::None)); |
| 341 | asked && (captioned || numbered) |
| 342 | } |
| 343 | |
| 344 | /// A figure's prefix in an outline or reference: its supplement, a no-break space, its numbers. |
| 345 | pub fn prefix(elem: &Content, numbers: Content) -> Content { |
| 346 | match elem.field("supplement") { |
| 347 | Some(Value::Content(s)) if !s.is_empty() => common::seq(vec![s.clone(), common::text("\u{a0}"), numbers]), |
| 348 | _ => numbers, |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /// The figure's caption body, or nothing: what an outline lists for it. |
| 353 | pub fn caption_body(elem: &Content) -> Content { |
| 354 | match elem.field("caption") { |
| 355 | Some(Value::Content(c)) => common::body(c, "body"), |
| 356 | _ => Content::empty(), |
| 357 | } |
| 358 | } |
| 359 |