diff --git a/Cargo.lock b/Cargo.lock index 8c267185e4..e1dd5493d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1238,7 +1238,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf dependencies = [ "base64 0.23.1", "chrono", - "craft-drawingml", + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "craft-math 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "craft-offcrypto", "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", @@ -1251,6 +1251,17 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "kurbo 0.13.1", + "log", + "serde", +] + [[package]] name = "craft-drawingml" version = "0.1.0" @@ -1437,10 +1448,10 @@ dependencies = [ [[package]] name = "craft-opc" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", "log", "thiserror 2.0.21", ] @@ -1448,10 +1459,10 @@ dependencies = [ [[package]] name = "craft-opc" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "log", "thiserror 2.0.21", ] @@ -1507,6 +1518,21 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-pptx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)", + "kurbo 0.13.1", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-prc" version = "0.1.0" @@ -1695,6 +1721,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1744,6 +1780,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -3864,6 +3910,7 @@ dependencies = [ "arrayvec", "euclid", "polycool", + "serde", "smallvec", ] @@ -4995,7 +5042,7 @@ dependencies = [ "craft-docx", "craft-markdown", "craft-mime", - "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", + "craft-pptx", "craft-rtf", "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", diff --git a/Cargo.toml b/Cargo.toml index b16ead7a1f..872461fd2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,8 +48,8 @@ craft-tiff = { git = "https://github.com/storytold/craft-libs", version = "0.1.0 craft-jpeg = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } craft-wordmodel = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" } -craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } -craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "010702ee9b030844c852939095b1da0d0656a890" } +craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "010702ee9b030844c852939095b1da0d0656a890" } craft-opc = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } # internal pdfcraft-geom = { path = "crates/geom", version = "0.21.0" } diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 6f9b030e59..c23ef4ec71 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -13,10 +13,10 @@ workspace = true [dependencies] craft-markdown.workspace = true craft-mime.workspace = true -craft-opc.workspace = true craft-rtf = { workspace = true, features = ["doc"] } craft-wordmodel.workspace = true craft-docx.workspace = true +craft-pptx.workspace = true craft-xml.workspace = true craft-zip.workspace = true diff --git a/crates/export/src/pptx.rs b/crates/export/src/pptx.rs index 59d9f858d9..be66397190 100644 --- a/crates/export/src/pptx.rs +++ b/crates/export/src/pptx.rs @@ -2,11 +2,16 @@ //! largest text as slide title, remaining paragraphs as body text, images and tables between them. use std::collections::HashMap; +use std::sync::Arc; -use crate::as_docx::{core_properties_xml, image_mime, room_for}; +use crate::as_docx::{image_mime, room_for}; use crate::{Image, Page, Run, Table, hex}; -use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut}; -use craft_xml::{DECL_STANDALONE, esc}; +use craft_pptx::table::{Cell, Table as DmlTable}; +use craft_pptx::text::{Paragraph, Run as DmlRun, RunKind, RunProps, TextBody}; +use craft_pptx::{ + ColorBase, ColorRef, Fill, Geom, Line, MediaId, MediaItem, Plain, Presentation, Rgba, Shape, ShapeId, ShapeKind, Size, Slide, SlideId, Xfrm, + style::PictureFill, +}; /// Items on a slide in reading order. enum SlideBodyItem<'a> { @@ -16,102 +21,105 @@ enum SlideBodyItem<'a> { Img(&'a Image), } -/// Convert points to EMU (English Metric Units: 1 pt = 12700 EMU). -/// -/// Clamps to PowerPoint's allowed slide dimensions: 1 inch (914,400 EMU = 72 pt) -/// to 56 inches (51,206,400 EMU = 4032 pt). -fn emu(pt: f64, default_pt: f64) -> i64 { - if pt.is_finite() && pt > 0.0 { (pt * 12700.0).round().clamp(914400.0, 51206400.0) as i64 } else { (default_pt * 12700.0).round() as i64 } +/// Clamp slide dimensions in points to PowerPoint's allowed range: 1 inch (72 pt) to 56 inches (4032 pt). +fn slide_pt(pt: f64, default_pt: f64) -> f64 { + if pt.is_finite() && pt > 0.0 { pt.clamp(72.0, 4032.0) } else { default_pt } } -/// The largest height one item may take on a slide (about 1,100 inches). Text sizes and image -/// rectangles come from the PDF, so a hostile size (say 1e300 pt) must not push the running -/// vertical position past what an `i64` (or a DrawingML coordinate) holds. -const MAX_ITEM_EMU: i64 = 1_000_000_000; +/// The largest height one item may take on a slide (about 1,100 inches in points). +const MAX_ITEM_PT: f64 = 78_740.0; -/// Points to EMU for a height or gap, clamped to `0..=MAX_ITEM_EMU` (NaN reads as 0). -fn height_emu(pt: f64) -> i64 { - let v = pt * 12700.0; - // Clamped to 0..=MAX_ITEM_EMU before the cast, so it cannot truncate or wrap. - if v.is_finite() { - v.round().clamp(0.0, MAX_ITEM_EMU as f64) as i64 - } else if v > 0.0 { - MAX_ITEM_EMU +/// Points for a height or gap, clamped to `0..=MAX_ITEM_PT` (NaN reads as 0). +fn height_pt(pt: f64) -> f64 { + if pt.is_finite() { + pt.clamp(0.0, MAX_ITEM_PT) + } else if pt > 0.0 { + MAX_ITEM_PT } else { - 0 + 0.0 } } -/// A formatted DrawingML text run (``). -fn run_drawingml(text: &str, size: f64, bold: bool, italic: bool, color: [f64; 3]) -> String { - let mut rpr = String::new(); - let sz = if size.is_finite() { (size * 100.0).round().clamp(100.0, 400000.0) as i64 } else { 1200 }; - rpr.push_str(&format!(" sz=\"{sz}\"")); - if bold { - rpr.push_str(" b=\"1\""); +/// Parse a 6-digit hex color string into `Rgba`. +fn rgba_from_hex(h: &str) -> Option { + if h.len() != 6 { + return None; } - if italic { - rpr.push_str(" i=\"1\""); - } - let fill = if let Some(h) = hex(color) { format!("") } else { String::new() }; - format!("{fill}{}", esc(text)) + let r = u8::from_str_radix(&h[0..2], 16).ok()?; + let g = u8::from_str_radix(&h[2..4], 16).ok()?; + let b = u8::from_str_radix(&h[4..6], 16).ok()?; + Some(Rgba::rgb(r, g, b)) } -/// Generates a DrawingML table inside a `` with explicit cell borders. -fn pptx_table(shape_id: usize, t: &Table, x: i64, y: i64, content_w: i64) -> (String, i64) { +/// A formatted DrawingML text run. +fn dml_run(text: &str, size: f64, bold: bool, italic: bool, color: [f64; 3]) -> DmlRun { + let sz = if size.is_finite() { size.clamp(1.0, 4000.0) } else { 12.0 }; + let fill = + hex(color).and_then(|h| rgba_from_hex(&h)).map(|c| Fill::Solid { color: ColorRef { base: ColorBase::Rgb { rgb: c }, mods: Vec::new() } }); + DmlRun { + text: text.to_string(), + props: RunProps { + size: Some(sz), + bold: if bold { Some(true) } else { None }, + italic: if italic { Some(true) } else { None }, + fill, + ..Default::default() + }, + kind: RunKind::Text, + } +} + +/// Generates a DrawingML table shape with explicit cell borders. +fn pptx_table(shape_id: u32, t: &Table, x: f64, y: f64, content_w: f64) -> (Shape, f64) { let mut edges = t.cols.clone(); edges.push(t.rect[2]); let widths_pt: Vec = edges.windows(2).map(|w| (w[1] - w[0]).max(20.0)).collect(); let total_pt: f64 = widths_pt.iter().sum(); - let scale = if total_pt > 0.0 { (content_w as f64 / (total_pt * 12700.0)).min(1.0) } else { 1.0 }; - let widths: Vec = widths_pt.iter().map(|w| (w * scale * 12700.0).round().max(127000.0) as i64).collect(); - let table_w: i64 = widths.iter().fold(0i64, |sum, w| sum.saturating_add(*w)); - let row_h: i64 = height_emu(24.0); - let table_h: i64 = i64::try_from(t.rows.len()).unwrap_or(i64::MAX).saturating_mul(row_h).min(MAX_ITEM_EMU); + let scale = if total_pt > 0.0 { (content_w / total_pt).min(1.0) } else { 1.0 }; + let widths: Vec = widths_pt.iter().map(|w| (w * scale).max(10.0)).collect(); + let table_w: f64 = widths.iter().sum(); + let row_h: f64 = 24.0; + let table_h: f64 = (t.rows.len() as f64 * row_h).min(MAX_ITEM_PT); - let mut grid_cols = String::new(); - for w in &widths { - grid_cols.push_str(&format!("")); - } + let border = Some(Line { + width: Some(1.0), + fill: Some(Fill::Solid { color: ColorRef { base: ColorBase::Rgb { rgb: Rgba::rgb(0x99, 0x99, 0x99) }, mods: Vec::new() } }), + ..Default::default() + }); + let borders = [border.clone(), border.clone(), border.clone(), border]; - const TC_PR: &str = "\ -\ -\ -\ -\ -"; - - let mut rows_xml = String::new(); + let mut rows = Vec::with_capacity(t.rows.len()); for row in &t.rows { - rows_xml.push_str(&format!("")); + let mut cells = Vec::new(); for c in row { let span = c.span.max(1); - let run = run_drawingml(&c.text, c.size, c.bold, c.italic, c.color); - if span > 1 { - rows_xml.push_str(&format!("{run}{TC_PR}")); - for _ in 1..span { - rows_xml.push_str(&format!("{TC_PR}")); - } - } else { - rows_xml.push_str(&format!("{run}{TC_PR}")); + let run = dml_run(&c.text, c.size, c.bold, c.italic, c.color); + cells.push(Cell { + text: TextBody { paragraphs: vec![Paragraph { runs: vec![run], ..Default::default() }], ..Default::default() }, + grid_span: span as u32, + row_span: 1, + borders: borders.clone(), + ..Default::default() + }); + for _ in 1..span { + cells.push(Cell { h_merge: true, borders: borders.clone(), ..Default::default() }); } } let used: usize = row.iter().map(|c| c.span).sum(); for _ in used..t.cols.len() { - rows_xml.push_str(&format!("{TC_PR}")); + cells.push(Cell { borders: borders.clone(), ..Default::default() }); } - rows_xml.push_str(""); + rows.push(craft_pptx::table::Row { height: row_h, cells }); } - let frame = format!( - "\ -\ -\ -\ -{grid_cols}{rows_xml}\ -" - ); - (frame, table_h) + let shape = Shape { + id: ShapeId(shape_id), + name: format!("Table {shape_id}"), + xfrm: Some(Xfrm { x, y, w: table_w, h: table_h, ..Default::default() }), + kind: ShapeKind::Table(DmlTable { cols: widths, rows, ..Default::default() }), + ..Default::default() + }; + (shape, table_h) } /// A PowerPoint presentation (.pptx, Office Open XML PresentationML): each page becomes a slide, @@ -123,18 +131,18 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { let dummy = [Page::default()]; let pages = if pages.is_empty() { &dummy[..] } else { pages }; - let (sw, sh) = pages.first().map_or((9144000, 6858000), |p| (emu(p.width, 720.0), emu(p.height, 540.0))); - let mx = (sw / 16).clamp(360000, 914400); - let my = (sh / 16).clamp(360000, 914400); - let content_w = sw - 2 * mx; + let (sw, sh) = pages.first().map_or((720.0, 540.0), |p| (slide_pt(p.width, 720.0), slide_pt(p.height, 540.0))); + let mx = (sw / 16.0).clamp(360000.0 / 12700.0, 72.0); + let my = (sh / 16.0).clamp(360000.0 / 12700.0, 72.0); + let content_w = sw - 2.0 * mx; let mut global_media: Vec<(String, &Image)> = Vec::new(); // Shared picture bytes remain alive throughout export; only admitted media enter this map. let mut media_of: HashMap<*const u8, usize> = HashMap::new(); - let mut slides: Vec<(String, RelsOut)> = Vec::new(); // (slide_xml, slide_rels) + let mut slides: Vec> = Vec::new(); let body = crate::layout::body_size(pages); - for p in pages { + for (slide_idx, p) in pages.iter().enumerate() { let (tables, consumed) = crate::tables(&p.blocks, &p.rules); let title_idx = p .blocks @@ -161,52 +169,60 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { let items = crate::reading_order(p, parts); let mut current_y = my; - let mut shapes = String::new(); - let mut shape_id = 2usize; - let mut slide_media_rels: Vec<(String, usize)> = Vec::new(); + let mut shapes: Vec = Vec::new(); + let mut shape_id = 2u32; if let Some(title_block) = title_idx.and_then(|ti| p.blocks.get(ti)) { - let title_h = height_emu((title_block.size * 1.5).max(36.0)); - let title_run = run_drawingml(&title_block.text, title_block.size, title_block.bold, title_block.italic, title_block.color); - shapes.push_str(&format!( - "\ -\ -{title_run}" - )); + let title_h = height_pt((title_block.size * 1.5).max(36.0)); + let title_run = dml_run(&title_block.text, title_block.size, title_block.bold, title_block.italic, title_block.color); + shapes.push(Shape { + id: ShapeId(shape_id), + name: format!("Title {shape_id}"), + xfrm: Some(Xfrm { x: mx, y: current_y, w: content_w, h: title_h, ..Default::default() }), + kind: ShapeKind::Shape, + geom: Geom::Preset { name: "rect".into(), adj: Vec::new() }, + fill: Some(Fill::None), + text_box: true, + text: Some(TextBody { paragraphs: vec![Paragraph { runs: vec![title_run], ..Default::default() }], ..Default::default() }), + ..Default::default() + }); shape_id += 1; - current_y = current_y.saturating_add(title_h).saturating_add(height_emu(18.0)); + current_y = (current_y + title_h + 18.0).min(MAX_ITEM_PT); } let mut i = 0; while let Some(item) = items.get(i) { match item { SlideBodyItem::Para(_) => { - let mut paras_xml = String::new(); + let mut paragraphs = Vec::new(); let mut est_h_pt = 0.0; while let Some(SlideBodyItem::Para(runs)) = items.get(i) { - paras_xml.push_str(""); - for r in runs { - paras_xml.push_str(&run_drawingml(&r.text, r.block.size, r.block.bold, r.block.italic, r.block.color)); - } - paras_xml.push_str(""); + let dml_runs = runs.iter().map(|r| dml_run(&r.text, r.block.size, r.block.bold, r.block.italic, r.block.color)).collect(); + paragraphs.push(Paragraph { runs: dml_runs, ..Default::default() }); let len = runs.iter().map(|r| r.text.len()).fold(0usize, usize::saturating_add); let size = runs.iter().map(|r| r.block.size).fold(10.0, f64::max); let lines = (len as f64 / 60.0).ceil().max(1.0); est_h_pt += (size * 1.3) * lines + 6.0; i += 1; } - let box_h = height_emu(est_h_pt); - shapes.push_str(&format!( - "\ -\ -{paras_xml}" - )); + let box_h = height_pt(est_h_pt); + shapes.push(Shape { + id: ShapeId(shape_id), + name: format!("TextBox {shape_id}"), + xfrm: Some(Xfrm { x: mx, y: current_y, w: content_w, h: box_h, ..Default::default() }), + kind: ShapeKind::Shape, + geom: Geom::Preset { name: "rect".into(), adj: Vec::new() }, + fill: Some(Fill::None), + text_box: true, + text: Some(TextBody { paragraphs, ..Default::default() }), + ..Default::default() + }); shape_id += 1; - current_y = current_y.saturating_add(box_h).saturating_add(height_emu(12.0)); + current_y = (current_y + box_h + 12.0).min(MAX_ITEM_PT); } SlideBodyItem::Img(im) => { let global_img_idx = match media_of.get(&im.bytes.as_ptr()) { - Some(&i) => i, + Some(&idx) => idx, None if room_for(&global_media, im) => { let n = global_media.len() + 1; global_media.push((format!("image{n}.{}", im.ext), *im)); @@ -218,173 +234,55 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { continue; } }; - let r_id = format!("rIdImg{}", slide_media_rels.len() + 1); - slide_media_rels.push((r_id.clone(), global_img_idx)); let (w, h) = ((im.rect[2] - im.rect[0]).max(1.0), (im.rect[3] - im.rect[1]).max(1.0)); - let max_w = content_w as f64 / 12700.0; - let k = (max_w / w).min(1.0); - let cx = height_emu(w * k).min(content_w); - let cy = height_emu(h * k); - let x = mx + (content_w - cx) / 2; + let k = (content_w / w).min(1.0); + let cx = height_pt(w * k).min(content_w); + let cy = height_pt(h * k); + let x = mx + (content_w - cx) / 2.0; let y = current_y; - shapes.push_str(&format!( - "\ -\ -" - )); + shapes.push(Shape { + id: ShapeId(shape_id), + name: format!("Picture {shape_id}"), + xfrm: Some(Xfrm { x, y, w: cx, h: cy, ..Default::default() }), + kind: ShapeKind::Picture { fill: PictureFill { media: MediaId(global_img_idx as u32), ..Default::default() } }, + geom: Geom::Preset { name: "rect".into(), adj: Vec::new() }, + ..Default::default() + }); shape_id += 1; - current_y = current_y.saturating_add(cy).saturating_add(height_emu(12.0)); + current_y = (current_y + cy + 12.0).min(MAX_ITEM_PT); i += 1; } SlideBodyItem::Table(t) => { - let (table_xml, table_h) = pptx_table(shape_id, t, mx, current_y, content_w); - shapes.push_str(&table_xml); + let (table_shape, table_h) = pptx_table(shape_id, t, mx, current_y, content_w); + shapes.push(table_shape); shape_id += 1; - current_y = current_y.saturating_add(table_h).saturating_add(height_emu(12.0)); + current_y = (current_y + table_h + 12.0).min(MAX_ITEM_PT); i += 1; } } } - let slide_xml = format!( - "\n\ -\ -\ -\ -{shapes}" - ); - - let mut slide_rels = RelsOut::default(); - slide_rels.items.push(RelOut { - id: "rIdLayout".into(), - ty: format!("{RT_BASE}slideLayout"), - target: "../slideLayouts/slideLayout1.xml".into(), - external: false, - }); - for (r_id, global_idx) in &slide_media_rels { - if let Some((name, _)) = global_media.get(*global_idx - 1) { - slide_rels.items.push(RelOut { - id: r_id.clone(), - ty: format!("{RT_BASE}image"), - target: format!("../media/{name}"), - external: false, - }); - } - } - - slides.push((slide_xml, slide_rels)); + slides.push(Arc::new(Slide { id: SlideId(256 + slide_idx as u32), shapes, ..Default::default() })); } - let mut pkg = PackageOut::default(); - let mut root_rels = RelsOut::default(); - root_rels.add("officeDocument", "ppt/presentation.xml", false); - root_rels.add("metadata/core-properties", "docProps/core.xml", false); - pkg.rels("", &root_rels); + let media: Vec = global_media + .into_iter() + .enumerate() + .map(|(idx, (name, im))| MediaItem { + id: MediaId((idx + 1) as u32), + name, + content_type: image_mime(im.ext).to_string(), + data: Arc::new(im.bytes.to_vec()), + link: None, + }) + .collect(); - let mut sld_id_lst = String::new(); - let mut pres_rels = RelsOut::default(); - pres_rels.items.push(RelOut { - id: "rIdMaster1".into(), - ty: format!("{RT_BASE}slideMaster"), - target: "slideMasters/slideMaster1.xml".into(), - external: false, - }); - for i in 1..=slides.len() { - let sld_id = 255 + i; - sld_id_lst.push_str(&format!("")); - pres_rels.items.push(RelOut { - id: format!("rIdSld{i}"), - ty: format!("{RT_BASE}slide"), - target: format!("slides/slide{i}.xml"), - external: false, - }); - } - pkg.rels("ppt/presentation.xml", &pres_rels); + let mut pres = Presentation { slide_size: Size::new(sw, sh), slides, media, ..Default::default() }; + pres.props.title = title.to_string(); - let pres = format!( - "{DECL_STANDALONE}\ -\ -{sld_id_lst}" - ); - - let slide_master = format!( - "{DECL_STANDALONE}\ -\ -\ -\ -" - ); - - let mut slide_master_rels = RelsOut::default(); - slide_master_rels.items.push(RelOut { - id: "rIdLayout1".into(), - ty: format!("{RT_BASE}slideLayout"), - target: "../slideLayouts/slideLayout1.xml".into(), - external: false, - }); - slide_master_rels.items.push(RelOut { - id: "rIdTheme1".into(), - ty: format!("{RT_BASE}theme"), - target: "../theme/theme1.xml".into(), - external: false, - }); - pkg.rels("ppt/slideMasters/slideMaster1.xml", &slide_master_rels); - - let slide_layout = format!( - "{DECL_STANDALONE}\ -\ -" - ); - - let mut slide_layout_rels = RelsOut::default(); - slide_layout_rels.add("slideMaster", "../slideMasters/slideMaster1.xml", false); - pkg.rels("ppt/slideLayouts/slideLayout1.xml", &slide_layout_rels); - - let theme = format!( - "{DECL_STANDALONE}\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -\ -" - ); - - pkg.xml_part("docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml", core_properties_xml(title)); - pkg.xml_part("ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", pres.into_bytes()); - pkg.xml_part( - "ppt/slideMasters/slideMaster1.xml", - "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", - slide_master.into_bytes(), - ); - pkg.xml_part( - "ppt/slideLayouts/slideLayout1.xml", - "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml", - slide_layout.into_bytes(), - ); - pkg.xml_part("ppt/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml", theme.into_bytes()); - for (i, (s_xml, s_rels)) in slides.iter().enumerate() { - let num = i + 1; - let path = format!("ppt/slides/slide{num}.xml"); - pkg.xml_part(&path, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml", s_xml.as_bytes().to_vec()); - pkg.rels(&path, s_rels); - } - for (name, im) in &global_media { - pkg.binary(&format!("ppt/media/{name}"), image_mime(im.ext), im.bytes.to_vec()); - } - pkg.finish().unwrap_or_default() + craft_pptx::export(&pres, &Plain).unwrap_or_default() } #[cfg(test)]