@clypra-studio/engine 1.10.2 → 2.2.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.
@@ -899,7 +899,7 @@ var CANONICAL_RENDERER_VERSION = "1.4.0";
899
899
  // src/textTemplates/contract.ts
900
900
  var TEXT_TEMPLATE_KIND = "text-template";
901
901
  var TEXT_TEMPLATE_SCHEMA_VERSION = 4;
902
- var TEXT_TEMPLATE_RENDERER_VERSION = "1.5.0";
902
+ var TEXT_TEMPLATE_RENDERER_VERSION = "1.6.0";
903
903
 
904
904
  // src/engine/textLayout.ts
905
905
  function measureLine(ctx, line, letterSpacing) {
@@ -1235,6 +1235,10 @@ function hexToRgb(hex) {
1235
1235
  b: parseInt(result[3], 16)
1236
1236
  } : { r: 255, g: 255, b: 255 };
1237
1237
  }
1238
+ function hexToRgba(hex, alpha) {
1239
+ const rgb = hexToRgb(hex);
1240
+ return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`;
1241
+ }
1238
1242
 
1239
1243
  // src/renderer.ts
1240
1244
  function getCanvas2DContext(canvas) {
@@ -1391,9 +1395,11 @@ function renderTextEffectCore(ctx, cfg) {
1391
1395
  };
1392
1396
  const renderWithShadowTrick = (mode, sColor, sBlur, sOffsetX, sOffsetY, opacity, overrideStyle = "#000", spread = 0) => {
1393
1397
  ctx.save();
1394
- ctx.globalAlpha = opacity / 100;
1398
+ const normalizedOpacity = Math.max(0, Math.min(100, opacity)) / 100;
1399
+ const shadowColor2 = /^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(sColor) ? hexToRgba(sColor, normalizedOpacity) : sColor;
1400
+ ctx.globalAlpha = /^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(sColor) ? 1 : normalizedOpacity;
1395
1401
  const shiftX = 1e4;
1396
- ctx.shadowColor = sColor;
1402
+ ctx.shadowColor = shadowColor2;
1397
1403
  ctx.shadowBlur = sBlur;
1398
1404
  ctx.shadowOffsetX = shiftX + sOffsetX;
1399
1405
  ctx.shadowOffsetY = sOffsetY;
@@ -2625,7 +2631,90 @@ function renderTextEffectToCanvas(ctx, input) {
2625
2631
  // src/textTemplates/validator.ts
2626
2632
  var MAX_NODES = 500;
2627
2633
  var MAX_CONTROLS = 100;
2628
- function validateTextTemplateArtifact(artifact) {
2634
+ var MAX_ADDITIONAL_TARGETS = 16;
2635
+ var UNSAFE_PATH_PARTS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
2636
+ function readPath(target, path) {
2637
+ if (!path || path.split(".").some((part) => UNSAFE_PATH_PARTS.has(part))) return { exists: false };
2638
+ let cursor = target;
2639
+ for (const part of path.split(".")) {
2640
+ if (!cursor || typeof cursor !== "object" || !Object.prototype.hasOwnProperty.call(cursor, part)) return { exists: false };
2641
+ cursor = cursor[part];
2642
+ }
2643
+ return { exists: true, value: cursor };
2644
+ }
2645
+ function valueType(value) {
2646
+ if (value === null) return "null";
2647
+ if (typeof value === "string") return "string";
2648
+ if (typeof value === "number") return "number";
2649
+ if (typeof value === "boolean") return "boolean";
2650
+ return "other";
2651
+ }
2652
+ function controlTypeMatches(control, value) {
2653
+ switch (control.type) {
2654
+ case "text":
2655
+ case "color":
2656
+ case "enum":
2657
+ case "font":
2658
+ case "media":
2659
+ return typeof value === "string";
2660
+ case "number":
2661
+ return typeof value === "number" && Number.isFinite(value);
2662
+ case "boolean":
2663
+ return typeof value === "boolean";
2664
+ default:
2665
+ return false;
2666
+ }
2667
+ }
2668
+ function transformedDefaultValue(control, target) {
2669
+ const transform = target.transform;
2670
+ if (!transform) return control.defaultValue;
2671
+ if (transform.kind === "linear" && typeof control.defaultValue === "number") {
2672
+ return control.defaultValue * transform.scale + transform.offset;
2673
+ }
2674
+ if (transform.kind === "enumMap" && typeof control.defaultValue === "string" && Object.prototype.hasOwnProperty.call(transform.values, control.defaultValue)) {
2675
+ return transform.values[control.defaultValue];
2676
+ }
2677
+ return void 0;
2678
+ }
2679
+ function validateTargetTransform(control, target, currentValue, diagnostics, path) {
2680
+ const transform = target.transform;
2681
+ if (!transform) {
2682
+ if (!controlTypeMatches(control, currentValue)) {
2683
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_TYPE", message: `Control ${control.id} value type does not match ${target.propertyPath}`, nodeId: target.nodeId });
2684
+ }
2685
+ return;
2686
+ }
2687
+ if (transform.kind === "linear") {
2688
+ if (control.type !== "number" || typeof currentValue !== "number" || !Number.isFinite(transform.scale) || !Number.isFinite(transform.offset)) {
2689
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} linear transforms require a numeric control, numeric target, scale, and offset`, nodeId: target.nodeId });
2690
+ return;
2691
+ }
2692
+ return;
2693
+ }
2694
+ if (transform.kind === "enumMap") {
2695
+ const values = transform.values;
2696
+ if (control.type !== "enum" || !values || typeof values !== "object" || Array.isArray(values)) {
2697
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap transforms require an enum control and values object`, nodeId: target.nodeId });
2698
+ return;
2699
+ }
2700
+ const targetType = valueType(currentValue);
2701
+ for (const mappedValue of Object.values(values)) {
2702
+ if (valueType(mappedValue) !== targetType) {
2703
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap values must match the target property type`, nodeId: target.nodeId });
2704
+ break;
2705
+ }
2706
+ }
2707
+ if (control.constraints?.options?.some((option) => !Object.prototype.hasOwnProperty.call(values, option.value))) {
2708
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap must cover every enum option`, nodeId: target.nodeId });
2709
+ }
2710
+ if (transformedDefaultValue(control, target) === void 0) {
2711
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap must map its default value`, nodeId: target.nodeId });
2712
+ }
2713
+ return;
2714
+ }
2715
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} has an unknown transform kind`, nodeId: target.nodeId });
2716
+ }
2717
+ function validateTextTemplateArtifact(artifact, options = {}) {
2629
2718
  const diagnostics = [];
2630
2719
  if (!artifact || artifact.kind !== "text-template") {
2631
2720
  return [{ severity: "error", code: "TEMPLATE_KIND", message: "Artifact kind must be text-template" }];
@@ -2685,16 +2774,51 @@ function validateTextTemplateArtifact(artifact) {
2685
2774
  diagnostics.push({ severity: "error", code: "CONTROL_LIMIT", message: `Template exceeds the ${MAX_CONTROLS}-control limit` });
2686
2775
  }
2687
2776
  const controlIds = /* @__PURE__ */ new Set();
2777
+ const targetOwners = /* @__PURE__ */ new Map();
2688
2778
  for (const control of controls || []) {
2689
2779
  if (controlIds.has(control.id)) {
2690
2780
  diagnostics.push({ severity: "error", code: "DUPLICATE_CONTROL_ID", message: `Duplicate control ID: ${control.id}` });
2691
2781
  }
2692
2782
  controlIds.add(control.id);
2693
- if (!ids.has(control.target.nodeId)) {
2694
- diagnostics.push({ severity: "error", code: "CONTROL_TARGET", message: `Control ${control.id} targets missing node ${control.target.nodeId}` });
2783
+ if (control.target.transform !== void 0) {
2784
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} primary target cannot have a transform` });
2785
+ }
2786
+ if (control.additionalTargets !== void 0 && !Array.isArray(control.additionalTargets)) {
2787
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGETS", message: `Control ${control.id} additionalTargets must be an array` });
2788
+ continue;
2789
+ }
2790
+ const targets = [
2791
+ { target: control.target, primary: true },
2792
+ ...(control.additionalTargets || []).map((target) => ({ target, primary: false }))
2793
+ ];
2794
+ if ((control.additionalTargets || []).length > MAX_ADDITIONAL_TARGETS) {
2795
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_LIMIT", message: `Control ${control.id} exceeds the ${MAX_ADDITIONAL_TARGETS}-additional-target limit` });
2695
2796
  }
