JFLib

diagram_core/src/document.rs#

Source: lib/diagram_core/src/document.rs

document -- the document: pages, masters, styles, the document sheet, themes and parts

  Document::new(DocKind::Drawing)        empty: no pages, masters or styles
  doc.intern("PinX") / sym(..) / str(sym)   the document's string table (strings.rs)
  doc.pages() / page(r) / page_by_id(id) / page_by_name_u(..)     (masters, styles alike)
  doc.shape(r) -> Option<Shape>          a shape with its links: id, data, children,
                                         parent, master shape, NameU and Name
  doc.sheet(SheetRef) / sheet_styles(..)  any ShapeSheet of the document
  doc.insert_page(page) / insert_master / insert_style      (untracked, for loaders)
  doc.page_mut(r) / master_mut / style_mut / doc_sheet_mut  (untracked)
  doc.unused_page_name("Page")           "Page-3": the first free number
  doc.transaction(label)                 tracked edits with undo (edit.rs)

Handles: PageRef, MasterRef and StyleRef are slots of the document like ShapeRef is a slot of
its tree: stable for the document's life, also across undo, never reused.  The file's IDs
(page ID, master ID, style ID, shape ID) are attributes of the objects; references inside
the model use them as the file does (Master="2", LineStyle="3", BackPage="4", MasterShape),
so a shape whose master is gone still says which one it was.

Untracked access (the *_mut functions, the public fields) is for building a document --
diagram_io while reading, tests -- and for data the edit API does not cover yet.  Changes
made that way bypass the transactions, so an undo history recorded before them no longer
fits the document: clear it.

Names (LG-04): NameU is the universal key (formulas, matching masters, own shapes), Name what
the user sees; a file's Name stays as read.  A shape without NameU is "Sheet.<ID>", an
instance "<master's NameU>.<ID>"; without Name it shows its NameU.
  • struct PageRef(pub(crate) u32) a page of the document (a slot, stable across undo)

  • struct MasterRef(pub(crate) u32) a master of the document

  • struct StyleRef(pub(crate) u32) a style of the document

  • enum Owner what holds a shape tree

    • Page(PageRef) -- the shapes of a page
    • Master(MasterRef) -- the shapes of a master
  • struct ShapeRef a shape: its tree and its slot there

    • owner: Owner -- the page or master
    • slot: u32 -- the slot in its tree
  • enum SheetRef any ShapeSheet of the document

    • Document -- the document sheet
    • Style(StyleRef) -- a style sheet
    • Page(PageRef) -- a page sheet
    • Master(MasterRef) -- a master's page sheet
    • Shape(ShapeRef) -- a shape's sheet
  • enum DocKind what the document is

    • Drawing -- a drawing (.vsdx, .vsdm)
    • Template -- a template (.vstx, .vstm)
    • Stencil -- a stencil: masters without pages (.vssx, .vssm)
  • struct DocSettings the DocumentSettings element

    • top_page: Option<u32> -- TopPage: the ID of the page shown first
    • default_text_style: Option<u32> -- DefaultTextStyle: the style ID new text gets
    • default_line_style: Option<u32> -- DefaultLineStyle
    • default_fill_style: Option<u32> -- DefaultFillStyle
    • default_guide_style: Option<u32> -- DefaultGuideStyle
    • attrs: Vec<(Sym, Sym)> -- the other attributes in file order
    • items: Vec<(Sym, Sym)> -- the child elements and their text in file order (GlueSettings, SnapSettings, SnapExtensions, SnapAngles, DynamicGridEnabled, ProtectStyles ...)
    • extras: Extras -- unknown items
  • struct ColorEntry an entry of the old colour table (Colors): index -> RGB

    • ix: u32 -- IX
    • rgb: Sym -- RGB as written ("#000000")
  • struct FaceName a font of the document (FaceName), its attributes in file order (NameU, Panose ...)

    • attrs: Vec<(Sym, Sym)> -- the attributes
  • struct DocSheet the document's own ShapeSheet (DocumentSheet)

    • styles: StyleRefs -- LineStyle, FillStyle, TextStyle
    • attrs: Vec<(Sym, Sym)> -- the other attributes in file order (Name, NameU ...)
    • sheet: Sheet -- the cells
  • struct Theme a theme part, kept as read (typed later, JD11)

    • part: Sym -- the part name (/visio/theme/theme1.xml)
    • data: Vec<u8> -- its bytes
  • struct Part a package part the model keeps as bytes: media, document properties, windows, comments, the thumbnail, a macro project (data recordsets are Document::data since JD18)

    • name: Sym -- the part name
    • content_type: Sym -- its content type
    • rel_type: Sym -- the type of the relationship that points to it
    • data: Vec<u8> -- its bytes
  • struct Relationship a relationship of the package as read: which part (or the package) points to which target with which type. The model derives the ones it owns (pages, masters, themes, pictures) from its own links when it writes; the list keeps every relationship so that parts it only keeps as bytes (Part), external targets and ids that kept XML refers to (r:id) survive

    • source: Sym -- the source part (/visio/document.xml), "" for the package itself
    • id: Sym -- the id (rId3), unique per source
    • kind: Sym -- the type, a URI
    • target: Sym -- the target as written: relative to the source, or a URI when external
    • external: bool -- TargetMode="External": outside the package, never followed
    • part: Option<Sym> -- the part an internal target names (/visio/media/image1.png); None when external
  • enum DocError why an object could not be put into the document

    • DuplicateId(u32) -- another object of the same kind has this ID
  • struct Document a drawing, template or stencil

    • kind: DocKind -- drawing, template or stencil
    • macros: bool -- whether the file is macro-enabled (.vsdm, .vssm, .vstm); macros are kept, never run
    • settings: DocSettings -- DocumentSettings
    • colors: Vec<ColorEntry> -- the old colour table
    • faces: Vec<FaceName> -- the fonts
    • themes: Vec<Theme> -- the theme parts (Page::theme indexes them)
    • parts: Vec<Part> -- the other package parts, as bytes
    • data: crate::data::DataSets -- (JD18) external data: connections, recordsets, rows bound to shapes (data.rs; changed through data::* in transactions)
    • rels: Vec<Relationship> -- every relationship of the package as read (see Relationship)
    • attrs: Vec<(Sym, Sym)> -- the attributes of the VisioDocument element
    • extras: Extras -- unknown items of the VisioDocument element (EventList, HeaderFooter, PublishSettings ... until they are typed)
    • pages_extras: Extras -- unknown items of the Pages element
    • masters_extras: Extras -- unknown items of the Masters element
    • colors_extras: Extras -- unknown items of the Colors element (a ColorEntry with more than IX and RGB too)
    • faces_extras: Extras -- unknown items of the FaceNames element (a FaceName with children too)
    • styles_extras: Extras -- unknown items of the StyleSheets element
    • fields: crate::field::Env -- what text fields show that the file does not hold: the app's locale and clock, the file's path (field::Env; never written)

