Expand description
平台无关文本服务(实现在 mgui-text)
TextRenderObject + parley 富文本布局 + swash 栅格化 + 字形图集
阶段 4-P(parley):
- 用
parley::FontContext+LayoutContext替代 cosmic-text 的FontSystem+Buffer swash::scale::ScaleContext栅格化(re-exported by parley)- R8 灰度 glyph atlas,按行打包,key = 自造 GlyphKey
- paint 时按 glyph 提交
PaintCommand::TexturedRect
设计取舍:
- 整个 UI 层是单线程的(refs + Rc),不需要 Mutex 包裹 FontState
parley::FontContext不是Sync,所以用thread_local!是最自然的做法- 公共 API 不变:measure_text_width / text_visual_height / paint_text_line 签名与行为保持一致
Structs§
- Atlas
- 简单的行式(Row-based)glyph atlas
- Atlas
Entry - 单个 glyph 在 atlas 中的位置
- Atlas
Snapshot - Atlas 当前状态快照
- Atlas
Stats - atlas 汇总(Inspector/诊断用;不拷贝像素)
- Font
State - 文字渲染所需全局状态 — parley + swash 版
- Glyph
Key - Inline
Item - 内联项:固定尺寸、底部对齐到文本基线,绘制由调用方回调完成(绘制型,无命中/状态)。
- Rich
Text - 预处理后的富文本:拼接文本 + 文本 span 的字节区间 + 内联项(一次构造,多次测量/绘制)。
- Rich
Text Layout - 富文本布局结果:整体尺寸 + 每个内联项的相对矩形(逻辑像素,原点 = 文本视觉左上)。
- Rich
Text Span - 富文本样式 span。颜色只影响绘制,不参与 shape 缓存 key。
- Text
Render Object
Enums§
- Rich
Item - 富文本 item:文本 span 或内联绘制项。
Constants§
- DEFAULT_
FONT_ STACK - 默认字体栈(CSS 格式):覆盖特殊符号、CJK、emoji 的 fallback 链。
旧版文本 API 的隐式默认值;
*_styled版 API 可显式传入自定义字体栈 (代码编辑器用等宽字体栈,UI 组件沿用此默认)。
Functions§
- atlas_
clear_ dirty - 清 atlas dirty 标记(GPU 上传后调用)
- atlas_
generation - atlas 内容版本(写 glyph/扩容/清空时递增,与 GPU dirty 独立)
- atlas_
snapshot - 拿到 atlas 快照
- atlas_
stats - is_
rtl_ text - 文本主方向是否 RTL(首个强方向字符决定;无强方向字符按 LTR)。
- measure_
caret_ positions - 方向感知的光标 x 表(逻辑字符边界,长度 = char_count + 1,单位逻辑像素)。
- measure_
char_ prefix_ widths - measure_
text_ width - 阶段 2-E:测一段文本的宽度(shaping 后,逻辑像素)
- measure_
text_ width_ styled measure_text_width的字体栈参数化版本(编辑器等宽字体栈用)- mirror_
carets_ for_ rtl - 把 LTR 前缀宽度表镜像为 RTL 光标表(纯函数,便于测试)。
- nearest_
caret_ index - 光标表中离
x最近的逻辑位置(LTR/RTL 通用;距离相同取较小下标)。 - paint_
rich_ text - 绘制富文本,返回最大行宽(逻辑像素)。
- paint_
rich_ text_ with_ default - 与
paint_rich_text相同,但未显式设色的 span 使用default_color。 - paint_
text_ line - 阶段 2-E:画一行/多行文字(glyph → scale → atlas → TexturedRect)
- paint_
text_ line_ styled paint_text_line的字体栈参数化版本(编辑器语法着色逐 token 绘制用)- paint_
text_ span - 共享整串 + 可选字节区间绘制(编辑器逐语法 span 复用整行
Rc<str>)。 - rich_
text_ layout - 仅布局:供交互型内联 Widget 定位子元素(不绘制)。
- rich_
text_ size - 测量富文本(逻辑像素)。
max_width= soft wrap 宽度上限。 - set_
text_ dpr - 设置文字栅格化 DPR(在 CPU paint 之前调用;≤0 视为 1.0)
- text_
ascent - 文本首行字体 ascent(逻辑像素)。
- text_
span_ ascent - 共享整串 + 区间的首行字体 ascent(逻辑像素)。
与
paint_text_span使用同一 shape 缓存 key,不产生额外 shaping。 - text_
visual_ height - 单行文本真实视觉高度(parley 版)
- text_
visual_ height_ styled text_visual_height的字体栈参数化版本- with_
atlas - CPU 侧直接访问 atlas 像素(R8,行优先,左上原点,零拷贝)。
- with_
font - 在全局 FontState 上执行操作(惰性初始化)
- wrapped_
line_ ranges - 测单行文本每个字符的起始 x(前缀宽度,逻辑像素)
Type Aliases§
- Inline
Painter - 内联项绘制回调(逻辑坐标 rect,相对 RichText 原点)。