跳到正文
开发版

字体栈与曲线排字 ​

mirui 先将文本整形(shaping)为带位置的字形(glyph),随后可按需将这些字形排布到已注册的矢量路径上。同一套 Path 与 PathId 类型同时驱动矢量绘制、裁剪、命中测试与文本基线。

TextPath
1020static WAVE_CREST: Path = path!(M 8 64 Q 60 18 112 64 Q 164 110 216 64);
1021static WAVE_VALLEY: Path = path!(M 8 50 Q 60 96 112 50 Q 164 4 216 50);
1022
1023#[model]
1024pub struct CurveRoute {
1025 crest: PathId,
1026 valley: PathId,
1027 #[observe]
1028 pub inverted: bool,
1029 #[observe]
1030 pub offset: i32,
1031}
1032
1033impl CurveRoute {
1034 pub fn new(crest: PathId, valley: PathId) -> Self {
1035 Self {
1036 crest,
1037 valley,
1038 inverted: false,
1039 offset: 4,
1040 }
1041 }
1042}
1043
1044#[model]
1045impl CurveRoute {
1046 #[observe]
1047 pub fn placement(&self) -> TextPath {
1048 let path = if self.inverted {
1049 self.valley
1050 } else {
1051 self.crest
1052 };
1053 TextPath::new(path).with_offset(Fixed::from_int(self.offset))
1054 }
1055
1056 pub fn flip(&mut self) {
1057 self.inverted = !self.inverted;
1058 }
1059
1060 pub fn advance(&mut self) {
1061 self.offset = (self.offset + 14) % 42;
1062 }
1063}
1064
1065#[compose]
1066fn curve_scene(route: model!(CurveRoute)) {
1067 ui! {
1068 Column (
1069 grow: 1.0,
1070 padding: Padding::all(16),
1071 row_gap: 10,
1072 text_color: ColorToken::OnSurface
1073 ) {
1074 Row (height: 30, column_gap: 6, align: AlignItems::Center) {
1075 Text (
1076 text: ${ format_args!("+{} px", route.offset()) },
1077 capacity: 16,
1078 grow: 1.0,
1079 min_width: 0,
1080 height: 22,
1081 font_size: 13,
1082 text_color: ColorToken::OnSurfaceVariant,
1083 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
1084 )
1085 Button (
1086 "Slide +14",
1087 width: 72,
1088 height: 28,
1089 font_size: 12,
1090 border_radius: 7,
1091 normal_color: ColorToken::SurfaceVariant,
1092 pressed_color: ColorToken::Outline,
1093 text_color: ColorToken::OnSurface
1094 ) on Tap { route.advance(); }
1095 Button (
1096 "Flip wave",
1097 width: 74,
1098 height: 28,
1099 font_size: 12,
1100 border_radius: 7,
1101 normal_color: ColorToken::Primary,
1102 pressed_color: ColorToken::Secondary,
1103 text_color: ColorToken::OnPrimary
1104 ) on Tap { route.flip(); }
1105 }
1106 View (
1107 grow: 1.0,
1108 bg_color: ColorToken::SurfaceVariant,
1109 border_radius: 12,
1110 clip_children: true
1111 ) {
1112 Text (
1113 "mirui rides the wave",
1114 path: ${ route.placement() },
1115 position: Position::Absolute,
1116 left: 6,
1117 top: 8,
1118 width: Dimension::percent(96),
1119 height: Dimension::percent(88),
1120 font_size: 14,
1121 text_color: ColorToken::Primary
1122 )
1123 }
1124 }
1125 };
1126}
正在加载预览…

点击 Slide +18 沿贝塞尔曲线推进起始偏移,或点击 Flip wave 在波峰与波谷路径之间切换响应式 TextPath。

默认字体与提供方 ​

Font::default() 打开内嵌的 8 px ASCII MIRX 字体,其 A1 字形图集直接借用自程序只读存储。font_size 同时缩放字体度量与绘制尺寸。除非字体栈提供了额外字形覆盖,否则 ASCII 以外的字符会回退显示为该字体的问号字形。需要更多字符集的应用可通过 App::with_fonts 注册其他 MIRX 字体。

font、font_stack、font_size 与 text_color 沿 UI 父子链向下继承,每个字段各自取最近的祖先声明值。根节点默认使用默认字体栈、所选字体的原生字号以及 ColorToken::OnSurface。若子树继承了其他字号,可显式指定 font_size: FontSize::Native 以恢复所选字体的原生字号。段落排版属性(paragraph)在每个文本控件上单独指定。自定义绘制回调可通过 paint.typography() 读取最终生效的排版参数。

包括内嵌字体在内,所有字体均通过 FontProvider 接口访问。自定义提供方保存在 Font 的 provider: Rc<dyn FontProvider> 字段中;克隆 Font 会共享同一个提供方。软件渲染器与硬件加速渲染器消费相同的光栅区域与覆盖率元数据。软件渲染器直接读取原尺寸 A1 覆盖率,并在缩放时重采样覆盖率数据。

文本框溢出 ​

