@mark1russell7/layout-contract 0.10.1 → 0.10.3
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 +5 -0
- package/build/index.d.ts +295 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +752 -9
- package/build/index.js.map +1 -1
- package/package.json +2 -1
- package/src/geometry3.ts +260 -0
- package/src/index.ts +3 -0
- package/src/path.ts +300 -0
- package/src/picture.ts +277 -0
package/src/picture.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// The picture of a layout (`docs/PICTURE.md`): the record `layout-picture/1`. It holds the placed boxes, the route of each edge and the labels.
|
|
2
|
+
// A viewer draws a picture. Thus it shows the output of each engine, and it imports no engine.
|
|
3
|
+
import type { PlacedBox } from "./balance.ts";
|
|
4
|
+
import type { Point, Rect, Region } from "./geometry.ts";
|
|
5
|
+
import type { LayoutBox, LayoutInput } from "./input.ts";
|
|
6
|
+
import { checkPath, pathBounds, pathOf, type PathSegment } from "./path.ts";
|
|
7
|
+
|
|
8
|
+
/** The schema of a picture. */
|
|
9
|
+
export const PICTURE_SCHEMA = "layout-picture/1";
|
|
10
|
+
|
|
11
|
+
/** The places of the lines of a label in its width. */
|
|
12
|
+
export const LABEL_ALIGNS = ["left", "center", "right"] as const;
|
|
13
|
+
|
|
14
|
+
/** The place of the lines of a label in its width. */
|
|
15
|
+
export type LabelAlign = (typeof LABEL_ALIGNS)[number];
|
|
16
|
+
|
|
17
|
+
/** One box of a picture: its rectangle in the coordinates of the picture. A child does not have coordinates relative to its parent. */
|
|
18
|
+
export interface PictureBox extends PlacedBox {
|
|
19
|
+
/** The box that contains this box. A parent comes before its children in the list. */
|
|
20
|
+
readonly parent?: string;
|
|
21
|
+
/** The region of the box, relative to its top-left corner, as `LayoutBox.shape`. Without it, the box is its rectangle. */
|
|
22
|
+
readonly shape?: Region;
|
|
23
|
+
/** The free data of the box. A viewer chooses the look of the box from it. */
|
|
24
|
+
readonly data?: Readonly<Record<string, unknown>>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The route of one edge: the two boxes of the edge and the path that a renderer draws. */
|
|
28
|
+
export interface PictureRoute {
|
|
29
|
+
/** The id of the edge. */
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly source: string;
|
|
32
|
+
readonly target: string;
|
|
33
|
+
/** The path from the source to the target. A polyline is a path of lines (`pathOf`). */
|
|
34
|
+
readonly path: readonly PathSegment[];
|
|
35
|
+
/** The free data of the edge. A viewer chooses the look of the route from it. */
|
|
36
|
+
readonly data?: Readonly<Record<string, unknown>>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One label: the lines of a text that an engine measured, at a place. A renderer draws the lines with `font`, and it does not break them again.
|
|
41
|
+
* The rectangle is the area of the text. Line `i` is in the band that starts at `y + i * lineHeight`, and the band has the height `lineHeight`.
|
|
42
|
+
*/
|
|
43
|
+
export interface PictureLabel extends PlacedBox {
|
|
44
|
+
/** The box that the label names. A label has at most one of `box` and `route`. */
|
|
45
|
+
readonly box?: string;
|
|
46
|
+
/** The route that the label names. */
|
|
47
|
+
readonly route?: string;
|
|
48
|
+
readonly lines: readonly string[];
|
|
49
|
+
/** The height of each line. A finite number above 0. */
|
|
50
|
+
readonly lineHeight: number;
|
|
51
|
+
/** The font of the measure, as the CSS property `font`. */
|
|
52
|
+
readonly font: string;
|
|
53
|
+
/** The place of each line in the width `w`. The default is `left`. */
|
|
54
|
+
readonly align?: LabelAlign;
|
|
55
|
+
readonly data?: Readonly<Record<string, unknown>>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A picture: the boxes, the routes and the labels of a layout, in the coordinates of the picture.
|
|
60
|
+
* Each list is in the order of the paint, so its first record is at the back. A viewer chooses the order of the three lists.
|
|
61
|
+
*/
|
|
62
|
+
export interface LayoutPicture {
|
|
63
|
+
readonly schema: typeof PICTURE_SCHEMA;
|
|
64
|
+
/** The id of the input of the picture. */
|
|
65
|
+
readonly id?: string;
|
|
66
|
+
readonly boxes: readonly PictureBox[];
|
|
67
|
+
readonly routes: readonly PictureRoute[];
|
|
68
|
+
readonly labels: readonly PictureLabel[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A route of a router, for `pictureOf`: the id of its edge and the points of its polyline. A `Route` of `highways` has this form.
|
|
73
|
+
* With `path`, the picture has that path, for example a curve through the points.
|
|
74
|
+
*/
|
|
75
|
+
export interface PictureRouteSource {
|
|
76
|
+
readonly edge: string;
|
|
77
|
+
readonly points: readonly Point[];
|
|
78
|
+
readonly path?: readonly PathSegment[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The parts of a picture that an input does not hold, for `pictureOf`. */
|
|
82
|
+
export interface PictureParts {
|
|
83
|
+
readonly routes?: readonly PictureRouteSource[];
|
|
84
|
+
readonly labels?: readonly PictureLabel[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type Fields = Record<string, unknown>;
|
|
88
|
+
|
|
89
|
+
const isFields = (v: unknown): v is Fields => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
90
|
+
|
|
91
|
+
const REGION_KINDS: readonly unknown[] = ["rect", "polygon", "circle", "ellipse"];
|
|
92
|
+
|
|
93
|
+
/** This function gives the list `v`. A value that is not a list adds a problem and gives an empty list. */
|
|
94
|
+
function listOf(out: string[], v: unknown, name: string): readonly unknown[] {
|
|
95
|
+
if (Array.isArray(v)) return v;
|
|
96
|
+
out.push(`${name} is not a list`);
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* This function gives the fields and the name of record `i` of a list, or `undefined` for a record without a good id.
|
|
102
|
+
* The name is the kind and the id, for the text of a problem. An id two times in the list adds a problem.
|
|
103
|
+
*/
|
|
104
|
+
function recordOf(out: string[], kind: string, v: unknown, i: number, seen: Set<string>): { fields: Fields; id: string; name: string } | undefined {
|
|
105
|
+
if (!isFields(v)) {
|
|
106
|
+
out.push(`${kind} ${i} is not an object`);
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const id = v["id"];
|
|
110
|
+
if (typeof id !== "string" || id === "") {
|
|
111
|
+
out.push(`${kind} ${i}: the id is not a string with a character`);
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (seen.has(id)) out.push(`${kind} ${id}: the id is not unique`);
|
|
115
|
+
return { fields: v, id, name: `${kind} ${id}` };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** This function adds the problems of the rectangle of a record: a value that is not finite, or a size less than 0. */
|
|
119
|
+
function rectProblems(out: string[], name: string, v: Fields): void {
|
|
120
|
+
for (const key of ["x", "y", "w", "h"] as const) {
|
|
121
|
+
const n = v[key];
|
|
122
|
+
if (typeof n !== "number" || !Number.isFinite(n)) out.push(`${name}: ${key} is not a finite number`);
|
|
123
|
+
else if (n < 0 && (key === "w" || key === "h")) out.push(`${name}: ${key} is less than 0`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** This function adds a problem for a field `data` that is not an object. */
|
|
128
|
+
function dataProblems(out: string[], name: string, v: Fields): void {
|
|
129
|
+
if (v["data"] !== undefined && !isFields(v["data"])) out.push(`${name}: data is not an object`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* This function gives the problems of one `layout-picture/1` value. An empty list means a good picture.
|
|
134
|
+
* Each id occurs one time in its list, each number is finite, and a size is 0 or more. A parent is a box before its child in the list.
|
|
135
|
+
* The source and the target of a route are boxes of the picture, and its path has no problem of `checkPath`.
|
|
136
|
+
*
|
|
137
|
+
* A label has lines of text, a line height above 0 and a font. Its `box` or its `route` is in the picture, and it does not have the two.
|
|
138
|
+
* The check accepts fields that the format does not know.
|
|
139
|
+
*/
|
|
140
|
+
export function checkPicture(value: unknown): string[] {
|
|
141
|
+
if (!isFields(value)) return ["the picture is not an object"];
|
|
142
|
+
const out: string[] = [];
|
|
143
|
+
if (value["schema"] !== PICTURE_SCHEMA) out.push(`schema is not ${PICTURE_SCHEMA}`);
|
|
144
|
+
if (value["id"] !== undefined && typeof value["id"] !== "string") out.push("id is not a string");
|
|
145
|
+
const boxIds = new Set<string>();
|
|
146
|
+
listOf(out, value["boxes"], "boxes").forEach((v, i) => {
|
|
147
|
+
const box = recordOf(out, "box", v, i, boxIds);
|
|
148
|
+
if (box === undefined) return;
|
|
149
|
+
rectProblems(out, box.name, box.fields);
|
|
150
|
+
const parent = box.fields["parent"];
|
|
151
|
+
if (parent !== undefined && (typeof parent !== "string" || parent === box.id || !boxIds.has(parent))) out.push(`${box.name}: the parent ${String(parent)} is not a box before it in the list`);
|
|
152
|
+
const shape = box.fields["shape"];
|
|
153
|
+
if (shape !== undefined && !(isFields(shape) && REGION_KINDS.includes(shape["kind"]))) out.push(`${box.name}: the shape is not a region`);
|
|
154
|
+
dataProblems(out, box.name, box.fields);
|
|
155
|
+
boxIds.add(box.id);
|
|
156
|
+
});
|
|
157
|
+
const routeIds = new Set<string>();
|
|
158
|
+
listOf(out, value["routes"], "routes").forEach((v, i) => {
|
|
159
|
+
const route = recordOf(out, "route", v, i, routeIds);
|
|
160
|
+
if (route === undefined) return;
|
|
161
|
+
for (const key of ["source", "target"] as const) {
|
|
162
|
+
const end = route.fields[key];
|
|
163
|
+
if (typeof end !== "string" || !boxIds.has(end)) out.push(`${route.name}: the ${key} ${String(end)} is not a box of the picture`);
|
|
164
|
+
}
|
|
165
|
+
for (const problem of checkPath(route.fields["path"])) out.push(`${route.name}: ${problem}`);
|
|
166
|
+
dataProblems(out, route.name, route.fields);
|
|
167
|
+
routeIds.add(route.id);
|
|
168
|
+
});
|
|
169
|
+
const labelIds = new Set<string>();
|
|
170
|
+
listOf(out, value["labels"], "labels").forEach((v, i) => {
|
|
171
|
+
const label = recordOf(out, "label", v, i, labelIds);
|
|
172
|
+
if (label === undefined) return;
|
|
173
|
+
const f = label.fields;
|
|
174
|
+
rectProblems(out, label.name, f);
|
|
175
|
+
if (f["box"] !== undefined && f["route"] !== undefined) out.push(`${label.name}: the label has a box and a route`);
|
|
176
|
+
if (f["box"] !== undefined && (typeof f["box"] !== "string" || !boxIds.has(f["box"]))) out.push(`${label.name}: the box ${String(f["box"])} is not a box of the picture`);
|
|
177
|
+
if (f["route"] !== undefined && (typeof f["route"] !== "string" || !routeIds.has(f["route"]))) out.push(`${label.name}: the route ${String(f["route"])} is not a route of the picture`);
|
|
178
|
+
const lines = f["lines"];
|
|
179
|
+
if (!Array.isArray(lines) || !lines.every((line) => typeof line === "string")) out.push(`${label.name}: lines is not a list of strings`);
|
|
180
|
+
const lineHeight = f["lineHeight"];
|
|
181
|
+
if (typeof lineHeight !== "number" || !Number.isFinite(lineHeight) || lineHeight <= 0) out.push(`${label.name}: lineHeight is not a finite number above 0`);
|
|
182
|
+
if (typeof f["font"] !== "string" || f["font"] === "") out.push(`${label.name}: font is not a string with a character`);
|
|
183
|
+
if (f["align"] !== undefined && !(LABEL_ALIGNS as readonly unknown[]).includes(f["align"])) out.push(`${label.name}: align is not one of ${LABEL_ALIGNS.join(", ")}`);
|
|
184
|
+
dataProblems(out, label.name, f);
|
|
185
|
+
labelIds.add(label.id);
|
|
186
|
+
});
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* This function reads one `layout-picture/1` value, for example a parsed JSON document.
|
|
192
|
+
* It throws a `TypeError` with the problems of `checkPicture`.
|
|
193
|
+
*/
|
|
194
|
+
export function parsePicture(value: unknown): LayoutPicture {
|
|
195
|
+
const problems = checkPicture(value);
|
|
196
|
+
if (problems.length > 0) throw new TypeError(`The value is not a layout-picture/1: ${problems.join(", ")}.`);
|
|
197
|
+
return value as LayoutPicture;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** This function gives the box of a picture for a placed box of an input. */
|
|
201
|
+
function pictureBox(box: LayoutBox, x: number, y: number): PictureBox {
|
|
202
|
+
return {
|
|
203
|
+
id: box.id,
|
|
204
|
+
x,
|
|
205
|
+
y,
|
|
206
|
+
w: box.w,
|
|
207
|
+
h: box.h,
|
|
208
|
+
...(box.parent === undefined ? {} : { parent: box.parent }),
|
|
209
|
+
...(box.shape === undefined ? {} : { shape: box.shape }),
|
|
210
|
+
...(box.data === undefined ? {} : { data: box.data }),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* This function makes the picture of a placed input. Each box with `x` and `y` is in the picture, and a box with no place is not.
|
|
216
|
+
* The coordinates of each box are those of the input. The order is the order of the input, but each box comes immediately before its children.
|
|
217
|
+
*
|
|
218
|
+
* Each route gets the source, the target and the data of its edge in the input. A route with no `path` gets the polyline of its points.
|
|
219
|
+
* The labels of `parts` go into the picture with no change.
|
|
220
|
+
* A `RangeError` comes for a placed box below a box with no place, and for a loop of parents.
|
|
221
|
+
* It comes also for a route with no edge in the input, and for each problem of `checkPicture`.
|
|
222
|
+
*/
|
|
223
|
+
export function pictureOf(input: Pick<LayoutInput, "id" | "boxes" | "edges">, parts: PictureParts = {}): LayoutPicture {
|
|
224
|
+
const placed = input.boxes.filter((b) => b.x !== undefined && b.y !== undefined);
|
|
225
|
+
const ids = new Set(placed.map((b) => b.id));
|
|
226
|
+
const roots: LayoutBox[] = [];
|
|
227
|
+
const children = new Map<string, LayoutBox[]>();
|
|
228
|
+
for (const b of placed) {
|
|
229
|
+
if (b.parent === undefined) {
|
|
230
|
+
roots.push(b);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (!ids.has(b.parent)) throw new RangeError(`layout-picture: the box ${b.id} has a place, and its parent ${b.parent} has no place in the input.`);
|
|
234
|
+
const list = children.get(b.parent);
|
|
235
|
+
if (list === undefined) children.set(b.parent, [b]);
|
|
236
|
+
else list.push(b);
|
|
237
|
+
}
|
|
238
|
+
const boxes: PictureBox[] = [];
|
|
239
|
+
const stack = roots.reverse();
|
|
240
|
+
for (let b = stack.pop(); b !== undefined; b = stack.pop()) {
|
|
241
|
+
boxes.push(pictureBox(b, b.x!, b.y!));
|
|
242
|
+
const list = children.get(b.id);
|
|
243
|
+
if (list !== undefined) for (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!);
|
|
244
|
+
}
|
|
245
|
+
if (boxes.length < placed.length) throw new RangeError("layout-picture: the parents of the boxes of the input make a loop.");
|
|
246
|
+
const edges = new Map((input.edges ?? []).map((e) => [e.id, e]));
|
|
247
|
+
const routes = (parts.routes ?? []).map((r): PictureRoute => {
|
|
248
|
+
const edge = edges.get(r.edge);
|
|
249
|
+
if (edge === undefined) throw new RangeError(`layout-picture: the route ${r.edge} has no edge in the input.`);
|
|
250
|
+
return { id: edge.id, source: edge.source, target: edge.target, path: r.path ?? pathOf(r.points), ...(edge.data === undefined ? {} : { data: edge.data }) };
|
|
251
|
+
});
|
|
252
|
+
const picture: LayoutPicture = { schema: PICTURE_SCHEMA, id: input.id, boxes, routes, labels: [...(parts.labels ?? [])] };
|
|
253
|
+
const problems = checkPicture(picture);
|
|
254
|
+
if (problems.length > 0) throw new RangeError(`layout-picture: ${problems.join(", ")}.`);
|
|
255
|
+
return picture;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* This function gives the bounding box of a picture: its boxes, the exact bounds of its paths and its labels.
|
|
260
|
+
* A picture with no record gives `undefined`. The picture must be good (`checkPicture`).
|
|
261
|
+
*/
|
|
262
|
+
export function pictureBounds(picture: LayoutPicture): Rect | undefined {
|
|
263
|
+
let x0 = Infinity;
|
|
264
|
+
let y0 = Infinity;
|
|
265
|
+
let x1 = -Infinity;
|
|
266
|
+
let y1 = -Infinity;
|
|
267
|
+
const add = (r: Rect): void => {
|
|
268
|
+
x0 = Math.min(x0, r.x);
|
|
269
|
+
y0 = Math.min(y0, r.y);
|
|
270
|
+
x1 = Math.max(x1, r.x + r.w);
|
|
271
|
+
y1 = Math.max(y1, r.y + r.h);
|
|
272
|
+
};
|
|
273
|
+
for (const b of picture.boxes) add(b);
|
|
274
|
+
for (const r of picture.routes) add(pathBounds(r.path));
|
|
275
|
+
for (const l of picture.labels) add(l);
|
|
276
|
+
return x0 === Infinity ? undefined : { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };
|
|
277
|
+
}
|