2696
- if (control.target.propertyPath.includes("__proto__") || control.target.propertyPath.includes("constructor") || control.target.propertyPath.includes("prototype")) {
2697
- diagnostics.push({ severity: "error", code: "UNSAFE_TARGET", message: `Control ${control.id} has an unsafe target path` });
2797
+ for (const { target, primary } of targets) {
2798
+ const targetPath = `${control.id}.${primary ? "target" : "additionalTargets"}`;
2799
+ if (!ids.has(target.nodeId)) {
2800
+ diagnostics.push({ severity: "error", code: primary ? "CONTROL_TARGET" : "CONTROL_ADDITIONAL_TARGET", message: `Control ${control.id} targets missing node ${target.nodeId}` });
2801
+ continue;
2802
+ }
2803
+ if (!target.propertyPath || target.propertyPath.split(".").some((part) => UNSAFE_PATH_PARTS.has(part))) {
2804
+ diagnostics.push({ severity: "error", code: "UNSAFE_TARGET", message: `Control ${control.id} has an unsafe target path` });
2805
+ continue;
2806
+ }
2807
+ const key = `${target.nodeId}\0${target.propertyPath}`;
2808
+ const owner = targetOwners.get(key);
2809
+ if (owner) {
2810
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_CONFLICT", message: `Controls ${owner} and ${control.id} write the same target ${target.nodeId}.${target.propertyPath}` });
2811
+ } else {
2812
+ targetOwners.set(key, control.id);
2813
+ }
2814
+ const node = (document2.nodes || []).find((candidate) => candidate.id === target.nodeId);
2815
+ const resolved = node ? readPath(node, target.propertyPath) : { exists: false };
2816
+ if (primary && !options.strictControlTargets) continue;
2817
+ if (!resolved.exists) {
2818
+ diagnostics.push({ severity: "error", code: "CONTROL_PROPERTY_PATH", message: `Control ${control.id} targets a missing property ${target.propertyPath}`, nodeId: target.nodeId });
2819
+ continue;
2820
+ }
2821
+ validateTargetTransform(control, target, resolved.value, diagnostics, targetPath);
2698
2822
  }
2699
2823
  }