LF、CR 与 CRLF 用于分隔段落行,自身不会请求字体覆盖或生成字形。开头、连续或末尾的换行符会保留空行及其对应的光标位置。文本、选区与剪贴板偏移量均直接对应原始 UTF-8 字节;CRLF 的两个字节之间不存在光标位置。TextEditing 保持单行显示,在水平方向移动光标或删除时会一次性跨过整个 CRLF 字节对。

当文本需要自动换行或显示省略号时,请为控件指定有限宽度。使用 Ellipsis 还要求字体栈中至少有一款字体映射了 U+2026(…)。在显式指定尺寸的轴向上,普通 Text 在两种溢出策略下都会裁剪到变换后的文本框内;而 Auto 与 Content 尺寸则会在祖先裁剪区域内保留字形墨迹外延(ink overhang)。路径文本因为字形墨迹可能延伸出布局框之外,始终使用其排布后的字形几何与祖先裁剪区域。

TextOverflow::Ellipsis
1207#[model]
1208#[derive(Default)]
1209pub struct OverflowDemo {
1210 #[observe]
1211 pub wrap: bool,
1212}
1213
1214#[model]
1215impl OverflowDemo {
1216 pub fn toggle(&mut self) {
1217 self.wrap = !self.wrap;
1218 }
1219}
1220
1221#[compose]
1222fn typo_ellipsis_scene(mode: model!(OverflowDemo)) {
1223 ui! {
1224 Column (
1225 grow: 1.0,
1226 padding: Padding::all(18),
1227 justify: JustifyContent::SpaceBetween,
1228 text_color: ColorToken::OnSurface
1229 ) {
1230 Row (height: 32, align: AlignItems::Center) {
1231 Text (
1232 text: ${ if mode.wrap() { "Wrap · max_lines: 2" } else { "NoWrap · Ellipsis (1 line)" } },
1233 grow: 1.0,
1234 height: 20,
1235 font_size: 12,
1236 text_color: ColorToken::OnSurfaceVariant
1237 )
1238 Button (
1239 "Toggle wrap",
1240 width: 96,
1241 height: 30,
1242 font_size: 12,
1243 border_radius: 7,
1244 normal_color: ColorToken::Primary,
1245 pressed_color: ColorToken::Secondary,
1246 text_color: ColorToken::OnPrimary
1247 ) on Tap { mode.toggle(); }
1248 }
1249 View (
1250 height: 68,
1251 padding: Padding::all(14),
1252 bg_color: ColorToken::SurfaceVariant,
1253 border_radius: 10
1254 ) {
1255 Text (
1256 "A long status label that clips with an ellipsis or wraps across two lines when toggled.",
1257 width: Dimension::percent(100),
1258 height: 40,
1259 font_size: 14,
1260 paragraph: ${
1261 ParagraphStyle {
1262 wrap: if mode.wrap() { TextWrap::Word } else { TextWrap::NoWrap },
1263 max_lines: Some(if mode.wrap() { 2 } else { 1 }),
1264 overflow: TextOverflow::Ellipsis,
1265 ..ParagraphStyle::default()
1266 }
1267 }
1268 )
1269 }
1270 }
1271 };
1272}
正在加载预览…

点击 Toggle wrap 在单行省略号截断(NoWrap + TextOverflow::Ellipsis)与双行自动换行(TextWrap::Word)之间切换。

布局失败报告 ​

当文本准备失败(包括 TextInput 正文与占位符)时,App::render() 与 App::render_dirty() 会返回 RenderError::TextLayout。当前帧的绘制、滚动拷贝与帧缓冲刷写都会被跳过。调用 App::last_text_layout_failure() 可查看具体的文本实体、失败所处的准备阶段以及可用容量详情;后续一旦布局成功,该诊断信息会自动清空。

TextEditing 会在自身最终尺寸确定后准备单行文本布局。绘制阶段由字形与光标直接读取该布局;输入编辑、占位符变更、通过 App::with_fonts 替换字体以及重置缓存都会触发重新准备。空输入框将光标保持在起始位置;超长内容则会水平滚动以确保光标始终处于可见区域内。

底层调用方可使用 render_system::try_update_layout 捕获布局错误并在绘制前提前中止。collect_dirty_region 与 collect_dirty_regions 同样返回携带 TextLayoutFailure 的 Result;App::dirty_region() 会将该错误映射为 RenderError::TextLayout。成功查询出空脏区与布局失败是截然不同的两种结果。布局失败还会向 World 记录 TextLayoutFailure 资源并使已缓存的几何失效以便下次重试,但不会恢复在本次准备过程中已被淘汰的文本缓存条目。

App 在全量或增量渲染成功后记录已绘制边界。底层自定义渲染循环应在成功完成绘制后、发生进一步状态变更前调用 render_system::seed_prev_rects;仅收集脏区不会将该帧标记为已绘制。

有界动态文本内容 ​

capacity 为 Text 或 Button 标签预留固定字节数的 UTF-8 内容存储。响应式表达式可以直接在栈缓冲区中完成格式化,再将完整结果拷入该预留存储,从而在更新文本内容时实现零堆分配:

rust
#[compose]
fn reading_label(reading: Signal<u16>) {
    ui! {
        Text(text: ${ format_args!("{:03}", reading.get()) }, capacity: 4)
    };
}

capacity 统计的是 UTF-8 字节数而非字符数。若新值超出容量或格式化失败,控件会保留上一次的有效内容,并在 Text 组件上记录 TextContentError。如果首帧初始值即告失败,App::render() 与 App::render_dirty() 会在绘制或刷写前直接返回 RenderError::TextContent。App::last_text_content_failure() 会返回出错实体与具体错误,并优先上报尚未获得合法初始值的标签。后续只要有一次合法更新即可清除该诊断状态。已有合法初始值后,运行期发生内容溢出不会阻断界面其余部分的正常渲染。

有界写入仅涵盖 Text 内部执行的存储与格式化过程;若表达式本身写成了 format!(...)、.to_string() 或克隆了 Signal<String>,则在进入有界写入前仍可能发生堆分配。当要求更新过程严格零分配时,请对借用值或标量值使用 format_args!。文本整形、排版布局、字体缓存与后端资源各自拥有独立的容量限制。有界内容存放的是纯文本值;若标签需要随语言环境(locale)动态切换,请使用现有的本地化文本路径。

预留有界排版存储 ​

App::with_text_layout_capacity 在首次执行布局传递前预分配文本缓存存储。当默认字节预算不适用时,请先设置 TextLayoutLimits。若调用返回错误,原先安装的缓存保持不变;在文本布局已经启动后再调用该方法会返回 TextLayoutError::InUse。App::try_with_text_layout_limits 也支持在完成容量预留之后、首次布局开始之前调整限制参数并保留已声明的容量;若新限制与已预留容量不兼容,则返回错误且不替换原缓存。流式方法 with_text_layout_limits 在遇到不兼容限制时会直接 panic,而不会静默移除容量上限。

rust
use mirui::text::{TextLayoutCapacity, WorkspaceCapacity};

app.with_text_layout_capacity(TextLayoutCapacity {
    layout_slots: 4,
    measurements: 4,
    lines: 16,
    runs: 32,
    glyphs: 64,
    carets: 64,
    workspace: WorkspaceCapacity {
        runs: 16,
        glyphs: 32,
        scratch_glyphs: 32,
        lines: 16,
    },
})?;

layout_slots 涵盖保留的布局句柄及其代际(generation)存储。行(lines)、字形段(runs)、字形(glyphs)与光标(carets)容量涵盖所有常驻段落加上当前正在排版段落的临时输出——它们是全局总量上限,不同于 TextLayoutLimits 中的单段落上限。workspace 约束私有整形缓冲区的大小。预留操作会校验实际保留的向量容量与工作区之和是否未超出 TextLayoutLimits::cache_bytes。后续运行中一旦请求超出已声明的容量,将显式报错失败,而不会动态扩容这些缓冲区。渲染器与字体缓存各自拥有独立的存储空间。

可复用的 UI 模块可以在初始化阶段通过 App::require_text_layout_capacity 声明自身的 module_text_capacity: TextLayoutCapacity。各模块的布局槽位、测量项、行、字形段、字形与光标需求会累加计算;整形工作区因为全局复用同一块暂存区,会在各模块需求之间取最大值。当所有模块组装完毕后,请在首次布局传递前调用 App::prepare_text_layout:

rust
app.require_text_layout_capacity(module_text_capacity)?;
// 先组装其余 UI 模块,随后统一准备共享缓存。
app.prepare_text_layout()?;

声明需求阶段不会立即替换缓存;调用 prepare_text_layout 时才会对照当前字节预算校验汇总后的容量需求,若失败则保留原缓存不动。在布局启动后调用、或将累加式需求与 with_text_layout_capacity 混用都会返回错误,以免冲掉其他模块的预留配额。App::render() 与 App::render_dirty() 在进入布局前也会自动准备待生效的容量需求;若准备失败则返回 RenderError::TextPreparation,可通过 App::last_text_layout_preparation_error() 查询原因。自行控制启动流程的应用应当显式调用 prepare_text_layout,以便在进入帧循环前捕获配置错误。若未启用任何容量预留,文本布局保持原有的按需分配行为。

在 Web Canvas 后端上,示例或应用可以在启动阶段提供未变换、1× 缩放下单条字形段(run)的墨迹范围提示:

rust
app.prefer_text_raster_scratch(256, 8)?;

