Skip to content

refactor: unify HTML preview rendering units to pt#22

Merged
goworm merged 1 commit intomainfrom
claude/review-missing-features-gBBYH
Mar 29, 2026
Merged

refactor: unify HTML preview rendering units to pt#22
goworm merged 1 commit intomainfrom
claude/review-missing-features-gBBYH

Conversation

@goworm
Copy link
Copy Markdown
Contributor

@goworm goworm commented Mar 29, 2026

Summary

  • 统一三模块 HTML Preview 渲染单位为 pt(points),消除跨模块精度损失
  • 新增共享 Units 工具类(Core/Units.cs),利用精确整数比(1pt = 20 twips, 1pt = 12700 EMU)实现零损失转换
  • Word: TwipsToPx(twips/1440×96, 有舍入)→ Units.TwipsToPt(twips/20, 精确),涉及 17 处
  • PPT HTML: EmuToCm(EMU/360000, 无理数)→ Units.EmuToPt(EMU/12700, 精确),涉及 36 处
  • Excel: 列宽/行高/冻结窗格偏移从 px 转为 pt,保持单位一致
  • PPT SVG 模块不改动(viewBox 是无单位坐标空间)
  • 新增 18 个 UnitsTests 单元测试验证转换精度

改动背景

之前三个模块各自使用不同的 CSS 单位:

模块 旧单位 新单位 精度提升
Word 段落间距/缩进 px(twips/1440*96, Round 1位) pt(twips/20, 精确) 消除浮点舍入
PPT shape 定位 cm(EMU/360000, Round 3位) pt(EMU/12700, 精确整除) 消除无理数截断
Excel 列宽 px(width*7.5+5, 魔数) pt(width*5.625+3.75 单位体系统一

涉及文件(12个)

新增:

  • src/officecli/Core/Units.cs
  • tests/.../UnitsTests.cs

修改:

  • Word: WordHandler.HtmlPreview.Css.cs, WordHandler.HtmlPreview.Tables.cs
  • PPT: PowerPointHandler.HtmlPreview.cs, .Shapes.cs, .Tables.cs, .Charts.cs, .Text.cs, .Css.cs
  • Excel: ExcelHandler.HtmlPreview.cs, ExcelHandler.HtmlPreview.Charts.cs

Test plan

  • 静态验证:所有旧函数引用(TwipsToPx, EmuToCm)已清除,零残留
  • 新增 18 个 UnitsTests 覆盖关键转换精度
  • CI 构建通过
  • 使用真实文档对比改动前后 view html 截图,确认无 >2px 布局偏移

Relates to

Closes #21

https://claude.ai/code/session_019rKwH4ZHPkkykhRauA7wP7

Replace mixed CSS units (px, cm) with pt (points) as the standard
rendering unit for Word, PowerPoint, and Excel HTML previews.

Key changes:
- Add shared Units utility class (Core/Units.cs) with TwipsToPt and
  EmuToPt conversions that leverage exact integer ratios (1pt = 20
  twips, 1pt = 12700 EMU) for zero-precision-loss conversions
- Word: replace TwipsToPx (twips/1440*96, lossy) with TwipsToPt
  (twips/20, exact) across 17 call sites
- PowerPoint HTML: replace EmuToCm (EMU/360000, irrational) with
  EmuToPt (EMU/12700, exact) across 36 call sites
- Excel: convert column widths, row heights, and frozen pane offsets
  from px to pt for unit consistency
- PPT SVG module intentionally unchanged (viewBox is unitless)
- Add UnitsTests with precision validation for all conversions

https://claude.ai/code/session_019rKwH4ZHPkkykhRauA7wP7
var max = (int)(col.Max?.Value ?? (uint)min);
// Hidden columns get width 0
var widthPx = col.Hidden?.Value == true ? 0 : (col.Width.Value == 0 ? 0 : col.Width.Value * 7.5 + 5);
var widthPt = col.Hidden?.Value == true ? 0 : (col.Width.Value == 0 ? 0 : col.Width.Value * 5.625 + 3.75);
@goworm goworm merged commit 6de15ed into main Mar 29, 2026
8 checks passed
goworm added a commit that referenced this pull request Mar 29, 2026
…gBBYH

refactor: unify HTML preview rendering units to pt
goworm added a commit that referenced this pull request Apr 2, 2026
…gBBYH

refactor: unify HTML preview rendering units to pt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] HTML Preview 渲染单位统一为 pt — 消除跨模块精度损失

2 participants