Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/par.rs

5.8 KiB, 6 runs

created by r1870400018:63276, 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/par.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: par, par.line and parbreak, primitives the flow sets. `par.line`'s fields are
5// style-only (Typst's ghost fields), set by `set par.line(..)` and read by flow for line numbering.
6
7use crate::eval::content::{
8 ElemKind,
9 FieldDefault,
10 FieldSpec,
11 FieldType,
12 Fold,
13};
14use crate::eval::lib::model::common::{
15 choice,
16 expect,
17 expect_cast,
18 pt,
19 to_content,
20 CastErr,
21 K,
22};
23use crate::eval::value::{
24 Dict,
25 Ratio,
26 Relative,
27 Length,
28 Type,
29 Value,
30};
31
32use oxedyne_fe2o3_core::prelude::*;
33
34const ANY: FieldType = FieldType::Any;
35const BOOL: FieldType = FieldType::Of(Type::Bool);
36const LENGTH: FieldType = FieldType::Of(Type::Length);
37
38static PAR: [FieldSpec; 8] = [
39 FieldSpec::named("leading", LENGTH, FieldDefault::Em(0.65)),
40 FieldSpec::named("spacing", LENGTH, FieldDefault::Em(1.2)),
41 FieldSpec::named("justify", BOOL, FieldDefault::Bool(false)),
42 FieldSpec::named("justification-limits", ANY, FieldDefault::Computed).fold(Fold::Custom),
43 FieldSpec::named("linebreaks", ANY, FieldDefault::Auto),
44 FieldSpec::named("first-line-indent", ANY, FieldDefault::Computed).fold(Fold::Keyed("amount")),
45 FieldSpec::named("hanging-indent", LENGTH, FieldDefault::Pt(0.0)),
46 FieldSpec::required("body", FieldType::Content),
47];
48
49static PAR_LINE: [FieldSpec; 5] = [
50 FieldSpec::named("numbering", ANY, FieldDefault::None),
51 FieldSpec::named("number-align", ANY, FieldDefault::Auto),
52 FieldSpec::named("number-margin", ANY, FieldDefault::Computed),
53 FieldSpec::named("number-clearance", ANY, FieldDefault::Auto),
54 FieldSpec::named("numbering-scope", ANY, FieldDefault::Str("document")),
55];
56
57pub fn fields(kind: ElemKind) -> &'static [FieldSpec] {
58 match kind {
59 ElemKind::Par => &PAR,
60 ElemKind::ParLine => &PAR_LINE,
61 _ => &[],
62 }
63}
64
65pub fn default_value(kind: ElemKind, name: &str) -> Option<Value> {
66 match (kind, name) {
67 (ElemKind::Par, "justification-limits") => {
68 let mut spacing = Dict::new();
69 spacing.insert("min", rel(2.0 / 3.0));
70 spacing.insert("max", rel(1.5));
71 let mut tracking = Dict::new();
72 tracking.insert("min", pt(0.0));
73 tracking.insert("max", pt(0.0));
74 let mut d = Dict::new();
75 d.insert("spacing", Value::dict(spacing));
76 d.insert("tracking", Value::dict(tracking));
77 Some(Value::dict(d))
78 }
79 (ElemKind::Par, "first-line-indent") => {
80 let mut d = Dict::new();
81 d.insert("amount", pt(0.0));
82 d.insert("all", Value::Bool(false));
83 Some(Value::dict(d))
84 }
85 (ElemKind::ParLine, "number-margin") => Some(Value::Alignment(crate::eval::value::Alignment {
86 x: Some(crate::eval::value::HAlign::Start),
87 y: None,
88 })),
89 _ => None,
90 }
91}
92
93fn rel(r: f64) -> Value { Value::Relative(Relative { abs: Length::zero(), rel: Ratio(r) }) }
94
95pub fn cast(kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> {
96 match (kind, name) {
97 (ElemKind::Par, "leading") | (ElemKind::Par, "spacing") | (ElemKind::Par, "hanging-indent")
98 => expect(&v, &[K::Length]).map(|_| v),
99 (ElemKind::Par, "justify") => expect(&v, &[K::Bool]).map(|_| v),
100 (ElemKind::Par, "justification-limits") => limits(v),
101 (ElemKind::Par, "linebreaks") => choice(&v, &["simple", "optimized"], true, false).map(|_| v),
102 (ElemKind::Par, "first-line-indent") => first_line_indent(v),
103 (ElemKind::Par, "body") => expect(&v, &[K::Content]).map(|_| to_content(v)),
104 (ElemKind::ParLine, "numbering") => expect_cast(v, &[K::Str, K::Func, K::None]),
105 (ElemKind::ParLine, "number-align") => expect(&v, &[K::Alignment, K::Auto]).map(|_| v),
106 (ElemKind::ParLine, "number-margin") => expect(&v, &[K::Alignment]).map(|_| v),
107 (ElemKind::ParLine, "number-clearance") => expect(&v, &[K::Length, K::Auto]).map(|_| v),
108 (ElemKind::ParLine, "numbering-scope") => choice(&v, &["document", "page"], false, false).map(|_| v),
109 _ => Ok(v),
110 }
111}
112
113/// A length is the amount alone; a dictionary may give `amount` and `all`, nothing else.
114fn first_line_indent(v: Value) -> Result<Value, CastErr> {
115 match &v {
116 Value::Length(_) => {
117 let mut d = Dict::new();
118 d.insert("amount", v);
119 Ok(Value::dict(d))
120 }
121 Value::Dict(d) => {
122 for k in d.keys() {
123 if k != "amount" && k != "all" {
124 return Err(CastErr::Value(fmt!(
125 "unexpected key \"{}\", valid keys are \"amount\" and \"all\"", k)));
126 }
127 }
128 if let Some(a) = d.get("amount") {
129 if let Err(e) = expect(a, &[K::Length]) {
130 return Err(e);
131 }
132 }
133 if let Some(a) = d.get("all") {
134 if let Err(e) = expect(a, &[K::Bool]) {
135 return Err(e);
136 }
137 }
138 Ok(v)
139 }
140 other => Err(CastErr::Type(super::common::mismatch("length or dictionary", other))),
141 }
142}
143
144/// `justification-limits`: a dictionary of `spacing` and `tracking`, each a `min`/`max` dictionary.
145fn limits(v: Value) -> Result<Value, CastErr> {
146 let d = match &v {
147 Value::Dict(d) => d.clone(),
148 other => return Err(CastErr::Type(super::common::mismatch("dictionary", other))),
149 };
150 for k in d.keys() {
151 if k != "spacing" && k != "tracking" {
152 return Err(CastErr::Value(fmt!(
153 "unexpected key \"{}\", valid keys are \"spacing\" and \"tracking\"", k)));
154 }
155 }
156 for (key, want) in [("spacing", K::Rel), ("tracking", K::Length)] {
157 if let Some(x) = d.get(key) {
158 let inner = match x {
159 Value::Dict(i) => i.clone(),
160 other => return Err(CastErr::Type(super::common::mismatch("dictionary", other))),
161 };
162 for m in ["min", "max"] {
163 match inner.get(m) {
164 Some(val) => if let Err(e) = expect(val, &[want]) {
165 return Err(CastErr::Value(fmt!("`{}` value of `{}` is invalid ({})", m, key, e.message())));
166 },
167 None => return Err(CastErr::Value(fmt!("dictionary does not contain key \"{}\"", m))),
168 }
169 }
170 }
171 }
172 Ok(v)
173}