App 会取所有提示中的最大宽高,并在每帧开始前结合当前视口缩放比准备光栅化槽位。槽位数量按已构建的有界 Text 控件数量分配;每个槽位各自保留其 RGBA 缓冲区、离屏 Canvas、ImageData 及像素视图。每个槽位归属一条文本字形段:内容未变时直接复用已光栅化的图像,内容改变时在同一槽位内原地重绘,不会挤占其他字形段的槽位。所有槽位共享同一块软件光栅化暂存区。单代(generation)请求的 RGBA 总字节数上限为 8 MiB;另有独立的 8 MiB 预检额度,用于在视口缩放重建期间同时容纳旧代与新代的 RGBA 缓冲及槽位元数据。字形缓存(glyph cache)拥有自身独立的 8 MiB 标称画布像素上限,二者不共用同一个合并预算。暂存区检查约束的是受管 Rust 请求大小,而非底层分配器实际开销或浏览器堆内存占用。视口放大或新增有界文本控件可能需要更多存储。若准备失败,旧槽位继续可用,新增或超出的部分按需回退到字形缓存路径绘制,并可通过 App::last_text_raster_scratch_preparation_error() 读取错误详情。因几何尺寸或预算预检被拒绝的相同请求不会在每一帧重复尝试重建。对象池构建失败后,两次重试之间最多跳过 64 次准备调用;请求参数改变或调用 trim_memory 会重置失败状态。内存裁剪(memory trim)会在下次准备前释放保留的暂存区以及字形/纹理池。静态文本与超出预置范围的字形段继续走原有的字形缓存路径;该提示并非硬性容量上限,也不构成零资源增长保证。单个有界 Text 可能产生多条布局字形段;当活跃字形段身份超出预置槽位数时,新字形段按最近最少使用(LRU)策略淘汰旧槽位。capacity 仅限制 UTF-8 文本字节数,不限制光栅化像素尺寸;带几何变换的文本也可能超出提示尺寸。

WebCanvasRendererFactory::text_resource_stats() 详细区分了暂存池准备尝试次数与失败次数、保留槽位命中次数、重绘次数、字形段淘汰次数,以及因超出条件而回退创建字形缓存表面的有界字形段次数。准备计数器仅统计实际发起的新池构建尝试,不包含命中缓存拒绝或跳过的调用。bounded_scratch_current 与 bounded_scratch_peak 是 WebScratchResourceUsage 快照:其中的 Rust 容量统计 RGBA 槽位与槽位表大小,峰值包含新旧代重叠及部分构建失败时的瞬时占用;Canvas 与 ImageData 字段统计保留对象数及构建重叠数。ImageData 字节数按其声明的 RGBA 像素长度计算;Canvas 像素字节数仅表示等效 RGBA 面积,并非浏览器底层实测显存或后备存储开销。js_pixels 视图与 ImageData 共享存储,保留的 rust_view 则指向 Rust 侧 RGBA 缓冲区。bounded_scratch_upload_bytes 统计缓存未命中后的整槽上传字节数(而非新分配字节数);bounded_upload_bytes 统计新增的字形缓存像素上传量。暂存池与回退计数器仅涵盖原生线性有界文本;路径排布文本与透视投影文本走各自独立的渲染路由。这些计数器不包含软件光栅化工作暂存区、字形缓存元数据、浏览器对象头开销、Canvas 内部缓冲以及延迟垃圾回收,因此不等同于浏览器堆上限。普通文本与文本输入框保持原有渲染路径不变。

收到显式内存告警时,有界文本布局缓存及其存活句柄会被保留,以确保容量预留契约在下一帧依然有效;而 Web Canvas 光栅化暂存池与字形/纹理池属于可重建资源,会被立即释放。无界文本缓存则保持原有的内存裁剪行为。

静态路径上的文本 ​

path! 宏将路径命令直接存放在程序镜像中。使用 insert_static 注册这些命令可保持零拷贝借用,并分配一个稳定的 PathId。

Static Path
1274static ARC: Path = path!(M 12 72 Q 114 10 216 72);
1275
1276fn register_arc<B: Surface, F: RendererFactory<B>>(app: &mut App<B, F>) -> PathId {
1277 app.paths()
1278 .insert_static(ARC.commands())
1279 .expect("path capacity")
1280}
1281
1282#[compose]
1283fn curved_title(arc: PathId) {
1284 ui! {
1285 Column (
1286 grow: 1.0,
1287 padding: Padding::all(16),
1288 justify: JustifyContent::Center
1289 ) {
1290 View (
1291 height: 104,
1292 bg_color: ColorToken::SurfaceVariant,
1293 border_radius: 12,
1294 clip_children: true
1295 ) {
1296 Text (
1297 "Status · Ready on arc",
1298 path: arc,
1299 position: Position::Absolute,
1300 left: 8,
1301 top: 10,
1302 width: Dimension::percent(94),
1303 height: 86,
1304 font_size: 14,
1305 text_color: ColorToken::Primary
1306 )
1307 }
1308 }
1309 };
1310}
正在加载预览…

insert_static 直接借用编译期 ARC 路径命令并返回 PathId,Text(path: arc) 从距离 0 处开始沿弧线排布字形。

直接传入 PathId 时,默认使用子路径 0、从弧长距离 0 处开始、沿路径正向排布,并使用整条可用路径长度。

多行路径排布 ​

段落中的每一行依次消费一条连续的子路径(从 TextPath::subpath() 开始)。每条子路径的可用长度会在放置字形前先行约束自动换行、对齐、两端对齐(justification)以及省略号截断。

