mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 22:37:12 +00:00
Merge branch 'ppt95-text' into ppt95-shapes
# Conflicts: # crates/ppt-binary/src/lib.rs # crates/ppt-binary/src/ppt95/tests.rs
This commit is contained in:
@@ -27,15 +27,19 @@ assert!(craft_ppt_binary::import(b"not a .ppt", &craft_pptx::Plain).is_err());
|
||||
- Damaged files read as far as they can: a broken edit chain falls back to a scan of the edits,
|
||||
and unknown or damaged records are skipped. Sizes, counts, nesting and picture decoding are
|
||||
bounded.
|
||||
- Password-protected files are refused (`PptError::Encrypted`), as are PowerPoint 4.0 and earlier
|
||||
files (`PptError::TooOld`).
|
||||
- Password-protected files are refused (`PptError::Encrypted`). Formats older than PowerPoint 95
|
||||
aren't read: a compound file whose current user record has neither of [MS-PPT]'s tokens and
|
||||
that has no edit record is refused as too old (`PptError::TooOld`), and a file that isn't a
|
||||
compound file is `PptError::NotPpt`. No file from an earlier PowerPoint was tried.
|
||||
|
||||
## PowerPoint 95
|
||||
|
||||
PowerPoint 95 (7.0) files use the same compound file and many of the same record numbers, but a
|
||||
16-byte record header and a single record tree with no edit chain. A file whose `PowerPoint
|
||||
Document` stream starts with that tree is read on its own path; anything else takes the
|
||||
PowerPoint 97 path. What was learned of the format came from files PowerPoint 95 itself wrote,
|
||||
PowerPoint 97 path. The tree's Document must hold its DocumentAtom, which counts the slides;
|
||||
`import_checked` names the slides that count says a cut-short file is missing ("slide 3"). What
|
||||
was learned of the format came from files PowerPoint 95 itself wrote,
|
||||
one feature per file, and the public [MS-PPT] numbering where the records keep it.
|
||||
|
||||
So far a PowerPoint 95 file imports with its slide size, its masters (each the caller's default
|
||||
|
||||
@@ -11,8 +11,10 @@
|
||||
//! as far as their slide size, masters with their colour schemes, backgrounds, placeholder boxes
|
||||
//! and text styles, and slides with their order, master, layout type, background, placeholders
|
||||
//! and text boxes with formatted, anchored text, and shapes and lines with their fill, line and
|
||||
//! shadow; their pictures and notes are not read yet.
|
||||
//! PowerPoint 4.0 and earlier are refused as too old.
|
||||
//! shadow; their pictures and notes are not read yet. Older formats aren't read: a compound file
|
||||
//! in neither framing is refused as too old when its current user record has neither of
|
||||
//! [MS-PPT]'s tokens and it has no edit record, and a file that isn't a compound file is not a
|
||||
//! presentation.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
@@ -41,7 +43,7 @@ pub enum PptError {
|
||||
NotPpt,
|
||||
#[error("the presentation is password-protected")]
|
||||
Encrypted,
|
||||
#[error("the presentation was made by PowerPoint 4.0 or earlier")]
|
||||
#[error("the presentation is in a PowerPoint format older than PowerPoint 95")]
|
||||
TooOld,
|
||||
#[error("the presentation is damaged: {0}")]
|
||||
Damaged(String),
|
||||
@@ -60,7 +62,8 @@ pub fn import(bytes: &[u8], defaults: &dyn Defaults) -> Result<Presentation, Ppt
|
||||
}
|
||||
|
||||
/// [`import`], plus the slides the file lists but whose objects couldn't be found, so they are not
|
||||
/// in the presentation.
|
||||
/// in the presentation; for a PowerPoint 95 file, the slides its DocumentAtom counts past those it
|
||||
/// holds, by position.
|
||||
pub fn import_checked(bytes: &[u8], defaults: &dyn Defaults) -> Result<(Presentation, Vec<String>), PptError> {
|
||||
let file = CompoundFile::open(bytes).map_err(|e| match e {
|
||||
CfbError::NotCfb => PptError::NotPpt,
|
||||
@@ -76,7 +79,9 @@ pub fn import_checked(bytes: &[u8], defaults: &dyn Defaults) -> Result<(Presenta
|
||||
// PowerPoint 95 writes one tree of 16-byte-header records; anything else takes the
|
||||
// PowerPoint 97 path, which refuses older formats as too old.
|
||||
if let Some(document) = ppt95::document(&stream) {
|
||||
return Ok((ppt95::convert(&document, defaults), vec![]));
|
||||
let p = ppt95::convert(&document, defaults);
|
||||
let lost = ppt95::lost(&document, &p);
|
||||
return Ok((p, lost));
|
||||
}
|
||||
let current_user = file.read_path_capped("Current User", cap).unwrap_or_default();
|
||||
// A damaged or missing picture stream costs the pictures, not the presentation.
|
||||
|
||||
@@ -97,6 +97,8 @@ const NO_INSTANCE: u32 = 0xFFFF;
|
||||
/// Slides, and masters, read at most: beyond any real presentation, so a file of empty slide
|
||||
/// records can't make an unbounded presentation.
|
||||
const MAX_SLIDES: usize = 1 << 16;
|
||||
/// Where the DocumentAtom holds the number of slides.
|
||||
const SLIDE_COUNT: usize = 32;
|
||||
/// Masters read at most. Each becomes a copy of the caller's default master with its layouts, so
|
||||
/// the cap is lower than for slides; PowerPoint 95 writes one.
|
||||
const MAX_MASTERS: usize = 256;
|
||||
@@ -185,11 +187,23 @@ impl<'a> Iterator for Records<'a> {
|
||||
}
|
||||
|
||||
/// The Document container of a PowerPoint 95 `PowerPoint Document` stream: the stream starts with
|
||||
/// a type-3 record whose first child is a Document (1000) with no instance. `None` for anything
|
||||
/// else, PowerPoint 97 files included (their 8-byte headers don't read this way).
|
||||
/// a type-3 record whose first child is a Document (1000) with no instance, holding a DocumentAtom
|
||||
/// (1001) long enough for its slide count. `None` for anything else, PowerPoint 97 files included
|
||||
/// (their 8-byte headers don't read this way).
|
||||
pub fn document(stream: &[u8]) -> Option<Rec<'_>> {
|
||||
let root = Rec::at(stream, 0).filter(|r| r.kind == rt::ROOT)?;
|
||||
root.children().next().filter(|d| d.kind == rt::DOCUMENT && d.instance == NO_INSTANCE)
|
||||
let document = root.children().next().filter(|d| d.kind == rt::DOCUMENT && d.instance == NO_INSTANCE)?;
|
||||
document.child(rt::DOCUMENT_ATOM)?.u32(SLIDE_COUNT)?;
|
||||
Some(document)
|
||||
}
|
||||
|
||||
/// The slides the DocumentAtom counts but the file doesn't hold, by position ("slide 3"): what a
|
||||
/// truncated file lost. Empty for a whole file.
|
||||
pub fn lost(document: &Rec, p: &Presentation) -> Vec<String> {
|
||||
let count = document.child(rt::DOCUMENT_ATOM).and_then(|a| a.u32(SLIDE_COUNT)).map_or(0, |c| c as usize);
|
||||
let read = p.slides.len();
|
||||
// At most as many as could have been read, so a damaged count can't make a huge list.
|
||||
(read + 1..=count.min(read + MAX_SLIDES)).map(|n| format!("slide {n}")).collect()
|
||||
}
|
||||
|
||||
/// What a slide or master says about itself.
|
||||
|
||||
@@ -426,11 +426,20 @@ fn other_framings_keep_the_powerpoint_97_path() {
|
||||
let f = build(&[("PowerPoint Document", ¬95), ("Current User", &unknown_token)]);
|
||||
let e = crate::import(&f, &AppDefaults).unwrap_err();
|
||||
assert!(matches!(e, crate::PptError::TooOld));
|
||||
assert_eq!(e.to_string(), "the presentation was made by PowerPoint 4.0 or earlier");
|
||||
assert_eq!(e.to_string(), "the presentation is in a PowerPoint format older than PowerPoint 95");
|
||||
// The Document must be the root's first child, with no instance.
|
||||
let mut wrong_instance = stream_of(document_atom(5760, 4320, 0), &[], &[]);
|
||||
wrong_instance[20..24].copy_from_slice(&0u32.to_le_bytes());
|
||||
assert!(super::document(&wrong_instance).is_none());
|
||||
// ...and hold a DocumentAtom with its slide count: an empty Document is not this path's, and
|
||||
// without the PowerPoint 97 records it is damaged.
|
||||
let empty = container(rt::ROOT, 0, &[container(rt::DOCUMENT, 0, &[])]);
|
||||
assert!(super::document(&empty).is_none());
|
||||
assert!(matches!(import(&empty), Err(crate::PptError::Damaged(_))));
|
||||
// An atom too short to hold the count.
|
||||
assert!(super::document(&stream_of(rec(rt::DOCUMENT_ATOM, 4, 0, &[0; 32]), &[], &[])).is_none());
|
||||
// A document with no slides is still one.
|
||||
assert_eq!(import(&stream_of(document_atom(5760, 4320, 0), &[], &[])).unwrap().slides.len(), 0);
|
||||
|
||||
let p = crate::import(&crate::tests::file(), &AppDefaults).unwrap();
|
||||
assert_eq!(p.slides.len(), 1);
|
||||
@@ -651,6 +660,32 @@ fn a_bulleted_body_keeps_levels_bullets_and_margins() {
|
||||
|
||||
/// Character runs: bold, italic and underline bits, the font by slot, the size, and an RGB or a
|
||||
/// scheme slot of the slide's scheme as the colour.
|
||||
/// A run of no characters is skipped: it neither formats the text nor uses up the runs read, so a
|
||||
/// run after it still formats the text.
|
||||
#[test]
|
||||
fn zero_length_runs_are_skipped() {
|
||||
let runs = [plain(0), plain(0), plain(0), plain(0), char_run(4, 1, 40, 1, [0, 0, 0, 1])];
|
||||
let body = text(b"abc", &runs, &[para_run(0, false, 0, 2, 0), para_run(4, false, 0, 1, 0)], 1, [0; 4], BODY);
|
||||
let p = deck_of(vec![shape_95([-2362, -1613, -518, -1325], Some(body))], &[]);
|
||||
let para = &body_of(&p.slides[0].shapes[0]).paragraphs[0];
|
||||
let r = ¶.runs[0].props;
|
||||
assert_eq!((para.runs[0].text.as_str(), r.font.as_deref(), r.size, r.bold), ("abc", Some("Arial"), Some(40.0), Some(true)));
|
||||
assert_eq!(para.props.align, Some(Align::Center));
|
||||
}
|
||||
|
||||
/// The font table is the first font list holding a face: a list whose only face is cut short
|
||||
/// gives way to a later one.
|
||||
#[test]
|
||||
fn the_font_table_is_the_first_list_with_a_face() {
|
||||
let cut = container(4022, 9, &[rec(4023, 0, 0, &[0; 8])]);
|
||||
let lists = [container(2005, 0x19, &[cut, slot_end(0)]), container(2005, 0x19, &[font(b"Arial"), slot_end(0)])];
|
||||
let mut s = SlideSpec::new(0x100, 16);
|
||||
s.objects = vec![shape_95([-2362, -1613, -518, -1325], Some(simple_text(b"Hi", [-2362, -1613, -518, -1325], BODY)))];
|
||||
let stream = stream_with(document_atom(5760, 4320, 1), &[slide(&s)], &[master(MASTER, DEFAULT, [0; 4])], container(1010, 0x15, &lists));
|
||||
let p = import(&stream).unwrap();
|
||||
assert_eq!(body_of(&p.slides[0].shapes[0]).paragraphs[0].runs[0].props.font.as_deref(), Some("Arial"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_runs_keep_their_formatting() {
|
||||
let runs = [
|
||||
@@ -922,7 +957,7 @@ fn hostile_text_never_panics() {
|
||||
let t = one(text(b"Hi", &[plain(1000)], &[para_run(u32::MAX, true, 0, 7, 200)], 1, [0; 4], BODY)).unwrap();
|
||||
assert_eq!(t.text(), "Hi");
|
||||
assert_eq!(t.paragraphs[0].level, 4, "the level is capped at PowerPoint 95's fifth");
|
||||
// Many zero-length runs: no more are read than the text has characters (plus its end).
|
||||
// Many zero-length runs: skipped, and the text is all there.
|
||||
let many = vec![plain(0); 100_000];
|
||||
let t = one(text(b"abc", &many, &vec![para_run(0, false, 0, 0, 0); 50_000], 1, [0; 4], BODY)).unwrap();
|
||||
assert_eq!(t.text(), "abc");
|
||||
@@ -2227,9 +2262,23 @@ fn check_outlines(name: &str, p: &Presentation, wmf: &[u8]) -> usize {
|
||||
checked
|
||||
}
|
||||
|
||||
/// A file holding fewer slides than its DocumentAtom counts names the missing ones by position;
|
||||
/// a whole file names none.
|
||||
#[test]
|
||||
fn slides_the_count_says_are_missing_are_named() {
|
||||
let one = slide(&SlideSpec::new(0x100, 16));
|
||||
let checked = |count: u32| {
|
||||
crate::import_checked(&file_of(&stream_of(document_atom(5760, 4320, count), std::slice::from_ref(&one), &[])), &AppDefaults).unwrap()
|
||||
};
|
||||
assert_eq!(checked(3).1, ["slide 2", "slide 3"]);
|
||||
assert!(checked(1).1.is_empty());
|
||||
// A huge count names no more slides than the reader could have read.
|
||||
assert_eq!(checked(u32::MAX).1.len(), super::MAX_SLIDES);
|
||||
}
|
||||
|
||||
/// Real PowerPoint 95 files, when `CRAFT_PPT95_SAMPLES` names a directory of them (they are not in
|
||||
/// the repository). Each imports with the slide count its DocumentAtom gives, at 10 × 7.5 in, and
|
||||
/// its shapes' outlines match the rendering PowerPoint 95 saved beside it, if any.
|
||||
/// the repository). Each imports with the slide count and size its DocumentAtom gives and nothing
|
||||
/// lost, and its shapes' outlines match the rendering PowerPoint 95 saved beside it, if any.
|
||||
#[test]
|
||||
fn powerpoint_95_samples() {
|
||||
let Some(dir) = std::env::var_os("CRAFT_PPT95_SAMPLES") else { return };
|
||||
@@ -2241,12 +2290,15 @@ fn powerpoint_95_samples() {
|
||||
}
|
||||
let name = path.file_stem().unwrap().to_string_lossy().into_owned();
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
let p = crate::import(&bytes, &AppDefaults).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
let (p, lost) = crate::import_checked(&bytes, &AppDefaults).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
assert!(lost.is_empty(), "{name}: {lost:?}");
|
||||
let file = craft_cfb::CompoundFile::open(&bytes).unwrap();
|
||||
let stream = file.read_path_capped("PowerPoint Document", bytes.len() as u64).unwrap();
|
||||
let count = super::document(&stream).and_then(|d| d.child(rt::DOCUMENT_ATOM)).and_then(|a| a.u32(32)).unwrap();
|
||||
assert_eq!(p.slides.len(), count as usize, "{name}");
|
||||
assert_eq!((p.slide_size.width, p.slide_size.height), (720.0, 540.0), "{name}");
|
||||
let atom = super::document(&stream).and_then(|d| d.child(rt::DOCUMENT_ATOM)).unwrap();
|
||||
assert_eq!(p.slides.len(), atom.u32(32).unwrap() as usize, "{name}");
|
||||
// The size in master units, 8 to the point.
|
||||
let side = |at: usize| f64::from(atom.u32(at).unwrap()) / 8.0;
|
||||
assert_eq!((p.slide_size.width, p.slide_size.height), (side(0), side(4)), "{name}");
|
||||
check_sample(&name, &p);
|
||||
if let Ok(wmf) = std::fs::read(path.with_extension("wmf")) {
|
||||
outlines += check_outlines(&name, &p, &wmf);
|
||||
|
||||
@@ -110,12 +110,12 @@ impl Env {
|
||||
|
||||
pub(super) fn new(document: &Rec) -> Env {
|
||||
let Some(environment) = document.child(rt::ENVIRONMENT) else { return Env::default() };
|
||||
// There may be more than one font list; the first that holds fonts is the table.
|
||||
// There may be more than one font list; the first that holds a face is the table.
|
||||
let fonts = environment
|
||||
.children()
|
||||
.filter(|r| r.kind == rt::FONTS)
|
||||
.map(|list| slots(&list, rt::FONT, |f| f.child(rt::FONT_ATOM).map(|a| face(&a))))
|
||||
.find(|f| f.iter().any(Option::is_some))
|
||||
.find(|f| f.iter().flatten().any(|face| !face.is_empty()))
|
||||
.unwrap_or_default();
|
||||
let rulers =
|
||||
environment.child(rt::RULERS).map(|list| slots(&list, rt::RULER, |r| r.child(rt::RULER_ATOM).map(|a| ruler(&a)))).unwrap_or_default();
|
||||
@@ -257,7 +257,12 @@ fn entries<'a>(runs: &Rec<'a>, kind: u32, size: usize, total: u32) -> Vec<&'a [u
|
||||
if covered >= total {
|
||||
return out;
|
||||
}
|
||||
covered = covered.saturating_add(u32_at(e, 0).max(1));
|
||||
// A run of no characters formats nothing, so it is skipped rather than counted.
|
||||
let len = u32_at(e, 0);
|
||||
if len == 0 {
|
||||
continue;
|
||||
}
|
||||
covered = covered.saturating_add(len);
|
||||
out.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user