Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich356e308bfb8a
diff --git a/crates/liteparse-ooxml/src/docx/parse/drawing/schema/shape.rs b/crates/liteparse-ooxml/src/docx/parse/drawing/schema/shape.rs
index 50fe13a..1622b1c 100644
--- a/crates/liteparse-ooxml/src/docx/parse/drawing/schema/shape.rs
+++ b/crates/liteparse-ooxml/src/docx/parse/drawing/schema/shape.rs
@@ -560,7 +560,7 @@ pub(crate) struct WspXml {
// whose schema differs (`fontRef/@idx` is a string enum, not a number), so
// they're silently skipped via serde's default unknown-element behavior.
-#[derive(Deserialize, Default)]
+#[derive(Debug, Deserialize, Default)]
pub(crate) struct ShapeStyleXml {
#[serde(rename = "lnRef", default)]
pub(crate) ln_ref: Option<StyleMatrixRefXml>,
@@ -575,7 +575,7 @@ pub(crate) struct ShapeStyleXml {
}
/// §20.1.4.1.17 CT_FontReference.
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
pub(crate) struct FontRefXml {
#[serde(
rename = "@idx",
@@ -588,7 +588,7 @@ pub(crate) struct FontRefXml {
}
/// §20.1.8.30 ST_FontCollectionIndex.
-#[derive(Deserialize, Default)]
+#[derive(Debug, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub(crate) enum StFontCollectionIndex {
Major,
@@ -618,7 +618,7 @@ impl From<FontRefXml> for FontReference {
/// §20.1.4.2.19 CT_StyleMatrixReference — `idx` + optional color for
/// `phClr` substitution within the referenced theme style.
-#[derive(Deserialize)]
+#[derive(Debug, Deserialize)]
pub(crate) struct StyleMatrixRefXml {
/// Style-matrix index is cosmetic; zero selects the default entry.
#[serde(
diff --git a/crates/liteparse-ooxml/src/pptx/mod.rs b/crates/liteparse-ooxml/src/pptx/mod.rs
index 596f81d..682f64e 100644
--- a/crates/liteparse-ooxml/src/pptx/mod.rs
+++ b/crates/liteparse-ooxml/src/pptx/mod.rs
@@ -27,8 +27,8 @@ pub use media::MediaCache;
pub use package::{EMU_PER_POINT, Part, PresentationInfo, PresentationPackage, SlideParts, walk};
pub use shapes::{
AutoShape, Background, Connector, GraphicFrame, GraphicFramePayload, Group, Placeholder,
- PlaceholderKind, Shape, ShapeKind, SlidePart, Table, TableCell, TableCellProperties, TableRow,
- parse_shape_tree, parse_slide_part, visit_all,
+ PlaceholderKind, Shape, ShapeKind, ShapeStyle, SlidePart, Table, TableCell,
+ TableCellProperties, TableRow, parse_shape_tree, parse_slide_part, visit_all,
};
pub use text::{
AutoNumberScheme, Bullet, ListStyle, Spacing, TextBody, TextParagraph, TextParagraphProperties,
diff --git a/crates/liteparse-ooxml/src/pptx/shapes.rs b/crates/liteparse-ooxml/src/pptx/shapes.rs
index d57cc79..d5c1ac1 100644
--- a/crates/liteparse-ooxml/src/pptx/shapes.rs
+++ b/crates/liteparse-ooxml/src/pptx/shapes.rs
@@ -47,7 +47,7 @@ use serde::Deserialize;
use crate::model::dimension::{Dimension, Emu};
use crate::model::geometry::{Offset, Size};
use crate::model::{
- BodyProperties, ColorMap, DocProperties, DrawingFill, NvPicProperties, Picture,
+ BodyProperties, ColorMap, DocProperties, DrawingFill, FontReference, NvPicProperties, Picture,
ShapeProperties, StyleMatrixRef, TextAnchoringType, TextVerticalType, Transform2D,
};
@@ -56,8 +56,8 @@ use crate::docx::parse::drawing::schema::color::StSchemeColorVal;
use crate::docx::parse::drawing::schema::fill::BlipFillXml;
use crate::docx::parse::drawing::schema::picture::{CNvPicPrXml, CNvPrXml};
use crate::docx::parse::drawing::schema::shape::{
- ExtXml, OffXml, SpPrXml, StTextAnchoringType, StTextVerticalType, StyleMatrixRefXml, XfrmXml,
- pick_fill,
+ ExtXml, OffXml, ShapeStyleXml, SpPrXml, StTextAnchoringType, StTextVerticalType,
+ StyleMatrixRefXml, XfrmXml, pick_fill,
};
use crate::docx::parse::serde_xml;
@@ -98,9 +98,64 @@ pub struct Shape {
/// probe can still see the declared EMU, which is what they check against
/// the source XML.
pub slide_rect: Option<SlideRect>,
+ /// §19.3.1.46 `p:style` — the shape's references into the theme's style
+ /// matrices, when it declares one. 2,181 of the corpus's 17,024 shapes do.
+ ///
+ /// On [`Shape`] rather than on each [`ShapeKind`] variant because all three
+ /// kinds that can carry one (`p:sp`, `p:cxnSp`, `p:pic`) carry the *same*
+ /// `a:CT_ShapeStyle`, and both consumers — the fill emitter and, later, the
+ /// text cascade — reach it from the shape rather than from its payload.
+ /// `p:grpSp` and `p:graphicFrame` have no `style` in the schema and always
+ /// leave this `None`.
+ pub style: Option<ShapeStyle>,
pub kind: ShapeKind,
}
+/// §20.1.4.1.4 `a:CT_ShapeStyle` — the four theme-matrix references a
+/// `p:style` holds.
+///
+/// The spec requires all four children, and the corpus agrees exactly (2,181
+/// of each), but they are modelled as `Option` because a file that omits one
+/// should lose that reference and not the whole shape.
+///
+/// Deliberately *not* four loose fields on [`Shape`] the way
+/// [`crate::model::WordProcessingShape`] carries them: there they arrived one
+/// at a time as Word's needs appeared, and here all four exist from the start
+/// and are always read together.
+#[derive(Clone, Debug, Default)]
+pub struct ShapeStyle {
+ /// `a:lnRef` — the theme line style. Supplies the outline's colour when
+ /// `a:ln` declares none (179 corpus shapes, today's black default) and the
+ /// **entire** outline when there is no `a:ln` at all (312).
+ pub line_ref: Option<StyleMatrixRef>,
+ /// `a:fillRef` — the theme fill style, used only when `spPr` declares no
+ /// fill element of its own. 631 corpus shapes inherit through it, of which
+ /// 335 name `idx="0"` and therefore inherit *nothing*.
+ pub fill_ref: Option<StyleMatrixRef>,
+ /// `a:effectRef` — the theme effect style, consulted when the direct
+ /// `a:effectLst` is absent or empty.
+ pub effect_ref: Option<StyleMatrixRef>,
+ /// `a:fontRef` — the shape's default text colour and theme font
+ /// collection.
+ ///
+ /// Parsed here but **not yet consumed**: it belongs to the text cascade,
+ /// not the paint walk, and wiring it into one and not the other would make
+ /// the markdown emitter and the geometry pass disagree about a run's
+ /// colour. 2,181 corpus shapes declare one.
+ pub font_ref: Option<FontReference>,
+}
+
+impl From<ShapeStyleXml> for ShapeStyle {
+ fn from(x: ShapeStyleXml) -> Self {
+ Self {
+ line_ref: x.ln_ref.map(Into::into),
+ fill_ref: x.fill_ref.map(Into::into),
+ effect_ref: x.effect_ref.map(Into::into),
+ font_ref: x.font_ref.map(Into::into),
+ }
+ }
+}
+
impl Shape {
/// True when this shape declares no transform of its own and therefore
/// depends on the placeholder cascade for its position and size.
@@ -861,6 +916,11 @@ struct SpXml {
nv_sp_pr: Option<NvSpPrXml>,
#[serde(rename = "spPr", default)]
sp_pr: Option<SpPrXml>,
+ /// §19.3.1.46 `p:style`. Same `a:CT_ShapeStyle` the DOCX `wps:style`
+ /// holds, so the schema type is shared — the content models coincide
+ /// element for element, which is the only case where reusing one is safe.
+ #[serde(rename = "style", default)]
+ style: Option<ShapeStyleXml>,
#[serde(rename = "txBody", default)]
tx_body: Option<TextBodyXml>,
}
@@ -947,6 +1007,8 @@ struct PicXml {
blip_fill: BlipFillXml,
#[serde(rename = "spPr", default)]
sp_pr: Option<SpPrXml>,
+ #[serde(rename = "style", default)]
+ style: Option<ShapeStyleXml>,
}
#[derive(Debug, Deserialize)]
@@ -966,6 +1028,8 @@ struct CxnSpXml {
nv_cxn_sp_pr: Option<NvCxnSpPrXml>,
#[serde(rename = "spPr", default)]
sp_pr: Option<SpPrXml>,
+ #[serde(rename = "style", default)]
+ style: Option<ShapeStyleXml>,
}
#[derive(Debug, Deserialize)]
@@ -1275,6 +1339,7 @@ fn lower_sp(sp: SpXml) -> Shape {
transform: properties.as_ref().and_then(|p| p.transform),
transform_inherited: false,
slide_rect: None,
+ style: sp.style.map(Into::into),
kind: ShapeKind::AutoShape(Box::new(AutoShape {
properties,
text: sp.tx_body.map(TextBodyXml::into_model),
@@ -1300,6 +1365,7 @@ fn lower_pic(pic: PicXml) -> Shape {
transform,
transform_inherited: false,
slide_rect: None,
+ style: pic.style.map(Into::into),
kind: ShapeKind::Picture(Box::new(Picture {
nv_pic_pr: NvPicProperties {
cnv_pr: non_visual,
@@ -1319,6 +1385,7 @@ fn lower_cxn(cxn: CxnSpXml) -> Shape {
transform: properties.as_ref().and_then(|p| p.transform),
transform_inherited: false,
slide_rect: None,
+ style: cxn.style.map(Into::into),
kind: ShapeKind::Connector(Box::new(Connector { properties })),
}
}
@@ -1332,6 +1399,8 @@ fn lower_grp(grp: GrpSpXml) -> Shape {
transform,
transform_inherited: false,
slide_rect: None,
+ // §19.3.1.22: `p:grpSp` has no `style` child in the schema.
+ style: None,
kind: ShapeKind::Group(Box::new(Group {
properties: None,
child_offset: xfrm
@@ -1364,6 +1433,8 @@ fn lower_graphic_frame(gf: GraphicFrameXml) -> Shape {
transform: gf.xfrm.map(Into::into),
transform_inherited: false,
slide_rect: None,
+ // §19.3.1.21: `p:graphicFrame` has no `style` child in the schema.
+ style: None,
kind: ShapeKind::GraphicFrame(Box::new(GraphicFrame { payload })),
}
}
@@ -1538,6 +1609,39 @@ mod tests {
assert!(!shapes[0].needs_inherited_geometry());
}
+ /// §19.3.1.46. All four references, with the `phClr` substitute each one
+ /// carries — the substitute is the load-bearing half, since every theme
+ /// matrix entry on the corpus is written in terms of `phClr` and resolves
+ /// to black without it.
+ #[test]
+ fn shape_style_lowers_all_four_references() {
+ let shapes = tree(&sp(r#"<p:spPr/><p:style>
+ <a:lnRef idx="2"><a:schemeClr val="accent1"><a:shade val="50000"/></a:schemeClr></a:lnRef>
+ <a:fillRef idx="1"><a:schemeClr val="accent1"/></a:fillRef>
+ <a:effectRef idx="0"><a:schemeClr val="accent1"/></a:effectRef>
+ <a:fontRef idx="minor"><a:schemeClr val="lt1"/></a:fontRef>
+ </p:style>"#));
+ let style = shapes[0].style.as_ref().expect("p:style lowered");
+ let ln = style.line_ref.as_ref().expect("lnRef");
+ assert_eq!(ln.idx, 2);
+ assert!(ln.color.is_some(), "the phClr substitute must survive");
+ assert_eq!(style.fill_ref.as_ref().expect("fillRef").idx, 1);
+ // §20.1.4.2.19: 0 is the no-reference sentinel and is kept as declared,
+ // not normalised away — 335 corpus `fillRef`s rely on the resolver
+ // seeing it.
+ assert_eq!(style.effect_ref.as_ref().expect("effectRef").idx, 0);
+ assert!(style.font_ref.is_some(), "parsed, though unused for now");
+ }
+
+ /// A shape with no `p:style` gets `None`, not a default-constructed set of
+ /// references — an all-zero `ShapeStyle` would read as "asked the theme and
+ /// got nothing" rather than "never asked".
+ #[test]
+ fn shape_without_style_has_no_references() {
+ let shapes = tree(&sp("<p:spPr/>"));
+ assert!(shapes[0].style.is_none());
+ }
+
/// `p:ph@idx` reaches exactly `u32::MAX` in 13 of the 45 corpus decks.
/// A narrower integer wraps or hard-fails on all of them.
#[test]
diff --git a/crates/liteparse-ooxml/src/render/resolve/shape_visuals.rs b/crates/liteparse-ooxml/src/render/resolve/shape_visuals.rs
index 27387dd..fb71ba5 100644
--- a/crates/liteparse-ooxml/src/render/resolve/shape_visuals.rs
+++ b/crates/liteparse-ooxml/src/render/resolve/shape_visuals.rs
@@ -27,6 +27,15 @@ pub struct ResolvedVisuals {
pub fill: ResolvedFill,
pub stroke: Option<ResolvedStroke>,
pub effects: Vec<ResolvedEffect>,
+ /// True when the stroke is painted black because *nothing* declared a
+ /// colour — neither the direct `<a:ln>` nor the theme line style its
+ /// `lnRef` names. Distinct from a stroke that is black because the file
+ /// says black, which is not a defect.
+ ///
+ /// Reported rather than left for a caller to re-derive, because the only
+ /// way to re-derive it is to redo the theme lookup this function already
+ /// did, and a second copy of that lookup is a second thing to get wrong.
+ pub stroke_color_defaulted: bool,
}
/// Resolve the visual aspect of a shape (fill, outline, effects) into the
@@ -59,6 +68,7 @@ pub fn resolve_shape_visuals(
fill: resolve_theme_fill(style_fill_ref, theme, ctx, media),
stroke: None,
effects: Vec::new(),
+ stroke_color_defaulted: false,
};
}
};
@@ -71,10 +81,22 @@ pub fn resolve_shape_visuals(
};
let theme_ln = theme_line_style(style_line_ref, theme);
- let stroke = props
- .outline
- .as_ref()
- .and_then(|o| resolve_outline(o, theme_ln, ctx));
+ // §20.1.4.1.22: the theme line style is written in terms of `phClr`, which
+ // the `lnRef` itself supplies — the same substitution `resolve_theme_fill`
+ // does. Resolving the theme outline in the *shape's* context instead would
+ // hand `phClr` to a scheme lookup that has never heard of it.
+ let ln_ctx = match style_line_ref.and_then(|r| r.color.as_ref()) {
+ Some(c) => ctx.with_placeholder(resolve_drawing_color(c, ctx)),
+ None => *ctx,
+ };
+ let stroke = match props.outline.as_ref() {
+ Some(o) => resolve_outline(o, theme_ln, ctx, &ln_ctx),
+ // No `<a:ln>` at all: the `lnRef` is the whole outline, not just its
+ // defaults. 312 corpus shapes are in this case and PowerPoint strokes
+ // every one of them, so declining here is a missing outline rather
+ // than a conservative one.
+ None => theme_ln.and_then(|t| resolve_outline(t, None, &ln_ctx, &ln_ctx)),
+ };
let direct_effects = props
.effect_list
@@ -87,10 +109,12 @@ pub fn resolve_shape_visuals(
resolve_theme_effects(style_effect_ref, theme, ctx)
};
+ let stroke_color_defaulted = stroke.as_ref().is_some_and(|&(_, d)| d);
ResolvedVisuals {
fill,
- stroke,
+ stroke: stroke.map(|(s, _)| s),
effects,
+ stroke_color_defaulted,
}
}
@@ -322,11 +346,20 @@ pub fn resolve_blip_fill(fill: &crate::model::BlipFill, media: Option<&PartMedia
// ── Outline → Stroke ────────────────────────────────────────────────────────
+/// Resolve one `<a:ln>` into a painter stroke, with `theme_ln` supplying every
+/// property the direct outline omits.
+///
+/// Two colour contexts, because the two outlines are written in different
+/// vocabularies: `ctx` resolves the shape's own colours, and `ln_ctx` is `ctx`
+/// with `phClr` bound to the `lnRef`'s colour, which is the only context the
+/// theme line style's `<a:schemeClr val="phClr"/>` means anything in. Passing
+/// one context for both would silently paint every inherited stroke black.
fn resolve_outline(
outline: &Outline,
theme_ln: Option<&Outline>,
ctx: &DrawingColorContext<'_>,
-) -> Option<ResolvedStroke> {
+ ln_ctx: &DrawingColorContext<'_>,
+) -> Option<(ResolvedStroke, bool)> {
// Width: direct `@w` wins; else theme `lnRef`; else spec default 0.75pt.
// OOXML `w` is EMU (12700 per pt).
let width = outline
@@ -335,16 +368,28 @@ fn resolve_outline(
.map(emu_to_pt)
.unwrap_or_else(|| Pt::new(0.75));
- // Pull the outline color from its fill. If the fill is non-solid or
- // absent, default to black; Tier 0 cannot paint gradient strokes.
- let color = match outline.fill.as_ref() {
- Some(DrawingFill::Solid(c)) => resolve_drawing_color(c, ctx),
- Some(DrawingFill::None) => return None,
- Some(DrawingFill::Gradient(_) | DrawingFill::Blip(_) | DrawingFill::Pattern(_)) => {
+ // Pull the outline color from its fill. A direct fill wins; an outline
+ // with no fill of its own inherits the theme line style's, which is what
+ // `lnRef` exists for. Only when neither declares one is black correct.
+ let declared = outline
+ .fill
+ .as_ref()
+ .map(|f| (f, ctx))
+ .or_else(|| theme_ln.and_then(|t| t.fill.as_ref()).map(|f| (f, ln_ctx)));
+ let mut color_defaulted = false;
+ let color = match declared {
+ Some((DrawingFill::Solid(c), cx)) => resolve_drawing_color(c, cx),
+ Some((DrawingFill::None, _)) => return None,
+ Some((DrawingFill::Gradient(_) | DrawingFill::Blip(_) | DrawingFill::Pattern(_), _)) => {
log::warn!("shape_visuals: non-solid stroke fill not yet supported");
Rgba::BLACK
}
- Some(DrawingFill::Group) | None => Rgba::BLACK,
+ // `a:grpFill` on a stroke, or no colour anywhere in the cascade. Black
+ // is a guess in both cases, and the flag says so.
+ Some((DrawingFill::Group, _)) | None => {
+ color_defaulted = true;
+ Rgba::BLACK
+ }
};
let cap = outline
@@ -365,13 +410,16 @@ fn resolve_outline(
.map(|d| map_line_dash(d, width))
.unwrap_or(ResolvedDashPattern::Solid);
- Some(ResolvedStroke {
- width,
- color,
- dash,
- cap,
- join,
- })
+ Some((
+ ResolvedStroke {
+ width,
+ color,
+ dash,
+ cap,
+ join,
+ },
+ color_defaulted,
+ ))
}
fn map_line_cap(cap: LineCap) -> ResolvedLineCap {
@@ -725,6 +773,138 @@ mod tests {
assert_eq!(s.width, Pt::new(2.0));
assert_eq!(s.color.to_rgb24(), 0xD99F34);
assert_eq!(s.cap, ResolvedLineCap::Butt);
+ assert!(!v.stroke_color_defaulted, "the shape declared the colour");
+ }
+
+ /// A theme line style whose colour is the `phClr` placeholder, which is how
+ /// every one of the corpus's `lnStyleLst` entries is written.
+ fn theme_with_phclr_line(width_emu: i64) -> Theme {
+ use crate::model::DrawingColor;
+ Theme {
+ line_styles: vec![Outline {
+ width: Some(Dimension::new(width_emu)),
+ fill: Some(DrawingFill::Solid(DrawingColor::Scheme {
+ name: crate::model::SchemeColorVal::PhClr,
+ transforms: vec![],
+ })),
+ ..empty_outline()
+ }],
+ ..Theme::default()
+ }
+ }
+
+ /// §20.1.4.1.22: an `<a:ln>` that names no colour takes the theme line
+ /// style's, recoloured by the `lnRef`'s own `phClr` substitute — 179 corpus
+ /// shapes, every one of which used to be painted black.
+ #[test]
+ fn colourless_outline_takes_its_colour_from_the_line_ref() {
+ use crate::model::DrawingColor;
+ let props = shape_props(
+ None,
+ Some(Outline {
+ width: Some(Dimension::new(12_700)),
+ ..empty_outline()
+ }),
+ None,
+ );
+ let theme = theme_with_phclr_line(25_400);
+ let ln_ref = StyleMatrixRef {
+ idx: 1,
+ color: Some(DrawingColor::Srgb {
+ rgb: 0x4472C4,
+ transforms: vec![],
+ }),
+ };
+ let v = resolve_shape_visuals(
+ Some(&props),
+ Some(&ln_ref),
+ None,
+ None,
+ &DrawingColorContext::new(Some(&theme)),
+ None,
+ );
+ let s = v.stroke.expect("a colourless outline is still an outline");
+ assert_eq!(s.color.to_rgb24(), 0x4472C4, "phClr := the lnRef's colour");
+ // The direct `@w` still wins over the theme's 2pt.
+ assert_eq!(s.width, Pt::new(1.0));
+ assert!(!v.stroke_color_defaulted);
+ }
+
+ /// No `<a:ln>` at all: the `lnRef` is the whole outline. 312 corpus shapes,
+ /// which PowerPoint strokes and this resolver used to leave bare.
+ #[test]
+ fn line_ref_supplies_the_whole_outline_when_there_is_no_ln() {
+ use crate::model::DrawingColor;
+ let props = shape_props(None, None, None);
+ let theme = theme_with_phclr_line(25_400);
+ let ln_ref = StyleMatrixRef {
+ idx: 1,
+ color: Some(DrawingColor::Srgb {
+ rgb: 0xED7D31,
+ transforms: vec![],
+ }),
+ };
+ let v = resolve_shape_visuals(
+ Some(&props),
+ Some(&ln_ref),
+ None,
+ None,
+ &DrawingColorContext::new(Some(&theme)),
+ None,
+ );
+ let s = v.stroke.expect("the theme line style is the outline");
+ assert_eq!(s.color.to_rgb24(), 0xED7D31);
+ assert_eq!(s.width, Pt::new(2.0), "width comes from the theme too");
+ assert!(!v.stroke_color_defaulted);
+ }
+
+ /// The flag means *nothing declared a colour anywhere*, not "the stroke is
+ /// black". Without a `lnRef` there is nowhere else to look.
+ #[test]
+ fn colourless_outline_with_no_line_ref_reports_a_defaulted_colour() {
+ let props = shape_props(
+ None,
+ Some(Outline {
+ width: Some(Dimension::new(12_700)),
+ ..empty_outline()
+ }),
+ None,
+ );
+ let v = resolve_shape_visuals(
+ Some(&props),
+ None,
+ None,
+ None,
+ &DrawingColorContext::new(None),
+ None,
+ );
+ assert_eq!(v.stroke.unwrap().color.to_rgb24(), 0x000000);
+ assert!(
+ v.stroke_color_defaulted,
+ "black here is a guess, not a fact"
+ );
+ }
+
+ /// A shape with no `<a:ln>` and a `lnRef` the theme cannot satisfy stays
+ /// unstroked — the miss must not become an invented black outline.
+ #[test]
+ fn line_ref_out_of_range_leaves_the_shape_unstroked() {
+ let props = shape_props(None, None, None);
+ let theme = theme_with_phclr_line(25_400);
+ let ln_ref = StyleMatrixRef {
+ idx: 3,
+ color: None,
+ };
+ let v = resolve_shape_visuals(
+ Some(&props),
+ Some(&ln_ref),
+ None,
+ None,
+ &DrawingColorContext::new(Some(&theme)),
+ None,
+ );
+ assert!(v.stroke.is_none());
+ assert!(!v.stroke_color_defaulted);
}
#[test]
diff --git a/crates/liteparse/src/office/pptx_layout.rs b/crates/liteparse/src/office/pptx_layout.rs
index 05ecb37..1577d28 100644
--- a/crates/liteparse/src/office/pptx_layout.rs
+++ b/crates/liteparse/src/office/pptx_layout.rs
@@ -51,7 +51,8 @@
use liteparse_ooxml::model::dimension::Dimension;
use liteparse_ooxml::model::{
- Alignment, ColorMap, DrawingFill, PresetGeometryDef, PresetShapeType, ShapeGeometry, Theme,
+ Alignment, ColorMap, DrawingFill, PresetGeometryDef, PresetShapeType, ShapeGeometry,
+ StyleMatrixRef, Theme,
};
use liteparse_ooxml::pptx::{
self, PresentationPackage, ResolvedTextStyle, Shape, ShapeKind, Spacing, TextBody, TextCascade,
@@ -139,12 +140,38 @@ pub struct SlideGeometry {
/// cannot build — an unimplemented preset, or none declared. Their text is
/// still laid out; only their ink is missing.
pub unpainted_shapes: usize,
- /// Painted shapes whose `<a:ln>` names no colour of its own, so the shared
- /// resolver defaults it to black where DrawingML would inherit
- /// `p:style/a:lnRef`. The census put this at 7.7% of strokes; it is the one
- /// place this pass paints something *wrong* rather than painting nothing,
- /// and it is counted rather than hidden until `p:style` is parsed.
+ /// Painted shapes whose stroke is black because **nothing** named a
+ /// colour — not the `<a:ln>`, and not the theme line style its
+ /// `p:style/a:lnRef` points at.
+ ///
+ /// Still the one place this pass paints something *wrong* rather than
+ /// painting nothing, but it now means what it says: before `p:style` was
+ /// parsed it also counted every outline whose colour was sitting in the
+ /// theme, unread.
pub outlines_defaulted_black: usize,
+ /// Shapes carrying a `p:style` that the paint walk consulted. 2,181 on the
+ /// corpus, of which 1,744 are on slides and the rest on layouts and
+ /// masters.
+ pub shapes_with_style: usize,
+ /// Shapes whose fill came from the theme's `fillStyleLst` via `a:fillRef`,
+ /// because `spPr` declared no fill element at all. Counted after
+ /// resolution, so an `idx="0"` reference — 335 corpus shapes, more than
+ /// half of those that inherit — is not counted here.
+ pub fills_from_style_ref: usize,
+ /// Shapes stroked entirely from the theme's `lnStyleLst`, having no
+ /// `<a:ln>` of their own. This is ink the pass did not put down before,
+ /// which makes it the one counter here that can only grow the raster.
+ pub strokes_from_style_ref: usize,
+ /// Shapes that ask a style matrix for a fill or an outline — a non-zero
+ /// `idx`, with nothing declared locally — and get **nothing back**: the
+ /// theme part is missing, or its `fillStyleLst`/`lnStyleLst` is shorter
+ /// than the index.
+ ///
+ /// The honest residue of the two counters above. Without it a theme that
+ /// resolves to an empty matrix is indistinguishable from a corpus that
+ /// never asked, and the difference is a shape PowerPoint paints and we do
+ /// not.
+ pub style_refs_unresolved: usize,
/// Per page, the command index of the full-slide background
/// [`DrawCommand::Path`], when one was emitted. Parallel to `pages`.
///
@@ -321,6 +348,10 @@ fn layout_deck(pkg: &PresentationPackage, registry: &FontRegistry) -> SlideGeome
painted_shapes: 0,
unpainted_shapes: 0,
outlines_defaulted_black: 0,
+ shapes_with_style: 0,
+ fills_from_style_ref: 0,
+ strokes_from_style_ref: 0,
+ style_refs_unresolved: 0,
inherited_shapes_painted: 0,
inherited_shapes_unpainted: 0,
inherited_shapes_with_text: 0,
@@ -366,6 +397,10 @@ fn layout_deck(pkg: &PresentationPackage, registry: &FontRegistry) -> SlideGeome
painted_shapes: &mut out.painted_shapes,
unpainted_shapes: &mut out.unpainted_shapes,
outlines_defaulted_black: &mut out.outlines_defaulted_black,
+ shapes_with_style: &mut out.shapes_with_style,
+ fills_from_style_ref: &mut out.fills_from_style_ref,
+ strokes_from_style_ref: &mut out.strokes_from_style_ref,
+ style_refs_unresolved: &mut out.style_refs_unresolved,
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,
@@ -457,6 +492,10 @@ struct ShapeCtx<'a, 'r> {
painted_shapes: &'a mut usize,
unpainted_shapes: &'a mut usize,
outlines_defaulted_black: &'a mut usize,
+ shapes_with_style: &'a mut usize,
+ fills_from_style_ref: &'a mut usize,
+ strokes_from_style_ref: &'a mut usize,
+ style_refs_unresolved: &'a mut usize,
inherited_shapes_painted: &'a mut usize,
inherited_shapes_unpainted: &'a mut usize,
inherited_shapes_with_text: &'a mut usize,
@@ -680,18 +719,47 @@ fn paint_shape(shape: &Shape, from: Source<'_>, ctx: &mut ShapeCtx<'_, '_>) {
if from.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
- // never misleading.
+ // §19.3.1.46 `p:style` — the theme-matrix references this shape inherits
+ // its fill and outline from when `spPr` declares none of its own. 2,181
+ // corpus shapes carry one; `a:fontRef` is deliberately not consulted here,
+ // because a run colour resolved in the paint walk and not in the text
+ // cascade would make the two disagree.
+ let style = shape.style.as_ref();
let mut visuals = resolve_shape_visuals(
Some(props),
- None,
- None,
- None,
+ style.and_then(|s| s.line_ref.as_ref()),
+ style.and_then(|s| s.effect_ref.as_ref()),
+ style.and_then(|s| s.fill_ref.as_ref()),
&DrawingColorContext::new(ctx.theme).with_color_map(ctx.color_map),
Some(from.media),
);
+ // Where the ink came from, read *before* the picture override below
+ // replaces the fill: a `p:pic` whose frame inherits a theme fill still
+ // inherited it, and the photograph that lands on top is a different fact.
+ // Both are tallied only once the shape is known to paint, so they stay
+ // subsets of `painted_shapes`.
+ let fill_from_ref = props.fill.is_none() && !matches!(visuals.fill, ResolvedFill::None);
+ let stroke_from_ref = props.outline.is_none() && visuals.stroke.is_some();
+ // A reference that named a matrix entry and came back empty. `idx == 0` is
+ // the spec's "no reference" sentinel — 335 corpus `fillRef`s use it — and
+ // is not a miss.
+ let asked = |r: Option<&StyleMatrixRef>| r.is_some_and(|r| r.idx != 0);
+ let missed_fill = props.fill.is_none()
+ && asked(style.and_then(|s| s.fill_ref.as_ref()))
+ && matches!(visuals.fill, ResolvedFill::None);
+ let missed_stroke = props.outline.is_none()
+ && asked(style.and_then(|s| s.line_ref.as_ref()))
+ && visuals.stroke.is_none();
+ if style.is_some() {
+ *ctx.shapes_with_style += 1;
+ }
+ // Counted here rather than beside the two "inherited" tallies below: a
+ // shape whose only ink was the reference that missed does not paint at
+ // all, and a miss that reported itself only on shapes that painted anyway
+ // would hide exactly the case it exists to expose.
+ if missed_fill || missed_stroke {
+ *ctx.style_refs_unresolved += 1;
+ }
// §19.3.1.37: a `p:pic`'s image is its own `p:blipFill`, a sibling of
// `spPr` — not an `spPr` fill element. So the picture's fill has to be
// resolved separately and take precedence; reading `shape_properties`
@@ -757,9 +825,18 @@ fn paint_shape(shape: &Shape, from: Source<'_>, ctx: &mut ShapeCtx<'_, '_>) {
return;
};
- if visuals.stroke.is_some() && props.outline.as_ref().is_some_and(|o| o.fill.is_none()) {
+ // Asked of the resolver rather than re-derived from `props`: whether a
+ // colourless `<a:ln>` is a defect now depends on what the `lnRef` found in
+ // the theme, and only the resolver did that lookup.
+ if visuals.stroke_color_defaulted {
*ctx.outlines_defaulted_black += 1;
}
+ if fill_from_ref {
+ *ctx.fills_from_style_ref += 1;
+ }
+ if stroke_from_ref {
+ *ctx.strokes_from_style_ref += 1;
+ }
ctx.tally(from.rung, true);
if matches!(visuals.fill, ResolvedFill::Blip(_)) {
*ctx.blips_painted += 1;