Files
craft-libs/crates/ppt-binary/src/tests.rs
T
cbf6840b10 Add craft-ppt-binary: DeckCraft's PowerPoint 97-2003 reader on craft-cfb and craft-pptx (#2316)
* ppt-binary: open PowerPoint 97-2003 .ppt/.pot/.pps files

New crate deckcraft-ppt (L3), written from the public [MS-CFB], [MS-PPT]
and [MS-ODRAW] Open Specifications: a compound-file reader (ported from
FilmCraft's clean-room reader), the edit chain and persist directory, and
the OfficeArt drawings. Main masters become masters (colours, fonts, text
styles, background, decorations), title masters become layouts; slides
keep their shapes (presets, freeforms, groups, rotation, flips), fills,
lines, shadows, pictures (PNG, JPEG, DIB, TIFF, EMF, WMF), text with the
master styles resolved into it, fields, notes and hidden flags.

Import only: an opened .ppt has no save path, so Save asks for a new
name. A .pps from the system starts the slide show. File dialogs, drag
and drop, the web shell and the macOS, Windows and Linux file
associations offer the three extensions. Tests build .ppt files from
records and sweep hostile input.

From storytold/deckcraft@0aef89cb1a (storytold/deckcraft#1137).

* ppt-binary: address review: budget picture decoding, read each persist directory once, macOS .pot/.pps types

- Pictures decode lazily, when a shape first uses them, each at most once and all
  within a shared 4 GiB budget: many store entries pointing at one deflated blob
  can no longer ask for unbounded memory.
- A persist directory named by many edits is read once.
- Info.plist declares com.microsoft.powerpoint.pot and .pps alongside .ppt.

From storytold/deckcraft@9e575ccecc (storytold/deckcraft#1137).

* ppt-binary: address review: check the picture budget before copying bitmap data

From storytold/deckcraft@470a429392 (storytold/deckcraft#1137).

* ppt-binary: build against current main

- convert.rs: set `flip: None` on imported gradients. `Gradient` gained the field in
  ad97533a; None is what an unflipped gradient was before.
- tests.rs: split `imports_a_presentation` into one check per section (title, picture,
  group, text box, master) for the workspace `cognitive_complexity` lint (6ddd2fb8).
  The assertions are unchanged.
- Cargo.lock: deckcraft-ppt takes the workspace version, 0.15.0.

From storytold/deckcraft@a0ececefeb (storytold/deckcraft#1137).

* ppt-binary: keep the picture budget on 32-bit targets

`MAX_TOTAL` was `4 << 30` as a `usize`. On wasm32 that is 2^32, which wraps to 0
without any error, so the web build's picture budget was zero and every .ppt
picture was dropped. Use the address space as the limit there. A compile-time
assertion that the budget holds at least one picture keeps it from happening
again on any target: the CI wasm step (`cargo check`) now fails if it does.

From storytold/deckcraft@4fd930ce1a (storytold/deckcraft#1137).

* ppt-binary: read past the character fields PowerPoint 2002 and later add

A TextCFException can end with pp10runid (mask bit 20), newEAFontRef (24),
csFontRef (25) and pp11ext (26), after `position`. read_cf stopped at
`position`, so in a StyleTextPropAtom the next run's count and mask were read
from the leftover bytes, and every later run got garbage formatting or the
walk ended. Master text styles go through the same reader. The fields are
read past and not used. The test has a run carrying all four followed by a
bold 18 pt run, which now reads as written.

From storytold/deckcraft@773af639c9 (storytold/deckcraft#1137).

* ppt-binary: convert each listed object once and index notes pages

Nothing stopped a slide list from naming one persist object many times, and
each listing converted its whole drawing again: a 100k-shape slide listed
180k times fits in about 10 MB and makes 1.8e10 shapes. An object is now
listed once across the slide, master and notes lists (PowerPoint never lists
one twice), and a repeat is logged and skipped. Notes pages are looked up
through a map built once, instead of a scan per slide (slides x notes). The
first matching page still wins.

From storytold/deckcraft@b3aa0983bd (storytold/deckcraft#1137).

* ppt-binary: find formatting runs by binary search

paragraphs() found the run at a position (rposition) and the next run start
(find) by scanning the run starts for every run, break and field, so text
cost grew with chars x runs. The starts are a running sum and never
decrease, so both lookups are now partition_point. Measured in a debug
build, 640k one-character runs (about 6 MB of file): 75 s before, about 2 s
after. The test uses a 20 s bound, like render's pathgrad budget test.

From storytold/deckcraft@b70b022362 (storytold/deckcraft#1137).

* ppt-binary: cap decoded pictures at the .pptx package total

The picture budget was 4 GiB, so a few MB of small deflated metafiles, each
inflating to 64 MiB, could make the import hold 4 GiB. A .pptx package may
inflate to 2 GiB in total (deckcraft-pptx opc MAX_TOTAL), and pictures are
the only part of a .ppt this crate inflates, so use the same 2 GiB. That also
fits a 32-bit usize, which replaces the wasm32 special case. This is a limit
change, so there is no new test: the budget test
(the_store_decodes_lazily_within_a_budget) and the compile-time check that
the budget holds a picture still cover it.

From storytold/deckcraft@a9cefff8b8 (storytold/deckcraft#1137).

* ppt-binary: fall back to the first main master the same way every time

A slide naming a master that doesn't exist fell back to the lowest index in
the masters HashMap. A title master is stored with its main master's index,
so the two tied and the HashMap's random order picked one: the same file got
the "Title Master" layout on some opens and the main master's on others. The
fallback now considers main masters only. The test opens such a deck 30
times; before the fix it took the title master's layout within those opens.

From storytold/deckcraft@60942d5950 (storytold/deckcraft#1137).

* ppt-binary: try the edit scan before calling a file too old

Any Current User token other than the plain or encrypted one, including none
at all from a truncated record, returned TooOld at once. A PowerPoint 97 file
with a damaged Current User stream was reported as "made by PowerPoint 95 or
earlier", and the UserEditAtom scan meant for a missing stream never ran.
[MS-PPT] allows only those two tokens, so an unknown one can't tell a damaged
file from an older format. The scan now runs, and TooOld is returned only when
it finds no edit record behind an unknown token.

From storytold/deckcraft@3869c3dc51 (storytold/deckcraft#1137).

* ppt-binary: read persist directories within one stream's worth of words

dirs_seen only skips a directory named at the same offset again. Directories
nested inside one another, each named by its own UserEditAtom, were each read
to the end of the stream: MAX_EDITS x the stream. In a debug build, 4095 such
edits over a 64 MiB stream took about 48 s. All directories now share a budget
of stream.len() / 4 words. Real directories are separate records inside the
stream, so they always fit. The hostile case stops in milliseconds and is
reported as damaged.

From storytold/deckcraft@5799048d88 (storytold/deckcraft#1137).

* ppt-binary: check two additions on input-derived sizes

- cfb.rs: the mini sector end `at + MINI_SECTOR_SIZE` was unchecked. On a
  32-bit target (wasm32), a sector number near 0x03FF_FFFF puts `at` at the
  top of usize, so the add panics in a debug build and wraps in release.
- text.rs: the style-run total `chars.len() as u32 + 1` truncated and could
  overflow past u32::MAX characters. It now saturates.

Neither case can be reached in a native test (one needs a 32-bit target, the
other more than 4 GiB of text), so there is no new test. The `+ 1`s in
convert.rs paragraphs() that the review also named are bounded by the text
length and can't overflow, so they are unchanged.

From storytold/deckcraft@b58c354246 (storytold/deckcraft#1137).

* ppt-binary: a sector chain that revisits a sector is an error

read_chain stopped at the stream's size, so a FAT loop shorter than the stream returned the
looped sectors again as the stream's bytes. It now tracks visited sectors and fails on a revisit,
and reserves the stream's size (capped at the file length) up front.

a_looping_chain_stops expected the repeated bytes; it now expects the error (eliets2's review).

From storytold/deckcraft@2b0940bff5 (storytold/deckcraft#1137).

* ppt-binary: slide shapes inherit from their own master's shapes

Master shape properties were remembered by shape id alone, so a later master (a title master,
or a second main master) that reused a shape id replaced the first master's entry, and slides
inherited fill and line from the wrong master. They are now keyed by master slide id and shape id,
and a shape's hspMaster is looked up in its slide's master, then a title master's main master.
Notes pages have no imported master, so they inherit nothing.

PowerPoint gives shapes file-unique ids, so this takes a damaged or generated file (eliets2's
review).

From storytold/deckcraft@7ad2f5152b (storytold/deckcraft#1137).

* ppt-binary: drop the unused fBackground field

RawShape.background was set from the FSP flag but never read: the background shape is the
drawing container's own shape (OfficeArtDgContainer.shape, [MS-ODRAW] 2.2.13), which drawing()
already takes. The comment there now says so (eliets2's review).

From storytold/deckcraft@afd57f47e0 (storytold/deckcraft#1137).

* ppt-binary: shadowed text keeps its shadow

TextCFException's shadow bit was read and merged but never reached the run. A shadowed run now
gets a shadow in the colour scheme's shadow colour, at the distance and direction of the app's own
Text Shadow command, with no blur (the record has none). Emboss has no counterpart in the model,
so its field is gone and the loss is listed in the docs (eliets2's review).

From storytold/deckcraft@ab5cf618e1 (storytold/deckcraft#1137).

* ppt-binary: the .pps show-on-open test opens a real 97-2003 file

a_ppsx_from_the_system_starts_the_show saved a .pptx under the name "Old talk.pps", so it went
through the PPTX path and never reached the legacy importer. crates/ppt/tests/fixtures/old-talk.pps
is a one-slide .pps built from records by the ppt tests (rebuilt with DECKCRAFT_WRITE_PPT_FIXTURE=1,
checked on every run), attributed CC0 like the other generated fixtures, and the UI test opens it
(eliets2's review).

From storytold/deckcraft@f511a2a66b (storytold/deckcraft#1137).

* ppt-binary: test mini streams, 4096-byte sectors and the DIFAT chain

The compound-file test builder only made version-3 files with 512-byte sectors, no mini stream
and every FAT sector in the header, so read_mini_chain, 4096-byte sectors and the DIFAT chain never
ran. build_with takes the sector shift and the mini stream cutoff, and lists FAT sectors past the
header's 109 in DIFAT sectors; build is build_with(streams, 9, 0), so existing tests see the same
layout. Three tests read streams back through each path (eliets2's review).

From storytold/deckcraft@212788b693 (storytold/deckcraft#1137).

* ppt-binary: test every picture kind, two-UID blips and blips inside the FBSE

The blip tests covered PNG, deflated WMF and DIB only. New tests decode JPEG (RGB and CMYK), TIFF
and PNG with one and two UIDs, an uncompressed EMF with each UID count, a metafile over the budget
(compressed or not), and a blip held inside its FBSE rather than in the Pictures stream. All
passed as written (eliets2's review).

From storytold/deckcraft@fe8023b9c2 (storytold/deckcraft#1137).

* ppt-binary: placeholder text held only in the slide list is imported

A shape's client text box may hold just an OutlineTextRefAtom, with no TextHeaderAtom ([MS-PPT]
OfficeArtClientTextbox): the text lives in the slide list, and that is the usual form for slide
placeholders. read_block returned None without a header, so text_block never reached its
outline fallback and the placeholder came in empty. A block with a reference and no header is
now returned for the caller to follow.

placeholder_text_can_come_from_the_slide_list covers the reference alone (it failed before this
change) and a header with a reference; the test document builder can put outline blocks in the
slide list (streams_with_outline). Found writing the test eliets2's review asked for.

From storytold/deckcraft@bf8ec603f3 (storytold/deckcraft#1137).

* ppt-binary: test newer edits and the two other encryption signs

doc::read had tests for one edit and the Current User token only. a_later_edit_replaces_a_slide
adds an edit whose persist directory gives the slide a new object; the newer slide is imported and
the master from the older directory is kept. encryption_is_found_in_the_edit_and_the_persist_directory
covers an edit record long enough for encryptSessionPersistIdRef and a CryptSession object named
by a later edit. Both passed as written (eliets2's review).

From storytold/deckcraft@f0b8525cc6 (storytold/deckcraft#1137).

* ppt-binary: test title masters and slides that follow nothing

No test made a slide of a title master or a slide with the follow-master flags off. The new test
checks that a title master becomes a "Title Master" layout with its own background and shapes
and without the master's shapes, that its slide uses that layout and follows its background, and
that a slide with all three flags off keeps its own background and colour scheme and hides the
master's shapes. It passed as written (eliets2's review).

From storytold/deckcraft@a41bf545e7 (storytold/deckcraft#1137).

* ppt-binary: merge boolean properties flag by flag

A boolean property word holds sixteen flags, each with its own fUse bit.
Props::inherit and add_table kept the whole word from the most specific
set, so a placeholder or text box that set only fNoFillHitTest lost the
inherited "not filled" and "no line" and was drawn with a fill and an
outline. Merge those words per flag instead (storytold/deckcraft#1137 review, issue 1).

From storytold/deckcraft@79ae833152 (storytold/deckcraft#1137).

* ppt-binary: recover a broken edit chain and report lost slides

An edit whose previous-edit offset points back at itself (or anywhere
that isn't an edit) cut the older edits off, and with them the persist
directories holding the slides: such a file opened as an empty deck
with no warning. When the chain doesn't end at offset 0, read every
edit's persist directory in the stream, newest first, filling only what
the chain left unset.

Slides the slide list names but whose objects are still missing are now
returned by deckcraft_ppt::import_checked and reported like a damaged
.pptx: lostSlides, a "(Repaired)" copy and the CLI warning
(storytold/deckcraft#1137 review, issue 3).

From storytold/deckcraft@135674101f (storytold/deckcraft#1137).

* ppt-binary: picture frames, inline picture fills, gradients, footers, numbering

The remaining differences from LibreOffice in the storytold/deckcraft#1137 stress test:

- Picture frames (and OLE frames) that say nothing about fill or line
  start with neither, as placeholders and text boxes do, instead of the
  drawing defaults' fill.
- A pib or fillBlip held in the property's own complex bytes (fBid) is
  decoded within the picture budget: table cells with a picture fill
  showed a plain colour.
- Shaded fills put the back colour at fillFocus ([MS-ODRAW]), and the
  linear angle follows how LibreOffice writes and reads it (0 degrees
  from the top): the linear gradient was reversed, the radial inside out.
- HeadersFooters (the slide's, else the document's) add the date,
  footer and slide number placeholders; the master's footer placement,
  alignment, size and colour reach every layout.
- Numbering from the document's ___PPT9 tag (OutlineTextProps9, pp9rt
  in the runs' fontStyle) turns bulleted paragraphs into numbered ones.
- Wingdings diamonds draw as a diamond, not a dot.

From storytold/deckcraft@24437bd075 (storytold/deckcraft#1137).

* ppt-binary: read CString records up to their first NUL

Header/footer text and the ___PPT9 tag name were decoded from the whole
CString body. A writer that ends the string with a NUL would put the NUL
into the footer text and make the tag name fail to match, losing the
numbering. Stop at the first NUL, as the font names already do.

From storytold/deckcraft@110e1420fd (storytold/deckcraft#1137).

* ppt-binary: on craft-cfb and craft-pptx (the move from DeckCraft)

What the move from storytold/deckcraft#1137 changes; the commits before this one are that
PR's crates/ppt as it is on its branch, without its manifest.

- craft-cfb replaces the crate's own compound-file reader (cfb.rs, 552 lines). Streams are
  read capped at the file's length, as before. Its six tests (looping chains, mini streams,
  version 4 sectors, the DIFAT) are covered by craft-cfb's own. The byte helpers move to
  record.rs. Test files are written by craft-cfb's writer.
- deckcraft-model, deckcraft-color and deckcraft-geom become craft-pptx and craft-drawingml,
  the same types (storytold/deckcraft#2179, storytold/deckcraft#2180 and #792's plan). A gradient now states its
  shape and rotate-with-shape explicitly, as craft-drawingml's are optional.
- DeckCraft's default master (defaults::build_master and blank_presentation) becomes the
  caller's craft_pptx::Defaults: import(bytes, &defaults), as craft_pptx::import takes. The
  file's masters take the default master's layouts and placeholders, then the file's theme,
  placeholder boxes and styles. The tests use a default master with title, body and footer
  placeholders and the seven layouts .ppt layout types map to; a new test reads with
  craft_pptx::Plain.
- The two tests that drive DeckCraft's engine (open and save, lost slides in the app) stay in
  DeckCraft.
- The fixture's rebuild variable is CRAFT_WRITE_PPT_FIXTURE; old-talk.pps is unchanged and
  gets its ATTRIBUTION row. Bare #1137 references in comments name storytold/deckcraft#1137.
- README, crate README (doctested) and the workspace entry.

* ppt-binary: bound text, strings, paths and repeated work from hostile files

Tab stops keep the first 64 entries while still reading past the rest, string properties stop
at 4096 units, and a freeform path uses at most 8192 vertices, since each of these is copied
into every paragraph or inheriting shape. An outline block in the slide list is the text of
one shape only; a second reference keeps the shape's own text.

The fallback master and each master's layout per type are found once before the slides are
converted, a shape's primary property tables are collected without inserting at the front,
and a compressed metafile that fails to inflate is charged to the picture budget.

* ppt-binary: review fixes for footers, rulers, title masters, paths and numbering

A slide's own headers and footers record (instance 0) now wins over the document's, and a
text box that points at an outline block keeps the margins and indents of its own ruler. A
slide list entry whose object is not a slide is reported as lost.

Title masters place their layout's placeholders from their own placeholder boxes, adding any
the title layout lacks, and copy only placeholders from an earlier title layout. Slides that
don't name a title master no longer pick its layout.

Freeform paths skip the points of escape segments, and without segments follow shapePath
(closed lines by default, open lines or curves). DIB colour tables are sized by bit depth and
refused when they run past the bitmap. Numbering schemes 0x10 to 0x28 map to their DrawingML
names, master style levels go to the level they name, and a broken edit chain reads the newest
edits rather than the oldest.

The nested persist directory test now checks the file is reported damaged, the footer test
fails when a layout has no slide number, and the README says tables come in as groups of
shapes.

* ppt-binary: second review round: failed pictures, title layouts, notes, arcs and property tables

Inflating a compressed metafile is now charged its compressed bytes as well as its output, and
is refused when those bytes alone pass the remaining budget, so many entries pointing at one
stream that inflates to nothing can't each read it again for free.

A title master's layout starts only from the defaults' own title layout, not one made for an
earlier title master. A notes page shared by two slide records with the same slide id goes to
the first only. After elliptical quadrants or a quadratic Bezier escape a path goes on from
their last point; after an arc or angle ellipse, whose end isn't one of its vertices, the path
stops rather than draw from the wrong place. A property set keeps at most 1024 distinct
properties, so copying the drawing defaults into each shape costs a bounded amount.

* ppt-binary: take each main master's title-layout base once; test outline notes

A title master's layout starts from the placeholders of the defaults' title layout. They are now
taken once, when the main master is made, instead of rebuilding the set of title-master layouts
and rescanning the master's layouts for every title master, which was quadratic in title masters.

A notes page whose body text is in the notes list's outline block also goes to one slide only.

---------

Co-authored-by: Oliwier Kulczycki <oleknuso282@gmail.com>
Co-authored-by: kiwi4boy <kiwi4boy@gmail.com>
2026-11-18 23:23:02 -05:00

1246 lines
60 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! End-to-end tests over `.ppt` files built here from records (never files made by PowerPoint).
#![allow(clippy::unwrap_used)]
use craft_pptx::text::{Align, Bullet, RunKind};
use craft_pptx::{
Background, Defaults, Fill, Layout, LayoutId, LayoutType, Master, MasterId, PhType, Placeholder, Presentation, Rgba, Shape, ShapeId, ShapeKind,
Size, Slide, TextBody, Xfrm,
};
use crate::doc::rt;
use crate::draw::prop;
use crate::record::build::{atom, container};
use crate::text::tests::{cf_bytes, pf_bytes};
/// A default master as an application gives one: title, body and footer placeholders on the
/// master, and the layouts `.ppt` layout types map to, each with its placeholders. The `.ppt`
/// masters place and style these.
struct AppDefaults;
impl Defaults for AppDefaults {
fn master(&self, size: Size, after: u32) -> (Master, u32) {
let next = std::cell::Cell::new(after);
let id = || {
next.set(next.get() + 1);
next.get()
};
let (w, h) = (size.width, size.height);
let ph = |kind: PhType, name: &str, x: [f64; 4]| Shape {
id: ShapeId(id()),
name: name.into(),
xfrm: Some(Xfrm::new(x[0] * w, x[1] * h, x[2] * w, x[3] * h)),
ph: Some(Placeholder { kind, ..Default::default() }),
text: Some(TextBody::default()),
..Default::default()
};
let title = || ph(PhType::Title, "Title", [0.06, 0.05, 0.88, 0.17]);
let footers = || {
vec![
ph(PhType::Date, "Date", [0.06, 0.92, 0.22, 0.05]),
ph(PhType::Footer, "Footer", [0.33, 0.92, 0.34, 0.05]),
ph(PhType::SlideNum, "Slide Number", [0.72, 0.92, 0.22, 0.05]),
]
};
let mut shapes = vec![title(), ph(PhType::Body, "Text", [0.06, 0.25, 0.88, 0.62])];
shapes.extend(footers());
let kinds = [
(LayoutType::Title, "Title Slide"),
(LayoutType::TitleAndContent, "Title and Content"),
(LayoutType::TitleOnly, "Title Only"),
(LayoutType::TwoContent, "Two Content"),
(LayoutType::VerticalTitleAndText, "Vertical Title and Text"),
(LayoutType::TitleAndVerticalText, "Title and Vertical Text"),
(LayoutType::Blank, "Blank"),
];
let layouts = kinds
.into_iter()
.map(|(kind, name)| {
let mut l: Vec<Shape> = if kind == LayoutType::Blank { vec![] } else { vec![title()] };
l.extend(footers());
Layout { id: LayoutId(id()), name: name.into(), kind, shapes: l, show_master_shapes: true, ..Default::default() }
})
.collect();
let master = Master { id: MasterId(id()), name: "Default".into(), shapes, layouts, ..Default::default() };
(master, next.get())
}
}
fn import(bytes: &[u8]) -> Result<Presentation, crate::PptError> {
crate::import(bytes, &AppDefaults)
}
fn import_checked(bytes: &[u8]) -> Result<(Presentation, Vec<String>), crate::PptError> {
crate::import_checked(bytes, &AppDefaults)
}
/// A version 3 compound file holding `streams` at its root.
fn build(streams: &[(&str, &[u8])]) -> Vec<u8> {
craft_cfb::write_streams(craft_cfb::Version::V3, streams).unwrap()
}
/// With no default master of the caller's, a file's masters still read: the plain master has no
/// placeholders for the file's to place, and the slides keep their own.
#[test]
fn plain_defaults_still_read_the_file() {
let p = crate::import(&file(), &craft_pptx::Plain).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].shapes[0].text.as_ref().unwrap().text(), "Hello\nWorld");
assert!(p.layout(p.slides[0].layout).is_some());
}
fn le32(v: &[u32]) -> Vec<u8> {
v.iter().flat_map(|x| x.to_le_bytes()).collect()
}
fn utf16(s: &str) -> Vec<u8> {
s.encode_utf16().flat_map(u16::to_le_bytes).collect()
}
fn fopt(entries: &[(u16, u32)]) -> Vec<u8> {
let mut b = vec![];
for (id, op) in entries {
b.extend_from_slice(&id.to_le_bytes());
b.extend_from_slice(&op.to_le_bytes());
}
atom(rt::FOPT, entries.len() as u16, &b)
}
fn fsp(shape_type: u16, spid: u32, flags: u32) -> Vec<u8> {
atom(rt::FSP, shape_type, &le32(&[spid, flags]))
}
/// A client anchor in master units: top, left, right, bottom.
fn anchor(t: i16, l: i16, r: i16, b: i16) -> Vec<u8> {
atom(rt::CLIENT_ANCHOR, 0, &[t, l, r, b].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<_>>())
}
fn text_box(text_type: u32, text: &str, extra: &[Vec<u8>]) -> Vec<u8> {
let mut kids = vec![atom(rt::TEXT_HEADER, 0, &text_type.to_le_bytes()), atom(rt::TEXT_CHARS, 0, &utf16(text))];
kids.extend_from_slice(extra);
container(rt::CLIENT_TEXTBOX, 0, &kids)
}
fn placeholder(kind: u8) -> Vec<u8> {
container(rt::CLIENT_DATA, 0, &[atom(rt::PLACEHOLDER_ATOM, 0, &[0, 0, 0, 0, kind, 0, 0, 0])])
}
fn drawing(background: Option<Vec<u8>>, shapes: &[Vec<u8>]) -> Vec<u8> {
let patriarch = container(rt::SP_CONTAINER, 0, &[atom(rt::FSPGR, 1, &[0; 16]), fsp(0, 1024, 0x5)]);
let mut tree = vec![patriarch];
tree.extend_from_slice(shapes);
let mut dg = vec![container(rt::SPGR_CONTAINER, 0, &tree)];
dg.extend(background);
container(rt::DRAWING, 0, &[container(rt::DG_CONTAINER, 0, &dg)])
}
fn solid_background(rgb: u32) -> Vec<u8> {
container(rt::SP_CONTAINER, 0, &[fsp(1, 1025, 0x400 | 0x800), fopt(&[(prop::FILL_COLOR, rgb), (prop::FILL_BOOLS, 0x0010_0010)])])
}
fn slide_atom(geom: u32, master_id: u32, flags: u16) -> Vec<u8> {
let mut b = geom.to_le_bytes().to_vec();
b.extend_from_slice(&[0; 8]);
b.extend_from_slice(&le32(&[master_id, 0]));
b.extend_from_slice(&flags.to_le_bytes());
b.extend_from_slice(&[0; 2]);
atom(rt::SLIDE_ATOM, 0, &b)
}
/// A master text style with one level: a paragraph and a character exception.
fn master_style(instance: u16, pf: &[u8], cf: &[u8]) -> Vec<u8> {
atom(rt::TEXT_MASTER_STYLE, instance, &[&1u16.to_le_bytes()[..], pf, cf].concat())
}
const MASTER_ID: u32 = 0x8000_0001;
const SLIDE_ID: u32 = 256;
const TITLE_MASTER_ID: u32 = 0x8000_0002;
fn master() -> Vec<u8> {
master_with(&[])
}
/// The test master with `shapes` after its decoration.
fn master_with(shapes: &[Vec<u8>]) -> Vec<u8> {
// Background, text, shadow, title, fill, accent, hyperlink, followed hyperlink.
let scheme: Vec<u8> = [0xFFFFFFu32, 0x000000, 0x808080, 0x000099, 0x00CC00, 0x0000FF, 0x00FF00, 0xFF0000]
.iter()
.flat_map(|c| [(c >> 16) as u8, (c >> 8) as u8, *c as u8, 0])
.collect();
let decoration = container(
rt::SP_CONTAINER,
0,
&[
fsp(1, 1026, 0xA00),
fopt(&[(prop::FILL_COLOR, 0x0800_0004), (prop::FILL_BOOLS, 0x0010_0010), (prop::LINE_BOOLS, 0x0008_0000)]),
anchor(0, 0, 5760, 400),
],
);
container(
rt::MAIN_MASTER,
0,
&[
slide_atom(1, 0, 0),
atom(rt::COLOR_SCHEME, 1, &scheme),
master_style(0, &pf_bytes(1, false), &cf_bytes(true, 0, 44, [0x11, 0x22, 0x33])),
master_style(1, &pf_bytes(0, true), &cf_bytes(false, 0, 28, [0, 0, 0])),
drawing(Some(solid_background(0x0000_00FF)), &[&[decoration][..], shapes].concat()),
],
)
}
fn slide() -> Vec<u8> {
let title = container(
rt::SP_CONTAINER,
0,
&[fsp(1, 2049, 0xA00), fopt(&[(prop::ANCHOR_TEXT, 1)]), anchor(400, 500, 5260, 1200), placeholder(0x0D), text_box(0, "Hello\rWorld", &[])],
);
let picture = container(rt::SP_CONTAINER, 0, &[fsp(75, 2050, 0xA00), fopt(&[(prop::PIB, 1)]), anchor(1600, 800, 2400, 2400)]);
let group = container(
rt::SPGR_CONTAINER,
0,
&[
container(rt::SP_CONTAINER, 0, &[atom(rt::FSPGR, 1, &le32(&[0, 0, 1000, 1000])), fsp(0, 2051, 0x201), anchor(1600, 3000, 4000, 3200)]),
container(
rt::SP_CONTAINER,
0,
&[
fsp(3, 2052, 0x2),
fopt(&[(prop::FILL_COLOR, 0x0000_FF00), (prop::LINE_WIDTH, 25400)]),
atom(rt::CHILD_ANCHOR, 0, &le32(&[500, 0, 1000, 500])),
],
),
],
);
// "Page *": the star is a slide number field. The first four characters are bold.
let mut style = vec![];
style.extend_from_slice(&7u32.to_le_bytes());
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&pf_bytes(2, false));
for (n, bold) in [(4u32, true), (3, false)] {
style.extend_from_slice(&n.to_le_bytes());
style.extend_from_slice(&cf_bytes(bold, 1, 12, [0xAB, 0xCD, 0xEF]));
}
let footer = container(
rt::SP_CONTAINER,
0,
&[
fsp(202, 2053, 0xA00),
anchor(4000, 500, 2000, 4200),
text_box(4, "Page *", &[atom(rt::STYLE_TEXT_PROP, 0, &style), atom(rt::SLIDE_NUMBER_MC, 0, &5u32.to_le_bytes())]),
],
);
// A hidden slide (SlideShowSlideInfoAtom fHidden).
let mut info = vec![0u8; 16];
info[10] = 0x4;
container(
rt::SLIDE,
0,
&[slide_atom(1, MASTER_ID, 0x7), atom(rt::SLIDE_SHOW_SLIDE_INFO, 0, &info), drawing(None, &[title, picture, group, footer])],
)
}
fn notes() -> Vec<u8> {
let number = container(rt::SP_CONTAINER, 0, &[fsp(1, 3073, 0xA00), anchor(0, 0, 100, 100), placeholder(0x08), text_box(2, "*", &[])]);
let body = container(
rt::SP_CONTAINER,
0,
&[fsp(1, 3074, 0xA00), anchor(100, 0, 4000, 4000), placeholder(0x0C), text_box(2, "Say hello\rThen wave", &[])],
);
container(rt::NOTES, 0, &[atom(rt::NOTES_ATOM, 0, &le32(&[SLIDE_ID, 0])), drawing(None, &[number, body])])
}
fn png() -> Vec<u8> {
let mut buf = std::io::Cursor::new(Vec::new());
image::RgbaImage::from_pixel(2, 2, image::Rgba([255, 0, 0, 255])).write_to(&mut buf, image::ImageFormat::Png).unwrap();
buf.into_inner()
}
/// The streams of a one-slide presentation, with `edit` choosing the current user's header token.
fn streams(token: u32) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_listing(token, 1)
}
/// The test document with its slide listed `times` times in the slide list.
fn streams_listing(token: u32, times: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_of(token, times, slide(), None)
}
/// The test document with `slide` as its slide and, when given, a title master (persist id 5,
/// slide id `TITLE_MASTER_ID`) listed after the main master.
fn streams_of(token: u32, times: usize, slide: Vec<u8>, title_master: Option<Vec<u8>>) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_with_outline(token, times, slide, title_master, &[])
}
/// As [`streams_of`], with `outline` (text blocks: header, characters, styles) following the
/// slide's persist atom in the slide list.
fn streams_with_outline(token: u32, times: usize, slide: Vec<u8>, title_master: Option<Vec<u8>>, outline: &[Vec<u8>]) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_with_document(token, times, master(), slide, title_master, outline, &[])
}
/// As [`streams_with_outline`], with `master` as the main master and `extra` records at the end of
/// the document container.
fn streams_with_document(
token: u32,
times: usize,
master: Vec<u8>,
slide: Vec<u8>,
title_master: Option<Vec<u8>>,
outline: &[Vec<u8>],
extra: &[Vec<u8>],
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_with_title_masters(token, times, master, slide, title_master.into_iter().collect(), outline, extra)
}
/// As [`streams_with_document`], with any number of title masters: persist ids 5 on, slide ids
/// `TITLE_MASTER_ID` on.
fn streams_with_title_masters(
token: u32,
times: usize,
master: Vec<u8>,
slide: Vec<u8>,
title_masters: Vec<Vec<u8>>,
outline: &[Vec<u8>],
extra: &[Vec<u8>],
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
streams_with_notes(token, times, master, slide, title_masters, outline, extra, notes(), &[])
}
/// As [`streams_with_title_masters`], with `notes` as the notes page (persist id 4) and
/// `notes_outline` following its persist atom in the notes list.
#[allow(clippy::too_many_arguments)]
fn streams_with_notes(
token: u32,
times: usize,
master: Vec<u8>,
slide: Vec<u8>,
title_masters: Vec<Vec<u8>>,
outline: &[Vec<u8>],
extra: &[Vec<u8>],
notes: Vec<u8>,
notes_outline: &[Vec<u8>],
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let pictures = atom(0xF01E, 0x6E0, &[&[0u8; 17][..], &png()].concat());
let mut fbse = vec![0u8; 36];
fbse[0] = 6;
fbse[1] = 6;
fbse[20..24].copy_from_slice(&(pictures.len() as u32).to_le_bytes());
let bstore = container(rt::BSTORE, 1, &[atom(rt::FBSE, 6, &fbse)]);
let mut font = utf16("Arial");
font.resize(68, 0);
let mut wing = utf16("Wingdings");
wing.resize(68, 0);
let mut doc_atom = le32(&[5760, 4320, 4320, 5760, 1, 2, 0, 0]);
doc_atom.extend_from_slice(&1u16.to_le_bytes());
doc_atom.resize(40, 0);
let persist = |id: u32| {
atom(
rt::SLIDE_PERSIST_ATOM,
0,
&le32(&[
id,
0,
0,
if id == 2 {
MASTER_ID
} else if id >= 5 {
TITLE_MASTER_ID + (id - 5)
} else if id == 3 {
SLIDE_ID
} else {
SLIDE_ID + 1
},
0,
]),
)
};
let parts = [
atom(rt::DOCUMENT_ATOM, 0, &doc_atom),
container(rt::ENVIRONMENT, 0, &[container(rt::FONT_COLLECTION, 0, &[atom(rt::FONT_ENTITY, 0, &font), atom(rt::FONT_ENTITY, 1, &wing)])]),
container(rt::DRAWING_GROUP, 0, &[container(rt::DGG_CONTAINER, 0, &[bstore])]),
container(
rt::SLIDE_LIST_WITH_TEXT,
1,
&(0..=title_masters.len() as u32).map(|k| persist(if k == 0 { 2 } else { 4 + k })).collect::<Vec<_>>(),
),
container(rt::SLIDE_LIST_WITH_TEXT, 0, &[vec![persist(3); times], outline.to_vec()].concat()),
container(rt::SLIDE_LIST_WITH_TEXT, 2, &[vec![persist(4)], notes_outline.to_vec()].concat()),
];
let document = container(rt::DOCUMENT, 0, &[&parts[..], extra].concat());
let mut stream = vec![];
let mut offsets = vec![];
let parts: Vec<Vec<u8>> = [document, master, slide, notes].into_iter().chain(title_masters).collect();
for part in &parts {
offsets.push(stream.len() as u32);
stream.extend_from_slice(part);
}
let dir_at = stream.len() as u32;
let mut dir = vec![1u32 | ((parts.len() as u32) << 20)];
dir.extend_from_slice(&offsets);
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&dir)));
let edit_at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, 0, dir_at, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let current_user = atom(rt::CURRENT_USER, 0, &le32(&[0x14, token, edit_at, 0, 0]));
(current_user, stream, pictures)
}
fn file_with(token: u32) -> Vec<u8> {
let (cu, doc, pics) = streams(token);
build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])
}
fn file() -> Vec<u8> {
file_with(0xE391_C05F)
}
#[test]
fn imports_a_presentation() {
let bytes = file();
assert!(crate::sniff(&bytes));
let p = import(&bytes).unwrap();
assert_eq!((p.slide_size.width, p.slide_size.height), (720.0, 540.0));
assert_eq!(p.slides.len(), 1);
let s = &p.slides[0];
assert!(s.hidden);
assert_eq!(s.notes.text(), "Say hello\nThen wave");
check_title_placeholder(s);
check_picture(&p, s);
check_group(s);
check_text_box(s);
check_master(&p, s);
}
fn check_title_placeholder(s: &Slide) {
// The title placeholder: its box, its text and the master's title style.
let title = &s.shapes[0];
assert_eq!(title.ph_type(), Some(PhType::Title));
let x = title.xfrm.unwrap();
assert_eq!((x.x, x.y, x.w, x.h), (62.5, 50.0, 595.0, 100.0));
assert_eq!(title.fill, Some(Fill::None));
let t = title.text.as_ref().unwrap();
assert_eq!(t.text(), "Hello\nWorld");
assert_eq!(t.body.anchor, Some(craft_pptx::text::Anchor::Middle));
let p0 = &t.paragraphs[0];
assert_eq!(p0.props.align, Some(Align::Center));
assert_eq!(p0.props.bullet, Some(Bullet::None));
let r = &p0.runs[0].props;
assert_eq!((r.size, r.bold, r.font.as_deref()), (Some(44.0), Some(true), Some("Arial")));
assert_eq!(r.fill, Some(Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0x11, 0x22, 0x33)))));
assert_eq!(s.title(), "Hello\nWorld");
}
fn check_picture(p: &Presentation, s: &Slide) {
// The picture.
let ShapeKind::Picture { fill } = &s.shapes[1].kind else { panic!("not a picture: {:?}", s.shapes[1].kind) };
let media = p.media.iter().find(|m| m.id == fill.media).unwrap();
assert_eq!(media.content_type, "image/png");
assert!(media.data.starts_with(b"\x89PNG"));
}
fn check_group(s: &Slide) {
// The group: children in slide points, the child space the group's own box.
let g = &s.shapes[2];
let ShapeKind::Group { children, child } = &g.kind else { panic!("not a group") };
let gx = g.xfrm.unwrap();
assert_eq!((gx.x, gx.y, gx.w, gx.h), (375.0, 200.0, 125.0, 200.0));
assert_eq!((child.x, child.y, child.w, child.h), (gx.x, gx.y, gx.w, gx.h));
let e = children[0].xfrm.unwrap();
assert_eq!((e.x, e.y, e.w, e.h, e.flip_h), (437.5, 200.0, 62.5, 100.0, false));
assert_eq!(children[0].geom.preset_name(), Some("ellipse"));
assert_eq!(children[0].fill, Some(Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0, 0xFF, 0)))));
assert_eq!(children[0].line.as_ref().and_then(|l| l.width), Some(2.0));
}
fn check_text_box(s: &Slide) {
// The text box: runs split at the format change, the star a slide number field.
let tb = s.shapes[3].text.as_ref().unwrap();
let runs = &tb.paragraphs[0].runs;
assert_eq!(tb.paragraphs[0].props.align, Some(Align::Right));
assert_eq!(runs.len(), 3, "{runs:?}");
assert_eq!((runs[0].text.as_str(), runs[0].props.bold), ("Page", Some(true)));
assert_eq!((runs[1].text.as_str(), runs[1].props.bold), (" ", Some(false)));
assert_eq!(runs[2].kind, RunKind::Field { field: "slidenum".into() });
assert_eq!(runs[0].props.font.as_deref(), Some("Wingdings"));
assert_eq!(s.shapes[3].fill, Some(Fill::None));
}
fn check_master(p: &Presentation, s: &Slide) {
// The master: its background, colours and decoration.
let m = &p.masters[0];
assert_eq!(m.background, Some(Background::Fill { fill: Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0xFF, 0, 0))) }));
assert_eq!(m.theme.colors.get(craft_pptx::SchemeSlot::Accent1), Rgba::rgb(0, 0xCC, 0));
assert!(m.shapes.iter().any(|sh| sh.ph.is_none() && sh.fill == Some(Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0, 0xCC, 0))))));
assert_eq!(m.title_style.level(0).and_then(|l| l.run.size), Some(44.0));
assert!(p.layout(s.layout).is_some());
}
/// A slide listed many times is one slide: a hostile file can't make the importer convert one
/// big drawing again for every listing (180k listings of a 100k-shape slide fit in ~10 MB).
#[test]
fn a_slide_listed_again_is_imported_once() {
let (cu, doc, pics) = streams_listing(0xE391_C05F, 3);
let bytes = build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]);
let p = import(&bytes).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(
p.slides[0].notes.text(),
"Say hello
Then wave"
);
}
/// Two slide records with the same slide id share its notes page, which goes to the first only:
/// converting it again for each would copy its text into every one.
#[test]
fn a_notes_page_goes_to_one_slide() {
let again = container(rt::SLIDE_LIST_WITH_TEXT, 0, &[atom(rt::SLIDE_PERSIST_ATOM, 0, &le32(&[6, 0, 0, SLIDE_ID, 0]))]);
let (_, mut stream, pics) = streams_with_document(0xE391_C05F, 1, master(), slide(), None, &[], &[again]);
let first_edit = stream.len() as u32 - 36;
// Persist id 6: a second copy of the slide.
let copy_at = stream.len() as u32;
stream.extend_from_slice(&slide());
let dir_at = stream.len() as u32;
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[6 | (1 << 20), copy_at])));
let edit_at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, first_edit, dir_at, 1, 7]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, edit_at, 0, 0]));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides.len(), 2);
assert_eq!(p.slides[0].notes.text(), "Say hello\nThen wave");
assert_eq!(p.slides[1].notes.text(), "");
}
/// As [`a_notes_page_goes_to_one_slide`], with the notes body's text in the notes list's outline
/// block: that block, too, is copied into the first slide's notes only.
#[test]
fn a_notes_pages_outline_text_goes_to_one_slide() {
let body = container(
rt::SP_CONTAINER,
0,
&[
fsp(1, 3074, 0xA00),
anchor(100, 0, 4000, 4000),
placeholder(0x0C),
container(rt::CLIENT_TEXTBOX, 0, &[atom(rt::OUTLINE_TEXT_REF, 0, &0u32.to_le_bytes())]),
],
);
let notes = container(rt::NOTES, 0, &[atom(rt::NOTES_ATOM, 0, &le32(&[SLIDE_ID, 0])), drawing(None, &[body])]);
let outline = [atom(rt::TEXT_HEADER, 0, &2u32.to_le_bytes()), atom(rt::TEXT_CHARS, 0, &utf16("From the outline"))];
let again = container(rt::SLIDE_LIST_WITH_TEXT, 0, &[atom(rt::SLIDE_PERSIST_ATOM, 0, &le32(&[6, 0, 0, SLIDE_ID, 0]))]);
let (_, mut stream, pics) = streams_with_notes(0xE391_C05F, 1, master(), slide(), vec![], &[], &[again], notes, &outline);
let first_edit = stream.len() as u32 - 36;
let copy_at = stream.len() as u32;
stream.extend_from_slice(&slide());
let dir_at = stream.len() as u32;
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[6 | (1 << 20), copy_at])));
let edit_at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, first_edit, dir_at, 1, 7]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, edit_at, 0, 0]));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides.len(), 2);
assert_eq!(p.slides[0].notes.text(), "From the outline");
assert_eq!(p.slides[1].notes.text(), "");
}
/// Text with very many formatting runs is laid into runs in about linear time. Each lookup used to
/// scan the run starts from the beginning, so the cost grew with runs²: in a debug build these 640k
/// one-character runs (about 6 MB of file) took 75 s that way, and take about 2 s with binary search.
#[test]
fn many_character_runs_are_not_quadratic() {
const N: usize = 640_000;
let text: String = (0..N).map(|i| if i % 2 == 0 { 'a' } else { '\u{0B}' }).collect();
let mut style = vec![];
style.extend_from_slice(&(N as u32 + 1).to_le_bytes());
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&0u32.to_le_bytes()); // a paragraph exception with no fields
for _ in 0..=N {
style.extend_from_slice(&1u32.to_le_bytes());
style.extend_from_slice(&0u32.to_le_bytes()); // a character exception with no fields
}
let shape = container(
rt::SP_CONTAINER,
0,
&[fsp(202, 2060, 0xA00), anchor(0, 0, 1000, 1000), text_box(1, &text, &[atom(rt::STYLE_TEXT_PROP, 0, &style)])],
);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[shape])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, None);
let bytes = build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]);
let t = std::time::Instant::now();
let p = import(&bytes).unwrap();
assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed());
let runs = &p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs;
assert_eq!(runs.iter().filter(|r| r.kind == RunKind::Break).count(), N / 2);
}
/// A slide naming a master that doesn't exist falls back to the first main master, every time. The
/// fallback used to be the lowest index in a HashMap where a title master ties with its main
/// master, so the slide took the "Title Master" layout on some opens and not on others.
#[test]
fn a_slide_with_an_unknown_master_falls_back_the_same_way_every_time() {
let title_master = container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x7), drawing(None, &[])]);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, 0x8000_0099, 0x7), drawing(None, &[])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, Some(title_master));
let bytes = build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]);
for _ in 0..30 {
let p = import(&bytes).unwrap();
assert_eq!(p.masters[0].layouts.iter().filter(|l| l.name == "Title Master").count(), 1, "the title master became a layout");
let (_, layout) = p.layout(p.slides[0].layout).unwrap();
assert_ne!(layout.name, "Title Master");
}
}
/// A damaged Current User record doesn't make a PowerPoint 97 file "too old": the newest edit is
/// found by scanning the document stream instead. A file with no edit record at all, behind a
/// token that isn't PowerPoint 97's, is still reported as too old.
#[test]
fn a_damaged_current_user_falls_back_to_the_scan() {
let (cu, doc, pics) = streams(0xE391_C05F);
let open = |cu: &[u8], doc: &[u8]| import(&build(&[("Current User", cu), ("PowerPoint Document", doc), ("Pictures", &pics)]));
// Truncated after the header and two bytes, so the token can't be read.
let truncated = atom(rt::CURRENT_USER, 0, &[0x14, 0]);
assert_eq!(open(&truncated, &doc).unwrap().slides.len(), 1);
// A corrupted token.
let corrupted = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xDEAD_BEEF, 0, 0, 0]));
assert_eq!(open(&corrupted, &doc).unwrap().slides.len(), 1);
assert!(matches!(open(&corrupted, &atom(rt::DOCUMENT_ATOM, 0, &[0; 40])), Err(crate::PptError::TooOld)));
// An intact token that points at no edit still gets the scan, and a stream without one is damaged.
assert!(matches!(open(&cu, &atom(rt::DOCUMENT_ATOM, 0, &[0; 40])), Err(crate::PptError::Damaged(_))));
}
/// Persist directories nested inside one another are not each read to the end of the stream. A chain
/// of edits, each naming a directory header 8 bytes after the last one, made every edit reparse the
/// rest of the stream (4095 edits × the filler): about 48 s for this 64 MiB stream in a debug build.
/// Within the shared budget it takes milliseconds, and the file is reported as damaged.
#[test]
fn nested_persist_directories_are_read_within_the_stream() {
const FILLER: usize = 1 << 26;
const EDITS: u32 = 4095;
let (_, mut stream, _) = streams(0xE391_C05F);
let mut prev = stream.len() as u32 - 36; // the test document's own UserEditAtom (8 + 28 bytes)
let base = stream.len() as u32;
for _ in 0..EDITS {
// A PersistDirectoryAtom header whose length runs past the end (clipped to the stream).
stream.extend_from_slice(&0u16.to_le_bytes());
stream.extend_from_slice(&rt::PERSIST_DIRECTORY.to_le_bytes());
stream.extend_from_slice(&0xFFFF_FFF0u32.to_le_bytes());
}
stream.resize(stream.len() + FILLER, 0);
for k in 0..EDITS {
let at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, prev, base + 8 * k, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
prev = at;
}
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, prev, 0, 0]));
// The streams are read directly: the test file builder can't hold a stream this size.
let t = std::time::Instant::now();
let r = crate::doc::read(&cu, &stream);
assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed());
assert!(matches!(r, Err(crate::PptError::Damaged(_))));
}
#[test]
fn an_encrypted_presentation_says_so() {
assert!(matches!(import(&file_with(0xF3D1_C4DF)), Err(crate::PptError::Encrypted)));
}
#[test]
fn other_files_are_not_ppt() {
assert!(!crate::sniff(b"PK\x03\x04"));
assert!(!crate::sniff(&build(&[("WordDocument", b"x")])));
assert!(matches!(import(b"not a presentation"), Err(crate::PptError::NotPpt)));
assert!(matches!(import(&build(&[("Workbook", b"x")])), Err(crate::PptError::NotPpt)));
}
/// Damaged input never panics, loops or allocates without bound: every 4-byte word of the
/// document stream set to hostile values, and truncations of it.
#[test]
fn hostile_documents_never_panic() {
let (cu, doc, pics) = streams(0xE391_C05F);
for at in (0..doc.len().saturating_sub(4)).step_by(3) {
for v in [0u32, 1, 0x7FFF_FFFF, 0xFFFF_FFFF, 0x0000_FFFF, 0xF00F_000F] {
let mut d = doc.clone();
d[at..at + 4].copy_from_slice(&v.to_le_bytes());
let f = build(&[("Current User", &cu), ("PowerPoint Document", &d), ("Pictures", &pics)]);
let _ = import(&f);
}
}
for cut in (0..doc.len()).step_by(7) {
let f = build(&[("Current User", &cu), ("PowerPoint Document", &doc[..cut]), ("Pictures", &pics[..cut.min(pics.len())])]);
let _ = import(&f);
}
}
/// A slide shape that inherits from a master shape (`hspMaster`) takes it from its own master.
/// Master shapes used to be remembered by shape id alone, so a title master reusing the main
/// master's shape id (here 1026, red instead of the scheme's green) replaced it for every slide.
#[test]
fn a_master_shape_id_is_looked_up_in_the_slides_own_master() {
let red = container(
rt::SP_CONTAINER,
0,
&[fsp(1, 1026, 0xA00), fopt(&[(prop::FILL_COLOR, 0x0000_00FF), (prop::FILL_BOOLS, 0x0010_0010)]), anchor(0, 0, 100, 100)],
);
let title_master = container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x7), drawing(None, &[red])]);
let inheriting = container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), fopt(&[(prop::HSP_MASTER, 1026)]), anchor(0, 0, 100, 100)]);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[inheriting])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, Some(title_master));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides[0].shapes[0].fill, Some(Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0, 0xCC, 0)))));
}
/// A run's shadow bit (TextCFException fontStyle bit 4) gives the run a shadow in the colour
/// scheme's shadow colour (grey in the test scheme). It used to be read and then dropped.
#[test]
fn a_shadowed_run_keeps_its_shadow() {
let mut style = vec![];
style.extend_from_slice(&7u32.to_le_bytes());
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&pf_bytes(0, false));
for (n, shadow) in [(3u32, true), (4, false)] {
style.extend_from_slice(&n.to_le_bytes());
style.extend_from_slice(&0x10u32.to_le_bytes()); // mask: shadow
style.extend_from_slice(&(if shadow { 0x10u16 } else { 0 }).to_le_bytes());
}
let tb = container(
rt::SP_CONTAINER,
0,
&[fsp(202, 2049, 0xA00), anchor(0, 0, 1000, 1000), text_box(4, "Hi you", &[atom(rt::STYLE_TEXT_PROP, 0, &style)])],
);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[tb])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, None);
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
let runs = &p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs;
assert_eq!(runs[0].text, "Hi ");
let shadow = runs[0].props.shadow.as_ref().expect("the first run is shadowed");
assert_eq!(shadow.color, craft_pptx::ColorRef::rgb(Rgba::rgb(0x80, 0x80, 0x80)));
assert!(runs[1].props.shadow.is_none());
}
/// `tests/fixtures/old-talk.pps`: a one-slide `.pps` built here from records, for applications'
/// tests that need real 97–2003 bytes (DeckCraft's show-on-open test). Run this test with
/// `CRAFT_WRITE_PPT_FIXTURE=1` to rebuild the file after changing the builders.
#[test]
fn the_pps_fixture_is_a_built_presentation() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/old-talk.pps");
if std::env::var_os("CRAFT_WRITE_PPT_FIXTURE").is_some() {
let title =
container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), anchor(400, 500, 5260, 1200), placeholder(0x0D), text_box(0, "Old talk", &[])]);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[title])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, None);
std::fs::write(path, build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
}
let p = import(&std::fs::read(path).unwrap()).unwrap();
assert_eq!(p.slides.len(), 1);
assert!(!p.slides[0].hidden);
let title = p.slides[0].shapes[0].text.as_ref().unwrap();
assert_eq!(title.paragraphs[0].runs[0].text, "Old talk");
}
/// A placeholder whose client text box holds only an `OutlineTextRefAtom` takes its text and
/// formatting from that block of the slide list ([MS-PPT] OfficeArtClientTextbox allows the
/// reference alone). A text box that also has a header but no characters falls back the same way.
#[test]
fn placeholder_text_can_come_from_the_slide_list() {
let mut style = vec![];
style.extend_from_slice(&7u32.to_le_bytes()); // the text and its closing paragraph mark
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&pf_bytes(1, false));
for (n, bold) in [(2u32, true), (5, false)] {
style.extend_from_slice(&n.to_le_bytes());
style.extend_from_slice(&cf_bytes(bold, 0, 40, [0x12, 0x34, 0x56]));
}
let outline = [
atom(rt::TEXT_HEADER, 0, &0u32.to_le_bytes()),
atom(rt::TEXT_CHARS, 0, &utf16("Hi all")),
atom(rt::STYLE_TEXT_PROP, 0, &style),
atom(rt::TEXT_HEADER, 0, &1u32.to_le_bytes()),
atom(rt::TEXT_CHARS, 0, &utf16("Second block")),
];
let reference = |n: u32| atom(rt::OUTLINE_TEXT_REF, 0, &n.to_le_bytes());
let only_ref = container(
rt::SP_CONTAINER,
0,
&[fsp(1, 2049, 0xA00), anchor(400, 500, 5260, 1200), placeholder(0x0D), container(rt::CLIENT_TEXTBOX, 0, &[reference(0)])],
);
let header_and_ref = container(
rt::SP_CONTAINER,
0,
&[
fsp(1, 2050, 0xA00),
anchor(1300, 500, 5260, 3000),
placeholder(0x0E),
container(rt::CLIENT_TEXTBOX, 0, &[atom(rt::TEXT_HEADER, 0, &1u32.to_le_bytes()), reference(1)]),
],
);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[only_ref, header_and_ref])]);
let (cu, doc, pics) = streams_with_outline(0xE391_C05F, 1, slide, None, &outline);
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
let shapes = &p.slides[0].shapes;
let title = shapes[0].text.as_ref().expect("the title has the outline text");
let runs = &title.paragraphs[0].runs;
assert_eq!((runs[0].text.as_str(), runs[0].props.bold), ("Hi", Some(true)));
assert_eq!((runs[1].text.as_str(), runs[1].props.bold), (" all", Some(false)));
assert_eq!(runs[0].props.size, Some(40.0));
assert_eq!(title.paragraphs[0].props.align, Some(Align::Center));
let body = shapes[1].text.as_ref().expect("the body has the second block");
assert_eq!(body.paragraphs[0].runs[0].text, "Second block");
}
/// The newest edit wins: a later edit's persist directory that gives the slide's persist id a new
/// slide replaces the old one.
#[test]
fn a_later_edit_replaces_a_slide() {
let (_, mut stream, pics) = streams(0xE391_C05F);
let first_edit = stream.len() as u32 - 36; // the test document's UserEditAtom (8 + 28 bytes)
let title = container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), anchor(400, 500, 5260, 1200), placeholder(0x0D), text_box(0, "Edited", &[])]);
let slide_at = stream.len() as u32;
stream.extend_from_slice(&container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[title])]));
// Persist id 3 (the slide) only.
let dir_at = stream.len() as u32;
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[3 | (1 << 20), slide_at])));
let edit_at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, first_edit, dir_at, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, edit_at, 0, 0]));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs[0].text, "Edited");
// The master, from the first edit's directory, is still there.
assert_eq!(p.masters.len(), 1);
}
/// An edit whose "previous edit" points back at itself cuts the older edits off the chain, and
/// with them the document, master and notes (storytold/deckcraft#1137 review: such a file opened as an empty deck).
/// Every edit in the stream is read instead, the newest still winning.
#[test]
fn a_broken_edit_chain_is_recovered_from_every_edit() {
let (_, mut stream, pics) = streams(0xE391_C05F);
let title = container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), anchor(400, 500, 5260, 1200), placeholder(0x0D), text_box(0, "Edited", &[])]);
let slide_at = stream.len() as u32;
stream.extend_from_slice(&container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[title])]));
let dir_at = stream.len() as u32;
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[3 | (1 << 20), slide_at])));
let edit_at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, edit_at, dir_at, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, edit_at, 0, 0]));
let (p, lost) = import_checked(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs[0].text, "Edited");
assert_eq!(p.masters.len(), 1);
assert!(lost.is_empty(), "{lost:?}");
}
/// A slide the list names but whose object is gone is reported as lost, so an application can
/// say so instead of opening a deck that silently misses it.
#[test]
fn a_slide_without_its_object_is_reported_lost() {
let (cu, mut stream, pics) = streams(0xE391_C05F);
// The persist directory (ids 1–4: document, master, slide, notes) ends just before the
// 36-byte edit; point the slide's entry past the end of the stream.
let slide_entry = stream.len() - 36 - 8;
stream[slide_entry..slide_entry + 4].copy_from_slice(&0xFFFF_FFF0u32.to_le_bytes());
let bytes = build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)]);
let (p, lost) = import_checked(&bytes).unwrap();
assert!(p.slides.is_empty());
assert_eq!(lost, vec![format!("slide {SLIDE_ID}")]);
}
/// Two more signs of an encrypted file besides the Current User token: an edit record long enough
/// to hold `encryptSessionPersistIdRef`, and a `CryptSession` object in the persist directory.
#[test]
fn encryption_is_found_in_the_edit_and_the_persist_directory() {
let open = |doc: &[u8], pics: &[u8]| {
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, doc.len() as u32 - 36, 0, 0]));
import(&build(&[("Current User", &cu), ("PowerPoint Document", doc), ("Pictures", pics)]))
};
let (_, stream, pics) = streams(0xE391_C05F);
assert!(open(&stream, &pics).is_ok());
// The edit record with a fifth reference: 28 bytes become 32. The Current User points at the
// new last record, 40 bytes from the end.
let mut long_edit = stream.clone();
let edit_at = long_edit.len() - 36;
long_edit.truncate(edit_at);
let mut edit = le32(&[0, 0x0300_03F4, 0, 0, 1, 5]);
edit[12..16].copy_from_slice(&stream[edit_at + 8 + 12..edit_at + 8 + 16]); // offsetPersistDirectory
edit.extend_from_slice(&[0; 4]);
edit.extend_from_slice(&6u32.to_le_bytes()); // encryptSessionPersistIdRef
long_edit.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, edit_at as u32, 0, 0]));
let r = import(&build(&[("Current User", &cu), ("PowerPoint Document", &long_edit), ("Pictures", &pics)]));
assert!(matches!(r, Err(crate::PptError::Encrypted)), "{:?}", r.err());
// A CryptSession container given a persist id by a later edit.
let mut crypt = stream.clone();
let first_edit = crypt.len() as u32 - 36;
let session_at = crypt.len() as u32;
crypt.extend_from_slice(&container(rt::CRYPT_SESSION, 0, &[atom(0x0FFF, 0, &[0; 8])]));
let dir_at = crypt.len() as u32;
crypt.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[9 | (1 << 20), session_at])));
let mut edit = le32(&[0, 0x0300_03F4, first_edit, dir_at, 1, 9]);
edit.extend_from_slice(&[0; 4]);
crypt.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
assert!(matches!(open(&crypt, &pics), Err(crate::PptError::Encrypted)));
}
/// A colour scheme record whose fill colour (index 4) is `fill`, the rest as the test master's.
fn scheme_with_fill(fill: [u8; 3]) -> Vec<u8> {
let mut colors: Vec<u8> = [0xFFFFFFu32, 0x000000, 0x808080, 0x000099, 0, 0x0000FF, 0x00FF00, 0xFF0000]
.iter()
.flat_map(|c| [(c >> 16) as u8, (c >> 8) as u8, *c as u8, 0])
.collect();
colors[16..19].copy_from_slice(&fill);
atom(rt::COLOR_SCHEME, 1, &colors)
}
/// A shape filled with the scheme's fill colour.
fn scheme_filled(spid: u32) -> Vec<u8> {
container(
rt::SP_CONTAINER,
0,
&[fsp(1, spid, 0xA00), fopt(&[(prop::FILL_COLOR, 0x0800_0004), (prop::FILL_BOOLS, 0x0010_0010)]), anchor(0, 0, 100, 100)],
)
}
/// A title master becomes a layout of its main master, with its own background and shapes, and
/// without the master's shapes when it doesn't follow them. A slide of the title master uses that
/// layout. A slide that follows nothing keeps its own background and scheme and hides the
/// master's shapes.
#[test]
fn title_masters_and_slides_that_dont_follow_the_master() {
let title_master =
container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x0), drawing(Some(solid_background(0x0000_FF00)), &[scheme_filled(3000)])]);
let on_title = container(rt::SLIDE, 0, &[slide_atom(0, TITLE_MASTER_ID, 0x7), drawing(None, &[])]);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, on_title, Some(title_master));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
let (_, layout) = p.layout(p.slides[0].layout).unwrap();
assert_eq!(layout.name, "Title Master");
assert_eq!(layout.background, Some(Background::Fill { fill: Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0, 0xFF, 0))) }));
assert!(!layout.show_master_shapes);
assert!(layout.shapes.iter().any(|sh| sh.ph.is_none()), "the title master's own shape");
assert_eq!(p.slides[0].background, None, "the slide follows its master's background");
let alone = container(
rt::SLIDE,
0,
&[slide_atom(1, MASTER_ID, 0x0), scheme_with_fill([0x11, 0x22, 0xCC]), drawing(Some(solid_background(0x0000_00FF)), &[scheme_filled(2049)])],
);
let (cu, doc, pics) = streams_of(0xE391_C05F, 1, alone, None);
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap();
let s = &p.slides[0];
assert_eq!(s.background, Some(Background::Fill { fill: Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0xFF, 0, 0))) }));
assert!(!s.show_master_shapes);
assert_eq!(s.shapes[0].fill, Some(Fill::solid(craft_pptx::ColorRef::rgb(Rgba::rgb(0x11, 0x22, 0xCC)))), "the slide's own scheme");
}
fn open_streams((cu, doc, pics): (Vec<u8>, Vec<u8>, Vec<u8>)) -> Presentation {
import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)])).unwrap()
}
/// A picture frame that says nothing about its fill or line draws neither (storytold/deckcraft#1137 review): the
/// drawing defaults' fill would cover a picture's transparent parts, or an OLE chart's preview.
#[test]
fn a_picture_frame_has_no_fill_or_line_of_its_own() {
let p = import(&file()).unwrap();
let picture = &p.slides[0].shapes[1];
assert!(matches!(picture.kind, ShapeKind::Picture { .. }));
assert_eq!(picture.fill, Some(Fill::None));
assert_eq!(picture.line.as_ref().map(|l| l.fill.clone()), Some(Some(Fill::None)));
}
/// A picture fill whose blip is held in the shape's own `fillBlip` property (complex, with fBid),
/// as table cells with a picture have it (storytold/deckcraft#1137 review), shows that picture.
#[test]
fn a_picture_held_in_the_fill_property_is_read() {
let blip = atom(0xF01E, 0x6E0, &[&[0u8; 17][..], &png()].concat());
let mut entries = vec![];
for (id, op) in [(prop::FILL_TYPE, 3u32), (0xC000 | prop::FILL_BLIP, blip.len() as u32), (prop::FILL_BOOLS, 0x0010_0010)] {
entries.extend_from_slice(&id.to_le_bytes());
entries.extend_from_slice(&op.to_le_bytes());
}
entries.extend_from_slice(&blip);
let cell = container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), atom(rt::FOPT, 3, &entries), anchor(400, 500, 2000, 1200)]);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[cell])]);
let p = open_streams(streams_of(0xE391_C05F, 1, slide, None));
let Some(Fill::Picture(fill)) = &p.slides[0].shapes[0].fill else { panic!("{:?}", p.slides[0].shapes[0].fill) };
let media = p.media.iter().find(|m| m.id == fill.media).unwrap();
assert!(media.data.starts_with(b"\x89PNG"));
}
/// The headers and footers the document sets for its slides (storytold/deckcraft#1137 review: the date, footer and
/// slide number were missing) become the slide's footer placeholders, placed and styled as the
/// master's are on every layout.
#[test]
fn headers_and_footers_become_footer_placeholders() {
let cstring = |instance: u16, s: &str| atom(rt::CSTRING, instance, &utf16(s));
// fHasDate | fHasUserDate | fHasSlideNumber | fHasFooter. The footer string ends in a NUL, as
// some writers add one: the text stops there.
let flags: u16 = 0x01 | 0x04 | 0x08 | 0x20;
let hf = container(
rt::HEADERS_FOOTERS,
3,
&[
atom(rt::HEADERS_FOOTERS_ATOM, 0, &[&0u16.to_le_bytes()[..], &flags.to_le_bytes()].concat()),
cstring(0, "26 May 2004"),
cstring(2, "Workshop\0"),
],
);
// The master's slide number placeholder: bottom right, right-aligned 10 pt text.
let mut style = vec![];
style.extend_from_slice(&2u32.to_le_bytes());
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&pf_bytes(2, false));
style.extend_from_slice(&2u32.to_le_bytes());
style.extend_from_slice(&cf_bytes(false, 0, 10, [1, 2, 3]));
let number = container(
rt::SP_CONTAINER,
0,
&[fsp(1, 1030, 0xA00), anchor(4000, 5000, 5700, 4300), placeholder(0x08), text_box(4, "*", &[atom(rt::STYLE_TEXT_PROP, 0, &style)])],
);
let p = open_streams(streams_with_document(0xE391_C05F, 1, master_with(&[number]), slide(), None, &[], &[hf]));
let shapes = &p.slides[0].shapes;
let of = |k: PhType| shapes.iter().find(|s| s.ph_type() == Some(k)).unwrap_or_else(|| panic!("no {k:?}"));
assert_eq!(of(PhType::Date).text.as_ref().unwrap().text(), "26 May 2004");
assert_eq!(of(PhType::Footer).text.as_ref().unwrap().text(), "Workshop");
let runs = &of(PhType::SlideNum).text.as_ref().unwrap().paragraphs[0].runs;
assert!(matches!(&runs[0].kind, RunKind::Field { field } if field == "slidenum"));
assert!(p.header_footer.slide_number && p.header_footer.footer && p.header_footer.date);
// Every layout's slide number sits where the master's does, with its look.
let master = &p.masters[0];
for l in &master.layouts {
let n = l.shapes.iter().find(|s| s.ph_type() == Some(PhType::SlideNum)).unwrap_or_else(|| panic!("no slide number on {}", l.name));
let x = n.xfrm.unwrap();
assert_eq!((x.x, x.y), (5000.0 / 8.0, 4000.0 / 8.0), "{}", l.name);
let level = n.text.as_ref().unwrap().list_style.level(0).unwrap();
assert_eq!((level.para.align, level.run.size), (Some(Align::Right), Some(10.0)));
}
}
/// Numbered paragraphs (storytold/deckcraft#1137 review: drawn with the bullet character) take their numbering from
/// the document's `___PPT9` tag: the slide's outline block of that text type, and the entry the
/// run's `pp9rt` names. The tag's name ends in a NUL here, as some writers add one.
#[test]
fn outline_numbering_from_the_ppt9_tag() {
// Entry 0: nothing. Entry 1: numbered, "1." (scheme 3), from 4.
let mut prop9 = vec![0u8; 12];
prop9.extend_from_slice(&0x0300_0000u32.to_le_bytes());
prop9.extend_from_slice(&1u16.to_le_bytes());
prop9.extend_from_slice(&3u16.to_le_bytes());
prop9.extend_from_slice(&4i16.to_le_bytes());
prop9.extend_from_slice(&[0; 8]);
let tag_data = [atom(rt::OUTLINE_TEXT_PROPS_HEADER, 1, &le32(&[SLIDE_ID, 1])), atom(rt::STYLE_TEXT_PROP9, 0, &prop9)];
let tag = container(
rt::PROG_BINARY_TAG,
0,
&[atom(rt::CSTRING, 0, &utf16("___PPT9\0")), atom(rt::BINARY_TAG_DATA, 0, &container(rt::OUTLINE_TEXT_PROPS9, 0, &tag_data))],
);
let list = container(rt::LIST, 0, &[container(rt::PROG_TAGS, 0, &[tag])]);
// Two bulleted paragraphs: the second's run names entry 1 (pp9rt in fontStyle bits 10–13).
let pp9rt = |i: u16| [&0x0000_0400u32.to_le_bytes()[..], &(i << 10).to_le_bytes()].concat();
let mut style = vec![];
style.extend_from_slice(&8u32.to_le_bytes());
style.extend_from_slice(&0u16.to_le_bytes());
style.extend_from_slice(&pf_bytes(0, true));
for (n, i) in [(4u32, 0u16), (4, 1)] {
style.extend_from_slice(&n.to_le_bytes());
style.extend_from_slice(&pp9rt(i));
}
let outline = [atom(rt::TEXT_HEADER, 0, &1u32.to_le_bytes()), atom(rt::TEXT_CHARS, 0, &utf16("One\rTwo")), atom(rt::STYLE_TEXT_PROP, 0, &style)];
let body = container(
rt::SP_CONTAINER,
0,
&[
fsp(1, 2049, 0xA00),
anchor(1300, 500, 5260, 3000),
placeholder(0x0E),
container(rt::CLIENT_TEXTBOX, 0, &[atom(rt::OUTLINE_TEXT_REF, 0, &0u32.to_le_bytes())]),
],
);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[body])]);
let p = open_streams(streams_with_document(0xE391_C05F, 1, master(), slide, None, &outline, &[list]));
let paras = &p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs;
assert!(matches!(paras[0].props.bullet, Some(Bullet::Char { .. })), "{:?}", paras[0].props.bullet);
assert_eq!(paras[1].props.bullet, Some(Bullet::AutoNum { scheme: "arabicPeriod".into(), start_at: 4 }));
}
/// An outline block is one shape's text. A second shape pointing at the same block keeps its own
/// (here none), so a hostile file can't copy one big block into every shape of the slide.
#[test]
fn an_outline_block_is_used_by_one_shape() {
let outline = [atom(rt::TEXT_HEADER, 0, &0u32.to_le_bytes()), atom(rt::TEXT_CHARS, 0, &utf16("Shared"))];
let referring = |spid: u32| {
container(
rt::SP_CONTAINER,
0,
&[
fsp(1, spid, 0xA00),
anchor(400, 500, 5260, 1200),
placeholder(0x0D),
container(rt::CLIENT_TEXTBOX, 0, &[atom(rt::OUTLINE_TEXT_REF, 0, &0u32.to_le_bytes())]),
],
)
};
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[referring(2049), referring(2050)])]);
let p = open_streams(streams_with_outline(0xE391_C05F, 1, slide, None, &outline));
let texts: Vec<String> = p.slides[0].shapes.iter().map(|s| s.text.as_ref().unwrap().text()).collect();
assert_eq!(texts, ["Shared", ""]);
}
fn open_plain((cu, doc, pics): (Vec<u8>, Vec<u8>, Vec<u8>)) -> Presentation {
crate::import(&build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]), &craft_pptx::Plain).unwrap()
}
/// A footer-only `HeadersFootersContainer` of `instance` with `footer` as its text.
fn footer_record(instance: u16, footer: &str) -> Vec<u8> {
container(
rt::HEADERS_FOOTERS,
instance,
&[atom(rt::HEADERS_FOOTERS_ATOM, 0, &[&0u16.to_le_bytes()[..], &0x20u16.to_le_bytes()].concat()), atom(rt::CSTRING, 2, &utf16(footer))],
)
}
/// A slide's own headers and footers (`PerSlideHeadersFootersContainer`, instance 0) win over the
/// document's for every slide (instance 3). Only the document's used to be looked for.
#[test]
fn a_slides_own_footer_wins_over_the_documents() {
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[]), footer_record(0, "This slide")]);
let p = open_streams(streams_with_document(0xE391_C05F, 1, master(), slide, None, &[], &[footer_record(3, "Every slide")]));
let footer = p.slides[0].shapes.iter().find(|s| s.ph_type() == Some(PhType::Footer)).expect("a footer");
assert_eq!(footer.text.as_ref().unwrap().text(), "This slide");
assert_eq!(p.header_footer.footer_text, "Every slide");
}
/// A text box that points at an outline block keeps its own `TextRulerAtom`: the margin it sets
/// (one inch at level 1) wins over the block's. It used to be dropped with the reference.
#[test]
fn a_text_boxs_own_ruler_applies_to_its_outline_text() {
let outline = [atom(rt::TEXT_HEADER, 0, &1u32.to_le_bytes()), atom(rt::TEXT_CHARS, 0, &utf16("Indented"))];
let ruler = atom(rt::TEXT_RULER, 0, &[&(1u32 << 3).to_le_bytes()[..], &576u16.to_le_bytes()].concat());
let body = container(
rt::SP_CONTAINER,
0,
&[
fsp(1, 2049, 0xA00),
anchor(1300, 500, 5260, 3000),
placeholder(0x0E),
container(rt::CLIENT_TEXTBOX, 0, &[atom(rt::OUTLINE_TEXT_REF, 0, &0u32.to_le_bytes()), ruler]),
],
);
let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[body])]);
let p = open_streams(streams_with_outline(0xE391_C05F, 1, slide, None, &outline));
let t = p.slides[0].shapes[0].text.as_ref().unwrap();
assert_eq!(t.text(), "Indented");
assert_eq!(t.paragraphs[0].props.margin_left, Some(72.0));
}
/// A slide list entry whose object isn't a slide (here the notes page) is a lost slide, not a
/// slide silently left out.
#[test]
fn a_slide_entry_naming_another_object_is_reported_lost() {
let (cu, mut stream, pics) = streams(0xE391_C05F);
// The persist directory's last two entries (the slide's, then the notes page's) end just
// before the 36-byte edit: point the slide's at the notes page.
let end = stream.len() - 36;
let notes_at: [u8; 4] = stream[end - 4..end].try_into().unwrap();
stream[end - 8..end - 4].copy_from_slice(&notes_at);
let (p, lost) = import_checked(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert!(p.slides.is_empty());
assert_eq!(lost, vec![format!("slide {SLIDE_ID}")]);
}
/// A title master with a centred title and a subtitle placeholder, `extra` shapes after them.
fn title_master_with(extra: &[Vec<u8>]) -> Vec<u8> {
let ph =
|spid: u32, kind: u8, a: [i16; 4]| container(rt::SP_CONTAINER, 0, &[fsp(1, spid, 0xA00), anchor(a[0], a[1], a[2], a[3]), placeholder(kind)]);
let shapes = [vec![ph(3001, 0x0F, [1000, 600, 5160, 2000]), ph(3002, 0x10, [2200, 1200, 4540, 3200])], extra.to_vec()].concat();
container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x7), drawing(None, &shapes)])
}
/// The first "Title Master" layout of `p`'s first master.
fn title_layout(p: &Presentation) -> &Layout {
p.masters[0].layouts.iter().find(|l| l.name == "Title Master").expect("the title master's layout")
}
/// A title master's placeholders place its layout's, as a main master's place the master's: the
/// title takes its box, and the subtitle the default title layout lacks is added. With the plain
/// defaults (no placeholders at all) both are added.
#[test]
fn a_title_masters_placeholders_place_its_layouts() {
let boxes = |l: &Layout| {
let at = |f: &dyn Fn(PhType) -> bool| {
let x = l.shapes.iter().find(|s| s.ph_type().is_some_and(f)).and_then(|s| s.xfrm).unwrap_or_else(|| panic!("{:?}", l.shapes));
(x.x, x.y, x.w, x.h)
};
(at(&|t| matches!(t, PhType::Title | PhType::CtrTitle)), at(&|t| t == PhType::SubTitle))
};
let expected = ((75.0, 125.0, 570.0, 125.0), (150.0, 275.0, 417.5, 125.0));
let p = open_streams(streams_of(0xE391_C05F, 1, slide(), Some(title_master_with(&[]))));
assert_eq!(boxes(title_layout(&p)), expected);
let p = open_plain(streams_of(0xE391_C05F, 1, slide(), Some(title_master_with(&[]))));
assert_eq!(boxes(title_layout(&p)), expected);
}
/// A second title master's layout takes nothing from the first title master's layout: not its own
/// shapes, and not its placeholders or their boxes. With the plain defaults (no title layout of
/// theirs) it holds only its own placeholder, where it puts it.
#[test]
fn a_second_title_master_doesnt_copy_the_firsts_shapes() {
let title = container(rt::SP_CONTAINER, 0, &[fsp(1, 4001, 0xA00), anchor(200, 400, 2000, 800), placeholder(0x0F)]);
let second = container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x7), drawing(None, &[title])]);
let p =
open_plain(streams_with_title_masters(0xE391_C05F, 1, master(), slide(), vec![title_master_with(&[scheme_filled(3003)]), second], &[], &[]));
let layouts: Vec<&Layout> = p.masters[0].layouts.iter().filter(|l| l.name == "Title Master").collect();
assert_eq!(layouts.len(), 2);
assert_eq!(layouts[0].shapes.iter().filter(|s| s.ph.is_none()).count(), 1);
assert_eq!(layouts[1].shapes.len(), 1, "only its own placeholder: {:?}", layouts[1].shapes);
let x = layouts[1].shapes[0].xfrm.unwrap();
assert_eq!((layouts[1].shapes[0].ph_type(), x.x, x.y, x.w, x.h), (Some(PhType::CtrTitle), 50.0, 25.0, 200.0, 75.0));
}
/// A title slide of the main master takes an ordinary layout, not the one made for a title
/// master: that one is for the slides that name the title master.
#[test]
fn a_title_masters_layout_is_only_for_its_own_slides() {
let on_main = container(rt::SLIDE, 0, &[slide_atom(0, MASTER_ID, 0x7), drawing(None, &[])]);
let p = open_plain(streams_of(0xE391_C05F, 1, on_main, Some(title_master_with(&[]))));
let (_, layout) = p.layout(p.slides[0].layout).unwrap();
assert_eq!(layout.name, "Blank");
}
/// When the edit chain is broken and the stream holds more edits than are read, the newest are the
/// ones read: here the newest edit replaces the slide, behind 4095 edits with no directory.
#[test]
fn a_broken_chain_reads_the_newest_edits() {
let (_, mut stream, pics) = streams(0xE391_C05F);
let first_edit = stream.len() - 36;
let offsets: Vec<u32> =
(0..4).map(|i| u32::from_le_bytes(stream[first_edit - 16 + 4 * i..first_edit - 12 + 4 * i].try_into().unwrap())).collect();
// Edits that point at themselves and name the document as their directory.
let junk_at = stream.len() as u32;
for _ in 0..4095 {
let at = stream.len() as u32;
let mut edit = le32(&[0, 0x0300_03F4, at, 0, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
}
let title = container(rt::SP_CONTAINER, 0, &[fsp(1, 2049, 0xA00), anchor(400, 500, 5260, 1200), placeholder(0x0D), text_box(0, "Newest", &[])]);
let slide_at = stream.len() as u32;
stream.extend_from_slice(&container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[title])]));
let dir_at = stream.len() as u32;
stream.extend_from_slice(&atom(rt::PERSIST_DIRECTORY, 0, &le32(&[1 | (4 << 20), offsets[0], offsets[1], slide_at, offsets[3]])));
let mut edit = le32(&[0, 0x0300_03F4, first_edit as u32, dir_at, 1, 5]);
edit.extend_from_slice(&[0; 4]);
stream.extend_from_slice(&atom(rt::USER_EDIT, 0, &edit));
let cu = atom(rt::CURRENT_USER, 0, &le32(&[0x14, 0xE391_C05F, junk_at, 0, 0]));
let p = import(&build(&[("Current User", &cu), ("PowerPoint Document", &stream), ("Pictures", &pics)])).unwrap();
assert_eq!(p.slides[0].shapes[0].text.as_ref().unwrap().text(), "Newest");
}