For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/unstaged/proposal-intl-segmenter-v2.md.
  • 简体中文
  • Intl.Segmenter v2 ?

    提案概览
    提案速览

    该提案通过为 Unicode 换行添加新的“line”粒度来增强现有的 Intl.Segmenter API,满足需要逻辑换行点的自定义文本渲染和布局应用的需求。它移除了早期的批量模式想法。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    Intl Segmenter v2 提案

    Intl.Segmenter v2:JavaScript 中的 Unicode 分段

    ECMA402 提案的仓库模板。

    您可以浏览 ecmarkup 输出 或浏览 源代码

    阶段 1

    提议的规范文本(以绿色显示)作为 Intl.Segmenter 的差异

    阶段推进幻灯片

    人员

    • 冠军:Frank Yung-Fong Tang
    • 阶段 3 审查员(在推进到阶段 2 时待请求/注册):
      • 待定

    动机

    本提案旨在改进 Intl.Segmenter API:

    • 一种通过批量调用来分段文本的方法,以最小化用户调用和内部实现之间的调用次数。
    • 添加换行粒度以支持高端的基于 ECMAScript 的文本布局应用程序,这些应用已经有测量文本宽度的方法,但需要一种访问逻辑换行点的方式。

    使用场景

    批量模式

    注意:在与一些预期用户仔细讨论后,“批量模式”已从提案中移除。

    换行粒度

    • 来自 @gibson042: 在等宽字体上下文中格式化,例如 CLI 或 GitHub 源代码差异。
    • 在非 HTML 上下文中,当字体度量可用时
    • SVG 中的 <text> 和 https://d3js.org/
    • 避免在需要换行的地方误用单词分割。(Canvas drawstring 和 SVG 用例)
    • 即使在 Intl.Segmenter 发布后,用户仍持续要求
    • 来自 @my2iu: 现在 Web 应用自定义文本渲染越来越普遍,这些 Web 应用需要一个用于行分段的 API,以便能够以换行的方式在行中布局文本。HTML/CSS 过于高层,无法提供许多 Web 应用所需的字体支持,因此需要低层 API。换行库通常相当大,因此直接打包到 Web 应用中也不实际,所以如果浏览器能提供此功能会更好。自定义文本渲染用于:
      • 用于文字处理、矢量插图、绘图程序、SVG 可视化等应用程序 Web 应用,以及任何需要在 WebGL 或 VR/AR(通过 WebXR)中渲染文本的网页。
      • 3D 引擎中的美观文本通常使用有符号距离场实现,这需要自定义文本渲染器
    • 来自 @HackbrettXXX,jsPDF 的贡献者:
      • 创建任何类型的文本文档:例如,使用 jsPDF 生成 PDF:jsPDF 提供自动将文本拆分为行的功能。然而,目前它仅支持西式换行:单词 = text.split(" ")。向 Intl.Segmenter 添加换行可以改进这一点。在 jsPDF 中,通过加载字体文件并根据字形大小计算文本大小来测量文本。
      • 在浏览器中无法使用 HTML/CSS 布局的上下文中渲染多行文本,例如使用 SVG、canvas 或 WebGL 进行渲染时。在这里,可以使用 canvas 的 measureText() 或 SVG 的 getComputedTextLength() 或 getBBox() 来测量文本。
    • 来自 @mdebbar,Flutter Web 的贡献者:

    示例

    d8> s = new Intl.Segmenter("en", {granularity: "line"})
    [object Intl.Segmenter]
    d8> ss = s.segment("飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動")
    {}
    d8> for (s of ss) { print(JSON.stringify(s)) }
    {"segment":"飛","index":0,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"虎","index":1,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"隊","index":2,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"正","index":3,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"式","index":4,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"名","index":5,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"稱","index":6,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"為","index":7,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"「中","index":8,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"華","index":10,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"民","index":11,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"國","index":12,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"空","index":13,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"軍","index":14,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"美","index":15,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"籍","index":16,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"志","index":17,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"願","index":18,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"大","index":19,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"隊」,\n","index":20,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":true}
    {"segment":"1940 ","index":24,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"年","index":29,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"代","index":30,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"早","index":31,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"期","index":32,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"在","index":33,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"緬","index":34,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"甸","index":35,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"展","index":36,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"開","index":37,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"行","index":38,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    {"segment":"動","index":39,"input":"飛虎隊正式名稱為「中華民國空軍美籍志願大隊」,\n1940 年代早期在緬甸展開行動","isHardBreak":false}
    d8> ss = s.segment("The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s")
    {}
    d8> for (s of ss) { print(JSON.stringify(s)) }
    {"segment":"The ","index":0,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Flying ","index":4,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Tigers ","index":11,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"is ","index":18,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"officially ","index":21,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"called ","index":32,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"the ","index":39,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"\"Republic ","index":43,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"of ","index":53,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"China ","index":56,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Air ","index":62,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Force ","index":66,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"American ","index":72,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Volunteer ","index":81,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Brigade\",\n","index":91,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":true}
    {"segment":"and ","index":101,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"it ","index":105,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"launched ","index":108,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"operations ","index":117,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"in ","index":128,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"Myanmar ","index":131,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"in ","index":139,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"the ","index":142,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"early ","index":146,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}
    {"segment":"1940s","index":152,"input":"The Flying Tigers is officially called the \"Republic of China Air Force American Volunteer Brigade\",\nand it launched operations in Myanmar in the early 1940s","isHardBreak":false}

    上述结果基于 v8 原型 CL 的实际输出。

    阶段 1 之前的待办事项

    进入阶段 1 的标准

    • 确定“冠军”将推进该添加 已完成 => Frank Yung-fong Tang
    • 描述问题或需求以及解决方案大致形态的散文
    • 使用示例说明
    • 高级 API
    • 关键算法、抽象和语义的讨论
    • 识别潜在的“跨领域”关注点和实现挑战/复杂性
    • 一个公开可用的提案仓库,满足上述要求 已完成 => 本仓库

    进入阶段 1 的验收意义

    委员会期望投入时间研究问题空间、解决方案和跨领域关注点

    阶段 1 期间的用途

    • 为添加此项提供理由
    • 描述解决方案的形态
    • 识别潜在挑战

    阶段 2 之前的待办事项

    进入阶段 2 的标准

    ...

    进入阶段 2 的验收意义

    ...

    阶段 2 期间的用途

    ...

    TG2 需求分析

    先例(用于阶段 2)

    待写

    在用户域实现成本高(用于阶段 2)

    待写

    广泛吸引力(用于阶段 2)

    待写

    阶段 3 之前的待办事项

    进入阶段 3 的标准

    ...

    进入阶段 3 的验收意义

    ...

    阶段 3 期间的用途

    ...

    TG2 需求分析

    有效载荷缓解(用于阶段 3)

    待写

    仓库待办事项

    1. 通过运行以下命令避免与构建过程输出文件的合并冲突:
      git config --local --add merge.output.driver true
      git config --local --add merge.output.driver true
    2. 添加 post-rewrite git 钩子以在每次提交时自动重建输出:
      cp hooks/post-rewrite .git/hooks/post-rewrite
      chmod +x .git/hooks/post-rewrite
    3. spec.emu 的更改(ecmarkup 使用 HTML 语法,但不是 HTML,因此强烈建议不要将其命名为 ".html")
    4. 任何对规范进行有意义更改的提交都应运行 npm run build 并提交生成的输出。
    5. 每当更新 ecmarkup 时,运行 npm run build 并提交该依赖带来的任何更改。