A fast, helpful, and open-source document parser
screenshot wip
Logan Markewich685e9dfa1354
diff --git a/crates/liteparse-ooxml/src/pptx/shapes.rs b/crates/liteparse-ooxml/src/pptx/shapes.rsindex 0d926b3..d57cc79 100644--- a/crates/liteparse-ooxml/src/pptx/shapes.rs+++ b/crates/liteparse-ooxml/src/pptx/shapes.rs@@ -496,7 +496,7 @@ pub fn parse_shape_tree(data: &[u8]) -> Result<Vec<Shape>> {/// One struct rather than two entry points because both come out of the same/// `p:cSld`, and parsing the part twice to get them would double the cost of/// the deck walk for a field that is one element wide.-#[derive(Clone, Debug, Default)]+#[derive(Clone, Debug)]pub struct SlidePart {pub shapes: Vec<Shape>,/// §19.3.1.1 `<p:bg>`, when *this* part declares one. `None` means "not@@ -512,6 +512,29 @@ pub struct SlidePart {/// — and a `p:clrMapOvr` holding `a:masterClrMapping` is a `None`, because/// that element's entire content is "inherit".pub color_map: Option<ColorMap>,+ /// §19.2.1.32 `p:sld/@showMasterSp` and §19.3.1.39 `p:sldLayout`'s — "draw+ /// the shapes the rung above this one supplies". Defaults to **true**, per+ /// both elements' schema.+ ///+ /// A `bool` rather than an `Option`, because unlike `background` and+ /// `color_map` this attribute does not cascade: it is a statement about+ /// *this* part's own rendering, and a slide that says nothing shows its+ /// layout's shapes whatever the layout says about the master's.+ ///+ /// Corpus: 25 of 415 layouts and 4 of 1,278 slides opt out. A master's root+ /// has no such attribute and always reads `true`.+ pub show_inherited_shapes: bool,+}++impl Default for SlidePart {+ fn default() -> Self {+ Self {+ shapes: Vec::new(),+ background: None,+ color_map: None,+ show_inherited_shapes: true,+ }+ }}/// Parse a slide, layout, master or notes-slide part into its shapes and its@@ -524,9 +547,13 @@ pub fn parse_slide_part(data: &[u8]) -> Result<SlidePart> {.clr_map.or_else(|| part.clr_map_ovr.and_then(|o| o.override_clr_mapping)).map(ClrMapXml::into_color_map);+ // Also a sibling of `p:cSld`, and read before the early return for the same+ // reason as the colour map.+ let show_inherited_shapes = part.show_master_sp.is_none_or(|b| b.0);let Some(c_sld) = part.c_sld else {return Ok(SlidePart {color_map,+ show_inherited_shapes,..Default::default()});};@@ -537,6 +564,7 @@ pub fn parse_slide_part(data: &[u8]) -> Result<SlidePart> {.unwrap_or_default(),background: c_sld.bg.and_then(Background::from_xml),color_map,+ show_inherited_shapes,})}@@ -557,6 +585,11 @@ struct SlidePartXml {/// part's content model allows the two elements together.#[serde(rename = "clrMapOvr", default)]clr_map_ovr: Option<ClrMapOvrXml>,+ /// §19.2.1.32 / §19.3.1.39 `@showMasterSp`, on `p:sld` and `p:sldLayout`.+ /// Absent on `p:sldMaster` and `p:notes`, which is why it is an `Option`+ /// collapsed to the schema default of `true` by the caller.+ #[serde(rename = "@showMasterSp", default)]+ show_master_sp: Option<crate::docx::parse::primitives::toggles::AttrBool>,}/// §19.3.1.6 CT_ColorMapping. Every attribute is *required* by the schema, but@@ -1985,4 +2018,33 @@ mod tests {assert_eq!(part.shapes.len(), 1);assert!(part.background.is_some());}++ fn show_inherited(root_attrs: &str) -> bool {+ let xml = format!(+ r#"<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"+ {root_attrs}>+ <p:cSld><p:spTree>+ <p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>+ <p:grpSpPr/>+ </p:spTree></p:cSld>+ </p:sld>"#+ );+ parse_slide_part(xml.as_bytes())+ .expect("parses")+ .show_inherited_shapes+ }++ /// The schema default is `true`, and it is the *absence* of the attribute+ /// that carries it — a part that says nothing inherits its rung's shapes.+ #[test]+ fn show_master_sp_defaults_to_true() {+ assert!(show_inherited(""));+ }++ #[test]+ fn show_master_sp_is_read_when_stated() {+ assert!(!show_inherited(r#"showMasterSp="0""#));+ assert!(show_inherited(r#"showMasterSp="1""#));+ }}diff --git a/crates/liteparse/src/office/pptx.rs b/crates/liteparse/src/office/pptx.rsindex a07cc34..7b8b889 100644--- a/crates/liteparse/src/office/pptx.rs+++ b/crates/liteparse/src/office/pptx.rs@@ -116,6 +116,17 @@ pub struct Deck {/// states no map of its own.master_clr_map: HashMap<String, Option<ColorMap>>,layout_clr_map: HashMap<String, Option<ColorMap>>,+ /// The **non-placeholder** shapes each rung contributes to every slide+ /// under it, geometry already composed. See [`inherited_shapes`] for why+ /// the placeholders are dropped here rather than skipped at paint time.+ ///+ /// Cached beside the other rungs for the same reason, and it matters more+ /// here than anywhere else: this is a whole shape tree, not one element.+ master_shapes: HashMap<String, Vec<Shape>>,+ layout_shapes: HashMap<String, Vec<Shape>>,+ /// §19.3.1.39 `p:sldLayout/@showMasterSp` — whether slides under this+ /// layout draw the master's shapes. 25 of the corpus's 415 layouts say no.+ layout_shows_master: HashMap<String, bool>,default_text_style: ListStyle,/// The rung-7-only defaults a slide falls back to when its master has no/// entry. Stored on the deck rather than built as a local in the walk so@@ -156,6 +167,26 @@ pub struct PreparedSlide<'d> {/// tell "stated" from "assumed", which is exactly the 30-of-70 masters the/// census found.pub color_map: Option<ColorMap>,+ /// The master's and then the layout's own shapes, in the order they paint:+ /// **under** everything in `shapes`, master first. Empty when the rung+ /// contributes none, or when a `@showMasterSp="0"` turns it off.+ ///+ /// Borrowed, not cloned: unlike `background` this is a whole shape tree per+ /// part, shared by every slide under it.+ ///+ /// Read by the geometry pass only. The markdown emitter deliberately+ /// ignores it — see the module note on why layout text is a measured gap+ /// rather than something this field quietly fixes.+ pub inherited: [&'d [Shape]; 2],+ /// Whether each arm of `inherited` is empty because it was **declined**+ /// rather than because the part had nothing to give: `[master, layout]`,+ /// parallel to `inherited`.+ ///+ /// The distinction is the whole reason `@showMasterSp` is parsed. Both+ /// cases paint nothing, so without this a deck that correctly honoured 25+ /// opt-outs and a deck whose layout parse silently failed would report the+ /// same numbers.+ pub declined: [bool; 2],layout_text: Option<&'d PlaceholderTextStyles>,master_text: Option<&'d PlaceholderTextStyles>,deck_defaults: &'d DeckTextDefaults,@@ -190,6 +221,9 @@ impl Deck {layout_bg: HashMap::new(),master_clr_map: HashMap::new(),layout_clr_map: HashMap::new(),+ master_shapes: HashMap::new(),+ layout_shapes: HashMap::new(),+ layout_shows_master: HashMap::new(),fallback_defaults: DeckTextDefaults {master_styles: TextStyles::default(),default_text_style: default_text_style.clone(),@@ -251,6 +285,37 @@ impl Deck {pptx::apply_inherited_geometry(&mut shapes, layout_geo, MatchRule::Idx);pptx::apply_slide_geometry(&mut shapes);+ // §19.2.1.32: the slide's own `@showMasterSp` turns off *everything*+ // the rungs above supply, so it gates the master arm as well as the+ // layout's — a slide that declines its layout's design does not then+ // take the master's, which the layout was itself drawing over.+ // §19.3.1.39: the layout's gates only the master's.+ let layout = slide.layout.as_ref();+ let shows_layout = part.show_inherited_shapes;+ let shows_master = shows_layout+ && layout+ .and_then(|l| self.layout_shows_master.get(&l.path))+ .copied()+ .unwrap_or(true);+ let inherited = [+ if shows_master {+ slide+ .master+ .as_ref()+ .and_then(|m| self.master_shapes.get(&m.path))+ .map_or(&[][..], Vec::as_slice)+ } else {+ &[]+ },+ if shows_layout {+ layout+ .and_then(|l| self.layout_shapes.get(&l.path))+ .map_or(&[][..], Vec::as_slice)+ } else {+ &[]+ },+ ];+let master = slide.master.as_ref()@@ -259,6 +324,8 @@ impl Deck {shapes,background,color_map,+ inherited,+ declined: [!shows_master, !shows_layout],layout_text: slide.layout.as_ref()@@ -308,6 +375,8 @@ impl Deck {// twice would double the cost of priming for a one-element field.let part = pptx::parse_slide_part(&master.xml).unwrap_or_default();let shapes = part.shapes;+ self.master_shapes+ .insert(master.path.clone(), inherited_shapes(&shapes));self.master_bg.insert(master.path.clone(), part.background);self.master_clr_map.insert(master.path.clone(), part.color_map);@@ -339,7 +408,16 @@ impl Deck {self.layout_bg.insert(layout.path.clone(), part.background);self.layout_clr_map.insert(layout.path.clone(), part.color_map);+ self.layout_shows_master+ .insert(layout.path.clone(), part.show_inherited_shapes);pptx::apply_inherited_geometry(&mut shapes, master_geo, MatchRule::CollapsedKind);+ // After the placeholder cascade, before `from_layout` indexes it:+ // both read `Shape::transform`, which neither this nor the+ // composition below touches, so the order between them is free —+ // but the cascade above must come first, since a placeholder with+ // no rectangle of its own has nothing to compose.+ self.layout_shapes+ .insert(layout.path.clone(), inherited_shapes(&shapes));self.layout_geo.insert(layout.path.clone(),PlaceholderGeometry::from_layout(&shapes, master_geo),@@ -352,6 +430,35 @@ impl Deck {}}+/// The shapes a layout or master part contributes to every slide beneath it:+/// its **non-placeholder** shapes, with their group transforms composed.+///+/// PowerPoint draws a layout's ordinary shapes — the full-bleed panel, the+/// rule, the logo strip — on every slide that uses the layout. It does *not*+/// draw the layout's placeholders: those are prototypes, and the slide's own+/// placeholder is the thing that renders. Painting them would put every+/// layout's "Click to edit Master title style" prompt on the deck, and would+/// double-paint the fill of every placeholder a slide does use.+///+/// So the filter is the correctness rule, not an optimisation, and it is+/// applied here rather than at paint time so that the cache holds exactly what+/// the painter may draw. Top-level only, matching+/// `PlaceholderGeometry::index`: a placeholder never appears inside a group.+///+/// Corpus: 1,567 of 4,432 layout shapes and 179 of 395 master shapes survive.+fn inherited_shapes(shapes: &[Shape]) -> Vec<Shape> {+ let mut kept: Vec<Shape> = shapes+ .iter()+ .filter(|s| s.placeholder.is_none())+ .cloned()+ .collect();+ // These shapes never reach `Deck::prepare`'s composition pass — they are+ // not the slide's — so they compose here, once per part rather than once+ // per slide using it.+ pptx::apply_slide_geometry(&mut kept);+ kept+}+struct SlideCtx<'a> {cascade: TextCascade<'a>,/// The slide part, for resolving relationships. Hyperlinks need the rawdiff --git a/crates/liteparse/src/office/pptx_layout.rs b/crates/liteparse/src/office/pptx_layout.rsindex 5b7160e..1b03846 100644--- a/crates/liteparse/src/office/pptx_layout.rs+++ b/crates/liteparse/src/office/pptx_layout.rs@@ -180,6 +180,36 @@ pub struct SlideGeometry {/// a shape-level text colour this pass does not parse — 2,043 of the/// corpus's 2,181 `p:style` elements carry one.pub runs_colour_defaulted: usize,+ /// Shapes painted from a *layout* or *master* part rather than the slide's+ /// own tree — the panels, rules and logo strips a deck authors once and+ /// every slide under that rung inherits.+ ///+ /// Counted apart from `painted_shapes` rather than pooled with it, because+ /// the two answer different questions: this one is per *slide occurrence*,+ /// so one layout panel over 40 slides is 40 here and 1 in its part. Pooling+ /// them would make the slide-level number move whenever a deck happened to+ /// reuse a layout more.+ pub inherited_shapes_painted: usize,+ /// Inherited shapes that resolve to ink but whose geometry cannot be built+ /// — the same class as `unpainted_shapes`, counted separately for the same+ /// reason as above.+ pub inherited_shapes_unpainted: usize,+ /// Occurrences of an inherited shape that **carries text this pass does not+ /// lay out**: 237 layout and 15 master shapes on the corpus, 13.7k+ /// characters between them.+ ///+ /// A measured gap, deliberately left open. Emitting it would put text on a+ /// page that the markdown emitter — which walks the slide's tree only —+ /// does not emit, and the two must agree: a `TextItem` is a faithful box+ /// only for markdown a reader actually gets. Closing it means teaching both+ /// walks at once, which is its own step.+ pub inherited_shapes_with_text: usize,+ /// Slides that decline an inherited rung via `@showMasterSp="0"` — 4 slides+ /// declining their layout's shapes and, through 25 layouts, the slides that+ /// decline their master's. Counted because "painted nothing here" and+ /// "asked to paint nothing here" are different claims.+ pub slides_declining_layout: usize,+ pub slides_declining_master: usize,/// Runs whose resolved colour is **exactly the colour of the slide's own/// background**, i.e. text that cannot be read.///@@ -270,6 +300,11 @@ fn layout_deck(pkg: &PresentationPackage, registry: &FontRegistry) -> SlideGeomepainted_shapes: 0,unpainted_shapes: 0,outlines_defaulted_black: 0,+ inherited_shapes_painted: 0,+ inherited_shapes_unpainted: 0,+ inherited_shapes_with_text: 0,+ slides_declining_layout: 0,+ slides_declining_master: 0,background_commands: Vec::with_capacity(pkg.slides.len()),painted_backgrounds: 0,transparent_backgrounds: 0,@@ -305,6 +340,9 @@ fn layout_deck(pkg: &PresentationPackage, registry: &FontRegistry) -> SlideGeomepainted_shapes: &mut out.painted_shapes,unpainted_shapes: &mut out.unpainted_shapes,outlines_defaulted_black: &mut out.outlines_defaulted_black,+ inherited_shapes_painted: &mut out.inherited_shapes_painted,+ inherited_shapes_unpainted: &mut out.inherited_shapes_unpainted,+ inherited_shapes_with_text: &mut out.inherited_shapes_with_text,painted_backgrounds: &mut out.painted_backgrounds,transparent_backgrounds: &mut out.transparent_backgrounds,unrenderable_backgrounds: &mut out.unrenderable_backgrounds,@@ -318,10 +356,19 @@ fn layout_deck(pkg: &PresentationPackage, registry: &FontRegistry) -> SlideGeome// them. See [`paint_shape`] for why they cannot be one walk. The// background goes first because the command list *is* the z-order.background_at = paint_background(prepared.background.as_ref(), page_size, &mut ctx);- paint_shapes(&prepared.shapes, &mut ctx);+ // ...and so is this: master under layout under slide. §19.3.1.39+ // builds the page by drawing each rung over the one it inherits+ // from, which is why an inherited panel is a backdrop for the+ // slide's own shapes rather than a lid over them.+ for rung in prepared.inherited {+ paint_shapes(rung, Rung::Inherited, &mut ctx);+ }+ paint_shapes(&prepared.shapes, Rung::Slide, &mut ctx);for shape in reading_order(&prepared.shapes) {layout_shape(shape, &mut ctx);}+ out.slides_declining_master += usize::from(prepared.declined[0]);+ out.slides_declining_layout += usize::from(prepared.declined[1]);}out.placements.push(placements);@@ -374,6 +421,9 @@ struct ShapeCtx<'a, 'r> {painted_shapes: &'a mut usize,unpainted_shapes: &'a mut usize,outlines_defaulted_black: &'a mut usize,+ inherited_shapes_painted: &'a mut usize,+ inherited_shapes_unpainted: &'a mut usize,+ inherited_shapes_with_text: &'a mut usize,painted_backgrounds: &'a mut usize,transparent_backgrounds: &'a mut usize,unrenderable_backgrounds: &'a mut usize,@@ -386,7 +436,32 @@ struct ShapeCtx<'a, 'r> {runs_invisible_on_background: &'a mut usize,}+/// Which part a painted shape came from.+///+/// Threaded as an argument rather than kept as a flag on [`ShapeCtx`], because+/// a flag would have to be set and cleared around each walk and the failure+/// mode of forgetting is a tally that silently attributes a layout's panels to+/// the slide.+#[derive(Clone, Copy, PartialEq, Eq)]+enum Rung {+ /// The slide's own `p:spTree`.+ Slide,+ /// A layout's or master's, drawn under it.+ Inherited,+}+impl ShapeCtx<'_, '_> {+ /// Record one shape's outcome against the rung it came from.+ fn tally(&mut self, rung: Rung, painted: bool) {+ let counter = match (rung, painted) {+ (Rung::Slide, true) => &mut *self.painted_shapes,+ (Rung::Slide, false) => &mut *self.unpainted_shapes,+ (Rung::Inherited, true) => &mut *self.inherited_shapes_painted,+ (Rung::Inherited, false) => &mut *self.inherited_shapes_unpainted,+ };+ *counter += 1;+ }+/// Add one body's commands to the slide, wrapped in the bracket that/// places them.///@@ -501,14 +576,14 @@ fn paint_background(/// chrome filter: this walk paints chrome, the text walk still drops it, and/// the 347 chrome shapes that carry text are a divergence this step leaves/// open rather than closes (see the module-level note in the plan).-fn paint_shapes(shapes: &[Shape], ctx: &mut ShapeCtx<'_, '_>) {+fn paint_shapes(shapes: &[Shape], rung: Rung, ctx: &mut ShapeCtx<'_, '_>) {for shape in shapes {- paint_shape(shape, ctx);+ paint_shape(shape, rung, ctx);if let ShapeKind::Group(group) = &shape.kind {// A group's own `grpSpPr` fill is not lowered to `ShapeProperties`// at all, so a group paints nothing itself — its children carry// every fill, in their own declaration order.- paint_shapes(&group.children, ctx);+ paint_shapes(&group.children, rung, ctx);}}}@@ -521,10 +596,17 @@ fn paint_shapes(shapes: &[Shape], ctx: &mut ShapeCtx<'_, '_>) {/// self-placing. That also lets it carry the two flips, which a text bracket/// deliberately does not — §20.1.7.6 mirrors a shape's *geometry*, and/// PowerPoint does not mirror the text inside it.-fn paint_shape(shape: &Shape, ctx: &mut ShapeCtx<'_, '_>) {+fn paint_shape(shape: &Shape, rung: Rung, ctx: &mut ShapeCtx<'_, '_>) {let Some(props) = shape_properties(shape) else {return;};+ // Counted before the paint decision, not after: the text is missing+ // whether or not the shape also puts ink down, and a 237-shape gap that+ // only reported itself on shapes that happened to have a fill would be a+ // number that under-reports exactly where it matters.+ if rung == Rung::Inherited && shape_text_len(shape) > 0 {+ *ctx.inherited_shapes_with_text += 1;+ }// `p:style`'s `fillRef`/`lnRef`/`effectRef` are not parsed yet, so the// three style arguments are `None` and 3,381 corpus shapes with no `spPr`// fill element resolve to nothing. That is an *under*-paint: visible, but@@ -542,7 +624,7 @@ fn paint_shape(shape: &Shape, ctx: &mut ShapeCtx<'_, '_>) {}let Some(slide_rect) = shape.slide_rect else {- *ctx.unpainted_shapes += 1;+ ctx.tally(rung, false);return;};// The *bounding* box, not the raw rect: `pptx::geometry` composes group@@ -554,21 +636,21 @@ fn paint_shape(shape: &Shape, ctx: &mut ShapeCtx<'_, '_>) {height: emu_to_pt(box_.size.height.raw()),};let Some(geometry) = props.geometry.as_ref() else {- *ctx.unpainted_shapes += 1;+ ctx.tally(rung, false);return;};let Some(path) = build_geometry(geometry, extent) else {// An unimplemented preset. Counted, never approximated by its bounding// box: a `rect` drawn where a `cloud` was asked for is a wrong slide,// not a coarse one.- *ctx.unpainted_shapes += 1;+ ctx.tally(rung, false);return;};if visuals.stroke.is_some() && props.outline.as_ref().is_some_and(|o| o.fill.is_none()) {*ctx.outlines_defaulted_black += 1;}- *ctx.painted_shapes += 1;+ ctx.tally(rung, true);ctx.commands.push(DrawCommand::Path {origin: PtOffset::new(emu_to_pt(box_.origin.x.raw()),@@ -585,6 +667,21 @@ fn paint_shape(shape: &Shape, ctx: &mut ShapeCtx<'_, '_>) {});}+/// How much text an inherited shape carries that this pass will not lay out.+///+/// Only `p:sp` bodies are counted. A `p:graphicFrame`'s table is a second gap+/// of a different shape, and 2 of them appear in layout parts against 237+/// `p:sp`s — folding the two would make the number harder to act on, not more+/// complete.+fn shape_text_len(shape: &Shape) -> usize {+ let ShapeKind::AutoShape(sp) = &shape.kind else {+ return 0;+ };+ sp.text.as_ref().map_or(0, |body| {+ body.paragraphs.iter().map(|p| p.text().len()).sum()+ })+}+/// The `spPr` a shape paints from. Groups and graphic frames have none of/// their own — a table's cell fills are `a:tcPr`, which this pass does not/// paint yet.