Skip to main content

DocumentSnapshot Class

Definition​

Assembly:Avalonia.Controls.Documents
Package:Avalonia.Controls.Documents

Immutable snapshot of document state for background serialization. Created on UI thread, safe to read from any thread.

public class DocumentSnapshot

Inheritance: object -> DocumentSnapshot

Remarks​

DocumentSnapshot captures a point-in-time view of the document including:

  • Text content (via immutable RopeNode reference)
  • Tree structure (copied as SnapshotNode tree)
  • Formatting (captured as value types)

Thread-safety is achieved through snapshot isolation:

  • Text storage uses immutable functional rope nodes
  • Structure is copied at snapshot time
  • Formatting is stored as value types (no shared references)

Typical use:

// On UI thread:
var snapshot = document.CreateSnapshot();

// On background thread:
await Task.Run(() => serializer.Serialize(snapshot, stream));

Methods​

NameDescription
CaptureBlockFormattingCaptures the block-level formatting properties (alignment, margins, padding, etc.) from a Avalonia.Controls.Documents.Block into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value.
CaptureInlineFormattingCaptures the inline-level formatting properties (text decorations, baseline alignment) from a Avalonia.Controls.Documents.RichTextElement into an immutable Avalonia.Controls.Documents.TextModel.Formatting.InlineFormatting value.
CaptureListItemFormattingCaptures the block-level formatting properties from a Avalonia.Controls.Documents.ListItem into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value. Avalonia.Controls.Documents.ListItem is not a Avalonia.Controls.Documents.Block, so it needs its own capture even though it declares the same properties.
CaptureTableCellFormattingCaptures the block-level formatting properties from a Avalonia.Controls.Documents.TableCell into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value.
CaptureTableRowFormattingCaptures the block-level formatting of a table row (its minimum height).
CaptureTextElementFormattingCaptures the text-level formatting properties (font, foreground, background, etc.) from a Avalonia.Controls.Documents.RichTextElement into an immutable Avalonia.Controls.Documents.TextModel.Formatting.TextElementFormatting value.
EnumerateNodes (2 overloads)Enumerates all nodes in document order (pre-order traversal).
EnumerateParagraphsEnumerates all paragraphs in document order.
EnumerateTextRunsEnumerates all text runs in document order.
FindFootnoteThe note paired with noteId, or null.
GetFootnoteOrdinalThe number the anchor of noteId shows: the 1-based position of its note among Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.Footnotes, which are in anchor order; 0 when no note carries the id.
GetText (2 overloads)Gets text for a node's range as a string. Prefer Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.GetTextMemory(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode) or Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode,System.IO.TextWriter) to avoid allocation.
GetTextMemory (2 overloads)Gets a zero-copy memory chunk for a snapshot node's text range. The returned memory may be shorter than the node's full length when the text spans multiple rope leaves. Use Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode,System.IO.TextWriter) or iterate with Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.GetTextMemory(int,int) for complete text.
IndexOfPageBandThe position of the band with id in Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.PageBands, or -1.
WriteTextTo (4 overloads)Streams the text of a snapshot node to a System.IO.TextWriter in rope-leaf-sized chunks. No intermediate string or char[] allocation.

CaptureBlockFormatting Method​

Captures the block-level formatting properties (alignment, margins, padding, etc.) from a Avalonia.Controls.Documents.Block into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value.

public Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting CaptureBlockFormatting(Avalonia.Controls.Documents.Block block)

Parameters​

block Avalonia.Controls.Documents.Block

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting

CaptureInlineFormatting Method​

Captures the inline-level formatting properties (text decorations, baseline alignment) from a Avalonia.Controls.Documents.RichTextElement into an immutable Avalonia.Controls.Documents.TextModel.Formatting.InlineFormatting value.

public Avalonia.Controls.Documents.TextModel.Formatting.InlineFormatting CaptureInlineFormatting(Avalonia.Controls.Documents.RichTextElement element)

Parameters​

element Avalonia.Controls.Documents.RichTextElement

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.InlineFormatting

CaptureListItemFormatting Method​

