pub struct TextSpan { /* private fields */ }Expand description
A rich-text tree: each node carries a fragment of text, partial style
overrides — whatever is unspecified inherits from the parent, so a
.bold() child keeps its parent’s size — and children of its own.
let span = TextSpan::new("Hello ")
.child(TextSpan::new("bold").bold())
.child(TextSpan::new(" world"));
let runs = span.flatten(TextStyle::new(20.0));
assert_eq!(runs.len(), 3);
assert_eq!(runs[1].1.size, 20.0); // the bold child inherits the sizeImplementations§
Source§impl TextSpan
impl TextSpan
Sourcepub fn new(text: impl Into<String>) -> TextSpan
pub fn new(text: impl Into<String>) -> TextSpan
A text fragment with no overrides — it inherits everything.
Sourcepub fn weight(self, weight: FontWeight) -> TextSpan
pub fn weight(self, weight: FontWeight) -> TextSpan
Sets this subtree’s weight.
Sourcepub fn underline(self) -> TextSpan
pub fn underline(self) -> TextSpan
Underlines this subtree. It combines with the decoration already set on this node, but not with the parent’s.
Sourcepub fn strikethrough(self) -> TextSpan
pub fn strikethrough(self) -> TextSpan
Strikes through this subtree.
Sourcepub fn decoration(self, decoration: TextDecoration) -> TextSpan
pub fn decoration(self, decoration: TextDecoration) -> TextSpan
Sets this subtree’s decoration lines.
Sourcepub fn decoration_color(self, color: Color) -> TextSpan
pub fn decoration_color(self, color: Color) -> TextSpan
Sets this subtree’s decoration colour.
Sourcepub fn style(self, style: TextStyle) -> TextSpan
pub fn style(self, style: TextStyle) -> TextSpan
Applies a whole TextStyle as this subtree’s override, replacing whatever
this span had said before.
It used to force every typographic field to answered, because a TextStyle could
not say otherwise. It no longer has to: a style naming only a size now overrides
only the size, and the weight goes on being inherited from the parent span.
Sourcepub fn child(self, child: TextSpan) -> TextSpan
pub fn child(self, child: TextSpan) -> TextSpan
Adds a child, rendered after this node’s own text.
Sourcepub fn flatten(&self, base: TextStyle) -> Vec<(String, ResolvedTextStyle)>
pub fn flatten(&self, base: TextStyle) -> Vec<(String, ResolvedTextStyle)>
Flattens the tree into resolved runs (text, style), in reading order,
cascading the overrides down from base, the paragraph’s default style.
Nodes with no text of their own produce no empty run.
Sourcepub fn measure_hash<H>(&self, hasher: &mut H)where
H: Hasher,
pub fn measure_hash<H>(&self, hasher: &mut H)where
H: Hasher,
Mixes into hasher everything that affects the tree’s measurement:
text, sizes, weights and italics. Colours and decorations are excluded,
having no effect on geometry. This serves as the measurement fingerprint
(measure_key) without having to flatten the tree.