Files
craft-libs/crates/pptx/src/package.rs
Dylan M. TaylorandJordi Altayó 34ba6ae4da craft-pptx: the reader (from DeckCraft), on craft-opc and craft-drawingml (#1057)
* Add craft-drawingml: units, colours, preset shapes, custom geometry and text warps

The parts of DrawingML that need no document model, moved from DeckCraft
(60f91f78): crates/color, crates/geom (preset, warp, Xfrm and the EMU
conversions) and crates/pptx/src/custgeom.rs with CustomPath from
crates/model.

The code is unchanged except that custgeom reads craft-xml trees and writes
to a craft-xml Writer instead of DeckCraft's own XML helper, and matches
elements by local name. The 59 tests of the moved code come with it; six
more cover the custom geometry limits.

* drawingml: evaluate a custom geometry's guide lists once per coordinate space

read_cust_geom rebuilt Guides and evaluated avLst and gdLst again for every
path: up to 1,000 paths times up to 10,000 guides, each name copied and
hashed.

Guides are now cached per path coordinate space (w, h). At most 16 spaces
and 4 MiB of guide names are kept per geometry; paths in further spaces are
left out. The first space is always kept, and a geometry without guides is
not capped.

Ported from storytold/deckcraft#1789. A list counts as having guides only
when it has gd children, as that PR's review asked.

* drawingml: the model types for fills, lines, text, tables, charts and diagrams

Moved from DeckCraft (60f91f78) crates/model/src: style.rs, text.rs,
table.rs, chart.rs, numfmt.rs and trendline.rs, plus Diagram, DiagramNode
and the MediaId and SlideId newtypes from lib.rs.

The files are unchanged except that deckcraft_color:: is crate::color:: and
Fill and TableBorder allow clippy::large_enum_variant. Their 60 tests come
with them. Readers and writers follow.

* drawingml: the theme model (colour, font and format schemes)

Moved from DeckCraft crates/model/src/theme.rs at 60f91f78: FontSet, ScriptFont, FontScheme,
FormatScheme, Theme and default_format. The app's theme gallery, its variants and its own default
theme stay in the app. Theme::default() is a plain theme built in place, so nothing here depends
on a gallery.

* drawingml: readers and writers for fills, lines, text, charts, themes, table styles and diagrams

Moved from DeckCraft crates/pptx/src at 60f91f78: read/dml.rs, write/dml.rs, read/chart.rs,
read/chartex.rs, write/chart.rs, read/theme.rs, write/theme.rs, tables.rs and dgm.rs, onto
craft-xml. Package lookups (Imp, Part, Exp, Out) are the caller's part::Source and part::Sink.
The chart frame budget and kept-part capture stay with the presentation reader.

* drawingml: what WordCraft and GridCraft read that DeckCraft's copy did not

Every ST_PresetColorVal name has its colour, in any case and with the dk/lt/med abbreviations;
a sysClr without lastClr takes the usual value of its name; a Strict percentage (50%) reads
anywhere a percentage does; a chart series keeps its c:f formulas and the writer uses them.

* craft-drawingml: extra named theme colours and explicit tint rows

Ported from the format part of storytold/deckcraft#1746 by @jagjordi: ColorScheme::{extra, tints,
computed_tints}, ExtraColor, GalleryVariant, variants_of, and the a:clrScheme extension that
keeps them in a file.

* drawingml xmlx: copy only used, bounded namespace declarations onto kept fragments and mc branches

From storytold/deckcraft#1740 (988a4920), which landed after the extraction commit 60f91f78.

* Add craft-pptx: the PresentationML model (from DeckCraft)

Presentation, masters, layouts, slides, shapes, placeholders, media,
comments, sections, show settings, properties, embedded fonts, kept parts
and drawing guides from DeckCraft's crates/model/src/lib.rs, and
transitions and animations from anim.rs. The DrawingML types come from
craft-drawingml. The placeholder matching from DeckCraft's resolve.rs
moves as placeholder::parents, without the rest of resolve.

Presentation::default() is now an empty 16:9 deck with no masters; the
starting deck stays with the application.

* craft-pptx: the reader (from DeckCraft), on craft-opc and craft-drawingml

