summaryrefslogtreecommitdiff
path: root/crates/typst-library/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-library/src')
-rw-r--r--crates/typst-library/src/foundations/content/element.rs (renamed from crates/typst-library/src/foundations/element.rs)216
-rw-r--r--crates/typst-library/src/foundations/content/field.rs564
-rw-r--r--crates/typst-library/src/foundations/content/mod.rs (renamed from crates/typst-library/src/foundations/content.rs)434
-rw-r--r--crates/typst-library/src/foundations/content/packed.rs147
-rw-r--r--crates/typst-library/src/foundations/content/raw.rs426
-rw-r--r--crates/typst-library/src/foundations/content/vtable.rs383
-rw-r--r--crates/typst-library/src/foundations/mod.rs2
-rw-r--r--crates/typst-library/src/foundations/scope.rs11
-rw-r--r--crates/typst-library/src/foundations/selector.rs4
-rw-r--r--crates/typst-library/src/foundations/styles.rs222
-rw-r--r--crates/typst-library/src/foundations/target.rs2
-rw-r--r--crates/typst-library/src/html/mod.rs7
-rw-r--r--crates/typst-library/src/html/typed.rs4
-rw-r--r--crates/typst-library/src/introspection/counter.rs26
-rw-r--r--crates/typst-library/src/introspection/state.rs4
-rw-r--r--crates/typst-library/src/layout/align.rs10
-rw-r--r--crates/typst-library/src/layout/columns.rs1
-rw-r--r--crates/typst-library/src/layout/container.rs11
-rw-r--r--crates/typst-library/src/layout/em.rs2
-rw-r--r--crates/typst-library/src/layout/grid/mod.rs12
-rw-r--r--crates/typst-library/src/layout/grid/resolve.rs176
-rw-r--r--crates/typst-library/src/layout/hide.rs2
-rw-r--r--crates/typst-library/src/layout/page.rs10
-rw-r--r--crates/typst-library/src/layout/place.rs1
-rw-r--r--crates/typst-library/src/math/accent.rs1
-rw-r--r--crates/typst-library/src/math/attach.rs5
-rw-r--r--crates/typst-library/src/math/cancel.rs2
-rw-r--r--crates/typst-library/src/math/equation.rs40
-rw-r--r--crates/typst-library/src/math/lr.rs3
-rw-r--r--crates/typst-library/src/math/matrix.rs7
-rw-r--r--crates/typst-library/src/math/style.rs34
-rw-r--r--crates/typst-library/src/model/bibliography.rs53
-rw-r--r--crates/typst-library/src/model/cite.rs5
-rw-r--r--crates/typst-library/src/model/document.rs31
-rw-r--r--crates/typst-library/src/model/emph.rs4
-rw-r--r--crates/typst-library/src/model/enum.rs18
-rw-r--r--crates/typst-library/src/model/figure.rs94
-rw-r--r--crates/typst-library/src/model/footnote.rs17
-rw-r--r--crates/typst-library/src/model/heading.rs50
-rw-r--r--crates/typst-library/src/model/link.rs4
-rw-r--r--crates/typst-library/src/model/list.rs12
-rw-r--r--crates/typst-library/src/model/outline.rs36
-rw-r--r--crates/typst-library/src/model/par.rs3
-rw-r--r--crates/typst-library/src/model/quote.rs33
-rw-r--r--crates/typst-library/src/model/reference.rs15
-rw-r--r--crates/typst-library/src/model/strong.rs4
-rw-r--r--crates/typst-library/src/model/table.rs18
-rw-r--r--crates/typst-library/src/model/terms.rs27
-rw-r--r--crates/typst-library/src/pdf/embed.rs5
-rw-r--r--crates/typst-library/src/text/case.rs4
-rw-r--r--crates/typst-library/src/text/deco.rs113
-rw-r--r--crates/typst-library/src/text/lang.rs2
-rw-r--r--crates/typst-library/src/text/mod.rs59
-rw-r--r--crates/typst-library/src/text/raw.rs44
-rw-r--r--crates/typst-library/src/text/shift.rs33
-rw-r--r--crates/typst-library/src/text/smallcaps.rs5
-rw-r--r--crates/typst-library/src/text/smartquote.rs3
-rw-r--r--crates/typst-library/src/visualize/curve.rs1
-rw-r--r--crates/typst-library/src/visualize/image/mod.rs17
-rw-r--r--crates/typst-library/src/visualize/line.rs4
-rw-r--r--crates/typst-library/src/visualize/path.rs1
-rw-r--r--crates/typst-library/src/visualize/polygon.rs5
-rw-r--r--crates/typst-library/src/visualize/shape.rs34
63 files changed, 2343 insertions, 1180 deletions
diff --git a/crates/typst-library/src/foundations/element.rs b/crates/typst-library/src/foundations/content/element.rs
index 7ff00b9d..49b0b0f9 100644
--- a/crates/typst-library/src/foundations/element.rs
+++ b/crates/typst-library/src/foundations/content/element.rs
@@ -2,52 +2,54 @@ use std::any::TypeId;
use std::cmp::Ordering;
use std::fmt::{self, Debug};
use std::hash::Hash;
-use std::ptr::NonNull;
-use std::sync::LazyLock;
+use std::sync::OnceLock;
use ecow::EcoString;
use smallvec::SmallVec;
-#[doc(inline)]
-pub use typst_macros::elem;
use typst_utils::Static;
use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{
- cast, Args, Content, Dict, FieldAccessError, Func, ParamInfo, Repr, Scope, Selector,
- StyleChain, Styles, Value,
+ cast, Args, Content, ContentVtable, FieldAccessError, Func, ParamInfo, Repr, Scope,
+ Selector, StyleChain, Styles, Value,
};
use crate::text::{Lang, Region};
/// A document element.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
-pub struct Element(Static<NativeElementData>);
+pub struct Element(Static<ContentVtable>);
impl Element {
/// Get the element for `T`.
- pub fn of<T: NativeElement>() -> Self {
- T::elem()
+ pub const fn of<T: NativeElement>() -> Self {
+ T::ELEM
+ }
+
+ /// Get the element for `T`.
+ pub const fn from_vtable(vtable: &'static ContentVtable) -> Self {
+ Self(Static(vtable))
}
/// The element's normal name (e.g. `enum`).
pub fn name(self) -> &'static str {
- self.0.name
+ self.vtable().name
}
/// The element's title case name, for use in documentation
/// (e.g. `Numbered List`).
pub fn title(&self) -> &'static str {
- self.0.title
+ self.vtable().title
}
/// Documentation for the element (as Markdown).
pub fn docs(&self) -> &'static str {
- self.0.docs
+ self.vtable().docs
}
/// Search keywords for the element.
pub fn keywords(&self) -> &'static [&'static str] {
- self.0.keywords
+ self.vtable().keywords
}
/// Construct an instance of this element.
@@ -56,12 +58,12 @@ impl Element {
engine: &mut Engine,
args: &mut Args,
) -> SourceResult<Content> {
- (self.0.construct)(engine, args)
+ (self.vtable().construct)(engine, args)
}
/// Execute the set rule for the element and return the resulting style map.
pub fn set(self, engine: &mut Engine, mut args: Args) -> SourceResult<Styles> {
- let styles = (self.0.set)(engine, &mut args)?;
+ let styles = (self.vtable().set)(engine, &mut args)?;
args.finish()?;
Ok(styles)
}
@@ -77,12 +79,7 @@ impl Element {
/// Whether the element has the given capability where the capability is
/// given by a `TypeId`.
pub fn can_type_id(self, type_id: TypeId) -> bool {
- (self.0.vtable)(type_id).is_some()
- }
-
- /// The VTable for capabilities dispatch.
- pub fn vtable(self) -> fn(of: TypeId) -> Option<NonNull<()>> {
- self.0.vtable
+ (self.vtable().capability)(type_id).is_some()
}
/// Create a selector for this element.
@@ -98,12 +95,29 @@ impl Element {
/// The element's associated scope of sub-definition.
pub fn scope(&self) -> &'static Scope {
- &(self.0).0.scope
+ (self.vtable().store)().scope.get_or_init(|| (self.vtable().scope)())
}
/// Details about the element's fields.
pub fn params(&self) -> &'static [ParamInfo] {
- &(self.0).0.params
+ (self.vtable().store)().params.get_or_init(|| {
+ self.vtable()
+ .fields
+ .iter()
+ .filter(|field| !field.synthesized)
+ .map(|field| ParamInfo {
+ name: field.name,
+ docs: field.docs,
+ input: (field.input)(),
+ default: field.default,
+ positional: field.positional,
+ named: !field.positional,
+ variadic: field.variadic,
+ required: field.required,
+ settable: field.settable,
+ })
+ .collect()
+ })
}
/// Extract the field ID for the given field name.
@@ -111,7 +125,7 @@ impl Element {
if name == "label" {
return Some(255);
}
- (self.0.field_id)(name)
+ (self.vtable().field_id)(name)
}
/// Extract the field name for the given field ID.
@@ -119,7 +133,7 @@ impl Element {
if id == 255 {
return Some("label");
}
- (self.0.field_name)(id)
+ self.vtable().field(id).map(|data| data.name)
}
/// Extract the value of the field for the given field ID and style chain.
@@ -128,12 +142,20 @@ impl Element {
id: u8,
styles: StyleChain,
) -> Result<Value, FieldAccessError> {
- (self.0.field_from_styles)(id, styles)
+ self.vtable()
+ .field(id)
+ .and_then(|field| (field.get_from_styles)(styles))
+ .ok_or(FieldAccessError::Unknown)
}
/// The element's local name, if any.
pub fn local_name(&self, lang: Lang, region: Option<Region>) -> Option<&'static str> {
- (self.0).0.local_name.map(|f| f(lang, region))
+ self.vtable().local_name.map(|f| f(lang, region))
+ }
+
+ /// Retrieves the element's vtable for dynamic dispatch.
+ pub(super) fn vtable(&self) -> &'static ContentVtable {
+ (self.0).0
}
}
@@ -167,84 +189,34 @@ cast! {
v: Func => v.element().ok_or("expected element")?,
}
-/// A Typst element that is defined by a native Rust type.
-pub trait NativeElement:
- Debug
- + Clone
- + PartialEq
- + Hash
- + Construct
- + Set
- + Capable
- + Fields
- + Repr
- + Send
- + Sync
- + 'static
-{
- /// Get the element for the native Rust element.
- fn elem() -> Element
- where
- Self: Sized,
- {
- Element::from(Self::data())
- }
+/// Lazily initialized data for an element.
+#[derive(Default)]
+pub struct LazyElementStore {
+ pub scope: OnceLock<Scope>,
+ pub params: OnceLock<Vec<ParamInfo>>,
+}
- /// Pack the element into type-erased content.
- fn pack(self) -> Content
- where
- Self: Sized,
- {
- Content::new(self)
+impl LazyElementStore {
+ /// Create an empty store.
+ pub const fn new() -> Self {
+ Self { scope: OnceLock::new(), params: OnceLock::new() }
}
-
- /// Get the element data for the native Rust element.
- fn data() -> &'static NativeElementData
- where
- Self: Sized;
}
-/// Used to cast an element to a trait object for a trait it implements.
+/// A Typst element that is defined by a native Rust type.
///
/// # Safety
-/// If the `vtable` function returns `Some(p)`, then `p` must be a valid pointer
-/// to a vtable of `Packed<Self>` w.r.t to the trait `C` where `capability` is
-/// `TypeId::of::<dyn C>()`.
-pub unsafe trait Capable {
- /// Get the pointer to the vtable for the given capability / trait.
- fn vtable(capability: TypeId) -> Option<NonNull<()>>;
-}
-
-/// Defines how fields of an element are accessed.
-pub trait Fields {
- /// An enum with the fields of the element.
- type Enum
- where
- Self: Sized;
-
- /// Whether the element has the given field set.
- fn has(&self, id: u8) -> bool;
-
- /// Get the field with the given field ID.
- fn field(&self, id: u8) -> Result<Value, FieldAccessError>;
-
- /// Get the field with the given ID in the presence of styles.
- fn field_with_styles(
- &self,
- id: u8,
- styles: StyleChain,
- ) -> Result<Value, FieldAccessError>;
-
- /// Get the field with the given ID from the styles.
- fn field_from_styles(id: u8, styles: StyleChain) -> Result<Value, FieldAccessError>
- where
- Self: Sized;
-
- /// Resolve all fields with the styles and save them in-place.
- fn materialize(&mut self, styles: StyleChain);
+/// `ELEM` must hold the correct `Element` for `Self`.
+pub unsafe trait NativeElement:
+ Debug + Clone + Hash + Construct + Set + Send + Sync + 'static
+{
+ /// The associated element.
+ const ELEM: Element;
- /// Get the fields of the element.
- fn fields(&self) -> Dict;
+ /// Pack the element into type-erased content.
+ fn pack(self) -> Content {
+ Content::new(self)
+ }
}
/// An element's constructor function.
@@ -266,48 +238,6 @@ pub trait Set {
Self: Sized;
}
-/// Defines a native element.
-#[derive(Debug)]
-pub struct NativeElementData {
- /// The element's normal name (e.g. `align`), as exposed to Typst.
- pub name: &'static str,
- /// The element's title case name (e.g. `Align`).
- pub title: &'static str,
- /// The documentation for this element as a string.
- pub docs: &'static str,
- /// A list of alternate search terms for this element.
- pub keywords: &'static [&'static str],
- /// The constructor for this element (see [`Construct`]).
- pub construct: fn(&mut Engine, &mut Args) -> SourceResult<Content>,
- /// Executes this element's set rule (see [`Set`]).
- pub set: fn(&mut Engine, &mut Args) -> SourceResult<Styles>,
- /// Gets the vtable for one of this element's capabilities
- /// (see [`Capable`]).
- pub vtable: fn(capability: TypeId) -> Option<NonNull<()>>,
- /// Gets the numeric index of this field by its name.
- pub field_id: fn(name: &str) -> Option<u8>,
- /// Gets the name of a field by its numeric index.
- pub field_name: fn(u8) -> Option<&'static str>,
- /// Get the field with the given ID in the presence of styles (see [`Fields`]).
- pub field_from_styles: fn(u8, StyleChain) -> Result<Value, FieldAccessError>,
- /// Gets the localized name for this element (see [`LocalName`][crate::text::LocalName]).
- pub local_name: Option<fn(Lang, Option<Region>) -> &'static str>,
- pub scope: LazyLock<Scope>,
- /// A list of parameter information for each field.
- pub params: LazyLock<Vec<ParamInfo>>,
-}
-
-impl From<&'static NativeElementData> for Element {
- fn from(data: &'static NativeElementData) -> Self {
- Self(Static(data))
- }
-}
-
-cast! {
- &'static NativeElementData,
- self => Element::from(self).into_value(),
-}
-
/// Synthesize fields on an element. This happens before execution of any show
/// rule.
pub trait Synthesize {
@@ -331,3 +261,9 @@ pub trait ShowSet {
/// that should work even in the face of a user-defined show rule.
fn show_set(&self, styles: StyleChain) -> Styles;
}
+
+/// Tries to extract the plain-text representation of the element.
+pub trait PlainText {
+ /// Write this element's plain text into the given buffer.
+ fn plain_text(&self, text: &mut EcoString);
+}
diff --git a/crates/typst-library/src/foundations/content/field.rs b/crates/typst-library/src/foundations/content/field.rs
new file mode 100644
index 00000000..8d0fe529
--- /dev/null
+++ b/crates/typst-library/src/foundations/content/field.rs
@@ -0,0 +1,564 @@
+use std::fmt::{self, Debug};
+use std::hash::Hash;
+use std::marker::PhantomData;
+use std::sync::OnceLock;
+
+use ecow::{eco_format, EcoString};
+
+use crate::foundations::{
+ Container, Content, FieldVtable, Fold, FoldFn, IntoValue, NativeElement, Packed,
+ Property, Reflect, Repr, Resolve, StyleChain,
+};
+
+/// An accessor for the `I`-th field of the element `E`. Values of this type are
+/// generated for each field of an element can be used to interact with this
+/// field programmatically, for example to access the style chain, as in
+/// `styles.get(TextElem::size)`.
+#[derive(Copy, Clone)]
+pub struct Field<E: NativeElement, const I: u8>(pub PhantomData<E>);
+
+impl<E: NativeElement, const I: u8> Field<E, I> {
+ /// Creates a new zero-sized accessor.
+ pub const fn new() -> Self {
+ Self(PhantomData)
+ }
+
+ /// The index of the projected field.
+ pub const fn index(self) -> u8 {
+ I
+ }
+
+ /// Creates a dynamic property instance for this field.
+ ///
+ /// Prefer [`Content::set`] or
+ /// [`Styles::set`](crate::foundations::Styles::set) when working with
+ /// existing content or style value.
+ pub fn set(self, value: E::Type) -> Property
+ where
+ E: SettableProperty<I>,
+ E::Type: Debug + Clone + Hash + Send + Sync + 'static,
+ {
+ Property::new(self, value)
+ }
+}
+
+impl<E: NativeElement, const I: u8> Default for Field<E, I> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+/// A field that is present on every instance of the element.
+pub trait RequiredField<const I: u8>: NativeElement {
+ type Type: Clone;
+
+ const FIELD: RequiredFieldData<Self, I>;
+}
+
+/// Metadata and routines for a [`RequiredField`].
+pub struct RequiredFieldData<E: RequiredField<I>, const I: u8> {
+ name: &'static str,
+ docs: &'static str,
+ get: fn(&E) -> &E::Type,
+}
+
+impl<E: RequiredField<I>, const I: u8> RequiredFieldData<E, I> {
+ /// Creates the data from its parts. This is called in the `#[elem]` macro.
+ pub const fn new(
+ name: &'static str,
+ docs: &'static str,
+ get: fn(&E) -> &E::Type,
+ ) -> Self {
+ Self { name, docs, get }
+ }
+
+ /// Creates the vtable for a `#[required]` field.
+ pub const fn vtable() -> FieldVtable<Packed<E>>
+ where
+ E: RequiredField<I>,
+ E::Type: Reflect + IntoValue + PartialEq,
+ {
+ FieldVtable {
+ name: E::FIELD.name,
+ docs: E::FIELD.docs,
+ positional: true,
+ required: true,
+ variadic: false,
+ settable: false,
+ synthesized: false,
+ input: || <E::Type as Reflect>::input(),
+ default: None,
+ has: |_| true,
+ get: |elem| Some((E::FIELD.get)(elem).clone().into_value()),
+ get_with_styles: |elem, _| Some((E::FIELD.get)(elem).clone().into_value()),
+ get_from_styles: |_| None,
+ materialize: |_, _| {},
+ eq: |a, b| (E::FIELD.get)(a) == (E::FIELD.get)(b),
+ }
+ }
+
+ /// Creates the vtable for a `#[variadic]` field.
+ pub const fn vtable_variadic() -> FieldVtable<Packed<E>>
+ where
+ E: RequiredField<I>,
+ E::Type: Container + IntoValue + PartialEq,
+ <E::Type as Container>::Inner: Reflect,
+ {
+ FieldVtable {
+ name: E::FIELD.name,
+ docs: E::FIELD.docs,
+ positional: true,
+ required: true,
+ variadic: true,
+ settable: false,
+ synthesized: false,
+ input: || <<E::Type as Container>::Inner as Reflect>::input(),
+ default: None,
+ has: |_| true,
+ get: |elem| Some((E::FIELD.get)(elem).clone().into_value()),
+ get_with_styles: |elem, _| Some((E::FIELD.get)(elem).clone().into_value()),
+ get_from_styles: |_| None,
+ materialize: |_, _| {},
+ eq: |a, b| (E::FIELD.get)(a) == (E::FIELD.get)(b),
+ }
+ }
+}
+
+/// A field that is initially unset, but may be set through a
+/// [`Synthesize`](crate::foundations::Synthesize) implementation.
+pub trait SynthesizedField<const I: u8>: NativeElement {
+ type Type: Clone;
+
+ const FIELD: SynthesizedFieldData<Self, I>;
+}
+
+/// Metadata and routines for a [`SynthesizedField`].
+pub struct SynthesizedFieldData<E: SynthesizedField<I>, const I: u8> {
+ name: &'static str,
+ docs: &'static str,
+ get: fn(&E) -> &Option<E::Type>,
+}
+
+impl<E: SynthesizedField<I>, const I: u8> SynthesizedFieldData<E, I> {
+ /// Creates the data from its parts. This is called in the `#[elem]` macro.
+ pub const fn new(
+ name: &'static str,
+ docs: &'static str,
+ get: fn(&E) -> &Option<E::Type>,
+ ) -> Self {
+ Self { name, docs, get }
+ }
+
+ /// Creates type-erased metadata and routines for a `#[synthesized]` field.
+ pub const fn vtable() -> FieldVtable<Packed<E>>
+ where
+ E: SynthesizedField<I>,
+ E::Type: Reflect + IntoValue + PartialEq,
+ {
+ FieldVtable {
+ name: E::FIELD.name,
+ docs: E::FIELD.docs,
+ positional: false,
+ required: false,
+ variadic: false,
+ settable: false,
+ synthesized: true,
+ input: || <E::Type as Reflect>::input(),
+ default: None,
+ has: |elem| (E::FIELD.get)(elem).is_some(),
+ get: |elem| (E::FIELD.get)(elem).clone().map(|v| v.into_value()),
+ get_with_styles: |elem, _| {
+ (E::FIELD.get)(elem).clone().map(|v| v.into_value())
+ },
+ get_from_styles: |_| None,
+ materialize: |_, _| {},
+ // Synthesized fields don't affect equality.
+ eq: |_, _| true,
+ }
+ }
+}
+
+/// A field that is not actually there. It's only visible in the docs.
+pub trait ExternalField<const I: u8>: NativeElement {
+ type Type;
+
+ const FIELD: ExternalFieldData<Self, I>;
+}
+
+/// Metadata for an [`ExternalField`].
+pub struct ExternalFieldData<E: ExternalField<I>, const I: u8> {
+ name: &'static str,
+ docs: &'static str,
+ default: fn() -> E::Type,
+}
+
+impl<E: ExternalField<I>, const I: u8> ExternalFieldData<E, I> {
+ /// Creates the data from its parts. This is called in the `#[elem]` macro.
+ pub const fn new(
+ name: &'static str,
+ docs: &'static str,
+ default: fn() -> E::Type,
+ ) -> Self {
+ Self { name, docs, default }
+ }
+
+ /// Creates type-erased metadata and routines for an `#[external]` field.
+ pub const fn vtable() -> FieldVtable<Packed<E>>
+ where
+ E: ExternalField<I>,
+ E::Type: Reflect + IntoValue,
+ {
+ FieldVtable {
+ name: E::FIELD.name,
+ docs: E::FIELD.docs,
+ positional: false,
+ required: false,
+ variadic: false,
+ settable: false,
+ synthesized: false,
+ input: || <E::Type as Reflect>::input(),
+ default: Some(|| (E::FIELD.default)().into_value()),
+ has: |_| false,
+ get: |_| None,
+ get_with_styles: |_, _| None,
+ get_from_styles: |_| None,
+ materialize: |_, _| {},
+ eq: |_, _| true,
+ }
+ }
+}
+
+/// A field that has a default value and can be configured via a set rule, but
+/// can also present on elements and be present in the constructor.
+pub trait SettableField<const I: u8>: NativeElement {
+ type Type: Clone;
+
+ const FIELD: SettableFieldData<Self, I>;
+}
+
+/// Metadata and routines for a [`SettableField`].
+pub struct SettableFieldData<E: SettableField<I>, const I: u8> {
+ get: fn(&E) -> &Settable<E, I>,
+ get_mut: fn(&mut E) -> &mut Settable<E, I>,
+ property: SettablePropertyData<E, I>,
+}
+
+impl<E: SettableField<I>, const I: u8> SettableFieldData<E, I> {
+ /// Creates the data from its parts. This is called in the `#[elem]` macro.
+ pub const fn new(
+ name: &'static str,
+ docs: &'static str,
+ positional: bool,
+ get: fn(&E) -> &Settable<E, I>,
+ get_mut: fn(&mut E) -> &mut Settable<E, I>,
+ default: fn() -> E::Type,
+ slot: fn() -> &'static OnceLock<E::Type>,
+ ) -> Self {
+ Self {
+ get,
+ get_mut,
+ property: SettablePropertyData::new(name, docs, positional, default, slot),
+ }
+ }
+
+ /// Ensures that the property is folded on every access. See the
+ /// documentation of the [`Fold`] trait for more details.
+ pub const fn with_fold(mut self) -> Self
+ where
+ E::Type: Fold,
+ {
+ self.property.fold = Some(E::Type::fold);
+ self
+ }
+
+ /// Creates type-erased metadata and routines for a normal settable field.
+ pub const fn vtable() -> FieldVtable<Packed<E>>
+ where
+ E: SettableField<I>,
+ E::Type: Reflect + IntoValue + PartialEq,
+ {
+ FieldVtable {
+ name: E::FIELD.property.name,
+ docs: E::FIELD.property.docs,
+ positional: E::FIELD.property.positional,
+ required: false,
+ variadic: false,
+ settable: true,
+ synthesized: false,
+ input: || <E::Type as Reflect>::input(),
+ default: Some(|| E::default().into_value()),
+ has: |elem| (E::FIELD.get)(elem).is_set(),
+ get: |elem| (E::FIELD.get)(elem).as_option().clone().map(|v| v.into_value()),
+ get_with_styles: |elem, styles| {
+ Some((E::FIELD.get)(elem).get_cloned(styles).into_value())
+ },
+ get_from_styles: |styles| {
+ Some(styles.get_cloned::<E, I>(Field::new()).into_value())
+ },
+ materialize: |elem, styles| {
+ if !(E::FIELD.get)(elem).is_set() {
+ (E::FIELD.get_mut)(elem).set(styles.get_cloned::<E, I>(Field::new()));
+ }
+ },
+ eq: |a, b| (E::FIELD.get)(a).as_option() == (E::FIELD.get)(b).as_option(),
+ }
+ }
+}
+
+/// A field that has a default value and can be configured via a set rule, but
+/// is never present on elements.
+///
+/// This is provided for all `SettableField` impls through a blanket impl. In
+/// the case of `#[ghost]` fields, which only live in the style chain and not in
+/// elements, it is also implemented manually.
+pub trait SettableProperty<const I: u8>: NativeElement {
+ type Type: Clone;
+
+ const FIELD: SettablePropertyData<Self, I>;
+ const FOLD: Option<FoldFn<Self::Type>> = Self::FIELD.fold;
+
+ /// Produces an instance of the property's default value.
+ fn default() -> Self::Type {
+ // Avoid recreating an expensive instance over and over, but also
+ // avoid unnecessary lazy initialization for cheap types.
+ if std::mem::needs_drop::<Self::Type>() {
+ Self::default_ref().clone()
+ } else {
+ (Self::FIELD.default)()
+ }
+ }
+
+ /// Produces a static reference to this property's default value.
+ fn default_ref() -> &'static Self::Type {
+ (Self::FIELD.slot)().get_or_init(Self::FIELD.default)
+ }
+}
+
+impl<T, const I: u8> SettableProperty<I> for T
+where
+ T: SettableField<I>,
+{
+ type Type = <Self as SettableField<I>>::Type;
+
+ const FIELD: SettablePropertyData<Self, I> =
+ <Self as SettableField<I>>::FIELD.property;
+}
+
+/// Metadata and routines for a [`SettableProperty`].
+pub struct SettablePropertyData<E: SettableProperty<I>, const I: u8> {
+ name: &'static str,
+ docs: &'static str,
+ positional: bool,
+ default: fn() -> E::Type,
+ slot: fn() -> &'static OnceLock<E::Type>,
+ fold: Option<FoldFn<E::Type>>,
+}
+
+impl<E: SettableProperty<I>, const I: u8> SettablePropertyData<E, I> {
+ /// Creates the data from its parts. This is called in the `#[elem]` macro.
+ pub const fn new(
+ name: &'static str,
+ docs: &'static str,
+ positional: bool,
+ default: fn() -> E::Type,
+ slot: fn() -> &'static OnceLock<E::Type>,
+ ) -> Self {
+ Self { name, docs, positional, default, slot, fold: None }
+ }
+
+ /// Ensures that the property is folded on every access. See the
+ /// documentation of the [`Fold`] trait for more details.
+ pub const fn with_fold(self) -> Self
+ where
+ E::Type: Fold,
+ {
+ Self { fold: Some(E::Type::fold), ..self }
+ }
+
+ /// Creates type-erased metadata and routines for a `#[ghost]` field.
+ pub const fn vtable() -> FieldVtable<Packed<E>>
+ where
+ E: SettableProperty<I>,
+ E::Type: Reflect + IntoValue + PartialEq,
+ {
+ FieldVtable {
+ name: E::FIELD.name,
+ docs: E::FIELD.docs,
+ positional: E::FIELD.positional,
+ required: false,
+ variadic: false,
+ settable: true,
+ synthesized: false,
+ input: || <E::Type as Reflect>::input(),
+ default: Some(|| E::default().into_value()),
+ has: |_| false,
+ get: |_| None,
+ get_with_styles: |_, styles| {
+ Some(styles.get_cloned::<E, I>(Field::new()).into_value())
+ },
+ get_from_styles: |styles| {
+ Some(styles.get_cloned::<E, I>(Field::new()).into_value())
+ },
+ materialize: |_, _| {},
+ eq: |_, _| true,
+ }
+ }
+}
+
+/// A settable property that can be accessed by reference (because it is not
+/// folded).
+pub trait RefableProperty<const I: u8>: SettableProperty<I> {}
+
+/// A settable field of an element.
+///
+/// The field can be in two states: Unset or present.
+///
+/// See [`StyleChain`] for more details about the available accessor methods.
+#[derive(Copy, Clone, Hash)]
+pub struct Settable<E: NativeElement, const I: u8>(Option<E::Type>)
+where
+ E: SettableProperty<I>;
+
+impl<E: NativeElement, const I: u8> Settable<E, I>
+where
+ E: SettableProperty<I>,
+{
+ /// Creates a new unset instance.
+ pub fn new() -> Self {
+ Self(None)
+ }
+
+ /// Sets the instance to a value.
+ pub fn set(&mut self, value: E::Type) {
+ self.0 = Some(value);
+ }
+
+ /// Clears the value from the instance.
+ pub fn unset(&mut self) {
+ self.0 = None;
+ }
+
+ /// Views the type as an [`Option`] which is `Some` if the type is set
+ /// and `None` if it is unset.
+ pub fn as_option(&self) -> &Option<E::Type> {
+ &self.0
+ }
+
+ /// Views the type as a mutable [`Option`].
+ pub fn as_option_mut(&mut self) -> &mut Option<E::Type> {
+ &mut self.0
+ }
+
+ /// Whether the field is set.
+ pub fn is_set(&self) -> bool {
+ self.0.is_some()
+ }
+
+ /// Retrieves the value given styles. The styles are used if the value is
+ /// unset.
+ pub fn get<'a>(&'a self, styles: StyleChain<'a>) -> E::Type
+ where
+ E::Type: Copy,
+ {
+ self.get_cloned(styles)
+ }
+
+ /// Retrieves and clones the value given styles. The styles are used if the
+ /// value is unset or if it needs folding.
+ pub fn get_cloned<'a>(&'a self, styles: StyleChain<'a>) -> E::Type {
+ if let Some(fold) = E::FOLD {
+ let mut res = styles.get_cloned::<E, I>(Field::new());
+ if let Some(value) = &self.0 {
+ res = fold(value.clone(), res);
+ }
+ res
+ } else if let Some(value) = &self.0 {
+ value.clone()
+ } else {
+ styles.get_cloned::<E, I>(Field::new())
+ }
+ }
+
+ /// Retrieves a reference to the value given styles. The styles are used if
+ /// the value is unset.
+ pub fn get_ref<'a>(&'a self, styles: StyleChain<'a>) -> &'a E::Type
+ where
+ E: RefableProperty<I>,
+ {
+ if let Some(value) = &self.0 {
+ value
+ } else {
+ styles.get_ref::<E, I>(Field::new())
+ }
+ }
+
+ /// Retrieves the value and then immediately [resolves](Resolve) it.
+ pub fn resolve<'a>(&'a self, styles: StyleChain<'a>) -> <E::Type as Resolve>::Output
+ where
+ E::Type: Resolve,
+ {
+ self.get_cloned(styles).resolve(styles)
+ }
+}
+
+impl<E: NativeElement, const I: u8> Debug for Settable<E, I>
+where
+ E: SettableProperty<I>,
+ E::Type: Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
+impl<E: NativeElement, const I: u8> Default for Settable<E, I>
+where
+ E: SettableProperty<I>,
+{
+ fn default() -> Self {
+ Self(None)
+ }
+}
+
+impl<E: NativeElement, const I: u8> From<Option<E::Type>> for Settable<E, I>
+where
+ E: SettableProperty<I>,
+{
+ fn from(value: Option<E::Type>) -> Self {
+ Self(value)
+ }
+}
+
+/// An error arising when trying to access a field of content.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
+pub enum FieldAccessError {
+ Unknown,
+ Unset,
+}
+
+impl FieldAccessError {
+ /// Formats the error message given the content and the field name.
+ #[cold]
+ pub fn message(self, content: &Content, field: &str) -> EcoString {
+ let elem_name = content.elem().name();
+ match self {
+ FieldAccessError::Unknown => {
+ eco_format!("{elem_name} does not have field {}", field.repr())
+ }
+ FieldAccessError::Unset => {
+ eco_format!(
+ "field {} in {elem_name} is not known at this point",
+ field.repr()
+ )
+ }
+ }
+ }
+
+ /// Formats the error message for an `at` calls without a default value.
+ #[cold]
+ pub fn message_no_default(self, content: &Content, field: &str) -> EcoString {
+ let mut msg = self.message(content, field);
+ msg.push_str(" and no default was specified");
+ msg
+ }
+}
diff --git a/crates/typst-library/src/foundations/content.rs b/crates/typst-library/src/foundations/content/mod.rs
index 1855bb70..7ba790d8 100644
--- a/crates/typst-library/src/foundations/content.rs
+++ b/crates/typst-library/src/foundations/content/mod.rs
@@ -1,23 +1,33 @@
-use std::any::TypeId;
+mod element;
+mod field;
+mod packed;
+mod raw;
+mod vtable;
+
+pub use self::element::*;
+pub use self::field::*;
+pub use self::packed::Packed;
+pub use self::vtable::{ContentVtable, FieldVtable};
+#[doc(inline)]
+pub use typst_macros::elem;
+
use std::fmt::{self, Debug, Formatter};
-use std::hash::{Hash, Hasher};
+use std::hash::Hash;
use std::iter::{self, Sum};
-use std::marker::PhantomData;
-use std::ops::{Add, AddAssign, ControlFlow, Deref, DerefMut};
-use std::sync::Arc;
+use std::ops::{Add, AddAssign, ControlFlow};
use comemo::Tracked;
use ecow::{eco_format, EcoString};
use serde::{Serialize, Serializer};
+
use typst_syntax::Span;
-use typst_utils::{fat, singleton, LazyHash, SmallBitSet};
+use typst_utils::singleton;
use crate::diag::{SourceResult, StrResult};
use crate::engine::Engine;
use crate::foundations::{
- elem, func, scope, ty, Context, Dict, Element, Fields, IntoValue, Label,
- NativeElement, Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles,
- Value,
+ func, repr, scope, ty, Context, Dict, IntoValue, Label, Property, Recipe,
+ RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, Value,
};
use crate::introspection::Location;
use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides};
@@ -68,43 +78,14 @@ use crate::text::UnderlineElem;
/// elements the content is composed of and what fields they have.
/// Alternatively, you can inspect the output of the [`repr`] function.
#[ty(scope, cast)]
-#[derive(Clone, Hash)]
-#[allow(clippy::derived_hash_with_manual_eq)]
-pub struct Content {
- /// The partially element-dependent inner data.
- inner: Arc<Inner<dyn Bounds>>,
- /// The element's source code location.
- span: Span,
-}
-
-/// The inner representation behind the `Arc`.
-#[derive(Hash)]
-struct Inner<T: ?Sized + 'static> {
- /// An optional label attached to the element.
- label: Option<Label>,
- /// The element's location which identifies it in the layouted output.
- location: Option<Location>,
- /// Manages the element during realization.
- /// - If bit 0 is set, the element is prepared.
- /// - If bit n is set, the element is guarded against the n-th show rule
- /// recipe from the top of the style chain (counting from 1).
- lifecycle: SmallBitSet,
- /// The element's raw data.
- elem: LazyHash<T>,
-}
+#[derive(Clone, PartialEq, Hash)]
+#[repr(transparent)]
+pub struct Content(raw::RawContent);
impl Content {
/// Creates a new content from an element.
pub fn new<T: NativeElement>(elem: T) -> Self {
- Self {
- inner: Arc::new(Inner {
- label: None,
- location: None,
- lifecycle: SmallBitSet::new(),
- elem: elem.into(),
- }),
- span: Span::detached(),
- }
+ Self(raw::RawContent::new(elem))
}
/// Creates a empty sequence content.
@@ -114,25 +95,25 @@ impl Content {
/// Get the element of this content.
pub fn elem(&self) -> Element {
- self.inner.elem.dyn_elem()
+ self.0.elem()
}
/// Get the span of the content.
pub fn span(&self) -> Span {
- self.span
+ self.0.span()
}
/// Set the span of the content.
pub fn spanned(mut self, span: Span) -> Self {
- if self.span.is_detached() {
- self.span = span;
+ if self.0.span().is_detached() {
+ *self.0.span_mut() = span;
}
self
}
/// Get the label of the content.
pub fn label(&self) -> Option<Label> {
- self.inner.label
+ self.0.meta().label
}
/// Attach a label to the content.
@@ -143,7 +124,7 @@ impl Content {
/// Set the label of the content.
pub fn set_label(&mut self, label: Label) {
- self.make_mut().label = Some(label);
+ self.0.meta_mut().label = Some(label);
}
/// Assigns a location to the content.
@@ -159,28 +140,28 @@ impl Content {
/// Set the location of the content.
pub fn set_location(&mut self, location: Location) {
- self.make_mut().location = Some(location);
+ self.0.meta_mut().location = Some(location);
}
/// Check whether a show rule recipe is disabled.
pub fn is_guarded(&self, index: RecipeIndex) -> bool {
- self.inner.lifecycle.contains(index.0)
+ self.0.meta().lifecycle.contains(index.0)
}
/// Disable a show rule recipe.
pub fn guarded(mut self, index: RecipeIndex) -> Self {
- self.make_mut().lifecycle.insert(index.0);
+ self.0.meta_mut().lifecycle.insert(index.0);
self
}
/// Whether this content has already been prepared.
pub fn is_prepared(&self) -> bool {
- self.inner.lifecycle.contains(0)
+ self.0.meta().lifecycle.contains(0)
}
/// Mark this content as prepared.
pub fn mark_prepared(&mut self) {
- self.make_mut().lifecycle.insert(0);
+ self.0.meta_mut().lifecycle.insert(0);
}
/// Get a field by ID.
@@ -198,9 +179,14 @@ impl Content {
return Ok(label.into_value());
}
}
- match styles {
- Some(styles) => self.inner.elem.field_with_styles(id, styles),
- None => self.inner.elem.field(id),
+
+ match self.0.handle().field(id) {
+ Some(handle) => match styles {
+ Some(styles) => handle.get_with_styles(styles),
+ None => handle.get(),
+ }
+ .ok_or(FieldAccessError::Unset),
+ None => Err(FieldAccessError::Unknown),
}
}
@@ -215,8 +201,11 @@ impl Content {
.map(|label| label.into_value())
.ok_or(FieldAccessError::Unknown);
}
- let id = self.elem().field_id(name).ok_or(FieldAccessError::Unknown)?;
- self.get(id, None)
+
+ match self.elem().field_id(name).and_then(|id| self.0.handle().field(id)) {
+ Some(handle) => handle.get().ok_or(FieldAccessError::Unset),
+ None => Err(FieldAccessError::Unknown),
+ }
}
/// Get a field by ID, returning a missing field error if it does not exist.
@@ -240,7 +229,9 @@ impl Content {
/// Resolve all fields with the styles and save them in-place.
pub fn materialize(&mut self, styles: StyleChain) {
- self.make_mut().elem.materialize(styles);
+ for id in 0..self.elem().vtable().fields.len() as u8 {
+ self.0.handle_mut().field(id).unwrap().materialize(styles);
+ }
}
/// Create a new sequence element from multiples elements.
@@ -257,7 +248,7 @@ impl Content {
/// Whether the contained element is of type `T`.
pub fn is<T: NativeElement>(&self) -> bool {
- self.inner.elem.dyn_type_id() == TypeId::of::<T>()
+ self.0.is::<T>()
}
/// Downcasts the element to a packed value.
@@ -280,16 +271,6 @@ impl Content {
self.into_packed::<T>().map(Packed::unpack)
}
- /// Makes sure the content is not shared and returns a mutable reference to
- /// the inner data.
- fn make_mut(&mut self) -> &mut Inner<dyn Bounds> {
- let arc = &mut self.inner;
- if Arc::strong_count(arc) > 1 || Arc::weak_count(arc) > 0 {
- *self = arc.elem.dyn_clone(arc, self.span);
- }
- Arc::get_mut(&mut self.inner).unwrap()
- }
-
/// Whether the contained element has the given capability.
pub fn can<C>(&self) -> bool
where
@@ -304,13 +285,7 @@ impl Content {
where
C: ?Sized + 'static,
{
- // Safety: The vtable comes from the `Capable` implementation which
- // guarantees to return a matching vtable for `Packed<T>` and `C`.
- // Since any `Packed<T>` is a repr(transparent) `Content`, we can also
- // use a `*const Content` pointer.
- let vtable = self.elem().vtable()(TypeId::of::<C>())?;
- let data = self as *const Content as *const ();
- Some(unsafe { &*fat::from_raw_parts(data, vtable.as_ptr()) })
+ self.0.with::<C>()
}
/// Cast to a mutable trait object if the contained element has the given
@@ -319,18 +294,7 @@ impl Content {
where
C: ?Sized + 'static,
{
- // Safety: The vtable comes from the `Capable` implementation which
- // guarantees to return a matching vtable for `Packed<T>` and `C`.
- // Since any `Packed<T>` is a repr(transparent) `Content`, we can also
- // use a `*const Content` pointer.
- //
- // The resulting trait object contains an `&mut Packed<T>`. We do _not_
- // need to ensure that we hold the only reference to the `Arc` here
- // because `Packed<T>`'s DerefMut impl will take care of that if
- // mutable access is required.
- let vtable = self.elem().vtable()(TypeId::of::<C>())?;
- let data = self as *mut Content as *mut ();
- Some(unsafe { &mut *fat::from_raw_parts_mut(data, vtable.as_ptr()) })
+ self.0.with_mut::<C>()
}
/// Whether the content is an empty sequence.
@@ -372,6 +336,15 @@ impl Content {
Self::sequence(std::iter::repeat_with(|| self.clone()).take(count))
}
+ /// Sets a style property on the content.
+ pub fn set<E, const I: u8>(self, field: Field<E, I>, value: E::Type) -> Self
+ where
+ E: SettableProperty<I>,
+ E::Type: Debug + Clone + Hash + Send + Sync + 'static,
+ {
+ self.styled(Property::new(field, value))
+ }
+
/// Style this content with a style entry.
pub fn styled(mut self, style: impl Into<Style>) -> Self {
if let Some(style_elem) = self.to_packed_mut::<StyledElem>() {
@@ -476,7 +449,7 @@ impl Content {
// Call f on the element itself before recursively iterating its fields.
f(self.clone())?;
- for (_, value) in self.inner.elem.fields() {
+ for (_, value) in self.fields() {
walk_value(value, f)?;
}
ControlFlow::Continue(())
@@ -504,12 +477,12 @@ impl Content {
/// Link the content somewhere.
pub fn linked(self, dest: Destination) -> Self {
- self.styled(LinkElem::set_current(Some(dest)))
+ self.set(LinkElem::current, Some(dest))
}
/// Set alignments for this content.
pub fn aligned(self, align: Alignment) -> Self {
- self.styled(AlignElem::set_alignment(align))
+ self.set(AlignElem::alignment, align)
}
/// Pad this content at the sides.
@@ -562,7 +535,10 @@ impl Content {
return false;
};
- self.inner.elem.has(id)
+ match self.0.handle().field(id) {
+ Some(field) => field.has(),
+ None => false,
+ }
}
/// Access the specified field on the content. Returns the default value if
@@ -592,7 +568,12 @@ impl Content {
/// ```
#[func]
pub fn fields(&self) -> Dict {
- let mut dict = self.inner.elem.fields();
+ let mut dict = Dict::new();
+ for field in self.0.handle().fields() {
+ if let Some(value) = field.get() {
+ dict.insert(field.name.into(), value);
+ }
+ }
if let Some(label) = self.label() {
dict.insert("label".into(), label.into_value());
}
@@ -605,7 +586,7 @@ impl Content {
/// used with [counters]($counter), [state] and [queries]($query).
#[func]
pub fn location(&self) -> Option<Location> {
- self.inner.location
+ self.0.meta().location
}
}
@@ -617,7 +598,7 @@ impl Default for Content {
impl Debug for Content {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- self.inner.elem.fmt(f)
+ self.0.fmt(f)
}
}
@@ -627,16 +608,22 @@ impl<T: NativeElement> From<T> for Content {
}
}
-impl PartialEq for Content {
- fn eq(&self, other: &Self) -> bool {
- // Additional short circuit for different elements.
- self.elem() == other.elem() && self.inner.elem.dyn_eq(other)
- }
-}
-
impl Repr for Content {
fn repr(&self) -> EcoString {
- self.inner.elem.repr()
+ self.0.handle().repr().unwrap_or_else(|| {
+ let fields = self
+ .0
+ .handle()
+ .fields()
+ .filter_map(|field| field.get().map(|v| (field.name, v.repr())))
+ .map(|(name, value)| eco_format!("{name}: {value}"))
+ .collect::<Vec<_>>();
+ eco_format!(
+ "{}{}",
+ self.elem().name(),
+ repr::pretty_array_like(&fields, false),
+ )
+ })
}
}
@@ -717,190 +704,8 @@ impl Serialize for Content {
}
}
-/// The trait that combines all the other traits into a trait object.
-trait Bounds: Debug + Repr + Fields + Send + Sync + 'static {
- fn dyn_type_id(&self) -> TypeId;
- fn dyn_elem(&self) -> Element;
- fn dyn_clone(&self, inner: &Inner<dyn Bounds>, span: Span) -> Content;
- fn dyn_hash(&self, hasher: &mut dyn Hasher);
- fn dyn_eq(&self, other: &Content) -> bool;
-}
-
-impl<T: NativeElement> Bounds for T {
- fn dyn_type_id(&self) -> TypeId {
- TypeId::of::<Self>()
- }
-
- fn dyn_elem(&self) -> Element {
- Self::elem()
- }
-
- fn dyn_clone(&self, inner: &Inner<dyn Bounds>, span: Span) -> Content {
- Content {
- inner: Arc::new(Inner {
- label: inner.label,
- location: inner.location,
- lifecycle: inner.lifecycle.clone(),
- elem: LazyHash::reuse(self.clone(), &inner.elem),
- }),
- span,
- }
- }
-
- fn dyn_hash(&self, mut state: &mut dyn Hasher) {
- TypeId::of::<Self>().hash(&mut state);
- self.hash(&mut state);
- }
-
- fn dyn_eq(&self, other: &Content) -> bool {
- let Some(other) = other.to_packed::<Self>() else {
- return false;
- };
- *self == **other
- }
-}
-
-impl Hash for dyn Bounds {
- fn hash<H: Hasher>(&self, state: &mut H) {
- self.dyn_hash(state);
- }
-}
-
-/// A packed element of a static type.
-#[derive(Clone, PartialEq, Hash)]
-#[repr(transparent)]
-pub struct Packed<T: NativeElement>(
- /// Invariant: Must be of type `T`.
- Content,
- PhantomData<T>,
-);
-
-impl<T: NativeElement> Packed<T> {
- /// Pack element while retaining its static type.
- pub fn new(element: T) -> Self {
- // Safety: The element is known to be of type `T`.
- Packed(element.pack(), PhantomData)
- }
-
- /// Try to cast type-erased content into a statically known packed element.
- pub fn from_ref(content: &Content) -> Option<&Self> {
- if content.is::<T>() {
- // Safety:
- // - We have checked the type.
- // - Packed<T> is repr(transparent).
- return Some(unsafe { std::mem::transmute::<&Content, &Packed<T>>(content) });
- }
- None
- }
-
- /// Try to cast type-erased content into a statically known packed element.
- pub fn from_mut(content: &mut Content) -> Option<&mut Self> {
- if content.is::<T>() {
- // Safety:
- // - We have checked the type.
- // - Packed<T> is repr(transparent).
- return Some(unsafe {
- std::mem::transmute::<&mut Content, &mut Packed<T>>(content)
- });
- }
- None
- }
-
- /// Try to cast type-erased content into a statically known packed element.
- pub fn from_owned(content: Content) -> Result<Self, Content> {
- if content.is::<T>() {
- // Safety:
- // - We have checked the type.
- // - Packed<T> is repr(transparent).
- return Ok(unsafe { std::mem::transmute::<Content, Packed<T>>(content) });
- }
- Err(content)
- }
-
- /// Pack back into content.
- pub fn pack(self) -> Content {
- self.0
- }
-
- /// Extract the raw underlying element.
- pub fn unpack(self) -> T {
- // This function doesn't yet need owned self, but might in the future.
- (*self).clone()
- }
-
- /// The element's span.
- pub fn span(&self) -> Span {
- self.0.span()
- }
-
- /// Set the span of the element.
- pub fn spanned(self, span: Span) -> Self {
- Self(self.0.spanned(span), PhantomData)
- }
-
- /// Accesses the label of the element.
- pub fn label(&self) -> Option<Label> {
- self.0.label()
- }
-
- /// Accesses the location of the element.
- pub fn location(&self) -> Option<Location> {
- self.0.location()
- }
-
- /// Sets the location of the element.
- pub fn set_location(&mut self, location: Location) {
- self.0.set_location(location);
- }
-}
-
-impl<T: NativeElement> AsRef<T> for Packed<T> {
- fn as_ref(&self) -> &T {
- self
- }
-}
-
-impl<T: NativeElement> AsMut<T> for Packed<T> {
- fn as_mut(&mut self) -> &mut T {
- self
- }
-}
-
-impl<T: NativeElement> Deref for Packed<T> {
- type Target = T;
-
- fn deref(&self) -> &Self::Target {
- // Safety:
- // - Packed<T> guarantees that the content trait object wraps
- // an element of type `T`.
- // - This downcast works the same way as dyn Any's does. We can't reuse
- // that one because we don't want to pay the cost for every deref.
- let elem = &*self.0.inner.elem;
- unsafe { &*(elem as *const dyn Bounds as *const T) }
- }
-}
-
-impl<T: NativeElement> DerefMut for Packed<T> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- // Safety:
- // - Packed<T> guarantees that the content trait object wraps
- // an element of type `T`.
- // - We have guaranteed unique access thanks to `make_mut`.
- // - This downcast works the same way as dyn Any's does. We can't reuse
- // that one because we don't want to pay the cost for every deref.
- let elem = &mut *self.0.make_mut().elem;
- unsafe { &mut *(elem as *mut dyn Bounds as *mut T) }
- }
-}
-
-impl<T: NativeElement + Debug> Debug for Packed<T> {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- self.0.fmt(f)
- }
-}
-
/// A sequence of content.
-#[elem(Debug, Repr, PartialEq)]
+#[elem(Debug, Repr)]
pub struct SequenceElem {
/// The elements.
#[required]
@@ -922,19 +727,13 @@ impl Default for SequenceElem {
}
}
-impl PartialEq for SequenceElem {
- fn eq(&self, other: &Self) -> bool {
- self.children.iter().eq(other.children.iter())
- }
-}
-
impl Repr for SequenceElem {
fn repr(&self) -> EcoString {
if self.children.is_empty() {
"[]".into()
} else {
let elements = crate::foundations::repr::pretty_array_like(
- &self.children.iter().map(|c| c.inner.elem.repr()).collect::<Vec<_>>(),
+ &self.children.iter().map(|c| c.repr()).collect::<Vec<_>>(),
false,
);
eco_format!("sequence{}", elements)
@@ -974,49 +773,8 @@ impl Repr for StyledElem {
}
}
-/// Tries to extract the plain-text representation of the element.
-pub trait PlainText {
- /// Write this element's plain text into the given buffer.
- fn plain_text(&self, text: &mut EcoString);
-}
-
-/// An error arising when trying to access a field of content.
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum FieldAccessError {
- Unknown,
- Unset,
- Internal,
-}
-
-impl FieldAccessError {
- /// Formats the error message given the content and the field name.
- #[cold]
- pub fn message(self, content: &Content, field: &str) -> EcoString {
- let elem_name = content.elem().name();
- match self {
- FieldAccessError::Unknown => {
- eco_format!("{elem_name} does not have field {}", field.repr())
- }
- FieldAccessError::Unset => {
- eco_format!(
- "field {} in {elem_name} is not known at this point",
- field.repr()
- )
- }
- FieldAccessError::Internal => {
- eco_format!(
- "internal error when accessing field {} in {elem_name} – this is a bug",
- field.repr()
- )
- }
- }
- }
-
- /// Formats the error message for an `at` calls without a default value.
- #[cold]
- pub fn message_no_default(self, content: &Content, field: &str) -> EcoString {
- let mut msg = self.message(content, field);
- msg.push_str(" and no default was specified");
- msg
+impl<T: NativeElement> IntoValue for T {
+ fn into_value(self) -> Value {
+ Value::Content(self.pack())
}
}
diff --git a/crates/typst-library/src/foundations/content/packed.rs b/crates/typst-library/src/foundations/content/packed.rs
new file mode 100644
index 00000000..71bb66a9
--- /dev/null
+++ b/crates/typst-library/src/foundations/content/packed.rs
@@ -0,0 +1,147 @@
+use std::fmt::{self, Debug, Formatter};
+use std::hash::{Hash, Hasher};
+use std::marker::PhantomData;
+use std::ops::{Deref, DerefMut};
+
+use typst_syntax::Span;
+
+use crate::foundations::{Content, Label, NativeElement};
+use crate::introspection::Location;
+
+/// A packed element of a static type.
+#[derive(Clone)]
+#[repr(transparent)]
+pub struct Packed<T: NativeElement>(
+ /// Invariant: Must be of type `T`.
+ Content,
+ PhantomData<T>,
+);
+
+impl<T: NativeElement> Packed<T> {
+ /// Pack element while retaining its static type.
+ pub fn new(element: T) -> Self {
+ // Safety: The element is known to be of type `T`.
+ Packed(element.pack(), PhantomData)
+ }
+
+ /// Try to cast type-erased content into a statically known packed element.
+ pub fn from_ref(content: &Content) -> Option<&Self> {
+ if content.is::<T>() {
+ // Safety:
+ // - We have checked the type.
+ // - Packed<T> is repr(transparent).
+ return Some(unsafe { std::mem::transmute::<&Content, &Packed<T>>(content) });
+ }
+ None
+ }
+
+ /// Try to cast type-erased content into a statically known packed element.
+ pub fn from_mut(content: &mut Content) -> Option<&mut Self> {
+ if content.is::<T>() {
+ // Safety:
+ // - We have checked the type.
+ // - Packed<T> is repr(transparent).
+ return Some(unsafe {
+ std::mem::transmute::<&mut Content, &mut Packed<T>>(content)
+ });
+ }
+ None
+ }
+
+ /// Try to cast type-erased content into a statically known packed element.
+ pub fn from_owned(content: Content) -> Result<Self, Content> {
+ if content.is::<T>() {
+ // Safety:
+ // - We have checked the type.
+ // - Packed<T> is repr(transparent).
+ return Ok(unsafe { std::mem::transmute::<Content, Packed<T>>(content) });
+ }
+ Err(content)
+ }
+
+ /// Pack back into content.
+ pub fn pack(self) -> Content {
+ self.0
+ }
+
+ /// Extract the raw underlying element.
+ pub fn unpack(self) -> T {
+ // This function doesn't yet need owned self, but might in the future.
+ (*self).clone()
+ }
+
+ /// The element's span.
+ pub fn span(&self) -> Span {
+ self.0.span()
+ }
+
+ /// Set the span of the element.
+ pub fn spanned(self, span: Span) -> Self {
+ Self(self.0.spanned(span), PhantomData)
+ }
+
+ /// Accesses the label of the element.
+ pub fn label(&self) -> Option<Label> {
+ self.0.label()
+ }
+
+ /// Accesses the location of the element.
+ pub fn location(&self) -> Option<Location> {
+ self.0.location()
+ }
+
+ /// Sets the location of the element.
+ pub fn set_location(&mut self, location: Location) {
+ self.0.set_location(location);
+ }
+
+ pub fn as_content(&self) -> &Content {
+ &self.0
+ }
+}
+
+impl<T: NativeElement> AsRef<T> for Packed<T> {
+ fn as_ref(&self) -> &T {
+ self
+ }
+}
+
+impl<T: NativeElement> AsMut<T> for Packed<T> {
+ fn as_mut(&mut self) -> &mut T {
+ self
+ }
+}
+
+impl<T: NativeElement> Deref for Packed<T> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ // Safety: Packed<T> guarantees that the content is of element type `T`.
+ unsafe { (self.0).0.data::<T>() }
+ }
+}
+
+impl<T: NativeElement> DerefMut for Packed<T> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ // Safety: Packed<T> guarantees that the content is of element type `T`.
+ unsafe { (self.0).0.data_mut::<T>() }
+ }
+}
+
+impl<T: NativeElement + Debug> Debug for Packed<T> {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
+impl<T: NativeElement> PartialEq for Packed<T> {
+ fn eq(&self, other: &Self) -> bool {
+ self.0 == other.0
+ }
+}
+
+impl<T: NativeElement> Hash for Packed<T> {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.0.hash(state);
+ }
+}
diff --git a/crates/typst-library/src/foundations/content/raw.rs b/crates/typst-library/src/foundations/content/raw.rs
new file mode 100644
index 00000000..f5dfffd7
--- /dev/null
+++ b/crates/typst-library/src/foundations/content/raw.rs
@@ -0,0 +1,426 @@
+use std::any::TypeId;
+use std::fmt::{self, Debug, Formatter};
+use std::hash::{Hash, Hasher};
+use std::ptr::NonNull;
+use std::sync::atomic::{self, AtomicUsize, Ordering};
+
+use typst_syntax::Span;
+use typst_utils::{fat, HashLock, SmallBitSet};
+
+use super::vtable;
+use crate::foundations::{Element, Label, NativeElement, Packed};
+use crate::introspection::Location;
+
+/// The raw, low-level implementation of content.
+///
+/// The `ptr` + `elem` fields implement a fat pointer setup similar to an
+/// `Arc<Inner<dyn Trait>>`, but in a manual way, allowing us to have a custom
+/// [vtable].
+pub struct RawContent {
+ /// A type-erased pointer to an allocation containing two things:
+ /// - A header that is the same for all elements
+ /// - Element-specific `data` that holds the specific element
+ ///
+ /// This pointer is valid for both a `Header` and an `Inner<E>` where
+ /// `E::ELEM == self.elem` and can be freely cast between both. This is
+ /// possible because
+ /// - `Inner<E>` is `repr(C)`
+ /// - The first field of `Inner<E>` is `Header`
+ /// - ISO/IEC 9899:TC2 C standard § 6.7.2.1 - 13 states that a pointer to a
+ /// structure "points to its initial member" with no padding at the start
+ ptr: NonNull<Header>,
+ /// Describes which kind of element this content holds. This is used for
+ ///
+ /// - Direct comparisons, e.g. `is::<HeadingElem>()`
+ /// - Behavior: An `Element` is just a pointer to a `ContentVtable`
+ /// containing not just data, but also function pointers for various
+ /// element-specific operations that can be performed
+ ///
+ /// It is absolutely crucial that `elem == <E as NativeElement>::ELEM` for
+ /// `Inner<E>` pointed to by `ptr`. Otherwise, things will go very wrong
+ /// since we'd be using the wrong vtable.
+ elem: Element,
+ /// The content's span.
+ span: Span,
+}
+
+/// The allocated part of an element's representation.
+///
+/// This is `repr(C)` to ensure that a pointer to the whole structure may be
+/// cast to a pointer to its first field.
+#[repr(C)]
+struct Inner<E> {
+ /// It is crucial that this is the first field because we cast between
+ /// pointers to `Inner<E>` and pointers to `Header`. See the documentation
+ /// of `RawContent::ptr` for more details.
+ header: Header,
+ /// The element struct. E.g. `E = HeadingElem`.
+ data: E,
+}
+
+/// The header that is shared by all elements.
+struct Header {
+ /// The element's reference count. This works just like for `Arc`.
+ /// Unfortunately, we have to reimplement reference counting because we
+ /// have a custom fat pointer and `Arc` wouldn't know how to drop its
+ /// contents. Something with `ManuallyDrop<Arc<_>>` might also work, but at
+ /// that point we're not gaining much and with the way it's implemented now
+ /// we can also skip the unnecessary weak reference count.
+ refs: AtomicUsize,
+ /// Metadata for the element.
+ meta: Meta,
+ /// A cell for memoizing the hash of just the `data` part of the content.
+ hash: HashLock,
+}
+
+/// Metadata that elements can hold.
+#[derive(Clone, Hash)]
+pub(super) struct Meta {
+ /// An optional label attached to the element.
+ pub label: Option<Label>,
+ /// The element's location which identifies it in the laid-out output.
+ pub location: Option<Location>,
+ /// Manages the element during realization.
+ /// - If bit 0 is set, the element is prepared.
+ /// - If bit n is set, the element is guarded against the n-th show rule
+ /// recipe from the top of the style chain (counting from 1).
+ pub lifecycle: SmallBitSet,
+}
+
+impl RawContent {
+ /// Creates raw content wrapping an element, with all metadata set to
+ /// default (including a detached span).
+ pub(super) fn new<E: NativeElement>(data: E) -> Self {
+ Self::create(
+ data,
+ Meta {
+ label: None,
+ location: None,
+ lifecycle: SmallBitSet::new(),
+ },
+ HashLock::new(),
+ Span::detached(),
+ )
+ }
+
+ /// Creates and allocates raw content.
+ fn create<E: NativeElement>(data: E, meta: Meta, hash: HashLock, span: Span) -> Self {
+ let raw = Box::into_raw(Box::<Inner<E>>::new(Inner {
+ header: Header { refs: AtomicUsize::new(1), meta, hash },
+ data,
+ }));
+
+ // Safety: `Box` always holds a non-null pointer. See also
+ // `Box::into_non_null` (which is unstable).
+ let non_null = unsafe { NonNull::new_unchecked(raw) };
+
+ // Safety: See `RawContent::ptr`.
+ let ptr = non_null.cast::<Header>();
+
+ Self { ptr, elem: E::ELEM, span }
+ }
+
+ /// Destroys raw content and deallocates.
+ ///
+ /// # Safety
+ /// - The reference count must be zero.
+ /// - The raw content must be be of type `E`.
+ pub(super) unsafe fn drop_impl<E: NativeElement>(&mut self) {
+ debug_assert_eq!(self.header().refs.load(Ordering::Relaxed), 0);
+
+ // Safety:
+ // - The caller guarantees that the content is of type `E`.
+ // - Thus, `ptr` must have been created from `Box<Inner<E>>` (see
+ // `RawContent::ptr`).
+ // - And to clean it up, we can just reproduce our box.
+ unsafe {
+ let ptr = self.ptr.cast::<Inner<E>>();
+ drop(Box::<Inner<E>>::from_raw(ptr.as_ptr()));
+ }
+ }
+
+ /// Clones a packed element into new raw content.
+ pub(super) fn clone_impl<E: NativeElement>(elem: &Packed<E>) -> Self {
+ let raw = &elem.as_content().0;
+ let header = raw.header();
+ RawContent::create(
+ elem.as_ref().clone(),
+ header.meta.clone(),
+ header.hash.clone(),
+ raw.span,
+ )
+ }
+
+ /// Accesses the header part of the raw content.
+ fn header(&self) -> &Header {
+ // Safety: `self.ptr` is a valid pointer to a header structure.
+ unsafe { self.ptr.as_ref() }
+ }
+
+ /// Mutably accesses the header part of the raw content.
+ fn header_mut(&mut self) -> &mut Header {
+ self.make_unique();
+
+ // Safety:
+ // - `self.ptr` is a valid pointer to a header structure.
+ // - We have unique access to the backing allocation (just ensured).
+ unsafe { self.ptr.as_mut() }
+ }
+
+ /// Retrieves the contained element **without checking that the content is
+ /// of the correct type.**
+ ///
+ /// # Safety
+ /// This must be preceded by a check to [`is`]. The safe API for this is
+ /// [`Content::to_packed`] and the [`Packed`] struct.
+ pub(super) unsafe fn data<E: NativeElement>(&self) -> &E {
+ debug_assert!(self.is::<E>());
+
+ // Safety:
+ // - The caller guarantees that the content is of type `E`.
+ // - `self.ptr` is a valid pointer to an `Inner<E>` (see
+ // `RawContent::ptr`).
+ unsafe { &self.ptr.cast::<Inner<E>>().as_ref().data }
+ }
+
+ /// Retrieves the contained element mutably **without checking that the
+ /// content is of the correct type.**
+ ///
+ /// Ensures that the element's allocation is unique.
+ ///
+ /// # Safety
+ /// This must be preceded by a check to [`is`]. The safe API for this is
+ /// [`Content::to_packed_mut`] and the [`Packed`] struct.
+ pub(super) unsafe fn data_mut<E: NativeElement>(&mut self) -> &mut E {
+ debug_assert!(self.is::<E>());
+
+ // Ensure that the memoized hash is reset because we may mutate the
+ // element.
+ self.header_mut().hash.reset();
+
+ // Safety:
+ // - The caller guarantees that the content is of type `E`.
+ // - `self.ptr` is a valid pointer to an `Inner<E>` (see
+ // `RawContent::ptr`).
+ // - We have unique access to the backing allocation (due to header_mut).
+ unsafe { &mut self.ptr.cast::<Inner<E>>().as_mut().data }
+ }
+
+ /// Ensures that we have unique access to the backing allocation by cloning
+ /// if the reference count exceeds 1. This is used before performing
+ /// mutable operations, implementing a clone-on-write scheme.
+ fn make_unique(&mut self) {
+ if self.header().refs.load(Ordering::Relaxed) > 1 {
+ *self = self.handle().clone();
+ }
+ }
+
+ /// Retrieves the element this content is for.
+ pub(super) fn elem(&self) -> Element {
+ self.elem
+ }
+
+ /// Whether this content holds an element of type `E`.
+ pub(super) fn is<E: NativeElement>(&self) -> bool {
+ self.elem == E::ELEM
+ }
+
+ /// Retrieves the content's span.
+ pub(super) fn span(&self) -> Span {
+ self.span
+ }
+
+ /// Retrieves the content's span mutably.
+ pub(super) fn span_mut(&mut self) -> &mut Span {
+ &mut self.span
+ }
+
+ /// Retrieves the content's metadata.
+ pub(super) fn meta(&self) -> &Meta {
+ &self.header().meta
+ }
+
+ /// Retrieves the content's metadata mutably.
+ pub(super) fn meta_mut(&mut self) -> &mut Meta {
+ &mut self.header_mut().meta
+ }
+
+ /// Casts into a trait object for a given trait if the packed element
+ /// implements said trait.
+ pub(super) fn with<C>(&self) -> Option<&C>
+ where
+ C: ?Sized + 'static,
+ {
+ // Safety: The vtable comes from the `Capable` implementation which
+ // guarantees to return a matching vtable for `Packed<T>` and `C`. Since
+ // any `Packed<T>` is repr(transparent) with `Content` and `RawContent`,
+ // we can also use a `*const RawContent` pointer.
+ let vtable = (self.elem.vtable().capability)(TypeId::of::<C>())?;
+ let data = self as *const Self as *const ();
+ Some(unsafe { &*fat::from_raw_parts(data, vtable.as_ptr()) })
+ }
+
+ /// Casts into a mutable trait object for a given trait if the packed
+ /// element implements said trait.
+ pub(super) fn with_mut<C>(&mut self) -> Option<&mut C>
+ where
+ C: ?Sized + 'static,
+ {
+ // Safety: The vtable comes from the `Capable` implementation which
+ // guarantees to return a matching vtable for `Packed<T>` and `C`. Since
+ // any `Packed<T>` is repr(transparent) with `Content` and `RawContent`,
+ // we can also use a `*const Content` pointer.
+ //
+ // The resulting trait object contains an `&mut Packed<T>`. We do _not_
+ // need to ensure that we hold the only reference to the `Arc` here
+ // because `Packed<T>`'s DerefMut impl will take care of that if mutable
+ // access is required.
+ let vtable = (self.elem.vtable().capability)(TypeId::of::<C>())?;
+ let data = self as *mut Self as *mut ();
+ Some(unsafe { &mut *fat::from_raw_parts_mut(data, vtable.as_ptr()) })
+ }
+}
+
+impl RawContent {
+ /// Retrieves the element's vtable.
+ pub(super) fn handle(&self) -> vtable::ContentHandle<&RawContent> {
+ // Safety `self.elem.vtable()` is a matching vtable for `self`.
+ unsafe { vtable::Handle::new(self, self.elem.vtable()) }
+ }
+
+ /// Retrieves the element's vtable.
+ pub(super) fn handle_mut(&mut self) -> vtable::ContentHandle<&mut RawContent> {
+ // Safety `self.elem.vtable()` is a matching vtable for `self`.
+ unsafe { vtable::Handle::new(self, self.elem.vtable()) }
+ }
+
+ /// Retrieves the element's vtable.
+ pub(super) fn handle_pair<'a, 'b>(
+ &'a self,
+ other: &'b RawContent,
+ ) -> Option<vtable::ContentHandle<(&'a RawContent, &'b RawContent)>> {
+ (self.elem == other.elem).then(|| {
+ // Safety:
+ // - `self.elem.vtable()` is a matching vtable for `self`.
+ // - It's also matching for `other` because `self.elem == other.elem`.
+ unsafe { vtable::Handle::new((self, other), self.elem.vtable()) }
+ })
+ }
+}
+
+impl Debug for RawContent {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ self.handle().debug(f)
+ }
+}
+
+impl Clone for RawContent {
+ fn clone(&self) -> Self {
+ // See Arc's clone impl for details about memory ordering.
+ let prev = self.header().refs.fetch_add(1, Ordering::Relaxed);
+
+ // See Arc's clone impl details about guarding against incredibly
+ // degenerate programs.
+ if prev > isize::MAX as usize {
+ ref_count_overflow(self.ptr, self.elem, self.span);
+ }
+
+ Self { ptr: self.ptr, elem: self.elem, span: self.span }
+ }
+}
+
+impl Drop for RawContent {
+ fn drop(&mut self) {
+ // Drop our ref-count. If there was more than one content before
+ // (including this one), we shouldn't deallocate. See Arc's drop impl
+ // for details about memory ordering.
+ if self.header().refs.fetch_sub(1, Ordering::Release) != 1 {
+ return;
+ }
+
+ // See Arc's drop impl for details.
+ atomic::fence(Ordering::Acquire);
+
+ // Safety:
+ // No other content references the backing allocation (just checked)
+ unsafe {
+ self.handle_mut().drop();
+ }
+ }
+}
+
+impl PartialEq for RawContent {
+ fn eq(&self, other: &Self) -> bool {
+ let Some(handle) = self.handle_pair(other) else { return false };
+ handle
+ .eq()
+ .unwrap_or_else(|| handle.fields().all(|handle| handle.eq()))
+ }
+}
+
+impl Hash for RawContent {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.elem.hash(state);
+ let header = self.header();
+ header.meta.hash(state);
+ header.hash.get_or_insert_with(|| self.handle().hash()).hash(state);
+ self.span.hash(state);
+ }
+}
+
+// Safety:
+// - Works like `Arc`.
+// - `NativeElement` implies `Send` and `Sync`, see below.
+unsafe impl Sync for RawContent {}
+unsafe impl Send for RawContent {}
+
+fn _ensure_send_sync<T: NativeElement>() {
+ fn needs_send_sync<T: Send + Sync>() {}
+ needs_send_sync::<T>();
+}
+
+#[cold]
+fn ref_count_overflow(ptr: NonNull<Header>, elem: Element, span: Span) -> ! {
+ // Drop to decrement the ref count to counter the increment in `clone()`
+ drop(RawContent { ptr, elem, span });
+ panic!("reference count overflow");
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::foundations::{NativeElement, Repr, StyleChain, Value};
+ use crate::introspection::Location;
+ use crate::model::HeadingElem;
+ use crate::text::TextElem;
+
+ #[test]
+ fn test_miri() {
+ let styles = StyleChain::default();
+
+ let mut first = HeadingElem::new(TextElem::packed("Hi!")).with_offset(2).pack();
+ let hash1 = typst_utils::hash128(&first);
+ first.set_location(Location::new(10));
+ let _ = format!("{first:?}");
+ let _ = first.repr();
+
+ assert!(first.is::<HeadingElem>());
+ assert!(!first.is::<TextElem>());
+ assert_eq!(first.to_packed::<TextElem>(), None);
+ assert_eq!(first.location(), Some(Location::new(10)));
+ assert_eq!(first.field_by_name("offset"), Ok(Value::Int(2)));
+ assert!(!first.has("depth".into()));
+
+ let second = first.clone();
+ first.materialize(styles);
+
+ let first_packed = first.to_packed::<HeadingElem>().unwrap();
+ let second_packed = second.to_packed::<HeadingElem>().unwrap();
+
+ assert!(first.has("depth".into()));
+ assert!(!second.has("depth".into()));
+ assert!(first_packed.depth.is_set());
+ assert!(!second_packed.depth.is_set());
+ assert_ne!(first, second);
+ assert_ne!(hash1, typst_utils::hash128(&first));
+ }
+}
diff --git a/crates/typst-library/src/foundations/content/vtable.rs b/crates/typst-library/src/foundations/content/vtable.rs
new file mode 100644
index 00000000..fdc9d423
--- /dev/null
+++ b/crates/typst-library/src/foundations/content/vtable.rs
@@ -0,0 +1,383 @@
+//! A custom [vtable] implementation for content.
+//!
+//! This is similar to what is generated by the Rust compiler under the hood
+//! when using trait objects. However, ours has two key advantages:
+//!
+//! - It can store a _slice_ of sub-vtables for field-specific operations.
+//! - It can store not only methods, but also plain data, allowing us to access
+//! that data without going through dynamic dispatch.
+//!
+//! Because our vtable pointers are backed by `static` variables, we can also
+//! perform checks for element types by comparing raw vtable pointers giving us
+//! `RawContent::is` without dynamic dispatch.
+//!
+//! Overall, the custom vtable gives us just a little more flexibility and
+//! optimizability than using built-in trait objects.
+//!
+//! Note that all vtable methods receive elements of type `Packed<E>`, but some
+//! only perform actions on the `E` itself, with the shared part kept outside of
+//! the vtable (e.g. `hash`), while some perform the full action (e.g. `clone`
+//! as it needs to return new, fully populated raw content). Which one it is, is
+//! documented for each.
+//!
+//! # Safety
+//! This module contains a lot of `unsafe` keywords, but almost all of it is the
+//! same and quite straightfoward. All function pointers that operate on a
+//! specific element type are marked as unsafe. In combination with `repr(C)`,
+//! this grants us the ability to safely transmute a `ContentVtable<Packed<E>>`
+//! into a `ContentVtable<RawContent>` (or just short `ContentVtable`). Callers
+//! of functions marked as unsafe have to guarantee that the `ContentVtable` was
+//! transmuted from the same `E` as the RawContent was constructed from. The
+//! `Handle` struct provides a safe access layer, moving the guarantee that the
+//! vtable is matching into a single spot.
+//!
+//! [vtable]: https://en.wikipedia.org/wiki/Virtual_method_table
+
+use std::any::TypeId;
+use std::fmt::{self, Debug, Formatter};
+use std::ops::Deref;
+use std::ptr::NonNull;
+
+use ecow::EcoString;
+
+use super::raw::RawContent;
+use crate::diag::SourceResult;
+use crate::engine::Engine;
+use crate::foundations::{
+ Args, CastInfo, Construct, Content, LazyElementStore, NativeElement, NativeScope,
+ Packed, Repr, Scope, Set, StyleChain, Styles, Value,
+};
+use crate::text::{Lang, LocalName, Region};
+
+/// Encapsulates content and a vtable, granting safe access to vtable operations.
+pub(super) struct Handle<T, V: 'static>(T, &'static V);
+
+impl<T, V> Handle<T, V> {
+ /// Produces a new handle from content and a vtable.
+ ///
+ /// # Safety
+ /// The content and vtable must be matching, i.e. `vtable` must be derived
+ /// from the content's vtable.
+ pub(super) unsafe fn new(content: T, vtable: &'static V) -> Self {
+ Self(content, vtable)
+ }
+}
+
+impl<T, V> Deref for Handle<T, V> {
+ type Target = V;
+
+ fn deref(&self) -> &Self::Target {
+ self.1
+ }
+}
+
+pub(super) type ContentHandle<T> = Handle<T, ContentVtable>;
+pub(super) type FieldHandle<T> = Handle<T, FieldVtable>;
+
+/// A vtable for performing element-specific actions on type-erased content.
+/// Also contains general metadata for the specific element.
+#[repr(C)]
+pub struct ContentVtable<T: 'static = RawContent> {
+ /// The element's normal name, as in code.
+ pub(super) name: &'static str,
+ /// The element's title-cased name.
+ pub(super) title: &'static str,
+ /// The element's documentation (as Markdown).
+ pub(super) docs: &'static str,
+ /// Search keywords for the documentation.
+ pub(super) keywords: &'static [&'static str],
+
+ /// Subvtables for all fields of the element.
+ pub(super) fields: &'static [FieldVtable<T>],
+ /// Determines the ID for a field name. This is a separate function instead
+ /// of searching through `fields` so that Rust can generate optimized code
+ /// for the string matching.
+ pub(super) field_id: fn(name: &str) -> Option<u8>,
+
+ /// The constructor of the element.
+ pub(super) construct: fn(&mut Engine, &mut Args) -> SourceResult<Content>,
+ /// The set rule of the element.
+ pub(super) set: fn(&mut Engine, &mut Args) -> SourceResult<Styles>,
+ /// The element's local name in a specific lang-region pairing.
+ pub(super) local_name: Option<fn(Lang, Option<Region>) -> &'static str>,
+ /// Produces the associated [`Scope`] of the element.
+ pub(super) scope: fn() -> Scope,
+ /// If the `capability` function returns `Some(p)`, then `p` must be a valid
+ /// pointer to a native Rust vtable of `Packed<Self>` w.r.t to the trait `C`
+ /// where `capability` is `TypeId::of::<dyn C>()`.
+ pub(super) capability: fn(capability: TypeId) -> Option<NonNull<()>>,
+
+ /// The `Drop` impl (for the whole raw content). The content must have a
+ /// reference count of zero and may not be used anymore after `drop` was
+ /// called.
+ pub(super) drop: unsafe fn(&mut RawContent),
+ /// The `Clone` impl (for the whole raw content).
+ pub(super) clone: unsafe fn(&T) -> RawContent,
+ /// The `Hash` impl (for just the element).
+ pub(super) hash: unsafe fn(&T) -> u128,
+ /// The `Debug` impl (for just the element).
+ pub(super) debug: unsafe fn(&T, &mut Formatter) -> fmt::Result,
+ /// The `PartialEq` impl (for just the element). If this is `None`,
+ /// field-wise equality checks (via `FieldVtable`) should be performed.
+ pub(super) eq: Option<unsafe fn(&T, &T) -> bool>,
+ /// The `Repr` impl (for just the element). If this is `None`, a generic
+ /// name + fields representation should be produced.
+ pub(super) repr: Option<unsafe fn(&T) -> EcoString>,
+
+ /// Produces a reference to a `static` variable holding a `LazyElementStore`
+ /// that is unique for this element and can be populated with data that is
+ /// somewhat costly to initialize at runtime and shouldn't be initialized
+ /// over and over again. Must be a function rather than a direct reference
+ /// so that we can store the vtable in a `const` without Rust complaining
+ /// about the presence of interior mutability.
+ pub(super) store: fn() -> &'static LazyElementStore,
+}
+
+impl ContentVtable {
+ /// Creates the vtable for an element.
+ pub const fn new<E: NativeElement>(
+ name: &'static str,
+ title: &'static str,
+ docs: &'static str,
+ fields: &'static [FieldVtable<Packed<E>>],
+ field_id: fn(name: &str) -> Option<u8>,
+ capability: fn(TypeId) -> Option<NonNull<()>>,
+ store: fn() -> &'static LazyElementStore,
+ ) -> ContentVtable<Packed<E>> {
+ ContentVtable {
+ name,
+ title,
+ docs,
+ keywords: &[],
+ fields,
+ field_id,
+ construct: <E as Construct>::construct,
+ set: <E as Set>::set,
+ local_name: None,
+ scope: || Scope::new(),
+ capability,
+ drop: RawContent::drop_impl::<E>,
+ clone: RawContent::clone_impl::<E>,
+ hash: |elem| typst_utils::hash128(elem.as_ref()),
+ debug: |elem, f| Debug::fmt(elem.as_ref(), f),
+ eq: None,
+ repr: None,
+ store,
+ }
+ }
+
+ /// Retrieves the vtable of the element with the given ID.
+ pub fn field(&self, id: u8) -> Option<&'static FieldVtable> {
+ self.fields.get(usize::from(id))
+ }
+}
+
+impl<E: NativeElement> ContentVtable<Packed<E>> {
+ /// Attaches search keywords for the documentation.
+ pub const fn with_keywords(mut self, keywords: &'static [&'static str]) -> Self {
+ self.keywords = keywords;
+ self
+ }
+
+ /// Takes a [`Repr`] impl into account.
+ pub const fn with_repr(mut self) -> Self
+ where
+ E: Repr,
+ {
+ self.repr = Some(|e| E::repr(&**e));
+ self
+ }
+
+ /// Takes a [`PartialEq`] impl into account.
+ pub const fn with_partial_eq(mut self) -> Self
+ where
+ E: PartialEq,
+ {
+ self.eq = Some(|a, b| E::eq(&**a, &**b));
+ self
+ }
+
+ /// Takes a [`LocalName`] impl into account.
+ pub const fn with_local_name(mut self) -> Self
+ where
+ Packed<E>: LocalName,
+ {
+ self.local_name = Some(<Packed<E> as LocalName>::local_name);
+ self
+ }
+
+ /// Takes a [`NativeScope`] impl into account.
+ pub const fn with_scope(mut self) -> Self
+ where
+ E: NativeScope,
+ {
+ self.scope = || E::scope();
+ self
+ }
+
+ /// Type-erases the data.
+ pub const fn erase(self) -> ContentVtable {
+ // Safety:
+ // - `ContentVtable` is `repr(C)`.
+ // - `ContentVtable` does not hold any `E`-specific data except for
+ // function pointers.
+ // - All functions pointers have the same memory layout.
+ // - All functions containing `E` are marked as unsafe and callers need
+ // to uphold the guarantee that they only call them with raw content
+ // that is of type `E`.
+ // - `Packed<E>` and `RawContent` have the exact same memory layout
+ // because of `repr(transparent)`.
+ unsafe {
+ std::mem::transmute::<ContentVtable<Packed<E>>, ContentVtable<RawContent>>(
+ self,
+ )
+ }
+ }
+}
+
+impl<T> ContentHandle<T> {
+ /// Provides safe access to operations for the field with the given `id`.
+ pub(super) fn field(self, id: u8) -> Option<FieldHandle<T>> {
+ self.fields.get(usize::from(id)).map(|vtable| {
+ // Safety: Field vtables are of same type as the content vtable.
+ unsafe { Handle::new(self.0, vtable) }
+ })
+ }
+
+ /// Provides safe access to all field operations.
+ pub(super) fn fields(self) -> impl Iterator<Item = FieldHandle<T>>
+ where
+ T: Copy,
+ {
+ self.fields.iter().map(move |vtable| {
+ // Safety: Field vtables are of same type as the content vtable.
+ unsafe { Handle::new(self.0, vtable) }
+ })
+ }
+}
+
+impl ContentHandle<&RawContent> {
+ /// See [`ContentVtable::debug`].
+ pub fn debug(&self, f: &mut Formatter) -> fmt::Result {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.debug)(self.0, f) }
+ }
+
+ /// See [`ContentVtable::repr`].
+ pub fn repr(&self) -> Option<EcoString> {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { self.1.repr.map(|f| f(self.0)) }
+ }
+
+ /// See [`ContentVtable::clone`].
+ pub fn clone(&self) -> RawContent {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.clone)(self.0) }
+ }
+
+ /// See [`ContentVtable::hash`].
+ pub fn hash(&self) -> u128 {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.hash)(self.0) }
+ }
+}
+
+impl ContentHandle<&mut RawContent> {
+ /// See [`ContentVtable::drop`].
+ pub unsafe fn drop(&mut self) {
+ // Safety:
+ // - `Handle` has the invariant that the vtable is matching.
+ // - The caller satifies the requirements of `drop`
+ unsafe { (self.1.drop)(self.0) }
+ }
+}
+
+impl ContentHandle<(&RawContent, &RawContent)> {
+ /// See [`ContentVtable::eq`].
+ pub fn eq(&self) -> Option<bool> {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ let (a, b) = self.0;
+ unsafe { self.1.eq.map(|f| f(a, b)) }
+ }
+}
+
+/// A vtable for performing field-specific actions on type-erased
+/// content. Also contains general metadata for the specific field.
+#[repr(C)]
+pub struct FieldVtable<T: 'static = RawContent> {
+ /// The field's name, as in code.
+ pub(super) name: &'static str,
+ /// The fields's documentation (as Markdown).
+ pub(super) docs: &'static str,
+
+ /// Whether the field's parameter is positional.
+ pub(super) positional: bool,
+ /// Whether the field's parameter is variadic.
+ pub(super) variadic: bool,
+ /// Whether the field's parameter is required.
+ pub(super) required: bool,
+ /// Whether the field can be set via a set rule.
+ pub(super) settable: bool,
+ /// Whether the field is synthesized (i.e. initially not present).
+ pub(super) synthesized: bool,
+ /// Reflects what types the field's parameter accepts.
+ pub(super) input: fn() -> CastInfo,
+ /// Produces the default value of the field, if any. This would e.g. be
+ /// `None` for a required parameter.
+ pub(super) default: Option<fn() -> Value>,
+
+ /// Whether the field is set on the given element. Always true for required
+ /// fields, but can be false for settable or synthesized fields.
+ pub(super) has: unsafe fn(elem: &T) -> bool,
+ /// Retrieves the field and [turns it into a
+ /// value](crate::foundations::IntoValue).
+ pub(super) get: unsafe fn(elem: &T) -> Option<Value>,
+ /// Retrieves the field given styles. The resulting value may come from the
+ /// element, the style chain, or a mix (if it's a
+ /// [`Fold`](crate::foundations::Fold) field).
+ pub(super) get_with_styles: unsafe fn(elem: &T, StyleChain) -> Option<Value>,
+ /// Retrieves the field just from the styles.
+ pub(super) get_from_styles: fn(StyleChain) -> Option<Value>,
+ /// Sets the field from the styles if it is currently unset. (Or merges
+ /// with the style data in case of a `Fold` field).
+ pub(super) materialize: unsafe fn(elem: &mut T, styles: StyleChain),
+ /// Compares the field for equality.
+ pub(super) eq: unsafe fn(a: &T, b: &T) -> bool,
+}
+
+impl FieldHandle<&RawContent> {
+ /// See [`FieldVtable::has`].
+ pub fn has(&self) -> bool {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.has)(self.0) }
+ }
+
+ /// See [`FieldVtable::get`].
+ pub fn get(&self) -> Option<Value> {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.get)(self.0) }
+ }
+
+ /// See [`FieldVtable::get_with_styles`].
+ pub fn get_with_styles(&self, styles: StyleChain) -> Option<Value> {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.get_with_styles)(self.0, styles) }
+ }
+}
+
+impl FieldHandle<&mut RawContent> {
+ /// See [`FieldVtable::materialize`].
+ pub fn materialize(&mut self, styles: StyleChain) {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ unsafe { (self.1.materialize)(self.0, styles) }
+ }
+}
+
+impl FieldHandle<(&RawContent, &RawContent)> {
+ /// See [`FieldVtable::eq`].
+ pub fn eq(&self) -> bool {
+ // Safety: `Handle` has the invariant that the vtable is matching.
+ let (a, b) = self.0;
+ unsafe { (self.1.eq)(a, b) }
+ }
+}
diff --git a/crates/typst-library/src/foundations/mod.rs b/crates/typst-library/src/foundations/mod.rs
index 6840f855..382beb2c 100644
--- a/crates/typst-library/src/foundations/mod.rs
+++ b/crates/typst-library/src/foundations/mod.rs
@@ -17,7 +17,6 @@ mod datetime;
mod decimal;
mod dict;
mod duration;
-mod element;
mod fields;
mod float;
mod func;
@@ -49,7 +48,6 @@ pub use self::datetime::*;
pub use self::decimal::*;
pub use self::dict::*;
pub use self::duration::*;
-pub use self::element::*;
pub use self::fields::*;
pub use self::float::*;
pub use self::func::*;
diff --git a/crates/typst-library/src/foundations/scope.rs b/crates/typst-library/src/foundations/scope.rs
index e1ce61b8..838584cc 100644
--- a/crates/typst-library/src/foundations/scope.rs
+++ b/crates/typst-library/src/foundations/scope.rs
@@ -8,8 +8,7 @@ use typst_syntax::Span;
use crate::diag::{bail, DeprecationSink, HintedStrResult, HintedString, StrResult};
use crate::foundations::{
- Element, Func, IntoValue, NativeElement, NativeFunc, NativeFuncData, NativeType,
- Type, Value,
+ Func, IntoValue, NativeElement, NativeFunc, NativeFuncData, NativeType, Value,
};
use crate::{Category, Library};
@@ -149,15 +148,15 @@ impl Scope {
/// Define a native type.
#[track_caller]
pub fn define_type<T: NativeType>(&mut self) -> &mut Binding {
- let data = T::data();
- self.define(data.name, Type::from(data))
+ let ty = T::ty();
+ self.define(ty.short_name(), ty)
}
/// Define a native element.
#[track_caller]
pub fn define_elem<T: NativeElement>(&mut self) -> &mut Binding {
- let data = T::data();
- self.define(data.name, Element::from(data))
+ let elem = T::ELEM;
+ self.define(elem.name(), elem)
}
/// Define a built-in with compile-time known name and returns a mutable
diff --git a/crates/typst-library/src/foundations/selector.rs b/crates/typst-library/src/foundations/selector.rs
index bf5449d9..8f264a81 100644
--- a/crates/typst-library/src/foundations/selector.rs
+++ b/crates/typst-library/src/foundations/selector.rs
@@ -21,12 +21,12 @@ macro_rules! __select_where {
let mut fields = ::smallvec::SmallVec::new();
$(
fields.push((
- <$ty as $crate::foundations::Fields>::Enum::$field as u8,
+ <$ty>::$field.index(),
$crate::foundations::IntoValue::into_value($value),
));
)*
$crate::foundations::Selector::Elem(
- <$ty as $crate::foundations::NativeElement>::elem(),
+ <$ty as $crate::foundations::NativeElement>::ELEM,
Some(fields),
)
}};
diff --git a/crates/typst-library/src/foundations/styles.rs b/crates/typst-library/src/foundations/styles.rs
index d124f2c8..978b47d5 100644
--- a/crates/typst-library/src/foundations/styles.rs
+++ b/crates/typst-library/src/foundations/styles.rs
@@ -12,8 +12,8 @@ use typst_utils::LazyHash;
use crate::diag::{SourceResult, Trace, Tracepoint};
use crate::engine::Engine;
use crate::foundations::{
- cast, ty, Content, Context, Element, Func, NativeElement, OneOrMultiple, Repr,
- Selector,
+ cast, ty, Content, Context, Element, Field, Func, NativeElement, OneOrMultiple,
+ RefableProperty, Repr, Selector, SettableProperty,
};
use crate::text::{FontFamily, FontList, TextElem};
@@ -48,7 +48,16 @@ impl Styles {
/// If the property needs folding and the value is already contained in the
/// style map, `self` contributes the outer values and `value` is the inner
/// one.
- pub fn set(&mut self, style: impl Into<Style>) {
+ pub fn set<E, const I: u8>(&mut self, field: Field<E, I>, value: E::Type)
+ where
+ E: SettableProperty<I>,
+ E::Type: Debug + Clone + Hash + Send + Sync + 'static,
+ {
+ self.push(Property::new(field, value));
+ }
+
+ /// Add a new style to the list.
+ pub fn push(&mut self, style: impl Into<Style>) {
self.0.push(LazyHash::new(style.into()));
}
@@ -101,22 +110,25 @@ impl Styles {
}
/// Whether there is a style for the given field of the given element.
- pub fn has<T: NativeElement>(&self, field: u8) -> bool {
- let elem = T::elem();
+ pub fn has<E: NativeElement, const I: u8>(&self, _: Field<E, I>) -> bool {
+ let elem = E::ELEM;
self.0
.iter()
.filter_map(|style| style.property())
- .any(|property| property.is_of(elem) && property.id == field)
+ .any(|property| property.is_of(elem) && property.id == I)
}
/// Set a font family composed of a preferred family and existing families
/// from a style chain.
pub fn set_family(&mut self, preferred: FontFamily, existing: StyleChain) {
- self.set(TextElem::set_font(FontList(
- std::iter::once(preferred)
- .chain(TextElem::font_in(existing).into_iter().cloned())
- .collect(),
- )));
+ self.set(
+ TextElem::font,
+ FontList(
+ std::iter::once(preferred)
+ .chain(existing.get_ref(TextElem::font).into_iter().cloned())
+ .collect(),
+ ),
+ );
}
}
@@ -281,14 +293,14 @@ pub struct Property {
impl Property {
/// Create a new property from a key-value pair.
- pub fn new<E, T>(id: u8, value: T) -> Self
+ pub fn new<E, const I: u8>(_: Field<E, I>, value: E::Type) -> Self
where
- E: NativeElement,
- T: Debug + Clone + Hash + Send + Sync + 'static,
+ E: SettableProperty<I>,
+ E::Type: Debug + Clone + Hash + Send + Sync + 'static,
{
Self {
- elem: E::elem(),
- id,
+ elem: E::ELEM,
+ id: I,
value: Block::new(value),
span: Span::detached(),
liftable: false,
@@ -340,8 +352,11 @@ impl Block {
}
/// Downcasts the block to the specified type.
- fn downcast<T: 'static>(&self) -> Option<&T> {
- self.0.as_any().downcast_ref()
+ fn downcast<T: 'static>(&self, func: Element, id: u8) -> &T {
+ self.0
+ .as_any()
+ .downcast_ref()
+ .unwrap_or_else(|| block_wrong_type(func, id, self))
}
}
@@ -528,90 +543,113 @@ impl<'a> StyleChain<'a> {
Self { head: &root.0, tail: None }
}
- /// Make the given chainable the first link of this chain.
+ /// Retrieves the value of the given field from the style chain.
///
- /// The resulting style chain contains styles from `local` as well as
- /// `self`. The ones from `local` take precedence over the ones from
- /// `self`. For folded properties `local` contributes the inner value.
- pub fn chain<'b, C>(&'b self, local: &'b C) -> StyleChain<'b>
+ /// A `Field` value is a zero-sized value that specifies which field of an
+ /// element you want to retrieve on the type-system level. It also ensures
+ /// that Rust can infer the correct return type.
+ ///
+ /// Should be preferred over [`get_cloned`](Self::get_cloned) or
+ /// [`get_ref`](Self::get_ref), but is only available for [`Copy`] types.
+ /// For other types an explicit decision needs to be made whether cloning is
+ /// necessary.
+ pub fn get<E, const I: u8>(self, field: Field<E, I>) -> E::Type
where
- C: Chainable + ?Sized,
+ E: SettableProperty<I>,
+ E::Type: Copy,
{
- Chainable::chain(local, self)
+ self.get_cloned(field)
}
- /// Cast the first value for the given property in the chain.
- pub fn get<T: Clone + 'static>(
- self,
- func: Element,
- id: u8,
- inherent: Option<&T>,
- default: impl Fn() -> T,
- ) -> T {
- self.properties::<T>(func, id, inherent)
- .next()
- .cloned()
- .unwrap_or_else(default)
+ /// Retrieves and clones the value from the style chain.
+ ///
+ /// Prefer [`get`](Self::get) if the type is `Copy` and
+ /// [`get_ref`](Self::get_ref) if a reference suffices.
+ pub fn get_cloned<E, const I: u8>(self, _: Field<E, I>) -> E::Type
+ where
+ E: SettableProperty<I>,
+ {
+ if let Some(fold) = E::FOLD {
+ self.get_folded::<E::Type>(E::ELEM, I, fold, E::default())
+ } else {
+ self.get_unfolded::<E::Type>(E::ELEM, I)
+ .cloned()
+ .unwrap_or_else(E::default)
+ }
}
- /// Cast the first value for the given property in the chain,
- /// returning a borrowed value.
- pub fn get_ref<T: 'static>(
+ /// Retrieves a reference to the value of the given field from the style
+ /// chain.
+ ///
+ /// Not possible if the value needs folding.
+ pub fn get_ref<E, const I: u8>(self, _: Field<E, I>) -> &'a E::Type
+ where
+ E: RefableProperty<I>,
+ {
+ self.get_unfolded(E::ELEM, I).unwrap_or_else(|| E::default_ref())
+ }
+
+ /// Retrieves the value and then immediately [resolves](Resolve) it.
+ pub fn resolve<E, const I: u8>(
self,
- func: Element,
- id: u8,
- inherent: Option<&'a T>,
- default: impl Fn() -> &'a T,
- ) -> &'a T {
- self.properties::<T>(func, id, inherent)
- .next()
- .unwrap_or_else(default)
+ field: Field<E, I>,
+ ) -> <E::Type as Resolve>::Output
+ where
+ E: SettableProperty<I>,
+ E::Type: Resolve,
+ {
+ self.get_cloned(field).resolve(self)
+ }
+
+ /// Retrieves a reference to a field, also taking into account the
+ /// instance's value if any.
+ fn get_unfolded<T: 'static>(self, func: Element, id: u8) -> Option<&'a T> {
+ self.find(func, id).map(|block| block.downcast(func, id))
}
- /// Cast the first value for the given property in the chain, taking
- /// `Fold` implementations into account.
- pub fn get_folded<T: Fold + Clone + 'static>(
+ /// Retrieves a reference to a field, also taking into account the
+ /// instance's value if any.
+ fn get_folded<T: 'static + Clone>(
self,
func: Element,
id: u8,
- inherent: Option<&T>,
- default: impl Fn() -> T,
+ fold: fn(T, T) -> T,
+ default: T,
) -> T {
- fn next<T: Fold>(
- mut values: impl Iterator<Item = T>,
- default: &impl Fn() -> T,
- ) -> T {
- values
- .next()
- .map(|value| value.fold(next(values, default)))
- .unwrap_or_else(default)
+ let iter = self
+ .properties(func, id)
+ .map(|block| block.downcast::<T>(func, id).clone());
+
+ if let Some(folded) = iter.reduce(fold) {
+ fold(folded, default)
+ } else {
+ default
}
- next(self.properties::<T>(func, id, inherent).cloned(), &default)
}
/// Iterate over all values for the given property in the chain.
- fn properties<T: 'static>(
- self,
- func: Element,
- id: u8,
- inherent: Option<&'a T>,
- ) -> impl Iterator<Item = &'a T> {
- inherent.into_iter().chain(
- self.entries()
- .filter_map(|style| style.property())
- .filter(move |property| property.is(func, id))
- .map(|property| &property.value)
- .map(move |value| {
- value.downcast().unwrap_or_else(|| {
- panic!(
- "attempted to read a value of a different type than was written {}.{}: {:?}",
- func.name(),
- func.field_name(id).unwrap(),
- value
- )
- })
- }),
- )
+ fn find(self, func: Element, id: u8) -> Option<&'a Block> {
+ self.properties(func, id).next()
+ }
+
+ /// Iterate over all values for the given property in the chain.
+ fn properties(self, func: Element, id: u8) -> impl Iterator<Item = &'a Block> {
+ self.entries()
+ .filter_map(|style| style.property())
+ .filter(move |property| property.is(func, id))
+ .map(|property| &property.value)
+ }
+
+ /// Make the given chainable the first link of this chain.
+ ///
+ /// The resulting style chain contains styles from `local` as well as
+ /// `self`. The ones from `local` take precedence over the ones from
+ /// `self`. For folded properties `local` contributes the inner value.
+ pub fn chain<'b, C>(&'b self, local: &'b C) -> StyleChain<'b>
+ where
+ C: Chainable + ?Sized,
+ {
+ Chainable::chain(local, self)
}
/// Iterate over the entries of the chain.
@@ -804,6 +842,9 @@ impl<T: Resolve> Resolve for Option<T> {
/// #set rect(stroke: 4pt)
/// #rect()
/// ```
+///
+/// Note: Folding must be associative, i.e. any implementation must satisfy
+/// `fold(fold(a, b), c) == fold(a, fold(b, c))`.
pub trait Fold {
/// Fold this inner value with an outer folded value.
fn fold(self, outer: Self) -> Self;
@@ -847,6 +888,9 @@ impl<T> Fold for OneOrMultiple<T> {
}
}
+/// A [folding](Fold) function.
+pub type FoldFn<T> = fn(T, T) -> T;
+
/// A variant of fold for foldable optional (`Option<T>`) values where an inner
/// `None` value isn't respected (contrary to `Option`'s usual `Fold`
/// implementation, with which folding with an inner `None` always returns
@@ -884,3 +928,13 @@ impl Fold for Depth {
Self(outer.0 + self.0)
}
}
+
+#[cold]
+fn block_wrong_type(func: Element, id: u8, value: &Block) -> ! {
+ panic!(
+ "attempted to read a value of a different type than was written {}.{}: {:?}",
+ func.name(),
+ func.field_name(id).unwrap(),
+ value
+ )
+}
diff --git a/crates/typst-library/src/foundations/target.rs b/crates/typst-library/src/foundations/target.rs
index 2a21fd42..71e7554e 100644
--- a/crates/typst-library/src/foundations/target.rs
+++ b/crates/typst-library/src/foundations/target.rs
@@ -73,5 +73,5 @@ pub struct TargetElem {
/// ```
#[func(contextual)]
pub fn target(context: Tracked<Context>) -> HintedStrResult<Target> {
- Ok(TargetElem::target_in(context.styles()?))
+ Ok(context.styles()?.get(TargetElem::target))
}
diff --git a/crates/typst-library/src/html/mod.rs b/crates/typst-library/src/html/mod.rs
index 7fc8adec..f9835206 100644
--- a/crates/typst-library/src/html/mod.rs
+++ b/crates/typst-library/src/html/mod.rs
@@ -47,21 +47,22 @@ pub struct HtmlElem {
pub tag: HtmlTag,
/// The element's HTML attributes.
- #[borrowed]
pub attrs: HtmlAttrs,
/// The contents of the HTML element.
///
/// The body can be arbitrary Typst content.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
impl HtmlElem {
/// Add an attribute to the element.
pub fn with_attr(mut self, attr: HtmlAttr, value: impl Into<EcoString>) -> Self {
- self.attrs.get_or_insert_with(Default::default).push(attr, value);
+ self.attrs
+ .as_option_mut()
+ .get_or_insert_with(Default::default)
+ .push(attr, value);
self
}
}
diff --git a/crates/typst-library/src/html/typed.rs b/crates/typst-library/src/html/typed.rs
index 1e7c1ad6..8240b296 100644
--- a/crates/typst-library/src/html/typed.rs
+++ b/crates/typst-library/src/html/typed.rs
@@ -127,12 +127,12 @@ fn construct(element: &'static data::ElemInfo, args: &mut Args) -> SourceResult<
let tag = HtmlTag::constant(element.name);
let mut elem = HtmlElem::new(tag);
if !attrs.0.is_empty() {
- elem.push_attrs(attrs);
+ elem.attrs.set(attrs);
}
if !tag::is_void(tag) {
let body = args.eat::<Content>()?;
- elem.push_body(body);
+ elem.body.set(body);
}
Ok(elem.into_value())
diff --git a/crates/typst-library/src/introspection/counter.rs b/crates/typst-library/src/introspection/counter.rs
index 772bea96..a7925e13 100644
--- a/crates/typst-library/src/introspection/counter.rs
+++ b/crates/typst-library/src/introspection/counter.rs
@@ -338,7 +338,7 @@ impl Counter {
/// The selector relevant for this counter's updates.
fn selector(&self) -> Selector {
- let mut selector = select_where!(CounterUpdateElem, Key => self.0.clone());
+ let mut selector = select_where!(CounterUpdateElem, key => self.0.clone());
if let CounterKey::Selector(key) = &self.0 {
selector = Selector::Or(eco_vec![selector, key.clone()]);
@@ -367,16 +367,16 @@ impl Counter {
.or_else(|| {
let styles = styles?;
match self.0 {
- CounterKey::Page => PageElem::numbering_in(styles).clone(),
+ CounterKey::Page => styles.get_cloned(PageElem::numbering),
CounterKey::Selector(Selector::Elem(func, _)) => {
- if func == HeadingElem::elem() {
- HeadingElem::numbering_in(styles).clone()
- } else if func == FigureElem::elem() {
- FigureElem::numbering_in(styles).clone()
- } else if func == EquationElem::elem() {
- EquationElem::numbering_in(styles).clone()
- } else if func == FootnoteElem::elem() {
- Some(FootnoteElem::numbering_in(styles).clone())
+ if func == HeadingElem::ELEM {
+ styles.get_cloned(HeadingElem::numbering)
+ } else if func == FigureElem::ELEM {
+ styles.get_cloned(FigureElem::numbering)
+ } else if func == EquationElem::ELEM {
+ styles.get_cloned(EquationElem::numbering)
+ } else if func == FootnoteElem::ELEM {
+ Some(styles.get_cloned(FootnoteElem::numbering))
} else {
None
}
@@ -398,7 +398,7 @@ impl Counter {
/// Selects all state updates.
pub fn select_any() -> Selector {
- CounterUpdateElem::elem().select()
+ CounterUpdateElem::ELEM.select()
}
}
@@ -565,14 +565,14 @@ pub enum CounterKey {
cast! {
CounterKey,
self => match self {
- Self::Page => PageElem::elem().into_value(),
+ Self::Page => PageElem::ELEM.into_value(),
Self::Selector(v) => v.into_value(),
Self::Str(v) => v.into_value(),
},
v: Str => Self::Str(v),
v: Label => Self::Selector(Selector::Label(v)),
v: Element => {
- if v == PageElem::elem() {
+ if v == PageElem::ELEM {
Self::Page
} else {
Self::Selector(LocatableSelector::from_value(v.into_value())?.0)
diff --git a/crates/typst-library/src/introspection/state.rs b/crates/typst-library/src/introspection/state.rs
index cc3f566b..784f2acb 100644
--- a/crates/typst-library/src/introspection/state.rs
+++ b/crates/typst-library/src/introspection/state.rs
@@ -259,12 +259,12 @@ impl State {
/// The selector for this state's updates.
fn selector(&self) -> Selector {
- select_where!(StateUpdateElem, Key => self.key.clone())
+ select_where!(StateUpdateElem, key => self.key.clone())
}
/// Selects all state updates.
pub fn select_any() -> Selector {
- StateUpdateElem::elem().select()
+ StateUpdateElem::ELEM.select()
}
}
diff --git a/crates/typst-library/src/layout/align.rs b/crates/typst-library/src/layout/align.rs
index 0a978dba..e5ceddf6 100644
--- a/crates/typst-library/src/layout/align.rs
+++ b/crates/typst-library/src/layout/align.rs
@@ -100,7 +100,7 @@ pub struct AlignElem {
impl Show for Packed<AlignElem> {
#[typst_macros::time(name = "align", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(self.body.clone().aligned(self.alignment(styles)))
+ Ok(self.body.clone().aligned(self.alignment.get(styles)))
}
}
@@ -277,7 +277,7 @@ impl Resolve for Alignment {
type Output = Axes<FixedAlignment>;
fn resolve(self, styles: StyleChain) -> Self::Output {
- self.fix(TextElem::dir_in(styles))
+ self.fix(styles.resolve(TextElem::dir))
}
}
@@ -378,7 +378,7 @@ impl Resolve for HAlignment {
type Output = FixedAlignment;
fn resolve(self, styles: StyleChain) -> Self::Output {
- self.fix(TextElem::dir_in(styles))
+ self.fix(styles.resolve(TextElem::dir))
}
}
@@ -414,7 +414,7 @@ impl Resolve for OuterHAlignment {
type Output = FixedAlignment;
fn resolve(self, styles: StyleChain) -> Self::Output {
- self.fix(TextElem::dir_in(styles))
+ self.fix(styles.resolve(TextElem::dir))
}
}
@@ -636,7 +636,7 @@ where
type Output = Axes<FixedAlignment>;
fn resolve(self, styles: StyleChain) -> Self::Output {
- self.fix(TextElem::dir_in(styles))
+ self.fix(styles.resolve(TextElem::dir))
}
}
diff --git a/crates/typst-library/src/layout/columns.rs b/crates/typst-library/src/layout/columns.rs
index f2f36441..1cea5275 100644
--- a/crates/typst-library/src/layout/columns.rs
+++ b/crates/typst-library/src/layout/columns.rs
@@ -49,7 +49,6 @@ pub struct ColumnsElem {
pub count: NonZeroUsize,
/// The size of the gutter space between each column.
- #[resolve]
#[default(Ratio::new(0.04).into())]
pub gutter: Rel<Length>,
diff --git a/crates/typst-library/src/layout/container.rs b/crates/typst-library/src/layout/container.rs
index fc27644e..cff66fe3 100644
--- a/crates/typst-library/src/layout/container.rs
+++ b/crates/typst-library/src/layout/container.rs
@@ -51,7 +51,6 @@ pub struct BoxElem {
/// ```example
/// Image: #box(baseline: 40%, image("tiger.jpg", width: 2cm)).
/// ```
- #[resolve]
pub baseline: Rel<Length>,
/// The box's background color. See the
@@ -60,13 +59,11 @@ pub struct BoxElem {
/// The box's border color. See the
/// [rectangle's documentation]($rect.stroke) for more details.
- #[resolve]
#[fold]
pub stroke: Sides<Option<Option<Stroke>>>,
/// How much to round the box's corners. See the
/// [rectangle's documentation]($rect.radius) for more details.
- #[resolve]
#[fold]
pub radius: Corners<Option<Rel<Length>>>,
@@ -78,7 +75,6 @@ pub struct BoxElem {
/// ```example
/// #rect(inset: 0pt)[Tight]
/// ```
- #[resolve]
#[fold]
pub inset: Sides<Option<Rel<Length>>>,
@@ -97,7 +93,6 @@ pub struct BoxElem {
/// radius: 2pt,
/// )[rectangle].
/// ```
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
@@ -119,7 +114,6 @@ pub struct BoxElem {
/// The contents of the box.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
@@ -262,25 +256,21 @@ pub struct BlockElem {
/// The block's border color. See the
/// [rectangle's documentation]($rect.stroke) for more details.
- #[resolve]
#[fold]
pub stroke: Sides<Option<Option<Stroke>>>,
/// How much to round the block's corners. See the
/// [rectangle's documentation]($rect.radius) for more details.
- #[resolve]
#[fold]
pub radius: Corners<Option<Rel<Length>>>,
/// How much to pad the block's content. See the
/// [box's documentation]($box.inset) for more details.
- #[resolve]
#[fold]
pub inset: Sides<Option<Rel<Length>>>,
/// How much to expand the block's size without affecting the layout. See
/// the [box's documentation]($box.outset) for more details.
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
@@ -358,7 +348,6 @@ pub struct BlockElem {
/// The contents of the block.
#[positional]
- #[borrowed]
pub body: Option<BlockBody>,
}
diff --git a/crates/typst-library/src/layout/em.rs b/crates/typst-library/src/layout/em.rs
index 746e1769..460bf5fa 100644
--- a/crates/typst-library/src/layout/em.rs
+++ b/crates/typst-library/src/layout/em.rs
@@ -167,7 +167,7 @@ impl Resolve for Em {
if self.is_zero() {
Abs::zero()
} else {
- self.at(TextElem::size_in(styles))
+ self.at(styles.resolve(TextElem::size))
}
}
}
diff --git a/crates/typst-library/src/layout/grid/mod.rs b/crates/typst-library/src/layout/grid/mod.rs
index 52621c64..64e7464b 100644
--- a/crates/typst-library/src/layout/grid/mod.rs
+++ b/crates/typst-library/src/layout/grid/mod.rs
@@ -144,14 +144,12 @@ pub struct GridElem {
/// with that many `{auto}`-sized columns. Note that opposed to rows and
/// gutters, providing a single track size will only ever create a single
/// column.
- #[borrowed]
pub columns: TrackSizings,
/// The row sizes.
///
/// If there are more cells than fit the defined rows, the last row is
/// repeated until there are no more cells.
- #[borrowed]
pub rows: TrackSizings,
/// The gaps between rows and columns.
@@ -169,12 +167,10 @@ pub struct GridElem {
let gutter = args.named("gutter")?;
args.named("column-gutter")?.or_else(|| gutter.clone())
)]
- #[borrowed]
pub column_gutter: TrackSizings,
/// The gaps between rows.
#[parse(args.named("row-gutter")?.or_else(|| gutter.clone()))]
- #[borrowed]
pub row_gutter: TrackSizings,
/// How to fill the cells.
@@ -197,7 +193,6 @@ pub struct GridElem {
/// [O], [X], [O], [X],
/// )
/// ```
- #[borrowed]
pub fill: Celled<Option<Paint>>,
/// How to align the cells' content.
@@ -209,7 +204,6 @@ pub struct GridElem {
///
/// You can find an example for this argument at the
/// [`table.align`]($table.align) parameter.
- #[borrowed]
pub align: Celled<Smart<Alignment>>,
/// How to [stroke]($stroke) the cells.
@@ -289,7 +283,6 @@ pub struct GridElem {
/// ),
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Celled<Sides<Option<Option<Arc<Stroke>>>>>,
@@ -541,7 +534,6 @@ pub struct GridHLine {
///
/// Specifying `{none}` removes any lines previously placed across this
/// line's range, including hlines or per-cell stroke below it.
- #[resolve]
#[fold]
#[default(Some(Arc::new(Stroke::default())))]
pub stroke: Option<Arc<Stroke>>,
@@ -596,7 +588,6 @@ pub struct GridVLine {
///
/// Specifying `{none}` removes any lines previously placed across this
/// line's range, including vlines or per-cell stroke below it.
- #[resolve]
#[fold]
#[default(Some(Arc::new(Stroke::default())))]
pub stroke: Option<Arc<Stroke>>,
@@ -742,7 +733,6 @@ pub struct GridCell {
pub inset: Smart<Sides<Option<Rel<Length>>>>,
/// The cell's [stroke]($grid.stroke) override.
- #[resolve]
#[fold]
pub stroke: Sides<Option<Option<Arc<Stroke>>>>,
@@ -760,7 +750,7 @@ cast! {
impl Show for Packed<GridCell> {
fn show(&self, _engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- show_grid_cell(self.body.clone(), self.inset(styles), self.align(styles))
+ show_grid_cell(self.body.clone(), self.inset.get(styles), self.align.get(styles))
}
}
diff --git a/crates/typst-library/src/layout/grid/resolve.rs b/crates/typst-library/src/layout/grid/resolve.rs
index baf6b738..f3004088 100644
--- a/crates/typst-library/src/layout/grid/resolve.rs
+++ b/crates/typst-library/src/layout/grid/resolve.rs
@@ -31,14 +31,14 @@ pub fn grid_to_cellgrid<'a>(
locator: Locator<'a>,
styles: StyleChain,
) -> SourceResult<CellGrid<'a>> {
- let inset = elem.inset(styles);
- let align = elem.align(styles);
- let columns = elem.columns(styles);
- let rows = elem.rows(styles);
- let column_gutter = elem.column_gutter(styles);
- let row_gutter = elem.row_gutter(styles);
- let fill = elem.fill(styles);
- let stroke = elem.stroke(styles);
+ let inset = elem.inset.get_cloned(styles);
+ let align = elem.align.get_ref(styles);
+ let columns = elem.columns.get_ref(styles);
+ let rows = elem.rows.get_ref(styles);
+ let column_gutter = elem.column_gutter.get_ref(styles);
+ let row_gutter = elem.row_gutter.get_ref(styles);
+ let fill = elem.fill.get_ref(styles);
+ let stroke = elem.stroke.resolve(styles);
let tracks = Axes::new(columns.0.as_slice(), rows.0.as_slice());
let gutter = Axes::new(column_gutter.0.as_slice(), row_gutter.0.as_slice());
@@ -47,13 +47,13 @@ pub fn grid_to_cellgrid<'a>(
let resolve_item = |item: &GridItem| grid_item_to_resolvable(item, styles);
let children = elem.children.iter().map(|child| match child {
GridChild::Header(header) => ResolvableGridChild::Header {
- repeat: header.repeat(styles),
- level: header.level(styles),
+ repeat: header.repeat.get(styles),
+ level: header.level.get(styles),
span: header.span(),
items: header.children.iter().map(resolve_item),
},
GridChild::Footer(footer) => ResolvableGridChild::Footer {
- repeat: footer.repeat(styles),
+ repeat: footer.repeat.get(styles),
span: footer.span(),
items: footer.children.iter().map(resolve_item),
},
@@ -85,14 +85,14 @@ pub fn table_to_cellgrid<'a>(
locator: Locator<'a>,
styles: StyleChain,
) -> SourceResult<CellGrid<'a>> {
- let inset = elem.inset(styles);
- let align = elem.align(styles);
- let columns = elem.columns(styles);
- let rows = elem.rows(styles);
- let column_gutter = elem.column_gutter(styles);
- let row_gutter = elem.row_gutter(styles);
- let fill = elem.fill(styles);
- let stroke = elem.stroke(styles);
+ let inset = elem.inset.get_cloned(styles);
+ let align = elem.align.get_ref(styles);
+ let columns = elem.columns.get_ref(styles);
+ let rows = elem.rows.get_ref(styles);
+ let column_gutter = elem.column_gutter.get_ref(styles);
+ let row_gutter = elem.row_gutter.get_ref(styles);
+ let fill = elem.fill.get_ref(styles);
+ let stroke = elem.stroke.resolve(styles);
let tracks = Axes::new(columns.0.as_slice(), rows.0.as_slice());
let gutter = Axes::new(column_gutter.0.as_slice(), row_gutter.0.as_slice());
@@ -101,13 +101,13 @@ pub fn table_to_cellgrid<'a>(
let resolve_item = |item: &TableItem| table_item_to_resolvable(item, styles);
let children = elem.children.iter().map(|child| match child {
TableChild::Header(header) => ResolvableGridChild::Header {
- repeat: header.repeat(styles),
- level: header.level(styles),
+ repeat: header.repeat.get(styles),
+ level: header.level.get(styles),
span: header.span(),
items: header.children.iter().map(resolve_item),
},
TableChild::Footer(footer) => ResolvableGridChild::Footer {
- repeat: footer.repeat(styles),
+ repeat: footer.repeat.get(styles),
span: footer.span(),
items: footer.children.iter().map(resolve_item),
},
@@ -137,27 +137,27 @@ fn grid_item_to_resolvable(
) -> ResolvableGridItem<Packed<GridCell>> {
match item {
GridItem::HLine(hline) => ResolvableGridItem::HLine {
- y: hline.y(styles),
- start: hline.start(styles),
- end: hline.end(styles),
- stroke: hline.stroke(styles),
+ y: hline.y.get(styles),
+ start: hline.start.get(styles),
+ end: hline.end.get(styles),
+ stroke: hline.stroke.resolve(styles),
span: hline.span(),
- position: match hline.position(styles) {
+ position: match hline.position.get(styles) {
OuterVAlignment::Top => LinePosition::Before,
OuterVAlignment::Bottom => LinePosition::After,
},
},
GridItem::VLine(vline) => ResolvableGridItem::VLine {
- x: vline.x(styles),
- start: vline.start(styles),
- end: vline.end(styles),
- stroke: vline.stroke(styles),
+ x: vline.x.get(styles),
+ start: vline.start.get(styles),
+ end: vline.end.get(styles),
+ stroke: vline.stroke.resolve(styles),
span: vline.span(),
- position: match vline.position(styles) {
- OuterHAlignment::Left if TextElem::dir_in(styles) == Dir::RTL => {
+ position: match vline.position.get(styles) {
+ OuterHAlignment::Left if styles.resolve(TextElem::dir) == Dir::RTL => {
LinePosition::After
}
- OuterHAlignment::Right if TextElem::dir_in(styles) == Dir::RTL => {
+ OuterHAlignment::Right if styles.resolve(TextElem::dir) == Dir::RTL => {
LinePosition::Before
}
OuterHAlignment::Start | OuterHAlignment::Left => LinePosition::Before,
@@ -174,27 +174,27 @@ fn table_item_to_resolvable(
) -> ResolvableGridItem<Packed<TableCell>> {
match item {
TableItem::HLine(hline) => ResolvableGridItem::HLine {
- y: hline.y(styles),
- start: hline.start(styles),
- end: hline.end(styles),
- stroke: hline.stroke(styles),
+ y: hline.y.get(styles),
+ start: hline.start.get(styles),
+ end: hline.end.get(styles),
+ stroke: hline.stroke.resolve(styles),
span: hline.span(),
- position: match hline.position(styles) {
+ position: match hline.position.get(styles) {
OuterVAlignment::Top => LinePosition::Before,
OuterVAlignment::Bottom => LinePosition::After,
},
},
TableItem::VLine(vline) => ResolvableGridItem::VLine {
- x: vline.x(styles),
- start: vline.start(styles),
- end: vline.end(styles),
- stroke: vline.stroke(styles),
+ x: vline.x.get(styles),
+ start: vline.start.get(styles),
+ end: vline.end.get(styles),
+ stroke: vline.stroke.resolve(styles),
span: vline.span(),
- position: match vline.position(styles) {
- OuterHAlignment::Left if TextElem::dir_in(styles) == Dir::RTL => {
+ position: match vline.position.get(styles) {
+ OuterHAlignment::Left if styles.resolve(TextElem::dir) == Dir::RTL => {
LinePosition::After
}
- OuterHAlignment::Right if TextElem::dir_in(styles) == Dir::RTL => {
+ OuterHAlignment::Right if styles.resolve(TextElem::dir) == Dir::RTL => {
LinePosition::Before
}
OuterHAlignment::Start | OuterHAlignment::Left => LinePosition::Before,
@@ -219,12 +219,12 @@ impl ResolvableCell for Packed<TableCell> {
styles: StyleChain,
) -> Cell<'a> {
let cell = &mut *self;
- let colspan = cell.colspan(styles);
- let rowspan = cell.rowspan(styles);
- let breakable = cell.breakable(styles).unwrap_or(breakable);
- let fill = cell.fill(styles).unwrap_or_else(|| fill.clone());
+ let colspan = cell.colspan.get(styles);
+ let rowspan = cell.rowspan.get(styles);
+ let breakable = cell.breakable.get(styles).unwrap_or(breakable);
+ let fill = cell.fill.get_cloned(styles).unwrap_or_else(|| fill.clone());
- let cell_stroke = cell.stroke(styles);
+ let cell_stroke = cell.stroke.resolve(styles);
let stroke_overridden =
cell_stroke.as_ref().map(|side| matches!(side, Some(Some(_))));
@@ -238,22 +238,22 @@ impl ResolvableCell for Packed<TableCell> {
// cell stroke is the same as specifying 'none', so we equate the two
// concepts.
let stroke = cell_stroke.fold(stroke).map(Option::flatten);
- cell.push_x(Smart::Custom(x));
- cell.push_y(Smart::Custom(y));
- cell.push_fill(Smart::Custom(fill.clone()));
- cell.push_align(match align {
- Smart::Custom(align) => {
- Smart::Custom(cell.align(styles).map_or(align, |inner| inner.fold(align)))
- }
+ cell.x.set(Smart::Custom(x));
+ cell.y.set(Smart::Custom(y));
+ cell.fill.set(Smart::Custom(fill.clone()));
+ cell.align.set(match align {
+ Smart::Custom(align) => Smart::Custom(
+ cell.align.get(styles).map_or(align, |inner| inner.fold(align)),
+ ),
// Don't fold if the table is using outer alignment. Use the
// cell's alignment instead (which, in the end, will fold with
// the outer alignment when it is effectively displayed).
- Smart::Auto => cell.align(styles),
+ Smart::Auto => cell.align.get(styles),
});
- cell.push_inset(Smart::Custom(
- cell.inset(styles).map_or(inset, |inner| inner.fold(inset)),
+ cell.inset.set(Smart::Custom(
+ cell.inset.get(styles).map_or(inset, |inner| inner.fold(inset)),
));
- cell.push_stroke(
+ cell.stroke.set(
// Here we convert the resolved stroke to a regular stroke, however
// with resolved units (that is, 'em' converted to absolute units).
// We also convert any stroke unspecified by both the cell and the
@@ -266,7 +266,7 @@ impl ResolvableCell for Packed<TableCell> {
}))
}),
);
- cell.push_breakable(Smart::Custom(breakable));
+ cell.breakable.set(Smart::Custom(breakable));
Cell {
body: self.pack(),
locator,
@@ -280,19 +280,19 @@ impl ResolvableCell for Packed<TableCell> {
}
fn x(&self, styles: StyleChain) -> Smart<usize> {
- (**self).x(styles)
+ self.x.get(styles)
}
fn y(&self, styles: StyleChain) -> Smart<usize> {
- (**self).y(styles)
+ self.y.get(styles)
}
fn colspan(&self, styles: StyleChain) -> NonZeroUsize {
- (**self).colspan(styles)
+ self.colspan.get(styles)
}
fn rowspan(&self, styles: StyleChain) -> NonZeroUsize {
- (**self).rowspan(styles)
+ self.rowspan.get(styles)
}
fn span(&self) -> Span {
@@ -314,12 +314,12 @@ impl ResolvableCell for Packed<GridCell> {
styles: StyleChain,
) -> Cell<'a> {
let cell = &mut *self;
- let colspan = cell.colspan(styles);
- let rowspan = cell.rowspan(styles);
- let breakable = cell.breakable(styles).unwrap_or(breakable);
- let fill = cell.fill(styles).unwrap_or_else(|| fill.clone());
+ let colspan = cell.colspan.get(styles);
+ let rowspan = cell.rowspan.get(styles);
+ let breakable = cell.breakable.get(styles).unwrap_or(breakable);
+ let fill = cell.fill.get_cloned(styles).unwrap_or_else(|| fill.clone());
- let cell_stroke = cell.stroke(styles);
+ let cell_stroke = cell.stroke.resolve(styles);
let stroke_overridden =
cell_stroke.as_ref().map(|side| matches!(side, Some(Some(_))));
@@ -333,22 +333,22 @@ impl ResolvableCell for Packed<GridCell> {
// cell stroke is the same as specifying 'none', so we equate the two
// concepts.
let stroke = cell_stroke.fold(stroke).map(Option::flatten);
- cell.push_x(Smart::Custom(x));
- cell.push_y(Smart::Custom(y));
- cell.push_fill(Smart::Custom(fill.clone()));
- cell.push_align(match align {
- Smart::Custom(align) => {
- Smart::Custom(cell.align(styles).map_or(align, |inner| inner.fold(align)))
- }
+ cell.x.set(Smart::Custom(x));
+ cell.y.set(Smart::Custom(y));
+ cell.fill.set(Smart::Custom(fill.clone()));
+ cell.align.set(match align {
+ Smart::Custom(align) => Smart::Custom(
+ cell.align.get(styles).map_or(align, |inner| inner.fold(align)),
+ ),
// Don't fold if the grid is using outer alignment. Use the
// cell's alignment instead (which, in the end, will fold with
// the outer alignment when it is effectively displayed).
- Smart::Auto => cell.align(styles),
+ Smart::Auto => cell.align.get(styles),
});
- cell.push_inset(Smart::Custom(
- cell.inset(styles).map_or(inset, |inner| inner.fold(inset)),
+ cell.inset.set(Smart::Custom(
+ cell.inset.get(styles).map_or(inset, |inner| inner.fold(inset)),
));
- cell.push_stroke(
+ cell.stroke.set(
// Here we convert the resolved stroke to a regular stroke, however
// with resolved units (that is, 'em' converted to absolute units).
// We also convert any stroke unspecified by both the cell and the
@@ -361,7 +361,7 @@ impl ResolvableCell for Packed<GridCell> {
}))
}),
);
- cell.push_breakable(Smart::Custom(breakable));
+ cell.breakable.set(Smart::Custom(breakable));
Cell {
body: self.pack(),
locator,
@@ -375,19 +375,19 @@ impl ResolvableCell for Packed<GridCell> {
}
fn x(&self, styles: StyleChain) -> Smart<usize> {
- (**self).x(styles)
+ self.x.get(styles)
}
fn y(&self, styles: StyleChain) -> Smart<usize> {
- (**self).y(styles)
+ self.y.get(styles)
}
fn colspan(&self, styles: StyleChain) -> NonZeroUsize {
- (**self).colspan(styles)
+ self.colspan.get(styles)
}
fn rowspan(&self, styles: StyleChain) -> NonZeroUsize {
- (**self).rowspan(styles)
+ self.rowspan.get(styles)
}
fn span(&self) -> Span {
diff --git a/crates/typst-library/src/layout/hide.rs b/crates/typst-library/src/layout/hide.rs
index eca33471..5f3a5a2d 100644
--- a/crates/typst-library/src/layout/hide.rs
+++ b/crates/typst-library/src/layout/hide.rs
@@ -29,6 +29,6 @@ pub struct HideElem {
impl Show for Packed<HideElem> {
#[typst_macros::time(name = "hide", span = self.span())]
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(self.body.clone().styled(HideElem::set_hidden(true)))
+ Ok(self.body.clone().set(HideElem::hidden, true))
}
}
diff --git a/crates/typst-library/src/layout/page.rs b/crates/typst-library/src/layout/page.rs
index 98afbd06..a0b1c6fe 100644
--- a/crates/typst-library/src/layout/page.rs
+++ b/crates/typst-library/src/layout/page.rs
@@ -60,7 +60,6 @@ pub struct PageElem {
/// box(square(width: 1cm))
/// }
/// ```
- #[resolve]
#[parse(
let paper = args.named_or_find::<Paper>("paper")?;
args.named("width")?
@@ -77,7 +76,6 @@ pub struct PageElem {
/// page set rule. Most examples throughout this documentation use `{auto}`
/// for the height of the page to dynamically grow and shrink to fit their
/// content.
- #[resolve]
#[parse(
args.named("height")?
.or_else(|| paper.map(|paper| Smart::Custom(paper.height().into())))
@@ -201,7 +199,6 @@ pub struct PageElem {
/// #set text(fill: rgb("fdfdfd"))
/// *Dark mode enabled.*
/// ```
- #[borrowed]
#[ghost]
pub fill: Smart<Option<Paint>>,
@@ -219,7 +216,6 @@ pub struct PageElem {
///
/// #lorem(48)
/// ```
- #[borrowed]
#[ghost]
pub numbering: Option<Numbering>,
@@ -276,12 +272,10 @@ pub struct PageElem {
///
/// #lorem(19)
/// ```
- #[borrowed]
#[ghost]
pub header: Smart<Option<Content>>,
/// The amount the header is raised into the top margin.
- #[resolve]
#[default(Ratio::new(0.3).into())]
#[ghost]
pub header_ascent: Rel<Length>,
@@ -314,12 +308,10 @@ pub struct PageElem {
///
/// #lorem(48)
/// ```
- #[borrowed]
#[ghost]
pub footer: Smart<Option<Content>>,
/// The amount the footer is lowered into the bottom margin.
- #[resolve]
#[default(Ratio::new(0.3).into())]
#[ghost]
pub footer_descent: Rel<Length>,
@@ -340,7 +332,6 @@ pub struct PageElem {
/// In the year 2023, we plan to take
/// over the world (of typesetting).
/// ```
- #[borrowed]
#[ghost]
pub background: Option<Content>,
@@ -355,7 +346,6 @@ pub struct PageElem {
/// "Weak Reject" because they did
/// not understand our approach...
/// ```
- #[borrowed]
#[ghost]
pub foreground: Option<Content>,
diff --git a/crates/typst-library/src/layout/place.rs b/crates/typst-library/src/layout/place.rs
index bedeb507..b6c4a0cc 100644
--- a/crates/typst-library/src/layout/place.rs
+++ b/crates/typst-library/src/layout/place.rs
@@ -134,7 +134,6 @@ pub struct PlaceElem {
///
/// Has no effect if `float` is `{false}`.
#[default(Em::new(1.5).into())]
- #[resolve]
pub clearance: Length,
/// The horizontal displacement of the placed content.
diff --git a/crates/typst-library/src/math/accent.rs b/crates/typst-library/src/math/accent.rs
index c8569ea2..5a9c47ae 100644
--- a/crates/typst-library/src/math/accent.rs
+++ b/crates/typst-library/src/math/accent.rs
@@ -62,7 +62,6 @@ pub struct AccentElem {
/// ```example
/// $dash(A, size: #150%)$
/// ```
- #[resolve]
#[default(Rel::one())]
pub size: Rel<Length>,
diff --git a/crates/typst-library/src/math/attach.rs b/crates/typst-library/src/math/attach.rs
index d526aba5..0dda1d33 100644
--- a/crates/typst-library/src/math/attach.rs
+++ b/crates/typst-library/src/math/attach.rs
@@ -59,9 +59,9 @@ impl Packed<AttachElem> {
macro_rules! merge {
($content:ident) => {
- if base.$content.is_none() && elem.$content.is_some() {
+ if !base.$content.is_set() && elem.$content.is_set() {
base.$content = elem.$content.clone();
- elem.$content = None;
+ elem.$content.unset();
}
};
}
@@ -152,7 +152,6 @@ pub struct StretchElem {
/// The size to stretch to, relative to the maximum size of the glyph and
/// its attachments.
- #[resolve]
#[default(Rel::one())]
pub size: Rel<Length>,
}
diff --git a/crates/typst-library/src/math/cancel.rs b/crates/typst-library/src/math/cancel.rs
index 17f4dcfb..88637095 100644
--- a/crates/typst-library/src/math/cancel.rs
+++ b/crates/typst-library/src/math/cancel.rs
@@ -29,7 +29,6 @@ pub struct CancelElem {
/// $ a + cancel(x, length: #200%)
/// - cancel(x, length: #200%) $
/// ```
- #[resolve]
#[default(Rel::new(Ratio::one(), Abs::pt(3.0).into()))]
pub length: Rel<Length>,
@@ -89,7 +88,6 @@ pub struct CancelElem {
/// ),
/// ) $
/// ```
- #[resolve]
#[fold]
#[default(Stroke {
// Default stroke has 0.5pt for better visuals.
diff --git a/crates/typst-library/src/math/equation.rs b/crates/typst-library/src/math/equation.rs
index 32be216a..b97bb18d 100644
--- a/crates/typst-library/src/math/equation.rs
+++ b/crates/typst-library/src/math/equation.rs
@@ -63,7 +63,6 @@ pub struct EquationElem {
/// With @ratio, we get:
/// $ F_n = floor(1 / sqrt(5) phi.alt^n) $
/// ```
- #[borrowed]
pub numbering: Option<Numbering>,
/// The alignment of the equation numbering.
@@ -152,7 +151,7 @@ impl Synthesize for Packed<EquationElem> {
engine: &mut Engine,
styles: StyleChain,
) -> SourceResult<()> {
- let supplement = match self.as_ref().supplement(styles) {
+ let supplement = match self.as_ref().supplement.get_ref(styles) {
Smart::Auto => TextElem::packed(Self::local_name_in(styles)),
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => {
@@ -160,14 +159,15 @@ impl Synthesize for Packed<EquationElem> {
}
};
- self.push_supplement(Smart::Custom(Some(Supplement::Content(supplement))));
+ self.supplement
+ .set(Smart::Custom(Some(Supplement::Content(supplement))));
Ok(())
}
}
impl Show for Packed<EquationElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if self.block(styles) {
+ if self.block.get(styles) {
Ok(BlockElem::multi_layouter(
self.clone(),
engine.routines.layout_equation_block,
@@ -185,25 +185,27 @@ impl Show for Packed<EquationElem> {
impl ShowSet for Packed<EquationElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
- if self.block(styles) {
- out.set(AlignElem::set_alignment(Alignment::CENTER));
- out.set(BlockElem::set_breakable(false));
- out.set(ParLine::set_numbering(None));
- out.set(EquationElem::set_size(MathSize::Display));
+ if self.block.get(styles) {
+ out.set(AlignElem::alignment, Alignment::CENTER);
+ out.set(AlignElem::alignment, Alignment::CENTER);
+ out.set(BlockElem::breakable, false);
+ out.set(ParLine::numbering, None);
+ out.set(EquationElem::size, MathSize::Display);
} else {
- out.set(EquationElem::set_size(MathSize::Text));
+ out.set(EquationElem::size, MathSize::Text);
}
- out.set(TextElem::set_weight(FontWeight::from_number(450)));
- out.set(TextElem::set_font(FontList(vec![FontFamily::new(
- "New Computer Modern Math",
- )])));
+ out.set(TextElem::weight, FontWeight::from_number(450));
+ out.set(
+ TextElem::font,
+ FontList(vec![FontFamily::new("New Computer Modern Math")]),
+ );
out
}
}
impl Count for Packed<EquationElem> {
fn update(&self) -> Option<CounterUpdate> {
- (self.block(StyleChain::default()) && self.numbering().is_some())
+ (self.block.get(StyleChain::default()) && self.numbering().is_some())
.then(|| CounterUpdate::Step(NonZeroUsize::ONE))
}
}
@@ -215,24 +217,24 @@ impl LocalName for Packed<EquationElem> {
impl Refable for Packed<EquationElem> {
fn supplement(&self) -> Content {
// After synthesis, this should always be custom content.
- match (**self).supplement(StyleChain::default()) {
+ match self.supplement.get_cloned(StyleChain::default()) {
Smart::Custom(Some(Supplement::Content(content))) => content,
_ => Content::empty(),
}
}
fn counter(&self) -> Counter {
- Counter::of(EquationElem::elem())
+ Counter::of(EquationElem::ELEM)
}
fn numbering(&self) -> Option<&Numbering> {
- (**self).numbering(StyleChain::default()).as_ref()
+ self.numbering.get_ref(StyleChain::default()).as_ref()
}
}
impl Outlinable for Packed<EquationElem> {
fn outlined(&self) -> bool {
- self.block(StyleChain::default()) && self.numbering().is_some()
+ self.block.get(StyleChain::default()) && self.numbering().is_some()
}
fn prefix(&self, numbers: Content) -> Content {
diff --git a/crates/typst-library/src/math/lr.rs b/crates/typst-library/src/math/lr.rs
index 7558717a..5f5cb861 100644
--- a/crates/typst-library/src/math/lr.rs
+++ b/crates/typst-library/src/math/lr.rs
@@ -9,7 +9,6 @@ use crate::math::Mathy;
#[elem(title = "Left/Right", Mathy)]
pub struct LrElem {
/// The size of the brackets, relative to the height of the wrapped content.
- #[resolve]
#[default(Rel::one())]
pub size: Rel<Length>,
@@ -130,7 +129,7 @@ fn delimited(
]));
// Push size only if size is provided
if let Some(size) = size {
- elem.push_size(size);
+ elem.size.set(size);
}
elem.pack().spanned(span)
}
diff --git a/crates/typst-library/src/math/matrix.rs b/crates/typst-library/src/math/matrix.rs
index b6c4654e..2a51caef 100644
--- a/crates/typst-library/src/math/matrix.rs
+++ b/crates/typst-library/src/math/matrix.rs
@@ -46,7 +46,6 @@ pub struct VecElem {
/// #set math.vec(align: right)
/// $ vec(-1, 1, -1) $
/// ```
- #[resolve]
#[default(HAlignment::Center)]
pub align: HAlignment,
@@ -56,7 +55,6 @@ pub struct VecElem {
/// #set math.vec(gap: 1em)
/// $ vec(1, 2) $
/// ```
- #[resolve]
#[default(DEFAULT_ROW_GAP.into())]
pub gap: Rel<Length>,
@@ -107,7 +105,6 @@ pub struct MatElem {
/// #set math.mat(align: right)
/// $ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1) $
/// ```
- #[resolve]
#[default(HAlignment::Center)]
pub align: HAlignment,
@@ -141,7 +138,6 @@ pub struct MatElem {
/// ```example
/// $ mat(0, 0, 0; 1, 1, 1; augment: #(hline: 1, stroke: 2pt + green)) $
/// ```
- #[resolve]
#[fold]
pub augment: Option<Augment>,
@@ -162,7 +158,6 @@ pub struct MatElem {
/// #set math.mat(row-gap: 1em)
/// $ mat(1, 2; 3, 4) $
/// ```
- #[resolve]
#[parse(
let gap = args.named("gap")?;
args.named("row-gap")?.or(gap)
@@ -176,7 +171,6 @@ pub struct MatElem {
/// #set math.mat(column-gap: 1em)
/// $ mat(1, 2; 3, 4) $
/// ```
- #[resolve]
#[parse(args.named("column-gap")?.or(gap))]
#[default(DEFAULT_COL_GAP.into())]
pub column_gap: Rel<Length>,
@@ -259,7 +253,6 @@ pub struct CasesElem {
/// #set math.cases(gap: 1em)
/// $ x = cases(1, 2) $
/// ```
- #[resolve]
#[default(DEFAULT_ROW_GAP.into())]
pub gap: Rel<Length>,
diff --git a/crates/typst-library/src/math/style.rs b/crates/typst-library/src/math/style.rs
index f3d28f2a..53242e6e 100644
--- a/crates/typst-library/src/math/style.rs
+++ b/crates/typst-library/src/math/style.rs
@@ -11,7 +11,7 @@ pub fn bold(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_bold(true))
+ body.set(EquationElem::bold, true)
}
/// Upright (non-italic) font style in math.
@@ -24,7 +24,7 @@ pub fn upright(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_italic(Smart::Custom(false)))
+ body.set(EquationElem::italic, Smart::Custom(false))
}
/// Italic font style in math.
@@ -35,7 +35,7 @@ pub fn italic(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_italic(Smart::Custom(true)))
+ body.set(EquationElem::italic, Smart::Custom(true))
}
/// Serif (roman) font style in math.
@@ -46,7 +46,7 @@ pub fn serif(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Serif))
+ body.set(EquationElem::variant, MathVariant::Serif)
}
/// Sans-serif font style in math.
@@ -59,7 +59,7 @@ pub fn sans(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Sans))
+ body.set(EquationElem::variant, MathVariant::Sans)
}
/// Calligraphic font style in math.
@@ -93,7 +93,7 @@ pub fn cal(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Cal))
+ body.set(EquationElem::variant, MathVariant::Cal)
}
/// Fraktur font style in math.
@@ -106,7 +106,7 @@ pub fn frak(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Frak))
+ body.set(EquationElem::variant, MathVariant::Frak)
}
/// Monospace font style in math.
@@ -119,7 +119,7 @@ pub fn mono(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Mono))
+ body.set(EquationElem::variant, MathVariant::Mono)
}
/// Blackboard bold (double-struck) font style in math.
@@ -137,7 +137,7 @@ pub fn bb(
/// The content to style.
body: Content,
) -> Content {
- body.styled(EquationElem::set_variant(MathVariant::Bb))
+ body.set(EquationElem::variant, MathVariant::Bb)
}
/// Forced display style in math.
@@ -157,8 +157,8 @@ pub fn display(
#[default(false)]
cramped: bool,
) -> Content {
- body.styled(EquationElem::set_size(MathSize::Display))
- .styled(EquationElem::set_cramped(cramped))
+ body.set(EquationElem::size, MathSize::Display)
+ .set(EquationElem::cramped, cramped)
}
/// Forced inline (text) style in math.
@@ -179,8 +179,8 @@ pub fn inline(
#[default(false)]
cramped: bool,
) -> Content {
- body.styled(EquationElem::set_size(MathSize::Text))
- .styled(EquationElem::set_cramped(cramped))
+ body.set(EquationElem::size, MathSize::Text)
+ .set(EquationElem::cramped, cramped)
}
/// Forced script style in math.
@@ -200,8 +200,8 @@ pub fn script(
#[default(true)]
cramped: bool,
) -> Content {
- body.styled(EquationElem::set_size(MathSize::Script))
- .styled(EquationElem::set_cramped(cramped))
+ body.set(EquationElem::size, MathSize::Script)
+ .set(EquationElem::cramped, cramped)
}
/// Forced second script style in math.
@@ -222,8 +222,8 @@ pub fn sscript(
#[default(true)]
cramped: bool,
) -> Content {
- body.styled(EquationElem::set_size(MathSize::ScriptScript))
- .styled(EquationElem::set_cramped(cramped))
+ body.set(EquationElem::size, MathSize::ScriptScript)
+ .set(EquationElem::cramped, cramped)
}
/// The size of elements in an equation.
diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs
index f56f5813..c44748a9 100644
--- a/crates/typst-library/src/model/bibliography.rs
+++ b/crates/typst-library/src/model/bibliography.rs
@@ -158,7 +158,7 @@ pub struct BibliographyElem {
impl BibliographyElem {
/// Find the document's bibliography.
pub fn find(introspector: Tracked<Introspector>) -> StrResult<Packed<Self>> {
- let query = introspector.query(&Self::elem().select());
+ let query = introspector.query(&Self::ELEM.select());
let mut iter = query.iter();
let Some(elem) = iter.next() else {
bail!("the document does not contain a bibliography");
@@ -175,7 +175,7 @@ impl BibliographyElem {
pub fn has(engine: &Engine, key: Label) -> bool {
engine
.introspector
- .query(&Self::elem().select())
+ .query(&Self::ELEM.select())
.iter()
.any(|elem| elem.to_packed::<Self>().unwrap().sources.derived.has(key))
}
@@ -183,7 +183,7 @@ impl BibliographyElem {
/// Find all bibliography keys.
pub fn keys(introspector: Tracked<Introspector>) -> Vec<(Label, Option<EcoString>)> {
let mut vec = vec![];
- for elem in introspector.query(&Self::elem().select()).iter() {
+ for elem in introspector.query(&Self::ELEM.select()).iter() {
let this = elem.to_packed::<Self>().unwrap();
for (key, entry) in this.sources.derived.iter() {
let detail = entry.title().map(|title| title.value.to_str().into());
@@ -197,8 +197,8 @@ impl BibliographyElem {
impl Synthesize for Packed<BibliographyElem> {
fn synthesize(&mut self, _: &mut Engine, styles: StyleChain) -> SourceResult<()> {
let elem = self.as_mut();
- elem.push_lang(TextElem::lang_in(styles));
- elem.push_region(TextElem::region_in(styles));
+ elem.lang = Some(styles.get(TextElem::lang));
+ elem.region = Some(styles.get(TextElem::region));
Ok(())
}
}
@@ -212,7 +212,7 @@ impl Show for Packed<BibliographyElem> {
let span = self.span();
let mut seq = vec![];
- if let Some(title) = self.title(styles).unwrap_or_else(|| {
+ if let Some(title) = self.title.get_ref(styles).clone().unwrap_or_else(|| {
Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
}) {
seq.push(
@@ -227,7 +227,7 @@ impl Show for Packed<BibliographyElem> {
let references = works
.references
.as_ref()
- .ok_or_else(|| match self.style(styles).source {
+ .ok_or_else(|| match self.style.get_ref(styles).source {
CslSource::Named(style) => eco_format!(
"CSL style \"{}\" is not suitable for bibliographies",
style.display_name()
@@ -239,7 +239,7 @@ impl Show for Packed<BibliographyElem> {
.at(span)?;
if references.iter().any(|(prefix, _)| prefix.is_some()) {
- let row_gutter = ParElem::spacing_in(styles);
+ let row_gutter = styles.get(ParElem::spacing);
let mut cells = vec![];
for (prefix, reference) in references {
@@ -265,7 +265,7 @@ impl Show for Packed<BibliographyElem> {
let block = if works.hanging_indent {
let body = HElem::new((-INDENT).into()).pack() + realized;
let inset = Sides::default()
- .with(TextElem::dir_in(styles).start(), Some(INDENT.into()));
+ .with(styles.resolve(TextElem::dir).start(), Some(INDENT.into()));
BlockElem::new()
.with_body(Some(BlockBody::Content(body)))
.with_inset(inset)
@@ -285,8 +285,8 @@ impl ShowSet for Packed<BibliographyElem> {
fn show_set(&self, _: StyleChain) -> Styles {
const INDENT: Em = Em::new(1.0);
let mut out = Styles::new();
- out.set(HeadingElem::set_numbering(None));
- out.set(PadElem::set_left(INDENT.into()));
+ out.set(HeadingElem::numbering, None);
+ out.set(PadElem::left, INDENT.into());
out
}
}
@@ -643,7 +643,7 @@ impl<'a> Generator<'a> {
introspector: Tracked<Introspector>,
) -> StrResult<Self> {
let bibliography = BibliographyElem::find(introspector)?;
- let groups = introspector.query(&CiteGroup::elem().select());
+ let groups = introspector.query(&CiteGroup::ELEM.select());
let infos = Vec::with_capacity(groups.len());
Ok(Self {
routines,
@@ -661,7 +661,8 @@ impl<'a> Generator<'a> {
LazyLock::new(hayagriva::archive::locales);
let database = &self.bibliography.sources.derived;
- let bibliography_style = &self.bibliography.style(StyleChain::default()).derived;
+ let bibliography_style =
+ &self.bibliography.style.get_ref(StyleChain::default()).derived;
// Process all citation groups.
let mut driver = BibliographyDriver::new();
@@ -689,7 +690,7 @@ impl<'a> Generator<'a> {
continue;
};
- let supplement = child.supplement(StyleChain::default());
+ let supplement = child.supplement.get_cloned(StyleChain::default());
let locator = supplement.as_ref().map(|_| {
SpecificLocator(
citationberg::taxonomy::Locator::Custom,
@@ -698,7 +699,7 @@ impl<'a> Generator<'a> {
});
let mut hidden = false;
- let special_form = match child.form(StyleChain::default()) {
+ let special_form = match child.form.get(StyleChain::default()) {
None => {
hidden = true;
None
@@ -720,7 +721,7 @@ impl<'a> Generator<'a> {
continue;
}
- let style = match first.style(StyleChain::default()) {
+ let style = match first.style.get_ref(StyleChain::default()) {
Smart::Auto => bibliography_style.get(),
Smart::Custom(style) => style.derived.get(),
};
@@ -736,23 +737,20 @@ impl<'a> Generator<'a> {
driver.citation(CitationRequest::new(
items,
style,
- Some(locale(
- first.lang().copied().unwrap_or(Lang::ENGLISH),
- first.region().copied().flatten(),
- )),
+ Some(locale(first.lang.unwrap_or(Lang::ENGLISH), first.region.flatten())),
&LOCALES,
None,
));
}
let locale = locale(
- self.bibliography.lang().copied().unwrap_or(Lang::ENGLISH),
- self.bibliography.region().copied().flatten(),
+ self.bibliography.lang.unwrap_or(Lang::ENGLISH),
+ self.bibliography.region.flatten(),
);
// Add hidden items for everything if we should print the whole
// bibliography.
- if self.bibliography.full(StyleChain::default()) {
+ if self.bibliography.full.get(StyleChain::default()) {
for (_, entry) in database.iter() {
driver.citation(CitationRequest::new(
vec![CitationItem::new(entry, None, None, true, None)],
@@ -1071,25 +1069,24 @@ fn apply_formatting(mut content: Content, format: &hayagriva::Formatting) -> Con
match format.font_style {
citationberg::FontStyle::Normal => {}
citationberg::FontStyle::Italic => {
- content = content.styled(TextElem::set_style(FontStyle::Italic));
+ content = content.set(TextElem::style, FontStyle::Italic);
}
}
match format.font_variant {
citationberg::FontVariant::Normal => {}
citationberg::FontVariant::SmallCaps => {
- content =
- content.styled(TextElem::set_smallcaps(Some(Smallcaps::Minuscules)));
+ content = content.set(TextElem::smallcaps, Some(Smallcaps::Minuscules));
}
}
match format.font_weight {
citationberg::FontWeight::Normal => {}
citationberg::FontWeight::Bold => {
- content = content.styled(TextElem::set_delta(WeightDelta(300)));
+ content = content.set(TextElem::delta, WeightDelta(300));
}
citationberg::FontWeight::Light => {
- content = content.styled(TextElem::set_delta(WeightDelta(-100)));
+ content = content.set(TextElem::delta, WeightDelta(-100));
}
}
diff --git a/crates/typst-library/src/model/cite.rs b/crates/typst-library/src/model/cite.rs
index 29497993..19513990 100644
--- a/crates/typst-library/src/model/cite.rs
+++ b/crates/typst-library/src/model/cite.rs
@@ -106,7 +106,6 @@ pub struct CiteElem {
Some(Spanned { v: Smart::Auto, .. }) => Some(Smart::Auto),
None => None,
})]
- #[borrowed]
pub style: Smart<Derived<CslSource, CslStyle>>,
/// The text language setting where the citation is.
@@ -123,8 +122,8 @@ pub struct CiteElem {
impl Synthesize for Packed<CiteElem> {
fn synthesize(&mut self, _: &mut Engine, styles: StyleChain) -> SourceResult<()> {
let elem = self.as_mut();
- elem.push_lang(TextElem::lang_in(styles));
- elem.push_region(TextElem::region_in(styles));
+ elem.lang = Some(styles.get(TextElem::lang));
+ elem.region = Some(styles.get(TextElem::region));
Ok(())
}
}
diff --git a/crates/typst-library/src/model/document.rs b/crates/typst-library/src/model/document.rs
index 1bce6b35..47e99973 100644
--- a/crates/typst-library/src/model/document.rs
+++ b/crates/typst-library/src/model/document.rs
@@ -3,7 +3,7 @@ use ecow::EcoString;
use crate::diag::{bail, HintedStrResult, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
- cast, elem, Args, Array, Construct, Content, Datetime, Fields, OneOrMultiple, Smart,
+ cast, elem, Args, Array, Construct, Content, Datetime, OneOrMultiple, Smart,
StyleChain, Styles, Value,
};
@@ -109,23 +109,26 @@ impl DocumentInfo {
/// Document set rules are a bit special, so we need to do this manually.
pub fn populate(&mut self, styles: &Styles) {
let chain = StyleChain::new(styles);
- let has = |field| styles.has::<DocumentElem>(field as _);
- if has(<DocumentElem as Fields>::Enum::Title) {
- self.title =
- DocumentElem::title_in(chain).map(|content| content.plain_text());
+ if styles.has(DocumentElem::title) {
+ self.title = chain
+ .get_ref(DocumentElem::title)
+ .as_ref()
+ .map(|content| content.plain_text());
}
- if has(<DocumentElem as Fields>::Enum::Author) {
- self.author = DocumentElem::author_in(chain).0;
+ if styles.has(DocumentElem::author) {
+ self.author = chain.get_cloned(DocumentElem::author).0;
}
- if has(<DocumentElem as Fields>::Enum::Description) {
- self.description =
- DocumentElem::description_in(chain).map(|content| content.plain_text());
+ if styles.has(DocumentElem::description) {
+ self.description = chain
+ .get_ref(DocumentElem::description)
+ .as_ref()
+ .map(|content| content.plain_text());
}
- if has(<DocumentElem as Fields>::Enum::Keywords) {
- self.keywords = DocumentElem::keywords_in(chain).0;
+ if styles.has(DocumentElem::keywords) {
+ self.keywords = chain.get_cloned(DocumentElem::keywords).0;
}
- if has(<DocumentElem as Fields>::Enum::Date) {
- self.date = DocumentElem::date_in(chain);
+ if styles.has(DocumentElem::date) {
+ self.date = chain.get(DocumentElem::date);
}
}
}
diff --git a/crates/typst-library/src/model/emph.rs b/crates/typst-library/src/model/emph.rs
index 45097b34..2d9cbec1 100644
--- a/crates/typst-library/src/model/emph.rs
+++ b/crates/typst-library/src/model/emph.rs
@@ -40,13 +40,13 @@ impl Show for Packed<EmphElem> {
#[typst_macros::time(name = "emph", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone();
- Ok(if TargetElem::target_in(styles).is_html() {
+ Ok(if styles.get(TargetElem::target).is_html() {
HtmlElem::new(tag::em)
.with_body(Some(body))
.pack()
.spanned(self.span())
} else {
- body.styled(TextElem::set_emph(ItalicToggle(true)))
+ body.set(TextElem::emph, ItalicToggle(true))
})
}
}
diff --git a/crates/typst-library/src/model/enum.rs b/crates/typst-library/src/model/enum.rs
index f1f93702..8c191658 100644
--- a/crates/typst-library/src/model/enum.rs
+++ b/crates/typst-library/src/model/enum.rs
@@ -117,7 +117,6 @@ pub struct EnumElem {
/// + Numbering!
/// ```
#[default(Numbering::Pattern(NumberingPattern::from_str("1.").unwrap()))]
- #[borrowed]
pub numbering: Numbering,
/// Which number to start the enumeration with.
@@ -157,11 +156,9 @@ pub struct EnumElem {
pub reversed: bool,
/// The indentation of each item.
- #[resolve]
pub indent: Length,
/// The space between the numbering and the body of each item.
- #[resolve]
#[default(Em::new(0.5).into())]
pub body_indent: Length,
@@ -228,19 +225,19 @@ impl EnumElem {
impl Show for Packed<EnumElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let tight = self.tight(styles);
+ let tight = self.tight.get(styles);
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
let mut elem = HtmlElem::new(tag::ol);
- if self.reversed(styles) {
+ if self.reversed.get(styles) {
elem = elem.with_attr(attr::reversed, "reversed");
}
- if let Some(n) = self.start(styles).custom() {
+ if let Some(n) = self.start.get(styles).custom() {
elem = elem.with_attr(attr::start, eco_format!("{n}"));
}
let body = Content::sequence(self.children.iter().map(|item| {
let mut li = HtmlElem::new(tag::li);
- if let Some(nr) = item.number(styles) {
+ if let Some(nr) = item.number.get(styles) {
li = li.with_attr(attr::value, eco_format!("{nr}"));
}
// Text in wide enums shall always turn into paragraphs.
@@ -260,8 +257,9 @@ impl Show for Packed<EnumElem> {
if tight {
let spacing = self
- .spacing(styles)
- .unwrap_or_else(|| ParElem::leading_in(styles).into());
+ .spacing
+ .get(styles)
+ .unwrap_or_else(|| styles.get(ParElem::leading));
let v = VElem::new(spacing.into()).with_weak(true).with_attach(true).pack();
realized = v + realized;
}
diff --git a/crates/typst-library/src/model/figure.rs b/crates/typst-library/src/model/figure.rs
index bec667d6..7ac659a9 100644
--- a/crates/typst-library/src/model/figure.rs
+++ b/crates/typst-library/src/model/figure.rs
@@ -161,7 +161,6 @@ pub struct FigureElem {
pub scope: PlacementScope,
/// The figure's caption.
- #[borrowed]
pub caption: Option<Packed<FigureCaption>>,
/// The kind of figure this is.
@@ -214,13 +213,11 @@ pub struct FigureElem {
/// kind: "foo",
/// )
/// ```
- #[borrowed]
pub supplement: Smart<Option<Supplement>>,
/// How to number the figure. Accepts a
/// [numbering pattern or function]($numbering).
#[default(Some(NumberingPattern::from_str("1").unwrap().into()))]
- #[borrowed]
pub numbering: Option<Numbering>,
/// The vertical gap between the body and caption.
@@ -259,25 +256,25 @@ impl Synthesize for Packed<FigureElem> {
let span = self.span();
let location = self.location();
let elem = self.as_mut();
- let numbering = elem.numbering(styles);
+ let numbering = elem.numbering.get_ref(styles);
// Determine the figure's kind.
- let kind = elem.kind(styles).unwrap_or_else(|| {
+ let kind = elem.kind.get_cloned(styles).unwrap_or_else(|| {
elem.body
.query_first(&Selector::can::<dyn Figurable>())
.map(|elem| FigureKind::Elem(elem.func()))
- .unwrap_or_else(|| FigureKind::Elem(ImageElem::elem()))
+ .unwrap_or_else(|| FigureKind::Elem(ImageElem::ELEM))
});
// Resolve the supplement.
- let supplement = match elem.supplement(styles).as_ref() {
+ let supplement = match elem.supplement.get_ref(styles).as_ref() {
Smart::Auto => {
// Default to the local name for the kind, if available.
let name = match &kind {
FigureKind::Elem(func) => func
.local_name(
- TextElem::lang_in(styles),
- TextElem::region_in(styles),
+ styles.get(TextElem::lang),
+ styles.get(TextElem::region),
)
.map(TextElem::packed),
FigureKind::Name(_) => None,
@@ -307,24 +304,25 @@ impl Synthesize for Packed<FigureElem> {
// Construct the figure's counter.
let counter = Counter::new(CounterKey::Selector(
- select_where!(FigureElem, Kind => kind.clone()),
+ select_where!(FigureElem, kind => kind.clone()),
));
// Fill the figure's caption.
- let mut caption = elem.caption(styles).clone();
+ let mut caption = elem.caption.get_cloned(styles);
if let Some(caption) = &mut caption {
caption.synthesize(engine, styles)?;
- caption.push_kind(kind.clone());
- caption.push_supplement(supplement.clone());
- caption.push_numbering(numbering.clone());
- caption.push_counter(Some(counter.clone()));
- caption.push_figure_location(location);
+ caption.kind = Some(kind.clone());
+ caption.supplement = Some(supplement.clone());
+ caption.numbering = Some(numbering.clone());
+ caption.counter = Some(Some(counter.clone()));
+ caption.figure_location = Some(location);
}
- elem.push_kind(Smart::Custom(kind));
- elem.push_supplement(Smart::Custom(supplement.map(Supplement::Content)));
- elem.push_counter(Some(counter));
- elem.push_caption(caption);
+ elem.kind.set(Smart::Custom(kind));
+ elem.supplement
+ .set(Smart::Custom(supplement.map(Supplement::Content)));
+ elem.counter = Some(Some(counter));
+ elem.caption.set(caption);
Ok(())
}
@@ -334,19 +332,19 @@ impl Show for Packed<FigureElem> {
#[typst_macros::time(name = "figure", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let span = self.span();
- let target = TargetElem::target_in(styles);
+ let target = styles.get(TargetElem::target);
let mut realized = self.body.clone();
// Build the caption, if any.
- if let Some(caption) = self.caption(styles).clone() {
- let (first, second) = match caption.position(styles) {
+ if let Some(caption) = self.caption.get_cloned(styles) {
+ let (first, second) = match caption.position.get(styles) {
OuterVAlignment::Top => (caption.pack(), realized),
OuterVAlignment::Bottom => (realized, caption.pack()),
};
let mut seq = Vec::with_capacity(3);
seq.push(first);
if !target.is_html() {
- let v = VElem::new(self.gap(styles).into()).with_weak(true);
+ let v = VElem::new(self.gap.get(styles).into()).with_weak(true);
seq.push(v.pack().spanned(span))
}
seq.push(second);
@@ -370,14 +368,14 @@ impl Show for Packed<FigureElem> {
.spanned(span);
// Wrap in a float.
- if let Some(align) = self.placement(styles) {
+ if let Some(align) = self.placement.get(styles) {
realized = PlaceElem::new(realized)
.with_alignment(align.map(|align| HAlignment::Center + align))
- .with_scope(self.scope(styles))
+ .with_scope(self.scope.get(styles))
.with_float(true)
.pack()
.spanned(span);
- } else if self.scope(styles) == PlacementScope::Parent {
+ } else if self.scope.get(styles) == PlacementScope::Parent {
bail!(
span,
"parent-scoped placement is only available for floating figures";
@@ -394,8 +392,8 @@ impl ShowSet for Packed<FigureElem> {
// Still allows breakable figures with
// `show figure: set block(breakable: true)`.
let mut map = Styles::new();
- map.set(BlockElem::set_breakable(false));
- map.set(AlignElem::set_alignment(Alignment::CENTER));
+ map.set(BlockElem::breakable, false);
+ map.set(AlignElem::alignment, Alignment::CENTER);
map
}
}
@@ -413,29 +411,28 @@ impl Count for Packed<FigureElem> {
impl Refable for Packed<FigureElem> {
fn supplement(&self) -> Content {
// After synthesis, this should always be custom content.
- match (**self).supplement(StyleChain::default()).as_ref() {
- Smart::Custom(Some(Supplement::Content(content))) => content.clone(),
+ match self.supplement.get_cloned(StyleChain::default()) {
+ Smart::Custom(Some(Supplement::Content(content))) => content,
_ => Content::empty(),
}
}
fn counter(&self) -> Counter {
- (**self)
- .counter()
- .cloned()
+ self.counter
+ .clone()
.flatten()
- .unwrap_or_else(|| Counter::of(FigureElem::elem()))
+ .unwrap_or_else(|| Counter::of(FigureElem::ELEM))
}
fn numbering(&self) -> Option<&Numbering> {
- (**self).numbering(StyleChain::default()).as_ref()
+ self.numbering.get_ref(StyleChain::default()).as_ref()
}
}
impl Outlinable for Packed<FigureElem> {
fn outlined(&self) -> bool {
- (**self).outlined(StyleChain::default())
- && (self.caption(StyleChain::default()).is_some()
+ self.outlined.get(StyleChain::default())
+ && (self.caption.get_ref(StyleChain::default()).is_some()
|| self.numbering().is_some())
}
@@ -449,7 +446,8 @@ impl Outlinable for Packed<FigureElem> {
}
fn body(&self) -> Content {
- self.caption(StyleChain::default())
+ self.caption
+ .get_ref(StyleChain::default())
.as_ref()
.map(|caption| caption.body.clone())
.unwrap_or_default()
@@ -573,10 +571,10 @@ impl FigureCaption {
}
fn get_separator(&self, styles: StyleChain) -> Content {
- self.separator(styles).unwrap_or_else(|| {
+ self.separator.get_cloned(styles).unwrap_or_else(|| {
TextElem::packed(Self::local_separator(
- TextElem::lang_in(styles),
- TextElem::region_in(styles),
+ styles.get(TextElem::lang),
+ styles.get(TextElem::region),
))
})
}
@@ -585,7 +583,7 @@ impl FigureCaption {
impl Synthesize for Packed<FigureCaption> {
fn synthesize(&mut self, _: &mut Engine, styles: StyleChain) -> SourceResult<()> {
let elem = self.as_mut();
- elem.push_separator(Smart::Custom(elem.get_separator(styles)));
+ elem.separator.set(Smart::Custom(elem.get_separator(styles)));
Ok(())
}
}
@@ -601,10 +599,10 @@ impl Show for Packed<FigureCaption> {
Some(Some(counter)),
Some(Some(location)),
) = (
- self.supplement().cloned(),
- self.numbering(),
- self.counter(),
- self.figure_location(),
+ self.supplement.clone(),
+ &self.numbering,
+ &self.counter,
+ &self.figure_location,
) {
let numbers = counter.display_at_loc(engine, *location, styles, numbering)?;
if !supplement.is_empty() {
@@ -613,7 +611,7 @@ impl Show for Packed<FigureCaption> {
realized = supplement + numbers + self.get_separator(styles) + realized;
}
- Ok(if TargetElem::target_in(styles).is_html() {
+ Ok(if styles.get(TargetElem::target).is_html() {
HtmlElem::new(tag::figcaption)
.with_body(Some(realized))
.pack()
diff --git a/crates/typst-library/src/model/footnote.rs b/crates/typst-library/src/model/footnote.rs
index dfa3933b..63a461bd 100644
--- a/crates/typst-library/src/model/footnote.rs
+++ b/crates/typst-library/src/model/footnote.rs
@@ -67,7 +67,6 @@ pub struct FootnoteElem {
/// #footnote[Star],
/// #footnote[Dagger]
/// ```
- #[borrowed]
#[default(Numbering::Pattern(NumberingPattern::from_str("1").unwrap()))]
pub numbering: Numbering,
@@ -141,8 +140,8 @@ impl Show for Packed<FootnoteElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let span = self.span();
let loc = self.declaration_location(engine).at(span)?;
- let numbering = self.numbering(styles);
- let counter = Counter::of(FootnoteElem::elem());
+ let numbering = self.numbering.get_ref(styles);
+ let counter = Counter::of(FootnoteElem::ELEM);
let num = counter.display_at_loc(engine, loc, styles, numbering)?;
let sup = SuperElem::new(num).pack().spanned(span);
let loc = loc.variant(1);
@@ -248,7 +247,6 @@ pub struct FootnoteEntry {
/// ]
/// ```
#[default(Em::new(1.0).into())]
- #[resolve]
pub clearance: Length,
/// The gap between footnote entries.
@@ -261,7 +259,6 @@ pub struct FootnoteEntry {
/// #footnote[Apart]
/// ```
#[default(Em::new(0.5).into())]
- #[resolve]
pub gap: Length,
/// The indent of each footnote entry.
@@ -283,8 +280,8 @@ impl Show for Packed<FootnoteEntry> {
let span = self.span();
let number_gap = Em::new(0.05);
let default = StyleChain::default();
- let numbering = self.note.numbering(default);
- let counter = Counter::of(FootnoteElem::elem());
+ let numbering = self.note.numbering.get_ref(default);
+ let counter = Counter::of(FootnoteElem::ELEM);
let Some(loc) = self.note.location() else {
bail!(
span, "footnote entry must have a location";
@@ -300,7 +297,7 @@ impl Show for Packed<FootnoteEntry> {
.located(loc.variant(1));
Ok(Content::sequence([
- HElem::new(self.indent(styles).into()).pack(),
+ HElem::new(self.indent.get(styles).into()).pack(),
sup,
HElem::new(number_gap.into()).with_weak(true).pack(),
self.note.body_content().unwrap().clone(),
@@ -311,8 +308,8 @@ impl Show for Packed<FootnoteEntry> {
impl ShowSet for Packed<FootnoteEntry> {
fn show_set(&self, _: StyleChain) -> Styles {
let mut out = Styles::new();
- out.set(ParElem::set_leading(Em::new(0.5).into()));
- out.set(TextElem::set_size(TextSize(Em::new(0.85).into())));
+ out.set(ParElem::leading, Em::new(0.5).into());
+ out.set(TextElem::size, TextSize(Em::new(0.85).into()));
out
}
}
diff --git a/crates/typst-library/src/model/heading.rs b/crates/typst-library/src/model/heading.rs
index 00931c81..d6f6d01f 100644
--- a/crates/typst-library/src/model/heading.rs
+++ b/crates/typst-library/src/model/heading.rs
@@ -106,7 +106,6 @@ pub struct HeadingElem {
/// == A subsection
/// === A sub-subsection
/// ```
- #[borrowed]
pub numbering: Option<Numbering>,
/// A supplement for the heading.
@@ -187,8 +186,8 @@ pub struct HeadingElem {
impl HeadingElem {
pub fn resolve_level(&self, styles: StyleChain) -> NonZeroUsize {
- self.level(styles).unwrap_or_else(|| {
- NonZeroUsize::new(self.offset(styles) + self.depth(styles).get())
+ self.level.get(styles).unwrap_or_else(|| {
+ NonZeroUsize::new(self.offset.get(styles) + self.depth.get(styles).get())
.expect("overflow to 0 on NoneZeroUsize + usize")
})
}
@@ -200,7 +199,7 @@ impl Synthesize for Packed<HeadingElem> {
engine: &mut Engine,
styles: StyleChain,
) -> SourceResult<()> {
- let supplement = match (**self).supplement(styles) {
+ let supplement = match self.supplement.get_ref(styles) {
Smart::Auto => TextElem::packed(Self::local_name_in(styles)),
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => {
@@ -209,8 +208,9 @@ impl Synthesize for Packed<HeadingElem> {
};
let elem = self.as_mut();
- elem.push_level(Smart::Custom(elem.resolve_level(styles)));
- elem.push_supplement(Smart::Custom(Some(Supplement::Content(supplement))));
+ elem.level.set(Smart::Custom(elem.resolve_level(styles)));
+ elem.supplement
+ .set(Smart::Custom(Some(Supplement::Content(supplement))));
Ok(())
}
}
@@ -218,22 +218,22 @@ impl Synthesize for Packed<HeadingElem> {
impl Show for Packed<HeadingElem> {
#[typst_macros::time(name = "heading", span = self.span())]
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let html = TargetElem::target_in(styles).is_html();
+ let html = styles.get(TargetElem::target).is_html();
const SPACING_TO_NUMBERING: Em = Em::new(0.3);
let span = self.span();
let mut realized = self.body.clone();
- let hanging_indent = self.hanging_indent(styles);
+ let hanging_indent = self.hanging_indent.get(styles);
let mut indent = match hanging_indent {
Smart::Custom(length) => length.resolve(styles),
Smart::Auto => Abs::zero(),
};
- if let Some(numbering) = (**self).numbering(styles).as_ref() {
+ if let Some(numbering) = self.numbering.get_ref(styles).as_ref() {
let location = self.location().unwrap();
- let numbering = Counter::of(HeadingElem::elem())
+ let numbering = Counter::of(HeadingElem::ELEM)
.display_at_loc(engine, location, styles, numbering)?
.spanned(span);
@@ -293,7 +293,7 @@ impl Show for Packed<HeadingElem> {
let block = if indent != Abs::zero() {
let body = HElem::new((-indent).into()).pack() + realized;
let inset = Sides::default()
- .with(TextElem::dir_in(styles).start(), Some(indent.into()));
+ .with(styles.resolve(TextElem::dir).start(), Some(indent.into()));
BlockElem::new()
.with_body(Some(BlockBody::Content(body)))
.with_inset(inset)
@@ -307,7 +307,7 @@ impl Show for Packed<HeadingElem> {
impl ShowSet for Packed<HeadingElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
- let level = (**self).resolve_level(styles).get();
+ let level = self.resolve_level(styles).get();
let scale = match level {
1 => 1.4,
2 => 1.2,
@@ -319,49 +319,49 @@ impl ShowSet for Packed<HeadingElem> {
let below = Em::new(0.75) / scale;
let mut out = Styles::new();
- out.set(TextElem::set_size(TextSize(size.into())));
- out.set(TextElem::set_weight(FontWeight::BOLD));
- out.set(BlockElem::set_above(Smart::Custom(above.into())));
- out.set(BlockElem::set_below(Smart::Custom(below.into())));
- out.set(BlockElem::set_sticky(true));
+ out.set(TextElem::size, TextSize(size.into()));
+ out.set(TextElem::weight, FontWeight::BOLD);
+ out.set(BlockElem::above, Smart::Custom(above.into()));
+ out.set(BlockElem::below, Smart::Custom(below.into()));
+ out.set(BlockElem::sticky, true);
out
}
}
impl Count for Packed<HeadingElem> {
fn update(&self) -> Option<CounterUpdate> {
- (**self)
- .numbering(StyleChain::default())
+ self.numbering
+ .get_ref(StyleChain::default())
.is_some()
- .then(|| CounterUpdate::Step((**self).resolve_level(StyleChain::default())))
+ .then(|| CounterUpdate::Step(self.resolve_level(StyleChain::default())))
}
}
impl Refable for Packed<HeadingElem> {
fn supplement(&self) -> Content {
// After synthesis, this should always be custom content.
- match (**self).supplement(StyleChain::default()) {
+ match self.supplement.get_cloned(StyleChain::default()) {
Smart::Custom(Some(Supplement::Content(content))) => content,
_ => Content::empty(),
}
}
fn counter(&self) -> Counter {
- Counter::of(HeadingElem::elem())
+ Counter::of(HeadingElem::ELEM)
}
fn numbering(&self) -> Option<&Numbering> {
- (**self).numbering(StyleChain::default()).as_ref()
+ self.numbering.get_ref(StyleChain::default()).as_ref()
}
}
impl Outlinable for Packed<HeadingElem> {
fn outlined(&self) -> bool {
- (**self).outlined(StyleChain::default())
+ self.outlined.get(StyleChain::default())
}
fn level(&self) -> NonZeroUsize {
- (**self).resolve_level(StyleChain::default())
+ self.resolve_level(StyleChain::default())
}
fn prefix(&self, numbers: Content) -> Content {
diff --git a/crates/typst-library/src/model/link.rs b/crates/typst-library/src/model/link.rs
index ea85aa94..1e2c708e 100644
--- a/crates/typst-library/src/model/link.rs
+++ b/crates/typst-library/src/model/link.rs
@@ -108,7 +108,7 @@ impl Show for Packed<LinkElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone();
- Ok(if TargetElem::target_in(styles).is_html() {
+ Ok(if styles.get(TargetElem::target).is_html() {
if let LinkTarget::Dest(Destination::Url(url)) = &self.dest {
HtmlElem::new(tag::a)
.with_attr(attr::href, url.clone().into_inner())
@@ -138,7 +138,7 @@ impl Show for Packed<LinkElem> {
impl ShowSet for Packed<LinkElem> {
fn show_set(&self, _: StyleChain) -> Styles {
let mut out = Styles::new();
- out.set(TextElem::set_hyphenate(Smart::Custom(false)));
+ out.set(TextElem::hyphenate, Smart::Custom(false));
out
}
}
diff --git a/crates/typst-library/src/model/list.rs b/crates/typst-library/src/model/list.rs
index 3c3afd33..5e6db1fa 100644
--- a/crates/typst-library/src/model/list.rs
+++ b/crates/typst-library/src/model/list.rs
@@ -86,7 +86,6 @@ pub struct ListElem {
/// - Items
/// - Items
/// ```
- #[borrowed]
#[default(ListMarker::Content(vec![
// These are all available in the default font, vertically centered, and
// roughly of the same size (with the last one having slightly lower
@@ -98,11 +97,9 @@ pub struct ListElem {
pub marker: ListMarker,
/// The indent of each item.
- #[resolve]
pub indent: Length,
/// The spacing between the marker and the body of each item.
- #[resolve]
#[default(Em::new(0.5).into())]
pub body_indent: Length,
@@ -141,9 +138,9 @@ impl ListElem {
impl Show for Packed<ListElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let tight = self.tight(styles);
+ let tight = self.tight.get(styles);
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::ul)
.with_body(Some(Content::sequence(self.children.iter().map(|item| {
// Text in wide lists shall always turn into paragraphs.
@@ -167,8 +164,9 @@ impl Show for Packed<ListElem> {
if tight {
let spacing = self
- .spacing(styles)
- .unwrap_or_else(|| ParElem::leading_in(styles).into());
+ .spacing
+ .get(styles)
+ .unwrap_or_else(|| styles.get(ParElem::leading));
let v = VElem::new(spacing.into()).with_weak(true).with_attach(true).pack();
realized = v + realized;
}
diff --git a/crates/typst-library/src/model/outline.rs b/crates/typst-library/src/model/outline.rs
index 16a11614..bb061fb7 100644
--- a/crates/typst-library/src/model/outline.rs
+++ b/crates/typst-library/src/model/outline.rs
@@ -183,8 +183,7 @@ pub struct OutlineElem {
/// caption: [Experiment results],
/// )
/// ```
- #[default(LocatableSelector(HeadingElem::elem().select()))]
- #[borrowed]
+ #[default(LocatableSelector(HeadingElem::ELEM.select()))]
pub target: LocatableSelector,
/// The maximum level up to which elements are included in the outline. When
@@ -257,7 +256,7 @@ impl Show for Packed<OutlineElem> {
// Build the outline title.
let mut seq = vec![];
- if let Some(title) = self.title(styles).unwrap_or_else(|| {
+ if let Some(title) = self.title.get_cloned(styles).unwrap_or_else(|| {
Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
}) {
seq.push(
@@ -268,8 +267,8 @@ impl Show for Packed<OutlineElem> {
);
}
- let elems = engine.introspector.query(&self.target(styles).0);
- let depth = self.depth(styles).unwrap_or(NonZeroUsize::MAX);
+ let elems = engine.introspector.query(&self.target.get_ref(styles).0);
+ let depth = self.depth.get(styles).unwrap_or(NonZeroUsize::MAX);
// Build the outline entries.
for elem in elems {
@@ -291,13 +290,13 @@ impl Show for Packed<OutlineElem> {
impl ShowSet for Packed<OutlineElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
- out.set(HeadingElem::set_outlined(false));
- out.set(HeadingElem::set_numbering(None));
- out.set(ParElem::set_justify(false));
- out.set(BlockElem::set_above(Smart::Custom(ParElem::leading_in(styles).into())));
+ out.set(HeadingElem::outlined, false);
+ out.set(HeadingElem::numbering, None);
+ out.set(ParElem::justify, false);
+ out.set(BlockElem::above, Smart::Custom(styles.get(ParElem::leading).into()));
// Makes the outline itself available to its entries. Should be
// superseded by a proper ancestry mechanism in the future.
- out.set(OutlineEntry::set_parent(Some(self.clone())));
+ out.set(OutlineEntry::parent, Some(self.clone()));
out
}
}
@@ -395,7 +394,6 @@ pub struct OutlineEntry {
///
/// = A New Beginning
/// ```
- #[borrowed]
#[default(Some(
RepeatElem::new(TextElem::packed("."))
.with_gap(Em::new(0.15).into())
@@ -472,7 +470,9 @@ impl OutlineEntry {
gap: Length,
) -> SourceResult<Content> {
let styles = context.styles().at(span)?;
- let outline = Self::parent_in(styles)
+ let outline = styles
+ .get_ref(Self::parent)
+ .as_ref()
.ok_or("must be called within the context of an outline")
.at(span)?;
let outline_loc = outline.location().unwrap();
@@ -483,7 +483,7 @@ impl OutlineEntry {
.transpose()?;
let prefix_inset = prefix_width.map(|w| w + gap.resolve(styles));
- let indent = outline.indent(styles);
+ let indent = outline.indent.get_ref(styles);
let (base_indent, hanging_indent) = match &indent {
Smart::Auto => compute_auto_indents(
engine.introspector,
@@ -527,7 +527,7 @@ impl OutlineEntry {
};
let inset = Sides::default().with(
- TextElem::dir_in(styles).start(),
+ styles.resolve(TextElem::dir).start(),
Some(base_indent + Rel::from(hanging_indent.unwrap_or_default())),
);
@@ -582,7 +582,7 @@ impl OutlineEntry {
// See also:
// - https://github.com/typst/typst/issues/4476
// - https://github.com/typst/typst/issues/5176
- let rtl = TextElem::dir_in(styles) == Dir::RTL;
+ let rtl = styles.resolve(TextElem::dir) == Dir::RTL;
if rtl {
// "Right-to-Left Embedding"
seq.push(TextElem::packed("\u{202B}"));
@@ -596,11 +596,11 @@ impl OutlineEntry {
}
// Add the filler between the section name and page number.
- if let Some(filler) = self.fill(styles) {
+ if let Some(filler) = self.fill.get_cloned(styles) {
seq.push(SpaceElem::shared().clone());
seq.push(
BoxElem::new()
- .with_body(Some(filler.clone()))
+ .with_body(Some(filler))
.with_width(Fr::one().into())
.pack()
.spanned(span),
@@ -717,7 +717,7 @@ fn query_prefix_widths(
outline_loc: Location,
) -> SmallVec<[Option<Abs>; 4]> {
let mut widths = SmallVec::<[Option<Abs>; 4]>::new();
- let elems = introspector.query(&select_where!(PrefixInfo, Key => outline_loc));
+ let elems = introspector.query(&select_where!(PrefixInfo, key => outline_loc));
for elem in &elems {
let info = elem.to_packed::<PrefixInfo>().unwrap();
let level = info.level.get();
diff --git a/crates/typst-library/src/model/par.rs b/crates/typst-library/src/model/par.rs
index cf31b519..8aceec2c 100644
--- a/crates/typst-library/src/model/par.rs
+++ b/crates/typst-library/src/model/par.rs
@@ -108,7 +108,6 @@ pub struct ParElem {
/// to `{-0.2em}` to get a baseline gap of exactly `{2em}`. The exact
/// distribution of the top- and bottom-edge values affects the bounds of
/// the first and last line.
- #[resolve]
#[default(Em::new(0.65).into())]
pub leading: Length,
@@ -122,7 +121,6 @@ pub struct ParElem {
/// that block's [`above`]($block.above) or [`below`]($block.below) property
/// takes precedence over the paragraph spacing. Headings, for instance,
/// reduce the spacing below them by default for a better look.
- #[resolve]
#[default(Em::new(1.2).into())]
pub spacing: Length,
@@ -213,7 +211,6 @@ pub struct ParElem {
///
/// #lorem(15)
/// ```
- #[resolve]
pub hanging_indent: Length,
/// The contents of the paragraph.
diff --git a/crates/typst-library/src/model/quote.rs b/crates/typst-library/src/model/quote.rs
index cd45eec8..a8cf3eae 100644
--- a/crates/typst-library/src/model/quote.rs
+++ b/crates/typst-library/src/model/quote.rs
@@ -123,7 +123,6 @@ pub struct QuoteElem {
///
/// #bibliography("works.bib", style: "apa")
/// ```
- #[borrowed]
attribution: Option<Attribution>,
/// The quote.
@@ -158,19 +157,19 @@ impl Show for Packed<QuoteElem> {
#[typst_macros::time(name = "quote", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let mut realized = self.body.clone();
- let block = self.block(styles);
- let html = TargetElem::target_in(styles).is_html();
+ let block = self.block.get(styles);
+ let html = styles.get(TargetElem::target).is_html();
- if self.quotes(styles).unwrap_or(!block) {
+ if self.quotes.get(styles).unwrap_or(!block) {
let quotes = SmartQuotes::get(
- SmartQuoteElem::quotes_in(styles),
- TextElem::lang_in(styles),
- TextElem::region_in(styles),
- SmartQuoteElem::alternative_in(styles),
+ styles.get_ref(SmartQuoteElem::quotes),
+ styles.get(TextElem::lang),
+ styles.get(TextElem::region),
+ styles.get(SmartQuoteElem::alternative),
);
// Alternate between single and double quotes.
- let Depth(depth) = QuoteElem::depth_in(styles);
+ let Depth(depth) = styles.get(QuoteElem::depth);
let double = depth % 2 == 0;
if !html {
@@ -183,10 +182,10 @@ impl Show for Packed<QuoteElem> {
realized,
TextElem::packed(quotes.close(double)),
])
- .styled(QuoteElem::set_depth(Depth(1)));
+ .set(QuoteElem::depth, Depth(1));
}
- let attribution = self.attribution(styles);
+ let attribution = self.attribution.get_ref(styles);
if block {
realized = if html {
@@ -204,7 +203,7 @@ impl Show for Packed<QuoteElem> {
}
.spanned(self.span());
- if let Some(attribution) = attribution.as_ref() {
+ if let Some(attribution) = attribution {
let attribution = match attribution {
Attribution::Content(content) => content.clone(),
Attribution::Label(label) => CiteElem::new(*label)
@@ -247,11 +246,11 @@ impl Show for Packed<QuoteElem> {
impl ShowSet for Packed<QuoteElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
- if self.block(styles) {
- out.set(PadElem::set_left(Em::new(1.0).into()));
- out.set(PadElem::set_right(Em::new(1.0).into()));
- out.set(BlockElem::set_above(Smart::Custom(Em::new(2.4).into())));
- out.set(BlockElem::set_below(Smart::Custom(Em::new(1.8).into())));
+ if self.block.get(styles) {
+ out.set(PadElem::left, Em::new(1.0).into());
+ out.set(PadElem::right, Em::new(1.0).into());
+ out.set(BlockElem::above, Smart::Custom(Em::new(2.4).into()));
+ out.set(BlockElem::below, Smart::Custom(Em::new(1.8).into()));
}
out
}
diff --git a/crates/typst-library/src/model/reference.rs b/crates/typst-library/src/model/reference.rs
index 17f93b7c..2d04a97a 100644
--- a/crates/typst-library/src/model/reference.rs
+++ b/crates/typst-library/src/model/reference.rs
@@ -175,7 +175,6 @@ pub struct RefElem {
/// in @intro[Part], it is done
/// manually.
/// ```
- #[borrowed]
pub supplement: Smart<Option<Supplement>>,
/// The kind of reference to produce.
@@ -207,12 +206,12 @@ impl Synthesize for Packed<RefElem> {
let citation = to_citation(self, engine, styles)?;
let elem = self.as_mut();
- elem.push_citation(Some(citation));
- elem.push_element(None);
+ elem.citation = Some(Some(citation));
+ elem.element = Some(None);
if !BibliographyElem::has(engine, elem.target) {
if let Ok(found) = engine.introspector.query_label(elem.target).cloned() {
- elem.push_element(Some(found));
+ elem.element = Some(Some(found));
return Ok(());
}
}
@@ -227,7 +226,7 @@ impl Show for Packed<RefElem> {
let elem = engine.introspector.query_label(self.target);
let span = self.span();
- let form = self.form(styles);
+ let form = self.form.get(styles);
if form == RefForm::Page {
let elem = elem.at(span)?;
let elem = elem.clone();
@@ -299,7 +298,7 @@ impl Show for Packed<RefElem> {
.hint(eco_format!(
"you can enable {} numbering with `#set {}(numbering: \"1.\")`",
elem.func().name(),
- if elem.func() == EquationElem::elem() {
+ if elem.func() == EquationElem::ELEM {
"math.equation"
} else {
elem.func().name()
@@ -332,7 +331,7 @@ fn show_reference(
let loc = elem.location().unwrap();
let numbers = counter.display_at_loc(engine, loc, styles, &numbering.trimmed())?;
- let supplement = match reference.supplement(styles).as_ref() {
+ let supplement = match reference.supplement.get_ref(styles) {
Smart::Auto => supplement,
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => supplement.resolve(engine, styles, [elem])?,
@@ -353,7 +352,7 @@ fn to_citation(
styles: StyleChain,
) -> SourceResult<Packed<CiteElem>> {
let mut elem = Packed::new(CiteElem::new(reference.target).with_supplement(
- match reference.supplement(styles).clone() {
+ match reference.supplement.get_cloned(styles) {
Smart::Custom(Some(Supplement::Content(content))) => Some(content),
_ => None,
},
diff --git a/crates/typst-library/src/model/strong.rs b/crates/typst-library/src/model/strong.rs
index 16d04ba9..08cf4839 100644
--- a/crates/typst-library/src/model/strong.rs
+++ b/crates/typst-library/src/model/strong.rs
@@ -44,13 +44,13 @@ impl Show for Packed<StrongElem> {
#[typst_macros::time(name = "strong", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone();
- Ok(if TargetElem::target_in(styles).is_html() {
+ Ok(if styles.get(TargetElem::target).is_html() {
HtmlElem::new(tag::strong)
.with_body(Some(body))
.pack()
.spanned(self.span())
} else {
- body.styled(TextElem::set_delta(WeightDelta(self.delta(styles))))
+ body.set(TextElem::delta, WeightDelta(self.delta.get(styles)))
})
}
}
diff --git a/crates/typst-library/src/model/table.rs b/crates/typst-library/src/model/table.rs
index dcc77b0d..72c5acc5 100644
--- a/crates/typst-library/src/model/table.rs
+++ b/crates/typst-library/src/model/table.rs
@@ -125,12 +125,10 @@ use crate::visualize::{Paint, Stroke};
pub struct TableElem {
/// The column sizes. See the [grid documentation]($grid) for more
/// information on track sizing.
- #[borrowed]
pub columns: TrackSizings,
/// The row sizes. See the [grid documentation]($grid) for more information
/// on track sizing.
- #[borrowed]
pub rows: TrackSizings,
/// The gaps between rows and columns. This is a shorthand for setting
@@ -141,7 +139,6 @@ pub struct TableElem {
/// The gaps between columns. Takes precedence over `gutter`. See the
/// [grid documentation]($grid) for more information on gutters.
- #[borrowed]
#[parse(
let gutter = args.named("gutter")?;
args.named("column-gutter")?.or_else(|| gutter.clone())
@@ -151,7 +148,6 @@ pub struct TableElem {
/// The gaps between rows. Takes precedence over `gutter`. See the
/// [grid documentation]($grid) for more information on gutters.
#[parse(args.named("row-gutter")?.or_else(|| gutter.clone()))]
- #[borrowed]
pub row_gutter: TrackSizings,
/// How to fill the cells.
@@ -176,7 +172,6 @@ pub struct TableElem {
/// [Profit:], [500 €], [1000 €], [1500 €],
/// )
/// ```
- #[borrowed]
pub fill: Celled<Option<Paint>>,
/// How to align the cells' content.
@@ -194,7 +189,6 @@ pub struct TableElem {
/// [A], [B], [C],
/// )
/// ```
- #[borrowed]
pub align: Celled<Smart<Alignment>>,
/// How to [stroke] the cells.
@@ -209,7 +203,6 @@ pub struct TableElem {
///
/// See the [grid documentation]($grid.stroke) for more information on
/// strokes.
- #[resolve]
#[fold]
#[default(Celled::Value(Sides::splat(Some(Some(Arc::new(Stroke::default()))))))]
pub stroke: Celled<Sides<Option<Option<Arc<Stroke>>>>>,
@@ -267,10 +260,10 @@ fn show_cell_html(tag: HtmlTag, cell: &Cell, styles: StyleChain) -> Content {
let Some(cell) = cell.to_packed::<TableCell>() else { return cell };
let mut attrs = HtmlAttrs::default();
let span = |n: NonZeroUsize| (n != NonZeroUsize::MIN).then(|| n.to_string());
- if let Some(colspan) = span(cell.colspan(styles)) {
+ if let Some(colspan) = span(cell.colspan.get(styles)) {
attrs.push(attr::colspan, colspan);
}
- if let Some(rowspan) = span(cell.rowspan(styles)) {
+ if let Some(rowspan) = span(cell.rowspan.get(styles)) {
attrs.push(attr::rowspan, rowspan);
}
HtmlElem::new(tag)
@@ -357,7 +350,7 @@ fn show_cellgrid_html(grid: CellGrid, styles: StyleChain) -> Content {
impl Show for Packed<TableElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- Ok(if TargetElem::target_in(styles).is_html() {
+ Ok(if styles.get(TargetElem::target).is_html() {
// TODO: This is a hack, it is not clear whether the locator is actually used by HTML.
// How can we find out whether locator is actually used?
let locator = Locator::root();
@@ -621,7 +614,6 @@ pub struct TableHLine {
///
/// Specifying `{none}` removes any lines previously placed across this
/// line's range, including hlines or per-cell stroke below it.
- #[resolve]
#[fold]
#[default(Some(Arc::new(Stroke::default())))]
pub stroke: Option<Arc<Stroke>>,
@@ -666,7 +658,6 @@ pub struct TableVLine {
///
/// Specifying `{none}` removes any lines previously placed across this
/// line's range, including vlines or per-cell stroke below it.
- #[resolve]
#[fold]
#[default(Some(Arc::new(Stroke::default())))]
pub stroke: Option<Arc<Stroke>>,
@@ -802,7 +793,6 @@ pub struct TableCell {
pub inset: Smart<Sides<Option<Rel<Length>>>>,
/// The cell's [stroke]($table.stroke) override.
- #[resolve]
#[fold]
pub stroke: Sides<Option<Option<Arc<Stroke>>>>,
@@ -820,7 +810,7 @@ cast! {
impl Show for Packed<TableCell> {
fn show(&self, _engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- show_grid_cell(self.body.clone(), self.inset(styles), self.align(styles))
+ show_grid_cell(self.body.clone(), self.inset.get(styles), self.align.get(styles))
}
}
diff --git a/crates/typst-library/src/model/terms.rs b/crates/typst-library/src/model/terms.rs
index 3df74cd9..280c2d67 100644
--- a/crates/typst-library/src/model/terms.rs
+++ b/crates/typst-library/src/model/terms.rs
@@ -66,7 +66,6 @@ pub struct TermsElem {
/// / Colon: A nice separator symbol.
/// ```
#[default(HElem::new(Em::new(0.6).into()).with_weak(true).pack())]
- #[borrowed]
pub separator: Content,
/// The indentation of each item.
@@ -121,9 +120,9 @@ impl TermsElem {
impl Show for Packed<TermsElem> {
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let span = self.span();
- let tight = self.tight(styles);
+ let tight = self.tight.get(styles);
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::dl)
.with_body(Some(Content::sequence(self.children.iter().flat_map(
|item| {
@@ -148,14 +147,14 @@ impl Show for Packed<TermsElem> {
.pack());
}
- let separator = self.separator(styles);
- let indent = self.indent(styles);
- let hanging_indent = self.hanging_indent(styles);
- let gutter = self.spacing(styles).unwrap_or_else(|| {
+ let separator = self.separator.get_ref(styles);
+ let indent = self.indent.get(styles);
+ let hanging_indent = self.hanging_indent.get(styles);
+ let gutter = self.spacing.get(styles).unwrap_or_else(|| {
if tight {
- ParElem::leading_in(styles).into()
+ styles.get(ParElem::leading)
} else {
- ParElem::spacing_in(styles).into()
+ styles.get(ParElem::spacing)
}
});
@@ -179,19 +178,21 @@ impl Show for Packed<TermsElem> {
children.push(StackChild::Block(Content::sequence(seq)));
}
- let padding = Sides::default().with(TextElem::dir_in(styles).start(), pad.into());
+ let padding =
+ Sides::default().with(styles.resolve(TextElem::dir).start(), pad.into());
let mut realized = StackElem::new(children)
.with_spacing(Some(gutter.into()))
.pack()
.spanned(span)
.padded(padding)
- .styled(TermsElem::set_within(true));
+ .set(TermsElem::within, true);
if tight {
let spacing = self
- .spacing(styles)
- .unwrap_or_else(|| ParElem::leading_in(styles).into());
+ .spacing
+ .get(styles)
+ .unwrap_or_else(|| styles.get(ParElem::leading));
let v = VElem::new(spacing.into())
.with_weak(true)
.with_attach(true)
diff --git a/crates/typst-library/src/pdf/embed.rs b/crates/typst-library/src/pdf/embed.rs
index 4c01cd65..0f93f95a 100644
--- a/crates/typst-library/src/pdf/embed.rs
+++ b/crates/typst-library/src/pdf/embed.rs
@@ -48,7 +48,6 @@ pub struct EmbedElem {
let resolved = id.vpath().as_rootless_path().to_string_lossy().replace("\\", "/").into();
Derived::new(path.clone(), resolved)
)]
- #[borrowed]
pub path: Derived<EcoString, EcoString>,
/// Raw file data, optionally.
@@ -72,17 +71,15 @@ pub struct EmbedElem {
pub relationship: Option<EmbeddedFileRelationship>,
/// The MIME type of the embedded file.
- #[borrowed]
pub mime_type: Option<EcoString>,
/// A description for the embedded file.
- #[borrowed]
pub description: Option<EcoString>,
}
impl Show for Packed<EmbedElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if TargetElem::target_in(styles) == Target::Html {
+ if styles.get(TargetElem::target) == Target::Html {
engine
.sink
.warn(warning!(self.span(), "embed was ignored during HTML export"));
diff --git a/crates/typst-library/src/text/case.rs b/crates/typst-library/src/text/case.rs
index 69dbf5e1..3b2ae450 100644
--- a/crates/typst-library/src/text/case.rs
+++ b/crates/typst-library/src/text/case.rs
@@ -37,9 +37,7 @@ pub fn upper(
fn case(text: Caseable, case: Case) -> Caseable {
match text {
Caseable::Str(v) => Caseable::Str(case.apply(&v).into()),
- Caseable::Content(v) => {
- Caseable::Content(v.styled(TextElem::set_case(Some(case))))
- }
+ Caseable::Content(v) => Caseable::Content(v.set(TextElem::case, Some(case))),
}
}
diff --git a/crates/typst-library/src/text/deco.rs b/crates/typst-library/src/text/deco.rs
index d745a48f..8c1d5634 100644
--- a/crates/typst-library/src/text/deco.rs
+++ b/crates/typst-library/src/text/deco.rs
@@ -30,7 +30,6 @@ pub struct UnderlineElem {
/// [care],
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Smart<Stroke>,
@@ -42,7 +41,6 @@ pub struct UnderlineElem {
/// The Tale Of A Faraway Line I
/// ]
/// ```
- #[resolve]
pub offset: Smart<Length>,
/// The amount by which to extend the line beyond (or within if negative)
@@ -53,7 +51,6 @@ pub struct UnderlineElem {
/// underline(extent: 2pt)[Chapter 1]
/// )
/// ```
- #[resolve]
pub extent: Length,
/// Whether the line skips sections in which it would collide with the
@@ -84,7 +81,7 @@ pub struct UnderlineElem {
impl Show for Packed<UnderlineElem> {
#[typst_macros::time(name = "underline", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
// Note: In modern HTML, `<u>` is not the underline element, but
// rather an "Unarticulated Annotation" element (see HTML spec
// 4.5.22). Using `text-decoration` instead is recommended by MDN.
@@ -94,15 +91,18 @@ impl Show for Packed<UnderlineElem> {
.pack());
}
- Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
- line: DecoLine::Underline {
- stroke: self.stroke(styles).unwrap_or_default(),
- offset: self.offset(styles),
- evade: self.evade(styles),
- background: self.background(styles),
- },
- extent: self.extent(styles),
- }])))
+ Ok(self.body.clone().set(
+ TextElem::deco,
+ smallvec![Decoration {
+ line: DecoLine::Underline {
+ stroke: self.stroke.resolve(styles).unwrap_or_default(),
+ offset: self.offset.resolve(styles),
+ evade: self.evade.get(styles),
+ background: self.background.get(styles),
+ },
+ extent: self.extent.resolve(styles),
+ }],
+ ))
}
}
@@ -127,7 +127,6 @@ pub struct OverlineElem {
/// [The Forest Theme],
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Smart<Stroke>,
@@ -139,7 +138,6 @@ pub struct OverlineElem {
/// The Tale Of A Faraway Line II
/// ]
/// ```
- #[resolve]
pub offset: Smart<Length>,
/// The amount by which to extend the line beyond (or within if negative)
@@ -150,7 +148,6 @@ pub struct OverlineElem {
/// #set underline(extent: 4pt)
/// #overline(underline[Typography Today])
/// ```
- #[resolve]
pub extent: Length,
/// Whether the line skips sections in which it would collide with the
@@ -186,22 +183,25 @@ pub struct OverlineElem {
impl Show for Packed<OverlineElem> {
#[typst_macros::time(name = "overline", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::span)
.with_attr(attr::style, "text-decoration: overline")
.with_body(Some(self.body.clone()))
.pack());
}
- Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
- line: DecoLine::Overline {
- stroke: self.stroke(styles).unwrap_or_default(),
- offset: self.offset(styles),
- evade: self.evade(styles),
- background: self.background(styles),
- },
- extent: self.extent(styles),
- }])))
+ Ok(self.body.clone().set(
+ TextElem::deco,
+ smallvec![Decoration {
+ line: DecoLine::Overline {
+ stroke: self.stroke.resolve(styles).unwrap_or_default(),
+ offset: self.offset.resolve(styles),
+ evade: self.evade.get(styles),
+ background: self.background.get(styles),
+ },
+ extent: self.extent.resolve(styles),
+ }],
+ ))
}
}
@@ -225,7 +225,6 @@ pub struct StrikeElem {
/// This is #strike(stroke: 1.5pt + red)[very stricken through]. \
/// This is #strike(stroke: 10pt)[redacted].
/// ```
- #[resolve]
#[fold]
pub stroke: Smart<Stroke>,
@@ -239,7 +238,6 @@ pub struct StrikeElem {
/// This is #strike(offset: auto)[low-ish]. \
/// This is #strike(offset: -3.5pt)[on-top].
/// ```
- #[resolve]
pub offset: Smart<Length>,
/// The amount by which to extend the line beyond (or within if negative)
@@ -249,7 +247,6 @@ pub struct StrikeElem {
/// This #strike(extent: -2pt)[skips] parts of the word.
/// This #strike(extent: 2pt)[extends] beyond the word.
/// ```
- #[resolve]
pub extent: Length,
/// Whether the line is placed behind the content.
@@ -270,19 +267,22 @@ pub struct StrikeElem {
impl Show for Packed<StrikeElem> {
#[typst_macros::time(name = "strike", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::s).with_body(Some(self.body.clone())).pack());
}
- Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
- // Note that we do not support evade option for strikethrough.
- line: DecoLine::Strikethrough {
- stroke: self.stroke(styles).unwrap_or_default(),
- offset: self.offset(styles),
- background: self.background(styles),
- },
- extent: self.extent(styles),
- }])))
+ Ok(self.body.clone().set(
+ TextElem::deco,
+ smallvec![Decoration {
+ // Note that we do not support evade option for strikethrough.
+ line: DecoLine::Strikethrough {
+ stroke: self.stroke.resolve(styles).unwrap_or_default(),
+ offset: self.offset.resolve(styles),
+ background: self.background.get(styles),
+ },
+ extent: self.extent.resolve(styles),
+ }],
+ ))
}
}
@@ -312,7 +312,6 @@ pub struct HighlightElem {
/// stroke: fuchsia
/// )[stroked highlighting].
/// ```
- #[resolve]
#[fold]
pub stroke: Sides<Option<Option<Stroke>>>,
@@ -346,7 +345,6 @@ pub struct HighlightElem {
/// ```example
/// A long #highlight(extent: 4pt)[background].
/// ```
- #[resolve]
pub extent: Length,
/// How much to round the highlight's corners. See the
@@ -357,7 +355,6 @@ pub struct HighlightElem {
/// radius: 5pt, extent: 2pt
/// )[carefully], it will be on the test.
/// ```
- #[resolve]
#[fold]
pub radius: Corners<Option<Rel<Length>>>,
@@ -369,25 +366,29 @@ pub struct HighlightElem {
impl Show for Packed<HighlightElem> {
#[typst_macros::time(name = "highlight", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::mark)
.with_body(Some(self.body.clone()))
.pack());
}
- Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
- line: DecoLine::Highlight {
- fill: self.fill(styles),
- stroke: self
- .stroke(styles)
- .unwrap_or_default()
- .map(|stroke| stroke.map(Stroke::unwrap_or_default)),
- top_edge: self.top_edge(styles),
- bottom_edge: self.bottom_edge(styles),
- radius: self.radius(styles).unwrap_or_default(),
- },
- extent: self.extent(styles),
- }])))
+ Ok(self.body.clone().set(
+ TextElem::deco,
+ smallvec![Decoration {
+ line: DecoLine::Highlight {
+ fill: self.fill.get_cloned(styles),
+ stroke: self
+ .stroke
+ .resolve(styles)
+ .unwrap_or_default()
+ .map(|stroke| stroke.map(Stroke::unwrap_or_default)),
+ top_edge: self.top_edge.get(styles),
+ bottom_edge: self.bottom_edge.get(styles),
+ radius: self.radius.resolve(styles).unwrap_or_default(),
+ },
+ extent: self.extent.resolve(styles),
+ }],
+ ))
}
}
diff --git a/crates/typst-library/src/text/lang.rs b/crates/typst-library/src/text/lang.rs
index a170714b..530b1f00 100644
--- a/crates/typst-library/src/text/lang.rs
+++ b/crates/typst-library/src/text/lang.rs
@@ -250,7 +250,7 @@ pub trait LocalName {
where
Self: Sized,
{
- Self::local_name(TextElem::lang_in(styles), TextElem::region_in(styles))
+ Self::local_name(styles.get(TextElem::lang), styles.get(TextElem::region))
}
}
diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs
index 230f8e50..51663633 100644
--- a/crates/typst-library/src/text/mod.rs
+++ b/crates/typst-library/src/text/mod.rs
@@ -165,7 +165,6 @@ pub struct TextElem {
font_list.map(|font_list| font_list.v)
})]
#[default(FontList(vec![FontFamily::new("Libertinus Serif")]))]
- #[borrowed]
#[ghost]
pub font: FontList,
@@ -260,7 +259,6 @@ pub struct TextElem {
#[parse(args.named_or_find("size")?)]
#[fold]
#[default(TextSize(Abs::pt(11.0).into()))]
- #[resolve]
#[ghost]
pub size: TextSize,
@@ -292,7 +290,6 @@ pub struct TextElem {
/// ```example
/// #text(stroke: 0.5pt + red)[Stroked]
/// ```
- #[resolve]
#[ghost]
pub stroke: Option<Stroke>,
@@ -302,7 +299,6 @@ pub struct TextElem {
/// #set text(tracking: 1.5pt)
/// Distant text.
/// ```
- #[resolve]
#[ghost]
pub tracking: Length,
@@ -318,7 +314,6 @@ pub struct TextElem {
/// #set text(spacing: 200%)
/// Text with distant words.
/// ```
- #[resolve]
#[default(Rel::one())]
#[ghost]
pub spacing: Rel<Length>,
@@ -341,7 +336,6 @@ pub struct TextElem {
/// A #text(baseline: 3pt)[lowered]
/// word.
/// ```
- #[resolve]
#[ghost]
pub baseline: Length,
@@ -483,7 +477,6 @@ pub struct TextElem {
/// #set text(dir: rtl)
/// هذا عربي.
/// ```
- #[resolve]
#[ghost]
pub dir: TextDir,
@@ -950,24 +943,24 @@ pub fn families(styles: StyleChain<'_>) -> impl Iterator<Item = &'_ FontFamily>
.collect()
});
- let tail = if TextElem::fallback_in(styles) { fallbacks.as_slice() } else { &[] };
- TextElem::font_in(styles).into_iter().chain(tail.iter())
+ let tail = if styles.get(TextElem::fallback) { fallbacks.as_slice() } else { &[] };
+ styles.get_ref(TextElem::font).into_iter().chain(tail.iter())
}
/// Resolve the font variant.
pub fn variant(styles: StyleChain) -> FontVariant {
let mut variant = FontVariant::new(
- TextElem::style_in(styles),
- TextElem::weight_in(styles),
- TextElem::stretch_in(styles),
+ styles.get(TextElem::style),
+ styles.get(TextElem::weight),
+ styles.get(TextElem::stretch),
);
- let WeightDelta(delta) = TextElem::delta_in(styles);
+ let WeightDelta(delta) = styles.get(TextElem::delta);
variant.weight = variant
.weight
.thicken(delta.clamp(i16::MIN as i64, i16::MAX as i64) as i16);
- if TextElem::emph_in(styles).0 {
+ if styles.get(TextElem::emph).0 {
variant.style = match variant.style {
FontStyle::Normal => FontStyle::Italic,
FontStyle::Italic => FontStyle::Normal,
@@ -996,11 +989,11 @@ impl Resolve for TextSize {
type Output = Abs;
fn resolve(self, styles: StyleChain) -> Self::Output {
- let factor = match EquationElem::size_in(styles) {
+ let factor = match styles.get(EquationElem::size) {
MathSize::Display | MathSize::Text => 1.0,
- MathSize::Script => EquationElem::script_scale_in(styles).0 as f64 / 100.0,
+ MathSize::Script => styles.get(EquationElem::script_scale).0 as f64 / 100.0,
MathSize::ScriptScript => {
- EquationElem::script_scale_in(styles).1 as f64 / 100.0
+ styles.get(EquationElem::script_scale).1 as f64 / 100.0
}
};
factor * self.0.resolve(styles)
@@ -1123,7 +1116,7 @@ impl Resolve for TextDir {
fn resolve(self, styles: StyleChain) -> Self::Output {
match self.0 {
- Smart::Auto => TextElem::lang_in(styles).dir(),
+ Smart::Auto => styles.get(TextElem::lang).dir(),
Smart::Custom(dir) => dir,
}
}
@@ -1236,67 +1229,67 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
};
// Features that are on by default in Harfbuzz are only added if disabled.
- if !TextElem::kerning_in(styles) {
+ if !styles.get(TextElem::kerning) {
feat(b"kern", 0);
}
// Features that are off by default in Harfbuzz are only added if enabled.
- if let Some(sc) = TextElem::smallcaps_in(styles) {
+ if let Some(sc) = styles.get(TextElem::smallcaps) {
feat(b"smcp", 1);
if sc == Smallcaps::All {
feat(b"c2sc", 1);
}
}
- if TextElem::alternates_in(styles) {
+ if styles.get(TextElem::alternates) {
feat(b"salt", 1);
}
- for set in TextElem::stylistic_set_in(styles).sets() {
+ for set in styles.get(TextElem::stylistic_set).sets() {
let storage = [b's', b's', b'0' + set / 10, b'0' + set % 10];
feat(&storage, 1);
}
- if !TextElem::ligatures_in(styles) {
+ if !styles.get(TextElem::ligatures) {
feat(b"liga", 0);
feat(b"clig", 0);
}
- if TextElem::discretionary_ligatures_in(styles) {
+ if styles.get(TextElem::discretionary_ligatures) {
feat(b"dlig", 1);
}
- if TextElem::historical_ligatures_in(styles) {
+ if styles.get(TextElem::historical_ligatures) {
feat(b"hlig", 1);
}
- match TextElem::number_type_in(styles) {
+ match styles.get(TextElem::number_type) {
Smart::Auto => {}
Smart::Custom(NumberType::Lining) => feat(b"lnum", 1),
Smart::Custom(NumberType::OldStyle) => feat(b"onum", 1),
}
- match TextElem::number_width_in(styles) {
+ match styles.get(TextElem::number_width) {
Smart::Auto => {}
Smart::Custom(NumberWidth::Proportional) => feat(b"pnum", 1),
Smart::Custom(NumberWidth::Tabular) => feat(b"tnum", 1),
}
- if TextElem::slashed_zero_in(styles) {
+ if styles.get(TextElem::slashed_zero) {
feat(b"zero", 1);
}
- if TextElem::fractions_in(styles) {
+ if styles.get(TextElem::fractions) {
feat(b"frac", 1);
}
- match EquationElem::size_in(styles) {
+ match styles.get(EquationElem::size) {
MathSize::Script => feat(b"ssty", 1),
MathSize::ScriptScript => feat(b"ssty", 2),
_ => {}
}
- for (tag, value) in TextElem::features_in(styles).0 {
+ for (tag, value) in styles.get_cloned(TextElem::features).0 {
tags.push(Feature::new(tag, value, ..))
}
@@ -1306,8 +1299,8 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
/// Process the language and region of a style chain into a
/// rustybuzz-compatible BCP 47 language.
pub fn language(styles: StyleChain) -> rustybuzz::Language {
- let mut bcp: EcoString = TextElem::lang_in(styles).as_str().into();
- if let Some(region) = TextElem::region_in(styles) {
+ let mut bcp: EcoString = styles.get(TextElem::lang).as_str().into();
+ if let Some(region) = styles.get(TextElem::region) {
bcp.push('-');
bcp.push_str(region.as_str());
}
diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs
index e1f4cf13..67038163 100644
--- a/crates/typst-library/src/text/raw.rs
+++ b/crates/typst-library/src/text/raw.rs
@@ -158,7 +158,6 @@ pub struct RawElem {
///
/// This is ```typ also *Typst*```, but inline!
/// ````
- #[borrowed]
pub lang: Option<EcoString>,
/// The horizontal alignment that each line in a raw block should have.
@@ -250,7 +249,6 @@ pub struct RawElem {
Some(Spanned { v: Smart::Auto, .. }) => Some(Smart::Auto),
None => None,
})]
- #[borrowed]
pub theme: Smart<Option<Derived<DataSource, RawTheme>>>,
/// The size for a tab stop in spaces. A tab is replaced with enough spaces to
@@ -306,7 +304,7 @@ impl RawElem {
impl Synthesize for Packed<RawElem> {
fn synthesize(&mut self, _: &mut Engine, styles: StyleChain) -> SourceResult<()> {
let seq = self.highlight(styles);
- self.push_lines(seq);
+ self.lines = Some(seq);
Ok(())
}
}
@@ -319,8 +317,8 @@ impl Packed<RawElem> {
let count = lines.len() as i64;
let lang = elem
- .lang(styles)
- .as_ref()
+ .lang
+ .get_ref(styles)
.as_ref()
.map(|s| s.to_lowercase())
.or(Some("txt".into()));
@@ -337,8 +335,8 @@ impl Packed<RawElem> {
})
};
- let syntaxes = LazyCell::new(|| elem.syntaxes(styles));
- let theme: &synt::Theme = match elem.theme(styles) {
+ let syntaxes = LazyCell::new(|| elem.syntaxes.get_cloned(styles));
+ let theme: &synt::Theme = match elem.theme.get_ref(styles) {
Smart::Auto => &RAW_THEME,
Smart::Custom(Some(theme)) => theme.derived.get(),
Smart::Custom(None) => return non_highlighted_result(lines).collect(),
@@ -434,7 +432,7 @@ impl Packed<RawElem> {
impl Show for Packed<RawElem> {
#[typst_macros::time(name = "raw", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let lines = self.lines().map(|v| v.as_slice()).unwrap_or_default();
+ let lines = self.lines.as_deref().unwrap_or_default();
let mut seq = EcoVec::with_capacity((2 * lines.len()).saturating_sub(1));
for (i, line) in lines.iter().enumerate() {
@@ -447,8 +445,8 @@ impl Show for Packed<RawElem> {
let mut realized = Content::sequence(seq);
- if TargetElem::target_in(styles).is_html() {
- return Ok(HtmlElem::new(if self.block(styles) {
+ if styles.get(TargetElem::target).is_html() {
+ return Ok(HtmlElem::new(if self.block.get(styles) {
tag::pre
} else {
tag::code
@@ -458,9 +456,9 @@ impl Show for Packed<RawElem> {
.spanned(self.span()));
}
- if self.block(styles) {
+ if self.block.get(styles) {
// Align the text before inserting it into the block.
- realized = realized.aligned(self.align(styles).into());
+ realized = realized.aligned(self.align.get(styles).into());
realized = BlockElem::new()
.with_body(Some(BlockBody::Content(realized)))
.pack()
@@ -474,14 +472,14 @@ impl Show for Packed<RawElem> {
impl ShowSet for Packed<RawElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
- out.set(TextElem::set_overhang(false));
- out.set(TextElem::set_lang(Lang::ENGLISH));
- out.set(TextElem::set_hyphenate(Smart::Custom(false)));
- out.set(TextElem::set_size(TextSize(Em::new(0.8).into())));
- out.set(TextElem::set_font(FontList(vec![FontFamily::new("DejaVu Sans Mono")])));
- out.set(TextElem::set_cjk_latin_spacing(Smart::Custom(None)));
- if self.block(styles) {
- out.set(ParElem::set_justify(false));
+ out.set(TextElem::overhang, false);
+ out.set(TextElem::lang, Lang::ENGLISH);
+ out.set(TextElem::hyphenate, Smart::Custom(false));
+ out.set(TextElem::size, TextSize(Em::new(0.8).into()));
+ out.set(TextElem::font, FontList(vec![FontFamily::new("DejaVu Sans Mono")]));
+ out.set(TextElem::cjk_latin_spacing, Smart::Custom(None));
+ if self.block.get(styles) {
+ out.set(ParElem::justify, false);
}
out
}
@@ -789,7 +787,7 @@ fn preprocess(
let mut text = text.get();
if text.contains('\t') {
- let tab_size = RawElem::tab_size_in(styles);
+ let tab_size = styles.get(RawElem::tab_size);
text = align_tabs(&text, tab_size);
}
split_newlines(&text)
@@ -809,11 +807,11 @@ fn styled(
let mut body = TextElem::packed(piece).spanned(span);
if span_offset > 0 {
- body = body.styled(TextElem::set_span_offset(span_offset));
+ body = body.set(TextElem::span_offset, span_offset);
}
if style.foreground != foreground {
- body = body.styled(TextElem::set_fill(to_typst(style.foreground).into()));
+ body = body.set(TextElem::fill, to_typst(style.foreground).into());
}
if style.font_style.contains(synt::FontStyle::BOLD) {
diff --git a/crates/typst-library/src/text/shift.rs b/crates/typst-library/src/text/shift.rs
index b7f3ed92..1a05d8f9 100644
--- a/crates/typst-library/src/text/shift.rs
+++ b/crates/typst-library/src/text/shift.rs
@@ -69,7 +69,7 @@ impl Show for Packed<SubElem> {
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone();
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::sub)
.with_body(Some(body))
.pack()
@@ -79,9 +79,9 @@ impl Show for Packed<SubElem> {
show_script(
styles,
body,
- self.typographic(styles),
- self.baseline(styles),
- self.size(styles),
+ self.typographic.get(styles),
+ self.baseline.get(styles),
+ self.size.get(styles),
ScriptKind::Sub,
)
}
@@ -151,7 +151,7 @@ impl Show for Packed<SuperElem> {
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone();
- if TargetElem::target_in(styles).is_html() {
+ if styles.get(TargetElem::target).is_html() {
return Ok(HtmlElem::new(tag::sup)
.with_body(Some(body))
.pack()
@@ -161,9 +161,9 @@ impl Show for Packed<SuperElem> {
show_script(
styles,
body,
- self.typographic(styles),
- self.baseline(styles),
- self.size(styles),
+ self.typographic.get(styles),
+ self.baseline.get(styles),
+ self.size.get(styles),
ScriptKind::Super,
)
}
@@ -177,13 +177,16 @@ fn show_script(
size: Smart<TextSize>,
kind: ScriptKind,
) -> SourceResult<Content> {
- let font_size = TextElem::size_in(styles);
- Ok(body.styled(TextElem::set_shift_settings(Some(ShiftSettings {
- typographic,
- shift: baseline.map(|l| -Em::from_length(l, font_size)),
- size: size.map(|t| Em::from_length(t.0, font_size)),
- kind,
- }))))
+ let font_size = styles.resolve(TextElem::size);
+ Ok(body.set(
+ TextElem::shift_settings,
+ Some(ShiftSettings {
+ typographic,
+ shift: baseline.map(|l| -Em::from_length(l, font_size)),
+ size: size.map(|t| Em::from_length(t.0, font_size)),
+ kind,
+ }),
+ ))
}
/// Configuration values for sub- or superscript text.
diff --git a/crates/typst-library/src/text/smallcaps.rs b/crates/typst-library/src/text/smallcaps.rs
index 924a45e8..1c283893 100644
--- a/crates/typst-library/src/text/smallcaps.rs
+++ b/crates/typst-library/src/text/smallcaps.rs
@@ -64,8 +64,9 @@ pub struct SmallcapsElem {
impl Show for Packed<SmallcapsElem> {
#[typst_macros::time(name = "smallcaps", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let sc = if self.all(styles) { Smallcaps::All } else { Smallcaps::Minuscules };
- Ok(self.body.clone().styled(TextElem::set_smallcaps(Some(sc))))
+ let sc =
+ if self.all.get(styles) { Smallcaps::All } else { Smallcaps::Minuscules };
+ Ok(self.body.clone().set(TextElem::smallcaps, Some(sc)))
}
}
diff --git a/crates/typst-library/src/text/smartquote.rs b/crates/typst-library/src/text/smartquote.rs
index 09cefd01..24787d06 100644
--- a/crates/typst-library/src/text/smartquote.rs
+++ b/crates/typst-library/src/text/smartquote.rs
@@ -83,13 +83,12 @@ pub struct SmartQuoteElem {
/// #set smartquote(quotes: (single: ("[[", "]]"), double: auto))
/// 'Das sind eigene Anführungszeichen.'
/// ```
- #[borrowed]
pub quotes: Smart<SmartQuoteDict>,
}
impl PlainText for Packed<SmartQuoteElem> {
fn plain_text(&self, text: &mut EcoString) {
- if self.double.unwrap_or(true) {
+ if self.double.as_option().unwrap_or(true) {
text.push_str("\"");
} else {
text.push_str("'");
diff --git a/crates/typst-library/src/visualize/curve.rs b/crates/typst-library/src/visualize/curve.rs
index 50944a51..587f0d4a 100644
--- a/crates/typst-library/src/visualize/curve.rs
+++ b/crates/typst-library/src/visualize/curve.rs
@@ -86,7 +86,6 @@ pub struct CurveElem {
/// down, up, down, up, down,
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Smart<Option<Stroke>>,
diff --git a/crates/typst-library/src/visualize/image/mod.rs b/crates/typst-library/src/visualize/image/mod.rs
index f5109798..48a14f0e 100644
--- a/crates/typst-library/src/visualize/image/mod.rs
+++ b/crates/typst-library/src/visualize/image/mod.rs
@@ -160,7 +160,6 @@ pub struct ImageElem {
Some(Spanned { v: Smart::Auto, .. }) => Some(Smart::Auto),
None => None,
})]
- #[borrowed]
pub icc: Smart<Derived<DataSource, Bytes>>,
}
@@ -199,22 +198,22 @@ impl ImageElem {
let source = Derived::new(DataSource::Bytes(bytes), loaded);
let mut elem = ImageElem::new(source);
if let Some(format) = format {
- elem.push_format(format);
+ elem.format.set(format);
}
if let Some(width) = width {
- elem.push_width(width);
+ elem.width.set(width);
}
if let Some(height) = height {
- elem.push_height(height);
+ elem.height.set(height);
}
if let Some(alt) = alt {
- elem.push_alt(alt);
+ elem.alt.set(alt);
}
if let Some(fit) = fit {
- elem.push_fit(fit);
+ elem.fit.set(fit);
}
if let Some(scaling) = scaling {
- elem.push_scaling(scaling);
+ elem.scaling.set(scaling);
}
Ok(elem.pack().spanned(span))
}
@@ -223,8 +222,8 @@ impl ImageElem {
impl Show for Packed<ImageElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_image)
- .with_width(self.width(styles))
- .with_height(self.height(styles))
+ .with_width(self.width.get(styles))
+ .with_height(self.height.get(styles))
.pack()
.spanned(self.span()))
}
diff --git a/crates/typst-library/src/visualize/line.rs b/crates/typst-library/src/visualize/line.rs
index 689321f1..d058b926 100644
--- a/crates/typst-library/src/visualize/line.rs
+++ b/crates/typst-library/src/visualize/line.rs
@@ -22,15 +22,12 @@ pub struct LineElem {
/// The start point of the line.
///
/// Must be an array of exactly two relative lengths.
- #[resolve]
pub start: Axes<Rel<Length>>,
/// The point where the line ends.
- #[resolve]
pub end: Option<Axes<Rel<Length>>>,
/// The line's length. This is only respected if `end` is `{none}`.
- #[resolve]
#[default(Abs::pt(30.0).into())]
pub length: Rel<Length>,
@@ -50,7 +47,6 @@ pub struct LineElem {
/// line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))),
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Stroke,
}
diff --git a/crates/typst-library/src/visualize/path.rs b/crates/typst-library/src/visualize/path.rs
index 968146cd..e19e091d 100644
--- a/crates/typst-library/src/visualize/path.rs
+++ b/crates/typst-library/src/visualize/path.rs
@@ -55,7 +55,6 @@ pub struct PathElem {
///
/// Can be set to `{none}` to disable the stroke or to `{auto}` for a
/// stroke of `{1pt}` black if and if only if no fill is given.
- #[resolve]
#[fold]
pub stroke: Smart<Option<Stroke>>,
diff --git a/crates/typst-library/src/visualize/polygon.rs b/crates/typst-library/src/visualize/polygon.rs
index 42b08343..d75e1a65 100644
--- a/crates/typst-library/src/visualize/polygon.rs
+++ b/crates/typst-library/src/visualize/polygon.rs
@@ -43,7 +43,6 @@ pub struct PolygonElem {
///
/// Can be set to `{none}` to disable the stroke or to `{auto}` for a
/// stroke of `{1pt}` black if and if only if no fill is given.
- #[resolve]
#[fold]
pub stroke: Smart<Option<Stroke>>,
@@ -117,10 +116,10 @@ impl PolygonElem {
let mut elem = PolygonElem::new(vertices);
if let Some(fill) = fill {
- elem.push_fill(fill);
+ elem.fill.set(fill);
}
if let Some(stroke) = stroke {
- elem.push_stroke(stroke);
+ elem.stroke.set(stroke);
}
elem.pack().spanned(span)
}
diff --git a/crates/typst-library/src/visualize/shape.rs b/crates/typst-library/src/visualize/shape.rs
index ff05be2b..f21bf93e 100644
--- a/crates/typst-library/src/visualize/shape.rs
+++ b/crates/typst-library/src/visualize/shape.rs
@@ -63,7 +63,6 @@ pub struct RectElem {
/// rect(stroke: 2pt + red),
/// )
/// ```
- #[resolve]
#[fold]
pub stroke: Smart<Sides<Option<Option<Stroke>>>>,
@@ -101,20 +100,17 @@ pub struct RectElem {
/// ),
/// )
/// ```
- #[resolve]
#[fold]
pub radius: Corners<Option<Rel<Length>>>,
/// How much to pad the rectangle's content.
/// See the [box's documentation]($box.inset) for more details.
- #[resolve]
#[fold]
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
pub inset: Sides<Option<Rel<Length>>>,
/// How much to expand the rectangle's size without affecting the layout.
/// See the [box's documentation]($box.outset) for more details.
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
@@ -123,15 +119,14 @@ pub struct RectElem {
/// When this is omitted, the rectangle takes on a default size of at most
/// `{45pt}` by `{30pt}`.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
impl Show for Packed<RectElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_rect)
- .with_width(self.width(styles))
- .with_height(self.height(styles))
+ .with_width(self.width.get(styles))
+ .with_height(self.height.get(styles))
.pack()
.spanned(self.span()))
}
@@ -186,26 +181,22 @@ pub struct SquareElem {
/// How to stroke the square. See the
/// [rectangle's documentation]($rect.stroke) for more details.
- #[resolve]
#[fold]
pub stroke: Smart<Sides<Option<Option<Stroke>>>>,
/// How much to round the square's corners. See the
/// [rectangle's documentation]($rect.radius) for more details.
- #[resolve]
#[fold]
pub radius: Corners<Option<Rel<Length>>>,
/// How much to pad the square's content. See the
/// [box's documentation]($box.inset) for more details.
- #[resolve]
#[fold]
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
pub inset: Sides<Option<Rel<Length>>>,
/// How much to expand the square's size without affecting the layout. See
/// the [box's documentation]($box.outset) for more details.
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
@@ -215,15 +206,14 @@ pub struct SquareElem {
/// When this is omitted, the square takes on a default size of at most
/// `{30pt}`.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
impl Show for Packed<SquareElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_square)
- .with_width(self.width(styles))
- .with_height(self.height(styles))
+ .with_width(self.width.get(styles))
+ .with_height(self.height.get(styles))
.pack()
.spanned(self.span()))
}
@@ -257,20 +247,17 @@ pub struct EllipseElem {
/// How to stroke the ellipse. See the
/// [rectangle's documentation]($rect.stroke) for more details.
- #[resolve]
#[fold]
pub stroke: Smart<Option<Stroke>>,
/// How much to pad the ellipse's content. See the
/// [box's documentation]($box.inset) for more details.
- #[resolve]
#[fold]
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
pub inset: Sides<Option<Rel<Length>>>,
/// How much to expand the ellipse's size without affecting the layout. See
/// the [box's documentation]($box.outset) for more details.
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
@@ -279,15 +266,14 @@ pub struct EllipseElem {
/// When this is omitted, the ellipse takes on a default size of at most
/// `{45pt}` by `{30pt}`.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
impl Show for Packed<EllipseElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_ellipse)
- .with_width(self.width(styles))
- .with_height(self.height(styles))
+ .with_width(self.width.get(styles))
+ .with_height(self.height.get(styles))
.pack()
.spanned(self.span()))
}
@@ -347,36 +333,32 @@ pub struct CircleElem {
/// How to stroke the circle. See the
/// [rectangle's documentation]($rect.stroke) for more details.
- #[resolve]
#[fold]
#[default(Smart::Auto)]
pub stroke: Smart<Option<Stroke>>,
/// How much to pad the circle's content. See the
/// [box's documentation]($box.inset) for more details.
- #[resolve]
#[fold]
#[default(Sides::splat(Some(Abs::pt(5.0).into())))]
pub inset: Sides<Option<Rel<Length>>>,
/// How much to expand the circle's size without affecting the layout. See
/// the [box's documentation]($box.outset) for more details.
- #[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,
/// The content to place into the circle. The circle expands to fit this
/// content, keeping the 1-1 aspect ratio.
#[positional]
- #[borrowed]
pub body: Option<Content>,
}
impl Show for Packed<CircleElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_circle)
- .with_width(self.width(styles))
- .with_height(self.height(styles))
+ .with_width(self.width.get(styles))
+ .with_height(self.height.get(styles))
.pack()
.spanned(self.span()))
}