Skip to content

fix(measure-text): apply theme font in compose and lazy-register 851tegakizatsu - #210

Merged
Aarebecca merged 4 commits into
antvis:mainfrom
xxMudCloudxx:fix/measure-text
Feb 26, 2026
Merged

Aarebecca merged 4 commits into
antvis:mainfrom
xxMudCloudxx:fix/measure-text

Conversation

@xxMudCloudxx

@xxMudCloudxx xxMudCloudxx commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

背景 (Background)

在处理文本渲染和尺寸测量的过程中,当前逻辑未能在组合(Compose)阶段正确应用和传递主题配置中的自定义字体参数(如 themeConfig.base.text['font-family']),导致渲染过程中基于默认字体的宽度测量出现偏差。另外,针对额外引用的特殊字体(如 851tegakizatsu),在 SSR 等非浏览器环境中需要其字形数据进行降级测量,但在需要时未能自动注册。

改动详情 (Change Details)

  1. 更新组合渲染逻辑 (src/runtime/Infographic.tsx): 在执行 renderSVG 阶段之前,依据 themeConfig 读取相应的字体配置并使用 setDefaultFont 变更为当前的默认测量字体,等渲染与测量结束后,再撤销为原默认字体。确保了渲染流与测算端字体指标严格一致。
  2. 实现测算字体懒注册 (src/utils/measure-text.ts): 添加了额外的按需注册机制 ensureMeasuryFont。在每次执行文本测量前,提取实际所用 fontFamily (例如过滤 fallback 序列中的主字体),判断其如果在预定义列表 EXTRA_MEASURY_FONTS 中且未缓存过,则直接将其字形数据注入到 measury,保证准确测量。
  3. 完善单元测试用例 (tests/unit/utils/measure-text.test.ts): 补充了针对上述修改边界情况的测试用例。尤其是针对 SSR 场景拦截断言,确保 measury 依赖方法(registerFont)可被正常触发。

close issure: #209

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xxMudCloudxx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决文本渲染和测量中的字体一致性问题。它确保了在SVG生成过程中,主题中定义的自定义字体能够被正确应用于文本测量,从而避免了因字体不匹配导致的测量偏差。同时,为了支持在服务器端渲染(SSR)环境下对特定外部字体的准确测量,引入了一种按需加载和注册字体字形数据的机制,提升了系统的健壮性和准确性。

Highlights

  • 主题字体应用: 在SVG组合渲染阶段,根据主题配置(themeConfig.base.text['font-family'])设置默认测量字体,确保文本测量与渲染字体一致,并在完成后恢复原默认字体。
  • 字体懒注册机制: 引入ensureMeasuryFont函数,为851tegakizatsu等特殊字体实现按需注册。在SSR等非浏览器环境中,当需要测量这些字体时,自动注入其字形数据以保证测量准确性。
  • 单元测试增强: 补充了针对字体懒注册机制的单元测试用例,特别是模拟SSR环境,验证measury库的registerFont方法被正确触发。
Changelog
  • tests/unit/utils/measure-text.test.ts
    • 更新了measury库的registerFont方法的mock实现,以便在测试中进行跟踪。
    • 为851tegakizatsu-Regular字体添加了mock,以支持其在测试中的行为。
    • 新增了一个测试用例,验证在SSR路径下,当font-family堆栈中包含851tegakizatsu时,该字体能够被懒注册。
  • src/runtime/Infographic.tsx
    • 从../renderer导入了DEFAULT_FONT和setDefaultFont。
    • 修改了compose方法,在执行renderSVG之前,根据themeConfig设置默认字体,并在finally块中恢复原始默认字体,以确保测量准确性。
  • src/utils/measure-text.ts
    • 导入了measury/fonts/851tegakizatsu-Regular字体数据。
    • 导入了decodeFontFamily工具函数。
    • 定义了EXTRA_MEASURY_FONTS常量和registeredMeasuryFonts集合,用于管理需要懒注册的额外字体。
    • 实现了ensureMeasuryFont函数,用于解析字体栈并按需注册特定字体。
    • 在measureText函数内部调用了ensureMeasuryFont,以确保在文本测量前注册所需字体。
Activity
  • 目前没有与此拉取请求相关的评论、审查或活动记录。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces improvements to text measurement by correctly applying theme fonts in the Compose stage and implementing lazy registration for special fonts like '851tegakizatsu' in SSR environments. The changes enhance rendering accuracy and robustness, especially in non-browser contexts. The unit tests have been updated to cover these new functionalities, ensuring the measury dependency's registerFont method is triggered as expected.

Comment thread src/runtime/Infographic.tsx Resolved
Comment thread src/runtime/Infographic.tsx Resolved
Comment thread src/runtime/Infographic.tsx Resolved
Comment thread src/utils/measure-text.ts Resolved
Comment thread src/utils/measure-text.ts Resolved
Comment thread src/utils/measure-text.ts Resolved
Comment thread src/utils/measure-text.ts Resolved
Comment thread src/utils/measure-text.ts Resolved
@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

