Skip to content
pixi-silk
GitHub repository
Entry point

pixi-silk/text

SilkText: distance-field text with outline, shadow and gradient fill. SdfAtlas, layoutText, caret helpers.

import { SdfAtlas, SilkText, SilkTextGeometry, caretAtPoint } from 'pixi-silk/text';

Classes#

ClassDescription
SdfAtlasGlyphs of any CSS font, rasterised with Canvas2D and turned into signed distance fields, in one 8-bit texture every SilkText shares.
SilkTextText from pixi-silk/text, drawn from signed distance fields. Glyphs come from any CSS font. They are baked once at one size into a shared SdfAtlas and drawn scaled, with the same per-pixel anti-aliasing as SilkGraphics. Every glyph is an instance of one mesh.
SilkTextGeometryGeometry behind every SilkText: one unit quad plus one instance of 12 floats per glyph. SilkText owns and fills it. You rarely need it directly.

Interfaces#

InterfaceDescription
CaretHitWhere a point lands in a TextLayout: the nearest caret position.
CaretRectA caret position in layout units: a vertical segment from top to bottom at x.
FontMetricsVertical metrics of a font at the atlas size, measured the way PixiJS Text measures them.
FontSpecThe font of a glyph: CSS family list, weight and style. Size does not matter, glyphs are baked at one size.
GlyphA glyph baked into the atlas. Distances are in raster pixels, at SdfAtlas.size.
GlyphContextThe slice of a 2D canvas context the atlas needs. Pass your own to SdfAtlasOptions.context to bake glyphs somewhere else (a worker canvas, or a fake one in tests).
LayoutLineOne line of a laid-out text.
LayoutOptionsWhat layoutText needs besides the text. Sizes are in local units.
PlacedGlyphA character placed by layoutText.
SdfAtlasOptionsOptions of an SdfAtlas.
SilkTextOptionsOptions of a SilkText: the style plus the text, anchor and atlas.
SilkTextStyleThe look of a SilkText. Every field can change later through its setter or SilkText.set.
TextLayoutResult of layoutText.
TextMeasurerWidths of characters at the size being laid out, in local units.
TextOutlineA line drawn around the glyphs, outside their edge.
TextRectA rectangle in layout units, as rangeRects returns them.
TextShadowA soft copy of the glyphs under them: a drop shadow with an offset, a glow without one.
WordSegmentA run of text with a kind, as wordSegments returns them.

Type Aliases#

Type AliasDescription
ResolvedTextStyleA SilkTextStyle with every field set, as SilkText.defaults holds it.
TextAlignHorizontal alignment of the lines of a text block.

Functions#

FunctionDescription
caretAtPointThe caret position nearest to a point, in the layout’s own units (the top left of the text box is 0, 0). Points above the first line or below the last snap to them. isStop filters the offsets a caret may take, for example grapheme cluster boundaries.
caretRectWhere to draw a caret at index, in the layout’s units.
caretXThe x of a caret at index on line li: the nearest caret stop at or before it.
fontStringpixi-silk/text: SilkText, text drawn from a shared signed distance field atlas, plus the layout and caret helpers it uses. Not part of the root pixi-silk import.
graphemeStopsOffsets where a caret can sit: 0, every grapheme cluster boundary, and the text’s length.
isMacTrue on macOS, iOS and iPadOS, where Option moves by word and Command by line.
layoutTextLays out a block of text. Lines break at \n and, with wordWrap, at spaces. Each character gets its advance, kerning and letter spacing, then the lines are aligned. Widths come from measure, so the same layout works for any renderer.
lineOfIndexThe line a caret at index is on. At a wrap, upstream picks the end of the first line over the start of the next.
nextWordStopWhere a word jump to the right lands. mac (the default on Apple platforms) stops at the end of the next word, like Option + Right. Otherwise it stops at the start of the word after, like Ctrl + Right.
paragraphAtThe paragraph around index: from the newline before it to the newline after, newlines left out.
previousWordStopWhere a word jump to the left lands: the start of the word before index.
rangeRectsHighlight boxes of the range from to to, one per line, the full height of each line box. A range that runs on past a newline gets a sliver of newline width at that line’s end, so selected empty lines still show. Default newline width a quarter of the font height.
sdfFromAlphaTurns an anti-aliased coverage bitmap into an 8-bit signed distance field.
silkLabelA label factory that makes SilkText labels, for widgets such as ValuePill. The widget’s style comes first, then overrides (any SilkText option but the text). From the Pixi style it keeps the font, letter spacing, line height, left, center or right alignment, and a plain colour fill. Gradients, strokes and drop shadows of the Pixi style are dropped.
wordAtThe segment (word, run of spaces or punctuation) that holds the character at index.
wordSegmentsSplits text into words and the runs between them.