impl Document

  • fn new(kind: DocKind) -> Document an empty document of this kind

  • fn strings(&self) -> &Strings the string table

  • fn strings_mut(&mut self) -> &mut Strings the string table to add to (loaders hand it to Value::parse, Formula::parse, ShapeKind::parse); it only grows, so every Sym stays valid

  • fn intern(&mut self, text: &str) -> Sym the handle of a text, added when new

  • fn sym(&self, text: &str) -> Option<Sym> the handle of a text the document already has

  • fn str(&self, sym: Sym) -> &str the text of a handle

  • fn pages(&self) -> impl Iterator<Item = PageRef> + '_ the pages in document order (foreground and background)

  • fn foreground_pages(&self) -> impl Iterator<Item = PageRef> + '_ the foreground pages in document order

  • fn background_pages(&self) -> impl Iterator<Item = PageRef> + '_ the background pages in document order

  • fn page_count(&self) -> usize the number of pages

  • fn page(&self, r: PageRef) -> Option<&Page> a page

  • fn page_mut(&mut self, r: PageRef) -> Option<&mut Page> a page for untracked changes (see the module comment)

  • fn page_by_id(&self, id: u32) -> Option<PageRef> the page with this page ID

  • fn page_by_name_u(&self, name_u: &str) -> Option<PageRef> the page with this universal name

  • fn page_position(&self, r: PageRef) -> Option<usize> the position of a page in document order

  • fn background_of(&self, r: PageRef) -> Option<PageRef> the background page of a page

  • fn insert_page(&mut self, page: Page) -> Result<PageRef, DocError> add a page at the end (untracked)

  • fn unused_page_name(&self, prefix: &str) -> String "<prefix>-<n>" with the smallest n no page has as Name or NameU

  • fn masters(&self) -> impl Iterator<Item = MasterRef> + '_ the masters in document order

  • fn master(&self, r: MasterRef) -> Option<&Master> a master

  • fn master_mut(&mut self, r: MasterRef) -> Option<&mut Master> a master for untracked changes

  • fn master_by_id(&self, id: u32) -> Option<MasterRef> the master with this master ID

  • fn master_by_name_u(&self, name_u: &str) -> Option<MasterRef> the master with this universal name

  • fn insert_master(&mut self, master: Master) -> Result<MasterRef, DocError> add a master at the end (untracked)

  • fn styles(&self) -> impl Iterator<Item = StyleRef> + '_ the styles in document order

  • fn style(&self, r: StyleRef) -> Option<&Style> a style

  • fn style_mut(&mut self, r: StyleRef) -> Option<&mut Style> a style for untracked changes

  • fn style_by_id(&self, id: u32) -> Option<StyleRef> the style with this style ID

  • fn style_by_name_u(&self, name_u: &str) -> Option<StyleRef> the style with this universal name

  • fn insert_style(&mut self, style: Style) -> Result<StyleRef, DocError> add a style at the end (untracked)

  • fn doc_sheet(&self) -> &DocSheet the document's own ShapeSheet

  • fn doc_sheet_mut(&mut self) -> &mut DocSheet the document sheet for untracked changes

  • fn sheet(&self, r: SheetRef) -> Option<&Sheet> any sheet of the document

  • fn sheet_styles(&self, r: SheetRef) -> StyleRefs the styles a sheet inherits from (a style: the styles it is based on)

  • fn tree(&self, owner: Owner) -> Option<&ShapeTree> the shape tree of a page or master

  • fn shape(&self, r: ShapeRef) -> Option<Shape<'_>> a shape and its links

  • fn shape_by_id(&self, owner: Owner, id: u32) -> Option<ShapeRef> the shape with this file ID on a page or in a master

  • fn top_shapes(&self, owner: Owner) -> impl Iterator<Item = ShapeRef> + '_ the top-level shapes of a page or master in z-order

  • fn all_shapes(&self, owner: Owner) -> impl Iterator<Item = ShapeRef> + '_ every shape of a page or master, groups before their subshapes

  • fn master_shape_of(&self, r: ShapeRef) -> Option<ShapeRef> the shape r inherits from: for an instance its master's master shape, for a subshape of an instance the shape MasterShape names in the instance's master

  • fn master_of(&self, r: ShapeRef) -> Option<MasterRef> the master an instance (or a subshape of one) belongs to

  • fn master_in_use(&self, id: u32) -> bool whether any shape of the document is an instance of the master with this ID

  • fn master_depends_on(&self, a: u32, b: u32) -> bool whether the master with ID a uses the master with ID b (itself or through instances in its shapes, at any depth)

  • fn style_in_use(&self, id: u32) -> bool whether any sheet, style or setting of the document names the style with this ID

  • struct Shape<'d> a shape with access to its document (Document::shape)

impl<'d> Shape<'d>

  • fn r(&self) -> ShapeRef its handle
  • fn id(&self) -> u32 its file ID (unique on its page or in its master)
  • fn data(&self) -> &'d ShapeData everything it holds itself
  • fn header(&self) -> &'d ShapeHeader its attributes
  • fn sheet(&self) -> &'d Sheet its own cells (without inheritance)
  • fn sheet_ref(&self) -> SheetRef as a sheet reference
  • fn parent(&self) -> Option<ShapeRef> the group it belongs to
  • fn children(&self) -> impl Iterator<Item = ShapeRef> + 'd its subshapes in z-order
  • fn master_shape(&self) -> Option<ShapeRef> the shape it inherits from (Document::master_shape_of)
  • fn is_deleted(&self) -> bool whether the master's subshape is deleted here (Del="1")
  • fn text(&self) -> Option<&'d Text> its text, from the master shape when it has none of its own
  • fn name_u(&self) -> String the universal name: NameU, else derived ("Sheet.7", "<master's NameU>.7")
  • fn name(&self) -> String the shown name: Name, else NameU, else derived from the master's Name

Source: docs/API.md in the JFLib repository.