@nextgensoftwares/folio-diagrams
SmartArt (DrawingML diagrams) from their definitions: it reads the four diagram parts, lays the diagram out the way PowerPoint does, and writes the data and drawing parts back. DOM-free, with no dependency on slides or PPTX: the shapes it returns are plain boxes, geometry and text in layout units.
import { readDataModel, readLayoutDef, readStyleDef, readColorsDef, layoutDiagram, addNode, writeDataModel } from '@nextgensoftwares/folio-diagrams';
import { parseXml } from '@nextgensoftwares/folio-import/xml';
const diagram = {
data: readDataModel(parseXml(dataXml)),
layout: readLayoutDef(parseXml(layoutXml)),
style: readStyleDef(parseXml(quickStyleXml)),
colors: readColorsDef(parseXml(colorsXml)),
};
// A 6 × 2 inch frame in EMU (36000 per mm).
const { shapes, drawing, root } = layoutDiagram(diagram, 5486400, 1828800, { mm: 36000, measure: (text, sizePt) => myWidth(text, sizePt) });
const { data } = addNode(diagram.data, { after: someNodeId }, 'New step');
const dataXmlOut = writeDataModel(data, layoutDiagram({ ...diagram, data }, 5486400, 1828800, { mm: 36000 }).root);Reading
| Export | What |
|---|---|
readDataModel(root) → DataModel | dgm:dataModel: points (DataPoint: id, type, text, prSet, spPr), connections (Connection: parOf, presOf, presParOf with orders and transitions), the raw root (background, extensions, the drawing's relationship id) |
readLayoutDef(root) → LayoutDef | dgm:layoutDef: the layout node tree (LayoutNode: algorithm, shape, presOf, constraints, rules, variables) with forEach and choose / if / else |
readStyleDef(root) → StyleDef, readColorsDef(root) → ColorsDef | quick style (per style label: theme style references, text properties) and colour definition (fill, line, effect and text colour lists by label, with their meth: repeat, cycle, span) |
Diagram | { data, layout, style?, colors? } |
Layout
| Export | What |
|---|---|
layoutDiagram(diagram, w, h, opts: LayoutOptions) → DiagramLayout | the whole pipeline: data tree, presentation tree, layout, connector routes, shapes and the dsp:drawing part. opts.mm is layout units per millimetre (36000 for EMU, 96 / 25.4 for px); opts.measure(text, sizePt) gives text widths for font fitting (without it, text always fits) |
DiagramLayout | { root: PresNode, placed: Map<PresNode, Placed>, shapes: DiagramShape[], drawing: string } |
buildTree(data) → DataTree | the data points as a tree (TreePoint: point, parent, children, depth, index), connections by id |
buildPresTree(layout, tree, maxNodes?) → PresNode | the presentation tree the layout definition makes from the data. Saved presentation points supply their layout variables and the user's resize and move tweaks |
layoutTree(root, w, h, opts) → Map<PresNode, Placed> | boxes for every presentation node: constraints evaluated to a fixed point, then each node's algorithm (composite, lin, snake, cycle, pyra, hierRoot / hierChild, conn, tx, sp). Font rules (primFontSz…) shrink text together until it fits |
diagramShapes(root, placed, diagram, mm, routes) → DiagramShape[], drawingXml(shapes, mm) | the drawn shapes (DiagramShape: box, rotation, preset or path geometry, fill, line with arrowheads, style references, text as DiagramParagraph / DiagramRun with insets and anchor), and the drawing part from them |
storedSwapped(shape) | whether a file stores the shape's box swapped about its centre: true when the layout's own turn (baseRot, else the whole rot) is a quarter turn. drawingXml writes such shapes that way, and a comparison with a saved drawing swaps the same way (the SmartArt fidelity tool does) |
Box | { x, y, w, h, rot? } (clockwise degrees): a laid-out node's rectangle in layout units |
paragraphs(point), TextWidth | a point's text as plain paragraphs; the measure signature |
DiagramLimitError, MAX_PRES_NODES | thrown for a diagram that would grow past the limits (presentation nodes, selection visits, placements per pass). The limits keep crafted files from exhausting memory or time |
Editing and writing
| Export | What |
|---|---|
setText(d, id, text) | a node's text (lines become paragraphs; the first run's properties are kept) |
addNode(d, { after } | { parent }, text?) → { data, id } | a new node after a sibling or as a parent's last child, with its transitions |
removeNode(d, id) | the node and its subtree, with their connections |
moveNode(d, id, delta), indentNode(d, id), outdentNode(d, id) | reorder among siblings; demote under the previous sibling; promote after the parent |
writeDataModel(d, pres?) | the dgm:dataModel XML. Given the laid-out tree, it regenerates the presentation points and their connections, reusing saved ids and tweaks. It keeps the root's background and extensions |
textBody(text, like?), newId() | a dgm:t body, and a model id in PowerPoint's {8-4-4-4-12} form |
Slides use this through editDiagram and diagramToShapes in folio-slides. The editor's smartart.text / smartart.edit commands come from smartArtPlugin() in folio-slides-editor. PPTX import keeps each diagram's parts on its frame, and export writes the data and drawing parts back when the diagram was edited.
Library
Folio's own layouts, colour schemes and quick styles, written for Folio in the diagram language. They are named under Folio's URN, so Office treats them as custom layouts embedded in the file.
| Export | What |
|---|---|
DIAGRAM_LAYOUTS | LibraryLayout[] ({ id, name, kind, nested?, sample?, xml }), 39 layouts. List: block list, vertical list, vertical box list, vertical block list, horizontal list, pyramid list, chevron list, trapezoid list, square accent list, table list. Process: process, chevron, closed chevron, circle process, vertical, continuous arrow, snake (its arrows turn down between rows), accent process, timeline. Cycle: cycle, continuous cycle, text cycle, block cycle, nondirectional cycle, radial. Hierarchy: hierarchy, hierarchy list. Relationship: basic Venn (each label in the part of its circle no other circle covers, up to four circles; a ring past that), linear Venn, radial Venn, stacked Venn, target, opposing arrows, equation. Matrix: grid, grid matrix, titled matrix. Pyramid: pyramid, inverted pyramid |
DIAGRAM_COLORS, colorsDefXml(scheme?), DiagramColors | { id, name }[] for the accent1…accent6, colorful and outline colour definitions (theme colours); the colorsDef part XML for one (default accent1) |
DIAGRAM_STYLES, styleDefXml(style?), DiagramStyle | { id, name }[] for the flat, subtle and intense quick styles (the theme's line, fill and effect styles); the styleDef part XML for one (default flat) |
DiagramKind | a library layout's category: 'list' | 'process' | 'cycle' | 'hierarchy' | 'relationship' | 'matrix' | 'pyramid' |
newDiagramParts(layoutId, { items?, colors?, style?, id? }) → { data, layout, style, colors } | the four parts of a new diagram (an unknown id takes the first layout; colours accent1 and style flat by default; id generates model ids), with sample items by default (the layout's sample, else sampleItems(kind); DiagramItem: { text, children? }); diagramDataXml(items, ids) builds the data part |
estimateWidth(text, sizePt) | a text width estimate (per character class) for laying out without the fonts at hand |
Fidelity
node tools/fidelity/smartart/run.mjs DIR [--layout id] [--verbose] compares the layout with the drawing PowerPoint or Word saved in each .pptx or .docx file in DIR. It matches the drawn shapes by data point and reports how many fall within 1% and 3% of the frame (needs pnpm --filter @nextgensoftwares/folio-diagrams build). On a local corpus of 31 files (28 layouts, 477 shapes), 98% are within 1%: every layout but one, whose drawing was saved before its frame was resized (4:3 to 16:9). It measures text with each file's theme font: Calibri by Carlito (from the repo's font fixtures), other fonts by the stand-ins folio-fonts picks (fontFallbacks), as fontconfig has them.
Behaviour the drawings showed, which the spec leaves open:
- User tweaks.
custScaleX/Yresize a shape where it sits in a flow, and the flow reflows around it. A shrunk shape gives its room to the others, and the branches above take its new size. An enlarged shape reaches past its slot, and a composite holding it gets that much more room.custLinFact(Neighbor)X/Ythen move it by a share of its own or its neighbour's size; that neighbour is the next shape in the flow, else the previous one. Moves never resize branches. - Moves in composites and against spacers. In a composite, the shapes the user resized count when the composite aligns its children, and
custLinFact(Neighbor)X/Ythen move a shape by a share of its own size. In a flow, an empty spacer neighbour measures as wide as the flow across it. A move stops at the diagram's frame. - Offsets. An edge read from a shape includes its size offset (
t = b − (h + hOff)), as the shape is drawn. - Hierarchies.
alignOffindents hanging children by a share of the shape's width, without widening the branch. - Cycles. Curves are arcs of the ring, drawn in the box of the whole circle. They run from node edge to node edge, less
begPad/endPad(shares of that gap,connDist). Arrows sit midway between the nodes they join. Neighbours staysibSpapart, a round node measured by its diameter. - Text that doesn't fit. A text box's own rule on its size (
rule w fact 1overw = 0.3 × userA) widens it all the way before the fonts shrink. The flow holding it then scales the row, so its spaces shrink relative to the boxes. A word too wide for its line makes the font shrink; PowerPoint doesn't break it. - Flows. A rule on named children (
rule w for ch forName linV) shrinks those children, and the spaces sized from them, before the whole row scales.spgoes only between two shapes, since an explicit spacer replaces it. A row is a band as deep as its deepest child, placed byvertAlign/horzAlign. Composites flow by the shape their aspect ratio (ar) asks for. - Composites and constraints. A composite caps its children to its box, and a size tied to the capped one follows. A size follows from two of its edges. References read bounded values. A reference to a name no node has reads the value declared for it.
- Shapes. A shape the layout turns shrinks about its centre to stay within its box. A file stores a box swapped when the layout turns the shape a quarter turn.