Skip to main content

Content

Struct Content 

Source
pub struct Content {
    pub text: String,
    pub lines: Vec<Line>,
    pub marks: Vec<Mark>,
    pub islands: Vec<Island>,
}
Expand description

The canonical content model, the pre-built value the document mutators accept, and the canonical-form token every content read answers in. One content field as a content: the text plus the structure that rides on it.

The mint (Content::into_normalized) establishes the canonical form: marks sorted and unioned, container paths renumbered, a line’s kind agreeing with its text, a block island’s slot alone on its line, table props on one column count. Content::validate reports what the mint cannot repair: the text holds no \r, no bidi controls, and no line separator (every character Typst reads as a newline but \n); the count of ISLAND_SLOT equals islands.len(); lines.len() equals the number of \n-separated segments.

Fields§

§text: String

The content. \n is a line boundary; ISLAND_SLOT is an island slot.

§lines: Vec<Line>

One entry per \n-separated segment of text, in order. The line tree is derived from this flat list plus each line’s containers path, never stored, so a split/join is a single-char edit with no paragraph identity to reconcile.

§marks: Vec<Mark>

Marks over char ranges, kept normalized: sorted by (start, end, type, attrs), same-kind formatting marks unioned.

§islands: Vec<Island>

One entry per ISLAND_SLOT, in slot order (ascending char position).

Implementations§

Source§

impl Content

Source

pub fn new(text: String, lines: Vec<Line>) -> Content

The text and its per-line attributes; marks and islands start empty.

Constructing neither normalizes nor checks: the canonical form is the caller’s until into_normalized runs, and validate reports what that cannot repair. The codecs (crate::import, Content::from_canonical_json) do both.

Source

pub fn into_normalized(self) -> Normalized

Normalize and seal. With Normalized::empty, the only mint for Normalized; the codecs decode through here.

Source

pub fn with_marks(self, marks: Vec<Mark>) -> Content

Source

pub fn with_islands(self, islands: Vec<Island>) -> Content

Set the islands, one per ISLAND_SLOT in slot order.

Source

pub fn empty() -> Content

An empty content: one empty Para line, no marks, no islands.

Source

pub fn len_usv(&self) -> usize

Total length in USV.

Source

pub fn is_inline(&self) -> bool

Whether this content satisfies the richtext(inline) constraint: exactly one Para line, sitting in no container, with no islands. Content::empty is inline, so a blank inline field passes.

Source

pub fn is_plain(&self) -> bool

Whether this content satisfies the plaintext constraint: no marks, no islands, and every line a plain Para sitting in no container. continues is unconstrained. Content::empty is plain.

The distinguishing property of plaintext over richtext { marks: [] } is the literal codec (crate::import::from_plaintext), not this predicate.

Source

pub fn is_blank(&self) -> bool

Whether the text is empty or whitespace-only. An ISLAND_SLOT is not whitespace, so an island-bearing content is never blank.

Source

pub fn segment_count(&self) -> usize

Number of \n-separated segments: the required lines.len().

Source

pub fn normalize(&mut self)

Normalize in place: canonicalize container ordinal/instance, break a line around a block-only island’s slot, drop zero-width formatting, union same-kind formatting that is adjacent or overlapping, recursively key-sort island props, then sort marks canonically. Idempotent: the fixed point the canonical serialization commits to.

Source

pub fn validate(&self) -> Result<(), Invariant>

What the mint cannot repair. Ok(()) on every content a codec or an accepted op hands out; a hand-built one can fail it.

Source§

impl Content

Source

pub fn apply_text_delta(&mut self, delta: &Delta) -> Result<(), ApplyError>

Splice text via delta, rebase marks, sync lines to \n changes, cascade island removal for any deleted slot, then normalize.

Islands stay in lockstep with their ISLAND_SLOT chars: a delta that deletes a slot drops the corresponding Island; a delta that inserts a raw slot is rejected (ApplyError::IslandSlotInInsert).

Inserted text is sanitized first, mirroring what import applies at the string boundary: \r and Unicode bidi controls are stripped, and a line separator (VT, FF, NEL, U+2028, U+2029) becomes a space — the chars Content::validate forbids.

Source

pub fn apply_mark_ops(&mut self, ops: &[MarkOp]) -> Result<(), ApplyError>

Apply mark ops in final-text coordinates, then normalize.

Source

pub fn apply_island_ops(&mut self, ops: &[IslandOp]) -> Result<(), ApplyError>

Apply island ops: replace an entry by id, or insert a slot and its entry together.

Source

pub fn apply_line_ops(&mut self, ops: &[LineOp]) -> Result<(), ApplyError>

Apply line ops: split/join splice \n; set ops touch metadata only.

Source

pub fn apply_field_change( &mut self, bundle: &ChangeBundle, ) -> Result<(), ApplyError>

One committed field edit bundle: text delta, then island ops, then line ops, then marks, canonicalized by a single terminal normalize.

All-or-nothing: on any op’s error self is left exactly as it was. A bundle carrying ops stages on a scratch copy and swaps in only once every stage succeeds; the pure-text-delta path skips that clone, since apply_text_delta validates before mutating.

Stage order is a coordinate contract: each stage reads the text the earlier ones left. An island insert splices a slot, so a LineOp::SetKind { kind: Island } in the same bundle settles against a line that already carries it, and Split/Join and every mark range are then measured in a frame that includes the new slots.

One terminal normalize suffices because split/join rebase marks through their \n splice, so the formatting-edge \n-trim commutes with the line ops, and MarkOp::Remove is coverage-set subtraction, which commutes with normalize’s same-kind union ((A ∪ B) \ R = (A\R) ∪ (B\R)).

Source

pub fn map_marks(&self, bundle: &ChangeBundle) -> Result<Vec<Mark>, ApplyError>

Where bundle’s text-moving channels leave the marks this content already holds: the final-text coordinates ChangeBundle::mark_ops are written in, under the rebase rule stated on ChangeBundle.

bundle.mark_ops are ignored, so an editor building them diffs its intended marks against this instead of predicting the rebase. The answer is normalized, as the store’s is: marks a text move drops (out of range, zero-width formatting) are absent, and same-kind runs a move left adjacent arrive already unioned. A bundle whose mark_ops are empty therefore names the marks the field will hold.

Errors are apply_field_change’s on the same ops, minus those only a mark op raises.

Source§

impl Content

Source

pub fn from_canonical_json(s: &str) -> Result<Normalized, ParseError>

Parse canonical JSON, normalize, and validate. Returns ParseError::Invalid for a content that violates its invariants, so storage cannot silently round-trip a malformed value.

Trait Implementations§

Source§

impl Clone for Content

Source§

fn clone(&self) -> Content

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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<(), Error>

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

impl From<Content> for Normalized

Source§

fn from(rt: Content) -> Normalized

Converts to this type from the input type.
Source§

impl PartialEq for Content

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Content

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, C> ComponentsInto<C> for T

Source§

fn components_into(self) -> C

Cast this collection of color components into a collection of colors. Read more
Source§

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

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
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, 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> 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, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_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<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.