Skip to main content

Module units

Module units 

Source
Expand description

单位、基本类型与颜色。

本模块集中管理 pptx-rs 中所有“非 OOXML 强类型但又频繁使用“的小型数据载体。 它是 crate::lib 中“高阶 API 层“与“OPC / OOXML 模型层“之间的语义桥梁。

§PowerPoint 的几何单位

OOXML 中所有几何属性(<a:off> / <a:ext> / 行高 / 列宽 / 边框 / 缩进)都是 EMU(English Metric Unit) 整数。换算关系:

单位EMU备注
1 inch914 400 EMU1 in = 914 400
1 cm360 000 EMU1 cm = 360 000
1 pt12 700 EMU1 pt = 12 700
1 sp6 350 EMU1 pt = 20 sp(OpenType 子像素)

库内统一以 i64 形式的 EMU 作内部表示,理由:

  1. 无浮点漂移:在缩放 / 旋转 / 多次叠加时仍能保持整型精度。
  2. 互操作零成本:与 python-pptx / Open XML SDK / LibreOffice 完全一致。
  3. 可读性:用户阅读 Emu(914400)Inches(1.0) 时是等价的。

§类型分层

  • Emu:绝对内部单位,整数 NewType。
  • Pt / Inches / Cm:外部友好单位,浮点 NewType。
  • EmuPoint:2D 点(x, y),以 EMU 整数存储,主要给连接器/手绘 freeform 用。
  • EmuExt:扩展 trait,统一从 f64 / f32 / i32 / i64 / Pt / Inches / Cm 转换到 Emu
  • RGBColor:sRGB 颜色三字节 0-255,对应 OOXML <a:srgbClr val="RRGGBB"/>

§设计哲学

  • 不暴露 f64 形参:高阶 API 的“位置 / 大小“参数应接受 NewType 或显式单位 转换。例如 add_textbox(Inches(1.0), Inches(1.0), Inches(8.0), Inches(1.0)), 让“单位混用“在编译期就被拒绝。
  • 浮点语义f64EmuExt 中默认解释为“英寸“——这是最常用的隐含单位; 若需其它语义请显式包装 Pt(...) / Cm(...)
  • 颜色不归此模块管:主题色(schemeClr)、系统色(sysClr)、preset 颜色 见 crate::oxml::color

§与 python-pptx 的对应

  • pptx.util.Pt / pptx.util.Inches / pptx.util.Cm / pptx.util.Emu ←→ 同名 NewType。
  • pptx.util.Length.x / .y 抽象 → 本库通过 EmuPoint 暴露。
  • pptx.dml.color.RGBColor ←→ RGBColor(构造时 (r, g, b) 元组也支持)。

§示例

use pptx_rs::{Emu, Inches, Pt, Cm, EmuExt, RGBColor};

// 隐式单位转换
let w: Emu = Inches(8.5).emu();
assert_eq!(w.value(), (8.5 * 914_400.0) as i64);

// 跨单位混算
let h = Pt(72.0).emu() + Inches(1.0).emu();

// 颜色
let red: RGBColor = (255, 0, 0).into();
assert_eq!(red, RGBColor::RED);

§实现说明

本模块自 v0.5.0 起改为 re-export ooxml_core::units 的内容。 类型与 API 完全保持不变,仅是把实现下沉到 ooxml-core 公共基座, 便于未来 xlsx-rs / docx-rs 复用同一套单位系统。

Structs§

Cm
厘米:1 cm = 360000 EMU。
Emu
EMU(English Metric Unit),所有内部几何计算都使用 i64 形式的 EMU。
EmuPoint
2D 几何点(EMU 整数坐标)。
Inches
英寸:1 inch = 914400 EMU。
Pt
磅:1 pt = 12700 EMU。
RGBColor
sRGB 颜色(三字节 0-255)。

Traits§

EmuExt
单位转换 trait,封装“任意类型 → Emu“的语义。