Captures the block-level formatting properties from a Avalonia.Controls.Documents.ListItem into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value. Avalonia.Controls.Documents.ListItem is not a Avalonia.Controls.Documents.Block, so it needs its own capture even though it declares the same properties.

public Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting CaptureListItemFormatting(Avalonia.Controls.Documents.ListItem item)

Parameters​

item Avalonia.Controls.Documents.ListItem

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting

CaptureTableCellFormatting Method​

Captures the block-level formatting properties from a Avalonia.Controls.Documents.TableCell into an immutable Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting value.

public Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting CaptureTableCellFormatting(Avalonia.Controls.Documents.TableCell cell)

Parameters​

cell Avalonia.Controls.Documents.TableCell

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting

CaptureTableRowFormatting Method​

Captures the block-level formatting of a table row (its minimum height).

public Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting CaptureTableRowFormatting(Avalonia.Controls.Documents.TableRow row)

Parameters​

row Avalonia.Controls.Documents.TableRow

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.BlockFormatting

CaptureTextElementFormatting Method​

Captures the text-level formatting properties (font, foreground, background, etc.) from a Avalonia.Controls.Documents.RichTextElement into an immutable Avalonia.Controls.Documents.TextModel.Formatting.TextElementFormatting value.

public Avalonia.Controls.Documents.TextModel.Formatting.TextElementFormatting CaptureTextElementFormatting(Avalonia.Controls.Documents.RichTextElement element)

Parameters​

element Avalonia.Controls.Documents.RichTextElement

Returns​

Avalonia.Controls.Documents.TextModel.Formatting.TextElementFormatting

EnumerateNodes overloads​

EnumerateNodes Method​

Enumerates all nodes in document order (pre-order traversal).

public System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode> EnumerateNodes()
Returns​

System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode>

EnumerateNodes Method​

public System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode> EnumerateNodes(Func<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode, bool> predicate)
Parameters​

predicate Func<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode, bool>

Returns​

System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode>

EnumerateParagraphs Method​

Enumerates all paragraphs in document order.

public System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode> EnumerateParagraphs()

Returns​

System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode>

EnumerateTextRuns Method​

Enumerates all text runs in document order.

public System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode> EnumerateTextRuns()

Returns​

System.Collections.Generic.IEnumerable<Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode>

FindFootnote Method​

The note paired with noteId, or null.

public Avalonia.Controls.Documents.Serialization.Snapshot.FootnoteSnapshot FindFootnote(int noteId)

Parameters​

noteId int

Returns​

Avalonia.Controls.Documents.Serialization.Snapshot.FootnoteSnapshot

GetFootnoteOrdinal Method​

The number the anchor of noteId shows: the 1-based position of its note among Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.Footnotes, which are in anchor order; 0 when no note carries the id.

public int GetFootnoteOrdinal(int noteId)

Parameters​

noteId int

Returns​

int

GetText overloads​

GetText Method​

Gets text for a node's range as a string. Prefer Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.GetTextMemory(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode) or Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode,System.IO.TextWriter) to avoid allocation.

public string GetText(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode node)
Parameters​

node Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode

Returns​

string

GetText Method​

Gets text for a range as a string. Prefer Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.GetTextMemory(int,int) or Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.WriteTextTo(int,int,System.IO.TextWriter) to avoid allocation.

public string GetText(int offset, int length)
Parameters​

offset int

length int

Returns​

string

GetTextMemory overloads​

GetTextMemory Method​

Gets a zero-copy memory chunk for a snapshot node's text range. The returned memory may be shorter than the node's full length when the text spans multiple rope leaves. Use Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode,System.IO.TextWriter) or iterate with Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.GetTextMemory(int,int) for complete text.

public ReadOnlyMemory<char> GetTextMemory(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode node)
Parameters​

node Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode

The snapshot node.

Returns​

ReadOnlyMemory<char>

A slice of the underlying rope leaf. No allocation.

GetTextMemory Method​

