1687a9c05b0c
write.rs
use super::read::{map_rel_ids, vml_block_max};
use super::*;
// ---------------------------------------------------------------- writing
/// Names given to kept parts while a package is written.
#[derive(Default)]
pub struct KeptOut {
used: HashSet<String>,
n: u32,
/// The highest VML shape block a kept header and footer drawing uses: notes go after it.
vml_blocks: u32,
/// Package-root relationships (type, target, external), added when `_rels/.rels` is written.
root: Vec<(String, String, bool)>,
}
impl KeptOut {
/// Reserves a part name GridCraft writes itself, so kept parts don't take it. False when it is
/// taken.
pub fn reserve(&mut self, name: &str) -> bool {
self.used.insert(name.to_ascii_lowercase())
}
/// Reserves the VML shape blocks the kept header and footer drawings of `wb` use, so that the
/// notes drawings GridCraft writes don't reuse their shape ids.
pub fn reserve_vml_blocks(&mut self, wb: &Workbook) {
let vmls =
wb.sheets.iter().filter_map(|s| s.preserved.as_deref()).flat_map(|p| &p.parts).filter(|q| q.content_type.eq_ignore_ascii_case(CT_VML));
let max = vmls.map(|q| vml_block_max(&String::from_utf8_lossy(&q.data))).max().unwrap_or(0);
self.vml_blocks = u32::try_from(max).unwrap_or(u32::MAX);
}
/// The shape block of the `n`th notes drawing.
pub fn notes_block(&self, n: u32) -> u32 {
n.saturating_add(self.vml_blocks)
}
/// A part name for a kept part: its original name when that can't collide with anything
/// written, otherwise a fresh one in the same folder.
pub(super) fn claim(&mut self, original: &str) -> String {
let lower = original.to_ascii_lowercase();
let own = OWN_DIRS.iter().any(|d| lower.starts_with(d));
if own && self.used.insert(lower) {
return original.to_string();
}
let (dir, file) = package::split_dir(original);
let (stem, ext) = match file.rsplit_once('.') {
Some((s, e)) => (s, format!(".{e}")),
None => (file, String::new()),
};
let stem = if own { format!("{stem}_") } else { "kept".to_string() };
loop {
self.n = self.n.saturating_add(1);
let file = format!("{stem}{}{ext}", self.n);
let cand = if dir.is_empty() { file } else { format!("{dir}/{file}") };
if self.used.insert(cand.to_ascii_lowercase()) || self.n == u32::MAX {
return cand;
}
}
}
}
/// Original part name (normalized) → written part name.
pub(super) type NameMap = HashMap<String, String>;
fn rel_target(r: &PreservedRel, names: &NameMap) -> Option<String> {
if r.external { Some(r.target.clone()) } else { names.get(&normalize(&r.target)).map(|n| format!("/{n}")) }
}
/// A kept part's relationships, ids unchanged, targets renamed.
pub(super) fn part_rels_xml(rels: &[PreservedRel], names: &NameMap) -> String {
let mut s = format!("{XML_DECL}<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">");
for r in rels {
let Some(target) = rel_target(r, names) else { continue };
let _ = write!(
s,
"<Relationship Id=\"{}\" Type=\"{}\" Target=\"{}\"{}/>",
esc_attr(&r.id),
esc_attr(&r.rel_type),
esc_attr(&target),
if r.external { " TargetMode=\"External\"" } else { "" }
);
}
s.push_str("</Relationships>");
s
}
pub(super) fn write_parts(out: &mut Out, parts: &[PreservedPart]) -> NameMap {
let mut names = NameMap::new();
for p in parts {
names.entry(normalize(&p.name)).or_insert_with(|| out.kept.claim(&p.name));
}
let mut written = HashSet::new();
for p in parts {
let Some(new) = names.get(&normalize(&p.name)).cloned() else { continue };
if !written.insert(new.clone()) {
continue;
}
out.part(&new, Some(&p.content_type), p.data.to_vec());
if !p.rels.is_empty() {
out.part(&rels_part_name(&new), None, part_rels_xml(&p.rels, &names).into_bytes());
}
}
names
}
/// Adds the owner's relationships; returns old id → new id.
pub(super) fn attach(rels: &mut Rels, list: &[PreservedRel], names: &NameMap) -> HashMap<String, String> {
let mut ids = HashMap::new();
for r in list {
if let Some(target) = rel_target(r, names) {
let id = rels.add_raw(&r.rel_type, &target, r.external);
ids.insert(r.id.clone(), id);
}
}
ids
}
pub(super) fn remap(x: &str, ids: &HashMap<String, String>) -> Option<String> {
map_rel_ids(x, &mut |id| ids.get(id).cloned())
}
/// A kept chart sheet being written: its `<sheet>` entry's fields and its new relationship id.
pub struct KeptSheet {
before: usize,
name: String,
visibility: Visibility,
rid: String,
sheet_id: u32,
}
/// The `sheetId`s of the written sheets: each keeps the one the file gave it, unless another
/// sheet took it first (a copied sheet) or it has none (a new sheet), and those get the next id
/// that no sheet asks for.
pub struct SheetIds {
wanted: HashSet<u32>,
given: HashSet<u32>,
next: u32,
}
impl SheetIds {
pub fn new(wb: &Workbook, kept: &WorkbookKept) -> SheetIds {
let wanted: HashSet<u32> = wb.sheets.iter().map(|s| s.file_id).chain(kept.sheets.iter().map(|s| s.sheet_id)).filter(|&id| id != 0).collect();
SheetIds { wanted, given: HashSet::new(), next: 1 }
}
pub fn take(&mut self, wanted: u32) -> u32 {
if wanted != 0 && self.given.insert(wanted) {
return wanted;
}
while self.wanted.contains(&self.next) || self.given.contains(&self.next) {
self.next += 1;
}
self.given.insert(self.next);
self.next
}
}
/// The workbook's kept content as written.
#[derive(Default)]
pub struct WorkbookKept {
/// Workbook XML elements that go right after `<sheets>` (`<externalReferences>`).
pub elements: String,
sheets: Vec<KeptSheet>,
}
impl WorkbookKept {
/// Where the model's sheet `i` is in the file's sheet list: chart sheets come between worksheets.
pub fn file_index(&self, i: usize) -> usize {
i + self.sheets.iter().filter(|s| s.before <= i).count()
}
/// The `<sheet>` entries of the chart sheets that go right before the model's sheet `i`
/// (`i` = the number of sheets for those that come last), with their ids from `ids`.
pub fn sheet_entries(&self, i: usize, last: bool, ids: &mut SheetIds) -> String {
let mut x = String::new();
for s in self.sheets.iter().filter(|s| if last { s.before >= i } else { s.before == i }) {
let state = match s.visibility {
Visibility::Visible => "",
Visibility::Hidden => " state=\"hidden\"",
Visibility::VeryHidden => " state=\"veryHidden\"",
};
let name: String = s.name.chars().take(31).collect();
let id = ids.take(s.sheet_id);
let _ = write!(x, "<sheet name=\"{}\" sheetId=\"{id}\"{state} r:id=\"{}\"/>", esc_attr(&name), esc_attr(&s.rid));
}
x
}
}
/// The part names reachable from `root` through kept parts' relationships (normalized).
fn reachable(p: &Preserved, root: &PreservedRel) -> HashSet<String> {
let by_name: HashMap<String, &PreservedPart> = p.parts.iter().map(|q| (normalize(&q.name), q)).collect();
let mut seen = HashSet::new();
let mut queue = VecDeque::from([normalize(&root.target)]);
while let Some(n) = queue.pop_front() {
if !seen.insert(n.clone()) {
continue;
}
if let Some(q) = by_name.get(&n) {
queue.extend(q.rels.iter().filter(|r| !r.external).map(|r| normalize(&r.target)));
}
}
seen
}
/// Every formula (`<c:f>`) of a chart part, depth first.
fn chart_formulas(el: &xml::El, out: &mut Vec<String>) {
if el.name == "f" && !el.text.trim().is_empty() {
out.push(el.text.trim().to_string());
}
for c in &el.children {
chart_formulas(c, out);
}
}
/// The sheet names a formula such as `'My Sheet'!$A$1:$A$9,Data!$B$1` refers to. References to
/// other workbooks (`[1]Sheet!A1`) are not sheets of this one and are left out.
pub(crate) fn sheet_names_in(formula: &str) -> Vec<String> {
let mut names = vec![];
let chars: Vec<char> = formula.chars().collect();
let at = |i: usize| chars.get(i).copied();
let mut i = 0;
while let Some(c) = at(i) {
let mut name = String::new();
let start = i;
if c == '\'' {
i += 1;
while let Some(c) = at(i) {
if c == '\'' {
if at(i + 1) == Some('\'') {
name.push('\'');
i += 2;
continue;
}
break;
}
name.push(c);
i += 1;
}
i += 1;
} else {
while let Some(c) = at(i).filter(|c| !matches!(c, '!' | ',' | '(' | ')' | '\'' | ' ')) {
name.push(c);
i += 1;
}
}
if at(i) == Some('!') {
if !name.is_empty() && !name.starts_with('[') {
names.push(name);
}
i += 1;
} else if i == start {
i += 1;
}
}
names
}
/// Whether a kept chart sheet can be written back: its chart's series must still refer to sheets
/// that exist under the same names (the chart is kept as it was, so a series on a deleted or
/// renamed sheet would be left pointing at nothing), and no worksheet may have taken its name.
///
/// `usable` caches, by the normalized target of the chart sheet's relationship, whether its graph
/// refers only to sheets that exist (`false` when the relationship is missing): entries sharing one
/// graph, under the same or different relationship ids, are analysed once, and only the verdict is
/// kept, not the references it came from.
fn chart_sheet_usable(wb: &Workbook, p: &Preserved, s: &PreservedSheet, usable: &mut HashMap<String, bool>) -> bool {
let same = |a: &str, b: &str| a.chars().take(31).collect::<String>().eq_ignore_ascii_case(&b.chars().take(31).collect::<String>());
if wb.sheets.iter().any(|w| same(&w.name, &s.name)) {
return false;
}
let Some(root) = p.rels.iter().find(|r| r.id == s.rel_id) else { return false };
*usable.entry(normalize(&root.target)).or_insert_with(|| {
let parts = reachable(p, root);
let mut formulas = vec![];
for q in p.parts.iter().filter(|q| parts.contains(&normalize(&q.name)) && q.content_type.contains("chart")) {
if let Ok(x) = xml::parse(&q.data) {
chart_formulas(&x, &mut formulas);
}
}
formulas.iter().flat_map(|f| sheet_names_in(f)).all(|n| wb.sheets.iter().any(|w| same(&w.name, &n)))
})
}
/// Writes the workbook's kept parts and relationships. Chart sheets that can't be written back
/// (see [`chart_sheet_usable`]) are left out with the parts only they reach.
pub fn write_workbook(wb: &Workbook, wb_rels: &mut Rels, out: &mut Out) -> WorkbookKept {
let Some(p) = wb.preserved.as_deref() else { return WorkbookKept::default() };
let (live, dropped): (Vec<&PreservedSheet>, Vec<&PreservedSheet>) = {
let mut refs = HashMap::new();
p.sheets.iter().partition(|s| chart_sheet_usable(wb, p, s, &mut refs))
};
let root_of = |s: &PreservedSheet| p.rels.iter().find(|r| r.id == s.rel_id);
let dropped_ids: HashSet<&str> = dropped.iter().map(|s| s.rel_id.as_str()).collect();
// Parts reached only by dropped chart sheets go with them.
let mut gone: HashSet<String> = dropped.iter().filter_map(|s| root_of(s)).flat_map(|r| reachable(p, r)).collect();
if !gone.is_empty() {
let others = p.rels.iter().filter(|r| !dropped_ids.contains(r.id.as_str())).flat_map(|r| reachable(p, r));
for n in others {
gone.remove(&n);
}
}
let parts: Vec<PreservedPart> = p.parts.iter().filter(|q| !gone.contains(&normalize(&q.name))).cloned().collect();
let rels: Vec<PreservedRel> = p.rels.iter().filter(|r| !dropped_ids.contains(r.id.as_str())).cloned().collect();
let names = write_parts(out, &parts);
let ids = attach(wb_rels, &rels, &names);
for r in &p.package_rels {
if let Some(target) = rel_target(r, &names) {
out.kept.root.push((r.rel_type.clone(), target, r.external));
}
}
WorkbookKept {
elements: p.elements.iter().filter(|e| e.slot == "externalReferences").filter_map(|e| remap(&e.xml, &ids)).collect(),
sheets: live
.iter()
.filter_map(|s| {
Some(KeptSheet {
before: s.before,
name: s.name.clone(),
visibility: s.visibility,
rid: ids.get(&s.rel_id)?.clone(),
sheet_id: s.sheet_id,
})
})
.collect(),
}
}
/// The workbook's kept elements of `slot` that refer to no part (`fileVersion`, `ext`).
pub fn workbook_elements(wb: &Workbook, slot: &str) -> String {
let Some(p) = wb.preserved.as_deref() else { return String::new() };
p.elements.iter().filter(|e| e.slot == slot).map(|e| e.xml.as_str()).collect()
}
/// Adds the kept package-root relationships (custom properties…) to `_rels/.rels`.
pub fn write_package_rels(root: &mut Rels, out: &mut Out) {
for (ty, target, external) in std::mem::take(&mut out.kept.root) {
root.add_raw(&ty, &target, external);
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.