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>) -> Self
pub fn new(text: impl Into<String>) -> Self
A text fragment with no overrides — it inherits everything.
Sourcepub fn weight(self, weight: FontWeight) -> Self
pub fn weight(self, weight: FontWeight) -> Self
Sets this subtree’s weight.
Sourcepub fn underline(self) -> Self
pub fn underline(self) -> Self
Underlines this subtree. It combines with the decoration already set on this node, but not with the parent’s.
Sourcepub fn strikethrough(self) -> Self
pub fn strikethrough(self) -> Self
Strikes through this subtree.
Sourcepub fn decoration(self, decoration: TextDecoration) -> Self
pub fn decoration(self, decoration: TextDecoration) -> Self
Sets this subtree’s decoration lines.
Sourcepub fn decoration_color(self, color: Color) -> Self
pub fn decoration_color(self, color: Color) -> Self
Sets this subtree’s decoration colour.
Sourcepub fn style(self, style: TextStyle) -> Self
pub fn style(self, style: TextStyle) -> Self
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 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: Hasher>(&self, hasher: &mut H)
pub fn measure_hash<H: Hasher>(&self, hasher: &mut H)
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.