Skip to main content

Module text

Module text 

Source
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
AtlasEntry
单个 glyph 在 atlas 中的位置
AtlasSnapshot
Atlas 当前状态快照
AtlasStats
atlas 汇总(Inspector/诊断用;不拷贝像素)
FontState
文字渲染所需全局状态 — parley + swash 版
GlyphKey
InlineItem
内联项:固定尺寸、底部对齐到文本基线,绘制由调用方回调完成(绘制型,无命中/状态)。
RichText
预处理后的富文本:拼接文本 + 文本 span 的字节区间 + 内联项(一次构造,多次测量/绘制)。
RichTextLayout
富文本布局结果:整体尺寸 + 每个内联项的相对矩形(逻辑像素,原点 = 文本视觉左上)。
RichTextSpan
富文本样式 span。颜色只影响绘制,不参与 shape 缓存 key。
TextRenderObject

Enums§

RichItem
富文本 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_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§

InlinePainter
内联项绘制回调(逻辑坐标 rect,相对 RichText 原点)。