buildPptx function
Builds a minimal .pptx for tests: just the parts
PptxParser reads - presentation.xml + its rels, the slides + their
rels, and media. slides are slide-tree bodies (p:spTree children), in
presentation order; they're stored in reverse file-name order so a test
can tell p:sldIdLst order from file order. slideSize (EMUs) writes
a p:sldSz.
Implementation
Uint8List buildPptx(
List<String> slides, {
Map<String, List<int>> media = const {},
Map<int, String> slideRels = const {},
(int, int)? slideSize,
}) {
const ns =
'xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" '
'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" '
'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"';
final archive = Archive();
void add(String name, List<int> bytes) => archive.addFile(ArchiveFile.bytes(name, bytes));
void addText(String name, String text) => add(name, utf8.encode(text));
String fileFor(int index) => 'slide${slides.length - index}.xml';
addText(
'ppt/presentation.xml',
'<?xml version="1.0" encoding="UTF-8"?><p:presentation $ns><p:sldIdLst>'
'${[for (var i = 0; i < slides.length; i++) '<p:sldId id="${256 + i}" r:id="rId${i + 1}"/>'].join()}'
'</p:sldIdLst>${slideSize == null ? '' : '<p:sldSz cx="${slideSize.$1}" cy="${slideSize.$2}"/>'}</p:presentation>',
);
addText(
'ppt/_rels/presentation.xml.rels',
'<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
'${[for (var i = 0; i < slides.length; i++) '<Relationship Id="rId${i + 1}" Type="slide" Target="slides/${fileFor(i)}"/>'].join()}'
'</Relationships>',
);
for (var i = 0; i < slides.length; i++) {
addText(
'ppt/slides/${fileFor(i)}',
'<?xml version="1.0" encoding="UTF-8"?><p:sld $ns><p:cSld><p:spTree>${slides[i]}</p:spTree></p:cSld></p:sld>',
);
if (slideRels[i] case final rels?) {
addText(
'ppt/slides/_rels/${fileFor(i)}.rels',
'<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">$rels</Relationships>',
);
}
}
media.forEach((name, bytes) => add('ppt/media/$name', bytes));
return Uint8List.fromList(ZipEncoder().encodeBytes(archive));
}