oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/common.rs
17.2 KiB, 7 runs
created by r1870400018:63250, 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 | // U5 owns this file: the vocabulary every model show and synthesis is written in. A show builds other |
| 2 | // units' elements by field name through their own constructors, so a schema that lacks a field is |
| 3 | // reported as that unit's gap (tag `Unimplemented`), never papered over here. |
| 4 | |
| 5 | use crate::eval::args::Args; |
| 6 | use crate::eval::content::{ |
| 7 | self, |
| 8 | Content, |
| 9 | ElemKind, |
| 10 | FieldId, |
| 11 | }; |
| 12 | use crate::eval::lib::model; |
| 13 | use crate::eval::styles::{ |
| 14 | Property, |
| 15 | Style, |
| 16 | StyleChain, |
| 17 | Styles, |
| 18 | }; |
| 19 | use crate::eval::value::{ |
| 20 | FromValue, |
| 21 | Label, |
| 22 | Length, |
| 23 | Type, |
| 24 | Value, |
| 25 | }; |
| 26 | use crate::eval::Engine; |
| 27 | use crate::syntax::Span; |
| 28 | |
| 29 | use oxedyne_fe2o3_core::prelude::*; |
| 30 | |
| 31 | /// The id of a named field, or the owning unit's gap as an error. |
| 32 | pub fn fid(kind: ElemKind, name: &str) -> Outcome<FieldId> { |
| 33 | match kind.field_id(name) { |
| 34 | Some(id) => Ok(id), |
| 35 | None => Err(err!("{} has no `{}` field in its schema, which the model's default show needs", |
| 36 | kind.path(), name; Unimplemented)), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Builds an element through its family's constructor, as `kind(..positional, ..named)` would in Typst |
| 41 | /// code, so the owning unit's normalisation and checks apply. |
| 42 | pub fn build( |
| 43 | engine: &mut Engine, |
| 44 | kind: ElemKind, |
| 45 | span: Span, |
| 46 | positional: Vec<Value>, |
| 47 | named: Vec<(&str, Value)>, |
| 48 | ) |
| 49 | -> Outcome<Content> |
| 50 | { |
| 51 | let mut args = Args::new(span); |
| 52 | for v in positional { |
| 53 | args.push(span, v); |
| 54 | } |
| 55 | for (n, v) in named { |
| 56 | args.push_named(span, n, v); |
| 57 | } |
| 58 | let c = res!(content::construct(engine, kind, &mut args)); |
| 59 | Ok(spanned(c, span)) |
| 60 | } |
| 61 | |
| 62 | /// An element with the given fields set directly, for this unit's own elements whose constructor would |
| 63 | /// refuse them (synthesised fields, `cannot be constructed manually`). |
| 64 | pub fn raw_elem(kind: ElemKind, span: Span, fields: Vec<(&str, Value)>) -> Outcome<Content> { |
| 65 | let mut fv = Vec::with_capacity(fields.len()); |
| 66 | for (n, v) in fields { |
| 67 | fv.push((res!(fid(kind, n)), v)); |
| 68 | } |
| 69 | Ok(Content::new(kind, fv, span)) |
| 70 | } |
| 71 | |
| 72 | /// A property style, `set kind(name: value)` in code. |
| 73 | pub fn prop(kind: ElemKind, name: &str, value: Value) -> Outcome<Style> { |
| 74 | Ok(Style::Property(Property::new(kind, res!(fid(kind, name)), value, Span::detached()))) |
| 75 | } |
| 76 | |
| 77 | /// Styles from a list of properties. |
| 78 | pub fn props(list: Vec<(ElemKind, &str, Value)>) -> Outcome<Styles> { |
| 79 | let mut s = Styles::new(); |
| 80 | for (k, n, v) in list { |
| 81 | s.push(res!(prop(k, n, v))); |
| 82 | } |
| 83 | Ok(s) |
| 84 | } |
| 85 | |
| 86 | /// Content under one property, Typst's `content.set(field, value)`. |
| 87 | pub fn set(c: Content, kind: ElemKind, name: &str, value: Value) -> Outcome<Content> { |
| 88 | Ok(c.styled(Styles::from_style(res!(prop(kind, name, value))))) |
| 89 | } |
| 90 | |
| 91 | pub fn spanned(c: Content, span: Span) -> Content { |
| 92 | if c.span().is_detached() { c.with_span(span) } else { c } |
| 93 | } |
| 94 | |
| 95 | pub fn seq(parts: Vec<Content>) -> Content { |
| 96 | Content::sequence(parts.into_iter().filter(|c| !is_void(c)).collect()) |
| 97 | } |
| 98 | |
| 99 | /// An empty sequence with no label, which contributes nothing to a sequence. |
| 100 | fn is_void(c: &Content) -> bool { |
| 101 | matches!(c, Content::Sequence(s) if s.children.is_empty() && s.label.is_none()) |
| 102 | } |
| 103 | |
| 104 | pub fn text(s: &str) -> Content { Content::text(s) } |
| 105 | |
| 106 | /// A field of an element in hand: its own value, else the style chain's, else its default (from the |
| 107 | /// schema, or computed by the model for a default no static can hold). |
| 108 | pub fn get(elem: &Content, styles: &StyleChain, name: &str) -> Outcome<Value> { |
| 109 | let kind = match elem.kind() { |
| 110 | Some(k) => k, |
| 111 | None => return Err(err!("a field was asked of content that is not an element"; Bug)), |
| 112 | }; |
| 113 | let id = res!(fid(kind, name)); |
| 114 | if let Some(v) = res!(styles.resolve(elem, id)) { |
| 115 | return Ok(v); |
| 116 | } |
| 117 | match model::default_value(kind, name) { |
| 118 | Some(v) => Ok(v), |
| 119 | None => Ok(Value::None), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub fn get_as<T: FromValue>(elem: &Content, styles: &StyleChain, name: &str) -> Outcome<T> { |
| 124 | T::from_value(res!(get(elem, styles, name))) |
| 125 | } |
| 126 | |
| 127 | /// A style-only field of `kind` read from the chain alone, with its default. |
| 128 | pub fn style(styles: &StyleChain, kind: ElemKind, name: &str) -> Outcome<Value> { |
| 129 | let id = res!(fid(kind, name)); |
| 130 | if let Some(v) = res!(styles.get(kind, id)) { |
| 131 | return Ok(v); |
| 132 | } |
| 133 | match kind.family() { |
| 134 | content::Family::Model => Ok(model::default_value(kind, name).unwrap_or(Value::None)), |
| 135 | _ => Ok(kind.field_spec(id).and_then(|s| s.default.to_value()).unwrap_or(Value::None)), |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// A value as content, for the values a cast has already narrowed to content, text, a symbol or a |
| 140 | /// number. A value of any other kind shows as its `repr`; where one can arrive (a function's result), |
| 141 | /// [`shown`] displays it exactly as Typst does. |
| 142 | pub fn display(v: Value) -> Content { |
| 143 | match v { |
| 144 | Value::None => Content::empty(), |
| 145 | Value::Content(c) => c, |
| 146 | Value::Str(s) => Content::text(&s), |
| 147 | Value::Symbol(s) => Content::symbol(&crate::eval::ops::symbol_text(&s)), |
| 148 | other => Content::text(&crate::eval::lib::foundations::repr(&other)), |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Typst's `Value::display` for any value, a function's result included. |
| 153 | pub fn shown(engine: &mut Engine, v: Value, span: Span) -> Outcome<Content> { |
| 154 | content::display(engine, v, span) |
| 155 | } |
| 156 | |
| 157 | /// A content-valued field (`body`), displayed. |
| 158 | pub fn body(elem: &Content, name: &str) -> Content { |
| 159 | match elem.field(name) { |
| 160 | Some(v) => display(v.clone()), |
| 161 | None => Content::empty(), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn em(v: f64) -> Value { Value::Length(Length::em(v)) } |
| 166 | |
| 167 | pub fn pt(v: f64) -> Value { Value::Length(Length::pt(v)) } |
| 168 | |
| 169 | pub fn label_of(v: &Value) -> Option<Label> { |
| 170 | match v { |
| 171 | Value::Label(l) => Some(l.clone()), |
| 172 | _ => None, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Is the value `auto`, or an absent optional read as `auto`? |
| 177 | pub fn is_auto(v: &Value) -> bool { matches!(v, Value::Auto) } |
| 178 | |
| 179 | /// The type error Typst reports for a value a cast refuses. |
| 180 | pub fn mismatch(expected: &str, found: &Value) -> String { |
| 181 | fmt!("expected {}, found {}", expected, found.ty().long_name()) |
| 182 | } |
| 183 | |
| 184 | /// Does the value have one of these types? |
| 185 | pub fn is_one_of(v: &Value, ts: &[Type]) -> bool { ts.contains(&v.ty()) } |
| 186 | |
| 187 | /// Every element in `c`, depth first in document order, through fields holding content or arrays of |
| 188 | /// it: Typst's `Content::traverse`, which `query_first_naive` walks. |
| 189 | pub fn traverse<F: FnMut(&Content) -> bool>(c: &Content, f: &mut F) -> bool { |
| 190 | match c { |
| 191 | Content::Sequence(s) => { |
| 192 | for x in &s.children { |
| 193 | if traverse(x, f) { |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | false |
| 198 | } |
| 199 | Content::Styled(s) => traverse(&s.child, f), |
| 200 | Content::Elem(e) => { |
| 201 | if f(c) { |
| 202 | return true; |
| 203 | } |
| 204 | for (_, v) in &e.fields { |
| 205 | if traverse_value(v, f) { |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | false |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | fn traverse_value<F: FnMut(&Content) -> bool>(v: &Value, f: &mut F) -> bool { |
| 215 | match v { |
| 216 | Value::Content(c) => traverse(c, f), |
| 217 | Value::Array(a) => { |
| 218 | for x in a.iter() { |
| 219 | if traverse_value(x, f) { |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | false |
| 224 | } |
| 225 | _ => false, |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// The first element in `c` (itself included) that the predicate accepts. |
| 230 | pub fn find_first<P: Fn(&Content) -> bool>(c: &Content, pred: P) -> Option<Content> { |
| 231 | let mut found = None; |
| 232 | traverse(c, &mut |e| { |
| 233 | if pred(e) { |
| 234 | found = Some(e.clone()); |
| 235 | true |
| 236 | } else { |
| 237 | false |
| 238 | } |
| 239 | }); |
| 240 | found |
| 241 | } |
| 242 | |
| 243 | /// Typst's plain text of content, which also reads raw text and a smart quote. |
| 244 | pub fn plain_text(c: &Content) -> String { |
| 245 | let mut s = String::new(); |
| 246 | push_plain(c, &mut s); |
| 247 | s |
| 248 | } |
| 249 | |
| 250 | fn push_plain(c: &Content, s: &mut String) { |
| 251 | match c { |
| 252 | Content::Sequence(q) => for x in &q.children { push_plain(x, s); }, |
| 253 | Content::Styled(q) => push_plain(&q.child, s), |
| 254 | Content::Elem(e) => match e.kind { |
| 255 | ElemKind::Text => if let Some(Value::Str(t)) = c.get(FieldId(0)) { |
| 256 | s.push_str(t); |
| 257 | }, |
| 258 | ElemKind::Space | ElemKind::Linebreak => s.push(' '), |
| 259 | ElemKind::Parbreak => s.push_str("\n\n"), |
| 260 | ElemKind::SmartQuote => { |
| 261 | let double = !matches!(c.field("double"), Some(Value::Bool(false))); |
| 262 | s.push(if double { '"' } else { '\'' }); |
| 263 | } |
| 264 | ElemKind::Raw | ElemKind::RawLine => if let Some(Value::Str(t)) = c.field("text") { |
| 265 | s.push_str(t); |
| 266 | }, |
| 267 | _ => match c.field("body") { |
| 268 | Some(Value::Content(b)) => push_plain(b, s), |
| 269 | Some(Value::Str(t)) => s.push_str(t), |
| 270 | _ => (), |
| 271 | }, |
| 272 | }, |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Casting field values |
| 277 | |
| 278 | /// Why a field refused a value: its type (`find` skips such a positional argument) or its value. |
| 279 | #[derive(Clone, Debug)] |
| 280 | pub enum CastErr { |
| 281 | Type(String), |
| 282 | Value(String), |
| 283 | } |
| 284 | |
| 285 | impl CastErr { |
| 286 | pub fn message(&self) -> &str { |
| 287 | match self { |
| 288 | CastErr::Type(m) | CastErr::Value(m) => m, |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /// The kinds of value a field cast names in its message, in Typst's order and words. |
| 294 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 295 | pub enum K { |
| 296 | Bool, |
| 297 | Int, |
| 298 | Length, |
| 299 | Rel, |
| 300 | Content, |
| 301 | Str, |
| 302 | Label, |
| 303 | Func, |
| 304 | Dict, |
| 305 | Array, |
| 306 | Location, |
| 307 | Selector, |
| 308 | Alignment, |
| 309 | Path, |
| 310 | Bytes, |
| 311 | None, |
| 312 | Auto, |
| 313 | } |
| 314 | |
| 315 | impl K { |
| 316 | pub fn name(self) -> &'static str { |
| 317 | match self { |
| 318 | K::Bool => "boolean", |
| 319 | K::Int => "integer", |
| 320 | K::Length => "length", |
| 321 | K::Rel => "relative length", |
| 322 | K::Content => "content", |
| 323 | K::Str => "string", |
| 324 | K::Label => "label", |
| 325 | K::Func => "function", |
| 326 | K::Dict => "dictionary", |
| 327 | K::Array => "array", |
| 328 | K::Location => "location", |
| 329 | K::Selector => "selector", |
| 330 | K::Alignment => "alignment", |
| 331 | K::Path => "path", |
| 332 | K::Bytes => "bytes", |
| 333 | K::None => "none", |
| 334 | K::Auto => "auto", |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | pub fn accepts(self, v: &Value) -> bool { |
| 339 | match self { |
| 340 | K::Bool => matches!(v, Value::Bool(_)), |
| 341 | K::Int => matches!(v, Value::Int(_)), |
| 342 | K::Length => matches!(v, Value::Length(_)), |
| 343 | K::Rel => matches!(v, Value::Length(_) | Value::Ratio(_) | Value::Relative(_)), |
| 344 | // Content takes what displays as content, and `none` as nothing. |
| 345 | K::Content => matches!(v, Value::Content(_) | Value::Str(_) | Value::Symbol(_) | Value::None), |
| 346 | K::Str | K::Path => matches!(v, Value::Str(_)), |
| 347 | K::Label => matches!(v, Value::Label(_)), |
| 348 | K::Func => matches!(v, Value::Func(_)), |
| 349 | K::Dict => matches!(v, Value::Dict(_)), |
| 350 | K::Array => matches!(v, Value::Array(_)), |
| 351 | K::Location => matches!(v, Value::Location(_)), |
| 352 | K::Selector => matches!(v, Value::Selector(_)), |
| 353 | K::Alignment => matches!(v, Value::Alignment(_)), |
| 354 | K::Bytes => matches!(v, Value::Bytes(_)), |
| 355 | K::None => matches!(v, Value::None), |
| 356 | K::Auto => matches!(v, Value::Auto), |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /// Checks a value against the kinds a field accepts, with Typst's message on refusal. |
| 362 | pub fn expect(v: &Value, ks: &[K]) -> Result<(), CastErr> { |
| 363 | if ks.iter().any(|k| k.accepts(v)) { |
| 364 | return Ok(()); |
| 365 | } |
| 366 | let names: Vec<&str> = ks.iter().map(|k| k.name()).collect(); |
| 367 | Err(CastErr::Type(mismatch(&list(&names), v))) |
| 368 | } |
| 369 | |
| 370 | /// As [`expect`], handing the value back as the field keeps it: a symbol where a string or a path is |
| 371 | /// accepted is stored as the string it casts to, as Typst's `Str` cast makes it. |
| 372 | pub fn expect_cast(v: Value, ks: &[K]) -> Result<Value, CastErr> { |
| 373 | let v = if ks.iter().any(|k| matches!(k, K::Str | K::Path)) && !ks.iter().any(|k| k.accepts(&v)) { |
| 374 | v.symbol_as_str() |
| 375 | } else { |
| 376 | v |
| 377 | }; |
| 378 | expect(&v, ks).map(|_| v) |
| 379 | } |
| 380 | |
| 381 | /// "A, B, or C", as Typst lists what a cast accepts. |
| 382 | pub fn list(names: &[&str]) -> String { |
| 383 | match names.len() { |
| 384 | 0 => String::new(), |
| 385 | 1 => names[0].to_string(), |
| 386 | 2 => fmt!("{} or {}", names[0], names[1]), |
| 387 | n => fmt!("{}, or {}", names[..n - 1].join(", "), names[n - 1]), |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /// One of a closed set of strings, optionally `auto` or `none`: Typst's message names the strings quoted, |
| 392 | /// and says what was found for a value that is not a string. |
| 393 | pub fn choice(v: &Value, options: &[&str], auto: bool, none: bool) -> Result<(), CastErr> { |
| 394 | match v { |
| 395 | Value::Str(s) if options.contains(&s.as_str()) => return Ok(()), |
| 396 | Value::Auto if auto => return Ok(()), |
| 397 | Value::None if none => return Ok(()), |
| 398 | _ => (), |
| 399 | } |
| 400 | let mut names: Vec<String> = options.iter().map(|o| fmt!("\"{}\"", o)).collect(); |
| 401 | if none { |
| 402 | names.push("none".to_string()); |
| 403 | } |
| 404 | if auto { |
| 405 | names.push("auto".to_string()); |
| 406 | } |
| 407 | let refs: Vec<&str> = names.iter().map(|s| s.as_str()).collect(); |
| 408 | let msg = crate::eval::lib::foundations::keyword_message(&list(&refs), v); |
| 409 | // A string outside the set is a value error; any other type a type error. |
| 410 | match v { |
| 411 | Value::Str(_) => Err(CastErr::Value(msg)), |
| 412 | _ => Err(CastErr::Type(msg)), |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /// A positive integer (`number must be positive`) or, with `zero`, a non-negative one. |
| 417 | pub fn natural(v: &Value, zero: bool) -> Result<(), CastErr> { |
| 418 | match v { |
| 419 | Value::Int(i) if *i > 0 || (zero && *i == 0) => Ok(()), |
| 420 | Value::Int(_) if zero => Err(CastErr::Value("number must be at least zero".to_string())), |
| 421 | Value::Int(_) => Err(CastErr::Value("number must be positive".to_string())), |
| 422 | other => Err(CastErr::Type(mismatch("integer", other))), |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// An alignment restricted to some named positions: Typst's message names them in backticks and says |
| 427 | /// what was found. |
| 428 | pub fn alignment_of(v: &Value, names: &[&str]) -> Result<(), CastErr> { |
| 429 | let name = match v { |
| 430 | Value::Alignment(a) => alignment_name(a), |
| 431 | other => { |
| 432 | let quoted: Vec<String> = names.iter().map(|n| fmt!("`{}`", n)).collect(); |
| 433 | let refs: Vec<&str> = quoted.iter().map(|s| s.as_str()).collect(); |
| 434 | return Err(CastErr::Type(mismatch(&list(&refs), other))); |
| 435 | } |
| 436 | }; |
| 437 | if names.contains(&name.as_str()) { |
| 438 | return Ok(()); |
| 439 | } |
| 440 | let quoted: Vec<String> = names.iter().map(|n| fmt!("`{}`", n)).collect(); |
| 441 | let refs: Vec<&str> = quoted.iter().map(|s| s.as_str()).collect(); |
| 442 | Err(CastErr::Value(fmt!("expected {}, found {}", list(&refs), name))) |
| 443 | } |
| 444 | |
| 445 | /// An alignment's Typst name: `left`, `top`, `center + horizon`. |
| 446 | pub fn alignment_name(a: &crate::eval::value::Alignment) -> String { |
| 447 | use crate::eval::value::{ |
| 448 | HAlign, |
| 449 | VAlign, |
| 450 | }; |
| 451 | let x = a.x.map(|x| match x { |
| 452 | HAlign::Start => "start", |
| 453 | HAlign::Left => "left", |
| 454 | HAlign::Center => "center", |
| 455 | HAlign::Right => "right", |
| 456 | HAlign::End => "end", |
| 457 | }); |
| 458 | let y = a.y.map(|y| match y { |
| 459 | VAlign::Top => "top", |
| 460 | VAlign::Horizon => "horizon", |
| 461 | VAlign::Bottom => "bottom", |
| 462 | }); |
| 463 | match (x, y) { |
| 464 | (Some(x), Some(y)) => fmt!("{} + {}", x, y), |
| 465 | (Some(x), None) => x.to_string(), |
| 466 | (None, Some(y)) => y.to_string(), |
| 467 | (None, None) => "start".to_string(), |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /// A value displayed as content, after `expect(.., &[K::Content])` accepted it. |
| 472 | pub fn to_content(v: Value) -> Value { |
| 473 | match v { |
| 474 | Value::Content(_) => v, |
| 475 | other => Value::Content(display(other)), |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /// A style of another unit's element that only tunes a default show, read with Typst's default when that |
| 480 | /// element's schema does not have the field yet: no rule can have set a field the schema lacks, so the |
| 481 | /// default is exactly the value in force. |
| 482 | pub fn style_or(styles: &StyleChain, kind: ElemKind, name: &str, default: Value) -> Value { |
| 483 | match kind.field_id(name) { |
| 484 | Some(id) => match styles.get(kind, id) { |
| 485 | Ok(Some(v)) => v, |
| 486 | // A value its own family cannot fold is that family's error to report where it lays out. |
| 487 | _ => kind.field_spec(id).and_then(|s| s.default.to_value()).unwrap_or(default), |
| 488 | }, |
| 489 | None => default, |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /// The text language and region in force, Typst's `en` and none by default. |
| 494 | pub fn lang(styles: &StyleChain) -> (String, Option<String>) { |
| 495 | let lang = match style_or(styles, ElemKind::Text, "lang", Value::str("en")) { |
| 496 | Value::Str(s) => s.to_string(), |
| 497 | _ => "en".to_string(), |
| 498 | }; |
| 499 | let region = match style_or(styles, ElemKind::Text, "region", Value::None) { |
| 500 | Value::Str(s) => Some(s.to_string()), |
| 501 | _ => None, |
| 502 | }; |
| 503 | (lang, region) |
| 504 | } |
| 505 | |
| 506 | /// The word for `key` in the language in force. |
| 507 | pub fn local(styles: &StyleChain, key: &str) -> &'static str { |
| 508 | let (l, r) = lang(styles); |
| 509 | crate::eval::lib::model::local::local_name(key, &l, r.as_deref()) |
| 510 | } |
| 511 | |
| 512 | /// Is the text direction in force right to left: `text.dir` when set, else the language's own? |
| 513 | pub fn is_rtl(styles: &StyleChain) -> bool { |
| 514 | use crate::eval::value::Direction; |
| 515 | match style_or(styles, ElemKind::Text, "dir", Value::Auto) { |
| 516 | Value::Direction(Direction::Rtl) => true, |
| 517 | Value::Direction(_) => false, |
| 518 | _ => { |
| 519 | let (l, _) = lang(styles); |
| 520 | matches!(l.as_str(), "ar" | "dv" | "fa" | "he" | "ks" | "pa" | "ps" | "sd" | "ug" | "ur" | "yi") |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /// Sides with only the text's start side set: `(left: v)`, or `(right: v)` right to left. |
| 526 | pub fn start_side(styles: &StyleChain, v: Value) -> Value { |
| 527 | let mut d = crate::eval::value::Dict::new(); |
| 528 | d.insert(if is_rtl(styles) { "right" } else { "left" }, v); |
| 529 | Value::dict(d) |
| 530 | } |
| 531 | |
| 532 | /// `h(amount)`, weak when asked. |
| 533 | pub fn h(engine: &mut Engine, amount: Value, weak: bool) -> Outcome<Content> { |
| 534 | let mut named = Vec::new(); |
| 535 | if weak { |
| 536 | named.push(("weak", Value::Bool(true))); |
| 537 | } |
| 538 | build(engine, ElemKind::H, Span::detached(), vec![amount], named) |
| 539 | } |
| 540 | |
| 541 | /// `block(body)`, with named fields. |
| 542 | pub fn block(engine: &mut Engine, body: Content, span: Span, named: Vec<(&str, Value)>) -> Outcome<Content> { |
| 543 | build(engine, ElemKind::Block, span, vec![Value::Content(body)], named) |
| 544 | } |
| 545 | |
| 546 | /// A length negated, for a hanging indent's pull back. |
| 547 | pub fn neg(v: &Value) -> Value { |
| 548 | match v { |
| 549 | Value::Length(l) => Value::Length(Length { abs: -l.abs, em: -l.em }), |
| 550 | other => other.clone(), |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /// A length in points, its em part resolved against the font size in force. |
| 555 | pub fn resolve_pt(styles: &StyleChain, v: &Value) -> f64 { |
| 556 | match v { |
| 557 | Value::Length(l) => styles.resolve_length(*l), |
| 558 | _ => 0.0, |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /// A length or ratio as the relative length a `relative` field stores it as. |
| 563 | pub fn relative(v: Value) -> Value { |
| 564 | use crate::eval::value::{ |
| 565 | Ratio, |
| 566 | Relative, |
| 567 | }; |
| 568 | match v { |
| 569 | Value::Length(l) => Value::Relative(Relative { abs: l, rel: Ratio(0.0) }), |
| 570 | Value::Ratio(r) => Value::Relative(Relative { abs: Length::zero(), rel: r }), |
| 571 | other => other, |
| 572 | } |
| 573 | } |