diagram_io/src/vsdx.rs#
Source: lib/diagram_io/src/vsdx.rs
vsdx -- the VSDX family (.vsdx .vsdm .vstx .vstm .vssx .vssm) and diagram_core's model
vsdx::read(&bytes, &Limits::VSDX) -> Result<Loaded, Error>
Loaded { doc, format, warnings, more_warnings }
vsdx::write(&doc, &WriteOptions::DEFAULT) -> Result<Vec<u8>, WriteError>
WriteOptions { format, level, zip } another format (EX-11), the DEFLATE level, ZIP bounds
vsdx::new_document(kind, Measure) a new drawing, template or stencil (blank.rs)
Format::from_content_type(ct) / from_extension("vsdx") / extension() / content_type()
Limits::VSDX the bounds of a file: ZIP (archive::Limits), XML nesting and
attributes, counts of shapes, cells, rows and sections, the
length of one formula and of one shape text
Error / Warning why a file was refused / what was odd but kept
What lands where (MS-VSDX; FEATURES.md 1.1 and 1.6):
document.xml DocumentSettings, Colors, FaceNames, StyleSheets, DocumentSheet; EventList,
HeaderFooter, PublishSettings and everything else unknown in doc.extras
pages.xml the pages (PageSheet, the Rel to the page part), the page parts' shapes and
Connect entries
masters.xml the masters (PageSheet, Icon, Rel), the master parts' shapes and connects
themes every part a theme relationship names, raw (doc.themes); a page part's own
theme relationship sets Page::theme (Visio files pick a page's theme by its
ThemeIndex cell: JD4 reads that)
data (JD18) connections.xml, recordsets.xml and the recordset parts: doc.data
(data.rs; kept as parts when not understood)
everything else windows.xml, docProps (core, app, custom), the thumbnail, media, comments,
validation, solutions, the macro project, unknown parts: doc.parts as
bytes, in package order
relationships every one as read in doc.rels (also the external ones, never followed)
Unknown attributes, elements and text go into the Extras of the element they stood in
(diagram_core's sheet.rs says how). Nothing is computed on opening: values stay the cached
values of the file, connectors are not rerouted, Cell::stale stays false.
The macro project of a macro-enabled file (vbaProject.bin and its relationship) is kept as a
part and never run; Document::macros says the file has the right for one. A file whose main
content type is not macro-enabled but that carries a macro project is refused (Error::Macros).
Writing (pack.rs names the parts and keeps the relationships, write.rs makes the XML) puts
every part of the model back: document.xml, pages.xml and masters.xml with their parts, the
themes and every part kept as bytes (unknown packages pass through), the unknown items of
every element at their place, the relationships with their ids (raw XML may name them), the
main part's relationship of Visio's own type, deleted subshapes with ID 4294967295, children
in [MS-VSDX]'s order, numbers in the shortest form that reads back the same. The same model
always gives the same bytes, and a file read and written again reads as the same model and
writes the same bytes once more. A document without windows.xml gets one of its own: desktop
Visio refuses a file without it (error 271), although [MS-VSDX] calls the part unused. A
macro project is written back unchanged into a format
that may carry it (never run); into one that may not it is WriteError::Macros. The writer
does not compute anything either: a cell whose formula changed (Cell::stale) keeps the value
it has until the formula engine sets one. It does not recurse and never panics; XML the
model only holds as written must be well-formed where it stands, or the part is refused
(WriteError::Xml) rather than written broken.
Every file is untrusted input. The ZIP and OPC rules come from archive::opc (names, content
types, relationships, sizes, the ratio of compressed parts); this module adds the XML bounds,
the counts, the structure (root elements, required attributes, IDs as numbers, r:id that name a
relationship of the right type, a part used once), and refuses cycles: styles based on each
other, masters that contain instances of each other, background pages of each other. A
reference that names nothing (a master, master shape, style, background page, connected shape)
is kept as it is and reported as a Warning. No panic, and no recursion: deep groups cost heap,
never stack.
struct Formatone of the six files of the VSDX family: drawing, template or stencil, with or without the right to carry macroskind: DocKind-- drawing (.vsdx), template (.vstx) or stencil (.vssx)macros: bool-- macro-enabled (.vsdm, .vstm, .vssm)
impl Format
fn from_content_type(content_type: &str) -> Option<Format>the format of a main part's content type (exact, as [MS-VSDX] names them)fn from_extension(ext: &str) -> Option<Format>the format of a file extension without the dot, ASCII case ignored ("VSDX", "vssm")fn extension(self) -> &'static strthe extension without the dot ("vsdx")fn content_type(self) -> &'static strthe content type of the main partstruct Limitsthe bounds of one file; Limits::VSDX is the defaultzip: archive::Limits-- the ZIP container: parts, sizes, the ratio of a compressed part (archive::Limits)depth: usize-- most open elements in one XML part (a group in a group costs two levels); the reader does not recurse, so this bounds memory and time, never the stackattrs: usize-- most attributes on one elementshapes: usize-- most shapes in the document (pages and masters, all levels)cells: usize-- most cells in the document (every sheet)rows: usize-- most rows in the documentsections: usize-- most sections in the documentobjects: usize-- most pages, masters and styles togetheritems: usize-- most other things kept: connects, triggers, text marks and runs, colours, fonts, settings, relationships and unknown items (attributes, elements, text)formula_bytes: usize-- most bytes of one formula (F) or text value (V)text_bytes: usize-- most bytes of one shape textwarnings: usize-- most warnings kept in Loaded::warnings (the others are only counted)
impl Limits
const VSDX: Limits = Limits { zip: archive::Limits { entries: 100_000, part_by ...large drawings: 100000 parts, 512 MiB a part, 2 GiB in all, archives up to 1 GiB, no part denser than 200:1 (the proposal of archive::Limits); 256 levels of XML, 256 attributes, a million shapes, 16 million cells, 4 million rows, 2 million sections, formulas and values up to 1 MiB, shape texts up to 4 MiB, 100000 pages, masters and styles, 4 million other itemsenum Errorwhy a file was refusedPackage(opc::Error)-- the ZIP container or the OPC package: structure, names, content types, relationships, sizes and ratios (archive::opc)NotVisio-- no main part of the VSDX family: not a Visio file (another OPC format, or no main part)Macros-- a macro project in a file whose main content type is not macro-enabledXml { part: String, line: u32, error: XmlError, }-- a part is no XML this reader takes (linecounts from 1)Structure { part: String, line: u32, what: &'static str, }-- a part breaks the structure of the format: another root element, a missing or malformed attribute (an ID that is no number), an r:id that names no relationship of the right type, a part used twice, a duplicate IDLimit(&'static str)-- the document needs more than Limits allows: "shapes", "cells", "rows", "sections", "objects", "items", "formula", "text"Cycle { what: &'static str, id: u32, }-- a cycle: "style" (styles based on each other), "master" (masters containing instances of each other), "background" (background pages of each other); the ID where it was foundBinary { offset: u64, what: &'static str, }-- (vss.rs) a binary file of Visio 2003-2010 (.vss, .vsd, .vst) breaks its structure: the compound file, the VisioDocument stream or a stream or chunk in it;offsetis where in the stream (0 for the compound file),whatsays how ("version 6 (Visio 2000/2002)" for a version this reader does not take)
enum Warningsomething odd that was kept as it isMissingMaster { part: String, shape: u32, master: u32, }-- a shape names a master the document does not haveMissingMasterShape { part: String, shape: u32, master_shape: u32, }-- a subshape of an instance names a shape its master does not haveMissingStyle { part: String, style: u32, }-- a sheet or a setting names a style the document does not haveMissingBackPage { page: u32, back_page: u32, }-- a page names a background page that does not exist or is no background pageMissingShape { part: String, shape: u32, }-- a Connect entry names a shape the page or master does not haveNoContents { what: &'static str, id: u32, }-- a page or master without a Rel to its contents: it has no shapesExternalPicture { part: String, shape: u32, }-- a picture whose relationship points outside the package: kept, never fetchedUnusedPart { part: String, }-- a page or master part that no Page or Master element names: kept as a plain partLeftOut { what: String, }-- (vdx.rs) something of the XML formats of 2003-2010 that has no place in a file of the VSDX family, left out: tab stops, solution XML, data connections, a preview picture, a picture of an unknown kind, a macro project that is no OLE file, elements of other namespaces
struct Loadeda file read into the modeldoc: Document-- the documentformat: Format-- which of the six formats the main content type sayswarnings: Vec<Warning>-- what was odd, at most Limits::warnings of themmore_warnings: usize-- how many warnings more there were
struct WriteOptionshow vsdx::write writes a fileformat: Option<Format>-- the format to write; None: the document's own (Document::kind, Document::macros). A drawing can be written as a template and the other way round (EX-11); the content stays as it is, only the main part's content type changeslevel: u8-- DEFLATE level 1 (fastest) to 9 (smallest), 0: stored; 6 by defaultzip: archive::Limits-- the bounds of the ZIP container written (Limits::VSDX.zip by default): a document that needs more is refused instead of written into a file the reader would refuse
impl WriteOptions
const DEFAULT: WriteOptions = WriteOptions { format: None, level: 6, zip: Limits::VSDX. ...the document's own format, DEFLATE level 6, the reader's ZIP boundsenum WriteErrorwhy a document could not be writtenPackage(opc::Error)-- the package: a part name or content type OPC does not take, a part too large for WriteOptions::zip, more parts than it allows (archive::opc)Macros-- the document carries a macro project (vbaProject.bin and its relationship) and the format may not: write it as .vsdm, .vstm or .vssm, or take the project out of Document::partsPart { part: String, what: &'static str, }-- a part the model keeps as bytes (Document::parts) has a name that is no valid part name or that another part has, or no content typeXml { part: String, line: u32, }-- XML the model holds as written (unknown elements in the extras, unknown text marks, trigger contents) does not fit where it stands: the part would not be well-formed
fn remove_macros(doc: &mut Document) -> bool(JD16, DF-04) take the macro project out of a document: the parts a macro-enabled format carries for it (vbaProject.bin, vbaData.xml) and the relationships to them; Document::macros becomes false. For "Speichern als .vsdx" after the user said yes; true when there was onefn write(doc: &Document, options: &WriteOptions) -> Result<Vec<u8>, WriteError>write a document as a file of the VSDX family (see the module comment); the same document always gives the same bytes, and a document read from a file and written unchanged reads back as the same modelfn read(bytes: &[u8], limits: &Limits) -> Result<Loaded, Error>read a file of the VSDX family;limitsbound everything (Limits::VSDX for real files)