diff options
Diffstat (limited to 'subprojects/frontend/src/graph/parseBBox.ts')
-rw-r--r-- | subprojects/frontend/src/graph/parseBBox.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/subprojects/frontend/src/graph/parseBBox.ts b/subprojects/frontend/src/graph/parseBBox.ts new file mode 100644 index 00000000..34df746b --- /dev/null +++ b/subprojects/frontend/src/graph/parseBBox.ts | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | * Copyright 2017, Magnus Jacobsson | ||
3 | * Copyright 2023, The Refinery Authors <https://refinery.tools/> | ||
4 | * | ||
5 | * SPDX-License-Identifier: BSD-3-Clause | ||
6 | * | ||
7 | * This file Incorporates patches from the Refinery authors. | ||
8 | * | ||
9 | * Redistribution and use is only permitted if neither | ||
10 | * the name of the copyright holder Magnus Jacobsson nor the names of other | ||
11 | * contributors to the d3-graphviz project are used to endorse or promote | ||
12 | * products derived from this software as per the 3rd clause of the | ||
13 | * 3-clause BSD license. | ||
14 | * | ||
15 | * See LICENSES/BSD-3-Clause.txt for more details. | ||
16 | */ | ||
17 | |||
18 | export interface BBox { | ||
19 | x: number; | ||
20 | y: number; | ||
21 | width: number; | ||
22 | height: number; | ||
23 | } | ||
24 | |||
25 | function parsePoints(points: string[]): BBox { | ||
26 | const x = points.map((p) => Number(p.split(',')[0] ?? 0)); | ||
27 | const y = points.map((p) => Number(p.split(',')[1] ?? 0)); | ||
28 | const xmin = Math.min.apply(null, x); | ||
29 | const xmax = Math.max.apply(null, x); | ||
30 | const ymin = Math.min.apply(null, y); | ||
31 | const ymax = Math.max.apply(null, y); | ||
32 | return { | ||
33 | x: xmin, | ||
34 | y: ymin, | ||
35 | width: xmax - xmin, | ||
36 | height: ymax - ymin, | ||
37 | }; | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Compute the bounding box of a polygon without adding it to the DOM. | ||
42 | * | ||
43 | * Copyed from | ||
44 | * https://github.com/magjac/d3-graphviz/blob/81ab523fe5189a90da2d9d9cc9015c7079eea780/src/element.js#L36-L53 | ||
45 | * | ||
46 | * @param path The polygon to compute the bounding box of. | ||
47 | * @returns The computed bounding box. | ||
48 | */ | ||
49 | export function parsePolygonBBox(polygon: SVGPolygonElement): BBox { | ||
50 | const points = (polygon.getAttribute('points') ?? '').split(' '); | ||
51 | return parsePoints(points); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * Compute the bounding box of a path without adding it to the DOM. | ||
56 | * | ||
57 | * Copyed from | ||
58 | * https://github.com/magjac/d3-graphviz/blob/81ab523fe5189a90da2d9d9cc9015c7079eea780/src/element.js#L56-L75 | ||
59 | * | ||
60 | * @param path The path to compute the bounding box of. | ||
61 | * @returns The computed bounding box. | ||
62 | */ | ||
63 | export function parsePathBBox(path: SVGPathElement): BBox { | ||
64 | const d = path.getAttribute('d') ?? ''; | ||
65 | const points = d.split(/[A-Z ]/); | ||
66 | points.shift(); | ||
67 | return parsePoints(points); | ||
68 | } | ||