2700
2824
  const regions = [timing.intro, timing.outro, timing.loop].filter(Boolean);
@@ -2705,8 +2829,8 @@ function validateTextTemplateArtifact(artifact) {
2705
2829
  });
2706
2830
  return diagnostics;
2707
2831
  }
2708
- function assertValidTextTemplateArtifact(artifact) {
2709
- const errors = validateTextTemplateArtifact(artifact).filter((diagnostic) => diagnostic.severity === "error");
2832
+ function assertValidTextTemplateArtifact(artifact, options = {}) {
2833
+ const errors = validateTextTemplateArtifact(artifact, options).filter((diagnostic) => diagnostic.severity === "error");
2710
2834
  if (errors.length) {
2711
2835
  throw new Error(errors.map((diagnostic) => diagnostic.message).join("; "));
2712
2836
  }
@@ -3155,6 +3279,27 @@ function normalizeTextTemplateArtifact(input) {
3155
3279
  };
3156
3280
  }
3157
3281
 
3282
+ // src/textTemplates/compatibility.ts
3283
+ function versionParts(version) {
3284
+ return version.split(".").map((part) => Number.parseInt(part, 10)).map((part) => Number.isFinite(part) ? part : 0);
3285
+ }
3286
+ function isTextTemplateRendererVersionNewer(required, current = TEXT_TEMPLATE_RENDERER_VERSION) {
3287
+ const requiredParts = versionParts(required);
3288
+ const currentParts = versionParts(current);
3289
+ for (let index = 0; index < Math.max(requiredParts.length, currentParts.length); index += 1) {
3290
+ const requiredPart = requiredParts[index] ?? 0;
3291
+ const currentPart = currentParts[index] ?? 0;
3292
+ if (requiredPart !== currentPart) return requiredPart > currentPart;
3293
+ }
3294
+ return false;
3295
+ }
3296
+ function assertCompatibleTextTemplateRenderer(artifact, current = TEXT_TEMPLATE_RENDERER_VERSION) {
3297
+ const required = artifact.revision?.rendererVersion;
3298
+ if (required && isTextTemplateRendererVersionNewer(required, current)) {
3299
+ throw new Error(`Text template requires renderer ${required}, but this engine supports ${current}`);
3300
+ }
3301
+ }
3302
+
3158
3303
  // src/textTemplates/compiler.ts
3159
3304
  function clone2(value) {
3160
3305
  return JSON.parse(JSON.stringify(value));
@@ -3178,6 +3323,20 @@ function setPath(target, path, value) {
3178
3323
  }
3179
3324
  cursor[parts[parts.length - 1]] = value;
3180
3325
  }
3326
+ function transformTargetValue(value, target, fallback) {
3327
+ const transform = target.transform;
3328
+ if (!transform) return value;
3329
+ if (transform.kind === "linear") {
3330
+ return typeof value === "number" && Number.isFinite(value) ? value * transform.scale + transform.offset : value;
3331
+ }
3332
+ if (transform.kind === "enumMap" && typeof value === "string" && Object.prototype.hasOwnProperty.call(transform.values, value)) {
3333
+ return transform.values[value];
3334
+ }
3335
+ if (transform.kind === "enumMap" && typeof fallback === "string" && Object.prototype.hasOwnProperty.call(transform.values, fallback)) {
3336
+ return transform.values[fallback];
3337
+ }
3338
+ return value;
3339
+ }
3181
3340
  function finite(value, fallback) {
3182
3341
  return typeof value === "number" && Number.isFinite(value) ? value : fallback;
3183
3342
  }
@@ -3225,6 +3384,8 @@ function resolveControls(controls, values) {
3225
3384
  ) : control.defaultValue;
3226
3385
  } else if (control.type === "boolean") {
3227
3386
  resolved[control.id] = value === true;
3387
+ } else if (control.type === "enum" && control.constraints?.options && !control.constraints.options.some((option) => option.value === value)) {
3388
+ resolved[control.id] = control.defaultValue;
3228
3389
  } else {
3229
3390
  resolved[control.id] = value;
3230
3391
  }
