pub struct RichSpan {
pub text: String,
pub style: TextStyle,
}Expand description
A span of rich text (a styled text fragment).
Rich text is formed by concatenating RichSpans in order, where each span may carry
its own style: font family / size / weight / style / stretch / color / line height /
letter spacing / underline / strikethrough (corresponding to pango’s
family / size / weight / style / stretch / foreground / underline /
strikethrough / letter_spacing). A \n inside a span’s text is treated as a line break
by parley (consistent with the layout semantics of TextStyle).
Usage pattern (layout / measurement both go through the span-list entry point):
use lievisual::geometry::Color;
use lievisual::text::{FontWeight, RichSpan, TextStyle, measure_text};
let spans = vec![
RichSpan::new("Hello ", TextStyle::new(Color::BLACK, 20.0, "sans-serif")),
// mixed styling: bold + underline + strikethrough + letter spacing
RichSpan::new("World", TextStyle::new(Color::rgb(0, 0, 0xff), 20.0, "sans-serif")
.with_weight(FontWeight::Bold)
.with_underline(true)
.with_strikethrough(true)
.with_letter_spacing(2.0)),
];
let m = measure_text(&spans, None);
assert!(m.size.width > 0.0);Fields§
§text: StringThe span’s text (may contain \n).
style: TextStyleThe style applied to this span.
Implementations§
Source§impl RichSpan
impl RichSpan
Sourcepub fn new(text: impl Into<String>, style: TextStyle) -> Self
pub fn new(text: impl Into<String>, style: TextStyle) -> Self
Construct a styled span.
Examples found in repository?
examples/demo.rs (lines 211-214)
21fn build_scene() -> Scene {
22 let mut scene = Scene::new(560.0, 400.0)
23 .with_background(Color::WHITE)
24 .with_title("lievisual 丰富 IR 演示")
25 .with_description("展示图层 + 椭圆/圆角矩形/多边形/圆弧/扇形、径向渐变、虚线、透明度与命名")
26 .with_scale(2.0);
27
28 // ---------- 默认层(nodes):网格虚线 ----------
29 let dashed = Stroke::dashed(Color::rgb(0xbb, 0xbb, 0xbb), 1.0, vec![6.0, 4.0]);
30 scene.push(Element::poly(
31 vec![
32 Point::new(20.0, 260.0),
33 Point::new(120.0, 220.0),
34 Point::new(220.0, 260.0),
35 Point::new(320.0, 220.0),
36 ],
37 dashed,
38 ));
39 scene.push(Element::line(
40 Point::new(20.0, 300.0),
41 Point::new(540.0, 300.0),
42 Stroke::new(Color::rgb(0xdd, 0xdd, 0xdd), 1.0),
43 ));
44
45 // ---------- shapes 层 ----------
46 let linear = LinearGradient::new(
47 Point::new(0.0, 0.0),
48 Point::new(200.0, 200.0),
49 vec![
50 GradientStop {
51 offset: 0.0,
52 color: Color::rgb(0x00, 0x66, 0xcc),
53 },
54 GradientStop {
55 offset: 1.0,
56 color: Color::rgb(0xcc, 0x00, 0x66),
57 },
58 ],
59 );
60 let mut shapes = Layer::new("shapes").with_opacity(0.95);
61
62 // 圆(描边)
63 shapes.push(
64 SceneNode::new(Element::circle(
65 Point::new(100.0, 100.0),
66 60.0,
67 FillStrokeStyle::stroke(Color::BLACK, 2.0),
68 ))
69 .with_name("circle-outline"),
70 );
71 // 椭圆(线性渐变填充 + 旋转)
72 shapes.push(
73 SceneNode::new(Element::ellipse(
74 Point::new(180.0, 80.0),
75 Vec2::new(55.0, 28.0),
76 0.5,
77 FillStrokeStyle {
78 fill: Some(linear.clone().into()),
79 stroke: Some(Stroke::new(Color::rgb(0x22, 0x22, 0x22), 1.5)),
80 },
81 ))
82 .with_name("ellipse-rotated"),
83 );
84 // 圆角矩形(径向渐变填充)
85 let radial = RadialGradient::new(
86 Point::new(300.0, 70.0),
87 60.0,
88 vec![
89 GradientStop {
90 offset: 0.0,
91 color: Color::rgb(0xff, 0xff, 0xff),
92 },
93 GradientStop {
94 offset: 1.0,
95 color: Color::rgb(0xee, 0x99, 0x00),
96 },
97 ],
98 );
99 shapes.push(
100 SceneNode::new(Element::rounded_rect(
101 lievisual::Rect::new(270.0, 30.0, 120.0, 70.0),
102 14.0,
103 FillStrokeStyle {
104 fill: Some(radial.into()),
105 stroke: Some(Stroke::new(Color::rgb(0x33, 0x33, 0x33), 1.0)),
106 },
107 ))
108 .with_name("rounded-card"),
109 );
110 // 多边形
111 shapes.push(SceneNode::new(Element::polygon(
112 vec![
113 Point::new(400.0, 150.0),
114 Point::new(450.0, 90.0),
115 Point::new(520.0, 120.0),
116 Point::new(500.0, 200.0),
117 Point::new(420.0, 200.0),
118 ],
119 FillStrokeStyle {
120 fill: Some(Color::rgb(0x66, 0xcc, 0x66).into()),
121 stroke: Some(Stroke::new(Color::rgb(0x22, 0x66, 0x22), 1.5)),
122 },
123 )));
124 // 圆弧
125 shapes.push(SceneNode::new(Element::arc(
126 Point::new(120.0, 320.0),
127 Vec2::new(50.0, 40.0),
128 0.3,
129 4.2,
130 Stroke::new(Color::rgb(0x00, 0x88, 0x88), 3.0),
131 )));
132 // 裁剪:把超出圆形的矩形部分裁掉(clip 演示)
133 shapes.push(
134 SceneNode::new(Element::rect(
135 lievisual::Rect::new(20.0, 20.0, 80.0, 80.0),
136 FillStrokeStyle::fill(Color::rgb(0x88, 0x44, 0xcc)),
137 ))
138 .with_transform(Transform::translate(360.0, 60.0))
139 .with_clip(lievisual::Clip::circle(Point::new(40.0, 40.0), 30.0)),
140 );
141 // 组合 + 变换 + 透明度
142 let box_children = vec![
143 SceneNode::new(Element::rounded_rect(
144 lievisual::Rect::new(0.0, 0.0, 90.0, 50.0),
145 8.0,
146 FillStrokeStyle::fill(Color::rgb(0xff, 0xcc, 0x00)),
147 )),
148 SceneNode::new(Element::text(
149 "group",
150 Point::new(12.0, 30.0),
151 TextStyle::new(Color::rgb(0x33, 0x33, 0x33), 14.0, "sans-serif"),
152 )),
153 ];
154 shapes.push(
155 SceneNode::group(box_children)
156 .with_transform(Transform::translate(430.0, 260.0).then(&Transform::rotate(0.25)))
157 .with_name("demo-group"),
158 );
159
160 // ---------- pie 层(饼图 + 层内节点透明度分级) ----------
161 let pie_style = |c: Color| FillStrokeStyle {
162 fill: Some(c.into()),
163 stroke: Some(Stroke::new(Color::WHITE, 1.5)),
164 };
165 let mut pie = Layer::new("pie");
166 pie.push_node(SceneNode::new(Element::pie(
167 Point::new(300.0, 320.0),
168 70.0,
169 0.0,
170 1.2,
171 pie_style(Color::rgb(0xe6, 0x19, 0x4b)),
172 )));
173 pie.push_node(
174 SceneNode::new(Element::pie(
175 Point::new(300.0, 320.0),
176 70.0,
177 1.2,
178 1.6,
179 pie_style(Color::rgb(0xf0, 0x82, 0x0c)),
180 ))
181 .with_opacity(0.75),
182 );
183 pie.push_node(
184 SceneNode::new(Element::pie(
185 Point::new(300.0, 320.0),
186 70.0,
187 2.8,
188 1.4,
189 pie_style(Color::rgb(0x9a, 0xC7, 0x00)),
190 ))
191 .with_opacity(0.5),
192 );
193 pie.push_node(
194 SceneNode::new(Element::pie(
195 Point::new(300.0, 320.0),
196 70.0,
197 4.2,
198 2.08,
199 pie_style(Color::rgb(0x42, 0x62, 0xac)),
200 ))
201 .with_opacity(0.25),
202 );
203
204 // ---------- labels 层(顶部文本;演示 baseline/weight/align 与 measure_text) ----------
205 let title_style = TextStyle::new(Color::rgb(0x11, 0x11, 0x11), 20.0, "sans-serif")
206 .with_weight(lievisual::FontWeight::Bold)
207 .with_align(lievisual::TextAlign::Left);
208 // 先测量再做水平居中(canvas 风格:measureText → 用 width 定位锚点)。
209 // 文本以样式化片段表达:单个 RichSpan 即单文本。
210 let m = lievisual::measure_text(
211 std::slice::from_ref(&lievisual::RichSpan::new(
212 "lievisual · 丰富 IR · Layer",
213 title_style.clone(),
214 )),
215 None,
216 );
217 let title_x = 280.0 - m.size.width / 2.0; // 画布中心 280 居中
218 let labels = Layer::new("labels").with_nodes(vec![SceneNode::new(Element::text(
219 "lievisual · 丰富 IR · Layer",
220 Point::new(title_x, 380.0),
221 title_style,
222 ))]);
223
224 // ---------- canvas-api 层:Canvas-like builder(Ctx + Path2D) ----------
225 // 与上面手写 IR 的图层等价,但用 canvas 风格表达:不可变 Ctx 携带 fill / stroke /
226 // transform / alpha,Path2D 累积路径后由 fill() / stroke() 终结。
227 let mut canvas_api = Layer::new("canvas-api");
228 let ctx = Ctx::new()
229 .with_fill(Color::rgb(0x2e, 0x8b, 0x57))
230 .with_line(Color::rgb(0x1f, 0x5f, 0x3d), 2.0)
231 .with_alpha(0.85);
232
233 // 叶片轮廓:三次贝塞尔 + close_path,填充并描边。
234 let mut leaf = Path2D::new();
235 leaf.move_to(0.0, 0.0);
236 leaf.curve_to(20.0, -30.0, 50.0, -30.0, 60.0, 0.0);
237 leaf.curve_to(50.0, 30.0, 20.0, 30.0, 0.0, 0.0);
238 leaf.close_path();
239 canvas_api.push(ctx.clone().translate(410.0, 330.0).fill_stroke_path(&leaf));
240
241 // 四分之三圆环:Path2D::arc(内部用 ≤90° 的三次贝塞尔近似),仅描边。
242 let mut ring = Path2D::new();
243 ring.arc(0.0, 0.0, 18.0, 0.0, std::f64::consts::TAU * 0.75);
244 canvas_api.push(
245 ctx.clone()
246 .with_line(Color::rgb(0xc0, 0x39, 0x2b), 3.0)
247 .with_dash(vec![5.0, 3.0], 0.0)
248 .translate(500.0, 330.0)
249 .stroke(&ring),
250 );
251
252 // 富文本:同一段文本内混排多种样式(Canvas 的 fillText 做不到)。
253 let rich = vec![
254 lievisual::RichSpan::new(
255 "Canvas-like ",
256 TextStyle::new(Color::rgb(0x2c, 0x3e, 0x50), 14.0, "sans-serif")
257 .with_weight(lievisual::FontWeight::Bold),
258 ),
259 lievisual::RichSpan::new(
260 "builder · Path2D",
261 TextStyle::new(Color::rgb(0x7f, 0x8c, 0x8d), 14.0, "sans-serif"),
262 ),
263 ];
264 let m = ctx.measure_rich_text(&rich);
265 canvas_api.push(ctx.fill_rich_text(410.0 - m.size.width / 2.0 + 30.0, 375.0, rich));
266
267 // 图层顺序(从底到顶):默认层 nodes → shapes → pie → canvas-api → labels
268 scene.push_layer(shapes);
269 scene.push_layer(pie);
270 scene.push_layer(canvas_api);
271 scene.push_layer(labels);
272
273 scene
274}
275
276fn main() {
277 let scene = build_scene();
278
279 // SVG 输出(render_scene 会自动捕获 scene 的 title/description/scale 与各图层)
280 // 两个后端同一契约:构造尺寸 = 输出尺寸,故这里显式乘 scale 得到 2× 画布。
281 let mut svg = SvgRenderer::new(scene.width * scene.scale, scene.height * scene.scale)
282 .with_background(scene.background);
283 scene.render(&mut svg);
284 let svg_str = svg.into_string();
285 std::fs::write("demo.svg", &svg_str).expect("写 SVG 失败");
286 println!("SVG 已写入 demo.svg ({} 字节)", svg_str.len());
287 println!(" 图层数: {}", scene.layers().len());
288 for l in scene.layers() {
289 println!(
290 " - {} ({} 节点, visible={})",
291 l.name,
292 l.nodes.len(),
293 l.visible
294 );
295 }
296 // 演示 measure_text / TextMetrics(canvas 风格)
297 let demo_style = lievisual::TextStyle::new(Color::BLACK, 20.0, "sans-serif");
298 let tm = lievisual::measure_text(
299 std::slice::from_ref(&lievisual::RichSpan::new(
300 "Measure 示例",
301 demo_style.clone(),
302 )),
303 None,
304 );
305 println!(
306 " 测量 \"Measure 示例\": width={:.2} height={:.2} alphabetic_baseline={:.2} ascent={:.2} descent={:.2}",
307 tm.metrics.width,
308 tm.metrics.height,
309 tm.metrics.alphabetic_baseline,
310 tm.metrics.font_bounding_box_ascent,
311 tm.metrics.font_bounding_box_descent
312 );
313
314 // PNG 输出
315 {
316 use lievisual::render::VelloPixmapRenderer;
317 // 与 SVG 端同一契约:构造尺寸 = 输出尺寸,乘 scale 提升栅格分辨率。
318 let mut renderer = VelloPixmapRenderer::new(
319 (scene.width * scene.scale) as u32,
320 (scene.height * scene.scale) as u32,
321 )
322 .with_background(scene.background);
323 let png = renderer.render_png(&scene);
324 std::fs::write("demo.png", &png).expect("写 PNG 失败");
325 println!("PNG 已写入 demo.png ({} 字节)", png.len());
326 }
327}Trait Implementations§
impl StructuralPartialEq for RichSpan
Auto Trait Implementations§
impl Forget for RichSpan
impl Freeze for RichSpan
impl Move for RichSpan
impl RefUnwindSafe for RichSpan
impl Send for RichSpan
impl Sync for RichSpan
impl Unpin for RichSpan
impl UnsafeUnpin for RichSpan
impl UnwindSafe for RichSpan
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more