loom & weft
Made for Swift.

Published release 0.2.1. Unreleased PR #2 reference.

LOOM 0.2.1 / TEXT, MARKDOWN, AND EDITING

TextLayout

Caches sanitized source and display-cell geometry. Width is at least one cell. Newlines and tabs remain in copied source; controls are filtered. Tabs expand by TextWrapOptions.tabWidth cells (default four), not terminal tab stops. Wrapping and paint/hit/copy paths share whole-grapheme geometry. Rebuild after text/width changes.

Standard inherited Swift protocol operations such as synthesized equality and OptionSet set operations are not repeated. The public declarations below are extracted from the exact source revision listed in the coverage manifest.

Example

let layout = TextLayout("Hello 世界", width: 12, wrapping: .word)
let selection = TextSelection(anchor: Point(x: 0, y: 0), head: Point(x: 4, y: 0))
_ = layout.selectedText(selection)
_ = layout.lines[0].glyphs
Members on this page

Feature guides and release status

TextLayout

Structure
struct TextLayout

Conforms to: Swift.Sendable, Swift.SendableMetatype.

The single source of geometry for painting, hit testing, highlighting, and copying.

Construct once when text or available width changes. Mouse movement never lays out text.

Source · TextLayout.swift:30

Glyph

Structure
struct Glyph

Conforms to: Swift.Sendable, Swift.SendableMetatype.

One laid-out extended grapheme, including its cell position, width, style, link, and source String.Index range. Obtain these values from a layout’s lines; there is no public initializer.

Source · TextLayout.swift:31

Glyph.text

Instance Property
let text: String

The stored text content.

Source · TextLayout.swift:32

Glyph.column

Instance Property
let column: Int

The glyph’s starting cell column within its row.

Source · TextLayout.swift:33

Glyph.width

Instance Property
let width: Int

Cell width of this laid-out grapheme, normally 1 or 2.

Source · TextLayout.swift:34

Glyph.sourceRange

Instance Property
let sourceRange: Range<String.Index>

String indices into the parent TextLayout.source.

Source · TextLayout.swift:35

Glyph.style

Instance Property
let style: Style?

Visual attributes used when painting this value.

Source · TextLayout.swift:36

Glyph.patch

Instance Property
let patch: StylePatch?

Changes resolved over the explicit glyph style or the inherited paint style.

Source · TextLayout.swift:38

Glyph.copyable

Instance Property
let copyable: Bool

Whether this content participates in copied selections.

Source · TextLayout.swift:40

Line

Structure
struct Line

Conforms to: Swift.Sendable, Swift.SendableMetatype.

One cached display row, including laid-out glyphs and code-block metadata. Obtain rows from TextLayout.lines; construction and source geometry are managed by the layout.

Source · TextLayout.swift:56

Line.glyphs

Instance Property
var glyphs: [Glyph]

Laid-out graphemes for this display row.

Source · TextLayout.swift:57

Line.codeBlock

Instance Property
var codeBlock: TextCodeBlock? { get }

Original code and panel styling for this laid-out row, including soft wraps.

Source · TextLayout.swift:59

Line.isCodeBlockHeader

Instance Property
var isCodeBlockHeader: Bool { get }

Whether this span/row is a code-block header.

Source · TextLayout.swift:60

Line.codeBlockStyle

Instance Property
var codeBlockStyle: Style? { get }

Optional style for the code panel represented by this row.

Source · TextLayout.swift:61

source

Instance Property
let source: String

The sanitized source string used by this layout’s source indices.

Source · TextLayout.swift:70

width

Instance Property
let width: Int

Width in terminal cells.

Source · TextLayout.swift:71

lines

Instance Property
let lines: [Line]

Cached display rows.

Source · TextLayout.swift:72

paddedVertically(_:)

Instance Method
func paddedVertically(_ rows: Int) -> TextLayout

Adds presentation rows without adding characters to the selectable source.

Parameters (1)
rows
Row count or signed row movement, according to the method.

Returns TextLayout.

Source · TextLayout.swift:76

tailPreview(rows:reservedHeaderRows:omissionStyle:)

Instance Method
func tailPreview(rows: Int, reservedHeaderRows: Int = 0, omissionStyle: Style? = nil) -> TextLayout

Keeps the last wrapped rows while retaining source indices for copying.

Reserved header rows and the omission marker are presentation only.

Parameters (3)
rows
Row count or signed row movement, according to the method.
reservedHeaderRows
Argument type and any default are shown in the declaration; see the behavior notes above.
omissionStyle
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns TextLayout.

Source · TextLayout.swift:86

headPreview(rows:verticalPadding:)

Instance Method
func headPreview(rows: Int, verticalPadding: Int = 0) -> TextLayout

Keeps the first wrapped content rows and any vertical padding, with a display-only omission mark.

Parameters (2)
rows
Row count or signed row movement, according to the method.
verticalPadding
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns TextLayout.

Source · TextLayout.swift:105

init(_:width:wrapping:options:measure:)