DeckCraft's crates/pptx reader (read.rs, read/shapes.rs and the chart
frame code of read/chart.rs) moved onto craft-opc's Package and
craft-xml trees, with the DrawingML readers of craft-drawingml behind a
part::Source over the import state. What DeckCraft took from its own
defaults (a master for a file without one, the theme of a master that
names none) comes from a caller-supplied Defaults; Plain is the neutral
one. Placeholder geometry inheritance uses placeholder::parents.

Transitions, animations, notes and comments follow in later changes.

* craft-pptx: drop DeckCraft issue numbers from reader comments

* craft-pptx: link the package budgets from the tag budget's doc

---------

Co-authored-by: Jordi Altayó <jordiag@kth.se>
2026-11-11 21:36:57 -05:00

91 lines
3.1 KiB
Rust

//! The package being read: craft-opc's [`craft_opc::Package`] with the limits and lookups the
//! reader uses.
use craft_drawingml::xmlx;
use craft_opc::{ContentTypes, Limits, Oversized, Rels};
use craft_xml::El;
use crate::Error;
/// Most bytes one part inflates to.
pub const MAX_PART: u64 = 512 << 20;
/// Most bytes one XML part may have to be parsed.
pub const MAX_XML_PART: usize = 96 << 20;
/// Most bytes kept from the whole package.
pub const MAX_TOTAL: u64 = 2 << 30;
/// Most bytes inflated for the whole package, parts left out included.
pub const MAX_INFLATE_WORK: u64 = 2 * MAX_TOTAL;
/// Most ZIP entries.
pub const MAX_ENTRIES: usize = 50_000;
/// The package limits: oversized or damaged parts are left out with a warning, and reading stops
/// once [`MAX_INFLATE_WORK`] is spent.
pub const LIMITS: Limits =
Limits { max_entries: MAX_ENTRIES, max_part: MAX_PART, max_total: MAX_TOTAL, max_work: MAX_INFLATE_WORK, oversized: Oversized::Skip };
/// A parsed part: its root element.
#[derive(Clone, Debug, Default)]
pub(crate) struct Doc {
pub root: El,
}
impl Doc {
/// Namespace declarations (`xmlns:x`, `xmlns`) on the root element.
pub fn ns_decls(&self) -> Vec<(String, String)> {
xmlx::ns_decls(&self.root)
}
}
/// Parses a part with [`xmlx::parse`] (names as written, depth and node limits).
pub(crate) fn parse(bytes: &[u8]) -> Result<Doc, Error> {
xmlx::parse(bytes).map(|root| Doc { root }).map_err(|e| Error::Xml(e.to_string()))
}
pub(crate) struct Package {
pkg: craft_opc::Package,
types: ContentTypes,
}
impl Package {
pub fn open(bytes: &[u8]) -> Result<Self, Error> {
let pkg = craft_opc::Package::open(bytes, &LIMITS).map_err(|e| Error::NotPptx(e.to_string()))?;
Ok(Package { types: pkg.content_types(), pkg })
}
pub fn get(&self, name: &str) -> Option<&[u8]> {
self.pkg.get(name)
}
pub fn get_shared(&self, name: &str) -> Option<&std::sync::Arc<Vec<u8>>> {
self.pkg.get_shared(name)
}
/// An XML part's bytes, when it is within [`MAX_XML_PART`].
pub fn xml_bytes(&self, name: &str) -> Option<&[u8]> {
let b = self.get(name)?;
if b.len() > MAX_XML_PART {
log::warn!("pptx: XML part {name} too large");
return None;
}
Some(b)
}
/// An XML part, parsed; `None` (with a warning) when it is missing, too large or malformed.
pub fn xml(&self, name: &str) -> Option<Doc> {
match parse(self.xml_bytes(name)?) {
Ok(d) => Some(d),
Err(e) => {
log::warn!("pptx: {name}: {e}");
None
}
}
}
/// Relationships of a part (`ppt/slides/slide1.xml` → `ppt/slides/_rels/slide1.xml.rels`).
pub fn rels(&self, part: &str) -> Rels {
self.pkg.rels(part)
}
pub fn content_type(&self, part: &str) -> Option<String> {
self.types.of(part).map(str::to_string)
}
/// The part name as stored in the archive.
pub fn original_name(&self, name: &str) -> Option<&str> {
self.pkg.canonical(name)
}
}