Multi-Subpath
1312static LINES: Path = path!(
1313 M 12 30 L 216 30
1314 M 14 66 Q 114 100 214 66
1315);
1316
1317fn register_lines<B: Surface, F: RendererFactory<B>>(app: &mut App<B, F>) -> PathId {
1318 app.paths()
1319 .insert_static(LINES.commands())
1320 .expect("path capacity")
1321}
1322
1323#[compose]
1324fn multiline_label(lines: PathId) {
1325 ui! {
1326 Column (
1327 grow: 1.0,
1328 padding: Padding::all(16),
1329 justify: JustifyContent::Center
1330 ) {
1331 View (
1332 height: 112,
1333 bg_color: ColorToken::SurfaceVariant,
1334 border_radius: 12,
1335 clip_children: true
1336 ) {
1337 Text (
1338 "First line stays straight\nand line two curves.",
1339 path: lines,
1340 position: Position::Absolute,
1341 left: 8,
1342 top: 8,
1343 width: Dimension::percent(94),
1344 height: 96,
1345 font_size: 13,
1346 text_color: ColorToken::OnSurface,
1347 paragraph: ParagraphStyle {
1348 wrap: TextWrap::Word,
1349 max_lines: Some(2),
1350 ..ParagraphStyle::default()
1351 }
1352 )
1353 }
1354 }
1355 };
1356}
正在加载预览…

LINES 包含两条连续子路径(直线 M 18 32 L 254 32 与二次曲线 M 24 68 Q 138 106 252 68):第一行沿直线排布,第二行自动折行到下方的弧线上。

这一过程不会分配额外的基线数组,也不会复制路径命令缓冲区。纯直线子路径直接从 Path 采样;包含曲线的子路径则复用有界测量缓存。若所需的子路径缺失或非法,路径排版会在发布任何字形或光标几何前直接拒绝本次布局。

选择路径区间与方向 ​

TextPath 描述文本如何消费一条已注册路径,而无需拷贝底层路径命令。

Range & Direction
1358static RING_PATH: Path = path!(
1359 M 12 24 L 216 24
1360 M 14 66 Q 114 20 214 66
1361);
1362
1363#[model]
1364pub struct RangeControl {
1365 path: PathId,
1366 #[observe]
1367 pub reversed: bool,
1368 #[observe]
1369 pub offset: i32,
1370}
1371
1372#[model]
1373impl RangeControl {
1374 pub fn new(path: PathId) -> Self {
1375 Self {
1376 path,
1377 reversed: false,
1378 offset: 6,
1379 }
1380 }
1381
1382 #[observe]
1383 pub fn placement(&self) -> TextPath {
1384 let dir = if self.reversed {
1385 PathDirection::Reverse
1386 } else {
1387 PathDirection::Forward
1388 };
1389 TextPath::new(self.path)
1390 .with_subpath(1)
1391 .with_range(Fixed::from_int(8)..Fixed::from_int(202))
1392 .with_offset(Fixed::from_int(self.offset))
1393 .with_direction(dir)
1394 }
1395
1396 pub fn flip_dir(&mut self) {
1397 self.reversed = !self.reversed;
1398 }
1399
1400 pub fn step_offset(&mut self) {
1401 self.offset = (self.offset + 12) % 36;
1402 }
1403}
1404
1405#[compose]
1406fn typo_range_scene(state: model!(RangeControl)) {
1407 ui! {
1408 Column (
1409 grow: 1.0,
1410 padding: Padding::all(16),
1411 row_gap: 10,
1412 text_color: ColorToken::OnSurface
1413 ) {
1414 Row (height: 30, column_gap: 6, align: AlignItems::Center) {
1415 Text (
1416 text: ${
1417 format_args!(
1418 "{} · +{} px",
1419 if state.reversed() { "Reverse" } else { "Forward" },
1420 state.offset()
1421 )
1422 },
1423 capacity: 28,
1424 grow: 1.0,
1425 height: 20,
1426 font_size: 12,
1427 text_color: ColorToken::OnSurfaceVariant,
1428 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
1429 )
1430 Button (
1431 "Offset +12",
1432 width: 78,
1433 height: 28,
1434 font_size: 12,
1435 border_radius: 7,
1436 normal_color: ColorToken::SurfaceVariant,
1437 pressed_color: ColorToken::Outline,
1438 text_color: ColorToken::OnSurface
1439 ) on Tap { state.step_offset(); }
1440 Button (
1441 "Reverse",
1442 width: 68,
1443 height: 28,
1444 font_size: 12,
1445 border_radius: 7,
1446 normal_color: ColorToken::Primary,
1447 pressed_color: ColorToken::Secondary,
1448 text_color: ColorToken::OnPrimary
1449 ) on Tap { state.flip_dir(); }
1450 }
1451 View (
1452 grow: 1.0,
1453 bg_color: ColorToken::SurfaceVariant,
1454 border_radius: 12,
1455 clip_children: true
1456 ) {
1457 Text (
1458 "Range on subpath 1",
1459 path: ${ state.placement() },
1460 position: Position::Absolute,
1461 left: 6,
1462 top: 8,
1463 width: Dimension::percent(95),
1464 height: Dimension::percent(86),
1465 font_size: 14,
1466 text_color: ColorToken::Primary
1467 )
1468 }
1469 }
1470 };
1471}
正在加载预览…