Initializer
init(_ text: String, width: Int, wrapping: TextWrapping = .grapheme, options: TextWrapOptions = TextWrapOptions(), measure: (Character) -> Int = terminalCellWidth)

Creates a value with the supplied arguments. Default arguments are shown in the declaration.

Parameters (5)
text
Source text.
width
Available columns in terminal cells.
wrapping
Grapheme or word wrapping policy.
options
Terminal policies selected when opening.
measure
Returns terminal-cell width for one Character.
Source · TextLayout.swift:136

init(_:width:wrapping:continuationPrefix:options:measure:)

Initializer
init(_ spans: [TextSpan], width: Int, wrapping: TextWrapping = .grapheme, continuationPrefix: TextSpan? = nil, options: TextWrapOptions = TextWrapOptions(), measure: (Character) -> Int = terminalCellWidth)

Lays out styled spans as one source string.

A grapheme crossing a span boundary uses the style and patch of its first scalar, keeping selection intact. An explicit continuation prefix overrides repeated display-only leading glyphs on soft wraps, without entering copied text.

Parameters (6)
spans
Styled source spans, in order.
width
Available columns in terminal cells.
wrapping
Grapheme or word wrapping policy.
continuationPrefix
Argument type and any default are shown in the declaration; see the behavior notes above.
options
Terminal policies selected when opening.
measure
Returns terminal-cell width for one Character.
Source · TextLayout.swift:148

sourceRange(for:)

Instance Method
func sourceRange(for selection: TextSelection) -> Range<String.Index>?

Returns the source String.Index range covered by a display selection, or nil when no source grapheme is selected. Use indices only with this layout’s source.

Parameters (1)
for
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Range<String.Index>?.

Source · TextLayout.swift:327

selectedText(_:)

Instance Method
func selectedText(_ selection: TextSelection) -> String

Copies sanitized source covered by the inclusive display selection. Excludes non-copyable presentation spans and includes source newlines/tabs rather than soft-wrap separators.

Parameters (1)
selection
Selection using the containing type’s coordinate convention.

Returns String.

Source · TextLayout.swift:350

wordSelection(at:)

Instance Method
func wordSelection(at point: Point) -> TextSelection?

Selects the word or punctuation run under a laid-out cell, across soft wraps.

Parameters (1)
at
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns TextSelection?.

Source · TextLayout.swift:366

paragraphSelection(at:)

Instance Method
func paragraphSelection(at point: Point) -> TextSelection?

Selects the source line under a cell, including any soft-wrapped display rows.

Parameters (1)
at
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns TextSelection?.

Source · TextLayout.swift:386

horizontalOrigin(forRow:viewportWidth:alignment:)

Instance Method
func horizontalOrigin(forRow row: Int, viewportWidth: Int, alignment: CellAlignment = .left) -> Int

The content-to-viewport shift for a row.

Overwide lines stay start aligned.

Parameters (3)
forRow
Argument type and any default are shown in the declaration; see the behavior notes above.
viewportWidth
Argument type and any default are shown in the declaration; see the behavior notes above.
alignment
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Int.

Source · TextLayout.swift:425

contentPoint(at:in:rowOffset:columnOffset:alignment:)

Instance Method
func contentPoint(at point: Point, in area: Rect, rowOffset: Int = 0, columnOffset: Int = 0, alignment: CellAlignment = .left) -> Point?

Converts an absolute viewport cell to the logical content coordinates used by selections.

Blank alignment padding and cells without a copyable glyph return nil.

Parameters (5)
at
Argument type and any default are shown in the declaration; see the behavior notes above.
in
Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset
Number of cached layout rows skipped before painting.
columnOffset
Argument type and any default are shown in the declaration; see the behavior notes above.
alignment
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Point?.

Source · TextLayout.swift:439

viewportPoint(for:in:rowOffset:columnOffset:alignment:)

Instance Method
func viewportPoint(for point: Point, in area: Rect, rowOffset: Int = 0, columnOffset: Int = 0, alignment: CellAlignment = .left) -> Point?

Converts logical content coordinates to absolute viewport coordinates.

Parameters (5)
for
Argument type and any default are shown in the declaration; see the behavior notes above.
in
Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset
Number of cached layout rows skipped before painting.
columnOffset
Argument type and any default are shown in the declaration; see the behavior notes above.
alignment
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Point?.

Source · TextLayout.swift:463

paint(in:rowOffset:selection:style:columnOffset:alignment:buffer:)

Instance Method
func paint(in area: Rect, rowOffset: Int = 0, selection: TextSelection? = nil, style: Style = Style(), columnOffset: Int = 0, alignment: CellAlignment = .left, buffer: inout Buffer)

Paints cached glyphs with shared horizontal alignment, scrolling, clipping, and source selection geometry.

Parameters (7)
in
Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset
Number of cached layout rows skipped before painting.
selection
Selection using the containing type’s coordinate convention.
style
Text/cell appearance.
columnOffset
Argument type and any default are shown in the declaration; see the behavior notes above.
alignment
Argument type and any default are shown in the declaration; see the behavior notes above.
buffer
Mutable destination cell grid.
Source · TextLayout.swift:483