ai幻觉有点严重,这12条没有什么实际的建议,其中5-12全部是正面评价("is good"、"is correct"、"is excellent"),不是问题。

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

测试未通过的原因是:
之前 SSR 的 measury 没有注册 851tegakizatsu,所以会回退到 fallback 字体(基于 Arial 的估算),拉丁字符宽度相对合理,标题不会换行。

这次我把 851tegakizatsu 注册进 measury 后,测量逻辑改为使用该字体的 glyph 数据,但这份字体数据里 所有拉丁字符都被标成 512/1024 的半角宽度,与实际渲染不符。

因此 Customer Growth Engine 的测量结果被压缩为 264px,最终宽度 267px(×1.01 后取整),导致容器过窄,Engine 在 SSR 输出时被强制换行。

@xxMudCloudxx

xxMudCloudxx commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor Author

针对上面测试未通过的问题,这里存在一个策略权衡:

  1. 策略性回退:不用懒注册机制了,统一走基于 $Arial$ 的 Fallback 逻辑,逻辑更简洁但精度略低。
  2. 数据同步:更新 measury 包中关于 851tegakizatsu 的度量参数,确保排版精确。

请问哪种策略更加符合预期? @Aarebecca

@Aarebecca

Copy link
Copy Markdown
Contributor

针对上面测试未通过的问题,这里存在一个策略权衡:

  1. 策略性回退:禁用懒注册机制,统一走基于
    A
    r
    i
    a
    l
    的 Fallback 逻辑,逻辑更简洁但精度略低。
  2. 数据同步:更新 measury 包中关于 851tegakizatsu 的度量参数,确保排版精确。

请问哪种策略更加符合预期?

我验证了 measury 包中 851tegakizatsu 的测量结果,是准确的,并没有出现显著差异

image image

可以提供下你遇到的出现差异的内容吗

@xxMudCloudxx

xxMudCloudxx commented Feb 25, 2026 •

Copy link
Copy Markdown
Contributor Author

奇怪我再查一下为什么懒注册了851tegakizatsu后ssr渲染会换行,我遇到的问题是Customer Growth Engine中Engine会因为宽度不够导致换行(对应ssr测试11)

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

我想岔了,我之前一直在想为什么这个svg的Engine在svg中会换行,在浏览器上渲染又正常,因为svg默认字体的宽度和851tegakizatsu的不一样,之前不会换行的原因是测出来的够宽够用。。。。
image

@Aarebecca

Copy link
Copy Markdown
Contributor

奇怪我再查一下为什么懒注册了851tegakizatsu后ssr渲染会换行,我遇到的问题是Customer Growth Engine中Engine会因为宽度不够导致换行(对应ssr测试11)

非嵌入模式下,SSR 渲染的 SVG ,字体是通过 xml-stylesheet 嵌入的
嵌入模式下,字体数据是通过 font-face 注册的

需要确认下你的渲染环境是否支持

@codecov-commenter

codecov-commenter commented Feb 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.21%. Comparing base (1a3662e) to head (45364f4).

Files with missing lines Patch % Lines
src/runtime/Infographic.tsx 91.30% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #210      +/-   ##
==========================================
+ Coverage   46.14%   46.21%   +0.07%     
==========================================
  Files         341      341              
  Lines       27930    27964      +34     
  Branches     2282     2289       +7     
==========================================
+ Hits        12887    12923      +36     
+ Misses      15031    15029       -2     
  Partials       12       12              
Flag Coverage Δ
infographic 46.21% <96.15%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/utils/measure-text.ts 86.89% <100.00%> (+2.99%) ⬆️
src/runtime/Infographic.tsx 75.72% <91.30%> (+1.02%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@Aarebecca

Copy link
Copy Markdown
Contributor

@xxMudCloudxx 建议 rebase 一下 main 分支代码,另外有测试用例需要更新 SVG

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

@xxMudCloudxx 建议 rebase 一下 main 分支代码,另外有测试用例需要更新 SVG

@Aarebecca 我是应该直接rebase main分支后push -f一下,还是直接把你刚刚那推送的3个新commit拉下来。测试用例的话,这次修改只涉及到手写体即ssr的测试11,我前面更新过svg了

@Aarebecca

Copy link
Copy Markdown
Contributor

@xxMudCloudxx 建议 rebase 一下 main 分支代码,另外有测试用例需要更新 SVG

@Aarebecca 我是应该直接rebase main分支后push -f一下,还是直接把你刚刚那推送的3个新commit拉下来。测试用例的话,这次修改只涉及到手写体即ssr的测试11,我前面更新过svg了

直接 rebase 后 force push 吧~

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

ok了

@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

#204 那边调整���mearsure-text的FONT_EXTEND_FACTOR,所以pr 204 merge后这边可能会冲突,到时候我再重新更新下 ssr 第11测试的svg

@Aarebecca
Aarebecca merged commit d9b24ef into antvis:main Feb 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants