-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathview_box.ts
328 lines (302 loc) · 10.2 KB
/
view_box.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
import {PartialBoxAlignment, alignmentToNumber, boxAlignmentFromPartial} from './alignment.ts';
import {Point} from './point.ts';
import {almostEqual, roundReasonably} from './util.ts';
/** A rectangle defining bounding box of an element, or the view box of the SVG. */
export interface ViewBox {
readonly minX: number;
readonly minY: number;
readonly width: number;
readonly height: number;
}
export interface IncompleteDimSpec {
readonly pos?: "default" | "center";
readonly min?: number;
readonly max?: number;
readonly len?: number;
}
export type FullDimSpec =
IncompleteDimSpec & Pick<Required<IncompleteDimSpec>, "min" | "max" | "len">;
export type DefiniteDimSpec =
Pick<Required<IncompleteDimSpec>, "min" | "max"> |
Pick<Required<IncompleteDimSpec>, "min" | "len"> |
Pick<Required<IncompleteDimSpec>, "max" | "len"> |
Pick<Required<IncompleteDimSpec>, "min" | "pos"> |
Pick<Required<IncompleteDimSpec>, "max" | "pos"> |
Pick<Required<IncompleteDimSpec>, "len" | "pos">;
export interface DimSpec {
readonly min: number;
readonly len: number;
}
interface IncompleteDimSpecData {
readonly inferred: IncompleteDimSpec;
readonly forceInferred?: DimSpec;
readonly error?: string;
}
interface DefiniteDimSpecData {
readonly inferred: FullDimSpec;
readonly forceInferred?: DimSpec;
readonly error?: string;
}
function getDimSpecData(dimSpec: DefiniteDimSpec): DefiniteDimSpecData;
function getDimSpecData(dimSpec: IncompleteDimSpec): IncompleteDimSpecData;
function getDimSpecData(input: IncompleteDimSpec) {
const {pos, min, max, len} = input;
const incompatibleDimensions: IncompleteDimSpecData = {
inferred: input,
error: `Incompatible dimensions`,
};
function ret({min, max, len, forced = false}:
{min: number, max: number, len: number, forced?: boolean}) {
let lPos = pos;
if (!almostEqual(len, max - min))
return incompatibleDimensions;
if (lPos === "default" && min)
return incompatibleDimensions;
const centered = almostEqual(max, -min);
if (lPos === "center" && !centered)
return incompatibleDimensions;
if (!lPos) {
if (!min)
lPos = "default";
else if (centered)
lPos = "center";
}
let error: string | undefined;
if (len < 0)
error = `Negative dimension length`;
return {
inferred: forced ? input : {pos: lPos, min, max, len},
forceInferred: {min, len},
error,
};
}
if (min !== undefined) {
if (max !== undefined) {
if (len !== undefined)
return ret({min, max, len});
return ret({min, max, len: max - min});
}
if (len !== undefined)
return ret({min, max: min + len, len});
if (pos === "default")
return ret({min, max: 0, len: 0});
if (pos === "center")
return ret({min, max: -min, len: -2 * min});
return ret({min, max: 0, len: -min, forced: true});
}
if (max !== undefined) {
if (len !== undefined)
return ret({min: max - len, max, len});
if (pos === "default")
return ret({min: 0, max, len: max});
if (pos === "center")
return ret({min: -max, max, len: 2 * max});
return ret({min: 0, max, len: max, forced: true});
}
if (len !== undefined) {
if (pos === "default")
return ret({min: 0, max: len, len});
if (pos === "center")
return ret({min: -len / 2, max: len / 2, len});
return ret({min: 0, max: len, len, forced: true});
}
if (!pos || pos === "default")
return ret({min: 0, max: 1, len: 1, forced: true});
return ret({min: -0.5, max: 0.5, len: 1, forced: true});
}
export function inferDimSpec(spec: DefiniteDimSpec): FullDimSpec;
export function inferDimSpec(spec: IncompleteDimSpec): IncompleteDimSpec;
export function inferDimSpec(spec: IncompleteDimSpec) {
return getDimSpecData(spec).inferred;
}
export function dimSpecFromIncomplete(spec?: IncompleteDimSpec): DimSpec {
const {forceInferred, error} = getDimSpecData(spec || {});
if (!forceInferred)
throw new Error(`Error in partial dimension spec: ${error || `(unknown)`} ` +
`(input: ${JSON.stringify(spec)})`);
return forceInferred;
}
export interface PartialViewBoxFlat {
centered?: boolean;
side?: number;
centeredX?: boolean;
minX?: number;
maxX?: number;
width?: number;
centeredY?: boolean;
minY?: number;
maxY?: number;
height?: number;
margin?: PartialViewBoxMargin;
}
export interface PartialViewBoxSeparate {
x?: IncompleteDimSpec;
y?: IncompleteDimSpec;
margin?: PartialViewBoxMargin;
}
export type PartialViewBox = PartialViewBoxFlat | PartialViewBoxSeparate;
export function viewBoxFromPartial(viewBox?: PartialViewBox): ViewBox {
const {x, y, margin} = partialViewBoxToSeparate(viewBox);
const {min: minX, len: width} = dimSpecFromIncomplete(x);
const {min: minY, len: height} = dimSpecFromIncomplete(y);
return extendViewBox({minX, minY, width, height}, margin);
}
export function partialViewBoxToXY(viewBox: PartialViewBox) {
const {x, y, margin} = partialViewBoxToSeparate(viewBox);
const {left, right, top, bottom} = viewBoxMarginFromPartial(margin);
function expandByMargin(
dimSpec: IncompleteDimSpec | undefined, lowerMargin: number, upperMargin: number): DimSpec {
const {min, len} = dimSpecFromIncomplete(dimSpec);
return {
min: min - lowerMargin,
len: len + lowerMargin + upperMargin,
};
}
return {
x: expandByMargin(x, left, right),
y: expandByMargin(y, top, bottom),
};
}
function partialViewBoxToSeparate(viewBox: PartialViewBox = {}): PartialViewBoxSeparate {
const {
centered,
centeredX = centered,
centeredY = centered,
side,
minX, maxX,
width = side,
minY, maxY,
height = side,
x = {pos: centeredX ? "center" : undefined, min: minX, max: maxX, len: width},
y = {pos: centeredY ? "center" : undefined, min: minY, max: maxY, len: height},
margin,
}: Partial<PartialViewBoxFlat & PartialViewBoxSeparate> = viewBox;
return {x, y, margin};
}
/**
* Margin around a ViewBox. Positive values point outside of the rectangle, enlarging it,
* negative values point inside, shrinking it.
*/
export interface ViewBoxMargin {
readonly left: number;
readonly right: number;
readonly top: number;
readonly bottom: number;
}
export interface PartialViewBoxMarginInterface {
value?: number;
x?: number;
y?: number;
left?: number;
right?: number;
top?: number;
bottom?: number;
}
export type PartialViewBoxMargin = PartialViewBoxMarginInterface | number;
export function viewBoxMarginFromPartial(partial: PartialViewBoxMargin = {}, defaultValue = 0):
ViewBoxMargin {
return (({
value = defaultValue,
x = value,
y = value,
left = x,
right = x,
top = y,
bottom = y,
}) => ({left, right, top, bottom}))(
typeof partial === "number" ? {value: partial} : partial);
}
/** Returns the ViewBox created from `element.getBBox()`. */
export function viewBoxFromBBox(element: SVGGraphicsElement): ViewBox {
const {x: minX, y: minY, width, height} = element.getBBox();
return {minX, minY, width, height};
}
/**
* Multiplies the specified margin by the specified multiplier. The multiplier can specify
* different values for different sides, and defaults to 1 for the unspecified values.
*/
export function multiplyMargin(
{left, right, top, bottom}: ViewBoxMargin,
multiplier: PartialViewBoxMargin): ViewBoxMargin {
const {left: leftMult, right: rightMult, top: topMult, bottom: bottomMult} =
viewBoxMarginFromPartial(multiplier, 1);
return {
left: left * leftMult,
right: right * rightMult,
top: top * topMult,
bottom: bottom * bottomMult,
};
}
/** Extends the ViewBox by the specified margin, or shrinks in case of negative margin. */
export function extendViewBox(
{minX, minY, width, height}: ViewBox,
margin?: PartialViewBoxMargin): ViewBox {
const {left, right, top, bottom} = viewBoxMarginFromPartial(margin);
return {
minX: minX - left,
width: width + left + right,
minY: minY - top,
height: height + top + bottom,
};
}
/** Returns the actual margin between the bounding box and the view box. */
export function getMargin({boundingBox, viewBox}: {
boundingBox: ViewBox,
viewBox: ViewBox,
}): ViewBoxMargin {
return {
left: boundingBox.minX - viewBox.minX,
right: (viewBox.minX + viewBox.width) - (boundingBox.minX + boundingBox.width),
top: boundingBox.minY - viewBox.minY,
bottom: (viewBox.minY + viewBox.height) - (boundingBox.minY + boundingBox.height),
};
}
interface FitsInViewBoxArgs {
boundingBox: ViewBox;
viewBox: ViewBox;
minMargin?: PartialViewBoxMargin;
}
const MARGIN_EPSILON_TO_SIZE_RATIO = 1e-9;
const MARGIN_SIDES: (keyof ViewBoxMargin)[] = ["left", "right", "top", "bottom"];
function getSidesFit({boundingBox, viewBox, minMargin = 0}: FitsInViewBoxArgs) {
const epsilon = Math.max(viewBox.width, viewBox.height) * MARGIN_EPSILON_TO_SIZE_RATIO;
const fullMinMargin = viewBoxMarginFromPartial(minMargin);
const actualMargin = getMargin({boundingBox, viewBox});
return MARGIN_SIDES.map(side => {
const min = fullMinMargin[side];
const actual = actualMargin[side];
return {
side,
min,
actual,
fits: actual >= min - epsilon,
};
});
}
/**
* Calculates whether the specified bounding box fits in the specified view box with at least
* the specified minimum margin.
*/
export function fitsInViewBox(args: FitsInViewBoxArgs) {
return getSidesFit(args).every(({fits}) => fits);
}
export function assertFitsInViewBox(args: FitsInViewBoxArgs) {
const sidesFit = getSidesFit(args);
if (sidesFit.every(({fits}) => fits))
return;
throw new Error(`The bounding box does not fit in the view box` +
`(bounding box: ${viewBoxToString(args.boundingBox)}, ` +
`view box: ${viewBoxToString(args.viewBox)}, ` +
`sides: ${JSON.stringify(sidesFit)})`);
}
export function viewBoxToString({minX, minY, width, height}: ViewBox) {
return [minX, minY, width, height].map(v => roundReasonably(v)).join(" ");
}
export function getBoxPoint(box: PartialViewBox, point: PartialBoxAlignment): Point {
const {minX, minY, width, height} = viewBoxFromPartial(box);
const {x = "center", y = "center"} = boxAlignmentFromPartial(point);
return [
minX + (alignmentToNumber(x) + 1) / 2 * width,
minY + (alignmentToNumber(y) + 1) / 2 * height,
];
}