点击 Offset +16 沿子路径 1 推进起始偏移,或点击 Reverse 在正向(Forward)与反向(Reverse)排布之间切换。

with_seam 用于指定闭合子路径上作为起点的弧长位置。应用接缝(seam)偏移后,所选区间仍按路径弧长单位度量。

with_offset 在所选区间内部留出一段定点起始内边距。换行、对齐、省略号、字形与光标均使用扣除偏移后的剩余路径长度。

同一个 TextPath 配置值可以直接作为参数传递给组合函数:

rust
use mirui::prelude::*;
use mirui::ui::widgets::Text;

#[compose]
fn status_label(placement: TextPath) {
    ui! {
        Text("Status", font_size: 24, path: placement)
    };
}

响应式切换路径 ​

DSL 支持将 Signal<PathId> 作为响应式 path 属性传入。更新信号值会将现有文本实体平滑切换到新路径,并自动迁移其路径变更订阅。

Signal<PathId>
1473#[compose]
1474fn selectable_path(first: PathId, second: PathId) {
1475 let selected = Signal::new(first);
1476 let selection = selected.clone();
1477
1478 ui! {
1479 Column (
1480 grow: 1.0,
1481 padding: Padding::all(16),
1482 row_gap: 10,
1483 text_color: ColorToken::OnSurface
1484 ) {
1485 Row (height: 30, align: AlignItems::Center) {
1486 Text (
1487 "Signal<PathId> binding",
1488 grow: 1.0,
1489 height: 20,
1490 font_size: 12,
1491 text_color: ColorToken::OnSurfaceVariant
1492 )
1493 Button (
1494 "Switch path",
1495 width: 90,
1496 height: 28,
1497 font_size: 12,
1498 border_radius: 7,
1499 normal_color: ColorToken::Primary,
1500 pressed_color: ColorToken::Secondary,
1501 text_color: ColorToken::OnPrimary
1502 ) on Tap {
1503 selected.update(|path| {
1504 *path = if *path == first { second } else { first };
1505 });
1506 }
1507 }
1508 View (
1509 grow: 1.0,
1510 bg_color: ColorToken::SurfaceVariant,
1511 border_radius: 12,
1512 clip_children: true
1513 ) {
1514 Text (
1515 "Live route swaps PathId",
1516 path: $selection,
1517 position: Position::Absolute,
1518 left: 6,
1519 top: 10,
1520 width: Dimension::percent(95),
1521 height: Dimension::percent(85),
1522 font_size: 14,
1523 text_color: ColorToken::Primary
1524 )
1525 }
1526 }
1527 };
1528}
正在加载预览…

点击 Switch path 更新 selected: Signal<PathId>——同一个 Text 实体在两条已注册路径之间直接切换,无需销毁重建控件。

从信号派生路径几何 ​

创建一条可变路径,一次性预留好命令容量,再将其几何坐标绑定到信号上。bind_path 会安装一个跟随所有者生命周期的响应式副作用:每当信号变化时,直接原地修改现有路径、推进其修订号,并将所有订阅了该路径的文本或绘图实体标脏。

bind_path
1530#[compose]
1531fn elastic_baseline(amplitude: Signal<Fixed>) {
1532 let mut geometry = Path::try_with_capacity(2).expect("path storage");
1533 geometry
1534 .move_to(Point::new(12, 52))
1535 .quad_to(Point::new(114, 52), Point::new(216, 52));
1536 let path = cx.paths().insert(geometry).expect("path capacity");
1537 let live_amplitude = amplitude.clone();
1538 let toggle_amp = amplitude.clone();
1539
1540 cx.bind_path(path, move |geometry| {
1541 let y = Fixed::from_int(52) - live_amplitude.get();
1542 geometry
1543 .set_command(
1544 1,
1545 PathCmd::QuadTo {
1546 ctrl: Point::new(Fixed::from_int(114), y),
1547 end: Point::new(216, 52),
1548 },
1549 )
1550 .expect("stable command shape");
1551 })
1552 .expect("mutable path");
1553
1554 ui! {
1555 Column (
1556 grow: 1.0,
1557 padding: Padding::all(16),
1558 row_gap: 10,
1559 text_color: ColorToken::OnSurface
1560 ) {
1561 Row (height: 30, align: AlignItems::Center) {
1562 Text (
1563 "cx.bind_path(path, ...)",
1564 grow: 1.0,
1565 height: 20,
1566 font_size: 12,
1567 text_color: ColorToken::OnSurfaceVariant
1568 )
1569 Button (
1570 "Invert curve",
1571 width: 96,
1572 height: 28,
1573 font_size: 12,
1574 border_radius: 7,
1575 normal_color: ColorToken::Primary,
1576 pressed_color: ColorToken::Secondary,
1577 text_color: ColorToken::OnPrimary
1578 ) on Tap {
1579 toggle_amp.update(|a| *a = -*a);
1580 }
1581 }
1582 View (
1583 grow: 1.0,
1584 bg_color: ColorToken::SurfaceVariant,
1585 border_radius: 12,
1586 clip_children: true
1587 ) {
1588 Text (
1589 "Signal-driven curve",
1590 path: path,
1591 position: Position::Absolute,
1592 left: 8,
1593 top: 8,
1594 width: Dimension::percent(94),
1595 height: Dimension::percent(86),
1596 font_size: 14,
1597 text_color: ColorToken::Primary
1598 )
1599 }
1600 }
1601 };
1602}
正在加载预览…

