@pairdraw/pen 0.0.0-stage → 0.16.0
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/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/chunks/annotations.js +510 -0
- package/dist/chunks/annotations.js.map +1 -0
- package/dist/chunks/assets.js +158 -0
- package/dist/chunks/assets.js.map +1 -0
- package/dist/chunks/effects.js +775 -0
- package/dist/chunks/effects.js.map +1 -0
- package/dist/chunks/geometry.js +95 -0
- package/dist/chunks/geometry.js.map +1 -0
- package/dist/chunks/icons.js +156 -0
- package/dist/chunks/icons.js.map +1 -0
- package/dist/chunks/imports.js +120 -0
- package/dist/chunks/imports.js.map +1 -0
- package/dist/chunks/index.js +1478 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/index.d.ts +492 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/package.json +31 -4
- package/src/annotations.ts +128 -0
- package/src/assets.ts +224 -0
- package/src/effects.ts +62 -0
- package/src/geometry.ts +71 -0
- package/src/icons.ts +173 -0
- package/src/images.ts +109 -0
- package/src/imports.ts +169 -0
- package/src/index.ts +23 -0
- package/src/instances.ts +272 -0
- package/src/jsonc.ts +77 -0
- package/src/layout.ts +135 -0
- package/src/library.ts +153 -0
- package/src/nodes.ts +410 -0
- package/src/paints.ts +417 -0
- package/src/placeholders.ts +86 -0
- package/src/placement.ts +84 -0
- package/src/plugin-data.ts +86 -0
- package/src/read.ts +150 -0
- package/src/schema.ts +299 -0
- package/src/slots.ts +41 -0
- package/src/text.ts +92 -0
- package/src/variables.ts +284 -0
- package/src/write.ts +594 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { bindIfVar, resolveBoolean, resolveColor, resolveNumber } from "./annotations.js";
|
|
2
|
+
import { BLACK, TRANSPARENT } from "@pairdraw/scene-graph/constants";
|
|
3
|
+
import * as v from "valibot";
|
|
4
|
+
import { computeImageHash, coverImageTransform, imageSize } from "@pairdraw/scene-graph/images";
|
|
5
|
+
import { ellipticalGradientTransform, isMeshFill, linearGradientTransformFromAxes, meshPointCount } from "@pairdraw/scene-graph/gradient";
|
|
6
|
+
import { createShaderSourcePaint, withShaderPaints } from "@pairdraw/scene-graph/shaders";
|
|
7
|
+
//#region src/images.ts
|
|
8
|
+
/** Stores the images a host loaded, by the URLs paints name them with; others stay unpainted. */
|
|
9
|
+
function createImageStore(graph, files = /* @__PURE__ */ new Map()) {
|
|
10
|
+
const byURL = /* @__PURE__ */ new Map();
|
|
11
|
+
const store = (bytes) => {
|
|
12
|
+
const hash = computeImageHash(bytes);
|
|
13
|
+
graph.images.set(hash, bytes);
|
|
14
|
+
const size = imageSize(bytes);
|
|
15
|
+
return {
|
|
16
|
+
hash,
|
|
17
|
+
size: size && size.width > 0 && size.height > 0 ? size : null
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
return { get(url) {
|
|
21
|
+
if (byURL.has(url)) return byURL.get(url);
|
|
22
|
+
const file = files.get(url);
|
|
23
|
+
const image = file ? store(file) : void 0;
|
|
24
|
+
byURL.set(url, image);
|
|
25
|
+
return image;
|
|
26
|
+
} };
|
|
27
|
+
}
|
|
28
|
+
/** pen.dev's affine `[a, b, c, d, tx, ty]`, inverted, as the scene graph's matrix. */
|
|
29
|
+
function invertAffine([a, b, c, d, tx, ty]) {
|
|
30
|
+
const det = a * d - b * c;
|
|
31
|
+
if (Math.abs(det) < 1e-12) return null;
|
|
32
|
+
return {
|
|
33
|
+
m00: d / det,
|
|
34
|
+
m01: -c / det,
|
|
35
|
+
m02: (c * ty - d * tx) / det,
|
|
36
|
+
m10: -b / det,
|
|
37
|
+
m11: a / det,
|
|
38
|
+
m12: (b * tx - a * ty) / det
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function compose(outer, inner) {
|
|
42
|
+
return {
|
|
43
|
+
m00: outer.m00 * inner.m00 + outer.m01 * inner.m10,
|
|
44
|
+
m01: outer.m00 * inner.m01 + outer.m01 * inner.m11,
|
|
45
|
+
m02: outer.m00 * inner.m02 + outer.m01 * inner.m12 + outer.m02,
|
|
46
|
+
m10: outer.m10 * inner.m00 + outer.m11 * inner.m10,
|
|
47
|
+
m11: outer.m10 * inner.m01 + outer.m11 * inner.m11,
|
|
48
|
+
m12: outer.m10 * inner.m02 + outer.m11 * inner.m12 + outer.m12
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const IDENTITY = {
|
|
52
|
+
m00: 1,
|
|
53
|
+
m01: 0,
|
|
54
|
+
m02: 0,
|
|
55
|
+
m10: 0,
|
|
56
|
+
m11: 1,
|
|
57
|
+
m12: 0
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Where an image paint draws. `cover` and `contain` are Fill and Fit, and `stretch` a crop of the
|
|
61
|
+
* whole image. pen.dev's `transform` crops the image first and fits the crop as it would a whole
|
|
62
|
+
* image; the scene graph has that as a crop of the image's window, which needs the image's size
|
|
63
|
+
* when the crop is covered. A crop fitted with `contain` is drawn covered, since a crop cannot
|
|
64
|
+
* leave the bars `contain` shows beside it.
|
|
65
|
+
*/
|
|
66
|
+
function placement(paint, image, box) {
|
|
67
|
+
const mode = paint.mode ?? "cover";
|
|
68
|
+
const crop = paint.transform ? invertAffine(paint.transform) : null;
|
|
69
|
+
if (!crop) {
|
|
70
|
+
if (mode === "stretch") return {
|
|
71
|
+
imageScaleMode: "CROP",
|
|
72
|
+
imageTransform: IDENTITY
|
|
73
|
+
};
|
|
74
|
+
return { imageScaleMode: mode === "contain" ? "FIT" : "FILL" };
|
|
75
|
+
}
|
|
76
|
+
if (mode === "stretch" || !image.size) return {
|
|
77
|
+
imageScaleMode: "CROP",
|
|
78
|
+
imageTransform: crop
|
|
79
|
+
};
|
|
80
|
+
const cropWidth = image.size.width * Math.hypot(crop.m00, crop.m10);
|
|
81
|
+
const cropHeight = image.size.height * Math.hypot(crop.m01, crop.m11);
|
|
82
|
+
return {
|
|
83
|
+
imageScaleMode: "CROP",
|
|
84
|
+
imageTransform: compose(crop, coverImageTransform(box.width, box.height, cropWidth, cropHeight))
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function imagePaint(paint, image, box, opacity) {
|
|
88
|
+
return {
|
|
89
|
+
type: "IMAGE",
|
|
90
|
+
color: { ...BLACK },
|
|
91
|
+
opacity,
|
|
92
|
+
visible: true,
|
|
93
|
+
imageHash: image.hash,
|
|
94
|
+
...placement(paint, image, box)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/jsonc.ts
|
|
99
|
+
/**
|
|
100
|
+
* pen.dev accepts JSON with comments in `.pen` files. This turns such text into plain JSON: `//`
|
|
101
|
+
* and block comments outside strings become spaces, keeping line breaks so a parse error still
|
|
102
|
+
* points where it did, and a comma before a closing bracket or brace becomes a space, as readers
|
|
103
|
+
* of JSON with comments allow. Text without comments or such commas comes back unchanged.
|
|
104
|
+
*/
|
|
105
|
+
function stripJSONComments(text) {
|
|
106
|
+
if (!text.includes("/") && !text.includes(",")) return text;
|
|
107
|
+
const parts = [];
|
|
108
|
+
let copied = 0;
|
|
109
|
+
let index = 0;
|
|
110
|
+
const skipTo = (end, replacement) => {
|
|
111
|
+
parts.push(text.slice(copied, index), replacement);
|
|
112
|
+
copied = end;
|
|
113
|
+
index = end;
|
|
114
|
+
};
|
|
115
|
+
while (index < text.length) {
|
|
116
|
+
const char = text[index];
|
|
117
|
+
const next = text[index + 1];
|
|
118
|
+
if (char === "\"") index = stringEnd(text, index);
|
|
119
|
+
else if (char === "/" && next === "/") {
|
|
120
|
+
const newline = text.indexOf("\n", index);
|
|
121
|
+
const end = newline === -1 ? text.length : newline;
|
|
122
|
+
skipTo(end, blank(text.slice(index, end)));
|
|
123
|
+
} else if (char === "/" && next === "*") {
|
|
124
|
+
const close = text.indexOf("*/", index + 2);
|
|
125
|
+
const end = close === -1 ? text.length : close + 2;
|
|
126
|
+
skipTo(end, blank(text.slice(index, end)));
|
|
127
|
+
} else if (char === "," && closesNext(text, index + 1)) skipTo(index + 1, " ");
|
|
128
|
+
else index++;
|
|
129
|
+
}
|
|
130
|
+
parts.push(text.slice(copied));
|
|
131
|
+
return parts.join("");
|
|
132
|
+
}
|
|
133
|
+
/** The end of the string literal opening at `start`, past its closing quote. */
|
|
134
|
+
function stringEnd(text, start) {
|
|
135
|
+
let index = start + 1;
|
|
136
|
+
while (index < text.length) {
|
|
137
|
+
const char = text[index];
|
|
138
|
+
if (char === "\\") index += 2;
|
|
139
|
+
else if (char === "\"") return index + 1;
|
|
140
|
+
else index++;
|
|
141
|
+
}
|
|
142
|
+
return text.length;
|
|
143
|
+
}
|
|
144
|
+
/** Spaces in place of every character of `comment` but its line breaks. */
|
|
145
|
+
function blank(comment) {
|
|
146
|
+
return comment.replace(/[^\r\n]/g, " ");
|
|
147
|
+
}
|
|
148
|
+
/** Whether a closing bracket or brace comes next, past whitespace and comments. */
|
|
149
|
+
function closesNext(text, from) {
|
|
150
|
+
let index = from;
|
|
151
|
+
while (index < text.length) {
|
|
152
|
+
const char = text[index] ?? "";
|
|
153
|
+
if (/\s/.test(char)) index++;
|
|
154
|
+
else if (char === "/" && text[index + 1] === "/") {
|
|
155
|
+
const newline = text.indexOf("\n", index);
|
|
156
|
+
if (newline === -1) return false;
|
|
157
|
+
index = newline;
|
|
158
|
+
} else if (char === "/" && text[index + 1] === "*") {
|
|
159
|
+
const close = text.indexOf("*/", index + 2);
|
|
160
|
+
if (close === -1) return false;
|
|
161
|
+
index = close + 2;
|
|
162
|
+
} else return char === "}" || char === "]";
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/schema.ts
|
|
168
|
+
/**
|
|
169
|
+
* The `.pen` document as pen.dev writes it (schema 2.21), plus the older forms still found in
|
|
170
|
+
* files: the object `stroke` of 2.8 and `icon_font` nodes. Properties typed `…OrVariable` hold a
|
|
171
|
+
* `$name` reference in place of the value. Enumerated values stay strings so a value this reader
|
|
172
|
+
* does not know falls back to the default instead of rejecting the document.
|
|
173
|
+
*/
|
|
174
|
+
const numSchema = v.union([v.number(), v.string()]);
|
|
175
|
+
const boolSchema = v.union([v.boolean(), v.string()]);
|
|
176
|
+
const strSchema = v.string();
|
|
177
|
+
const sidesSchema = v.looseObject({
|
|
178
|
+
top: v.optional(numSchema),
|
|
179
|
+
right: v.optional(numSchema),
|
|
180
|
+
bottom: v.optional(numSchema),
|
|
181
|
+
left: v.optional(numSchema)
|
|
182
|
+
});
|
|
183
|
+
const paintBase = {
|
|
184
|
+
enabled: v.optional(boolSchema),
|
|
185
|
+
blendMode: v.optional(strSchema)
|
|
186
|
+
};
|
|
187
|
+
const colorPaintSchema = v.looseObject({
|
|
188
|
+
type: v.literal("color"),
|
|
189
|
+
...paintBase,
|
|
190
|
+
color: strSchema
|
|
191
|
+
});
|
|
192
|
+
const gradientPaintSchema = v.looseObject({
|
|
193
|
+
type: v.literal("gradient"),
|
|
194
|
+
...paintBase,
|
|
195
|
+
gradientType: v.optional(strSchema),
|
|
196
|
+
opacity: v.optional(numSchema),
|
|
197
|
+
center: v.optional(v.looseObject({
|
|
198
|
+
x: v.optional(v.number()),
|
|
199
|
+
y: v.optional(v.number())
|
|
200
|
+
})),
|
|
201
|
+
size: v.optional(v.looseObject({
|
|
202
|
+
width: v.optional(numSchema),
|
|
203
|
+
height: v.optional(numSchema)
|
|
204
|
+
})),
|
|
205
|
+
rotation: v.optional(numSchema),
|
|
206
|
+
colors: v.optional(v.array(v.looseObject({
|
|
207
|
+
color: strSchema,
|
|
208
|
+
position: numSchema
|
|
209
|
+
})))
|
|
210
|
+
});
|
|
211
|
+
const imagePaintSchema = v.looseObject({
|
|
212
|
+
type: v.literal("image"),
|
|
213
|
+
...paintBase,
|
|
214
|
+
opacity: v.optional(numSchema),
|
|
215
|
+
url: v.optional(strSchema),
|
|
216
|
+
mode: v.optional(strSchema),
|
|
217
|
+
transform: v.optional(v.tuple([
|
|
218
|
+
v.number(),
|
|
219
|
+
v.number(),
|
|
220
|
+
v.number(),
|
|
221
|
+
v.number(),
|
|
222
|
+
v.number(),
|
|
223
|
+
v.number()
|
|
224
|
+
]))
|
|
225
|
+
});
|
|
226
|
+
/**
|
|
227
|
+
* A `mesh_gradient` paint, as the `.pen` reference documents it: one color per vertex and one
|
|
228
|
+
* point per vertex in [0,1], `columns * rows` of them. Bezier handles ride on the points and do
|
|
229
|
+
* not change the grid this reader draws, so they pass through as unknowns.
|
|
230
|
+
*/
|
|
231
|
+
const meshPaintSchema = v.looseObject({
|
|
232
|
+
type: v.literal("mesh_gradient"),
|
|
233
|
+
...paintBase,
|
|
234
|
+
opacity: v.optional(numSchema),
|
|
235
|
+
columns: v.optional(numSchema),
|
|
236
|
+
rows: v.optional(numSchema),
|
|
237
|
+
colors: v.optional(v.array(v.unknown())),
|
|
238
|
+
points: v.optional(v.array(v.unknown()))
|
|
239
|
+
});
|
|
240
|
+
/**
|
|
241
|
+
* A `shader` paint: `url` names the shader file beside the document, whose source `PenReadOptions`
|
|
242
|
+
* gives by that URL. One whose source the host did not read draws nothing, as pen.dev shows a
|
|
243
|
+
* shader it cannot compile.
|
|
244
|
+
*/
|
|
245
|
+
const shaderPaintSchema = v.looseObject({
|
|
246
|
+
type: v.literal("shader"),
|
|
247
|
+
...paintBase,
|
|
248
|
+
opacity: v.optional(numSchema),
|
|
249
|
+
url: v.optional(strSchema)
|
|
250
|
+
});
|
|
251
|
+
/** Paint types this reader does not draw. */
|
|
252
|
+
const otherPaintSchema = v.looseObject({
|
|
253
|
+
type: strSchema,
|
|
254
|
+
...paintBase
|
|
255
|
+
});
|
|
256
|
+
const paintSchema = v.union([
|
|
257
|
+
strSchema,
|
|
258
|
+
colorPaintSchema,
|
|
259
|
+
gradientPaintSchema,
|
|
260
|
+
imagePaintSchema,
|
|
261
|
+
meshPaintSchema,
|
|
262
|
+
shaderPaintSchema,
|
|
263
|
+
otherPaintSchema
|
|
264
|
+
]);
|
|
265
|
+
const paintsSchema = v.union([paintSchema, v.array(paintSchema)]);
|
|
266
|
+
/** The 2.8 stroke: one colour with its weight and placement in one object. */
|
|
267
|
+
const legacyStrokeSchema = v.looseObject({
|
|
268
|
+
thickness: v.union([numSchema, sidesSchema]),
|
|
269
|
+
fill: v.optional(strSchema),
|
|
270
|
+
align: v.optional(strSchema),
|
|
271
|
+
join: v.optional(strSchema),
|
|
272
|
+
cap: v.optional(strSchema)
|
|
273
|
+
});
|
|
274
|
+
const effectSchema = v.looseObject({
|
|
275
|
+
type: strSchema,
|
|
276
|
+
enabled: v.optional(boolSchema),
|
|
277
|
+
radius: v.optional(numSchema),
|
|
278
|
+
shadowType: v.optional(strSchema),
|
|
279
|
+
offset: v.optional(v.looseObject({
|
|
280
|
+
x: numSchema,
|
|
281
|
+
y: numSchema
|
|
282
|
+
})),
|
|
283
|
+
blur: v.optional(numSchema),
|
|
284
|
+
/** Written by older files; current pen.dev shadows have no spread. */
|
|
285
|
+
spread: v.optional(numSchema),
|
|
286
|
+
color: v.optional(strSchema),
|
|
287
|
+
blendMode: v.optional(strSchema),
|
|
288
|
+
/** The shader file a `shader` effect names, read by `PenReadOptions.shaders`. */
|
|
289
|
+
url: v.optional(strSchema)
|
|
290
|
+
});
|
|
291
|
+
const sizeSchema = numSchema;
|
|
292
|
+
const paddingSchema = v.union([
|
|
293
|
+
numSchema,
|
|
294
|
+
v.tuple([numSchema, numSchema]),
|
|
295
|
+
v.tuple([
|
|
296
|
+
numSchema,
|
|
297
|
+
numSchema,
|
|
298
|
+
numSchema,
|
|
299
|
+
numSchema
|
|
300
|
+
])
|
|
301
|
+
]);
|
|
302
|
+
const cornerRadiusSchema = v.union([numSchema, v.array(numSchema)]);
|
|
303
|
+
/** Every property a node, or an override of one, can carry. */
|
|
304
|
+
const nodeFields = {
|
|
305
|
+
name: v.optional(strSchema),
|
|
306
|
+
context: v.optional(strSchema),
|
|
307
|
+
/**
|
|
308
|
+
* What a layer is, for agents: pen.dev writes a `type` and any fields of its own. It is only kept
|
|
309
|
+
* as plugin data, so any value is read as it is rather than rejecting the document.
|
|
310
|
+
*/
|
|
311
|
+
metadata: v.optional(v.unknown()),
|
|
312
|
+
x: v.optional(v.number()),
|
|
313
|
+
y: v.optional(v.number()),
|
|
314
|
+
width: v.optional(sizeSchema),
|
|
315
|
+
height: v.optional(sizeSchema),
|
|
316
|
+
reusable: v.optional(v.boolean()),
|
|
317
|
+
theme: v.optional(v.record(strSchema, strSchema)),
|
|
318
|
+
enabled: v.optional(boolSchema),
|
|
319
|
+
opacity: v.optional(numSchema),
|
|
320
|
+
flipX: v.optional(boolSchema),
|
|
321
|
+
flipY: v.optional(boolSchema),
|
|
322
|
+
layoutPosition: v.optional(strSchema),
|
|
323
|
+
rotation: v.optional(numSchema),
|
|
324
|
+
fill: v.optional(paintsSchema),
|
|
325
|
+
stroke: v.optional(v.union([paintsSchema, legacyStrokeSchema])),
|
|
326
|
+
strokeWidth: v.optional(v.union([numSchema, sidesSchema])),
|
|
327
|
+
strokeLinecap: v.optional(strSchema),
|
|
328
|
+
strokeLinejoin: v.optional(strSchema),
|
|
329
|
+
strokeAlignment: v.optional(strSchema),
|
|
330
|
+
effect: v.optional(v.union([effectSchema, v.array(effectSchema)])),
|
|
331
|
+
cornerRadius: v.optional(cornerRadiusSchema),
|
|
332
|
+
layout: v.optional(strSchema),
|
|
333
|
+
gap: v.optional(numSchema),
|
|
334
|
+
layoutIncludeStroke: v.optional(v.boolean()),
|
|
335
|
+
padding: v.optional(paddingSchema),
|
|
336
|
+
justifyContent: v.optional(strSchema),
|
|
337
|
+
alignItems: v.optional(strSchema),
|
|
338
|
+
clip: v.optional(boolSchema),
|
|
339
|
+
placeholder: v.optional(v.boolean()),
|
|
340
|
+
slot: v.optional(v.union([v.literal(false), v.array(strSchema)])),
|
|
341
|
+
innerRadius: v.optional(numSchema),
|
|
342
|
+
startAngle: v.optional(numSchema),
|
|
343
|
+
sweepAngle: v.optional(numSchema),
|
|
344
|
+
polygonCount: v.optional(numSchema),
|
|
345
|
+
geometry: v.optional(strSchema),
|
|
346
|
+
fillRule: v.optional(strSchema),
|
|
347
|
+
viewBox: v.optional(v.tuple([
|
|
348
|
+
v.number(),
|
|
349
|
+
v.number(),
|
|
350
|
+
v.number(),
|
|
351
|
+
v.number()
|
|
352
|
+
])),
|
|
353
|
+
content: v.optional(strSchema),
|
|
354
|
+
textGrowth: v.optional(strSchema),
|
|
355
|
+
fontFamily: v.optional(strSchema),
|
|
356
|
+
fontSize: v.optional(numSchema),
|
|
357
|
+
fontWeight: v.optional(numSchema),
|
|
358
|
+
fontStyle: v.optional(strSchema),
|
|
359
|
+
letterSpacing: v.optional(numSchema),
|
|
360
|
+
lineHeight: v.optional(numSchema),
|
|
361
|
+
textAlign: v.optional(strSchema),
|
|
362
|
+
textAlignVertical: v.optional(strSchema),
|
|
363
|
+
underline: v.optional(boolSchema),
|
|
364
|
+
strikethrough: v.optional(boolSchema),
|
|
365
|
+
href: v.optional(v.nullable(strSchema)),
|
|
366
|
+
library: v.optional(strSchema),
|
|
367
|
+
icon: v.optional(strSchema),
|
|
368
|
+
weight: v.optional(numSchema),
|
|
369
|
+
iconFontName: v.optional(strSchema),
|
|
370
|
+
iconFontFamily: v.optional(strSchema),
|
|
371
|
+
ref: v.optional(strSchema),
|
|
372
|
+
/** The model a `prompt` node asks. */
|
|
373
|
+
model: v.optional(strSchema),
|
|
374
|
+
/** A `script` node's JavaScript file, relative to the document. */
|
|
375
|
+
scriptUri: v.optional(strSchema),
|
|
376
|
+
/** A `script` node's input values by name. */
|
|
377
|
+
inputs: v.optional(v.record(strSchema, v.unknown())),
|
|
378
|
+
/** The page a `browser` node shows; empty asks for one. */
|
|
379
|
+
url: v.optional(strSchema),
|
|
380
|
+
/** The device a `browser` node emulates; responsive without one. */
|
|
381
|
+
deviceId: v.optional(strSchema),
|
|
382
|
+
zoom: v.optional(numSchema),
|
|
383
|
+
scrollX: v.optional(numSchema),
|
|
384
|
+
scrollY: v.optional(numSchema)
|
|
385
|
+
};
|
|
386
|
+
const overrideSchema = v.looseObject({
|
|
387
|
+
...nodeFields,
|
|
388
|
+
type: v.optional(strSchema),
|
|
389
|
+
id: v.optional(strSchema),
|
|
390
|
+
children: v.optional(v.array(v.lazy(() => nodeSchema))),
|
|
391
|
+
descendants: v.optional(v.record(strSchema, v.lazy(() => overrideSchema)))
|
|
392
|
+
});
|
|
393
|
+
const nodeSchema = v.looseObject({
|
|
394
|
+
...nodeFields,
|
|
395
|
+
type: strSchema,
|
|
396
|
+
id: strSchema,
|
|
397
|
+
children: v.optional(v.array(v.lazy(() => nodeSchema))),
|
|
398
|
+
descendants: v.optional(v.record(strSchema, overrideSchema))
|
|
399
|
+
});
|
|
400
|
+
const variableScalarSchema = v.union([
|
|
401
|
+
v.string(),
|
|
402
|
+
v.number(),
|
|
403
|
+
v.boolean()
|
|
404
|
+
]);
|
|
405
|
+
const variableSchema = v.looseObject({
|
|
406
|
+
type: strSchema,
|
|
407
|
+
value: v.union([variableScalarSchema, v.array(v.looseObject({
|
|
408
|
+
value: variableScalarSchema,
|
|
409
|
+
theme: v.optional(v.record(strSchema, strSchema))
|
|
410
|
+
}))])
|
|
411
|
+
});
|
|
412
|
+
const penDocumentSchema = v.looseObject({
|
|
413
|
+
version: v.optional(strSchema),
|
|
414
|
+
themes: v.optional(v.record(strSchema, v.array(strSchema))),
|
|
415
|
+
imports: v.optional(v.record(strSchema, strSchema)),
|
|
416
|
+
variables: v.optional(v.record(strSchema, variableSchema)),
|
|
417
|
+
children: v.array(nodeSchema)
|
|
418
|
+
});
|
|
419
|
+
/** A `.pen` file's text, which may hold comments, as its document. */
|
|
420
|
+
const penFileSchema = v.pipe(v.string(), v.transform(stripJSONComments), v.parseJson(), penDocumentSchema);
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/paints.ts
|
|
423
|
+
/**
|
|
424
|
+
* pen.dev blend modes by name. Linear burn and linear dodge have no counterpart in the scene
|
|
425
|
+
* graph, so they draw as normal.
|
|
426
|
+
*/
|
|
427
|
+
const BLEND_MODES = {
|
|
428
|
+
normal: "NORMAL",
|
|
429
|
+
darken: "DARKEN",
|
|
430
|
+
multiply: "MULTIPLY",
|
|
431
|
+
colorBurn: "COLOR_BURN",
|
|
432
|
+
light: "LIGHTEN",
|
|
433
|
+
screen: "SCREEN",
|
|
434
|
+
colorDodge: "COLOR_DODGE",
|
|
435
|
+
overlay: "OVERLAY",
|
|
436
|
+
softLight: "SOFT_LIGHT",
|
|
437
|
+
hardLight: "HARD_LIGHT",
|
|
438
|
+
difference: "DIFFERENCE",
|
|
439
|
+
exclusion: "EXCLUSION",
|
|
440
|
+
hue: "HUE",
|
|
441
|
+
saturation: "SATURATION",
|
|
442
|
+
color: "COLOR",
|
|
443
|
+
luminosity: "LUMINOSITY"
|
|
444
|
+
};
|
|
445
|
+
function mapBlendMode(value) {
|
|
446
|
+
return value === void 0 ? void 0 : BLEND_MODES[value];
|
|
447
|
+
}
|
|
448
|
+
const GRADIENT_TYPES = {
|
|
449
|
+
linear: "GRADIENT_LINEAR",
|
|
450
|
+
radial: "GRADIENT_RADIAL",
|
|
451
|
+
angular: "GRADIENT_ANGULAR",
|
|
452
|
+
diamond: "GRADIENT_DIAMOND"
|
|
453
|
+
};
|
|
454
|
+
/**
|
|
455
|
+
* pen.dev places a gradient in the layer's unit square: `center` defaults to the middle, a linear
|
|
456
|
+
* gradient runs `size.height` long from bottom to top, and radial, diamond, and angular ones are
|
|
457
|
+
* ellipses of diameter `size`. `rotation` turns all of it counter-clockwise in that square. An
|
|
458
|
+
* angular gradient starts at the top and turns clockwise. Measured against pen.dev 2.21 renders.
|
|
459
|
+
* A diamond gradient uses the same ellipse as a radial one.
|
|
460
|
+
*/
|
|
461
|
+
function gradientTransform(paint, type, ctx) {
|
|
462
|
+
const center = {
|
|
463
|
+
x: paint.center?.x ?? .5,
|
|
464
|
+
y: paint.center?.y ?? .5
|
|
465
|
+
};
|
|
466
|
+
const width = resolveNumber(paint.size?.width, ctx, 1);
|
|
467
|
+
const height = resolveNumber(paint.size?.height, ctx, 1);
|
|
468
|
+
const angle = resolveNumber(paint.rotation, ctx) * Math.PI / 180;
|
|
469
|
+
const up = {
|
|
470
|
+
x: -Math.sin(angle),
|
|
471
|
+
y: -Math.cos(angle)
|
|
472
|
+
};
|
|
473
|
+
const right = {
|
|
474
|
+
x: Math.cos(angle),
|
|
475
|
+
y: -Math.sin(angle)
|
|
476
|
+
};
|
|
477
|
+
const along = (direction, length) => ({
|
|
478
|
+
x: center.x + direction.x * length,
|
|
479
|
+
y: center.y + direction.y * length
|
|
480
|
+
});
|
|
481
|
+
if (type === "GRADIENT_LINEAR") return linearGradientTransformFromAxes(along(up, -height / 2), along(up, height / 2), {
|
|
482
|
+
x: right.x * height,
|
|
483
|
+
y: right.y * height
|
|
484
|
+
});
|
|
485
|
+
if (type === "GRADIENT_ANGULAR") return ellipticalGradientTransform(center, along(up, height / 2), along(right, width / 2));
|
|
486
|
+
return ellipticalGradientTransform(center, along(right, width / 2), {
|
|
487
|
+
x: center.x - up.x * (height / 2),
|
|
488
|
+
y: center.y - up.y * (height / 2)
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
function gradientStops(paint, ctx) {
|
|
492
|
+
return (paint.colors ?? []).map((stop) => ({
|
|
493
|
+
color: resolveColor(stop.color, ctx),
|
|
494
|
+
position: resolveNumber(stop.position, ctx)
|
|
495
|
+
}));
|
|
496
|
+
}
|
|
497
|
+
function convertPaint(paint, index, field, ctx, node, images, shaders) {
|
|
498
|
+
if (typeof paint === "string" || paint.type === "color") {
|
|
499
|
+
const raw = typeof paint === "string" ? paint : paint.color;
|
|
500
|
+
if (typeof raw !== "string") return null;
|
|
501
|
+
const color = resolveColor(raw, ctx);
|
|
502
|
+
if (node) bindIfVar(node, `${field}/${index}/color`, raw, ctx);
|
|
503
|
+
return {
|
|
504
|
+
type: "SOLID",
|
|
505
|
+
color,
|
|
506
|
+
opacity: color.a,
|
|
507
|
+
visible: typeof paint === "string" || resolveBoolean(paint.enabled, ctx),
|
|
508
|
+
blendMode: typeof paint === "string" ? void 0 : mapBlendMode(paint.blendMode)
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
if (paint.type === "gradient") {
|
|
512
|
+
const gradient = paint;
|
|
513
|
+
const type = GRADIENT_TYPES[gradient.gradientType ?? "linear"];
|
|
514
|
+
if (!type) return null;
|
|
515
|
+
return {
|
|
516
|
+
type,
|
|
517
|
+
color: { ...TRANSPARENT },
|
|
518
|
+
opacity: resolveNumber(gradient.opacity, ctx, 1),
|
|
519
|
+
visible: resolveBoolean(gradient.enabled, ctx),
|
|
520
|
+
blendMode: mapBlendMode(gradient.blendMode),
|
|
521
|
+
gradientStops: gradientStops(gradient, ctx),
|
|
522
|
+
gradientTransform: gradientTransform(gradient, type, ctx)
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
if (paint.type === "image") return convertImagePaint(paint, ctx, node, images);
|
|
526
|
+
if (paint.type === "mesh_gradient") return convertMeshPaint(paint, ctx);
|
|
527
|
+
if (paint.type === "shader") return convertShaderPaint(paint, ctx, shaders);
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
/** A pen.dev image paint as the `IMAGE` fill that draws the image its URL names. */
|
|
531
|
+
function convertImagePaint(paint, ctx, node, images) {
|
|
532
|
+
const image = paint.url ? images?.get(paint.url) : void 0;
|
|
533
|
+
if (!image) return null;
|
|
534
|
+
return {
|
|
535
|
+
...imagePaint(paint, image, {
|
|
536
|
+
width: node?.width ?? 0,
|
|
537
|
+
height: node?.height ?? 0
|
|
538
|
+
}, resolveNumber(paint.opacity, ctx, 1)),
|
|
539
|
+
visible: resolveBoolean(paint.enabled, ctx),
|
|
540
|
+
blendMode: mapBlendMode(paint.blendMode)
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* A pen.dev shader paint as the shader paint it draws: its source comes from the file `url` names,
|
|
545
|
+
* which the host read as `PenReadOptions.shaders`. One whose source is missing draws nothing, as a
|
|
546
|
+
* shader pen.dev cannot compile shows an empty frame.
|
|
547
|
+
*/
|
|
548
|
+
function convertShaderPaint(paint, ctx, shaders) {
|
|
549
|
+
const source = paint.url === void 0 ? void 0 : shaders?.sources?.get(paint.url);
|
|
550
|
+
if (!source || !shaders) return null;
|
|
551
|
+
const { paint: fill, shader } = createShaderSourcePaint(source);
|
|
552
|
+
shaders.out.push(shader);
|
|
553
|
+
return {
|
|
554
|
+
...fill,
|
|
555
|
+
opacity: resolveNumber(paint.opacity, ctx, 1),
|
|
556
|
+
visible: resolveBoolean(paint.enabled, ctx),
|
|
557
|
+
blendMode: mapBlendMode(paint.blendMode)
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* A pen.dev mesh paint as a `MESH` fill. `columns` and `rows` count points per side, so the grid
|
|
562
|
+
* between them is one smaller each way; a file that counts cells instead still matches when its
|
|
563
|
+
* grid is one larger. Anything else is not a mesh this reader draws, so it is skipped.
|
|
564
|
+
*/
|
|
565
|
+
function convertMeshPaint(raw, ctx) {
|
|
566
|
+
const parsed = v.safeParse(meshPaintSchema, raw);
|
|
567
|
+
if (!parsed.success) return null;
|
|
568
|
+
const paint = parsed.output;
|
|
569
|
+
const columns = Math.max(1, Math.floor(resolveNumber(paint.columns, ctx, 1)));
|
|
570
|
+
const rows = Math.max(1, Math.floor(resolveNumber(paint.rows, ctx, 1)));
|
|
571
|
+
const points = meshPoints(paint.colors ?? [], paint.points ?? [], ctx);
|
|
572
|
+
if (!points) return null;
|
|
573
|
+
let meshColumns = columns;
|
|
574
|
+
let meshRows = rows;
|
|
575
|
+
if (points.length !== meshPointCount(meshColumns, meshRows)) {
|
|
576
|
+
if (columns > 1 && rows > 1 && points.length === columns * rows) {
|
|
577
|
+
meshColumns = columns - 1;
|
|
578
|
+
meshRows = rows - 1;
|
|
579
|
+
} else return null;
|
|
580
|
+
}
|
|
581
|
+
const fill = {
|
|
582
|
+
type: "MESH",
|
|
583
|
+
color: { ...TRANSPARENT },
|
|
584
|
+
opacity: resolveNumber(paint.opacity, ctx, 1),
|
|
585
|
+
visible: resolveBoolean(paint.enabled, ctx),
|
|
586
|
+
blendMode: mapBlendMode(paint.blendMode),
|
|
587
|
+
meshColumns,
|
|
588
|
+
meshRows,
|
|
589
|
+
meshPoints: points
|
|
590
|
+
};
|
|
591
|
+
return isMeshFill(fill) ? fill : null;
|
|
592
|
+
}
|
|
593
|
+
/** One mesh point per vertex: a color with the point's position. */
|
|
594
|
+
function meshPoints(colors, positions, ctx) {
|
|
595
|
+
if (colors.length === 0 || colors.length !== positions.length) return null;
|
|
596
|
+
const points = [];
|
|
597
|
+
for (let index = 0; index < colors.length; index++) {
|
|
598
|
+
const rawColor = colors[index];
|
|
599
|
+
const rawPosition = positions[index];
|
|
600
|
+
if (typeof rawColor !== "string") return null;
|
|
601
|
+
let position = rawPosition;
|
|
602
|
+
if (typeof rawPosition === "object" && rawPosition !== null && "position" in rawPosition) position = rawPosition.position;
|
|
603
|
+
if (!Array.isArray(position) || position.length < 2) return null;
|
|
604
|
+
const [x, y] = position;
|
|
605
|
+
if (typeof x !== "number" || typeof y !== "number") return null;
|
|
606
|
+
points.push({
|
|
607
|
+
position: {
|
|
608
|
+
x: resolveNumber(x, ctx),
|
|
609
|
+
y: resolveNumber(y, ctx)
|
|
610
|
+
},
|
|
611
|
+
color: resolveColor(rawColor, ctx)
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
return points;
|
|
615
|
+
}
|
|
616
|
+
/** A node's fills, or its strokes' paints, in pen.dev's order: the last one draws on top. */
|
|
617
|
+
function convertPaints(paints, field, ctx, node, images, shaders) {
|
|
618
|
+
if (paints === void 0) return [];
|
|
619
|
+
const list = Array.isArray(paints) ? paints : [paints];
|
|
620
|
+
const result = [];
|
|
621
|
+
const read = {
|
|
622
|
+
sources: shaders,
|
|
623
|
+
out: []
|
|
624
|
+
};
|
|
625
|
+
for (const paint of list) {
|
|
626
|
+
const converted = convertPaint(paint, result.length, field, ctx, node, images, read);
|
|
627
|
+
if (converted) result.push(converted);
|
|
628
|
+
}
|
|
629
|
+
if (node && read.out.length > 0) {
|
|
630
|
+
const other = field === "fills" ? node.strokes : node.fills;
|
|
631
|
+
node.pluginData = withShaderPaints(node, [...result, ...other], read.out);
|
|
632
|
+
}
|
|
633
|
+
return result;
|
|
634
|
+
}
|
|
635
|
+
const ALIGNMENTS = {
|
|
636
|
+
inner: "INSIDE",
|
|
637
|
+
inside: "INSIDE",
|
|
638
|
+
center: "CENTER",
|
|
639
|
+
outer: "OUTSIDE",
|
|
640
|
+
outside: "OUTSIDE"
|
|
641
|
+
};
|
|
642
|
+
const JOINS = {
|
|
643
|
+
miter: "MITER",
|
|
644
|
+
bevel: "BEVEL",
|
|
645
|
+
round: "ROUND"
|
|
646
|
+
};
|
|
647
|
+
const CAPS = {
|
|
648
|
+
butt: "NONE",
|
|
649
|
+
round: "ROUND",
|
|
650
|
+
square: "SQUARE"
|
|
651
|
+
};
|
|
652
|
+
function isLegacyStroke(stroke) {
|
|
653
|
+
return typeof stroke === "object" && !Array.isArray(stroke) && "thickness" in stroke;
|
|
654
|
+
}
|
|
655
|
+
/** A node's stroke fields; an instance's own fields replace its component's one by one. */
|
|
656
|
+
function strokeFields(pen, base) {
|
|
657
|
+
if (isLegacyStroke(pen.stroke)) return {
|
|
658
|
+
paints: pen.stroke.fill,
|
|
659
|
+
width: pen.stroke.thickness,
|
|
660
|
+
alignment: pen.stroke.align,
|
|
661
|
+
cap: pen.stroke.cap,
|
|
662
|
+
join: pen.stroke.join
|
|
663
|
+
};
|
|
664
|
+
return {
|
|
665
|
+
paints: pen.stroke ?? base?.paints,
|
|
666
|
+
width: pen.strokeWidth ?? base?.width,
|
|
667
|
+
alignment: pen.strokeAlignment ?? base?.alignment,
|
|
668
|
+
cap: pen.strokeLinecap ?? base?.cap,
|
|
669
|
+
join: pen.strokeLinejoin ?? base?.join
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
const STROKE_SIDE_FIELDS = [
|
|
673
|
+
["top", "borderTopWeight"],
|
|
674
|
+
["right", "borderRightWeight"],
|
|
675
|
+
["bottom", "borderBottomWeight"],
|
|
676
|
+
["left", "borderLeftWeight"]
|
|
677
|
+
];
|
|
678
|
+
/** A per-side width, with a `$name` on a side kept as that side's binding. */
|
|
679
|
+
function applySideWidths(node, sides, ctx) {
|
|
680
|
+
node.independentStrokeWeights = true;
|
|
681
|
+
let weight = 0;
|
|
682
|
+
for (const [side, field] of STROKE_SIDE_FIELDS) {
|
|
683
|
+
const value = resolveNumber(sides[side], ctx);
|
|
684
|
+
node[field] = value;
|
|
685
|
+
bindIfVar(node, field, sides[side], ctx);
|
|
686
|
+
if (value > weight) weight = value;
|
|
687
|
+
}
|
|
688
|
+
return weight;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Sets a node's strokes. pen.dev draws a stroke only with both paints and a width: a missing side
|
|
692
|
+
* of a per-side width is zero, and the alignment defaults to centred.
|
|
693
|
+
*/
|
|
694
|
+
function applyStroke(node, fields, ctx, images, shaders) {
|
|
695
|
+
node.strokes = [];
|
|
696
|
+
node.independentStrokeWeights = false;
|
|
697
|
+
if (fields.paints === void 0 || fields.width === void 0) return;
|
|
698
|
+
let weight;
|
|
699
|
+
if (typeof fields.width === "object") weight = applySideWidths(node, fields.width, ctx);
|
|
700
|
+
else {
|
|
701
|
+
weight = resolveNumber(fields.width, ctx);
|
|
702
|
+
bindIfVar(node, "strokeWeight", fields.width, ctx);
|
|
703
|
+
}
|
|
704
|
+
if (weight <= 0) return;
|
|
705
|
+
const align = ALIGNMENTS[fields.alignment ?? "center"] ?? "CENTER";
|
|
706
|
+
const cap = CAPS[fields.cap ?? "butt"] ?? "NONE";
|
|
707
|
+
const join = JOINS[fields.join ?? "miter"] ?? "MITER";
|
|
708
|
+
node.strokeCap = cap;
|
|
709
|
+
node.strokeJoin = join;
|
|
710
|
+
node.strokes = convertPaints(fields.paints, "strokes", ctx, node, images, shaders).map((paint) => ({
|
|
711
|
+
...paint,
|
|
712
|
+
weight,
|
|
713
|
+
align,
|
|
714
|
+
cap,
|
|
715
|
+
join,
|
|
716
|
+
dashPattern: []
|
|
717
|
+
}));
|
|
718
|
+
}
|
|
719
|
+
//#endregion
|
|
720
|
+
//#region src/effects.ts
|
|
721
|
+
const DEFAULT_SHADOW_COLOR = {
|
|
722
|
+
r: 0,
|
|
723
|
+
g: 0,
|
|
724
|
+
b: 0,
|
|
725
|
+
a: .25
|
|
726
|
+
};
|
|
727
|
+
function convertEffect(effect, ctx, shaders) {
|
|
728
|
+
if (effect.type === "shader") {
|
|
729
|
+
const source = effect.url ? shaders?.get(effect.url) : void 0;
|
|
730
|
+
if (!source) return null;
|
|
731
|
+
return {
|
|
732
|
+
type: "SHADER",
|
|
733
|
+
source,
|
|
734
|
+
uniforms: {},
|
|
735
|
+
visible: resolveBoolean(effect.enabled, ctx),
|
|
736
|
+
blendMode: mapBlendMode(effect.blendMode) ?? "NORMAL"
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const base = {
|
|
740
|
+
visible: resolveBoolean(effect.enabled, ctx),
|
|
741
|
+
color: { ...DEFAULT_SHADOW_COLOR },
|
|
742
|
+
offset: {
|
|
743
|
+
x: 0,
|
|
744
|
+
y: 0
|
|
745
|
+
},
|
|
746
|
+
spread: 0,
|
|
747
|
+
blendMode: mapBlendMode(effect.blendMode) ?? "NORMAL"
|
|
748
|
+
};
|
|
749
|
+
if (effect.type === "blur" || effect.type === "background_blur") return {
|
|
750
|
+
...base,
|
|
751
|
+
type: effect.type === "blur" ? "LAYER_BLUR" : "BACKGROUND_BLUR",
|
|
752
|
+
radius: resolveNumber(effect.radius, ctx)
|
|
753
|
+
};
|
|
754
|
+
if (effect.type !== "shadow") return null;
|
|
755
|
+
return {
|
|
756
|
+
...base,
|
|
757
|
+
type: effect.shadowType === "inner" ? "INNER_SHADOW" : "DROP_SHADOW",
|
|
758
|
+
color: effect.color ? resolveColor(effect.color, ctx) : base.color,
|
|
759
|
+
offset: {
|
|
760
|
+
x: resolveNumber(effect.offset?.x, ctx),
|
|
761
|
+
y: resolveNumber(effect.offset?.y, ctx)
|
|
762
|
+
},
|
|
763
|
+
radius: resolveNumber(effect.blur, ctx),
|
|
764
|
+
spread: resolveNumber(effect.spread, ctx)
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
/** A node's effects, including the shader effects their files give source. */
|
|
768
|
+
function convertEffects(effect, ctx, shaders) {
|
|
769
|
+
if (!effect) return [];
|
|
770
|
+
return (Array.isArray(effect) ? effect : [effect]).flatMap((item) => convertEffect(item, ctx, shaders) ?? []);
|
|
771
|
+
}
|
|
772
|
+
//#endregion
|
|
773
|
+
export { applyStroke, convertEffects, convertPaints, createImageStore, penFileSchema, strokeFields };
|
|
774
|
+
|
|
775
|
+
//# sourceMappingURL=effects.js.map
|