@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.
@@ -0,0 +1,158 @@
1
+ import { possibleValues } from "./annotations.js";
2
+ //#region src/assets.ts
3
+ /**
4
+ * The icon set each of pen.dev's libraries is drawn from: Iconify's for Lucide, Feather, and
5
+ * Phosphor, which pen.dev draws at one weight, and Google's variable fonts for Material Symbols.
6
+ */
7
+ const ICON_SETS = {
8
+ lucide: "lucide",
9
+ feather: "feather",
10
+ phosphor: "ph",
11
+ "Material Symbols Outlined": "material-symbols-outlined",
12
+ "Material Symbols Rounded": "material-symbols-rounded",
13
+ "Material Symbols Sharp": "material-symbols-sharp"
14
+ };
15
+ /** The libraries pen.dev draws at an icon's `weight`: Material Symbols' variable fonts. */
16
+ const WEIGHTED_LIBRARIES = /* @__PURE__ */ new Set([
17
+ "Material Symbols Outlined",
18
+ "Material Symbols Rounded",
19
+ "Material Symbols Sharp"
20
+ ]);
21
+ /** The weight pen.dev draws Material Symbols at when an icon sets none. */
22
+ const MATERIAL_SYMBOLS_WEIGHT = 200;
23
+ /** An icon's name in its set. */
24
+ function iconName(library, icon) {
25
+ const set = ICON_SETS[library];
26
+ return set ? `${set}:${icon}` : null;
27
+ }
28
+ /** How an icon from `library` is drawn at `weight`: its font's weight axis, for Material Symbols. */
29
+ function iconOptions(library, weight) {
30
+ return WEIGHTED_LIBRARIES.has(library) ? { wght: weight ?? MATERIAL_SYMBOLS_WEIGHT } : void 0;
31
+ }
32
+ /**
33
+ * Whether an image URL names a file in the document's folder: no scheme, no absolute path, and no
34
+ * `..` step out of it.
35
+ */
36
+ function isDocumentAssetPath(url) {
37
+ if (/^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("/") || url.startsWith("\\")) return false;
38
+ return !url.split(/[/\\]/).includes("..");
39
+ }
40
+ /** Whether an image URL carries the image itself, for the host to decode as a URL. */
41
+ function isDataURL(url) {
42
+ return /^data:/i.test(url);
43
+ }
44
+ /** How an imported document's image files are named in the document importing it. */
45
+ const IMPORTED_IMAGE_PREFIX = "pen-import:";
46
+ /**
47
+ * An image URL `importedImageURL` gave an imported document's file, split into the alias of the
48
+ * import it came from and its URL there; null for any other URL.
49
+ */
50
+ function parseImportedImageURL(url) {
51
+ if (!url.startsWith("pen-import:")) return null;
52
+ const rest = url.slice(11);
53
+ const slash = rest.indexOf("/");
54
+ if (slash <= 0) return null;
55
+ return {
56
+ alias: rest.slice(0, slash),
57
+ url: rest.slice(slash + 1)
58
+ };
59
+ }
60
+ function paintList(paints) {
61
+ if (paints === void 0) return [];
62
+ return Array.isArray(paints) ? paints : [paints];
63
+ }
64
+ /** Adds a shader file a paint or effect names, if it is one beside the document. */
65
+ function addShaderFile(url, shaders) {
66
+ if (isDocumentAssetPath(url)) shaders.add(url);
67
+ }
68
+ function paintShaders(paints, shaders) {
69
+ for (const paint of paintList(paints)) if (typeof paint === "object" && paint.type === "shader" && typeof paint.url === "string") addShaderFile(paint.url, shaders);
70
+ }
71
+ function effectShaders(effect, shaders) {
72
+ const effects = effect === void 0 ? [] : [effect].flat();
73
+ for (const item of effects) if (item.type === "shader" && typeof item.url === "string") addShaderFile(item.url, shaders);
74
+ }
75
+ function paintImages(paints, images) {
76
+ for (const paint of paintList(paints)) if (typeof paint === "object" && paint.type === "image" && typeof paint.url === "string") {
77
+ const { url } = paint;
78
+ if (isDocumentAssetPath(url) || isDataURL(url) || parseImportedImageURL(url)) images.add(url);
79
+ }
80
+ }
81
+ /**
82
+ * Every weight a field can take, through the variables it names. A value that is no number, or a
83
+ * variable without one, leaves the weight unset, as drawing reads it.
84
+ */
85
+ function possibleWeights(weight, doc) {
86
+ if (weight === void 0) return [void 0];
87
+ if (typeof weight === "number") return [weight];
88
+ const values = possibleValues(weight, doc).map((value) => {
89
+ const parsed = Number(value);
90
+ return Number.isFinite(parsed) ? parsed : void 0;
91
+ });
92
+ return values.length > 0 ? values : [void 0];
93
+ }
94
+ function addIcons(libraries, icons, weights, found) {
95
+ for (const library of libraries) for (const icon of icons) {
96
+ const name = icon ? iconName(library, icon) : null;
97
+ if (!name) continue;
98
+ for (const weight of weights) {
99
+ const options = iconOptions(library, weight);
100
+ found.icons.set(`${name}\0${JSON.stringify(options ?? null)}`, {
101
+ name,
102
+ options
103
+ });
104
+ }
105
+ }
106
+ }
107
+ /** Adds the icon a layer draws, if it draws one, and the libraries and weights it needs. */
108
+ function collectIcon(node, assets) {
109
+ if (!node.icon || node.type !== "icon" && node.type !== void 0) return;
110
+ const icons = possibleValues(node.icon, assets.doc);
111
+ const weights = possibleWeights(node.weight, assets.doc);
112
+ for (const weight of weights) assets.weights.add(weight);
113
+ if (!node.library) {
114
+ for (const icon of icons) assets.bareIcons.add(icon);
115
+ return;
116
+ }
117
+ const libraries = possibleValues(node.library, assets.doc);
118
+ for (const library of libraries) assets.libraries.add(library);
119
+ addIcons(libraries, icons, weights, assets);
120
+ }
121
+ function collect(node, assets) {
122
+ collectIcon(node, assets);
123
+ paintImages(node.fill, assets.images);
124
+ paintShaders(node.fill, assets.shaders);
125
+ effectShaders(node.effect, assets.shaders);
126
+ if (!(typeof node.stroke === "object" && "thickness" in node.stroke)) {
127
+ paintImages(node.stroke, assets.images);
128
+ paintShaders(node.stroke, assets.shaders);
129
+ }
130
+ for (const child of node.children ?? []) collect(child, assets);
131
+ for (const override of Object.values(node.descendants ?? {})) collect(override, assets);
132
+ }
133
+ /**
134
+ * The icons and the image files a document draws, so a host can load them before reading it. An
135
+ * icon, library, or weight bound to a variable counts every value the variable has.
136
+ */
137
+ function penAssets(doc) {
138
+ const assets = {
139
+ doc,
140
+ icons: /* @__PURE__ */ new Map(),
141
+ images: /* @__PURE__ */ new Set(),
142
+ shaders: /* @__PURE__ */ new Set(),
143
+ libraries: /* @__PURE__ */ new Set(),
144
+ bareIcons: /* @__PURE__ */ new Set(),
145
+ weights: /* @__PURE__ */ new Set()
146
+ };
147
+ for (const node of doc.children) collect(node, assets);
148
+ addIcons([...assets.libraries], [...assets.bareIcons], [...assets.weights], assets);
149
+ return {
150
+ icons: [...assets.icons.values()],
151
+ images: [...assets.images],
152
+ shaders: [...assets.shaders]
153
+ };
154
+ }
155
+ //#endregion
156
+ export { IMPORTED_IMAGE_PREFIX, iconName, iconOptions, isDataURL, isDocumentAssetPath, parseImportedImageURL, penAssets };
157
+
158
+ //# sourceMappingURL=assets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assets.js","names":[],"sources":["../../src/assets.ts"],"sourcesContent":["import type { PenDocument, PenNode, PenOverride, PenPaint, PenPaints } from './schema'\nimport { possibleValues } from './variables'\n\n/**\n * The icon set each of pen.dev's libraries is drawn from: Iconify's for Lucide, Feather, and\n * Phosphor, which pen.dev draws at one weight, and Google's variable fonts for Material Symbols.\n */\nconst ICON_SETS: Partial<Record<string, string>> = {\n lucide: 'lucide',\n feather: 'feather',\n phosphor: 'ph',\n 'Material Symbols Outlined': 'material-symbols-outlined',\n 'Material Symbols Rounded': 'material-symbols-rounded',\n 'Material Symbols Sharp': 'material-symbols-sharp'\n}\n\n/** The libraries pen.dev draws at an icon's `weight`: Material Symbols' variable fonts. */\nconst WEIGHTED_LIBRARIES = new Set([\n 'Material Symbols Outlined',\n 'Material Symbols Rounded',\n 'Material Symbols Sharp'\n])\n\n/** The weight pen.dev draws Material Symbols at when an icon sets none. */\nconst MATERIAL_SYMBOLS_WEIGHT = 200\n\n/** An icon's name in its set. */\nexport function iconName(library: string, icon: string): string | null {\n const set = ICON_SETS[library]\n return set ? `${set}:${icon}` : null\n}\n\n/** How an icon from `library` is drawn at `weight`: its font's weight axis, for Material Symbols. */\nexport function iconOptions(\n library: string,\n weight: number | undefined\n): Record<string, number> | undefined {\n return WEIGHTED_LIBRARIES.has(library) ? { wght: weight ?? MATERIAL_SYMBOLS_WEIGHT } : undefined\n}\n\n/** An icon a document draws: its name in its set and how it is drawn there. */\nexport interface PenIconRequest {\n name: string\n options?: Record<string, number>\n}\n\n/**\n * Whether an image URL names a file in the document's folder: no scheme, no absolute path, and no\n * `..` step out of it.\n */\nexport function isDocumentAssetPath(url: string): boolean {\n if (/^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith('/') || url.startsWith('\\\\')) return false\n return !url.split(/[/\\\\]/).includes('..')\n}\n\n/** Whether an image URL carries the image itself, for the host to decode as a URL. */\nexport function isDataURL(url: string): boolean {\n return /^data:/i.test(url)\n}\n\n/** How an imported document's image files are named in the document importing it. */\nexport const IMPORTED_IMAGE_PREFIX = 'pen-import:'\n\n/**\n * An image URL `importedImageURL` gave an imported document's file, split into the alias of the\n * import it came from and its URL there; null for any other URL.\n */\nexport function parseImportedImageURL(url: string): { alias: string; url: string } | null {\n if (!url.startsWith(IMPORTED_IMAGE_PREFIX)) return null\n const rest = url.slice(IMPORTED_IMAGE_PREFIX.length)\n const slash = rest.indexOf('/')\n if (slash <= 0) return null\n return { alias: rest.slice(0, slash), url: rest.slice(slash + 1) }\n}\n\n/** What a document draws from outside itself. */\nexport interface PenAssets {\n /** Every icon, each way it is drawn. */\n icons: PenIconRequest[]\n /**\n * Images to load: files relative to the document, data URLs, and files of the documents it\n * imports, by the names `importedImageURL` gives them.\n */\n images: string[]\n /**\n * Shader files to read as text: files beside the document that a `shader` paint or effect names.\n * `PenReadOptions.shaders` gives them back by these URLs.\n */\n shaders: string[]\n}\n\nfunction paintList(paints: PenPaints | undefined): PenPaint[] {\n if (paints === undefined) return []\n return Array.isArray(paints) ? paints : [paints]\n}\n\n/** Adds a shader file a paint or effect names, if it is one beside the document. */\nfunction addShaderFile(url: string, shaders: Set<string>) {\n if (isDocumentAssetPath(url)) shaders.add(url)\n}\n\nfunction paintShaders(paints: PenPaints | undefined, shaders: Set<string>) {\n for (const paint of paintList(paints)) {\n if (typeof paint === 'object' && paint.type === 'shader' && typeof paint.url === 'string') {\n addShaderFile(paint.url, shaders)\n }\n }\n}\n\nfunction effectShaders(effect: PenOverride['effect'], shaders: Set<string>) {\n const effects = effect === undefined ? [] : [effect].flat()\n for (const item of effects) {\n if (item.type === 'shader' && typeof item.url === 'string') addShaderFile(item.url, shaders)\n }\n}\n\nfunction paintImages(paints: PenPaints | undefined, images: Set<string>) {\n for (const paint of paintList(paints)) {\n if (typeof paint === 'object' && paint.type === 'image' && typeof paint.url === 'string') {\n const { url } = paint\n if (isDocumentAssetPath(url) || isDataURL(url) || parseImportedImageURL(url)) images.add(url)\n }\n }\n}\n\ninterface Found {\n doc: PenDocument\n /** Requests by their name and options, so each is listed once. */\n icons: Map<string, PenIconRequest>\n images: Set<string>\n shaders: Set<string>\n libraries: Set<string>\n /** Icons an override names without its library, which comes from the layer it overrides. */\n bareIcons: Set<string>\n /** The weights icons are drawn at, for icons whose library an override leaves out. */\n weights: Set<number | undefined>\n}\n\n/**\n * Every weight a field can take, through the variables it names. A value that is no number, or a\n * variable without one, leaves the weight unset, as drawing reads it.\n */\nfunction possibleWeights(weight: PenOverride['weight'], doc: PenDocument): (number | undefined)[] {\n if (weight === undefined) return [undefined]\n if (typeof weight === 'number') return [weight]\n const values = possibleValues(weight, doc).map((value) => {\n const parsed = Number(value)\n return Number.isFinite(parsed) ? parsed : undefined\n })\n return values.length > 0 ? values : [undefined]\n}\n\nfunction addIcons(\n libraries: readonly string[],\n icons: readonly string[],\n weights: readonly (number | undefined)[],\n found: Found\n) {\n for (const library of libraries) {\n for (const icon of icons) {\n // A variable without a name in some theme draws no icon there.\n const name = icon ? iconName(library, icon) : null\n if (!name) continue\n for (const weight of weights) {\n const options = iconOptions(library, weight)\n found.icons.set(`${name}\\0${JSON.stringify(options ?? null)}`, {\n name,\n options\n })\n }\n }\n }\n}\n\n/** Adds the icon a layer draws, if it draws one, and the libraries and weights it needs. */\nfunction collectIcon(node: PenOverride, assets: Found) {\n if (!node.icon || (node.type !== 'icon' && node.type !== undefined)) return\n const icons = possibleValues(node.icon, assets.doc)\n const weights = possibleWeights(node.weight, assets.doc)\n for (const weight of weights) assets.weights.add(weight)\n if (!node.library) {\n for (const icon of icons) assets.bareIcons.add(icon)\n return\n }\n const libraries = possibleValues(node.library, assets.doc)\n for (const library of libraries) assets.libraries.add(library)\n addIcons(libraries, icons, weights, assets)\n}\n\nfunction collect(node: PenOverride, assets: Found) {\n collectIcon(node, assets)\n paintImages(node.fill, assets.images)\n paintShaders(node.fill, assets.shaders)\n effectShaders(node.effect, assets.shaders)\n if (!(typeof node.stroke === 'object' && 'thickness' in node.stroke)) {\n paintImages(node.stroke, assets.images)\n paintShaders(node.stroke, assets.shaders)\n }\n for (const child of node.children ?? []) collect(child, assets)\n for (const override of Object.values(node.descendants ?? {})) collect(override, assets)\n}\n\n/**\n * The icons and the image files a document draws, so a host can load them before reading it. An\n * icon, library, or weight bound to a variable counts every value the variable has.\n */\nexport function penAssets(doc: PenDocument): PenAssets {\n const assets: Found = {\n doc,\n icons: new Map(),\n images: new Set(),\n shaders: new Set(),\n libraries: new Set(),\n bareIcons: new Set(),\n weights: new Set()\n }\n for (const node of doc.children) collect(node satisfies PenNode, assets)\n addIcons([...assets.libraries], [...assets.bareIcons], [...assets.weights], assets)\n return {\n icons: [...assets.icons.values()],\n images: [...assets.images],\n shaders: [...assets.shaders]\n }\n}\n"],"mappings":";;;;;;AAOA,MAAM,YAA6C;CACjD,QAAQ;CACR,SAAS;CACT,UAAU;CACV,6BAA6B;CAC7B,4BAA4B;CAC5B,0BAA0B;AAC5B;;AAGA,MAAM,qCAAqB,IAAI,IAAI;CACjC;CACA;CACA;AACF,CAAC;;AAGD,MAAM,0BAA0B;;AAGhC,SAAgB,SAAS,SAAiB,MAA6B;CACrE,MAAM,MAAM,UAAU;CACtB,OAAO,MAAM,GAAG,IAAI,GAAG,SAAS;AAClC;;AAGA,SAAgB,YACd,SACA,QACoC;CACpC,OAAO,mBAAmB,IAAI,OAAO,IAAI,EAAE,MAAM,UAAU,wBAAwB,IAAI,KAAA;AACzF;;;;;AAYA,SAAgB,oBAAoB,KAAsB;CACxD,IAAI,uBAAuB,KAAK,GAAG,KAA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