alpha2 0.3.1 → 0.4.1

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/dist/layout.js ADDED
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Layout generators for authors: boxes laid out by CSS flexbox or grid around their text and
3
+ * shapes, as a project's `{ "type": "layout" }` generator holds them.
4
+ *
5
+ * {@link measureLayout} answers, in wasm and with the fonts the engine draws with, where every box
6
+ * of a layout lies on a picture and whether anything overflows, before anything is rendered. The
7
+ * builder ({@link box}, {@link text}, {@link shape}, {@link layout}) writes the document from
8
+ * CSS-like props: a value that is not animated becomes a constant, a `[top, right, bottom, left]`
9
+ * tuple the sides object, an `[x, y]` tuple a vector. Lengths are written as the document writes
10
+ * them: a number is a fraction of the picture's shorter side, `"12px"` pixels of the project
11
+ * canvas, `"50%"` a percent of the parent's content box. Text style (`font_family`, `font_size`,
12
+ * `color`, `line_height`, `text_align`, `outline`) may be set on any box and is inherited. Its
13
+ * output is a plain document that `validateProject` accepts; a CSS value string it cannot read
14
+ * throws, with the parser's message.
15
+ */
16
+ import { parseHex, parseInsets, parseLength, parseOffset, parsePlacement, parseSize, parseTrackList, } from "./layout-values.js";
17
+ /**
18
+ * Where every node of `generator` (a layout generator, or its root) lies on `picture` at picture
19
+ * time `time`, laid out with `binding`'s fonts as a frame lays it out. Measure at the size the
20
+ * picture is drawn at: at another size, lines can break elsewhere. The layout's `"<n>px"` lengths
21
+ * are pixels of `canvas`, the project canvas: `picture` itself by default, so a preview measured
22
+ * at half size passes the project's own size here.
23
+ *
24
+ * @throws An `INVALID_INPUT` error for a layout the model rejects (on that canvas), a picture or
25
+ * canvas outside 1 to 16384 pixels a side, or a time that is not whole ticks; a text error for a
26
+ * measurer without fonts when the layout has text.
27
+ */
28
+ export function measureLayout(binding, generator, picture, time = 0, canvas = picture) {
29
+ const document = "type" in generator && generator.type === "layout"
30
+ ? generator
31
+ : layout(generator);
32
+ return JSON.parse(binding.measureLayout(JSON.stringify(document), picture.width, picture.height, time, canvas.width, canvas.height));
33
+ }
34
+ /** A layout generator whose root is `root`: the whole picture. */
35
+ export function layout(root) {
36
+ return { type: "layout", root };
37
+ }
38
+ /** A box: a container holding `children`, or empty without them. */
39
+ export function box(props = {}, ...children) {
40
+ return children.length > 0
41
+ ? { ...boxFields(props), children }
42
+ : boxFields(props);
43
+ }
44
+ /**
45
+ * A box holding one paragraph of text, wrapped at its content width and drawn in the style it
46
+ * sets or inherits.
47
+ */
48
+ export function text(content, props = {}) {
49
+ return { ...boxFields(props), text: animated(content) };
50
+ }
51
+ /**
52
+ * A box holding shapes, drawn on a picture the size of its content box: positions normalized to
53
+ * it, lengths fractions of its shorter side. It has no size of its own: give it `width` and
54
+ * `height`, an `aspect_ratio`, or `flex_grow`.
55
+ */
56
+ export function shape(items, props = {}) {
57
+ return { ...boxFields(props), shape: { items } };
58
+ }
59
+ function isAnimated(value) {
60
+ return (typeof value === "object" &&
61
+ value !== null &&
62
+ "mode" in value &&
63
+ (value.mode === "constant" || value.mode === "keyframes"));
64
+ }
65
+ function animated(value) {
66
+ return isAnimated(value) ? value : { mode: "constant", value };
67
+ }
68
+ /** `value` when `parse` reads it; otherwise the parser's message, thrown. */
69
+ function checked(value, parsed) {
70
+ if (!parsed.ok)
71
+ throw new TypeError(parsed.error);
72
+ return value;
73
+ }
74
+ /** A length's or size's string checked as the engine reads it; numbers as they are. */
75
+ function lengthValue(value, percent) {
76
+ if (typeof value !== "string")
77
+ return value;
78
+ return checked(value, percent ? parseLength(value) : parseSize(value));
79
+ }
80
+ function sidesValue(value) {
81
+ if (typeof value === "number")
82
+ return value;
83
+ if (typeof value === "string")
84
+ return checked(value, parseInsets(value));
85
+ if (Array.isArray(value)) {
86
+ const [top, right, bottom, left] = value.map((side) => lengthValue(side, false));
87
+ return { top, right, bottom, left };
88
+ }
89
+ return Object.fromEntries(Object.entries(value).map(([side, size]) => [
90
+ side,
91
+ lengthValue(size, false),
92
+ ]));
93
+ }
94
+ function fillValue(value) {
95
+ return typeof value === "string" ? checked(value, parseHex(value)) : value;
96
+ }
97
+ function offsetValue(value) {
98
+ if (typeof value === "string")
99
+ return checked(value, parseOffset(value));
100
+ if (Array.isArray(value)) {
101
+ const [x, y] = value;
102
+ return { x: lengthValue(x, false), y: lengthValue(y, false) };
103
+ }
104
+ const { x, y } = value;
105
+ return { x: lengthValue(x, false), y: lengthValue(y, false) };
106
+ }
107
+ /** `value` as an animated field, each of its values mapped by `map` when it is not animated. */
108
+ function field(value, map) {
109
+ return isAnimated(value) ? value : { mode: "constant", value: map(value) };
110
+ }
111
+ function scaleField(value) {
112
+ if (typeof value === "number") {
113
+ return { mode: "constant", value: { x: value, y: value } };
114
+ }
115
+ if (Array.isArray(value)) {
116
+ const [x, y] = value;
117
+ return { mode: "constant", value: { x, y } };
118
+ }
119
+ return animated(value);
120
+ }
121
+ /** `props` as the document's fields, each shorthand written out; absent fields stay absent. */
122
+ function boxFields(props) {
123
+ const fields = {};
124
+ for (const [name, value] of Object.entries(props)) {
125
+ if (value === undefined)
126
+ continue;
127
+ switch (name) {
128
+ case "flex_grow":
129
+ case "flex_shrink":
130
+ case "opacity":
131
+ case "rotation_degrees":
132
+ fields[name] = animated(value);
133
+ break;
134
+ case "gap":
135
+ case "row_gap":
136
+ case "column_gap":
137
+ case "border_radius":
138
+ case "font_size":
139
+ fields[name] = field(value, (size) => lengthValue(size, false));
140
+ break;
141
+ case "padding":
142
+ case "margin":
143
+ fields[name] = field(value, sidesValue);
144
+ break;
145
+ case "background":
146
+ fields[name] = field(value, fillValue);
147
+ break;
148
+ case "border": {
149
+ const border = value;
150
+ fields[name] = {
151
+ width: field(border.width, (size) => lengthValue(size, false)),
152
+ paint: field(border.paint, fillValue),
153
+ };
154
+ break;
155
+ }
156
+ case "width":
157
+ case "height":
158
+ case "min_width":
159
+ case "min_height":
160
+ case "max_width":
161
+ case "max_height":
162
+ case "flex_basis":
163
+ fields[name] = field(value, (length) => lengthValue(length, true));
164
+ break;
165
+ case "inset":
166
+ fields[name] = Object.fromEntries(Object.entries(value)
167
+ .filter(([, side]) => side !== undefined)
168
+ .map(([side, length]) => [
169
+ side,
170
+ field(length, (inset) => lengthValue(inset, true)),
171
+ ]));
172
+ break;
173
+ case "grid_template_columns":
174
+ case "grid_template_rows":
175
+ case "grid_auto_columns":
176
+ case "grid_auto_rows":
177
+ fields[name] = checked(value, parseTrackList(value));
178
+ break;
179
+ case "grid_column":
180
+ case "grid_row":
181
+ fields[name] = checked(value, parsePlacement(value));
182
+ break;
183
+ case "color":
184
+ fields[name] = fillValue(value);
185
+ break;
186
+ case "translate":
187
+ fields[name] = field(value, offsetValue);
188
+ break;
189
+ case "scale":
190
+ fields[name] = scaleField(value);
191
+ break;
192
+ default:
193
+ // CSS keywords, the aspect ratio and the plain style values are written as they are;
194
+ // anything else the model refuses by name.
195
+ fields[name] = value;
196
+ }
197
+ }
198
+ return fields;
199
+ }
package/dist/project.d.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * Timeline values are integer ticks at {@link TICKS_PER_SECOND}. Ranges are
15
15
  * half-open: `[start, end)`.
