Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_locate.rs

5.2 KiB, 1 run

created by r1870400018:63598, 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//! Locations across a relayout (QF3): content that lays a body out is realised at a place of its own, so laying
2//! it out again, whatever was realised in between and in whatever region, locates what is in it as before.
3//!
4//! Each case realises a document, takes the prepared element its source made, and lays it out three times: in
5//! a region, again after the document's own level has been realised once more (which takes ordinals of the
6//! document's locator), and in a narrower region, which no cache of the last layout can answer. The footnote
7//! the element holds must have one location in all three. A footnote placed twice is the fault: the flow dedupes
8//! the notes it has composed by location, so a location that changes with the layout puts the note in again.
9
10use oxedyne_fe2o3_austenite::eval::content::{
11 Content,
12 ElemKind,
13};
14use oxedyne_fe2o3_austenite::eval::locate::Location;
15use oxedyne_fe2o3_austenite::eval::realise::{
16 realise,
17 Pair,
18 RealiseMode,
19};
20use oxedyne_fe2o3_austenite::eval::styles::StyleChain;
21use oxedyne_fe2o3_austenite::eval::{
22 eval_source,
23 Engine,
24 World,
25};
26use oxedyne_fe2o3_austenite::flow::block::{
27 self,
28 Regions,
29};
30
31use oxedyne_fe2o3_core::prelude::*;
32
33use std::path::PathBuf;
34
35/// A source, the kind of element it makes, and what laying that element out reaches.
36const CASES: &[(&str, &str, ElemKind)] = &[
37 ("an unbreakable block",
38 "#block(breakable: false)[A note#footnote[One.] and some words.]", ElemKind::Block),
39 ("a block of full width, paragraph by paragraph",
40 "#block(width: 100%)[First paragraph.\n\nSecond one#footnote[One.] with words.\n\nThird.]", ElemKind::Block),
41 ("a note that a text rule makes in a block",
42 "#show \"word\": it => [#it#footnote[One.]]\n#block(width: 100%)[First.\n\nSecond word here.]", ElemKind::Block),
43 ("a shape",
44 "#rect[A note#footnote[One.] and some words.]", ElemKind::Rect),
45 ("a float",
46 "#place(top, float: true)[A note#footnote[One.] and some words.]", ElemKind::Place),
47 ("a stack",
48 "#stack(dir: ttb, [First#footnote[One.]], [Second.])", ElemKind::Stack),
49 ("a bullet list",
50 "- an item#footnote[One.]\n- another", ElemKind::List),
51 ("a table",
52 "#table(columns: 2, [cell#footnote[One.]], [other])", ElemKind::Table),
53 ("columns",
54 "#columns(2)[A note#footnote[One.] and some words.]", ElemKind::Columns),
55 ("a layout",
56 "#layout(size => [A note#footnote[One.] and some words.])", ElemKind::Layout),
57 ("a pad where blocks may not break",
58 "#set block(breakable: false)\n#pad(left: 4pt)[A note#footnote[One.] and some words.]", ElemKind::Pad),
59];
60
61fn realised(src: &str) -> Outcome<(Engine, Content, Vec<Pair>)> {
62 let root = PathBuf::from("/");
63 let mut world = World::new(root.clone());
64 let id = res!(world.add_source(root.join("__locate.typ"), src.to_string()));
65 let mut engine = Engine::new(world);
66 let module = res!(eval_source(&mut engine, id));
67 let pairs = res!(realise(&mut engine, &module.content, &StyleChain::root(), RealiseMode::Document));
68 Ok((engine, module.content, pairs))
69}
70
71/// The locations of the footnotes in the frames the element lays out into, at a width.
72fn notes(engine: &mut Engine, pair: &Pair, width: f64) -> Outcome<Vec<Location>> {
73 let frames = res!(block::layout_fragment(engine, &pair.content, &pair.styles, Regions::one(width, 10_000.0, true, false)));
74 let mut found = Vec::new();
75 for f in &frames {
76 block::find_footnotes(f, 0.0, &mut found);
77 }
78 Ok(found.into_iter().filter_map(|(_, c)| c.location()).collect())
79}
80
81#[test]
82fn an_element_laid_out_again_locates_its_footnote_as_before() -> Outcome<()> {
83 for (name, src, kind) in CASES {
84 let (mut engine, content, pairs) = res!(realised(src));
85 let pair = res!(pairs.iter().find(|p| p.content.is(*kind)).ok_or_else(|| err!(
86 "{}: realisation made no {}", name, kind.path(); Test)));
87 assert!(pair.content.place().is_some(), "{}: the element was given no place", name);
88 let first = res!(notes(&mut engine, pair, 200.0));
89 assert_eq!(first.len(), 1, "{}: the laid out frames hold {} footnote(s), not one", name, first.len());
90 // The document's own level realised again takes ordinals of the document's locator.
91 res!(realise(&mut engine, &content, &StyleChain::root(), RealiseMode::Document));
92 let again = res!(notes(&mut engine, pair, 200.0));
93 assert_eq!(first, again, "{}: laid out again, the footnote was located anew", name);
94 let narrower = res!(notes(&mut engine, pair, 120.0));
95 assert_eq!(first, narrower, "{}: laid out in a narrower region, the footnote was located anew", name);
96 }
97 assert_eq!(CASES.len(), 11, "a case was dropped");
98 Ok(())
99}
100
101/// Two elements from one source span have places, and so footnote locations, of their own: a loop's blocks hold
102/// notes that are two notes, not one.
103#[test]
104fn elements_from_one_span_keep_their_own_locations() -> Outcome<()> {
105 let (mut engine, _, pairs) = res!(realised("#for i in range(2) [#block(breakable: false)[Word#footnote[Note.]]]"));
106 let blocks: Vec<&Pair> = pairs.iter().filter(|p| p.content.is(ElemKind::Block)).collect();
107 assert_eq!(blocks.len(), 2);
108 let a = res!(notes(&mut engine, blocks[0], 200.0));
109 let b = res!(notes(&mut engine, blocks[1], 200.0));
110 assert_eq!((a.len(), b.len()), (1, 1));
111 assert_ne!(a, b, "two blocks from one span located their notes alike");
112 Ok(())
113}