Gets a zero-copy memory chunk starting at offset. Returns up to maxLength characters from a single rope leaf. The returned memory may be shorter than maxLength if the leaf boundary is reached; callers should advance the offset and call again to read subsequent chunks.

public ReadOnlyMemory<char> GetTextMemory(int offset, int maxLength)
Parameters​

offset int

Start offset in the document.

maxLength int

Maximum number of characters to return.

Returns​

ReadOnlyMemory<char>

A slice of the underlying rope leaf memory. No allocation.

IndexOfPageBand Method​

The position of the band with id in Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot.PageBands, or -1.

public int IndexOfPageBand(Guid id)

Parameters​

id Guid

Returns​

int

WriteTextTo overloads​

WriteTextTo Method​

Streams the text of a snapshot node to a System.IO.TextWriter in rope-leaf-sized chunks. No intermediate string or char[] allocation.

public void WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode node, System.IO.TextWriter writer)
Parameters​

node Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode

writer System.IO.TextWriter

WriteTextTo Method​

Appends the text of a snapshot node to a System.Text.StringBuilder in rope-leaf-sized chunks. No intermediate string allocation.

public void WriteTextTo(Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode node, System.Text.StringBuilder sb)
Parameters​

node Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode

sb System.Text.StringBuilder

WriteTextTo Method​

Streams the text of a range to a System.IO.TextWriter in rope-leaf-sized chunks. No intermediate string or char[] allocation.

public void WriteTextTo(int offset, int length, System.IO.TextWriter writer)
Parameters​

offset int

length int

writer System.IO.TextWriter

WriteTextTo Method​

Appends the text of a range to a System.Text.StringBuilder in rope-leaf-sized chunks. No intermediate string allocation.

public void WriteTextTo(int offset, int length, System.Text.StringBuilder sb)
Parameters​

offset int

length int

sb System.Text.StringBuilder

Properties​

NameDescription
DocumentFormattingDocument-level formatting (page margins, etc.).
FootnotesThe footnotes the document owns (nested document snapshots) in anchor order, paired by id with the Avalonia.Controls.Documents.Serialization.Snapshot.FootnoteReferenceSnapshotNode anchors in the body. A range snapshot carries the notes of the anchors inside the range.
GenerationDocument generation at snapshot time. Can be used to detect changes.
PageBandsThe page bands the document owns (nested document snapshots), placed by their role and rule, or referenced by index from the sections' Avalonia.Controls.Documents.Serialization.Snapshot.SectionSnapshotNode.PageBandReferences. Empty for range snapshots and for snapshots taken at the text-model level.
RootThe root of the snapshot tree.
TextLengthTotal text length of the document at snapshot time.

DocumentFormatting Property​

Document-level formatting (page margins, etc.).

public Avalonia.Controls.Documents.TextModel.Formatting.DocumentFormatting DocumentFormatting { get; set; }

Footnotes Property​

The footnotes the document owns (nested document snapshots) in anchor order, paired by id with the Avalonia.Controls.Documents.Serialization.Snapshot.FootnoteReferenceSnapshotNode anchors in the body. A range snapshot carries the notes of the anchors inside the range.

public System.Collections.Generic.IReadOnlyList<Avalonia.Controls.Documents.Serialization.Snapshot.FootnoteSnapshot> Footnotes { get; set; }

Generation Property​

Document generation at snapshot time. Can be used to detect changes.

public long Generation { get; set; }

PageBands Property​

The page bands the document owns (nested document snapshots), placed by their role and rule, or referenced by index from the sections' Avalonia.Controls.Documents.Serialization.Snapshot.SectionSnapshotNode.PageBandReferences. Empty for range snapshots and for snapshots taken at the text-model level.

public System.Collections.Generic.IReadOnlyList<Avalonia.Controls.Documents.Serialization.Snapshot.PageBandSnapshot> PageBands { get; set; }

Root Property​

The root of the snapshot tree.

public Avalonia.Controls.Documents.Serialization.Snapshot.SnapshotNode Root { get; set; }

TextLength Property​

Total text length of the document at snapshot time.

public int TextLength { get; set; }