//! 备注母版 `<p:notesMaster>` 模型(TODO-045)。
//!
//! 备注母版是"主-版-页"三层中的备注页对应母版——所有 notesSlide
//! 默认继承它的形状与样式。对应 python-pptx 中 `NotesMaster` / `NotesMasterPart`。
//!
//! # 与 python-pptx 的对应
//!
//! - `pptx.parts.notesmaster.NotesMasterPart` ←→ 本模块 [`NotesMaster`] 的 OPC 宿主;
//! - `pptx.NotesMaster` ←→ [`crate::notes_masters::NotesMasterRef`] 高阶句柄。
//!
//! # 当前实现范围
//!
//! 本模块支持**读写双向**——`from_opc` 解析已有 .pptx 的 notesMaster,
//! `to_opc_package` 循环写出 notesMasterN.xml part + 关系 + notesMasterIdLst 元素。
//! `clrMap` / `notesStyle` 保留原始 XML 字符串以实现 round-trip 保真。
//!
//! # OOXML 结构
//!
//! ```text
//! <p:notesMaster>
//! <p:cSld>
//! <p:bg>?</p:bg> 可选背景
//! <p:spTree>...</p:spTree> 形状树(含占位符:日期/页码/正文等)
//! </p:cSld>
//! <p:clrMap .../> 颜色映射
//! <p:notesStyle>...</p:notesStyle> 备注文本样式
//! </p:notesMaster>
//! ```
use crate::oxml::shape::Sp;
use crate::oxml::slide::SlideBackground;
use crate::oxml::writer::XmlWriter;
/// 默认 `<p:notesStyle>` 内容(极简版,与 SldMaster 的 TX_STYLES 对齐)。
///
/// 包含 titleStyle / bodyStyle / otherStyle 各 1 个 lvl1pPr,确保 OOXML 校验通过。
/// round-trip 场景下优先使用解析到的原始 notesStyle XML。
const DEFAULT_NOTES_STYLE: &str = r##"<p:notesStyle><p:titleStyle><a:lvl1pPr algn="ctr" defTabSz="457200" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1"><a:spcBef><a:spcPct val="0"/></a:spcBef><a:buNone/><a:defRPr sz="4400" kern="1200"><a:solidFill><a:schemeClr val="tx1"/></a:solidFill><a:latin typeface="+mj-lt"/><a:ea typeface="+mj-ea"/><a:cs typeface="+mj-cs"/></a:defRPr></a:lvl1pPr></p:titleStyle><p:bodyStyle><a:lvl1pPr marL="342900" indent="-342900" algn="l" defTabSz="457200" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1"><a:spcBef><a:spcPct val="20000"/></a:spcBef><a:buFont typeface="Arial"/><a:buChar char="•"/><a:defRPr sz="3200" kern="1200"><a:solidFill><a:schemeClr val="tx1"/></a:solidFill><a:latin typeface="+mn-lt"/><a:ea typeface="+mj-ea"/><a:cs typeface="+mj-cs"/></a:defRPr></a:lvl1pPr></p:bodyStyle><p:otherStyle><a:defPPr><a:defRPr lang="en-US"/></a:defPPr><a:lvl1pPr marL="0" algn="l" defTabSz="457200" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1"><a:defRPr sz="1800" kern="1200"><a:solidFill><a:schemeClr val="tx1"/></a:solidFill><a:latin typeface="+mn-lt"/><a:ea typeface="+mj-ea"/><a:cs typeface="+mj-cs"/></a:defRPr></a:lvl1pPr></p:otherStyle></p:notesStyle>"##;
/// 默认 `<p:clrMap>` 内容(与 SldMaster 对齐)。
const DEFAULT_CLR_MAP: &str = "<p:clrMap bg1=\"lt1\" tx1=\"dk1\" bg2=\"lt2\" tx2=\"dk2\" accent1=\"accent1\" accent2=\"accent2\" accent3=\"accent3\" accent4=\"accent4\" accent5=\"accent5\" accent6=\"accent6\" hlink=\"hlink\" folHlink=\"folHlink\"/>";
/// `<p:notesMaster>` 的内存模型。
///
/// 承载 `cSld/spTree` 内的形状列表 + 可选背景 + clrMap/notesStyle 原始 XML。
/// 读写双向支持:`from_opc` 解析时回填,`to_opc_package` 写出时调用 [`NotesMaster::to_xml`]。
#[derive(Clone, Debug, Default)]
pub struct NotesMaster {
/// 备注母版中的形状列表(`<p:cSld>/<p:spTree>` 内的 `<p:sp>`)。
pub shapes: Vec<Sp>,
/// 备注母版背景(`<p:bg>`,可选)。`None` 表示使用默认背景。
pub background: Option<SlideBackground>,
/// 原始 `<p:clrMap>` XML 字符串(round-trip 保真)。
///
/// `None` 时 [`NotesMaster::to_xml`] 使用 [`DEFAULT_CLR_MAP`]。
/// `Some` 时直接 raw 输出原始 XML,确保读→写不漂移。
pub clr_map_xml: Option<String>,
/// 原始 `<p:notesStyle>` XML 字符串(round-trip 保真)。
///
/// `None` 时 [`NotesMaster::to_xml`] 使用 [`DEFAULT_NOTES_STYLE`]。
/// `Some` 时直接 raw 输出原始 XML,确保读→写不漂移。
pub notes_style_xml: Option<String>,
}
impl NotesMaster {
/// 创建一个空的备注母版。
pub fn new() -> Self {
Self::default()
}
/// 形状数量。
pub fn len(&self) -> usize {
self.shapes.len()
}
/// 是否无形状。
pub fn is_empty(&self) -> bool {
self.shapes.is_empty()
}
/// 写出 `<p:notesMaster>` 完整 XML(notesMasterN.xml 内容)。
///
/// # 元素结构(OOXML 严格顺序)
///
/// ```text
/// <p:notesMaster xmlns:a=... xmlns:p=... xmlns:r=...>
/// <p:cSld>
/// <p:bg>?</p:bg> 可选背景
/// <p:spTree>...</p:spTree> 形状树
/// </p:cSld>
/// <p:clrMap .../> 颜色映射(优先用 clr_map_xml,否则默认)
/// <p:notesStyle>...</p:notesStyle> 备注文本样式(优先用 notes_style_xml,否则默认)
/// </p:notesMaster>
/// ```
///
/// # 与 python-pptx 的对应
///
/// python-pptx 不直接生成 notesMaster XML(仅读取),本库为 round-trip 保真实现写路径。
pub fn to_xml(&self) -> String {
let mut xml = String::with_capacity(1024);
xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
xml.push_str(
"<p:notesMaster xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"",
);
xml.push_str(" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\"");
xml.push_str(
" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">",
);
// cSld —— 必须按 <p:bg>? <p:spTree> <p:custDataLst>? <p:controls>? <p:extLst>? 顺序
xml.push_str("<p:cSld>");
// 背景在 spTree 之前
if let Some(bg) = &self.background {
let mut w = XmlWriter::default();
bg.write_xml(&mut w);
xml.push_str(&w.into_string());
}
xml.push_str("<p:spTree>");
// nvGrpSpPr + grpSpPr (spTree 必填项)
xml.push_str(
"<p:nvGrpSpPr><p:cNvPr id=\"1\" name=\"\"/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>",
);
xml.push_str("<p:grpSpPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"0\" cy=\"0\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"0\" cy=\"0\"/></a:xfrm></p:grpSpPr>");
// 用户 shapes
let mut w = XmlWriter::default();
for s in &self.shapes {
s.write_xml(&mut w);
}
xml.push_str(&w.into_string());
xml.push_str("</p:spTree></p:cSld>");
// clrMap —— 优先用解析到的原始 XML,否则用默认
if let Some(clr_map) = &self.clr_map_xml {
xml.push_str(clr_map);
} else {
xml.push_str(DEFAULT_CLR_MAP);
}
// notesStyle —— 优先用解析到的原始 XML,否则用默认
if let Some(notes_style) = &self.notes_style_xml {
xml.push_str(notes_style);
} else {
xml.push_str(DEFAULT_NOTES_STYLE);
}
xml.push_str("</p:notesMaster>");
xml
}
}