点击 Invert amplitude 翻转 amplitude: Signal<Fixed>——cx.bind_path 原地修改二次贝塞尔控制点并触发路径文本重排。

该副作用始终复用最初分配的路径存储与命令容量。当路径的命令拓扑结构固定不变时,优先使用 set_command 原地更新控制点;只有当命令数量或类型本身发生变化时,才使用 clear 配合路径构建方法重写。

在事件处理器中编辑路径 ​

事件回调可通过 res!(mut PathStore) 可变借用路径仓库。按 PathId 原地编辑路径能够保持实体身份不变,并自动更新该路径的所有消费方。

res!(mut PathStore)
1604#[compose]
1605fn bendable_label(path: PathId) {
1606 ui! {
1607 Column (
1608 grow: 1.0,
1609 padding: Padding::all(16),
1610 justify: JustifyContent::Center
1611 ) {
1612 View (
1613 height: 108,
1614 bg_color: ColorToken::SurfaceVariant,
1615 border_radius: 12,
1616 clip_children: true
1617 ) {
1618 Text (
1619 "Tap text to bend curve",
1620 path: path,
1621 position: Position::Absolute,
1622 left: 8,
1623 top: 10,
1624 width: Dimension::percent(94),
1625 height: 88,
1626 font_size: 14,
1627 text_color: ColorToken::Primary
1628 ) on Tap(|paths: res!(mut PathStore)| {
1629 paths
1630 .edit(path, |geometry| {
1631 let next_y = match geometry.commands().get(1) {
1632 Some(PathCmd::QuadTo { ctrl, .. }) if ctrl.y < Fixed::from_int(40) => 92,
1633 _ => 12,
1634 };
1635 geometry
1636 .set_command(
1637 1,
1638 PathCmd::QuadTo {
1639 ctrl: Point::new(114, next_y),
1640 end: Point::new(216, 58),
1641 },
1642 )
1643 .expect("stable command shape");
1644 })
1645 .expect("mutable path");
1646 })
1647 }
1648 }
1649 };
1650}
正在加载预览…

直接点击右侧画布中的弧形文字触发 on Tap 回调,通过 res!(mut PathStore) 原地修改该 PathId 的控制点高度。

查询光标与选区几何 ​

Outputs<TextGeometry> 允许借用带有 Text 组件实体的已完成排版几何。在组件上声明 #[depends_on(TextGeometry)] 后,注册其 View 时即会请求保留该几何产物。排版、字形放置与光标放置与文本绘制共享同一份常驻存储;读取几何不会触发额外计算或内存拷贝。

Outputs<TextGeometry>
1652#[component]
1653#[depends_on(TextGeometry)]
1654struct CaretOverlay;
1655
1656#[view(component = CaretOverlay)]
1657fn caret_overlay(entity: entity!(), geometries: res!(Outputs<TextGeometry>), paint: paint!()) {
1658 let Some(geometry) = geometries.get(entity) else {
1659 return;
1660 };
1661 let mut storage = [PathCaretGeometry::default(); 64];
1662 let Ok(carets) = geometry.carets_into(&mut storage) else {
1663 return;
1664 };
1665 let rect = paint.rect();
1666 for caret in carets {
1667 let [start, end] = caret.segment();
1668 paint.draw(&DrawCommand::Line {
1669 p1: Point::new(rect.x + start.x, rect.y + start.y),
1670 p2: Point::new(rect.x + end.x, rect.y + end.y),
1671 transform: paint.transform(),
1672 color: Color::rgb(70, 160, 255),
1673 width: Fixed::ONE,
1674 opa: 255,
1675 });
1676 }
1677}
1678
1679#[compose]
1680fn typo_carets_scene(arc: PathId) {
1681 ui! {
1682 Column (
1683 grow: 1.0,
1684 padding: Padding::all(16),
1685 justify: JustifyContent::Center
1686 ) {
1687 View (
1688 height: 108,
1689 bg_color: ColorToken::SurfaceVariant,
1690 border_radius: 12,
1691 clip_children: true
1692 ) {
1693 Text (
1694 "Carets along arc",
1695 path: arc,
1696 position: Position::Absolute,
1697 left: 8,
1698 top: 14,
1699 width: Dimension::percent(94),
1700 height: 84,
1701 font_size: 15,
1702 text_color: ColorToken::OnSurface
1703 ) [CaretOverlay]
1704 }
1705 }
1706 };
1707}
正在加载预览…