@@ -3356,11 +3517,18 @@ function evaluateNodeProperty(node, property, fallback, time, duration) {
3356
3517
  }
3357
3518
  function compileTextTemplate(artifact, context) {
3358
3519
  assertValidTextTemplateArtifact(artifact);
3520
+ assertCompatibleTextTemplateRenderer(artifact);
3359
3521
  const resolvedControls = resolveControls(artifact.controls, context.controlValues);
3360
3522
  const document2 = clone2(artifact.document);
3361
3523
  for (const control of artifact.controls || []) {
3362
3524
  const node = findNode(document2.nodes, control.target.nodeId);
3363
3525
  if (node) setPath(node, control.target.propertyPath, resolvedControls[control.id]);
3526
+ for (const target of control.additionalTargets || []) {
3527
+ const additionalNode = findNode(document2.nodes, target.nodeId);
3528
+ if (additionalNode) {
3529
+ setPath(additionalNode, target.propertyPath, transformTargetValue(resolvedControls[control.id], target, control.defaultValue));
3530
+ }
3531
+ }
3364
3532
  }
3365
3533
  if (context.controlValues) {
3366
3534
  for (const node of document2.nodes || []) {
@@ -3471,7 +3639,7 @@ function compileTextTemplate(artifact, context) {
3471
3639
  const measuredSize = node.type === "text" ? measureTextNode(node, context.runtime) : void 0;
3472
3640
  const resolvedWidth = flexPos?.width ?? (node.width === "auto" ? measuredSize?.width ?? 1 : evaluateNodeProperty(node, "width", Number(node.width), time, artifact.timing.duration));
3473
3641
  const resolvedHeight = flexPos?.height ?? (node.height === "auto" ? measuredSize?.height ?? 1 : evaluateNodeProperty(node, "height", Number(node.height), time, artifact.timing.duration));
3474
- const baseOpacity = node.type === "shape" ? node.style.fillOpacity ?? 1 : node.type === "media" ? node.style?.opacity ?? 1 : node.type === "container" ? node.style?.opacity ?? 1 : 1;
3642
+ const baseOpacity = node.type === "container" ? node.style?.opacity ?? 1 : 1;
3475
3643
  return {
3476
3644
  id: node.id,
3477
3645
  type: node.type,