@md2nativedocx/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/diagrams/architecture-diagram/parser.d.ts +29 -0
- package/dist/diagrams/architecture-diagram/parser.js +142 -0
- package/dist/diagrams/architecture-diagram/parser.js.map +1 -0
- package/dist/diagrams/architecture-diagram/translator.d.ts +41 -0
- package/dist/diagrams/architecture-diagram/translator.js +172 -0
- package/dist/diagrams/architecture-diagram/translator.js.map +1 -0
- package/dist/diagrams/architecture-diagram/types.d.ts +39 -0
- package/dist/diagrams/architecture-diagram/types.js +18 -0
- package/dist/diagrams/architecture-diagram/types.js.map +1 -0
- package/dist/diagrams/block/parser.d.ts +17 -0
- package/dist/diagrams/block/parser.js +312 -0
- package/dist/diagrams/block/parser.js.map +1 -0
- package/dist/diagrams/block/translator.d.ts +16 -0
- package/dist/diagrams/block/translator.js +137 -0
- package/dist/diagrams/block/translator.js.map +1 -0
- package/dist/diagrams/block/types.d.ts +38 -0
- package/dist/diagrams/block/types.js +6 -0
- package/dist/diagrams/block/types.js.map +1 -0
- package/dist/diagrams/c4/parser.d.ts +29 -0
- package/dist/diagrams/c4/parser.js +264 -0
- package/dist/diagrams/c4/parser.js.map +1 -0
- package/dist/diagrams/c4/translator.d.ts +50 -0
- package/dist/diagrams/c4/translator.js +240 -0
- package/dist/diagrams/c4/translator.js.map +1 -0
- package/dist/diagrams/c4/types.d.ts +74 -0
- package/dist/diagrams/c4/types.js +46 -0
- package/dist/diagrams/c4/types.js.map +1 -0
- package/dist/diagrams/class-diagram/parser.d.ts +40 -0
- package/dist/diagrams/class-diagram/parser.js +213 -0
- package/dist/diagrams/class-diagram/parser.js.map +1 -0
- package/dist/diagrams/class-diagram/translator.d.ts +48 -0
- package/dist/diagrams/class-diagram/translator.js +197 -0
- package/dist/diagrams/class-diagram/translator.js.map +1 -0
- package/dist/diagrams/class-diagram/types.d.ts +54 -0
- package/dist/diagrams/class-diagram/types.js +16 -0
- package/dist/diagrams/class-diagram/types.js.map +1 -0
- package/dist/diagrams/cynefin/parser.d.ts +24 -0
- package/dist/diagrams/cynefin/parser.js +102 -0
- package/dist/diagrams/cynefin/parser.js.map +1 -0
- package/dist/diagrams/cynefin/translator.d.ts +37 -0
- package/dist/diagrams/cynefin/translator.js +190 -0
- package/dist/diagrams/cynefin/translator.js.map +1 -0
- package/dist/diagrams/cynefin/types.d.ts +45 -0
- package/dist/diagrams/cynefin/types.js +36 -0
- package/dist/diagrams/cynefin/types.js.map +1 -0
- package/dist/diagrams/er-diagram/parser.d.ts +29 -0
- package/dist/diagrams/er-diagram/parser.js +156 -0
- package/dist/diagrams/er-diagram/parser.js.map +1 -0
- package/dist/diagrams/er-diagram/translator.d.ts +26 -0
- package/dist/diagrams/er-diagram/translator.js +151 -0
- package/dist/diagrams/er-diagram/translator.js.map +1 -0
- package/dist/diagrams/er-diagram/types.d.ts +44 -0
- package/dist/diagrams/er-diagram/types.js +17 -0
- package/dist/diagrams/er-diagram/types.js.map +1 -0
- package/dist/diagrams/event-modeling/parser.d.ts +16 -0
- package/dist/diagrams/event-modeling/parser.js +193 -0
- package/dist/diagrams/event-modeling/parser.js.map +1 -0
- package/dist/diagrams/event-modeling/translator.d.ts +17 -0
- package/dist/diagrams/event-modeling/translator.js +154 -0
- package/dist/diagrams/event-modeling/translator.js.map +1 -0
- package/dist/diagrams/event-modeling/types.d.ts +46 -0
- package/dist/diagrams/event-modeling/types.js +6 -0
- package/dist/diagrams/event-modeling/types.js.map +1 -0
- package/dist/diagrams/gantt/date-utils.d.ts +51 -0
- package/dist/diagrams/gantt/date-utils.js +131 -0
- package/dist/diagrams/gantt/date-utils.js.map +1 -0
- package/dist/diagrams/gantt/parser.d.ts +37 -0
- package/dist/diagrams/gantt/parser.js +221 -0
- package/dist/diagrams/gantt/parser.js.map +1 -0
- package/dist/diagrams/gantt/translator.d.ts +23 -0
- package/dist/diagrams/gantt/translator.js +259 -0
- package/dist/diagrams/gantt/translator.js.map +1 -0
- package/dist/diagrams/gantt/types.d.ts +31 -0
- package/dist/diagrams/gantt/types.js +14 -0
- package/dist/diagrams/gantt/types.js.map +1 -0
- package/dist/diagrams/git-graph/parser.d.ts +29 -0
- package/dist/diagrams/git-graph/parser.js +237 -0
- package/dist/diagrams/git-graph/parser.js.map +1 -0
- package/dist/diagrams/git-graph/translator.d.ts +49 -0
- package/dist/diagrams/git-graph/translator.js +224 -0
- package/dist/diagrams/git-graph/translator.js.map +1 -0
- package/dist/diagrams/git-graph/types.d.ts +72 -0
- package/dist/diagrams/git-graph/types.js +35 -0
- package/dist/diagrams/git-graph/types.js.map +1 -0
- package/dist/diagrams/ishikawa/parser.d.ts +15 -0
- package/dist/diagrams/ishikawa/parser.js +85 -0
- package/dist/diagrams/ishikawa/parser.js.map +1 -0
- package/dist/diagrams/ishikawa/translator.d.ts +12 -0
- package/dist/diagrams/ishikawa/translator.js +95 -0
- package/dist/diagrams/ishikawa/translator.js.map +1 -0
- package/dist/diagrams/ishikawa/types.d.ts +19 -0
- package/dist/diagrams/ishikawa/types.js +6 -0
- package/dist/diagrams/ishikawa/types.js.map +1 -0
- package/dist/diagrams/journey/parser.d.ts +14 -0
- package/dist/diagrams/journey/parser.js +78 -0
- package/dist/diagrams/journey/parser.js.map +1 -0
- package/dist/diagrams/journey/translator.d.ts +11 -0
- package/dist/diagrams/journey/translator.js +90 -0
- package/dist/diagrams/journey/translator.js.map +1 -0
- package/dist/diagrams/journey/types.d.ts +20 -0
- package/dist/diagrams/journey/types.js +6 -0
- package/dist/diagrams/journey/types.js.map +1 -0
- package/dist/diagrams/kanban/parser.d.ts +17 -0
- package/dist/diagrams/kanban/parser.js +129 -0
- package/dist/diagrams/kanban/parser.js.map +1 -0
- package/dist/diagrams/kanban/translator.d.ts +13 -0
- package/dist/diagrams/kanban/translator.js +72 -0
- package/dist/diagrams/kanban/translator.js.map +1 -0
- package/dist/diagrams/kanban/types.d.ts +21 -0
- package/dist/diagrams/kanban/types.js +6 -0
- package/dist/diagrams/kanban/types.js.map +1 -0
- package/dist/diagrams/mindmap/parser.d.ts +23 -0
- package/dist/diagrams/mindmap/parser.js +108 -0
- package/dist/diagrams/mindmap/parser.js.map +1 -0
- package/dist/diagrams/mindmap/translator.d.ts +31 -0
- package/dist/diagrams/mindmap/translator.js +249 -0
- package/dist/diagrams/mindmap/translator.js.map +1 -0
- package/dist/diagrams/mindmap/types.d.ts +19 -0
- package/dist/diagrams/mindmap/types.js +10 -0
- package/dist/diagrams/mindmap/types.js.map +1 -0
- package/dist/diagrams/packet/parser.d.ts +15 -0
- package/dist/diagrams/packet/parser.js +99 -0
- package/dist/diagrams/packet/parser.js.map +1 -0
- package/dist/diagrams/packet/translator.d.ts +14 -0
- package/dist/diagrams/packet/translator.js +66 -0
- package/dist/diagrams/packet/translator.js.map +1 -0
- package/dist/diagrams/packet/types.d.ts +14 -0
- package/dist/diagrams/packet/types.js +6 -0
- package/dist/diagrams/packet/types.js.map +1 -0
- package/dist/diagrams/pie/chart.d.ts +26 -0
- package/dist/diagrams/pie/chart.js +64 -0
- package/dist/diagrams/pie/chart.js.map +1 -0
- package/dist/diagrams/pie/parser.d.ts +18 -0
- package/dist/diagrams/pie/parser.js +93 -0
- package/dist/diagrams/pie/parser.js.map +1 -0
- package/dist/diagrams/pie/translator.d.ts +15 -0
- package/dist/diagrams/pie/translator.js +150 -0
- package/dist/diagrams/pie/translator.js.map +1 -0
- package/dist/diagrams/pie/types.d.ts +16 -0
- package/dist/diagrams/pie/types.js +6 -0
- package/dist/diagrams/pie/types.js.map +1 -0
- package/dist/diagrams/quadrant/parser.d.ts +24 -0
- package/dist/diagrams/quadrant/parser.js +96 -0
- package/dist/diagrams/quadrant/parser.js.map +1 -0
- package/dist/diagrams/quadrant/translator.d.ts +21 -0
- package/dist/diagrams/quadrant/translator.js +204 -0
- package/dist/diagrams/quadrant/translator.js.map +1 -0
- package/dist/diagrams/quadrant/types.d.ts +41 -0
- package/dist/diagrams/quadrant/types.js +10 -0
- package/dist/diagrams/quadrant/types.js.map +1 -0
- package/dist/diagrams/radar/chart.d.ts +21 -0
- package/dist/diagrams/radar/chart.js +65 -0
- package/dist/diagrams/radar/chart.js.map +1 -0
- package/dist/diagrams/radar/parser.d.ts +15 -0
- package/dist/diagrams/radar/parser.js +186 -0
- package/dist/diagrams/radar/parser.js.map +1 -0
- package/dist/diagrams/radar/translator.d.ts +12 -0
- package/dist/diagrams/radar/translator.js +98 -0
- package/dist/diagrams/radar/translator.js.map +1 -0
- package/dist/diagrams/radar/types.d.ts +29 -0
- package/dist/diagrams/radar/types.js +6 -0
- package/dist/diagrams/radar/types.js.map +1 -0
- package/dist/diagrams/requirement-diagram/parser.d.ts +29 -0
- package/dist/diagrams/requirement-diagram/parser.js +156 -0
- package/dist/diagrams/requirement-diagram/parser.js.map +1 -0
- package/dist/diagrams/requirement-diagram/translator.d.ts +24 -0
- package/dist/diagrams/requirement-diagram/translator.js +214 -0
- package/dist/diagrams/requirement-diagram/translator.js.map +1 -0
- package/dist/diagrams/requirement-diagram/types.d.ts +37 -0
- package/dist/diagrams/requirement-diagram/types.js +18 -0
- package/dist/diagrams/requirement-diagram/types.js.map +1 -0
- package/dist/diagrams/sankey/parser.d.ts +14 -0
- package/dist/diagrams/sankey/parser.js +123 -0
- package/dist/diagrams/sankey/parser.js.map +1 -0
- package/dist/diagrams/sankey/translator.d.ts +15 -0
- package/dist/diagrams/sankey/translator.js +132 -0
- package/dist/diagrams/sankey/translator.js.map +1 -0
- package/dist/diagrams/sankey/types.d.ts +16 -0
- package/dist/diagrams/sankey/types.js +6 -0
- package/dist/diagrams/sankey/types.js.map +1 -0
- package/dist/diagrams/sequence/parser.d.ts +22 -0
- package/dist/diagrams/sequence/parser.js +256 -0
- package/dist/diagrams/sequence/parser.js.map +1 -0
- package/dist/diagrams/sequence/translator.d.ts +21 -0
- package/dist/diagrams/sequence/translator.js +309 -0
- package/dist/diagrams/sequence/translator.js.map +1 -0
- package/dist/diagrams/sequence/types.d.ts +62 -0
- package/dist/diagrams/sequence/types.js +8 -0
- package/dist/diagrams/sequence/types.js.map +1 -0
- package/dist/diagrams/state-diagram/parser.d.ts +37 -0
- package/dist/diagrams/state-diagram/parser.js +215 -0
- package/dist/diagrams/state-diagram/parser.js.map +1 -0
- package/dist/diagrams/state-diagram/translator.d.ts +25 -0
- package/dist/diagrams/state-diagram/translator.js +165 -0
- package/dist/diagrams/state-diagram/translator.js.map +1 -0
- package/dist/diagrams/state-diagram/types.d.ts +38 -0
- package/dist/diagrams/state-diagram/types.js +13 -0
- package/dist/diagrams/state-diagram/types.js.map +1 -0
- package/dist/diagrams/timeline/parser.d.ts +17 -0
- package/dist/diagrams/timeline/parser.js +82 -0
- package/dist/diagrams/timeline/parser.js.map +1 -0
- package/dist/diagrams/timeline/translator.d.ts +13 -0
- package/dist/diagrams/timeline/translator.js +90 -0
- package/dist/diagrams/timeline/translator.js.map +1 -0
- package/dist/diagrams/timeline/types.d.ts +18 -0
- package/dist/diagrams/timeline/types.js +6 -0
- package/dist/diagrams/timeline/types.js.map +1 -0
- package/dist/diagrams/tree-view/parser.d.ts +16 -0
- package/dist/diagrams/tree-view/parser.js +129 -0
- package/dist/diagrams/tree-view/parser.js.map +1 -0
- package/dist/diagrams/tree-view/translator.d.ts +11 -0
- package/dist/diagrams/tree-view/translator.js +73 -0
- package/dist/diagrams/tree-view/translator.js.map +1 -0
- package/dist/diagrams/tree-view/types.d.ts +22 -0
- package/dist/diagrams/tree-view/types.js +6 -0
- package/dist/diagrams/tree-view/types.js.map +1 -0
- package/dist/diagrams/treemap/parser.d.ts +15 -0
- package/dist/diagrams/treemap/parser.js +131 -0
- package/dist/diagrams/treemap/parser.js.map +1 -0
- package/dist/diagrams/treemap/translator.d.ts +15 -0
- package/dist/diagrams/treemap/translator.js +136 -0
- package/dist/diagrams/treemap/translator.js.map +1 -0
- package/dist/diagrams/treemap/types.d.ts +22 -0
- package/dist/diagrams/treemap/types.js +6 -0
- package/dist/diagrams/treemap/types.js.map +1 -0
- package/dist/diagrams/venn/parser.d.ts +27 -0
- package/dist/diagrams/venn/parser.js +129 -0
- package/dist/diagrams/venn/parser.js.map +1 -0
- package/dist/diagrams/venn/translator.d.ts +25 -0
- package/dist/diagrams/venn/translator.js +214 -0
- package/dist/diagrams/venn/translator.js.map +1 -0
- package/dist/diagrams/venn/types.d.ts +33 -0
- package/dist/diagrams/venn/types.js +11 -0
- package/dist/diagrams/venn/types.js.map +1 -0
- package/dist/diagrams/wardley/parser.d.ts +14 -0
- package/dist/diagrams/wardley/parser.js +254 -0
- package/dist/diagrams/wardley/parser.js.map +1 -0
- package/dist/diagrams/wardley/translator.d.ts +15 -0
- package/dist/diagrams/wardley/translator.js +196 -0
- package/dist/diagrams/wardley/translator.js.map +1 -0
- package/dist/diagrams/wardley/types.d.ts +74 -0
- package/dist/diagrams/wardley/types.js +8 -0
- package/dist/diagrams/wardley/types.js.map +1 -0
- package/dist/diagrams/xychart/chart.d.ts +24 -0
- package/dist/diagrams/xychart/chart.js +113 -0
- package/dist/diagrams/xychart/chart.js.map +1 -0
- package/dist/diagrams/xychart/parser.d.ts +16 -0
- package/dist/diagrams/xychart/parser.js +190 -0
- package/dist/diagrams/xychart/parser.js.map +1 -0
- package/dist/diagrams/xychart/translator.d.ts +14 -0
- package/dist/diagrams/xychart/translator.js +187 -0
- package/dist/diagrams/xychart/translator.js.map +1 -0
- package/dist/diagrams/xychart/types.d.ts +32 -0
- package/dist/diagrams/xychart/types.js +6 -0
- package/dist/diagrams/xychart/types.js.map +1 -0
- package/dist/diagrams/zenuml/parser.d.ts +21 -0
- package/dist/diagrams/zenuml/parser.js +228 -0
- package/dist/diagrams/zenuml/parser.js.map +1 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +189 -0
- package/dist/index.js.map +1 -0
- package/dist/label-runs.d.ts +16 -0
- package/dist/label-runs.js +30 -0
- package/dist/label-runs.js.map +1 -0
- package/dist/layout/layout.d.ts +53 -0
- package/dist/layout/layout.js +536 -0
- package/dist/layout/layout.js.map +1 -0
- package/dist/parser/diagram-type.d.ts +41 -0
- package/dist/parser/diagram-type.js +97 -0
- package/dist/parser/diagram-type.js.map +1 -0
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.js +2 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/parser.d.ts +25 -0
- package/dist/parser/parser.js +1134 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/render-diagram.d.ts +156 -0
- package/dist/render-diagram.js +326 -0
- package/dist/render-diagram.js.map +1 -0
- package/dist/rendering-plan.d.ts +47 -0
- package/dist/rendering-plan.js +43 -0
- package/dist/rendering-plan.js.map +1 -0
- package/dist/smartart/chain.d.ts +216 -0
- package/dist/smartart/chain.js +558 -0
- package/dist/smartart/chain.js.map +1 -0
- package/dist/smartart/classify.d.ts +96 -0
- package/dist/smartart/classify.js +162 -0
- package/dist/smartart/classify.js.map +1 -0
- package/dist/smartart/cycle.d.ts +91 -0
- package/dist/smartart/cycle.js +323 -0
- package/dist/smartart/cycle.js.map +1 -0
- package/dist/smartart/dispatch.d.ts +48 -0
- package/dist/smartart/dispatch.js +49 -0
- package/dist/smartart/dispatch.js.map +1 -0
- package/dist/smartart/drawing.d.ts +155 -0
- package/dist/smartart/drawing.js +574 -0
- package/dist/smartart/drawing.js.map +1 -0
- package/dist/smartart/embed.d.ts +78 -0
- package/dist/smartart/embed.js +136 -0
- package/dist/smartart/embed.js.map +1 -0
- package/dist/smartart/from-graph.d.ts +42 -0
- package/dist/smartart/from-graph.js +106 -0
- package/dist/smartart/from-graph.js.map +1 -0
- package/dist/smartart/from-timeline.d.ts +30 -0
- package/dist/smartart/from-timeline.js +59 -0
- package/dist/smartart/from-timeline.js.map +1 -0
- package/dist/smartart/from-tree.d.ts +36 -0
- package/dist/smartart/from-tree.js +74 -0
- package/dist/smartart/from-tree.js.map +1 -0
- package/dist/smartart/generate-options.d.ts +12 -0
- package/dist/smartart/generate-options.js +2 -0
- package/dist/smartart/generate-options.js.map +1 -0
- package/dist/smartart/kanban.d.ts +24 -0
- package/dist/smartart/kanban.js +250 -0
- package/dist/smartart/kanban.js.map +1 -0
- package/dist/smartart/node-shape.d.ts +16 -0
- package/dist/smartart/node-shape.js +22 -0
- package/dist/smartart/node-shape.js.map +1 -0
- package/dist/smartart/styles.d.ts +114 -0
- package/dist/smartart/styles.js +141 -0
- package/dist/smartart/styles.js.map +1 -0
- package/dist/smartart/text.d.ts +16 -0
- package/dist/smartart/text.js +48 -0
- package/dist/smartart/text.js.map +1 -0
- package/dist/smartart/timeline-grouped.d.ts +40 -0
- package/dist/smartart/timeline-grouped.js +279 -0
- package/dist/smartart/timeline-grouped.js.map +1 -0
- package/dist/smartart/timeline.d.ts +51 -0
- package/dist/smartart/timeline.js +276 -0
- package/dist/smartart/timeline.js.map +1 -0
- package/dist/smartart/tree-deep.d.ts +61 -0
- package/dist/smartart/tree-deep.js +293 -0
- package/dist/smartart/tree-deep.js.map +1 -0
- package/dist/smartart/tree.d.ts +159 -0
- package/dist/smartart/tree.js +497 -0
- package/dist/smartart/tree.js.map +1 -0
- package/dist/translator/boxes.d.ts +36 -0
- package/dist/translator/boxes.js +95 -0
- package/dist/translator/boxes.js.map +1 -0
- package/dist/translator/canvas.d.ts +82 -0
- package/dist/translator/canvas.js +135 -0
- package/dist/translator/canvas.js.map +1 -0
- package/dist/translator/graph-shapes.d.ts +98 -0
- package/dist/translator/graph-shapes.js +223 -0
- package/dist/translator/graph-shapes.js.map +1 -0
- package/dist/translator/native-chart.d.ts +64 -0
- package/dist/translator/native-chart.js +109 -0
- package/dist/translator/native-chart.js.map +1 -0
- package/dist/translator/ooxml-translator.d.ts +79 -0
- package/dist/translator/ooxml-translator.js +1213 -0
- package/dist/translator/ooxml-translator.js.map +1 -0
- package/dist/translator/path-shape.d.ts +11 -0
- package/dist/translator/path-shape.js +37 -0
- package/dist/translator/path-shape.js.map +1 -0
- package/dist/translator/unsupported-diagram-note.d.ts +16 -0
- package/dist/translator/unsupported-diagram-note.js +34 -0
- package/dist/translator/unsupported-diagram-note.js.map +1 -0
- package/dist/translator/xml-escape.d.ts +28 -0
- package/dist/translator/xml-escape.js +40 -0
- package/dist/translator/xml-escape.js.map +1 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,1213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OOXML/DrawingML translator: AST + layout coordinates -> a self-contained
|
|
3
|
+
* WordprocessingML paragraph containing a `wpc:wpc` (drawing canvas) of
|
|
4
|
+
* ungrouped shapes, as an XML string.
|
|
5
|
+
*
|
|
6
|
+
* This is the heart of the project (spec §5.3). It is a pure function from
|
|
7
|
+
* (Flowchart, Layout) to a single XML string that can be injected verbatim into
|
|
8
|
+
* a Pandoc `RawBlock('openxml', ...)` (ADR 0002). "Pure" is load-bearing: the
|
|
9
|
+
* same inputs must always produce byte-identical output, so all shape ids are
|
|
10
|
+
* allocated by a per-call counter (a module-level counter would leak state
|
|
11
|
+
* across calls and break the golden tests' determinism).
|
|
12
|
+
*
|
|
13
|
+
* The emitted fragment is a complete `w:p` paragraph wrapping the drawing in
|
|
14
|
+
* the schema-required hierarchy `w:p -> w:r -> w:drawing -> wp:inline ->
|
|
15
|
+
* a:graphic -> a:graphicData -> wpc:wpc -> [shapes]`. A bare `wpc:wpc` cannot
|
|
16
|
+
* be a direct child of `w:body`; emitting the full paragraph is what makes the
|
|
17
|
+
* `.docx` open cleanly in Word (spec §5.3 "Encapsulation en `<wpg:wgp>` ...
|
|
18
|
+
* inséré dans `<w:drawing><wp:inline>...`" — the spec's own wording predates
|
|
19
|
+
* this file dropping the `wpg:wgp` group it originally described, see
|
|
20
|
+
* `renderContent`'s doc comment; the paragraph-wrapping requirement itself is
|
|
21
|
+
* unchanged). No shape is ever grouped (`wpg:wgp`/`wpg:grpSp`) any more — see
|
|
22
|
+
* `renderContent` and `renderSubgraph`'s doc comments for why.
|
|
23
|
+
*
|
|
24
|
+
* The element structure follows the official Microsoft schemas
|
|
25
|
+
* (ECMA-376 + MS-OE376, as published in the Open XML SDK) and was diffed
|
|
26
|
+
* against a real Word-authored document (`tools/word-reference/`):
|
|
27
|
+
* - `wps:wsp` requires `wps:cNvPr`, then either `wps:cNvSpPr` (shape) or
|
|
28
|
+
* `wps:cNvCnPr` (connector), then `wps:spPr`, optional `wps:style`,
|
|
29
|
+
* optional `wps:txbx`, and `wps:bodyPr`.
|
|
30
|
+
* - `wp:inline` requires `wp:extent` and `wp:docPr` before `a:graphic`.
|
|
31
|
+
* - Every `id` in a drawing (`wp:docPr`, `wps:cNvPr`) must be distinct, or
|
|
32
|
+
* Word reports the file as corrupt and offers to repair it.
|
|
33
|
+
*
|
|
34
|
+
* Security invariants (AGENTS.md):
|
|
35
|
+
* - Every user-controlled string is XML-escaped (rule #2) via {@link escapeXml}.
|
|
36
|
+
* - Colors reaching an XML attribute are validated as 6-digit hex
|
|
37
|
+
* ({@link hexColor}), not merely escaped: `a:srgbClr/@val` is attacker-
|
|
38
|
+
* reachable through `classDef` and through the public `TranslateOptions`.
|
|
39
|
+
* - No external OOXML relationship is ever emitted (rule #3): the output is
|
|
40
|
+
* fully self-contained, with all namespaces declared inline.
|
|
41
|
+
*/
|
|
42
|
+
import { SUBGRAPH_TITLE_HEIGHT, estimateTextWidth } from '../layout/layout.js';
|
|
43
|
+
import { labelLines } from '../label-runs.js';
|
|
44
|
+
import { escapeXml } from './xml-escape.js';
|
|
45
|
+
/** Pixels -> EMU (English Metric Units). Word uses 914400 EMU per inch; at 96
|
|
46
|
+
* DPI that is 9525 EMU per pixel. */
|
|
47
|
+
const EMU_PER_PX = 9525;
|
|
48
|
+
/** Default shape fill/line colors (spec §6.1). */
|
|
49
|
+
const DEFAULT_FILL = 'D9E2F3';
|
|
50
|
+
const DEFAULT_LINE = '2F5496';
|
|
51
|
+
/** Subgraph cluster container colors (`renderSubgraph`) -- deliberately
|
|
52
|
+
* neutral gray, not `DEFAULT_FILL`/`DEFAULT_LINE`, so a cluster box reads as
|
|
53
|
+
* "container" rather than "another node" (matches Mermaid's own subgraph
|
|
54
|
+
* convention: a gray dashed border, not a themed one). */
|
|
55
|
+
const SUBGRAPH_FILL = 'E8E8E8';
|
|
56
|
+
const SUBGRAPH_LINE = '999999';
|
|
57
|
+
/** ~1pt at 96 DPI (`EMU_PER_PX` below), matching a typical Mermaid cluster border weight. */
|
|
58
|
+
const SUBGRAPH_BORDER_WIDTH_EMU = 9525;
|
|
59
|
+
/**
|
|
60
|
+
* Node/subgraph-title text's effective size in half-points (`w:sz`'s unit):
|
|
61
|
+
* 24 = 12pt, Pandoc's `reference.docx` inherited default (`layout.ts`'s
|
|
62
|
+
* `FONT_SIZE_PX` doc comment) — node text never used to declare `w:sz`
|
|
63
|
+
* explicitly, just relying on that inherited default. It has to become
|
|
64
|
+
* explicit now that the root `wpg:wgp` group is gone (`renderContent`'s doc
|
|
65
|
+
* comment): that group used to make an oversized/scaled-down diagram's
|
|
66
|
+
* *rendered* text shrink along with its geometry as one visual unit, for
|
|
67
|
+
* free, regardless of the literal point size declared in the XML — verified
|
|
68
|
+
* directly (2026-09-02): a 3-subgraph diagram scaled to ~40% by
|
|
69
|
+
* `scaledExtent` still rendered legible, properly-proportioned text under
|
|
70
|
+
* the old group-based approach, but rendered blank boxes with text
|
|
71
|
+
* overflowing far outside them once the group (and its free visual scaling)
|
|
72
|
+
* was removed and geometry alone was pre-scaled. Without a group, nothing
|
|
73
|
+
* shrinks text automatically any more, so every text run's `w:sz` is scaled
|
|
74
|
+
* by the same `scale` factor as its container's geometry ({@link
|
|
75
|
+
* scaledFontSizeHalfPt}) to reproduce that same proportion explicitly.
|
|
76
|
+
*/
|
|
77
|
+
const NODE_FONT_SIZE_HALFPT = 24;
|
|
78
|
+
/** Never scale a run below this — a diagram scaled small enough to hit this
|
|
79
|
+
* floor already has other legibility problems (see TODO.md's "lisibilité
|
|
80
|
+
* des gros diagrammes" entry); this only prevents an invalid/zero `w:sz`. */
|
|
81
|
+
const MIN_FONT_SIZE_HALFPT = 4;
|
|
82
|
+
/** Scale a base `w:sz` (half-points) by the diagram's overall scale factor,
|
|
83
|
+
* floored so it never reaches zero (see {@link MIN_FONT_SIZE_HALFPT}). */
|
|
84
|
+
function scaledFontSizeHalfPt(baseHalfPt, scale) {
|
|
85
|
+
return Math.max(MIN_FONT_SIZE_HALFPT, Math.round(baseHalfPt * scale));
|
|
86
|
+
}
|
|
87
|
+
/** Never scale a stroke below this (EMU) — same rationale as
|
|
88
|
+
* {@link MIN_FONT_SIZE_HALFPT}, just in the other unit. */
|
|
89
|
+
const MIN_LINE_WIDTH_EMU = 3175; // 0.25pt
|
|
90
|
+
/**
|
|
91
|
+
* Scale a base `a:ln`/`w` stroke width (EMU) by the diagram's overall scale
|
|
92
|
+
* factor, floored so it never reaches zero (see {@link MIN_LINE_WIDTH_EMU}).
|
|
93
|
+
*
|
|
94
|
+
* Found 2026-09-02 (user report, real Word, a heavily-scaled-down large
|
|
95
|
+
* diagram): node borders and — much more visibly — connector arrowheads were
|
|
96
|
+
* declared at a fixed width regardless of `scale`, same class of bug as the
|
|
97
|
+
* one {@link scaledFontSizeHalfPt} already fixes for text. `a:tailEnd`'s
|
|
98
|
+
* `w`/`len` (arrowhead size) are enums (`sm`/`med`/`lg`, not a numeric EMU
|
|
99
|
+
* value — no equivalent scaling possible there directly), but their
|
|
100
|
+
* *rendered* physical size is proportional to the connector's own `a:ln w`,
|
|
101
|
+
* so scaling that stroke width alone is enough to make the arrowhead shrink
|
|
102
|
+
* along with the geometry it decorates, without touching the enum itself.
|
|
103
|
+
*/
|
|
104
|
+
function scaledLineWidth(baseEmu, scale) {
|
|
105
|
+
return Math.max(MIN_LINE_WIDTH_EMU, Math.round(baseEmu * scale));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Pick `a:headEnd`/`a:tailEnd`'s `w`/`len` enum (`sm`/`med`/`lg`, no numeric
|
|
109
|
+
* value exists — see {@link scaledLineWidth}'s doc comment) for a connector
|
|
110
|
+
* whose stroke width came out floored at {@link MIN_LINE_WIDTH_EMU}.
|
|
111
|
+
*
|
|
112
|
+
* Found 2026-09-04 (`docs/specs/FUTURE_full_mermaid_coverage_SPEC.md`'s
|
|
113
|
+
* punch list item 2, root-caused via a real LibreOffice render at 600 DPI —
|
|
114
|
+
* see `test-corpus/visual/fixtures/edge-types-extended.mmd`'s history in
|
|
115
|
+
* TODO.md for the original symptom report): a marker's rendered physical
|
|
116
|
+
* size is proportional to `a:ln w` (confirmed by `scaledLineWidth`'s own
|
|
117
|
+
* 2026-09-02 fix), but every *other* coordinate in this file — including the
|
|
118
|
+
* gap between two adjacent nodes — is multiplied by the uncapped `scale`
|
|
119
|
+
* factor directly (`renderContent`'s doc comment). Once `scale` is small
|
|
120
|
+
* enough to hit the width floor, the marker stops shrinking any further
|
|
121
|
+
* while the gap it sits in keeps shrinking with the uncapped `scale`: at
|
|
122
|
+
* `'med'` (the enum this translator always used before this fix), that gap
|
|
123
|
+
* closes entirely on a long enough compressed chain. A single-headed arrow
|
|
124
|
+
* just looks oversized when that happens; a two-headed one (`<-->`/`o--o`/
|
|
125
|
+
* `x--x`) has its two markers fully overlap into what reads as one solid
|
|
126
|
+
* diamond, with the connecting line itself no longer visible at all —
|
|
127
|
+
* reproduced on a 14-node `flowchart LR` chain, confirmed fixed by this
|
|
128
|
+
* function at the same scale.
|
|
129
|
+
*
|
|
130
|
+
* `'sm'` isn't proportional to how far below the floor the *unfloored*
|
|
131
|
+
* width would have landed — OOXML only offers 3 discrete sizes — but it
|
|
132
|
+
* measurably shrinks the marker-to-gap ratio, and every diagram that never
|
|
133
|
+
* hits the floor (the vast majority — `scale` reaches 1 for anything that
|
|
134
|
+
* already fits the page) is completely unaffected, keeping `'med'` exactly
|
|
135
|
+
* as before.
|
|
136
|
+
*/
|
|
137
|
+
function arrowMarkerSize(baseWidthEmu, scale) {
|
|
138
|
+
return baseWidthEmu * scale < MIN_LINE_WIDTH_EMU ? 'sm' : 'med';
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Usable page area in EMU for Pandoc's default reference document (US Letter,
|
|
142
|
+
* 1 inch margins => 6.5in x 9in). A drawing larger than this is scaled down
|
|
143
|
+
* uniformly rather than being clipped by Word: `wp:extent` and the group's
|
|
144
|
+
* `a:ext` shrink while `a:chOff`/`a:chExt` stay in native coordinates, so Word
|
|
145
|
+
* applies the homothety to every child shape for us.
|
|
146
|
+
*
|
|
147
|
+
* These are only the *default* fallback now (`export_customization_SPEC.md`
|
|
148
|
+
* §2.4): a caller whose export uses a different page format/orientation/
|
|
149
|
+
* margins (Lot 1) passes the real usable area via `TranslateOptions.maxDrawingCx`/
|
|
150
|
+
* `maxDrawingCy` instead of relying on this Letter-portrait assumption.
|
|
151
|
+
*
|
|
152
|
+
* Height IS capped, on purpose, even though the drawing is `wp:inline` (flows
|
|
153
|
+
* with the text) and one might expect Word to paginate an over-height inline
|
|
154
|
+
* object the way it does an oversized picture. It does not, for this element
|
|
155
|
+
* — see {@link MIN_SAFE_ASPECT_RATIO} for what's actually going on and why a
|
|
156
|
+
* height cap alone was never the whole story. Not verified against real
|
|
157
|
+
* Word; treat the multi-page case as genuinely unresolved (see TODO.md).
|
|
158
|
+
*/
|
|
159
|
+
const MAX_DRAWING_CX = 5943600;
|
|
160
|
+
const MAX_DRAWING_CY = 8229600;
|
|
161
|
+
/**
|
|
162
|
+
* Above this native (unscaled) height, a `wpc:wpc`/`wpg:wgp` group renders in
|
|
163
|
+
* LibreOffice ONLY IF its width:height ratio is at least
|
|
164
|
+
* {@link MIN_SAFE_ASPECT_RATIO} — narrower than that, and headless
|
|
165
|
+
* `soffice --convert-to png/pdf` produces nothing at all for it: no shapes,
|
|
166
|
+
* no error, no partial output, just the surrounding document text. Below this
|
|
167
|
+
* height, any ratio renders fine, however narrow (verified down to 0.09).
|
|
168
|
+
*
|
|
169
|
+
* Found empirically with ~25 controlled renders (fixed content, only height
|
|
170
|
+
* and/or width varied): e.g. at a fixed native height of 13.75in, width
|
|
171
|
+
* 10.62in (ratio 0.77) rendered nothing while width 12.5in (ratio 0.91)
|
|
172
|
+
* rendered correctly — same height, only the ratio changed. Bracketed the
|
|
173
|
+
* height cliff itself (at ratio-safe / unscaled shapes) between 7.92in
|
|
174
|
+
* (renders) and 9.38in (does not). Neither boundary was pinned to exact
|
|
175
|
+
* precision, so both constants below carry a safety margin: 7.5in is below
|
|
176
|
+
* the lowest observed safe height, 1.0 is above the highest observed unsafe
|
|
177
|
+
* ratio. Not explained by LibreOffice's source (no access to it), and not
|
|
178
|
+
* verified in real Word.
|
|
179
|
+
*/
|
|
180
|
+
const TALL_RATIO_RISK_HEIGHT = 6858000; // 7.5in
|
|
181
|
+
const MIN_SAFE_ASPECT_RATIO = 1.0;
|
|
182
|
+
/**
|
|
183
|
+
* Map a node shape to its DrawingML preset geometry (spec §6.1, extended to
|
|
184
|
+
* the v11.3+ `@{ shape: ... }` catalog — see `SHAPE_ALIAS_MAP` in
|
|
185
|
+
* `parser.ts` for which Mermaid shape names route here). The `flowChart*`
|
|
186
|
+
* family is Word/PowerPoint's own built-in flowchart shape gallery, so most
|
|
187
|
+
* of these are exact matches rather than approximations.
|
|
188
|
+
*/
|
|
189
|
+
export const PRST_BY_SHAPE = {
|
|
190
|
+
rect: 'rect',
|
|
191
|
+
roundRect: 'roundRect',
|
|
192
|
+
stadium: 'roundRect', // stadium approximated by roundRect with max adj
|
|
193
|
+
diamond: 'diamond',
|
|
194
|
+
cylinder: 'can', // cylinder approximated by `can`
|
|
195
|
+
ellipse: 'ellipse',
|
|
196
|
+
hexagon: 'hexagon',
|
|
197
|
+
parallelogram: 'parallelogram',
|
|
198
|
+
parallelogramAlt: 'parallelogram', // mirrored via flipH, see FLIP_BY_SHAPE
|
|
199
|
+
trapezoid: 'trapezoid',
|
|
200
|
+
trapezoidAlt: 'trapezoid', // mirrored via flipV, see FLIP_BY_SHAPE
|
|
201
|
+
subroutine: 'flowChartPredefinedProcess', // rect with the two inset bars Mermaid draws
|
|
202
|
+
doubleCircle: 'ellipse', // no double-ring preset exists; approximated by a single ellipse
|
|
203
|
+
document: 'flowChartDocument',
|
|
204
|
+
card: 'flowChartPunchedCard',
|
|
205
|
+
delay: 'flowChartDelay',
|
|
206
|
+
triangle: 'flowChartExtract',
|
|
207
|
+
triangleInverted: 'flowChartExtract', // mirrored via flipV, see FLIP_BY_SHAPE
|
|
208
|
+
windowPane: 'flowChartInternalStorage',
|
|
209
|
+
hourglass: 'flowChartCollate',
|
|
210
|
+
curvedTrapezoid: 'flowChartDisplay',
|
|
211
|
+
bolt: 'lightningBolt',
|
|
212
|
+
braceLeft: 'leftBrace',
|
|
213
|
+
braceRight: 'rightBrace',
|
|
214
|
+
bracePair: 'bracePair',
|
|
215
|
+
crossedCircle: 'flowChartOr',
|
|
216
|
+
filledCircle: 'flowChartSummingJunction', // no plain-filled-dot preset; closest built-in junction symbol
|
|
217
|
+
paperTape: 'flowChartPunchedTape',
|
|
218
|
+
horizontalCylinder: 'flowChartMagneticDisk',
|
|
219
|
+
linedCylinder: 'flowChartMagneticDrum',
|
|
220
|
+
manualInput: 'flowChartManualInput',
|
|
221
|
+
asymmetric: 'homePlate', // Mermaid's asymmetric/flag shape (id>Text]); closest built-in preset
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Preset geometries are directional (e.g. `trapezoid` is narrow-top/wide-bottom
|
|
225
|
+
* by default): Mermaid's mirrored syntaxes (`[\Text\]`, `[\Text/]`) reuse the
|
|
226
|
+
* same preset and flip it on one axis instead of needing a second preset.
|
|
227
|
+
* Same idea for `triangleInverted` (Mermaid's "Manual File" shape, `@{shape:
|
|
228
|
+
* flipped-triangle}`), which is `triangle` flipped vertically.
|
|
229
|
+
*/
|
|
230
|
+
const FLIP_BY_SHAPE = {
|
|
231
|
+
parallelogramAlt: 'flipH',
|
|
232
|
+
trapezoidAlt: 'flipV',
|
|
233
|
+
triangleInverted: 'flipV',
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Map an edge type to its connector line style (spec §6.2): dash pattern,
|
|
237
|
+
* stroke width, and the marker at each end (`headEnd` = the `from` node,
|
|
238
|
+
* `tailEnd` = the `to` node — Word's connector direction always runs
|
|
239
|
+
* start-to-end regardless of Mermaid's arrow direction). `invisible` (`~~~`)
|
|
240
|
+
* still gets a real connector (so Word's Selection Pane and any future
|
|
241
|
+
* docx2mermaid reader see it) but with `<a:noFill/>` instead of a stroke —
|
|
242
|
+
* the edge stays in the AST purely for Dagre's ranking, same as Mermaid's
|
|
243
|
+
* own behavior for invisible links.
|
|
244
|
+
*/
|
|
245
|
+
const LINE_STYLE_BY_EDGE = {
|
|
246
|
+
arrow: { dash: 'solid', width: 12700, headEnd: 'none', tailEnd: 'triangle' },
|
|
247
|
+
line: { dash: 'solid', width: 12700, headEnd: 'none', tailEnd: 'none' },
|
|
248
|
+
dotted: { dash: 'dash', width: 12700, headEnd: 'none', tailEnd: 'triangle' },
|
|
249
|
+
dottedLine: { dash: 'dash', width: 12700, headEnd: 'none', tailEnd: 'none' },
|
|
250
|
+
thick: { dash: 'solid', width: 25400, headEnd: 'none', tailEnd: 'triangle' },
|
|
251
|
+
thickLine: { dash: 'solid', width: 25400, headEnd: 'none', tailEnd: 'none' },
|
|
252
|
+
bidirectional: { dash: 'solid', width: 12700, headEnd: 'triangle', tailEnd: 'triangle' },
|
|
253
|
+
circle: { dash: 'solid', width: 12700, headEnd: 'none', tailEnd: 'oval' },
|
|
254
|
+
cross: { dash: 'solid', width: 12700, headEnd: 'none', tailEnd: 'diamond' },
|
|
255
|
+
circleBoth: { dash: 'solid', width: 12700, headEnd: 'oval', tailEnd: 'oval' },
|
|
256
|
+
crossBoth: { dash: 'solid', width: 12700, headEnd: 'diamond', tailEnd: 'diamond' },
|
|
257
|
+
invisible: { dash: 'solid', width: 12700, headEnd: 'none', tailEnd: 'none', invisible: true },
|
|
258
|
+
};
|
|
259
|
+
/** Namespaces declared inline on the root `wpg:wgp` (self-contained, rule #3). */
|
|
260
|
+
const NS = {
|
|
261
|
+
wpg: 'xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"',
|
|
262
|
+
wps: 'xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"',
|
|
263
|
+
wp: 'xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"',
|
|
264
|
+
a: 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"',
|
|
265
|
+
pic: 'xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"',
|
|
266
|
+
r: 'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"',
|
|
267
|
+
w: 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"',
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* Allocates the distinct `id` values a drawing needs. Word treats
|
|
271
|
+
* `wp:docPr/@id` and every `cNvPr/@id` in the same drawing as one id space and
|
|
272
|
+
* flags the document as corrupt on a collision, so a single counter feeds them
|
|
273
|
+
* all. It is per-call so the translator stays a pure function.
|
|
274
|
+
*/
|
|
275
|
+
function createIdAllocator() {
|
|
276
|
+
let next = 1;
|
|
277
|
+
return () => next++;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Validate a color as 6 hexadecimal digits, falling back to `fallback` when it
|
|
281
|
+
* is not. Applied to every color that reaches an `a:srgbClr/@val` attribute:
|
|
282
|
+
* `classDef` fills and the public {@link TranslateOptions} are both
|
|
283
|
+
* user-controlled, and escaping alone would still let a bad value through into
|
|
284
|
+
* the rendered XML.
|
|
285
|
+
*/
|
|
286
|
+
function hexColor(value, fallback) {
|
|
287
|
+
return value !== undefined && /^[0-9A-Fa-f]{6}$/.test(value) ? value.toUpperCase() : fallback;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Pick a readable text color for a shape fill, so labels stay visible on both
|
|
291
|
+
* light and dark fills. Word resolves shape text color from the theme
|
|
292
|
+
* (`a:fontRef` -> `lt1`, i.e. white), which is invisible on the light default
|
|
293
|
+
* fill; an explicit run color overrides it.
|
|
294
|
+
*/
|
|
295
|
+
function textColorFor(fillHex) {
|
|
296
|
+
const r = parseInt(fillHex.slice(0, 2), 16);
|
|
297
|
+
const g = parseInt(fillHex.slice(2, 4), 16);
|
|
298
|
+
const b = parseInt(fillHex.slice(4, 6), 16);
|
|
299
|
+
// Relative luminance (sRGB coefficients, WCAG's simplified form).
|
|
300
|
+
const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
|
|
301
|
+
return luminance > 0.6 ? '000000' : 'FFFFFF';
|
|
302
|
+
}
|
|
303
|
+
/** Default node-text insets (EMU, unscaled): 0.1in left/right, 0.05in
|
|
304
|
+
* top/bottom — Word's own defaults for a text-bearing shape. */
|
|
305
|
+
const NODE_TEXT_INSET_X = 91440;
|
|
306
|
+
const NODE_TEXT_INSET_Y = 45720;
|
|
307
|
+
/**
|
|
308
|
+
* The full `wps:bodyPr` element Word emits for a text-bearing shape, with all
|
|
309
|
+
* the attributes needed for the fill to render (a bare `<wps:bodyPr/>` makes
|
|
310
|
+
* Word render the group as an empty gray rectangle).
|
|
311
|
+
*
|
|
312
|
+
* Insets scale with `scale` like everything else (`renderContent`'s doc
|
|
313
|
+
* comment): left at a fixed 91440/45720 EMU regardless of how small `scale`
|
|
314
|
+
* has made the surrounding box, a significantly scaled-down node's insets
|
|
315
|
+
* alone can consume the entire box width, leaving no room to lay out the
|
|
316
|
+
* text at all — found rendering a 3-subgraph diagram scaled to ~40%, where
|
|
317
|
+
* every node's label rendered as blank (vertOverflow pushed it entirely
|
|
318
|
+
* outside the visible box) until these insets scaled down too.
|
|
319
|
+
*/
|
|
320
|
+
function bodyPr(scale) {
|
|
321
|
+
const insX = Math.round(NODE_TEXT_INSET_X * scale);
|
|
322
|
+
const insY = Math.round(NODE_TEXT_INSET_Y * scale);
|
|
323
|
+
return [
|
|
324
|
+
'<wps:bodyPr rot="0" spcFirstLastPara="0" vertOverflow="overflow" horzOverflow="overflow"',
|
|
325
|
+
` vert="horz" wrap="square" lIns="${insX}" tIns="${insY}" rIns="${insX}" bIns="${insY}"`,
|
|
326
|
+
' numCol="1" spcCol="0" rtlCol="0" fromWordArt="0" anchor="ctr" anchorCtr="0"',
|
|
327
|
+
' forceAA="0" compatLnSpc="1">',
|
|
328
|
+
' <a:prstTxWarp prst="textNoShape"><a:avLst/></a:prstTxWarp>',
|
|
329
|
+
' <a:noAutofit/>',
|
|
330
|
+
'</wps:bodyPr>',
|
|
331
|
+
].join('\n');
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* The `wps:style` element Word emits for a shape, referencing the document
|
|
335
|
+
* theme. Required for the shape fill/line to render (the `a:solidFill`/`a:ln`
|
|
336
|
+
* in `wps:spPr` alone are not enough — Word resolves the visual style from
|
|
337
|
+
* these theme references).
|
|
338
|
+
*/
|
|
339
|
+
function style() {
|
|
340
|
+
return [
|
|
341
|
+
'<wps:style>',
|
|
342
|
+
' <a:lnRef idx="2"><a:schemeClr val="accent1"><a:shade val="15000"/></a:schemeClr></a:lnRef>',
|
|
343
|
+
' <a:fillRef idx="1"><a:schemeClr val="accent1"/></a:fillRef>',
|
|
344
|
+
' <a:effectRef idx="0"><a:schemeClr val="accent1"/></a:effectRef>',
|
|
345
|
+
' <a:fontRef idx="minor"><a:schemeClr val="lt1"/></a:fontRef>',
|
|
346
|
+
'</wps:style>',
|
|
347
|
+
].join('\n');
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Translate a flowchart + its layout into a self-contained WordprocessingML
|
|
351
|
+
* paragraph (`w:p`) wrapping the drawing in the schema-required hierarchy.
|
|
352
|
+
*
|
|
353
|
+
* @param flowchart - The parsed flowchart AST.
|
|
354
|
+
* @param layout - Layout result (node + subgraph coordinates) from `layout/layout.ts`.
|
|
355
|
+
* @param options - Optional color overrides.
|
|
356
|
+
* @returns A single XML string (a complete `w:p` paragraph), ready to inject
|
|
357
|
+
* into a Pandoc `RawBlock('openxml', ...)` (ADR 0002).
|
|
358
|
+
*/
|
|
359
|
+
export function translateToOoxml(flowchart, layout, options = {}) {
|
|
360
|
+
const fill = hexColor(options.fill, DEFAULT_FILL);
|
|
361
|
+
const line = hexColor(options.line, DEFAULT_LINE);
|
|
362
|
+
const nextId = createIdAllocator();
|
|
363
|
+
const maxExtent = {
|
|
364
|
+
cx: options.maxDrawingCx ?? MAX_DRAWING_CX,
|
|
365
|
+
cy: options.maxDrawingCy ?? MAX_DRAWING_CY,
|
|
366
|
+
};
|
|
367
|
+
// `wp:docPr` shares its id space with the shapes below it, so it draws from
|
|
368
|
+
// the same allocator and must be taken before the group is rendered.
|
|
369
|
+
const docPrId = nextId();
|
|
370
|
+
const bb = computeBoundingBox(layout);
|
|
371
|
+
// Every child coordinate is pre-multiplied by this factor (see
|
|
372
|
+
// `renderContent`'s doc comment) instead of relying on an enclosing
|
|
373
|
+
// group's chOff/chExt-vs-ext transform to apply it implicitly.
|
|
374
|
+
const { scale } = scaledExtent(bb, maxExtent);
|
|
375
|
+
const content = renderContent(flowchart, layout, fill, line, nextId, scale);
|
|
376
|
+
return wrapInParagraph(content, bb, docPrId, maxExtent);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Render subgraphs + nodes + edges as siblings directly under the drawing
|
|
380
|
+
* canvas (`wpc:wpc`, `wrapInParagraph`) — no enclosing root `wpg:wgp`.
|
|
381
|
+
*
|
|
382
|
+
* A `wpg:wgp`/`wpg:grpSp` is a real Word "Group" object: Word treats its
|
|
383
|
+
* contents as one bundled unit on first click (double-click, or "Ungroup",
|
|
384
|
+
* to select an individual shape inside it) and, per user report, visibly
|
|
385
|
+
* doubles the selection outline (the canvas frame plus the group's own
|
|
386
|
+
* boundary) around the whole diagram. Subgraphs don't use `wpg:grpSp` either
|
|
387
|
+
* any more (`renderSubgraph`'s doc comment) — nothing in this translator's
|
|
388
|
+
* output groups shapes at all now. `wpc:wpc` already accepts `wps:wsp`
|
|
389
|
+
* directly per the OOXML schema, with no group needed to hold shapes
|
|
390
|
+
* together as one drawing.
|
|
391
|
+
*
|
|
392
|
+
* Dropping the root group loses its chOff/chExt-vs-ext trick for scaling an
|
|
393
|
+
* oversized diagram down to the page (`scaledExtent`) — that trick worked by
|
|
394
|
+
* giving every descendant coordinate a *native* value and letting Word's
|
|
395
|
+
* single group transform apply the visual shrink uniformly. Without a group,
|
|
396
|
+
* `scale` is applied directly to every coordinate at emission time instead:
|
|
397
|
+
* mathematically the same result (every descendant coordinate ends up
|
|
398
|
+
* multiplied by the same factor either way), just computed here rather than
|
|
399
|
+
* left to Word.
|
|
400
|
+
*/
|
|
401
|
+
function renderContent(flowchart, layout, fill, line, nextId, scale) {
|
|
402
|
+
const parts = [];
|
|
403
|
+
// Pre-assign a unique numeric id to every node so connectors (stCxn/endCxn)
|
|
404
|
+
// can reference the shape ids (the `wps:cNvPr id` attribute). Mermaid node
|
|
405
|
+
// ids are arbitrary strings; the OOXML shape id must be a unique number.
|
|
406
|
+
const nodeIds = new Map();
|
|
407
|
+
for (const node of flowchart.nodes) {
|
|
408
|
+
nodeIds.set(node.id, nextId());
|
|
409
|
+
}
|
|
410
|
+
// Render subgraph titles (spec §6.1) as flat shapes, then nodes and edges.
|
|
411
|
+
const renderedSubgraphs = new Set();
|
|
412
|
+
for (const sg of flowchart.subgraphs) {
|
|
413
|
+
parts.push(renderSubgraph(sg, flowchart, layout, renderedSubgraphs, nextId, scale));
|
|
414
|
+
}
|
|
415
|
+
for (const node of flowchart.nodes) {
|
|
416
|
+
const box = layout.nodes[node.id];
|
|
417
|
+
if (!box)
|
|
418
|
+
continue;
|
|
419
|
+
// Per-node fill/stroke from classDef/style/`:::` take priority over the
|
|
420
|
+
// global default (spec §6.3).
|
|
421
|
+
const nodeFill = hexColor(node.fill, fill);
|
|
422
|
+
const nodeLine = hexColor(node.stroke, line);
|
|
423
|
+
parts.push(renderNode(nodeIds.get(node.id), node.id, node.label, node.labelRuns, node.shape, box, nodeFill, nodeLine, scale));
|
|
424
|
+
}
|
|
425
|
+
flowchart.edges.forEach((edge, i) => {
|
|
426
|
+
const from = layout.nodes[edge.from];
|
|
427
|
+
const to = layout.nodes[edge.to];
|
|
428
|
+
if (!from || !to)
|
|
429
|
+
return;
|
|
430
|
+
const fromId = nodeIds.get(edge.from);
|
|
431
|
+
const toId = nodeIds.get(edge.to);
|
|
432
|
+
if (fromId === undefined || toId === undefined)
|
|
433
|
+
return;
|
|
434
|
+
const dagrePoints = layout.edges[i] ?? [];
|
|
435
|
+
// Every other node's box, i.e. what this connector must NOT be drawn
|
|
436
|
+
// through — see connectorGeometry's doc comment for why this is checked
|
|
437
|
+
// geometrically rather than trusted from Dagre's point count.
|
|
438
|
+
const otherBoxes = Object.entries(layout.nodes)
|
|
439
|
+
.filter(([id]) => id !== edge.from && id !== edge.to)
|
|
440
|
+
.map(([, box]) => box);
|
|
441
|
+
// Per-edge stroke color/width from `linkStyle` takes priority over the
|
|
442
|
+
// global default (spec §6.3).
|
|
443
|
+
const edgeLine = hexColor(edge.stroke, line);
|
|
444
|
+
parts.push(renderEdge(fromId, toId, edge.from, edge.to, edge.type, from, to, dagrePoints, otherBoxes, edgeLine, edge.strokeWidth, nextId, scale));
|
|
445
|
+
if (edge.label && edge.labelRuns) {
|
|
446
|
+
parts.push(renderEdgeLabel(edge.labelRuns, from, to, dagrePoints, otherBoxes, nextId(), scale));
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
return parts.join('\n');
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Wrap a `wpg:wgp` group in the schema-required paragraph hierarchy so Word
|
|
453
|
+
* accepts the fragment as a drawing:
|
|
454
|
+
* `w:p -> w:r -> w:drawing -> wp:inline -> a:graphic -> a:graphicData ->
|
|
455
|
+
* wpc:wpc -> [shapes]`.
|
|
456
|
+
*
|
|
457
|
+
* The drawing is **inline** (spec §5.3), so it flows with the surrounding text
|
|
458
|
+
* instead of floating over it, and it sits inside a drawing canvas (`wpc:wpc`)
|
|
459
|
+
* — the container Word itself uses for a set of shapes, hence the
|
|
460
|
+
* `wordprocessingCanvas` `a:graphicData` URI. Content's top-level shapes are
|
|
461
|
+
* direct children of the canvas rather than wrapped in a `wpg:wgp` group
|
|
462
|
+
* (`renderContent`'s doc comment) — `wpc:wpc`'s content model accepts
|
|
463
|
+
* `wps:wsp`/`wpg:grpSp` directly, no enclosing group required — so the
|
|
464
|
+
* `wpg`/`wps`/`pic`/`r` namespaces those children need are declared here,
|
|
465
|
+
* on the canvas, instead of on a root group element that no longer exists.
|
|
466
|
+
*/
|
|
467
|
+
function wrapInParagraph(content, bb, docPrId, maxExtent = { cx: MAX_DRAWING_CX, cy: MAX_DRAWING_CY }) {
|
|
468
|
+
const { cx, cy } = scaledExtent(bb, maxExtent);
|
|
469
|
+
return [
|
|
470
|
+
`<w:p ${NS.w}>`,
|
|
471
|
+
' <w:r>',
|
|
472
|
+
' <w:drawing>',
|
|
473
|
+
` <wp:inline ${NS.wp} distT="0" distB="0" distL="0" distR="0">`,
|
|
474
|
+
` <wp:extent cx="${cx}" cy="${cy}"/>`,
|
|
475
|
+
' <wp:effectExtent l="0" t="0" r="0" b="0"/>',
|
|
476
|
+
` <wp:docPr id="${docPrId}" name="Diagram ${docPrId}"/>`,
|
|
477
|
+
' <wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/></wp:cNvGraphicFramePr>',
|
|
478
|
+
` <a:graphic ${NS.a}>`,
|
|
479
|
+
' <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas">',
|
|
480
|
+
` <wpc:wpc xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" ${NS.wpg} ${NS.wps} ${NS.pic} ${NS.r}>`,
|
|
481
|
+
' <wpc:bg><a:solidFill><a:prstClr val="white"/></a:solidFill></wpc:bg>',
|
|
482
|
+
' <wpc:whole/>',
|
|
483
|
+
content,
|
|
484
|
+
' </wpc:wpc>',
|
|
485
|
+
' </a:graphicData>',
|
|
486
|
+
' </a:graphic>',
|
|
487
|
+
' </wp:inline>',
|
|
488
|
+
' </w:drawing>',
|
|
489
|
+
' </w:r>',
|
|
490
|
+
'</w:p>',
|
|
491
|
+
].join('\n');
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* The native (unscaled) EMU size of the drawing frame, widened past the
|
|
495
|
+
* content's actual width when needed to stay clear of the LibreOffice
|
|
496
|
+
* narrow-tall rendering failure (see {@link MIN_SAFE_ASPECT_RATIO}). The
|
|
497
|
+
* padding is inert canvas margin to the right of the content — it does not
|
|
498
|
+
* move or resize any node, subgraph, or edge, it only makes the frame's own
|
|
499
|
+
* declared bounding box wide enough to render at all. `wp:extent`
|
|
500
|
+
* (via {@link scaledExtent}) derives from this same padded value.
|
|
501
|
+
*/
|
|
502
|
+
function nativeExtent(bb) {
|
|
503
|
+
const nativeCx = Math.max(1, Math.round(bb.width * EMU_PER_PX));
|
|
504
|
+
const nativeCy = Math.max(1, Math.round(bb.height * EMU_PER_PX));
|
|
505
|
+
if (nativeCy > TALL_RATIO_RISK_HEIGHT && nativeCx / nativeCy < MIN_SAFE_ASPECT_RATIO) {
|
|
506
|
+
return { cx: Math.round(nativeCy * MIN_SAFE_ASPECT_RATIO), cy: nativeCy };
|
|
507
|
+
}
|
|
508
|
+
return { cx: nativeCx, cy: nativeCy };
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Convert a pixel bounding box to the EMU extent of the drawing frame, scaled
|
|
512
|
+
* down uniformly if it would not fit the usable page area. Returns the scale
|
|
513
|
+
* factor so the group's `a:ext` can shrink in step with `wp:extent` while its
|
|
514
|
+
* `a:chExt` stays in native coordinates.
|
|
515
|
+
*/
|
|
516
|
+
function scaledExtent(bb, maxExtent = { cx: MAX_DRAWING_CX, cy: MAX_DRAWING_CY }) {
|
|
517
|
+
const { cx: nativeCx, cy: nativeCy } = nativeExtent(bb);
|
|
518
|
+
const scale = Math.min(1, maxExtent.cx / nativeCx, maxExtent.cy / nativeCy);
|
|
519
|
+
return {
|
|
520
|
+
cx: Math.max(1, Math.round(nativeCx * scale)),
|
|
521
|
+
cy: Math.max(1, Math.round(nativeCy * scale)),
|
|
522
|
+
scale,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Render a subgraph's title as a plain top-level `wps:wsp` text box (spec
|
|
527
|
+
* §6.1), positioned with the same absolute (pre-scaled) coordinates as a
|
|
528
|
+
* node rather than nested in a `wpg:grpSp` group. Nested subgraphs are
|
|
529
|
+
* rendered recursively, each contributing its own flat title box.
|
|
530
|
+
*
|
|
531
|
+
* Not wrapped in a group any more (found 2026-09-02, alongside dropping the
|
|
532
|
+
* root `wpg:wgp` — `renderContent`'s doc comment): a subgraph never actually
|
|
533
|
+
* groups real content in the first place — member nodes are always rendered
|
|
534
|
+
* as separate top-level shapes at absolute coordinates (`renderContent`'s
|
|
535
|
+
* node loop), never nested inside the subgraph's own group — so the
|
|
536
|
+
* `wpg:grpSp` here only ever held a single title textbox positioned at
|
|
537
|
+
* (0,0) local to it. That's not just redundant, it's actively broken
|
|
538
|
+
* without a `wpg:wgp` ancestor: verified directly (2026-09-02) that
|
|
539
|
+
* LibreOffice renders a `wpg:grpSp` nested directly under `wpc:wpc` (no
|
|
540
|
+
* enclosing `wpg:wgp`) at the wrong position — its declared `a:off` is not
|
|
541
|
+
* honored — while the exact same title rendered as a plain top-level
|
|
542
|
+
* `wps:wsp` at the equivalent absolute coordinates lands correctly. A flat
|
|
543
|
+
* shape sidesteps the whole question, and needs no group logic at all.
|
|
544
|
+
*/
|
|
545
|
+
function renderSubgraph(sg, flowchart, layout, rendered, nextId, scale) {
|
|
546
|
+
if (rendered.has(sg.id))
|
|
547
|
+
return '';
|
|
548
|
+
rendered.add(sg.id);
|
|
549
|
+
const box = layout.subgraphs[sg.id];
|
|
550
|
+
if (!box)
|
|
551
|
+
return '';
|
|
552
|
+
const x = Math.round(box.x * EMU_PER_PX * scale);
|
|
553
|
+
const y = Math.round(box.y * EMU_PER_PX * scale);
|
|
554
|
+
const w = Math.max(1, Math.round(box.width * EMU_PER_PX * scale));
|
|
555
|
+
const h = Math.max(1, Math.round(box.height * EMU_PER_PX * scale));
|
|
556
|
+
const safeTitle = escapeXml(sg.title);
|
|
557
|
+
const insX = Math.round(NODE_TEXT_INSET_X * scale);
|
|
558
|
+
const insY = Math.round(NODE_TEXT_INSET_Y * scale);
|
|
559
|
+
const parts = [];
|
|
560
|
+
// Cluster container: a full-size box behind the title/nodes (spec §6.1,
|
|
561
|
+
// "fidelity to the Mermaid preview" — Mermaid's own renderer draws a
|
|
562
|
+
// visible rect around a subgraph's members, this translator's box used to
|
|
563
|
+
// draw only the floating title below with `noFill`/`ln w="0"`, see git
|
|
564
|
+
// history around 2026-09-03). Neutral gray, not the node fill/line colors,
|
|
565
|
+
// matching Mermaid's own convention of a cluster border that reads as
|
|
566
|
+
// "container", not "another node" -- and dashed, also matching Mermaid's
|
|
567
|
+
// default subgraph border style. Rendered first (behind, per emission
|
|
568
|
+
// order = z-order in this format) so it never occludes member nodes/edges,
|
|
569
|
+
// which are drawn later in `renderContent`.
|
|
570
|
+
parts.push(' <wps:wsp>');
|
|
571
|
+
parts.push(` <wps:cNvPr id="${nextId()}" name="${safeTitle} (container)"/>`);
|
|
572
|
+
parts.push(' <wps:cNvSpPr/>');
|
|
573
|
+
parts.push(' <wps:spPr>');
|
|
574
|
+
parts.push(' <a:xfrm>');
|
|
575
|
+
parts.push(` <a:off x="${x}" y="${y}"/>`);
|
|
576
|
+
parts.push(` <a:ext cx="${w}" cy="${h}"/>`);
|
|
577
|
+
parts.push(' </a:xfrm>');
|
|
578
|
+
parts.push(' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
|
|
579
|
+
parts.push(` <a:solidFill><a:srgbClr val="${SUBGRAPH_FILL}"><a:alpha val="40000"/></a:srgbClr></a:solidFill>`);
|
|
580
|
+
parts.push(` <a:ln w="${SUBGRAPH_BORDER_WIDTH_EMU}"><a:solidFill><a:srgbClr val="${SUBGRAPH_LINE}"/></a:solidFill>` +
|
|
581
|
+
'<a:prstDash val="dash"/></a:ln>');
|
|
582
|
+
parts.push(' </wps:spPr>');
|
|
583
|
+
parts.push(' <wps:bodyPr/>');
|
|
584
|
+
parts.push(' </wps:wsp>');
|
|
585
|
+
parts.push(' <wps:wsp>');
|
|
586
|
+
parts.push(` <wps:cNvPr id="${nextId()}" name="${safeTitle}"/>`);
|
|
587
|
+
parts.push(' <wps:cNvSpPr/>');
|
|
588
|
+
parts.push(' <wps:spPr>');
|
|
589
|
+
parts.push(' <a:xfrm>');
|
|
590
|
+
parts.push(` <a:off x="${x}" y="${y}"/>`);
|
|
591
|
+
parts.push(` <a:ext cx="${w}" cy="${Math.round(SUBGRAPH_TITLE_HEIGHT * EMU_PER_PX * scale)}"/>`);
|
|
592
|
+
parts.push(' </a:xfrm>');
|
|
593
|
+
parts.push(' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
|
|
594
|
+
parts.push(' <a:noFill/>');
|
|
595
|
+
parts.push(' <a:ln w="0"><a:noFill/></a:ln>');
|
|
596
|
+
parts.push(' </wps:spPr>');
|
|
597
|
+
parts.push(' <wps:txbx>');
|
|
598
|
+
parts.push(' <w:txbxContent>');
|
|
599
|
+
parts.push(' <w:p>');
|
|
600
|
+
parts.push(' <w:pPr><w:spacing w:before="0" w:after="0"/><w:jc w:val="center"/></w:pPr>');
|
|
601
|
+
parts.push(' <w:r>');
|
|
602
|
+
parts.push(` <w:rPr><w:color w:val="000000"/><w:sz w:val="${scaledFontSizeHalfPt(NODE_FONT_SIZE_HALFPT, scale)}"/></w:rPr>`);
|
|
603
|
+
parts.push(` <w:t xml:space="preserve">${safeTitle}</w:t>`);
|
|
604
|
+
parts.push(' </w:r>');
|
|
605
|
+
parts.push(' </w:p>');
|
|
606
|
+
parts.push(' </w:txbxContent>');
|
|
607
|
+
parts.push(' </wps:txbx>');
|
|
608
|
+
parts.push(` <wps:bodyPr lIns="${insX}" tIns="${insY}" rIns="${insX}" bIns="${insY}" anchor="ctr" anchorCtr="0"/>`);
|
|
609
|
+
parts.push(' </wps:wsp>');
|
|
610
|
+
// Nested subgraphs, each its own flat title box.
|
|
611
|
+
for (const childId of sg.subgraphIds) {
|
|
612
|
+
const child = flowchart.subgraphs.find((s) => s.id === childId);
|
|
613
|
+
if (child)
|
|
614
|
+
parts.push(renderSubgraph(child, flowchart, layout, rendered, nextId, scale));
|
|
615
|
+
}
|
|
616
|
+
return parts.join('\n');
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Render a single node as a `wps:wsp` (wordprocessing shape).
|
|
620
|
+
*
|
|
621
|
+
* `mermaidId` is stored in `cNvPr/descr` (not `name`): `name` stays the human
|
|
622
|
+
* label, which is what Word shows in its Selection Pane (a friendlier UX than
|
|
623
|
+
* a raw Mermaid id like "A" or "decision1"), while `descr` carries the original
|
|
624
|
+
* id so a future docx2mermaid reader can recover it (`docs/specs/FUTURE_docx2mermaid_SPEC.md`
|
|
625
|
+
* §4). Caveat: `descr` is the shape's Alt Text in Word, which screen readers
|
|
626
|
+
* announce, so this is not an invisible field; where the id should live is an
|
|
627
|
+
* open V2 decision (`docs/specs/04-roadmap-and-open-decisions.md` §5.2).
|
|
628
|
+
*/
|
|
629
|
+
function renderNode(id, mermaidId, label, labelRuns, shape, box, fill, line, scale) {
|
|
630
|
+
const x = Math.round(box.x * EMU_PER_PX * scale);
|
|
631
|
+
const y = Math.round(box.y * EMU_PER_PX * scale);
|
|
632
|
+
const w = Math.max(1, Math.round(box.width * EMU_PER_PX * scale));
|
|
633
|
+
const h = Math.max(1, Math.round(box.height * EMU_PER_PX * scale));
|
|
634
|
+
const prst = PRST_BY_SHAPE[shape] ?? 'rect';
|
|
635
|
+
const flip = FLIP_BY_SHAPE[shape];
|
|
636
|
+
const flipAttr = flip === 'flipH' ? ' flipH="1"' : flip === 'flipV' ? ' flipV="1"' : '';
|
|
637
|
+
const safeLabel = escapeXml(label);
|
|
638
|
+
const safeMermaidId = escapeXml(mermaidId);
|
|
639
|
+
const textColor = textColorFor(fill);
|
|
640
|
+
return [
|
|
641
|
+
' <wps:wsp>',
|
|
642
|
+
` <wps:cNvPr id="${id}" name="${safeLabel}" descr="${safeMermaidId}"/>`,
|
|
643
|
+
' <wps:cNvSpPr/>',
|
|
644
|
+
' <wps:spPr>',
|
|
645
|
+
` <a:xfrm${flipAttr}>`,
|
|
646
|
+
` <a:off x="${x}" y="${y}"/>`,
|
|
647
|
+
` <a:ext cx="${w}" cy="${h}"/>`,
|
|
648
|
+
' </a:xfrm>',
|
|
649
|
+
` <a:prstGeom prst="${prst}">`,
|
|
650
|
+
' <a:avLst/>',
|
|
651
|
+
' </a:prstGeom>',
|
|
652
|
+
` <a:solidFill><a:srgbClr val="${fill}"/></a:solidFill>`,
|
|
653
|
+
` <a:ln w="${scaledLineWidth(12700, scale)}" cap="flat" cmpd="sng" algn="ctr">`,
|
|
654
|
+
` <a:solidFill><a:srgbClr val="${line}"/></a:solidFill>`,
|
|
655
|
+
' <a:prstDash val="solid"/>',
|
|
656
|
+
' </a:ln>',
|
|
657
|
+
' </wps:spPr>',
|
|
658
|
+
style(),
|
|
659
|
+
' <wps:txbx>',
|
|
660
|
+
' <w:txbxContent>',
|
|
661
|
+
// <w:spacing w:before="0" w:after="0"/> overrides Pandoc's reference.docx
|
|
662
|
+
// docDefaults (w:pPrDefault -> w:spacing w:after="200", i.e. 10pt after
|
|
663
|
+
// every paragraph unless overridden). Found from a real Word screenshot
|
|
664
|
+
// (2026-09-02): with `anchor="ctr"` centering the whole paragraph box —
|
|
665
|
+
// including that trailing 10pt nothing renders into — the visible glyphs
|
|
666
|
+
// sat noticeably higher than centered, a bigger gap below the text than
|
|
667
|
+
// above it. No w:before default exists to match, so the extra space was
|
|
668
|
+
// one-sided rather than at least being symmetric.
|
|
669
|
+
' <w:p>',
|
|
670
|
+
' <w:pPr><w:spacing w:before="0" w:after="0"/><w:jc w:val="center"/></w:pPr>',
|
|
671
|
+
renderLabelRuns(labelRuns, textColor, scaledFontSizeHalfPt(NODE_FONT_SIZE_HALFPT, scale)),
|
|
672
|
+
' </w:p>',
|
|
673
|
+
' </w:txbxContent>',
|
|
674
|
+
' </wps:txbx>',
|
|
675
|
+
bodyPr(scale),
|
|
676
|
+
' </wps:wsp>',
|
|
677
|
+
].join('\n');
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Render a label's rich-text body (`types.ts`'s `LabelToken[]` — bold/italic
|
|
681
|
+
* runs and `<br/>` line breaks, spec follow-up "rich-text runs") as the
|
|
682
|
+
* `w:r`/`w:br` children of one `w:p`, shared by {@link renderNode} and
|
|
683
|
+
* {@link renderEdgeLabel} (identical shape, only color/size differ). A break
|
|
684
|
+
* token is its own run holding a bare `w:br` — WordprocessingML requires the
|
|
685
|
+
* break element inside a run, but that run carries no `w:rPr`/`w:t` of its
|
|
686
|
+
* own — so a `<br/>` moves to a new line within this same paragraph instead
|
|
687
|
+
* of starting a new (differently-spaced) paragraph.
|
|
688
|
+
*/
|
|
689
|
+
function renderLabelRuns(tokens, color, sizeHalfPt) {
|
|
690
|
+
return tokens
|
|
691
|
+
.map((token) => {
|
|
692
|
+
if ('break' in token) {
|
|
693
|
+
return [' <w:r>', ' <w:br/>', ' </w:r>'].join('\n');
|
|
694
|
+
}
|
|
695
|
+
// CT_RPr is an ordered sequence (ECMA-376 §17.3.2.28): b, i, color, sz.
|
|
696
|
+
const rPr = [];
|
|
697
|
+
if (token.bold)
|
|
698
|
+
rPr.push('<w:b/>');
|
|
699
|
+
if (token.italic)
|
|
700
|
+
rPr.push('<w:i/>');
|
|
701
|
+
rPr.push(`<w:color w:val="${color}"/>`, `<w:sz w:val="${sizeHalfPt}"/>`);
|
|
702
|
+
return [
|
|
703
|
+
' <w:r>',
|
|
704
|
+
` <w:rPr>${rPr.join('')}</w:rPr>`,
|
|
705
|
+
` <w:t xml:space="preserve">${escapeXml(token.text)}</w:t>`,
|
|
706
|
+
' </w:r>',
|
|
707
|
+
].join('\n');
|
|
708
|
+
})
|
|
709
|
+
.join('\n');
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* The `wps:style` element Word emits for a connector, referencing the document
|
|
713
|
+
* theme. Unlike a shape, a connector has no fill (`fillRef idx="0"`) and uses
|
|
714
|
+
* the text-1 font (`fontRef val="tx1"`).
|
|
715
|
+
*/
|
|
716
|
+
function connectorStyle() {
|
|
717
|
+
return [
|
|
718
|
+
'<wps:style>',
|
|
719
|
+
' <a:lnRef idx="2"><a:schemeClr val="accent1"/></a:lnRef>',
|
|
720
|
+
' <a:fillRef idx="0"><a:schemeClr val="accent1"/></a:fillRef>',
|
|
721
|
+
' <a:effectRef idx="1"><a:schemeClr val="accent1"/></a:effectRef>',
|
|
722
|
+
' <a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef>',
|
|
723
|
+
'</wps:style>',
|
|
724
|
+
].join('\n');
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Connection-site indices for the built-in preset geometries used here (rect,
|
|
728
|
+
* roundRect, diamond, ellipse, can): 0=top, 1=left, 2=bottom, 3=right —
|
|
729
|
+
* counter-clockwise from the top, 0-based.
|
|
730
|
+
*
|
|
731
|
+
* Corrected 2026-09-02 (previously `{ top: 0, right: 1, bottom: 2, left: 3 }`,
|
|
732
|
+
* i.e. right/left swapped). The only empirical check this ever had
|
|
733
|
+
* (`tools/word-reference/`) used a *vertical* connector between two stacked
|
|
734
|
+
* rectangles, which only exercises idx 0 (top) and 2 (bottom) — the
|
|
735
|
+
* right/left half of the mapping was never actually tested, just assumed
|
|
736
|
+
* clockwise-from-top by symmetry. That assumption was wrong: decoded
|
|
737
|
+
* directly from LibreOffice's `oox-drawingml-cs-presets` (its own mirror of
|
|
738
|
+
* Microsoft's official `presetShapeDefinitions.xml`, per
|
|
739
|
+
* https://learn.microsoft.com/en-us/archive/blogs/openspecification/how-to-use-the-presetshapedefinitions-xml-file-and-fun-with-drawingml),
|
|
740
|
+
* both `rect` and `diamond`'s `GluePoints` (OOXML `cxnLst` equivalent) list
|
|
741
|
+
* connection sites in the order top(0)/left/(1)/bottom(2)/right(3) — the
|
|
742
|
+
* same for both shapes, so this isn't diamond-specific.
|
|
743
|
+
*
|
|
744
|
+
* This only affects the `idx` we *stamp* on `stCxn`/`endCxn` (the magnetic
|
|
745
|
+
* attachment Word uses to keep a connector attached to a shape when it's
|
|
746
|
+
* dragged, and — per user report — appears to influence initial rendering
|
|
747
|
+
* too); the connector's own drawn path (`bentConnectorGeometry`/
|
|
748
|
+
* `straightConnectorGeometry`) is a literal point list computed by
|
|
749
|
+
* `sitePoint()` below, unaffected by this constant and geometrically correct
|
|
750
|
+
* either way. That's why the old (wrong) mapping never produced a visibly
|
|
751
|
+
* broken connector in LibreOffice — LibreOffice draws the literal path we
|
|
752
|
+
* provide — but did in a real Word, which appears to prefer its own
|
|
753
|
+
* connection-site semantics over the static geometry for at least some
|
|
754
|
+
* shapes/paths. Not independently re-verified against a real Word document
|
|
755
|
+
* with a horizontal connector (`tools/word-reference/` only has the vertical
|
|
756
|
+
* case) — flagged in TODO.md.
|
|
757
|
+
*/
|
|
758
|
+
const SITE = { top: 0, right: 3, bottom: 2, left: 1 };
|
|
759
|
+
/** The point where a connector attaches to a box for a given connection site. */
|
|
760
|
+
function sitePoint(box, side) {
|
|
761
|
+
switch (side) {
|
|
762
|
+
case SITE.right:
|
|
763
|
+
return { x: box.x + box.width, y: box.y + box.height / 2 };
|
|
764
|
+
case SITE.bottom:
|
|
765
|
+
return { x: box.x + box.width / 2, y: box.y + box.height };
|
|
766
|
+
case SITE.left:
|
|
767
|
+
return { x: box.x, y: box.y + box.height / 2 };
|
|
768
|
+
case SITE.top:
|
|
769
|
+
default:
|
|
770
|
+
return { x: box.x + box.width / 2, y: box.y };
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Pick which side of each box a connector leaves/arrives from, based on the
|
|
775
|
+
* relative position of the two boxes (spec §6.2 magnetic connectors).
|
|
776
|
+
*/
|
|
777
|
+
function chooseSides(from, to) {
|
|
778
|
+
const dx = to.x + to.width / 2 - (from.x + from.width / 2);
|
|
779
|
+
const dy = to.y + to.height / 2 - (from.y + from.height / 2);
|
|
780
|
+
if (Math.abs(dy) >= Math.abs(dx)) {
|
|
781
|
+
return dy > 0 ? { stSide: SITE.bottom, endSide: SITE.top } : { stSide: SITE.top, endSide: SITE.bottom };
|
|
782
|
+
}
|
|
783
|
+
return dx > 0 ? { stSide: SITE.right, endSide: SITE.left } : { stSide: SITE.left, endSide: SITE.right };
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Which connection site a point sits closest to, by perpendicular distance
|
|
787
|
+
* to each of the box's 4 sides (extended as infinite lines, not clamped to
|
|
788
|
+
* the segment) — used for a self-loop's connection sites, where the point
|
|
789
|
+
* isn't the sitePoint()-computed midpoint of a side but one of Dagre's own
|
|
790
|
+
* loop-route endpoints (still on the box's perimeter, just not centered).
|
|
791
|
+
*/
|
|
792
|
+
function nearestSite(box, point) {
|
|
793
|
+
const distances = [
|
|
794
|
+
[SITE.top, Math.abs(point.y - box.y)],
|
|
795
|
+
[SITE.bottom, Math.abs(point.y - (box.y + box.height))],
|
|
796
|
+
[SITE.left, Math.abs(point.x - box.x)],
|
|
797
|
+
[SITE.right, Math.abs(point.x - (box.x + box.width))],
|
|
798
|
+
];
|
|
799
|
+
distances.sort((a, b) => a[1] - b[1]);
|
|
800
|
+
return distances[0][0];
|
|
801
|
+
}
|
|
802
|
+
/** Whether point (x,y) falls within (or on the boundary of) a box. */
|
|
803
|
+
function pointInBox(x, y, box) {
|
|
804
|
+
return x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
805
|
+
}
|
|
806
|
+
/** Whether segments (a1,a2) and (b1,b2) cross (standard orientation test). */
|
|
807
|
+
function segmentsIntersect(a1, a2, b1, b2) {
|
|
808
|
+
const ccw = (p, q, r) => (r.y - p.y) * (q.x - p.x) > (q.y - p.y) * (r.x - p.x);
|
|
809
|
+
return ccw(a1, b1, b2) !== ccw(a2, b1, b2) && ccw(a1, a2, b1) !== ccw(a1, a2, b2);
|
|
810
|
+
}
|
|
811
|
+
/** Whether the segment (x1,y1)-(x2,y2) passes through the given box at all. */
|
|
812
|
+
function segmentIntersectsBox(x1, y1, x2, y2, box) {
|
|
813
|
+
if (pointInBox(x1, y1, box) || pointInBox(x2, y2, box))
|
|
814
|
+
return true;
|
|
815
|
+
const p1 = { x: x1, y: y1 };
|
|
816
|
+
const p2 = { x: x2, y: y2 };
|
|
817
|
+
const { x: left, y: top } = box;
|
|
818
|
+
const right = box.x + box.width;
|
|
819
|
+
const bottom = box.y + box.height;
|
|
820
|
+
const corners = [
|
|
821
|
+
{ x: left, y: top },
|
|
822
|
+
{ x: right, y: top },
|
|
823
|
+
{ x: right, y: bottom },
|
|
824
|
+
{ x: left, y: bottom },
|
|
825
|
+
];
|
|
826
|
+
for (let i = 0; i < 4; i++) {
|
|
827
|
+
if (segmentsIntersect(p1, p2, corners[i], corners[(i + 1) % 4]))
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
return false;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Compute where a connector between two boxes actually starts and ends, i.e.
|
|
834
|
+
* on the boxes' perimeter (the connection site), not their centers — the
|
|
835
|
+
* `a:xfrm` off/ext must match what the connector visually is, or Word draws a
|
|
836
|
+
* line straight through the shape interiors instead of stopping at their edges.
|
|
837
|
+
*
|
|
838
|
+
* `dagrePoints` is the route Dagre computed for this edge (`LayoutResult.edges`)
|
|
839
|
+
* and `otherBoxes` is every OTHER node's box (not this edge's own source/
|
|
840
|
+
* target). The straight line between the two connection sites is used as-is
|
|
841
|
+
* UNLESS it actually passes through one of `otherBoxes` — only then does the
|
|
842
|
+
* connector bend, reusing Dagre's own interior waypoints as the detour.
|
|
843
|
+
*
|
|
844
|
+
* This is deliberately a real geometric test rather than trusting Dagre's
|
|
845
|
+
* point count as a proxy for "needs routing": Dagre hands back more than the
|
|
846
|
+
* usual 3 points (a start, one stylistic mid-rank point, and an end — even
|
|
847
|
+
* for two directly-adjacent, unobstructed nodes) for an edge that crosses a
|
|
848
|
+
* *subgraph cluster* boundary too, not only for one that skips a rank. Empty
|
|
849
|
+
* `otherBoxes`, or a straight line that clears everything, means the simple
|
|
850
|
+
* two-point path — matching what this translator always emitted before edge
|
|
851
|
+
* routing existed, with no dependency on Dagre's point count at all.
|
|
852
|
+
*/
|
|
853
|
+
function connectorGeometry(from, to, dagrePoints, otherBoxes) {
|
|
854
|
+
// A self-loop (`A --> A`): `from`/`to` are the exact same Box object here
|
|
855
|
+
// (both looked up as `layout.nodes[edge.from]`/`layout.nodes[edge.to]` from
|
|
856
|
+
// the same id in renderContent, never cloned) — a reliable proxy for
|
|
857
|
+
// "same node" without threading the Mermaid id through this function too.
|
|
858
|
+
// chooseSides()/sitePoint() below assume two *different* boxes and pick a
|
|
859
|
+
// side from their relative position; for identical boxes that degenerates
|
|
860
|
+
// to dx=dy=0, always resolving to "top -> bottom" — a straight line
|
|
861
|
+
// through the node's own interior, not a loop, found by real-render audit
|
|
862
|
+
// (2026-09-04, punch list item 5). Dagre already computes a real loop
|
|
863
|
+
// bulging away from the node for a self-edge (verified across TD/LR/BT/RL
|
|
864
|
+
// -- the bulge direction rotates with rankdir, but both ends always land
|
|
865
|
+
// on the node's own perimeter), so a self-loop uses those points verbatim
|
|
866
|
+
// instead of ever calling chooseSides()/sitePoint().
|
|
867
|
+
if (from === to) {
|
|
868
|
+
const start = dagrePoints[0];
|
|
869
|
+
const end = dagrePoints[dagrePoints.length - 1];
|
|
870
|
+
return {
|
|
871
|
+
stSide: nearestSite(from, start),
|
|
872
|
+
endSide: nearestSite(to, end),
|
|
873
|
+
points: dagrePoints.map((p) => ({ x: p.x, y: p.y })),
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
const { stSide, endSide } = chooseSides(from, to);
|
|
877
|
+
const start = sitePoint(from, stSide);
|
|
878
|
+
const end = sitePoint(to, endSide);
|
|
879
|
+
const rawWaypoints = dagrePoints.length > 2 ? dagrePoints.slice(1, -1) : [];
|
|
880
|
+
const needsRouting = rawWaypoints.length > 0 &&
|
|
881
|
+
otherBoxes.some((box) => segmentIntersectsBox(start.x, start.y, end.x, end.y, box));
|
|
882
|
+
const waypoints = needsRouting ? rawWaypoints : [];
|
|
883
|
+
return { stSide, endSide, points: [start, ...waypoints, end] };
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* The point at a given fraction (0..1) of the way along a polyline's total
|
|
887
|
+
* length — used to place an edge label on a bent connector's actual path
|
|
888
|
+
* instead of the straight-line midpoint between its two ends, which could
|
|
889
|
+
* land on whatever the connector is routed around.
|
|
890
|
+
*/
|
|
891
|
+
function pointAlongPath(points, fraction) {
|
|
892
|
+
const segments = [];
|
|
893
|
+
let total = 0;
|
|
894
|
+
for (let i = 1; i < points.length; i++) {
|
|
895
|
+
const dx = points[i].x - points[i - 1].x;
|
|
896
|
+
const dy = points[i].y - points[i - 1].y;
|
|
897
|
+
const length = Math.hypot(dx, dy);
|
|
898
|
+
segments.push(length);
|
|
899
|
+
total += length;
|
|
900
|
+
}
|
|
901
|
+
if (total === 0)
|
|
902
|
+
return points[0];
|
|
903
|
+
let target = total * fraction;
|
|
904
|
+
for (let i = 0; i < segments.length; i++) {
|
|
905
|
+
const length = segments[i];
|
|
906
|
+
if (target <= length || i === segments.length - 1) {
|
|
907
|
+
const t = length === 0 ? 0 : target / length;
|
|
908
|
+
const a = points[i];
|
|
909
|
+
const b = points[i + 1];
|
|
910
|
+
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
911
|
+
}
|
|
912
|
+
target -= length;
|
|
913
|
+
}
|
|
914
|
+
return points[points.length - 1];
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* The `a:prstGeom`/`a:xfrm` for a straight two-point connector — the common
|
|
918
|
+
* case, byte-identical to what this translator emitted before edge routing
|
|
919
|
+
* existed.
|
|
920
|
+
*/
|
|
921
|
+
function straightConnectorGeometry(startX, startY, endX, endY) {
|
|
922
|
+
// `a:xfrm` is a top-left box, so a connector running right-to-left or
|
|
923
|
+
// bottom-to-top has to be flipped for the arrow head to land on the target.
|
|
924
|
+
const flipH = endX < startX ? ' flipH="1"' : '';
|
|
925
|
+
const flipV = endY < startY ? ' flipV="1"' : '';
|
|
926
|
+
return [
|
|
927
|
+
` <a:xfrm${flipH}${flipV}>`,
|
|
928
|
+
` <a:off x="${Math.min(startX, endX)}" y="${Math.min(startY, endY)}"/>`,
|
|
929
|
+
` <a:ext cx="${Math.abs(endX - startX)}" cy="${Math.abs(endY - startY)}"/>`,
|
|
930
|
+
' </a:xfrm>',
|
|
931
|
+
' <a:prstGeom prst="line">',
|
|
932
|
+
' <a:avLst/>',
|
|
933
|
+
' </a:prstGeom>',
|
|
934
|
+
].join('\n');
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* The `a:custGeom`/`a:xfrm` for a connector Dagre routed around an
|
|
938
|
+
* intermediate rank's nodes (spec §9 "0 croisement de flèches") — an explicit
|
|
939
|
+
* `moveTo`/`lnTo*` path through every waypoint, rather than one of Word's
|
|
940
|
+
* built-in `bentConnectorN`/`curvedConnectorN` presets: those are parametrized
|
|
941
|
+
* by a handful of `adj` guide values with no documented public formula for
|
|
942
|
+
* "here are N arbitrary points, produce the adj values that trace them", so
|
|
943
|
+
* reproducing Dagre's routing through them would mean reverse-engineering
|
|
944
|
+
* Word's own connector-routing heuristic. A custom path draws exactly the
|
|
945
|
+
* route Dagre already computed, no guessing involved. `wps:cNvCnPr` still
|
|
946
|
+
* carries `stCxn`/`endCxn` for the magnetic-attachment behaviour (spec §6.2)
|
|
947
|
+
* — that comes from being declared a connection shape, not from the geometry.
|
|
948
|
+
*
|
|
949
|
+
* The path's own coordinate space is set to exactly the shape's EMU extent
|
|
950
|
+
* (`w`/`h` on `a:path` equal to `a:ext`'s `cx`/`cy`), so every path point can
|
|
951
|
+
* be a plain EMU offset from the bounding box's top-left with no extra scale
|
|
952
|
+
* factor to track.
|
|
953
|
+
*/
|
|
954
|
+
function bentConnectorGeometry(points) {
|
|
955
|
+
const xs = points.map((p) => p.x);
|
|
956
|
+
const ys = points.map((p) => p.y);
|
|
957
|
+
const minX = Math.min(...xs);
|
|
958
|
+
const minY = Math.min(...ys);
|
|
959
|
+
const w = Math.max(1, Math.max(...xs) - minX);
|
|
960
|
+
const h = Math.max(1, Math.max(...ys) - minY);
|
|
961
|
+
const [first, ...rest] = points;
|
|
962
|
+
return [
|
|
963
|
+
` <a:xfrm>`,
|
|
964
|
+
` <a:off x="${minX}" y="${minY}"/>`,
|
|
965
|
+
` <a:ext cx="${w}" cy="${h}"/>`,
|
|
966
|
+
' </a:xfrm>',
|
|
967
|
+
' <a:custGeom>',
|
|
968
|
+
' <a:avLst/>',
|
|
969
|
+
' <a:gdLst/>',
|
|
970
|
+
' <a:ahLst/>',
|
|
971
|
+
' <a:cxnLst/>',
|
|
972
|
+
' <a:rect l="0" t="0" r="0" b="0"/>',
|
|
973
|
+
' <a:pathLst>',
|
|
974
|
+
` <a:path w="${w}" h="${h}">`,
|
|
975
|
+
` <a:moveTo><a:pt x="${first.x - minX}" y="${first.y - minY}"/></a:moveTo>`,
|
|
976
|
+
...rest.map((p) => ` <a:lnTo><a:pt x="${p.x - minX}" y="${p.y - minY}"/></a:lnTo>`),
|
|
977
|
+
' </a:path>',
|
|
978
|
+
' </a:pathLst>',
|
|
979
|
+
' </a:custGeom>',
|
|
980
|
+
].join('\n');
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Render an edge as a `wps:wsp` connector (with `wps:cNvCnPr`), following the
|
|
984
|
+
* route Dagre computed (`dagrePoints`, from `LayoutResult.edges`) when the
|
|
985
|
+
* straight line between the two connection sites would actually cross
|
|
986
|
+
* another node — see {@link connectorGeometry}.
|
|
987
|
+
*
|
|
988
|
+
* `cNvPr/name` is set to `"{mermaidFromId}--{mermaidToId}"` rather than a
|
|
989
|
+
* generic label (spec follow-up, `docs/specs/FUTURE_docx2mermaid_SPEC.md` §4) — no
|
|
990
|
+
* downside here, unlike node `name`: connectors don't currently carry a
|
|
991
|
+
* friendlier alternative Word would otherwise show in its Selection Pane.
|
|
992
|
+
*
|
|
993
|
+
* A self-loop (`A --> A`) is declared `wps:cNvSpPr` (a plain shape) instead
|
|
994
|
+
* of `wps:cNvCnPr` (a connector), even though its geometry (custom, from
|
|
995
|
+
* {@link connectorGeometry}'s dedicated self-loop branch) is identical
|
|
996
|
+
* either way — found by real-render audit (2026-09-04, punch list item 5):
|
|
997
|
+
* LibreOffice **discards** a `wps:cNvCnPr` shape's own `a:custGeom`/`a:xfrm`
|
|
998
|
+
* entirely and substitutes its own computed path once `stCxn`/`endCxn` name
|
|
999
|
+
* the *same* shape id, whether at the same connection index (collapses to
|
|
1000
|
+
* an invisible zero-length line — the original bug) or two different ones
|
|
1001
|
+
* (a visible but unrelated auto-routed shape, ignoring the real Dagre loop
|
|
1002
|
+
* geometry). A plain shape's `a:custGeom` is never second-guessed this way
|
|
1003
|
+
* — same rendering path already trusted for a routed edge between two
|
|
1004
|
+
* *different* shapes (`bentConnectorGeometry`'s own doc comment) — so a
|
|
1005
|
+
* self-loop uses it too, verified end to end against a real LibreOffice
|
|
1006
|
+
* render (the "audit" — no prior attempt existed to compare against). The
|
|
1007
|
+
* accepted tradeoff: a self-loop won't visually follow its node if the node
|
|
1008
|
+
* is later dragged in Word (no real `stCxn`/`endCxn` magnetic attachment,
|
|
1009
|
+
* since there is nothing meaningfully different to attach the two ends
|
|
1010
|
+
* *to* on the very same shape) — far better than the alternatives above.
|
|
1011
|
+
*/
|
|
1012
|
+
function renderEdge(fromId, toId, mermaidFromId, mermaidToId, type, from, to, dagrePoints, otherBoxes, line, strokeWidthPx, nextId, scale) {
|
|
1013
|
+
const lineStyle = LINE_STYLE_BY_EDGE[type] ?? LINE_STYLE_BY_EDGE.arrow;
|
|
1014
|
+
// `linkStyle N stroke-width:Npx` overrides the type's default line weight
|
|
1015
|
+
// (spec §6.3); px -> EMU the same way every other coordinate in the AST is
|
|
1016
|
+
// converted, not pre-scaled here since `scaledLineWidth` below applies the
|
|
1017
|
+
// diagram's overall `scale` uniformly to whichever width wins.
|
|
1018
|
+
const baseWidthEmu = strokeWidthPx !== undefined ? Math.max(1, Math.round(strokeWidthPx * EMU_PER_PX)) : lineStyle.width;
|
|
1019
|
+
const { stSide, endSide, points } = connectorGeometry(from, to, dagrePoints, otherBoxes);
|
|
1020
|
+
const emuPoints = points.map((p) => ({
|
|
1021
|
+
x: Math.round(p.x * EMU_PER_PX * scale),
|
|
1022
|
+
y: Math.round(p.y * EMU_PER_PX * scale),
|
|
1023
|
+
}));
|
|
1024
|
+
const geometry = emuPoints.length === 2
|
|
1025
|
+
? straightConnectorGeometry(emuPoints[0].x, emuPoints[0].y, emuPoints[1].x, emuPoints[1].y)
|
|
1026
|
+
: bentConnectorGeometry(emuPoints);
|
|
1027
|
+
const markerSize = arrowMarkerSize(baseWidthEmu, scale);
|
|
1028
|
+
const headEnd = lineStyle.headEnd !== 'none'
|
|
1029
|
+
? ` <a:headEnd type="${lineStyle.headEnd}" w="${markerSize}" len="${markerSize}"/>`
|
|
1030
|
+
: '';
|
|
1031
|
+
const tailEnd = lineStyle.tailEnd !== 'none'
|
|
1032
|
+
? ` <a:tailEnd type="${lineStyle.tailEnd}" w="${markerSize}" len="${markerSize}"/>`
|
|
1033
|
+
: '';
|
|
1034
|
+
const safeName = escapeXml(`${mermaidFromId}--${mermaidToId}`);
|
|
1035
|
+
const isSelfLoop = fromId === toId;
|
|
1036
|
+
const connectionProps = isSelfLoop
|
|
1037
|
+
? ' <wps:cNvSpPr/>'
|
|
1038
|
+
: [
|
|
1039
|
+
' <wps:cNvCnPr>',
|
|
1040
|
+
` <a:stCxn id="${fromId}" idx="${stSide}"/>`,
|
|
1041
|
+
` <a:endCxn id="${toId}" idx="${endSide}"/>`,
|
|
1042
|
+
' </wps:cNvCnPr>',
|
|
1043
|
+
].join('\n');
|
|
1044
|
+
return [
|
|
1045
|
+
' <wps:wsp>',
|
|
1046
|
+
` <wps:cNvPr id="${nextId()}" name="${safeName}"/>`,
|
|
1047
|
+
connectionProps,
|
|
1048
|
+
' <wps:spPr>',
|
|
1049
|
+
geometry,
|
|
1050
|
+
` <a:ln w="${scaledLineWidth(baseWidthEmu, scale)}" cap="flat" cmpd="sng" algn="ctr">`,
|
|
1051
|
+
lineStyle.invisible
|
|
1052
|
+
? ' <a:noFill/>'
|
|
1053
|
+
: [
|
|
1054
|
+
` <a:solidFill><a:srgbClr val="${line}"/></a:solidFill>`,
|
|
1055
|
+
` <a:prstDash val="${lineStyle.dash}"/>`,
|
|
1056
|
+
].join('\n'),
|
|
1057
|
+
...(lineStyle.invisible ? [] : [headEnd, tailEnd].filter(Boolean)),
|
|
1058
|
+
' </a:ln>',
|
|
1059
|
+
' </wps:spPr>',
|
|
1060
|
+
connectorStyle(),
|
|
1061
|
+
' <wps:bodyPr/>',
|
|
1062
|
+
' </wps:wsp>',
|
|
1063
|
+
].join('\n');
|
|
1064
|
+
}
|
|
1065
|
+
/** Height in EMU of the transparent box holding an edge label (0.25in). */
|
|
1066
|
+
const EDGE_LABEL_CY = 228600;
|
|
1067
|
+
/** Point size of edge-label text (`w:sz`, half-points, so 16 = 8pt), in px
|
|
1068
|
+
* at 96 DPI — needed to size the box to its own text (see `edgeLabelWidth`),
|
|
1069
|
+
* distinct from `FONT_SIZE_PX` (node/subgraph-title text's larger effective
|
|
1070
|
+
* size, `layout.ts`). */
|
|
1071
|
+
const EDGE_LABEL_FONT_SIZE_PX = (8 * 96) / 72;
|
|
1072
|
+
/** Horizontal padding (px, each side) between an edge label's text and its
|
|
1073
|
+
* box edge — mirrors `bodyPr`'s `lIns`/`rIns` below, applied twice (once
|
|
1074
|
+
* baked into the box width, once as the inset) so a same-size rendering
|
|
1075
|
+
* error in `estimateTextWidth` (an approximation, no real font metrics —
|
|
1076
|
+
* see `layout.ts`'s `nodeDimensions` doc comment for why) still leaves the
|
|
1077
|
+
* text clear of the edge instead of exactly touching it. */
|
|
1078
|
+
const EDGE_LABEL_PAD_X_PX = 8;
|
|
1079
|
+
/**
|
|
1080
|
+
* Safety multiplier applied to `estimateTextWidth`'s raw estimate before
|
|
1081
|
+
* sizing an edge-label box. Found empirically (2026-09-02): `layout.ts`'s
|
|
1082
|
+
* per-character em table was calibrated against `FONT_SIZE_PX` (16px, node
|
|
1083
|
+
* text); scaled linearly down to `EDGE_LABEL_FONT_SIZE_PX` (~10.7px, an 8pt
|
|
1084
|
+
* edge-label caption), it undershot badly — bisecting the real
|
|
1085
|
+
* non-wrapping width for "Non" in LibreOffice landed around 42px total box
|
|
1086
|
+
* width (34px of interior text room after insets), while the unscaled
|
|
1087
|
+
* estimate for the same word predicted only ~20px. Small serif glyphs don't
|
|
1088
|
+
* shrink as fast as their point size at this scale, an effect the
|
|
1089
|
+
* single-size-calibrated table doesn't capture. 1.8x tracks the ~1.7x gap
|
|
1090
|
+
* observed, with a little room to spare.
|
|
1091
|
+
*/
|
|
1092
|
+
const EDGE_LABEL_TEXT_SAFETY_MARGIN = 1.8;
|
|
1093
|
+
/** Floor under a computed edge-label width, so a one-character label (e.g.
|
|
1094
|
+
* a bare "?") doesn't get a box narrower than is comfortable to read. */
|
|
1095
|
+
const MIN_EDGE_LABEL_WIDTH_PX = 44;
|
|
1096
|
+
/**
|
|
1097
|
+
* An edge label's box width in EMU, sized to its own text instead of a fixed
|
|
1098
|
+
* constant — a fixed-width box (previously 0.75in for every label
|
|
1099
|
+
* regardless of content) was both too narrow for long captions, which then
|
|
1100
|
+
* overflowed past its edges (`wrap="none"`, no horizontal insets), and too
|
|
1101
|
+
* *wide* for short ones ("Oui", "Non"): centering an oversized box on a
|
|
1102
|
+
* diagonal connector's midpoint only sits the true center pixel on the
|
|
1103
|
+
* line, not the text itself, since the diagonal only crosses the box's
|
|
1104
|
+
* vertical middle at one point along its width — the wider the box, the more
|
|
1105
|
+
* the visible text departs from that single point, reading as the label not
|
|
1106
|
+
* being centered on the arrow even though the box's geometric center is
|
|
1107
|
+
* exact. Right-sizing the box makes that gap negligible.
|
|
1108
|
+
*/
|
|
1109
|
+
function edgeLabelWidthEmu(labelRuns) {
|
|
1110
|
+
// A `<br/>` (rare in an edge label, but not disallowed) sizes the box to
|
|
1111
|
+
// its widest line, not the sum of all lines — same per-line-then-combine
|
|
1112
|
+
// shape as layout.ts's nodeDimensions, mirrored here since edge labels are
|
|
1113
|
+
// sized independently of that function.
|
|
1114
|
+
const widestLinePx = Math.max(...labelLines(labelRuns).map((line) => estimateTextWidth(line, EDGE_LABEL_FONT_SIZE_PX)));
|
|
1115
|
+
const textWidthPx = widestLinePx * EDGE_LABEL_TEXT_SAFETY_MARGIN;
|
|
1116
|
+
const widthPx = Math.max(MIN_EDGE_LABEL_WIDTH_PX, textWidthPx + 2 * EDGE_LABEL_PAD_X_PX);
|
|
1117
|
+
return Math.round(widthPx * EMU_PER_PX);
|
|
1118
|
+
}
|
|
1119
|
+
/** An edge label's box height in EMU: `EDGE_LABEL_CY` per line, growing for
|
|
1120
|
+
* a `<br/>`-forced multi-line label instead of clipping every line past the
|
|
1121
|
+
* first (the box was previously a single fixed height regardless of content,
|
|
1122
|
+
* back when no label could ever have more than one line). */
|
|
1123
|
+
function edgeLabelHeightEmu(labelRuns) {
|
|
1124
|
+
return EDGE_LABEL_CY * labelLines(labelRuns).length;
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Render an edge label (`-->|Texte|`) as a borderless, fill-free `wps:txbx`
|
|
1128
|
+
* centered on the midpoint of the connector's actual path (spec §6.2), not the
|
|
1129
|
+
* midpoint between the two node centers — a node sitting between the source
|
|
1130
|
+
* and target (a common case: a decision node's branches fan out past a third
|
|
1131
|
+
* node) would otherwise get the label superimposed on it. It is a sibling
|
|
1132
|
+
* shape rather than text on the connector itself: Word renders connector-owned
|
|
1133
|
+
* text along the line, which is unreadable for a diagram caption.
|
|
1134
|
+
*/
|
|
1135
|
+
function renderEdgeLabel(labelRuns, from, to, dagrePoints, otherBoxes, id, scale) {
|
|
1136
|
+
const { points } = connectorGeometry(from, to, dagrePoints, otherBoxes);
|
|
1137
|
+
// The midpoint by arc length along the real (possibly bent) path, not the
|
|
1138
|
+
// straight line between the two ends — for a routed edge, that straight
|
|
1139
|
+
// line is exactly the segment the routing was drawn to avoid.
|
|
1140
|
+
const mid = pointAlongPath(points, 0.5);
|
|
1141
|
+
const cx = Math.round(edgeLabelWidthEmu(labelRuns) * scale);
|
|
1142
|
+
const cy = Math.round(edgeLabelHeightEmu(labelRuns) * scale);
|
|
1143
|
+
const x = Math.round(mid.x * EMU_PER_PX * scale - cx / 2);
|
|
1144
|
+
const y = Math.round(mid.y * EMU_PER_PX * scale - cy / 2);
|
|
1145
|
+
const insetEmu = Math.round((EDGE_LABEL_PAD_X_PX / 2) * EMU_PER_PX * scale);
|
|
1146
|
+
return [
|
|
1147
|
+
' <wps:wsp>',
|
|
1148
|
+
` <wps:cNvPr id="${id}" name="EdgeLabel"/>`,
|
|
1149
|
+
' <wps:cNvSpPr txBox="1"/>',
|
|
1150
|
+
' <wps:spPr>',
|
|
1151
|
+
' <a:xfrm>',
|
|
1152
|
+
` <a:off x="${x}" y="${y}"/>`,
|
|
1153
|
+
` <a:ext cx="${cx}" cy="${cy}"/>`,
|
|
1154
|
+
' </a:xfrm>',
|
|
1155
|
+
' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>',
|
|
1156
|
+
' <a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>',
|
|
1157
|
+
' <a:ln><a:noFill/></a:ln>',
|
|
1158
|
+
' </wps:spPr>',
|
|
1159
|
+
' <wps:txbx>',
|
|
1160
|
+
' <w:txbxContent>',
|
|
1161
|
+
' <w:p>',
|
|
1162
|
+
' <w:pPr><w:spacing w:before="0" w:after="0"/><w:jc w:val="center"/></w:pPr>',
|
|
1163
|
+
renderLabelRuns(labelRuns, '000000', scaledFontSizeHalfPt(16, scale)),
|
|
1164
|
+
' </w:p>',
|
|
1165
|
+
' </w:txbxContent>',
|
|
1166
|
+
' </wps:txbx>',
|
|
1167
|
+
` <wps:bodyPr rot="0" vert="horz" wrap="none" lIns="${insetEmu}" tIns="0" rIns="${insetEmu}" bIns="0"`,
|
|
1168
|
+
' anchor="ctr" anchorCtr="1"><a:noAutofit/></wps:bodyPr>',
|
|
1169
|
+
' </wps:wsp>',
|
|
1170
|
+
].join('\n');
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Compute the total bounding box of a layout, in pixels.
|
|
1174
|
+
*
|
|
1175
|
+
* Subgraph containers are included: a cluster box can extend past the nodes it
|
|
1176
|
+
* holds, and leaving it out of the extent makes Word clip the cluster.
|
|
1177
|
+
*/
|
|
1178
|
+
function computeBoundingBox(layout) {
|
|
1179
|
+
let minX = Infinity;
|
|
1180
|
+
let minY = Infinity;
|
|
1181
|
+
let maxX = -Infinity;
|
|
1182
|
+
let maxY = -Infinity;
|
|
1183
|
+
const boxes = [
|
|
1184
|
+
...Object.values(layout.nodes),
|
|
1185
|
+
...Object.values(layout.subgraphs),
|
|
1186
|
+
];
|
|
1187
|
+
for (const box of boxes) {
|
|
1188
|
+
minX = Math.min(minX, box.x);
|
|
1189
|
+
minY = Math.min(minY, box.y);
|
|
1190
|
+
maxX = Math.max(maxX, box.x + box.width);
|
|
1191
|
+
maxY = Math.max(maxY, box.y + box.height);
|
|
1192
|
+
}
|
|
1193
|
+
// An edge route can extend past every node/subgraph box: found via a
|
|
1194
|
+
// self-loop audit (2026-09-04, punch list item 5) — a self-loop's route
|
|
1195
|
+
// (connectorGeometry's doc comment) always bulges out past its own node's
|
|
1196
|
+
// perimeter, so the diagram's declared canvas size (this function) has to
|
|
1197
|
+
// include it too, or the loop is clipped by the canvas frame even though
|
|
1198
|
+
// its own XML geometry is entirely correct. The same could in principle
|
|
1199
|
+
// happen for a normal edge routed wide around an obstacle, just less
|
|
1200
|
+
// reliably than a self-loop's guaranteed bulge — included here for both.
|
|
1201
|
+
for (const points of layout.edges) {
|
|
1202
|
+
for (const point of points) {
|
|
1203
|
+
minX = Math.min(minX, point.x);
|
|
1204
|
+
minY = Math.min(minY, point.y);
|
|
1205
|
+
maxX = Math.max(maxX, point.x);
|
|
1206
|
+
maxY = Math.max(maxY, point.y);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
if (minX === Infinity)
|
|
1210
|
+
return { width: 0, height: 0 };
|
|
1211
|
+
return { width: maxX - minX, height: maxY - minY };
|
|
1212
|
+
}
|
|
1213
|
+
//# sourceMappingURL=ooxml-translator.js.map
|