typst::foundations

Struct Content

source
pub struct Content { /* private fields */ }
Expand description

A piece of document content.

This type is at the heart of Typst. All markup you write and most functions you call produce content values. You can create a content value by enclosing markup in square brackets. This is also how you pass content to functions.

§Example

Type of *Hello!* is
#type([*Hello!*])

Content can be added with the + operator, joined together and multiplied with integers. Wherever content is expected, you can also pass a string or {none}.

§Representation

Content consists of elements with fields. When constructing an element with its element function, you provide these fields as arguments and when you have a content value, you can access its fields with field access syntax.

Some fields are required: These must be provided when constructing an element and as a consequence, they are always available through field access on content of that type. Required fields are marked as such in the documentation.

Most fields are optional: Like required fields, they can be passed to the element function to configure them for a single element. However, these can also be configured with set rules to apply them to all elements within a scope. Optional fields are only available with field access syntax when they were explicitly passed to the element function, not when they result from a set rule.

Each element has a default appearance. However, you can also completely customize its appearance with a show rule. The show rule is passed the element. It can access the element’s field and produce arbitrary content from it.

In the web app, you can hover over a content variable to see exactly which elements the content is composed of and what fields they have. Alternatively, you can inspect the output of the [repr] function.

Implementations§

source§

impl Content

source

pub fn new<T: NativeElement>(elem: T) -> Self

Creates a new content from an element.

source

pub fn empty() -> Self

Creates a empty sequence content.

source

pub fn elem(&self) -> Element

Get the element of this content.

source

pub fn span(&self) -> Span

Get the span of the content.

source

pub fn spanned(self, span: Span) -> Self

Set the span of the content.

source

pub fn label(&self) -> Option<Label>

Get the label of the content.

source

pub fn labelled(self, label: Label) -> Self

Attach a label to the content.

source

pub fn set_label(&mut self, label: Label)

Set the label of the content.

source

pub fn located(self, loc: Location) -> Self

Assigns a location to the content.

This identifies the content and e.g. makes it linkable by .linked(Destination::Location(loc)).

Useful in combination with Location::variant.

source

pub fn set_location(&mut self, location: Location)

Set the location of the content.

source

pub fn is_guarded(&self, index: RecipeIndex) -> bool

Check whether a show rule recipe is disabled.

source

pub fn guarded(self, index: RecipeIndex) -> Self

Disable a show rule recipe.

source

pub fn is_prepared(&self) -> bool

Whether this content has already been prepared.

source

pub fn mark_prepared(&mut self)

Mark this content as prepared.

source

