oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/emph.rs
1.8 KiB, 1 run
created by r1870400018:63256, 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/{emph,strong}.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: strong and emph. Neither picks a font: `strong` adds its `delta` to the weight in |
| 5 | // force and `emph` toggles italics, through `text`'s internal `delta` and `emph` style fields, so nested |
| 6 | // emphasis cancels as Typst's does. |
| 7 | |
| 8 | use crate::eval::content::{ |
| 9 | Content, |
| 10 | ElemKind, |
| 11 | FieldDefault, |
| 12 | FieldSpec, |
| 13 | FieldType, |
| 14 | }; |
| 15 | use crate::eval::lib::model::common::{ |
| 16 | self, |
| 17 | expect, |
| 18 | to_content, |
| 19 | CastErr, |
| 20 | K, |
| 21 | }; |
| 22 | use crate::eval::styles::StyleChain; |
| 23 | use crate::eval::value::{ |
| 24 | Type, |
| 25 | Value, |
| 26 | }; |
| 27 | use crate::eval::Engine; |
| 28 | |
| 29 | use oxedyne_fe2o3_core::prelude::*; |
| 30 | |
| 31 | static STRONG: [FieldSpec; 2] = [ |
| 32 | FieldSpec::named("delta", FieldType::Of(Type::Int), FieldDefault::Int(300)), |
| 33 | FieldSpec::required("body", FieldType::Any), |
| 34 | ]; |
| 35 | |
| 36 | static EMPH: [FieldSpec; 1] = [ |
| 37 | FieldSpec::required("body", FieldType::Any), |
| 38 | ]; |
| 39 | |
| 40 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 41 | match kind { |
| 42 | ElemKind::Strong => &STRONG, |
| 43 | ElemKind::Emph => &EMPH, |
| 44 | _ => &[], |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn cast(_kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 49 | match name { |
| 50 | "delta" => expect(&v, &[K::Int]).map(|_| v), |
| 51 | "body" => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 52 | _ => Ok(v), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn show(_engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 57 | let body = common::body(elem, "body"); |
| 58 | match elem.kind() { |
| 59 | Some(ElemKind::Strong) => { |
| 60 | let delta = res!(common::get(elem, styles, "delta")); |
| 61 | Ok(Some(res!(common::set(body, ElemKind::Text, "delta", delta)))) |
| 62 | } |
| 63 | _ => Ok(Some(res!(common::set(body, ElemKind::Text, "emph", Value::Bool(true))))), |
| 64 | } |
| 65 | } |