Oregami
Repositories/oxedyne/fe2o3

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

25.8 KiB, 31 runs

created by r1870400018:63494, 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 `text/` and typst-layout `rules.rs`, version 0.15.1).
3// Modified for Austenite: ported to Hematite's types, IR and error handling.
4// U6a owns this file: schemas for the text family and `lorem`, `upper`, `lower`. One fixed point: the
5// `text` element's field 0 is its string, which `Content::text` and `plain_text` rely on; its style
6// fields (font, size, weight, fill, lang, ...) follow it with Typst's defaults.
7//
8// Typst 0.15.1 is the reference (`typst-library/src/text`, Apache-2.0, (c) the Typst project authors):
9// the field names and defaults, `text(..)`'s constructor that styles its body rather than making an
10// element, and the native shows of `smallcaps`, `underline`, `overline`, `strike`, `highlight`, `super`
11// and `sub`, each its body under an internal `text` style that shaping reads (`smallcaps`, `deco`,
12// `shift-settings`). `strong` and `emph` (the model family) set the internal `delta` and `emph`.
13// `lorem` reproduces the `lipsum` crate's Markov chain and seeded generator (MIT, (c) 2017 Martin
14// Geisler), which Typst's `lorem` is built on, so it yields Typst's words.
15
16use crate::diag::DiagnosticKind;
17use crate::eval::args::Args;
18use crate::eval::content::{
19 Content,
20 ElemKind,
21 FieldDefault,
22 FieldSpec,
23 FieldType,
24 Fold,
25};
26use crate::eval::func::{
27 Func,
28 NativeFunc,
29};
30use crate::eval::scope::Scope;
31use crate::eval::styles::{
32 set_rule,
33 Property,
34 Style,
35 StyleChain,
36 Styles,
37};
38use crate::eval::value::{
39 Color,
40 ColorSpace,
41 Dict,
42 Length,
43 Type,
44 Value,
45};
46use crate::eval::Engine;
47use crate::syntax::Span;
48
49use oxedyne_fe2o3_core::prelude::*;
50
51use std::collections::HashMap;
52use std::sync::OnceLock;
53
54native_fns! {
55 pub enum TextFn {
56 Lorem => "lorem",
57 Upper => "upper",
58 Lower => "lower",
59 }
60}
61
62// Keyword choices
63const STYLE: &str = "\"normal\", \"italic\", or \"oblique\"";
64const WEIGHT: &str = "integer, \"thin\", \"extralight\", \"light\", \"regular\", \"medium\", \"semibold\", \"bold\", \"extrabold\", or \"black\"";
65const TOP_EDGE: &str = "\"ascender\", \"cap-height\", \"x-height\", \"baseline\", \"bounds\", or length";
66const BOTTOM_EDGE: &str = "\"baseline\", \"descender\", \"bounds\", or length";
67const NUMBER_TYPE: &str = "\"lining\", \"old-style\", or auto";
68const NUMBER_WIDTH: &str = "\"proportional\", \"tabular\", or auto";
69
70const PAINT: FieldType = FieldType::OneOf(&[Type::Color, Type::Gradient, Type::Tiling]);
71const REL: FieldType = FieldType::OneOf(&[Type::Relative, Type::Length, Type::Ratio]);
72
73const TEXT: &[FieldSpec] = &[
74 FieldSpec::required("text", FieldType::Of(Type::Str)).unsettable(),
75 FieldSpec::named("font", FieldType::OneOf(&[Type::Str, Type::Array, Type::Dict]), FieldDefault::Computed),
76 FieldSpec::named("fallback", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
77 FieldSpec::named("style", FieldType::Keyword(&[], STYLE), FieldDefault::Str("normal")),
78 FieldSpec::named("weight", FieldType::Keyword(&[Type::Int], WEIGHT), FieldDefault::Int(400)),
79 FieldSpec::named("stretch", FieldType::Of(Type::Ratio), FieldDefault::Ratio(1.0)),
80 FieldSpec::named("size", FieldType::Of(Type::Length), FieldDefault::Pt(11.0)).fold(Fold::Add).positional(),
81 FieldSpec::named("fill", PAINT, FieldDefault::Computed).positional(),
82 FieldSpec::named("stroke", FieldType::Any, FieldDefault::None).fold(Fold::Stroke),
83 FieldSpec::named("tracking", FieldType::Of(Type::Length), FieldDefault::Pt(0.0)),
84 FieldSpec::named("spacing", REL, FieldDefault::Ratio(1.0)),
85 FieldSpec::named("cjk-latin-spacing", FieldType::OneOf(&[Type::Auto, Type::None]), FieldDefault::Auto),
86 FieldSpec::named("baseline", FieldType::Of(Type::Length), FieldDefault::Pt(0.0)),
87 FieldSpec::named("overhang", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
88 FieldSpec::named("top-edge", FieldType::Keyword(&[Type::Length], TOP_EDGE), FieldDefault::Str("cap-height")),
89 FieldSpec::named("bottom-edge", FieldType::Keyword(&[Type::Length], BOTTOM_EDGE), FieldDefault::Str("baseline")),
90 FieldSpec::named("lang", FieldType::Of(Type::Str), FieldDefault::Str("en")),
91 FieldSpec::named("region", FieldType::OneOf(&[Type::Str, Type::None]), FieldDefault::None),
92 FieldSpec::named("script", FieldType::OneOf(&[Type::Str, Type::Auto]), FieldDefault::Auto),
93 FieldSpec::named("dir", FieldType::OneOf(&[Type::Direction, Type::Auto]), FieldDefault::Auto),
94 FieldSpec::named("hyphenate", FieldType::OneOf(&[Type::Bool, Type::Auto]), FieldDefault::Auto),
95 FieldSpec::named("costs", FieldType::Of(Type::Dict), FieldDefault::Computed).fold(Fold::Add),
96 FieldSpec::named("kerning", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
97 FieldSpec::named("alternates", FieldType::OneOf(&[Type::Bool, Type::Int]), FieldDefault::Bool(false)),
98 FieldSpec::named("stylistic-set", FieldType::OneOf(&[Type::None, Type::Int, Type::Array]), FieldDefault::None),
99 FieldSpec::named("ligatures", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
100 FieldSpec::named("discretionary-ligatures", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
101 FieldSpec::named("historical-ligatures", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
102 FieldSpec::named("number-type", FieldType::Keyword(&[Type::Auto], NUMBER_TYPE), FieldDefault::Auto),
103 FieldSpec::named("number-width", FieldType::Keyword(&[Type::Auto], NUMBER_WIDTH), FieldDefault::Auto),
104 FieldSpec::named("slashed-zero", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
105 FieldSpec::named("fractions", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
106 FieldSpec::named("features", FieldType::OneOf(&[Type::Array, Type::Dict]), FieldDefault::Computed).fold(Fold::Custom),
107 FieldSpec::named("variations", FieldType::Of(Type::Dict), FieldDefault::Computed).fold(Fold::Custom),
108 // Internal: set by show rules, never by a document.
109 FieldSpec::named("delta", FieldType::Of(Type::Int), FieldDefault::Int(0)).fold(Fold::Custom).unsettable(),
110 FieldSpec::named("emph", FieldType::Of(Type::Bool), FieldDefault::Bool(false)).fold(Fold::Custom).unsettable(),
111 FieldSpec::named("deco", FieldType::Of(Type::Array), FieldDefault::EmptyArray).fold(Fold::Custom).unsettable(),
112 FieldSpec::named("case", FieldType::OneOf(&[Type::Str, Type::None]), FieldDefault::None).unsettable(),
113 FieldSpec::named("smallcaps", FieldType::OneOf(&[Type::Str, Type::None]), FieldDefault::None).unsettable(),
114 FieldSpec::named("shift-settings", FieldType::OneOf(&[Type::Dict, Type::None]), FieldDefault::None).unsettable(),
115];
116
117const LINEBREAK: &[FieldSpec] = &[
118 FieldSpec::named("justify", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
119];
120
121// By contract field 0, as `Content::symbol` builds it.
122const SYMBOL: &[FieldSpec] = &[
123 FieldSpec::required("text", FieldType::Of(Type::Str)),
124];
125
126const SMARTQUOTE: &[FieldSpec] = &[
127 FieldSpec::named("double", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
128 FieldSpec::named("enabled", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
129 FieldSpec::named("alternative", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
130 FieldSpec::named("quotes", FieldType::OneOf(&[Type::Auto, Type::Str, Type::Array, Type::Dict]), FieldDefault::Auto),
131];
132
133const SMALLCAPS: &[FieldSpec] = &[
134 FieldSpec::named("all", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
135 FieldSpec::required("body", FieldType::Content),
136];
137
138const LINE_DECO: &[FieldSpec] = &[
139 FieldSpec::named("stroke", FieldType::Any, FieldDefault::Auto).fold(Fold::Stroke),
140 FieldSpec::named("offset", FieldType::OneOf(&[Type::Auto, Type::Length]), FieldDefault::Auto),
141 FieldSpec::named("extent", FieldType::Of(Type::Length), FieldDefault::Pt(0.0)),
142 FieldSpec::named("evade", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
143 FieldSpec::named("background", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
144 FieldSpec::required("body", FieldType::Content),
145];
146
147const STRIKE: &[FieldSpec] = &[
148 FieldSpec::named("stroke", FieldType::Any, FieldDefault::Auto).fold(Fold::Stroke),
149 FieldSpec::named("offset", FieldType::OneOf(&[Type::Auto, Type::Length]), FieldDefault::Auto),
150 FieldSpec::named("extent", FieldType::Of(Type::Length), FieldDefault::Pt(0.0)),
151 FieldSpec::named("background", FieldType::Of(Type::Bool), FieldDefault::Bool(false)),
152 FieldSpec::required("body", FieldType::Content),
153];
154
155const HIGHLIGHT: &[FieldSpec] = &[
156 FieldSpec::named("fill", FieldType::OneOf(&[Type::None, Type::Color, Type::Gradient, Type::Tiling]), FieldDefault::Computed),
157 FieldSpec::named("stroke", FieldType::Any, FieldDefault::None).fold(Fold::Sides),
158 FieldSpec::named("top-edge", FieldType::Keyword(&[Type::Length], TOP_EDGE), FieldDefault::Str("ascender")),
159 FieldSpec::named("bottom-edge", FieldType::Keyword(&[Type::Length], BOTTOM_EDGE), FieldDefault::Str("descender")),
160 FieldSpec::named("extent", FieldType::Of(Type::Length), FieldDefault::Pt(0.0)),
161 FieldSpec::named("radius", FieldType::Any, FieldDefault::Pt(0.0)).fold(Fold::Corners),
162 FieldSpec::required("body", FieldType::Content),
163];
164
165const SCRIPT: &[FieldSpec] = &[
166 FieldSpec::named("typographic", FieldType::Of(Type::Bool), FieldDefault::Bool(true)),
167 FieldSpec::named("baseline", FieldType::OneOf(&[Type::Auto, Type::Length]), FieldDefault::Auto),
168 FieldSpec::named("size", FieldType::OneOf(&[Type::Auto, Type::Length]), FieldDefault::Auto),
169 FieldSpec::required("body", FieldType::Content),
170];
171
172pub fn define(scope: &mut Scope) {
173 for f in TextFn::ALL {
174 scope.define(f.name(), Value::Func(Func::Native(NativeFunc::Text(*f))));
175 }
176}
177
178pub fn call(f: TextFn, engine: &mut Engine, mut args: Args) -> Outcome<Value> {
179 let span = args.span;
180 match f {
181 TextFn::Lorem => {
182 let words = match res!(args.eat::<Value>()) {
183 Some(Value::Int(n)) if n >= 0 => n as usize,
184 Some(Value::Int(_)) => return Err(engine.error(DiagnosticKind::Type, span, "number must be at least zero")),
185 Some(other) => return Err(engine.error(DiagnosticKind::Type, span, fmt!(
186 "expected integer, found {}", other.ty().long_name()))),
187 None => return Err(engine.error(DiagnosticKind::Type, span, "missing argument: words")),
188 };
189 res!(finish(engine, args));
190 Ok(Value::str(lorem(words)))
191 }
192 TextFn::Upper | TextFn::Lower => {
193 let upper = f == TextFn::Upper;
194 let v = match res!(args.eat::<Value>()) {
195 Some(v) => v,
196 None => return Err(engine.error(DiagnosticKind::Type, span, "missing argument: text")),
197 };
198 res!(finish(engine, args));
199 match v.symbol_as_str() {
200 Value::Str(s) => Ok(Value::str(if upper { s.to_uppercase() } else { s.to_lowercase() })),
201 Value::Content(c) => {
202 let case = if upper { "upper" } else { "lower" };
203 Ok(Value::Content(res!(internal(c, "case", Value::str(case)))))
204 }
205 other => Err(engine.error(DiagnosticKind::Type, span, fmt!(
206 "expected string or content, found {}", other.ty().long_name()))),
207 }
208 }
209 }
210}
211
212fn finish(engine: &mut Engine, args: Args) -> Outcome<()> {
213 let span = args.span;
214 match args.finish() {
215 Ok(()) => Ok(()),
216 Err(e) => Err(engine.error(DiagnosticKind::Type, span, fmt!("{}", e))),
217 }
218}
219
220pub fn fields(kind: ElemKind) -> &'static [FieldSpec] {
221 match kind {
222 ElemKind::Text => TEXT,
223 ElemKind::Symbol => SYMBOL,
224 ElemKind::Linebreak => LINEBREAK,
225 ElemKind::SmartQuote => SMARTQUOTE,
226 ElemKind::Smallcaps => SMALLCAPS,
227 ElemKind::Underline | ElemKind::Overline => LINE_DECO,
228 ElemKind::Strike => STRIKE,
229 ElemKind::Highlight => HIGHLIGHT,
230 ElemKind::Super | ElemKind::Sub => SCRIPT,
231 _ => &[],
232 }
233}
234
235/// `text(..)` styles its body rather than making an element; everything else is built generically.
236/// Its size and fill may be given without a name, as Typst allows.
237pub fn construct(engine: &mut Engine, kind: ElemKind, args: &mut Args) -> Outcome<Option<Content>> {
238 if kind != ElemKind::Text {
239 return Ok(None);
240 }
241 let span = args.span;
242 let mut rest = args.take();
243 // Typst's order: `size` and `fill` are each the named argument or else the first positional one of
244 // their type (`named_or_find`), and the body is then the first positional argument left, whatever
245 // it is, cast to content -- so `text(red, none)` is empty text and `text(red, 2)` a type error.
246 let mut styles = Styles::new();
247 for name in ["size", "fill"] {
248 if rest.items.iter().any(|b| b.name.as_deref() == Some(name)) {
249 continue;
250 }
251 let pos = rest.items.iter().position(|a| a.name.is_none() && match name {
252 "size" => matches!(a.value, Value::Length(_)),
253 _ => matches!(a.value, Value::Color(_) | Value::Gradient(_) | Value::Tiling(_)),
254 });
255 if let Some(p) = pos {
256 let a = rest.items.remove(p);
257 if let Some(id) = ElemKind::Text.field_id(name) {
258 styles.push(Style::Property(Property::new(ElemKind::Text, id, a.value, a.span)));
259 }
260 }
261 }
262 let body = match rest.items.iter().position(|a| a.name.is_none()) {
263 Some(i) => rest.items.remove(i),
264 None => return Err(engine.error(DiagnosticKind::Type, span, "missing argument: body")),
265 };
266 let named = res!(set_rule(engine, ElemKind::Text, rest));
267 styles.extend(&named);
268 let body: Content = match body.value {
269 Value::Int(_) | Value::Float(_) => return Err(engine.error(DiagnosticKind::Type, body.span,
270 fmt!("expected content, found {}", body.value.ty().long_name()))),
271 v => match v.cast() {
272 Ok(c) => c,
273 Err(e) => return Err(engine.error(DiagnosticKind::Type, body.span, crate::diag::message_of(&e))),
274 },
275 };
276 Ok(Some(body.styled(styles)))
277}
278
279/// Typst's `check_font_list`: one `missing_font` warning, at the argument that named it, for each family
280/// of a `font` value that neither the embedded faces nor a supplied font declare. A `set` rule and a
281/// `text(..)` call both pass here, as both do in Typst.
282pub fn check_font_list(engine: &mut Engine, list: &Value, span: Span) -> Outcome<()> {
283 let mut names: Vec<String> = Vec::new();
284 family_names(list, &mut names);
285 if names.is_empty() {
286 return Ok(());
287 }
288 let book = res!(engine.fonts.book());
289 for name in names {
290 if !book.has_family(&name) {
291 engine.warn(DiagnosticKind::MissingFont, span, fmt!("unknown font family: {}", name));
292 }
293 }
294 Ok(())
295}
296
297/// The lower-cased names in a `font` value: a name, a `(name: .., covers: ..)` dictionary, or an array
298/// of either.
299fn family_names(v: &Value, out: &mut Vec<String>) {
300 match v {
301 Value::Str(s) => out.push(s.to_lowercase()),
302 Value::Dict(d) => if let Some(Value::Str(s)) = d.get("name") {
303 out.push(s.to_lowercase());
304 },
305 Value::Array(a) => for item in a.iter() {
306 family_names(item, out);
307 },
308 _ => (),
309 }
310}
311
312/// `content` under one of `text`'s internal fields.
313fn internal(content: Content, field: &str, value: Value) -> Outcome<Content> {
314 let id = match ElemKind::Text.field_id(field) {
315 Some(id) => id,
316 None => return Err(err!("The text schema has no field `{}`.", field; Bug)),
317 };
318 let span = content.span();
319 Ok(content.styled(Styles::from_style(Style::Property(Property::new(ElemKind::Text, id, value, span)))))
320}
321
322/// A text field's default where the schema holds none (`Computed`): the font is the one Typst names, in the
323/// lower case it keeps family names in, the fill is black, the costs are all 100%, and no feature or variation
324/// is set. A highlight's fill is Typst's yellow.
325pub fn default_value(kind: ElemKind, name: &str) -> Option<Value> {
326 match (kind, name) {
327 (ElemKind::Text, "font") => Some(Value::str("libertinus serif")),
328 (ElemKind::Text, "fill") => Some(Value::Color(Color {
329 space: ColorSpace::Luma,
330 c: [0.0; 4],
331 alpha: 1.0,
332 })),
333 // Every cost at its neutral 100%, in the order Typst lists them.
334 (ElemKind::Text, "costs") => {
335 let mut d = Dict::new();
336 for k in ["hyphenation", "runt", "widow", "orphan"] {
337 d.insert(k, Value::Ratio(crate::eval::value::Ratio(1.0)));
338 }
339 Some(Value::dict(d))
340 },
341 (ElemKind::Text, "features") | (ElemKind::Text, "variations") => Some(Value::dict(Dict::new())),
342 // The highlight's own yellow, `#fffd11a1`.
343 (ElemKind::Highlight, "fill") => Some(Value::Color(Color {
344 space: ColorSpace::Rgb,
345 c: [255.0 / 255.0, 253.0 / 255.0, 17.0 / 255.0, 0.0],
346 alpha: 161.0 / 255.0,
347 })),
348 _ => kind.field_id(name).and_then(|id| kind.field_spec(id)).and_then(|s| s.default.to_value()),
349 }
350}
351
352/// A value as a read of its field gives it back, as Typst's `into_value` does: a list of font families in the lower
353/// case it keeps them in, the first-line indent with both its keys.
354pub fn as_read(kind: ElemKind, name: &str, v: Value) -> Outcome<Value> {
355 Ok(match (kind, name, v) {
356 (ElemKind::Text, "font", Value::Array(a)) => {
357 let names: Vec<Value> = a.iter().map(|x| match x {
358 Value::Str(s) => Value::str(s.to_lowercase()),
359 other => other.clone(),
360 }).collect();
361 Value::array(names)
362 },
363 // The indent is read as the dictionary it is written as, with both of its keys.
364 (ElemKind::Par, "first-line-indent", Value::Dict(d)) => {
365 let mut full = Dict::new();
366 full.insert("amount", d.get("amount").cloned().unwrap_or_else(|| Value::Length(Length::pt(0.0))));
367 full.insert("all", d.get("all").cloned().unwrap_or(Value::Bool(false)));
368 Value::dict(full)
369 },
370 (_, _, other) => other,
371 })
372}
373
374/// A field of the element in hand, else the chain's, else the default.
375fn field(elem: &Content, styles: &StyleChain, name: &str) -> Outcome<Option<Value>> {
376 let (kind, id) = match elem.kind().and_then(|k| k.field_id(name).map(|id| (k, id))) {
377 Some(x) => x,
378 None => return Ok(None),
379 };
380 Ok(res!(styles.resolve(elem, id)).or_else(|| kind.field_spec(id).and_then(|s| s.default.to_value())))
381}
382
383fn body_of(elem: &Content) -> Content {
384 match elem.field("body") {
385 Some(Value::Content(c)) => c.clone(),
386 Some(Value::Str(s)) => Content::text(s),
387 _ => Content::empty(),
388 }
389}
390
391/// Typst's default highlight, `rgb("#fffd11a1")`.
392fn highlight_default() -> Value {
393 Value::Color(Color {
394 space: ColorSpace::Rgb,
395 c: [1.0, 253.0 / 255.0, 17.0 / 255.0, 0.0],
396 alpha: 161.0 / 255.0,
397 })
398}
399
400pub fn show(_engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> {
401 let kind = match elem.kind() {
402 Some(k) => k,
403 None => return Ok(None),
404 };
405 let flag = |v: Option<Value>, dflt: bool| match v {
406 Some(Value::Bool(b)) => b,
407 _ => dflt,
408 };
409 match kind {
410 ElemKind::Smallcaps => {
411 let sc = if flag(res!(field(elem, styles, "all")), false) { "all" } else { "minuscules" };
412 Ok(Some(res!(internal(body_of(elem), "smallcaps", Value::str(sc)))))
413 }
414 ElemKind::Underline | ElemKind::Overline | ElemKind::Strike | ElemKind::Highlight => {
415 let mut d = Dict::new();
416 let line = match kind {
417 ElemKind::Underline => "underline",
418 ElemKind::Overline => "overline",
419 ElemKind::Strike => "strike",
420 _ => "highlight",
421 };
422 d.insert("line", Value::str(line));
423 for name in ["stroke", "offset", "extent", "evade", "background", "top-edge", "bottom-edge", "radius"] {
424 if let Some(v) = res!(field(elem, styles, name)) {
425 d.insert(name, v);
426 }
427 }
428 if kind == ElemKind::Highlight {
429 let set = match kind.field_id("fill") {
430 Some(id) => res!(styles.resolve(elem, id)),
431 None => None,
432 };
433 d.insert("fill", set.unwrap_or_else(highlight_default));
434 }
435 let deco = Value::array(vec![Value::dict(d)]);
436 Ok(Some(res!(internal(body_of(elem), "deco", deco))))
437 }
438 ElemKind::Super | ElemKind::Sub => {
439 let size = styles.font_size();
440 let mut d = Dict::new();
441 d.insert("typographic", Value::Bool(flag(res!(field(elem, styles, "typographic")), true)));
442 if let Some(Value::Length(l)) = res!(field(elem, styles, "baseline")) {
443 d.insert("shift", Value::Float(-l.resolve(size) / size));
444 }
445 if let Some(Value::Length(l)) = res!(field(elem, styles, "size")) {
446 d.insert("size", Value::Float(l.resolve(size) / size));
447 }
448 d.insert("kind", Value::str(if kind == ElemKind::Super { "super" } else { "sub" }));
449 Ok(Some(res!(internal(body_of(elem), "shift-settings", Value::dict(d)))))
450 }
451 _ => Ok(None),
452 }
453}
454
455/// Folds the text fields declared `Fold::Custom`: `strong`'s weight deltas add, `emph`'s toggles
456/// cancel in pairs, decorations and features accumulate outermost first, variations overlay.
457pub fn fold(_kind: ElemKind, field: &str, inner: Value, outer: Value) -> Outcome<Value> {
458 Ok(match (field, inner, outer) {
459 ("delta", Value::Int(i), Value::Int(o)) => Value::Int(i.saturating_add(o)),
460 ("emph", Value::Bool(i), Value::Bool(o)) => Value::Bool(i ^ o),
461 ("deco" | "features", Value::Array(i), Value::Array(o)) => {
462 let mut v = (*o).clone();
463 v.extend(i.iter().cloned());
464 Value::array(v)
465 }
466 ("features" | "variations", Value::Dict(i), Value::Dict(o)) => {
467 let mut d = (*o).clone();
468 for (k, v) in i.iter() {
469 d.insert(k, v.clone());
470 }
471 Value::dict(d)
472 }
473 (_, inner, _) => inner,
474 })
475}
476
477// ┌───────────────────────────────────────────────────────────────────────────┐
478// │ LOREM │
479// └───────────────────────────────────────────────────────────────────────────┘
480
481const LOREM_IPSUM: &str = include_str!("../../../data/lipsum/lorem-ipsum.txt");
482const LIBER_PRIMUS: &str = include_str!("../../../data/lipsum/liber-primus.txt");
483
484/// The order-two Markov chain `lipsum` learns: each pair of words and the words seen after it, and the
485/// pairs sorted, which a jump to a random state picks from.
486struct Chain {
487 map: HashMap<(&'static str, &'static str), Vec<&'static str>>,
488 keys: Vec<(&'static str, &'static str)>,
489}
490
491fn chain() -> &'static Chain {
492 static CHAIN: OnceLock<Chain> = OnceLock::new();
493 CHAIN.get_or_init(|| {
494 let mut map: HashMap<(&'static str, &'static str), Vec<&'static str>> = HashMap::new();
495 for text in [LOREM_IPSUM, LIBER_PRIMUS] {
496 let words: Vec<&'static str> = text.split_whitespace().collect();
497 for w in words.windows(3) {
498 map.entry((w[0], w[1])).or_default().push(w[2]);
499 }
500 }
501 let mut keys: Vec<(&'static str, &'static str)> = map.keys().cloned().collect();
502 keys.sort_unstable();
503 Chain { map, keys }
504 })
505}
506
507/// `rand_chacha`'s `ChaCha20Rng` seeded by `seed_from_u64`, as `lipsum` seeds it (97), drawing 32-bit
508/// words in its block order.
509struct ChaCha20 {
510 key: [u32; 8],
511 block: u64,
512 buf: [u32; 64],
513 index: usize,
514}
515
516impl ChaCha20 {
517 fn seed_from_u64(mut state: u64) -> Self {
518 const MUL: u64 = 6_364_136_223_846_793_005;
519 const INC: u64 = 11_634_580_027_462_260_723;
520 let mut key = [0u32; 8];
521 for k in key.iter_mut() {
522 state = state.wrapping_mul(MUL).wrapping_add(INC);
523 let xorshifted = (((state >> 18) ^ state) >> 27) as u32;
524 let rot = (state >> 59) as u32;
525 // The PCG output as little-endian bytes, read back as a little-endian key word.
526 *k = xorshifted.rotate_right(rot);
527 }
528 Self { key, block: 0, buf: [0; 64], index: 64 }
529 }
530
531 fn refill(&mut self) {
532 for b in 0..4 {
533 let ctr = self.block.wrapping_add(b as u64);
534 let init: [u32; 16] = [
535 0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574,
536 self.key[0], self.key[1], self.key[2], self.key[3],
537 self.key[4], self.key[5], self.key[6], self.key[7],
538 ctr as u32, (ctr >> 32) as u32, 0, 0,
539 ];
540 let mut x = init;
541 for _ in 0..10 {
542 qr(&mut x, 0, 4, 8, 12);
543 qr(&mut x, 1, 5, 9, 13);
544 qr(&mut x, 2, 6, 10, 14);
545 qr(&mut x, 3, 7, 11, 15);
546 qr(&mut x, 0, 5, 10, 15);
547 qr(&mut x, 1, 6, 11, 12);
548 qr(&mut x, 2, 7, 8, 13);
549 qr(&mut x, 3, 4, 9, 14);
550 }
551 for i in 0..16 {
552 self.buf[b * 16 + i] = x[i].wrapping_add(init[i]);
553 }
554 }
555 self.block = self.block.wrapping_add(4);
556 self.index = 0;
557 }
558
559 fn next_u32(&mut self) -> u32 {
560 if self.index >= 64 {
561 self.refill();
562 }
563 let v = self.buf[self.index];
564 self.index += 1;
565 v
566 }
567
568 /// An index below `n`, as `rand` 0.8's `gen_range(0..n)` draws one: widening multiply, rejecting
569 /// the low part above the zone.
570 fn below(&mut self, n: u32) -> u32 {
571 if n == 0 {
572 return self.next_u32();
573 }
574 let zone = (n << n.leading_zeros()).wrapping_sub(1);
575 loop {
576 let v = self.next_u32();
577 let m = (v as u64) * (n as u64);
578 let (hi, lo) = ((m >> 32) as u32, m as u32);
579 if lo <= zone {
580 return hi;
581 }
582 }
583 }
584}
585
586fn qr(x: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize) {
587 x[a] = x[a].wrapping_add(x[b]); x[d] ^= x[a]; x[d] = x[d].rotate_left(16);
588 x[c] = x[c].wrapping_add(x[d]); x[b] ^= x[c]; x[b] = x[b].rotate_left(12);
589 x[a] = x[a].wrapping_add(x[b]); x[d] ^= x[a]; x[d] = x[d].rotate_left(8);
590 x[c] = x[c].wrapping_add(x[d]); x[b] ^= x[c]; x[b] = x[b].rotate_left(7);
591}
592
593/// The next word of the chain from `state`, jumping to a random state where the chain has none.
594fn next_word(ch: &Chain, state: &mut (&'static str, &'static str), rng: &mut ChaCha20) -> Option<&'static str> {
595 if ch.map.is_empty() {
596 return None;
597 }
598 let result = state.0;
599 while !ch.map.contains_key(state) {
600 let k = rng.below(ch.keys.len() as u32) as usize;
601 *state = *ch.keys.get(k)?;
602 }
603 let words = ch.map.get(state)?;
604 let w = *words.get(rng.below(words.len() as u32) as usize)?;
605 *state = (state.1, w);
606 Some(result)
607}
608
609/// `n` words of blind text, as Typst's `lorem(n)`.
610pub fn lorem(n: usize) -> String {
611 if n == 0 {
612 return String::new();
613 }
614 let ch = chain();
615 let mut rng = ChaCha20::seed_from_u64(97);
616 let mut state: (&'static str, &'static str) = ("Lorem", "ipsum");
617 let ends = |s: &str| s.ends_with(['.', '!', '?']);
618 let mut out = String::new();
619 let mut count = 0;
620 let mut needs_cap = false;
621 while count < n {
622 let word = match next_word(ch, &mut state, &mut rng) {
623 Some(w) => w,
624 None => break,
625 };
626 if count > 0 {
627 out.push(' ');
628 }
629 if word == "--" {
630 out.push('\u{2013}');
631 continue;
632 }
633 if needs_cap {
634 let mut cs = word.chars();
635 if let Some(c) = cs.next() {
636 out.extend(c.to_uppercase());
637 out.push_str(cs.as_str());
638 }
639 } else {
640 out.push_str(word);
641 }
642 needs_cap = ends(&out);
643 count += 1;
644 }
645 if !ends(&out) {
646 let keep = out.trim_end_matches(|c: char| c.is_ascii_punctuation()).len();
647 out.truncate(keep);
648 out.push('.');
649 }
650 out
651}