Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/geom.rs

7.7 KiB, 1 run

created by r1870400018:63236, 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// U3 owns this file. Methods on `length`, `angle`, `alignment` and `direction`, and the alignment and
2// direction globals (`left`, `top`, `ltr`...). Fields (`1pt.abs`, `(left + top).x`) are
3// `foundations::field`'s. `to-absolute` resolves `em` against the contextual text size, the one method
4// here that needs context.
5
6use crate::diag::DiagnosticKind;
7use crate::eval::args::Args;
8use crate::eval::content::ElemKind;
9use crate::eval::lib::foundations::{
10 finish,
11 mismatch,
12 need,
13 no_method,
14 receiver,
15 repr_length,
16};
17use crate::eval::scope::Scope;
18use crate::eval::value::{
19 Alignment,
20 Direction,
21 HAlign,
22 Length,
23 Type,
24 VAlign,
25 Value,
26};
27use crate::eval::Engine;
28use crate::syntax::Span;
29
30use oxedyne_fe2o3_core::prelude::*;
31
32native_fns! {
33 pub enum GeomFn {
34 Pt => "pt",
35 Mm => "mm",
36 Cm => "cm",
37 Inches => "inches",
38 ToAbsolute => "to-absolute",
39 Deg => "deg",
40 Rad => "rad",
41 Axis => "axis",
42 Inv => "inv",
43 Start => "start",
44 End => "end",
45 Sign => "sign",
46 From => "from",
47 To => "to",
48 }
49}
50
51const DEFAULT_TEXT_SIZE: f64 = 11.0;
52
53pub fn define(scope: &mut Scope) {
54 let h = |x| Value::Alignment(Alignment { x: Some(x), y: None });
55 let v = |y| Value::Alignment(Alignment { x: None, y: Some(y) });
56 scope.define("start", h(HAlign::Start));
57 scope.define("left", h(HAlign::Left));
58 scope.define("center", h(HAlign::Center));
59 scope.define("right", h(HAlign::Right));
60 scope.define("end", h(HAlign::End));
61 scope.define("top", v(VAlign::Top));
62 scope.define("horizon", v(VAlign::Horizon));
63 scope.define("bottom", v(VAlign::Bottom));
64 scope.define("ltr", Value::Direction(Direction::Ltr));
65 scope.define("rtl", Value::Direction(Direction::Rtl));
66 scope.define("ttb", Value::Direction(Direction::Ttb));
67 scope.define("btt", Value::Direction(Direction::Btt));
68}
69
70/// The methods of length, angle, alignment and direction, by name; `axis` and `inv` serve both alignment
71/// and direction.
72pub fn method(name: &str) -> Option<GeomFn> {
73 let f = match name {
74 "pt" => GeomFn::Pt,
75 "mm" => GeomFn::Mm,
76 "cm" => GeomFn::Cm,
77 "inches" => GeomFn::Inches,
78 "to-absolute" => GeomFn::ToAbsolute,
79 "deg" => GeomFn::Deg,
80 "rad" => GeomFn::Rad,
81 "axis" => GeomFn::Axis,
82 "inv" => GeomFn::Inv,
83 "start" => GeomFn::Start,
84 "end" => GeomFn::End,
85 "sign" => GeomFn::Sign,
86 _ => return None,
87 };
88 Some(f)
89}
90
91/// `direction.from(side)` and `direction.to(side)`, the direction's static functions.
92pub fn type_static(ty: Type, name: &str) -> Option<GeomFn> {
93 match (ty, name) {
94 (Type::Direction, "from") => Some(GeomFn::From),
95 (Type::Direction, "to") => Some(GeomFn::To),
96 _ => None,
97 }
98}
99
100fn method_types(f: GeomFn) -> &'static [Type] {
101 match f {
102 GeomFn::Pt | GeomFn::Mm | GeomFn::Cm | GeomFn::Inches | GeomFn::ToAbsolute => &[Type::Length],
103 GeomFn::Deg | GeomFn::Rad => &[Type::Angle],
104 GeomFn::Axis | GeomFn::Inv => &[Type::Alignment, Type::Direction],
105 GeomFn::Start | GeomFn::End | GeomFn::Sign => &[Type::Direction],
106 GeomFn::From | GeomFn::To => &[],
107 }
108}
109
110pub fn inv_h(h: HAlign) -> HAlign {
111 match h {
112 HAlign::Start => HAlign::End,
113 HAlign::End => HAlign::Start,
114 HAlign::Left => HAlign::Right,
115 HAlign::Right => HAlign::Left,
116 HAlign::Center => HAlign::Center,
117 }
118}
119
120pub fn inv_v(v: VAlign) -> VAlign {
121 match v {
122 VAlign::Top => VAlign::Bottom,
123 VAlign::Bottom => VAlign::Top,
124 VAlign::Horizon => VAlign::Horizon,
125 }
126}
127
128pub fn inv_dir(d: Direction) -> Direction {
129 match d {
130 Direction::Ltr => Direction::Rtl,
131 Direction::Rtl => Direction::Ltr,
132 Direction::Ttb => Direction::Btt,
133 Direction::Btt => Direction::Ttb,
134 }
135}
136
137fn is_horizontal(d: Direction) -> bool { matches!(d, Direction::Ltr | Direction::Rtl) }
138
139// The absolute pt of a length, refusing an unresolved em part with Typst's message.
140fn pt_of(engine: &mut Engine, span: Span, l: &Length, unit: &str) -> Outcome<f64> {
141 if l.em != 0.0 {
142 let e = engine.error_hint(DiagnosticKind::Type, span,
143 fmt!("cannot convert a length with non-zero em units (`{}`) to {}", repr_length(l), unit),
144 "use `length.to-absolute()` to resolve its em component (requires context)");
145 if let Some(d) = engine.diags.last_mut() {
146 d.hints.push("or use `length.abs.pt()` instead to ignore its em component".to_string());
147 }
148 return Err(e);
149 }
150 Ok(l.abs)
151}
152
153// The contextual text size in points, for resolving `em`.
154fn text_size(engine: &mut Engine, span: Span) -> Outcome<f64> {
155 let styles = match &engine.context.styles {
156 Some(s) => s.clone(),
157 None => return Err(engine.error_hint(DiagnosticKind::Type, span, "can only be used when context is known",
158 "try wrapping this in a `context` expression")),
159 };
160 let size = match ElemKind::Text.field_id("size") {
161 Some(id) => res!(styles.get(ElemKind::Text, id)),
162 None => None,
163 };
164 Ok(match size {
165 Some(Value::Length(l)) => l.resolve(DEFAULT_TEXT_SIZE),
166 _ => DEFAULT_TEXT_SIZE,
167 })
168}
169
170pub fn call(f: GeomFn, engine: &mut Engine, mut args: Args) -> Outcome<Value> {
171 let span = args.span;
172 if matches!(f, GeomFn::From | GeomFn::To) {
173 let side = res!(need(engine, &mut args, "side"));
174 res!(finish(engine, args));
175 let a = match side {
176 Value::Alignment(a) => a,
177 other => return Err(mismatch(engine, span, "alignment", &other)),
178 };
179 // A side is one of left, right, top and bottom; `to` names the side a direction travels to.
180 let d = match (a.x, a.y) {
181 (Some(HAlign::Left), None) => Direction::Ltr,
182 (Some(HAlign::Right), None) => Direction::Rtl,
183 (None, Some(VAlign::Top)) => Direction::Ttb,
184 (None, Some(VAlign::Bottom)) => Direction::Btt,
185 _ => return Err(engine.error(DiagnosticKind::Type, span, "expected `left`, `right`, `top`, or `bottom`")),
186 };
187 return Ok(Value::Direction(if f == GeomFn::To { inv_dir(d) } else { d }));
188 }
189 let recv = res!(receiver(&mut args));
190 let ty = recv.ty();
191 if !method_types(f).contains(&ty) {
192 return Err(no_method(engine, span, ty, f.name()));
193 }
194 let out = match (f, &recv) {
195 (GeomFn::Pt, Value::Length(l)) => Value::Float(res!(pt_of(engine, span, l, "pt"))),
196 (GeomFn::Mm, Value::Length(l)) => Value::Float(res!(pt_of(engine, span, l, "mm")) / 72.0 * 25.4),
197 (GeomFn::Cm, Value::Length(l)) => Value::Float(res!(pt_of(engine, span, l, "cm")) / 72.0 * 2.54),
198 (GeomFn::Inches, Value::Length(l)) => Value::Float(res!(pt_of(engine, span, l, "inches")) / 72.0),
199 (GeomFn::ToAbsolute, Value::Length(l)) => {
200 if l.em == 0.0 {
201 Value::Length(*l)
202 } else {
203 let size = res!(text_size(engine, span));
204 Value::Length(Length::pt(l.resolve(size)))
205 }
206 }
207 (GeomFn::Deg, Value::Angle(a)) => Value::Float(a.0.to_degrees()),
208 (GeomFn::Rad, Value::Angle(a)) => Value::Float(a.0),
209 (GeomFn::Axis, Value::Alignment(a)) => match (a.x, a.y) {
210 (Some(_), None) => Value::str("horizontal"),
211 (None, Some(_)) => Value::str("vertical"),
212 _ => Value::None,
213 },
214 (GeomFn::Inv, Value::Alignment(a)) => Value::Alignment(Alignment { x: a.x.map(inv_h), y: a.y.map(inv_v) }),
215 (GeomFn::Axis, Value::Direction(d)) => Value::str(if is_horizontal(*d) { "horizontal" } else { "vertical" }),
216 (GeomFn::Inv, Value::Direction(d)) => Value::Direction(inv_dir(*d)),
217 (GeomFn::Start, Value::Direction(d)) | (GeomFn::End, Value::Direction(d)) => {
218 let d = if f == GeomFn::End { inv_dir(*d) } else { *d };
219 Value::Alignment(match d {
220 Direction::Ltr => Alignment { x: Some(HAlign::Left), y: None },
221 Direction::Rtl => Alignment { x: Some(HAlign::Right), y: None },
222 Direction::Ttb => Alignment { x: None, y: Some(VAlign::Top) },
223 Direction::Btt => Alignment { x: None, y: Some(VAlign::Bottom) },
224 })
225 }
226 (GeomFn::Sign, Value::Direction(d)) => Value::Int(match d {
227 Direction::Ltr | Direction::Ttb => 1,
228 _ => -1,
229 }),
230 (_, other) => return Err(mismatch(engine, span, "length, angle, alignment, or direction", other)),
231 };
232 res!(finish(engine, args));
233 Ok(out)
234}