/
renderast.ts
1253 lines (1146 loc) · 33.9 KB
/
renderast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cloneDeep from "lodash/cloneDeep";
import type {
INormalizedRenderOptions,
RegularArcTextVerticalAlignmentType,
} from "../../../types/mscgen";
import type {
ISequenceChart,
IOptionsNormalized,
ArcKindNormalizedType,
} from "../../parse/mscgenjsast";
import aggregatekind from "../astmassage/aggregatekind";
import {
flatten,
type IEntityNormalized,
type IFlatArc,
type IFlatSequenceChart,
} from "../astmassage/flatten";
import constants from "./constants";
import { IOandD, Thing } from "./entities";
import * as idmanager from "./idmanager";
import * as kind2class from "./kind2class";
import * as markermanager from "./markermanager";
import * as renderlabels from "./renderlabels";
import * as renderskeleton from "./renderskeleton";
import * as renderutensils from "./renderutensils";
import * as rowmemory from "./rowmemory";
import * as svgelementfactory from "./svgelementfactory/index";
import * as svgutensils from "./svgutensils";
let entities = new Thing(0);
//#region type declarations
interface IChartLayers {
lifeline: SVGGElement;
sequence: SVGGElement;
notes: SVGGElement;
inline: SVGGElement;
watermark: SVGGElement;
}
interface IChart {
arcRowHeight: number;
arcGradient: number;
arcEndX: number;
wordWrapArcs: boolean;
mirrorEntitiesOnBottom: boolean;
regularArcTextVerticalAlignment: RegularArcTextVerticalAlignmentType;
maxDepth: number;
document: Document;
layers: IChartLayers;
}
interface ICanvas {
x: number;
y: number;
width: number;
height: number;
horizontaltransform: number;
autoscale: boolean;
verticaltransform: number;
scale: number;
}
//#endregion
//#region const
const PAD_VERTICAL = 3;
const DEFAULT_ARCROW_HEIGHT = 38; // chart only
const DEFAULT_ARC_GRADIENT = 0; // chart only
//#endregion
//#region global variables
/* sensible default - get overwritten in bootstrap */
const gChart: IChart = Object.seal({
arcRowHeight: DEFAULT_ARCROW_HEIGHT,
arcGradient: DEFAULT_ARC_GRADIENT,
arcEndX: 0,
wordWrapArcs: false,
mirrorEntitiesOnBottom: false,
regularArcTextVerticalAlignment:
"middle" as RegularArcTextVerticalAlignmentType,
maxDepth: 0,
document: {} as Document,
layers: {
lifeline: {} as SVGGElement,
sequence: {} as SVGGElement,
notes: {} as SVGGElement,
inline: {} as SVGGElement,
watermark: {} as SVGGElement,
},
});
let gInlineExpressionMemory: any[] = [];
//#endregion
function getParentElement(
pWindow: Window,
pParentElementId: string
): HTMLElement {
return (
pWindow.document.getElementById(pParentElementId) || pWindow.document.body
);
}
//#region render level 0 & 1
function renderASTPre(
pAST: IFlatSequenceChart,
pWindow: Window,
pParentElement: HTMLElement,
pOptions: INormalizedRenderOptions
) {
gChart.document = renderskeleton.bootstrap(
pWindow,
pParentElement,
idmanager.get(),
markermanager.getMarkerDefs(idmanager.get(), pAST),
pOptions
);
gChart.mirrorEntitiesOnBottom = pOptions.mirrorEntitiesOnBottom;
gChart.regularArcTextVerticalAlignment =
pOptions.regularArcTextVerticalAlignment;
svgutensils.init(gChart.document);
gChart.layers = createLayerShortcuts(gChart.document);
gChart.maxDepth = pAST.depth;
preProcessOptions(gChart, pAST.options);
}
function renderASTMain(pAST: IFlatSequenceChart) {
renderEntities(pAST.entities, 0, pAST.options);
rowmemory.clear(entities.getDims().height, gChart.arcRowHeight);
renderArcRows(pAST.arcs, pAST.entities, pAST.options);
if (gChart.mirrorEntitiesOnBottom) {
renderEntitiesOnBottom(pAST.entities, pAST.options);
}
}
function renderASTPost(pAST: IFlatSequenceChart) {
let lCanvas: ICanvas = calculateCanvasDimensions(pAST);
/* canvg ignores the background-color on svg level and makes the background
* transparent in stead. To work around this insert a white rectangle the size
* of the canvas in the background layer.
*
* We do this _before_ scaling is applied to the svg
*/
renderBackground(lCanvas);
lCanvas = postProcessOptions(pAST.options, lCanvas);
renderSvgElement(lCanvas);
}
//#endregion
function createLayerShortcuts(pDocument) {
return {
lifeline: pDocument.getElementById(idmanager.get("_lifelines")),
sequence: pDocument.getElementById(idmanager.get("_sequence")),
notes: pDocument.getElementById(idmanager.get("_notes")),
inline: pDocument.getElementById(idmanager.get("_arcspans")),
watermark: pDocument.getElementById(idmanager.get("_watermark")),
};
}
function preProcessOptionsArcs(pChart: IChart, pOptions: IOptionsNormalized) {
pChart.arcRowHeight = DEFAULT_ARCROW_HEIGHT;
pChart.arcGradient = DEFAULT_ARC_GRADIENT;
pChart.wordWrapArcs = false;
if (pOptions.arcgradient) {
pChart.arcRowHeight =
parseInt(pOptions.arcgradient, 10) + DEFAULT_ARCROW_HEIGHT;
pChart.arcGradient =
parseInt(pOptions.arcgradient, 10) + DEFAULT_ARC_GRADIENT;
}
pChart.wordWrapArcs = Boolean(pOptions.wordwraparcs);
}
/**
* preProcessOptions() -
* - resets the global variables governing entity width and height,
* row height to their default values
* - modifies them if passed
* - hscale (influences the entity width and inter entity spacing defaults)
* - arcgradient (influences the arc row height, sets the global arc gradient)
* - wordwraparcs (sets the wordwraparcs global)
*
* Note that width is not processed here as this can only be done
* reliably after most rendering calculations have been executed.
*
* @param <object> - pOptions - the option part of the AST
*/
function preProcessOptions(pChart: IChart, pOptions: IOptionsNormalized) {
entities = new Thing(pOptions && pOptions.hscale);
preProcessOptionsArcs(pChart, pOptions);
}
function calculateCanvasDimensions(pAST: IFlatSequenceChart): ICanvas {
const lDepthCorrection = renderutensils.determineDepthCorrection(
pAST.depth,
constants.LINE_WIDTH
);
const lRowInfo = rowmemory.getLast();
const lCanvas = {
width:
pAST.entities.length * entities.getDims().interEntitySpacing +
lDepthCorrection,
height: Boolean(gChart.mirrorEntitiesOnBottom)
? 2 * entities.getDims().height +
lRowInfo.y +
lRowInfo.height +
2 * PAD_VERTICAL
: lRowInfo.y + lRowInfo.height / 2 + 2 * PAD_VERTICAL,
horizontaltransform:
(entities.getDims().interEntitySpacing +
lDepthCorrection -
entities.getDims().width) /
2,
autoscale:
!!pAST.options && !!pAST.options.width && pAST.options.width === "auto",
verticaltransform: PAD_VERTICAL,
scale: 1,
} as any;
lCanvas.x = 0 - lCanvas.horizontaltransform;
lCanvas.y = 0 - lCanvas.verticaltransform;
return lCanvas;
}
function renderBackground(pCanvas: ICanvas) {
const lBackground = gChart.document.getElementById(
idmanager.get("_background")
);
if (lBackground) {
lBackground.appendChild(
svgelementfactory.createRect(pCanvas, { class: "bglayer" })
);
}
}
function renderWatermark(pWatermark: string, pCanvas: ICanvas) {
gChart.layers.watermark.appendChild(
svgelementfactory.createDiagonalText(pWatermark, pCanvas, "watermark")
);
}
function postProcessOptions(pOptions: IOptionsNormalized, pCanvas: ICanvas) {
if (pOptions.watermark) {
renderWatermark(pOptions.watermark, pCanvas);
}
if (pOptions.width && pOptions.width !== "auto") {
pCanvas = renderutensils.scaleCanvasToWidth(pOptions.width, pCanvas);
}
return pCanvas;
}
function renderSvgElement(pCanvas: ICanvas) {
const lSvgElement = gChart.document.getElementById(idmanager.get());
const lBody = gChart.document.getElementById(idmanager.get("_body"));
if (lBody && lSvgElement) {
lBody.setAttribute(
"transform",
`translate(${pCanvas.horizontaltransform},${pCanvas.verticaltransform}) ` +
`scale(${pCanvas.scale},${pCanvas.scale})`
);
if (!!pCanvas.autoscale && pCanvas.autoscale === true) {
svgelementfactory.updateSVG(lSvgElement, {
width: "100%",
height: "100%",
viewBox: `0 0 ${pCanvas.width.toString()} ${pCanvas.height.toString()}`,
});
} else {
svgelementfactory.updateSVG(lSvgElement, {
width: pCanvas.width.toString(),
height: pCanvas.height.toString(),
viewBox: `0 0 ${pCanvas.width.toString()} ${pCanvas.height.toString()}`,
});
}
}
}
//#region entities
function renderEntitiesOnBottom(
pEntities: IEntityNormalized[],
pOptions: IOptionsNormalized
) {
const lLifeLineSpacerY =
rowmemory.getLast().y +
(rowmemory.getLast().height + gChart.arcRowHeight) / 2;
/*
insert a life line between the last arc and the entities so there's
some visual breathing room
*/
createLifeLines(
pEntities,
"arcrow",
gChart.arcRowHeight,
lLifeLineSpacerY
).forEach((pLifeLine) => {
gChart.layers.lifeline.appendChild(pLifeLine);
});
/*
We used to have a simple 'use' element here that refered to the
entities on top. It's cheaper and faster, however in Firefox
56.0b6 (developer edition) they rendered with the wrong font
in the best of cases, and as solid black boxes in the worst.
Looks like a bug in Firefox, that should be fixed there, but
implementing this workaround is safer. It also does away with
`use` s that seem to be hard to implement well for svg render engine
builders (ref the links in uses that didn't work in some browsers).
*/
renderEntities(
pEntities,
lLifeLineSpacerY + gChart.arcRowHeight / 2,
pOptions
);
}
/**
* renderEntities() - renders the given pEntities (subtree of the AST) into
* the gChart.layers.sequence layer
*
* @param <object> - pEntities - the entities to render
* @param <int> - pEntityYPos - the Y position to render the entities on
* @param <object> - pOptions
*
*/
function renderEntities(
pEntities: IEntityNormalized[],
pEntityYPos: number,
pOptions: IOptionsNormalized
) {
gChart.layers.sequence.appendChild(
entities.renderEntities(pEntities, pEntityYPos, pOptions)
);
gChart.arcEndX =
entities.getDims().entityXHWM -
entities.getDims().interEntitySpacing +
entities.getDims().width;
}
//#endregion
function renderBroadcastArc(
pArc: IFlatArc,
pEntities: IEntityNormalized[],
pRowMemory,
pRowNumber: number,
pOptions: IOptionsNormalized
) {
let xTo = 0;
const lLabel = pArc.label;
const xFrom = entities.getX(pArc.from);
pArc.label = "";
pEntities.forEach((pEntity) => {
let lElement = {};
if (pEntity.name !== pArc.from) {
xTo = entities.getX(pEntity.name);
lElement = createArc(pArc, xFrom, xTo, pRowNumber, pOptions);
pRowMemory.push({
layer: gChart.layers.sequence,
element: lElement,
});
}
});
pArc.label = lLabel;
}
function renderRegularArc(
pArc: IFlatArc,
pEntities: IEntityNormalized[],
pRowMemory,
pRowNumber: number,
pOptions: IOptionsNormalized
) {
let lElement: SVGGElement = svgelementfactory.createGroup();
if (pArc.from && pArc.to) {
if (pArc.to === "*") {
// it's a broadcast arc
renderBroadcastArc(pArc, pEntities, pRowMemory, pRowNumber, pOptions);
/* creates a label on the current line, smack in the middle */
lElement = renderlabels.createLabel(
pArc,
{
x: 0,
y: rowmemory.get(pRowNumber).y,
width: gChart.arcEndX,
},
Object.assign(
{
alignAround: true,
ownBackground: true,
},
cloneDeep(pOptions)
)
);
pRowMemory.push({
title: pArc.title,
layer: gChart.layers.sequence,
element: lElement,
});
} else {
// it's a regular arc
lElement = createArc(
pArc,
entities.getX(pArc.from),
entities.getX(pArc.to),
pRowNumber,
pOptions
);
pRowMemory.push({
title: pArc.title,
layer: gChart.layers.sequence,
element: lElement,
});
} // / lTo or pArc.from === "*"
} // if both a from and a to
return lElement;
}
function getArcRowHeight(
pArcRow: IFlatArc[],
pEntities: IEntityNormalized[],
pOptions: IOptionsNormalized
) {
let lRetval = 0;
pArcRow.forEach((pArc: IFlatArc) => {
let lElement: SVGGElement;
switch (aggregatekind(pArc.kind)) {
case "empty":
lElement = renderEmptyArc(pArc, 0);
break;
case "box":
lElement = createBox(
entities.getOAndD(pArc.from, pArc.to),
pArc,
0,
pOptions
);
break;
case "inline_expression":
lElement = renderInlineExpressionLabel(pArc, 0);
break;
default: /* ignore arc skips when calculating row heights */
const lArc = cloneDeep(pArc);
lArc.arcskip = 0;
lElement = renderRegularArc(lArc, pEntities, [], 0, pOptions); // TODO is 0 a good row number for this?
} // switch
lRetval = Math.max(
lRetval,
svgutensils.getBBox(lElement).height + 2 * constants.LINE_WIDTH
);
}); // for all arcs in a row
return lRetval;
}
function renderArcRow(
pArcRow: IFlatArc[],
pRowNumber,
pEntities: IEntityNormalized[],
pOptions: IOptionsNormalized
) {
let lArcRowClass = "arcrow";
const lRowMemory: any[] = [];
pArcRow.forEach((pArc: IFlatArc) => {
let lElement = {};
switch (aggregatekind(pArc.kind)) {
case "empty":
lElement = renderEmptyArc(pArc, rowmemory.get(pRowNumber).y);
if ("..." === pArc.kind) {
lArcRowClass = "arcrowomit";
}
lRowMemory.push({
layer: gChart.layers.sequence,
element: lElement,
});
break;
case "box":
lElement = createBox(
entities.getOAndD(pArc.from, pArc.to),
pArc,
rowmemory.get(pRowNumber).y,
pOptions
);
lRowMemory.push({
title: pArc.title,
layer: gChart.layers.notes,
element: lElement,
});
break;
case "inline_expression":
lElement = renderInlineExpressionLabel(
pArc,
rowmemory.get(pRowNumber).y
);
lRowMemory.push({
layer: gChart.layers.notes,
element: lElement,
});
gInlineExpressionMemory.push({
arc: pArc,
rownum: pRowNumber,
});
break;
default:
lElement = renderRegularArc(
pArc,
pEntities,
lRowMemory,
pRowNumber,
pOptions
);
} // switch
}); // for all arcs in a row
/*
* only here we can determine the height of the row and the y position
*/
createLifeLines(
pEntities,
lArcRowClass,
rowmemory.get(pRowNumber).height,
rowmemory.get(pRowNumber).y
).forEach((pLifeLine) => {
gChart.layers.lifeline.appendChild(pLifeLine);
});
lRowMemory.forEach((pRowMemoryLine) => {
if (pRowMemoryLine.element) {
if (pRowMemoryLine.title) {
pRowMemoryLine.element.appendChild(
svgelementfactory.createTitle(pRowMemoryLine.title)
);
}
pRowMemoryLine.layer.appendChild(pRowMemoryLine.element);
}
});
}
function precalculateArcRowHeights(
pArcRows: IFlatArc[][],
pEntities: IEntityNormalized[],
pOptions: IOptionsNormalized
) {
let lRealRowNumber = 0;
pArcRows.forEach((pArcRow, pRowNumber) => {
if (pArcRow.every((pArc) => pArc.isVirtual)) {
rowmemory.set(
pRowNumber,
Math.max(
rowmemory.get(pRowNumber).height,
getArcRowHeight(pArcRow, pEntities, pOptions)
)
);
} else {
rowmemory.set(
pRowNumber,
Math.max(
rowmemory.get(pRowNumber).height,
getArcRowHeight(pArcRow, pEntities, pOptions)
),
lRealRowNumber
);
lRealRowNumber++;
}
});
}
/** renderArcRows() - renders the arcrows from an AST
*
* @param <object> - pArcRows - the arc rows to render
* @param <object> - pEntities - the entities to consider
*/
function renderArcRows(
pArcRows: IFlatArc[][],
pEntities: IEntityNormalized[],
pOptions: IOptionsNormalized
) {
gInlineExpressionMemory = [];
/* put some space between the entities and the arcs */
createLifeLines(
pEntities,
"arcrow",
gChart.arcRowHeight,
rowmemory.get(-1).y
).forEach((pLifeLine) => {
gChart.layers.lifeline.appendChild(pLifeLine);
});
precalculateArcRowHeights(pArcRows, pEntities, pOptions);
pArcRows.forEach((pArcRow, pCounter) => {
renderArcRow(pArcRow, pCounter, pEntities, pOptions);
});
renderInlineExpressions(gInlineExpressionMemory);
}
/**
* renderInlineExpressionLabel() - renders the label of an inline expression
* (/ arc spanning arc)
*
* @param <object> pArc - the arc spanning arc
* @param <number pY - where to start
*/
function renderInlineExpressionLabel(pArc: IFlatArc, pY: number) {
const lOnD = entities.getOAndD(pArc.from, pArc.to);
const FOLD_SIZE = 7;
const lLabelContentAlreadyDetermined = pY > 0;
const lMaxDepthCorrection = gChart.maxDepth * 2 * constants.LINE_WIDTH;
const lMaxWidth =
lOnD.to -
lOnD.from +
(entities.getDims().interEntitySpacing - 2 * constants.LINE_WIDTH) -
FOLD_SIZE -
constants.LINE_WIDTH;
const lStart =
lOnD.from -
(entities.getDims().interEntitySpacing -
3 * constants.LINE_WIDTH -
lMaxDepthCorrection) /
2 -
(gChart.maxDepth - pArc.depth) * 2 * constants.LINE_WIDTH;
const lGroup = svgelementfactory.createGroup();
if (!lLabelContentAlreadyDetermined) {
pArc.label = pArc.kind + (pArc.label ? ": " + pArc.label : "");
}
const lTextGroup = renderlabels.createLabel(
pArc,
{
x: lStart + constants.LINE_WIDTH - lMaxWidth / 2,
y: pY + gChart.arcRowHeight / 4,
width: lMaxWidth,
},
{
alignLeft: true,
ownBackground: false,
wordwraparcs: gChart.wordWrapArcs,
}
);
const lBBox = svgutensils.getBBox(lTextGroup);
const lHeight = Math.max(
lBBox.height + 2 * constants.LINE_WIDTH,
gChart.arcRowHeight / 2 - 2 * constants.LINE_WIDTH
);
const lWidth = Math.min(lBBox.width + 2 * constants.LINE_WIDTH, lMaxWidth);
const lBox = svgelementfactory.createEdgeRemark(
{
width: lWidth - constants.LINE_WIDTH + FOLD_SIZE,
height: lHeight,
x: lStart,
y: pY,
},
{
class: "box inline_expression_label",
color: pArc.linecolor,
bgColor: pArc.textbgcolor,
foldSize: FOLD_SIZE,
}
);
lGroup.appendChild(lBox);
lGroup.appendChild(lTextGroup);
return lGroup;
}
function createInlineExpressionBox(
pOAndD,
pArc: IFlatArc,
pHeight: number,
pY: number
) {
/* begin: same as createBox */
const lMaxDepthCorrection = gChart.maxDepth * 2 * constants.LINE_WIDTH;
const lWidth =
pOAndD.to -
pOAndD.from +
entities.getDims().interEntitySpacing -
2 * constants.LINE_WIDTH -
lMaxDepthCorrection; // px
const lStart =
pOAndD.from -
(entities.getDims().interEntitySpacing -
2 * constants.LINE_WIDTH -
lMaxDepthCorrection) /
2;
/* end: same as createBox */
const lArcDepthCorrection =
(gChart.maxDepth - pArc.depth) * 2 * constants.LINE_WIDTH;
return svgelementfactory.createRect(
{
width: lWidth + lArcDepthCorrection * 2,
height: pHeight
? pHeight
: gChart.arcRowHeight - 2 * constants.LINE_WIDTH,
x: lStart - lArcDepthCorrection,
y: pY,
},
{
class: `box inline_expression ${pArc.kind}`,
color: pArc.linecolor,
bgColor: pArc.textbgcolor,
}
);
}
function renderInlineExpressions(pInlineExpressions) {
pInlineExpressions.forEach((pInlineExpression) => {
gChart.layers.inline.appendChild(
renderInlineExpression(
pInlineExpression,
rowmemory.get(pInlineExpression.rownum).y
)
);
});
}
function renderInlineExpression(pArcMem, pY: number) {
const lFromY = rowmemory.get(pArcMem.rownum).y;
const lToY = rowmemory.get(pArcMem.rownum + pArcMem.arc.numberofrows + 1).y;
const lHeight = lToY - lFromY;
pArcMem.arc.label = "";
return createInlineExpressionBox(
entities.getOAndD(pArcMem.arc.from, pArcMem.arc.to),
pArcMem.arc,
lHeight,
pY
);
}
function createLifeLines(
pEntities: IEntityNormalized[],
pClass: string,
pHeight: number,
pY: number
): (SVGLineElement | SVGPathElement)[] {
/* istanbul ignore if */
if (pHeight < gChart.arcRowHeight) {
pHeight = gChart.arcRowHeight;
}
return pEntities.map((pEntity) => {
const lLine = svgelementfactory.createLine(
{
xFrom: entities.getX(pEntity.name),
yFrom: 0 - pHeight / 2 + (pY ? pY : 0),
xTo: entities.getX(pEntity.name),
yTo: pHeight / 2 + (pY ? pY : 0),
},
{
class: pClass,
}
);
if (pEntity.linecolor) {
lLine.setAttribute("style", `stroke:${pEntity.linecolor};`);
}
return lLine;
});
}
function createSelfRefArc(
pKind: ArcKindNormalizedType,
pX: number,
pYTo: number,
pDouble: boolean,
pY: number,
pLineColor?: string
) {
// globals: (gChart ->) arcRowHeight, (entities ->) interEntitySpacing
const lHeight = 2 * (gChart.arcRowHeight / 5);
const lWidth = entities.getDims().interEntitySpacing / 2;
let lRetval: any = {};
const lClass = `arc ${kind2class.getAggregateClass(
pKind
)} ${kind2class.getClass(pKind)}`;
if (pDouble) {
lRetval = svgelementfactory.createGroup();
const lInnerTurn = svgelementfactory.createUTurn(
{
x: pX,
y: pY,
width: lWidth - 2 * constants.LINE_WIDTH,
height: lHeight,
},
pY + pYTo + lHeight - 2 * constants.LINE_WIDTH, // pY
{
class: lClass,
dontHitHome: pKind !== "::",
lineWidth: constants.LINE_WIDTH,
}
);
/* we need a middle turn to attach the arrow to */
const lMiddleTurn = svgelementfactory.createUTurn(
{
x: pX,
y: pY,
width: lWidth,
height: lHeight,
},
pY + pYTo + lHeight - constants.LINE_WIDTH,
{ lineWidth: constants.LINE_WIDTH }
);
const lOuterTurn = svgelementfactory.createUTurn(
{
x: pX,
y: pY,
width: lWidth,
height: lHeight,
},
pY + pYTo + lHeight,
{
class: lClass,
dontHitHome: pKind !== "::",
lineWidth: constants.LINE_WIDTH,
}
);
if (!!pLineColor) {
lInnerTurn.setAttribute("style", `stroke:${pLineColor}`);
}
markermanager
.getAttributes(idmanager.get(), pKind, pLineColor, pX, pX)
.forEach((pAttribute: any) => {
lMiddleTurn.setAttribute(pAttribute.name, pAttribute.value);
});
lMiddleTurn.setAttribute("style", "stroke:transparent;");
if (Boolean(pLineColor)) {
lOuterTurn.setAttribute("style", `stroke:${pLineColor}`);
}
lRetval.appendChild(lInnerTurn);
lRetval.appendChild(lOuterTurn);
lRetval.appendChild(lMiddleTurn);
lRetval.setAttribute("class", lClass);
} else {
lRetval = svgelementfactory.createUTurn(
{
x: pX,
y: pY,
width: lWidth,
height: lHeight,
},
pY + pYTo + lHeight,
{
class: lClass,
dontHitHome: pKind === "-x",
lineWidth: constants.LINE_WIDTH,
}
);
markermanager
.getAttributes(idmanager.get(), pKind, pLineColor, pX, pX)
.forEach((pAttribute: any) => {
lRetval.setAttribute(pAttribute.name, pAttribute.value);
});
}
return lRetval;
}
function renderEmptyArc(pArc: IFlatArc, pY: number) {
if (pArc.kind === "---") {
return createComment(pArc, entities.getOAndD(pArc.from, pArc.to), pY);
} else {
/* "..." / "|||" */
return createLifeLinesText(pArc, entities.getOAndD(pArc.from, pArc.to), pY);
}
}
function determineYToAbsolute(
pRowNumber: number,
pArcGradient: number,
pArcSkip?: number
): number {
let lRetval = rowmemory.get(pRowNumber).y + pArcGradient;
if (!!pArcSkip) {
const lWholeArcSkip = Math.floor(pArcSkip);
const lRestArcSkip = pArcSkip - lWholeArcSkip;
const lCurrentRealRowNumber = rowmemory.get(pRowNumber).realRowNumber;
lRetval =
rowmemory.getByRealRowNumber(lCurrentRealRowNumber + lWholeArcSkip).y +
lRestArcSkip *
(rowmemory.getByRealRowNumber(lCurrentRealRowNumber + lWholeArcSkip + 1)
.y -
rowmemory.getByRealRowNumber(lCurrentRealRowNumber + lWholeArcSkip)
.y);
}
return lRetval;
}
function determineDirectionClass(pArcKind: ArcKindNormalizedType): string {
if (pArcKind === "<:>") {
return "bidi ";
} else if (pArcKind === "::") {
return "nodi ";
}
return "";
}
function createArc(
pArc: IFlatArc,
pXFrom: number,
pXTo: number,
pRowNumber: number,
pOptions: IOptionsNormalized
) {
const lGroup = svgelementfactory.createGroup();
let lClass = "arc ";
lClass += determineDirectionClass(pArc.kind);
lClass += `${kind2class.getAggregateClass(pArc.kind)} ${kind2class.getClass(
pArc.kind
)}`;
const lDoubleLine = [":>", "::", "<:>"].includes(pArc.kind);
const lYToAbsolute = determineYToAbsolute(
pRowNumber,
gChart.arcGradient,
pArc.arcskip
);
pXTo = renderutensils.determineArcXTo(pArc.kind, pXFrom, pXTo);
if (pXFrom === pXTo) {
lGroup.appendChild(
createSelfRefArc(
pArc.kind,
pXFrom,
lYToAbsolute - rowmemory.get(pRowNumber).y - gChart.arcGradient,
lDoubleLine,
rowmemory.get(pRowNumber).y,
pArc.linecolor
)
);
/* creates a label left aligned, a little above the arc*/
const lTextWidth = (2 * entities.getDims().interEntitySpacing) / 3;
lGroup.appendChild(
renderlabels.createLabel(
pArc,
{
x: pXFrom + 1.5 * constants.LINE_WIDTH - lTextWidth / 2,
y:
rowmemory.get(pRowNumber).y -
gChart.arcRowHeight / 5 -
constants.LINE_WIDTH / 2,
width: lTextWidth,
},
Object.assign(
{
alignLeft: true,
alignAbove: true,
ownBackground: true,
},
cloneDeep(pOptions)
)
)
);
} else {
const lLine = svgelementfactory.createLine(
{
xFrom: pXFrom,
yFrom: rowmemory.get(pRowNumber).y,
xTo: pXTo,
yTo: lYToAbsolute,
},
{
class: lClass,
doubleLine: lDoubleLine,
}
);
markermanager
.getAttributes(idmanager.get(), pArc.kind, pArc.linecolor, pXFrom, pXTo)
.forEach((pAttribute: any) => {
lLine.setAttribute(pAttribute.name, pAttribute.value);