mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 01:22:49 +00:00
* Add craft-drawingml: units, colours, preset shapes, custom geometry and text warps The parts of DrawingML that need no document model, moved from DeckCraft (60f91f78): crates/color, crates/geom (preset, warp, Xfrm and the EMU conversions) and crates/pptx/src/custgeom.rs with CustomPath from crates/model. The code is unchanged except that custgeom reads craft-xml trees and writes to a craft-xml Writer instead of DeckCraft's own XML helper, and matches elements by local name. The 59 tests of the moved code come with it; six more cover the custom geometry limits. * drawingml: evaluate a custom geometry's guide lists once per coordinate space read_cust_geom rebuilt Guides and evaluated avLst and gdLst again for every path: up to 1,000 paths times up to 10,000 guides, each name copied and hashed. Guides are now cached per path coordinate space (w, h). At most 16 spaces and 4 MiB of guide names are kept per geometry; paths in further spaces are left out. The first space is always kept, and a geometry without guides is not capped. Ported from storytold/deckcraft#1789. A list counts as having guides only when it has gd children, as that PR's review asked. * drawingml: the model types for fills, lines, text, tables, charts and diagrams Moved from DeckCraft (60f91f78) crates/model/src: style.rs, text.rs, table.rs, chart.rs, numfmt.rs and trendline.rs, plus Diagram, DiagramNode and the MediaId and SlideId newtypes from lib.rs. The files are unchanged except that deckcraft_color:: is crate::color:: and Fill and TableBorder allow clippy::large_enum_variant. Their 60 tests come with them. Readers and writers follow. * drawingml: the theme model (colour, font and format schemes) Moved from DeckCraft crates/model/src/theme.rs at 60f91f78: FontSet, ScriptFont, FontScheme, FormatScheme, Theme and default_format. The app's theme gallery, its variants and its own default theme stay in the app. Theme::default() is a plain theme built in place, so nothing here depends on a gallery. * drawingml: readers and writers for fills, lines, text, charts, themes, table styles and diagrams Moved from DeckCraft crates/pptx/src at 60f91f78: read/dml.rs, write/dml.rs, read/chart.rs, read/chartex.rs, write/chart.rs, read/theme.rs, write/theme.rs, tables.rs and dgm.rs, onto craft-xml. Package lookups (Imp, Part, Exp, Out) are the caller's part::Source and part::Sink. The chart frame budget and kept-part capture stay with the presentation reader. * drawingml: what WordCraft and GridCraft read that DeckCraft's copy did not Every ST_PresetColorVal name has its colour, in any case and with the dk/lt/med abbreviations; a sysClr without lastClr takes the usual value of its name; a Strict percentage (50%) reads anywhere a percentage does; a chart series keeps its c:f formulas and the writer uses them. * craft-drawingml: extra named theme colours and explicit tint rows Ported from the format part of storytold/deckcraft#1746 by @jagjordi: ColorScheme::{extra, tints, computed_tints}, ExtraColor, GalleryVariant, variants_of, and the a:clrScheme extension that keeps them in a file. * drawingml xmlx: copy only used, bounded namespace declarations onto kept fragments and mc branches From storytold/deckcraft#1740 (988a4920), which landed after the extraction commit 60f91f78. * Add craft-pptx: the PresentationML model (from DeckCraft) Presentation, masters, layouts, slides, shapes, placeholders, media, comments, sections, show settings, properties, embedded fonts, kept parts and drawing guides from DeckCraft's crates/model/src/lib.rs, and transitions and animations from anim.rs. The DrawingML types come from craft-drawingml. The placeholder matching from DeckCraft's resolve.rs moves as placeholder::parents, without the rest of resolve. Presentation::default() is now an empty 16:9 deck with no masters; the starting deck stays with the application. --------- Co-authored-by: Jordi Altayó <jordiag@kth.se>
107 lines
5.1 KiB
Rust
107 lines
5.1 KiB
Rust
//! Placeholder inheritance: which layout and master placeholders a placeholder takes its
|
|
//! position, formatting and text styles from (`p:ph` `type` and `idx`, ECMA-376 part 1, 19.3.1.36).
|
|
//!
|
|
//! Only the matching lives here. What a caller does with the parents (effective position, fill,
|
|
//! text style) is its own business.
|
|
|
|
use crate::{Layout, Master, PhType, Shape};
|
|
|
|
/// Where a shape lives.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Owner {
|
|
Slide,
|
|
Layout,
|
|
Master,
|
|
}
|
|
|
|
/// Can a slide placeholder of type `slide` inherit from a layout placeholder of type `other`?
|
|
fn compatible(slide: PhType, other: PhType) -> bool {
|
|
use PhType::*;
|
|
match slide {
|
|
Title | CtrTitle => matches!(other, Title | CtrTitle),
|
|
SubTitle | Body | Obj | Chart | Table | ClipArt | Diagram | Media | Picture => {
|
|
!matches!(other, Title | CtrTitle | Date | Footer | SlideNum | Header)
|
|
}
|
|
k => k == other,
|
|
}
|
|
}
|
|
|
|
/// The placeholder in `shapes` that `shape` matches: by `idx` (when `by_idx`) and a compatible
|
|
/// type, else the same type, else a compatible one.
|
|
pub fn find_ph<'s>(shapes: &'s [Shape], shape: &Shape, by_idx: bool) -> Option<&'s Shape> {
|
|
let ph = shape.ph.as_ref()?;
|
|
if by_idx
|
|
&& ph.idx != 0
|
|
&& let Some(s) = shapes.iter().find(|s| s.ph.as_ref().is_some_and(|p| p.idx == ph.idx && compatible(ph.kind, p.kind)))
|
|
{
|
|
return Some(s);
|
|
}
|
|
// Same type first, then a compatible one.
|
|
shapes
|
|
.iter()
|
|
.find(|s| s.ph.as_ref().is_some_and(|p| p.kind == ph.kind && (!by_idx || ph.idx == 0 || p.idx == ph.idx || ph.kind.is_title())))
|
|
.or_else(|| shapes.iter().find(|s| s.ph.as_ref().is_some_and(|p| p.kind == ph.kind)))
|
|
.or_else(|| shapes.iter().find(|s| s.ph.as_ref().is_some_and(|p| compatible(ph.kind, p.kind))))
|
|
}
|
|
|
|
/// The master placeholder a placeholder of type `kind` inherits from: the title, the matching
|
|
/// footer-row placeholder, or the body.
|
|
pub fn master_ph(master: &Master, kind: PhType) -> Option<&Shape> {
|
|
let want = match kind {
|
|
PhType::Title | PhType::CtrTitle => PhType::Title,
|
|
PhType::Date | PhType::Footer | PhType::SlideNum | PhType::Header => kind,
|
|
_ => PhType::Body,
|
|
};
|
|
master.shapes.iter().find(|s| s.ph_type() == Some(want))
|
|
}
|
|
|
|
/// The layout and master placeholders `shape` inherits from. A slide's placeholder matches one on
|
|
/// `layout`, and the master placeholder follows the layout one's type; a layout's placeholder
|
|
/// inherits from the master only; a master's from nothing.
|
|
pub fn parents<'a>(master: &'a Master, layout: Option<&'a Layout>, owner: Owner, shape: &Shape) -> (Option<&'a Shape>, Option<&'a Shape>) {
|
|
let Some(ph) = shape.ph.as_ref() else { return (None, None) };
|
|
match owner {
|
|
Owner::Slide => {
|
|
let lp = layout.and_then(|l| find_ph(&l.shapes, shape, true));
|
|
let kind = lp.and_then(Shape::ph_type).unwrap_or(ph.kind);
|
|
(lp, master_ph(master, kind))
|
|
}
|
|
Owner::Layout => (None, master_ph(master, ph.kind)),
|
|
Owner::Master => (None, None),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{Placeholder, ShapeId};
|
|
|
|
fn ph(id: u32, kind: PhType, idx: u32) -> Shape {
|
|
Shape { id: ShapeId(id), ph: Some(Placeholder { kind, idx, ..Default::default() }), ..Default::default() }
|
|
}
|
|
|
|
#[test]
|
|
fn slide_placeholders_match_by_index_then_type() {
|
|
let master = Master { shapes: vec![ph(1, PhType::Title, 0), ph(2, PhType::Body, 1), ph(3, PhType::SlideNum, 12)], ..Default::default() };
|
|
let layout = Layout { shapes: vec![ph(10, PhType::Title, 0), ph(11, PhType::Body, 1), ph(12, PhType::Obj, 2)], ..Default::default() };
|
|
let id = |s: Option<&Shape>| s.map(|s| s.id.0);
|
|
// By idx: idx 2 is the layout's obj, whose master parent is the body.
|
|
let (l, m) = parents(&master, Some(&layout), Owner::Slide, &ph(20, PhType::Body, 2));
|
|
assert_eq!((id(l), id(m)), (Some(12), Some(2)));
|
|
// A centred title takes the layout's title and the master's title.
|
|
let (l, m) = parents(&master, Some(&layout), Owner::Slide, &ph(21, PhType::CtrTitle, 0));
|
|
assert_eq!((id(l), id(m)), (Some(10), Some(1)));
|
|
// An unknown idx falls back to the same type.
|
|
let (l, _) = parents(&master, Some(&layout), Owner::Slide, &ph(22, PhType::Body, 9));
|
|
assert_eq!(id(l), Some(11));
|
|
// A title never inherits from a body, nor a slide number from a title.
|
|
assert_eq!(find_ph(&layout.shapes[1..], &ph(23, PhType::Title, 0), true).map(|s| s.id.0), None);
|
|
let (l, m) = parents(&master, Some(&layout), Owner::Slide, &ph(24, PhType::SlideNum, 12));
|
|
assert_eq!((id(l), id(m)), (None, Some(3)));
|
|
// Layouts inherit from the master only, masters from nothing, other shapes from nothing.
|
|
assert_eq!(parents(&master, None, Owner::Layout, &ph(25, PhType::SubTitle, 1)), (None, master.shapes.get(1)));
|
|
assert_eq!(parents(&master, None, Owner::Master, &ph(26, PhType::Title, 0)), (None, None));
|
|
assert_eq!(parents(&master, Some(&layout), Owner::Slide, &Shape::default()), (None, None));
|
|
}
|
|
}
|