CaretOverlay 声明 #[depends_on(TextGeometry)],在自定义 #[view] 中读取每个字符沿曲线法线方向排列的光标线段并绘制出来。

注册 caret_overlay::view() 并将 CaretOverlay 挂载到路径文本节点上即可生效。carets_into 与 selection_into 会把源节点局部坐标系下的几何写入调用方提供的栈缓冲区。上例在此基础上加上当前绘制原点并统一应用一次绘制变换。hit_test 接收屏幕坐标;transform() 返回已准备好的源局部坐标到屏幕坐标的变换矩阵(已合并祖先变换与滚动偏移)。对于普通线性文本,还可通过 layout()、metrics() 与 content_rect() 访问其保留的排版结构、字体度量及局部内容矩形。

双向混排文本(Bidi)的选区会在视觉字形段(visual run)与行边界处自动切分。若光标缓冲区容量不足,仅会使交互几何不可用,而不会丢弃已准备好的绘制字形。当选区输出容量校验失败时,选区缓冲保持不变;穿过或落入近裁剪面后方的命中测试会返回 PathTextGeometryError::InvalidProjection。缓存替换、内存裁剪、源节点隐藏或文本移除都会使已发布的几何失效,直至下一次排版准备成功。TextEditing 共享底层的布局存储,但不会覆盖同一实体上 Text 组件发布的几何结果。

后端行为 ​

HYBRID ENGINE & EXACT FALLBACK2D 位块外设、VG-Lite 矢量 GPU 特性查询与有界软件回退路由流水线

`VgLiteEngine::route` 对照启动时通过 `vg_lite_query_feature(gcFEATURE_BIT_VG_*)` 探测的硬件特性表:二次/三次贝塞尔曲线填充(`FillPath`)、描边(`StrokePath`)与 3×3 透视矩阵纹理合成(`Blit`)直接返回 `Ok(RenderRoute::Native)`。

if vg_lite_query_feature(gcFEATURE_BIT_VG_STROKE_PATH) != 0 { return Ok(RenderRoute::Native); }Hardware Bézier tessellation + 3×3 matrix blit
几何类型SoftwareWGPUSDL GPUWeb Canvas
线性与仿射变换原生原生原生字形段缓存原生 Canvas 路径
带姿态覆盖率字形(Posed coverage)逆变换采样实例化图集批处理标量图集逐字形变换
带姿态 SDF 字形(Posed SDF)偏导感知采样偏导感知采样精确覆盖率路由精确覆盖率路由
透视投影字形(Projective glyphs)逆变换采样透视投影图集有界软件渲染目标有界软件渲染目标

SDL GPU 的字形段纹理将非预乘(straight)RGB 颜色与字形覆盖率分开存储。透明纹素保留文本颜色,从而避免线性滤波在字形边缘引入暗边;文本颜色透明度与绘制不透明度在最终合成阶段统一应用。

SDL GPU 的带姿态覆盖率批次会在字形区域外侧纹素中心处切分几何,既保留源采样缩放比例,又将插值严格钳制在各自字形内部。空裁剪区域不会向 GPU 提交任何文本几何。

render::PosedGlyphs 将借用的定位字形切片与姿态变换架(placement frames)配对,供渲染命令、Canvas 调用及场景重放使用。若两个切片长度不一致,构造器会直接拒绝而不会发生内存分配。

对于透视投影字形、填充、边框以及带圆角、透明度或混合模式的图像拷贝,SDL GPU 与 Web Canvas 会使用通过渲染器工厂提供的 ProjectiveFallback。其固定大小的 RGBA 存储同时充当渲染目标与工作暂存区。ProjectiveFallback::borrowed(&mut bytes) 借用调用方持有的固定缓冲区;ProjectiveFallback::new(bytes) 则接管缓冲区所有权。图形与图像请求利用投影后的视觉边界限制所需存储空间;字形请求则使用投影后的墨迹边界。预检阶段若发现未配置回退缓冲或容量不足,会在实际绘制前直接拒绝。不支持的透视绘制命令仍按错误处理。Gallery 在 Web Canvas 与 SDL GPU 上为此目标预留了 2 MiB 空间。完整的路由规则、数值域与对齐内存契约请参阅精确后端回退。

存储与失效机制 ​

  • PathId 是带代际(generational)的句柄;移除路径会使残留的旧句柄立即失效。
  • 静态条目直接借用命令切片,并拒绝任何编辑操作。
  • 可变条目拥有或接管其命令存储,并维护单调递增的修订号。
  • 文本实体会自动订阅其 TextPath 组件所指向的路径。
  • 基线采样、带姿态字形、光标坐标架、墨迹边界与命中测试均按路径身份与修订号缓存。
  • 编辑路径会重算受影响的行可用长度,并通过同一个路径修订号使字形排布失效。
  • 未挂载 TextPath 的普通文本始终留在常规的线性整形与渲染快路径上。

请在路径注册表尚为空时设置活跃路径上限。修改该上限会保留已退役 ID 的代际计数,且不会释放槽位元数据:

rust
app.with_path_capacity(24)?;