16
16
  */
17
- import type { Animated, Easing, FrameRate, Interpolation, MediaTime, Project } from "./generated/project-schema.js";
17
+ import type { Animated, Easing, FrameRate, Insets, Interpolation, LayoutOffset, LayoutSize, MediaTime, Paint, Project, Rgba } from "./generated/project-schema.js";
18
18
  import { MAX_JSON_DEPTH } from "./json-text.js";
19
19
  export * from "./generated/project-schema.js";
20
20
  /** The deepest nesting of arrays and objects the engine reads in a project (serde_json's limit). */
@@ -136,6 +136,61 @@ export declare function validateProjectJson(json: string): string[];
136
136
  * {@link TICKS_PER_SECOND} frames per second).
137
137
  */
138
138
  export declare function validFrameRate(rate: unknown): rate is FrameRate;
139
+ /** A color as the engine writes it: the `{ red, green, blue, alpha }` object, never hex. */
140
+ export type RgbaObject = Exclude<Rgba, string>;
141
+ /** A paint as the engine writes it: tagged, its colors objects. */
142
+ export type CanonicalPaint = CanonicalPart<Exclude<Paint, Rgba>>;
143
+ /** Padding or margin sides as the engine writes them: all four, each a length. */
144
+ export interface CanonicalInsets {
145
+ readonly top: LayoutSize;
146
+ readonly right: LayoutSize;
147
+ readonly bottom: LayoutSize;
148
+ readonly left: LayoutSize;
149
+ }
150
+ /** An offset (a layout node's `translate`) as the engine writes it: `{ x, y }`. */
151
+ export type CanonicalOffset = Exclude<LayoutOffset, string>;
152
+ type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
153
+ /**
154
+ * `T`, a part of a project document, in the forms the engine writes: each color an
155
+ * {@link RgbaObject}, each paint a {@link CanonicalPaint}, each padding or margin
156
+ * {@link CanonicalInsets}, each offset a {@link CanonicalOffset}. What {@link canonicalProject}
157
+ * returns for a `Project`. Lengths and grid values keep the CSS spelling they were written in.
158
+ */
159
+ export type Canonical<T> = CanonicalValue<Exclude<T, null | undefined>> | Extract<T, null | undefined>;
160
+ type CanonicalValue<T> = [T] extends [never] ? never : Same<T, Rgba> extends true ? RgbaObject : Same<T, Paint> extends true ? CanonicalPaint : Same<T, Insets> extends true ? CanonicalInsets : Same<T, LayoutOffset> extends true ? CanonicalOffset : CanonicalPart<T>;
161
+ type CanonicalPart<T> = T extends string | number | boolean | bigint ? T : {
162
+ readonly [K in keyof T]: Canonical<T[K]>;
163
+ };
164
+ /** A project document in the forms the engine writes ({@link Canonical}). */
165
+ export type CanonicalProject = Canonical<Project>;
166
+ /**
167
+ * `color` as the engine writes it: a CSS hex string becomes the components object, each
168
+ * component the 32-bit value the engine reads, spelled shortest.
169
+ *
170
+ * @throws A `TypeError` for a string that is not CSS hex.
171
+ */
172
+ export declare function canonicalRgba(color: Rgba): RgbaObject;
173
+ /**
174
+ * `paint` as the engine writes it: a color (hex or object) becomes a solid paint of it, and
175
+ * every color in it the components object.
176
+ *
177
+ * @throws A `TypeError` for a color string that is not CSS hex.
178
+ */
179
+ export declare function canonicalPaint(paint: Paint): CanonicalPaint;
180
+ /**
181
+ * `project` in the forms the engine writes it back ({@link Canonical}): every color the
182
+ * components object, every paint tagged, every padding and margin the four sides, every offset
183
+ * `{ x, y }`. The forms the engine reads but never writes (CSS hex, a color as a paint, the
184
+ * sides shorthand, a `translate` string) are gone, so code that reads the document can take the
185
+ * one form. A value it cannot read (a string that is not hex, say) stays as it is, for
186
+ * {@link validateProject} to report: give it a document that validates to get a document in
187
+ * canonical form.
188
+ *
189
+ * The input is not changed. A part that is already canonical comes back as the same object, so
190
+ * a document in canonical form is returned itself, and a frozen part met before is not walked
191
+ * again: run on a changed copy of a document read before, it costs the parts that changed.
192
+ */
193
+ export declare function canonicalProject(project: Project): CanonicalProject;
139
194
  /** What a {@link LintIssue} is about. */
140
195
  export type LintOwner = {
141
196
  readonly type: "clip";