pub fn get( &self, id: u8, styles: Option<StyleChain<'_>>, ) -> Result<Value, FieldAccessError>

Get a field by ID.

This is the preferred way to access fields. However, you can only use it if you have set the field IDs yourself or are using the field IDs generated by the #[elem] macro.

source

pub fn get_by_name(&self, name: &str) -> Result<Value, FieldAccessError>

Get a field by name.

If you have access to the field IDs of the element, use Self::get instead.

source

pub fn field(&self, id: u8) -> StrResult<Value>

Get a field by ID, returning a missing field error if it does not exist.

This is the preferred way to access fields. However, you can only use it if you have set the field IDs yourself or are using the field IDs generated by the #[elem] macro.

source

pub fn field_by_name(&self, name: &str) -> StrResult<Value>

Get a field by name, returning a missing field error if it does not exist.

If you have access to the field IDs of the element, use Self::field instead.

source

pub fn materialize(&mut self, styles: StyleChain<'_>)

Resolve all fields with the styles and save them in-place.

source

pub fn sequence(iter: impl IntoIterator<Item = Self>) -> Self

Create a new sequence element from multiples elements.

source

pub fn is<T: NativeElement>(&self) -> bool

Whether the contained element is of type T.

source

pub fn to_packed<T: NativeElement>(&self) -> Option<&Packed<T>>

Downcasts the element to a packed value.

source

pub fn to_packed_mut<T: NativeElement>(&mut self) -> Option<&mut Packed<T>>

Downcasts the element to a mutable packed value.

source

pub fn into_packed<T: NativeElement>(self) -> Result<Packed<T>, Self>

Downcasts the element into an owned packed value.

source

pub fn unpack<T: NativeElement>(self) -> Result<T, Self>

Extract the raw underlying element.

source

pub fn can<C>(&self) -> bool
where C: ?Sized + 'static,

Whether the contained element has the given capability.

source

pub fn with<C>(&self) -> Option<&C>
where C: ?Sized + 'static,

Cast to a trait object if the contained element has the given capability.

source

pub fn with_mut<C>(&mut self) -> Option<&mut C>
where C: ?Sized + 'static,

Cast to a mutable trait object if the contained element has the given capability.

source

pub fn is_empty(&self) -> bool

Whether the content is an empty sequence.

source

pub fn sequence_recursive_for_each<'a>(&'a self, f: &mut impl FnMut(&'a Self))

Also auto expands sequence of sequences into flat sequence

source

pub fn styled_with_recipe( self, engine: &mut Engine<'_>, context: Tracked<'_, Context<'_>>, recipe: Recipe, ) -> SourceResult<Self>

Style this content with a recipe, eagerly applying it if possible.

source

pub fn repeat(&self, count: usize) -> Self

Repeat this content count times.

source

pub fn styled(self, style: impl Into<Style>) -> Self

Style this content with a style entry.

source

pub fn styled_with_map(self, styles: Styles) -> Self

Style this content with a full style map.

source

pub fn style_in_place(&mut self, styles: Styles)

Style this content with a full style map in-place.

source

pub fn query(&self, selector: Selector) -> Vec<Content>

Queries the content tree for all elements that match the given selector.

Elements produced in show rules will not be included in the results.

source

pub fn query_first(&self, selector: Selector) -> Option<Content>

Queries the content tree for the first element that match the given selector.

Elements produced in show rules will not be included in the results.

source

pub fn plain_text(&self) -> EcoString

Extracts the plain text of this content.

source§

impl Content

source

pub fn strong(self) -> Self

Strongly emphasize this content.

source

pub fn emph(self) -> Self

Emphasize this content.

source

pub fn underlined(self) -> Self

Underline this content.

source

pub fn linked(self, dest: Destination) -> Self

Link the content somewhere.

source

pub fn aligned(self, align: Alignment) -> Self

Set alignments for this content.

source

pub fn padded(self, padding: Sides<Rel<Length>>) -> Self

Pad this content at the sides.

source

pub fn moved(self, delta: Axes<Rel<Length>>) -> Self

Transform this content’s contents without affecting layout.

source§

impl Content

source

pub fn func(&self) -> Element

The content’s element function. This function can be used to create the element contained in this content. It can be used in set and show rules for the element. Can be compared with global functions to check whether you have a specific kind of element.

source

pub fn has(&self, field: Str) -> bool

Whether the content has the specified field.

source

pub fn at(&self, field: Str, default: Option<Value>) -> StrResult<Value>

Access the specified field on the content. Returns the default value if the field does not exist or fails with an error if no default value was specified.

source

pub fn fields(&self) -> Dict

Returns the fields of this content.

#rect(
  width: 10cm,
  height: 10cm,
).fields()
source

pub fn location(&self) -> Option<Location>

The location of the content. This is only available on content returned by [query] or provided by a show rule, for other content it will be {none}. The resulting location can be used with counters, [state] and queries.

Trait Implementations§

source§

impl<'a> Add<&'a Content> for Content

source§

type Output = Content

The resulting type after applying the + operator.
source§

fn add(self, rhs: &'a Self) -> Self::Output

Performs the + operation. Read more
source§

impl Add for Content

source§

type Output = Content

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<&Content> for Content

source§

fn add_assign(&mut self, rhs: &Self)

Performs the += operation. Read more
source§

impl AddAssign for Content

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl Clone for Content

source§

fn clone(&self) -> Content

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Content

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Content

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<Content> for GridCell

source§

fn from(value: Content) -> Self

Converts to this type from the input type.
source§

impl From<Content> for TableCell

source§

fn from(value: Content) -> Self

Converts to this type from the input type.
source§

impl<T: NativeElement> From<T> for Content

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl FromValue for Content

source§

fn from_value(value: Value) -> HintedStrResult<Self>

Try to cast the value into an instance of Self.
source§

impl Hash for Content

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IntoValue for Content

source§

fn into_value(self) -> Value

Cast this type into a value.
source§

impl NativeScope for Content

source§

fn constructor() -> Option<&'static NativeFuncData>

The constructor function for the type, if any.
source§

fn scope() -> Scope

Get the associated scope for the type.
source§

impl NativeType for Content

source§

const NAME: &'static str = "content"

The type’s name. Read more
source§

fn data() -> &'static NativeTypeData

source§

fn ty() -> Type

Get the type for the native Rust type.
source§

impl PartialEq for Content

source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Reflect for Content

source§

fn input() -> CastInfo

Describe what can be cast into this value.
source§

fn output() -> CastInfo

Describe what this value can be cast into.
source§

fn castable(value: &Value) -> bool

Whether the given value can be converted to T. Read more
source§

fn error(found: &Value) -> HintedString

Produce an error message for an unacceptable value type. Read more
source§

impl Repr for Content

source§

fn repr(&self) -> EcoString

Return the debug representation of the value.
source§

impl Serialize for Content

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Sum for Content

source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl TryFrom<Content> for GridChild

source§

type Error = HintedString

The type returned in the event of a conversion error.
source§

fn try_from(value: Content) -> HintedStrResult<Self>

Performs the conversion.
source§

impl TryFrom<Content> for GridItem

source§

type Error = HintedString

The type returned in the event of a conversion error.
source§

fn try_from(value: Content) -> HintedStrResult<Self>

Performs the conversion.
source§

impl TryFrom<Content> for TableChild

source§

type Error = HintedString

The type returned in the event of a conversion error.
source§

fn try_from(value: Content) -> HintedStrResult<Self>

Performs the conversion.
source§

impl TryFrom<Content> for TableItem

source§

type Error = HintedString

The type returned in the event of a conversion error.
source§

fn try_from(value: Content) -> HintedStrResult<Self>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> Filterable for T

source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromAngle<T> for T

source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
source§

impl<T> FromValue<Spanned<Value>> for T
where T: FromValue,

source§

fn from_value(value: Spanned<Value>) -> Result<T, HintedString>

Try to cast the value into an instance of Self.
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> IntoAngle<U> for T
where U: FromAngle<T>,

source§

fn into_angle(self) -> U

Performs a conversion into T.
source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> IntoResult for T
where T: IntoValue,

source§

fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>

Cast this type into a value.
source§

impl<T> IntoStimulus<T> for T

source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,