@clypra-studio/engine 2.0.0 → 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.
package/dist/index.cjs CHANGED
@@ -179,6 +179,7 @@ __export(index_exports, {
179
179
  applyStyleToLottie: () => applyStyleToLottie,
180
180
  applyStyleToLottieLayer: () => applyStyleToLottieLayer,
181
181
  applyTimelineAtTime: () => applyTimelineAtTime,
182
+ assertCompatibleTextTemplateRenderer: () => assertCompatibleTextTemplateRenderer,
182
183
  assertValidTextTemplateArtifact: () => assertValidTextTemplateArtifact,
183
184
  assetRegistry: () => assetRegistry,
184
185
  bakeAnimationIntoLayer: () => bakeAnimationIntoLayer,
@@ -315,7 +316,7 @@ __export(index_exports, {
315
316
  hashCaptionPreset: () => hashCaptionPreset,
316
317
  hexToLottieColor: () => hexToLottieColor,
317
318
  hexToLottieRgb: () => hexToLottieRgb,
318
- hexToRgba: () => hexToRgba,
319
+ hexToRgba: () => hexToRgba2,
319
320
  hitTestTransformHandles: () => hitTestTransformHandles,
320
321
  initializeFontSystem: () => initializeFontSystem,
321
322
  injectBatch: () => injectBatch,
@@ -331,6 +332,7 @@ __export(index_exports, {
331
332
  interpolateValue: () => interpolateValue,
332
333
  isKeyframed: () => isKeyframed,
333
334
  isMp4ExportSupported: () => isMp4ExportSupported,
335
+ isTextTemplateRendererVersionNewer: () => isTextTemplateRendererVersionNewer,
334
336
  isWebMExportSupported: () => isWebMExportSupported,
335
337
  layerToTextEffectConfig: () => layerToTextEffectConfig,
336
338
  layoutEngine: () => layoutEngine,
@@ -1219,6 +1221,10 @@ function hexToRgb(hex) {
1219
1221
  b: parseInt(result[3], 16)
1220
1222
  } : { r: 255, g: 255, b: 255 };
1221
1223
  }
1224
+ function hexToRgba(hex, alpha2) {
1225
+ const rgb = hexToRgb(hex);
1226
+ return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha2})`;
1227
+ }
1222
1228
 
1223
1229
  // src/renderer.ts
1224
1230
  function getCanvas2DContext(canvas) {
@@ -1375,9 +1381,11 @@ function renderTextEffectCore(ctx, cfg) {
1375
1381
  };
1376
1382
  const renderWithShadowTrick = (mode, sColor, sBlur, sOffsetX, sOffsetY, opacity, overrideStyle = "#000", spread = 0) => {
1377
1383
  ctx.save();
1378
- ctx.globalAlpha = opacity / 100;
1384
+ const normalizedOpacity = Math.max(0, Math.min(100, opacity)) / 100;
1385
+ const shadowColor2 = /^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(sColor) ? hexToRgba(sColor, normalizedOpacity) : sColor;
1386
+ ctx.globalAlpha = /^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(sColor) ? 1 : normalizedOpacity;
1379
1387
  const shiftX = 1e4;
1380
- ctx.shadowColor = sColor;
1388
+ ctx.shadowColor = shadowColor2;
1381
1389
  ctx.shadowBlur = sBlur;
1382
1390
  ctx.shadowOffsetX = shiftX + sOffsetX;
1383
1391
  ctx.shadowOffsetY = sOffsetY;
@@ -8703,7 +8711,7 @@ var EffectEngine = class {
8703
8711
  };
8704
8712
 
8705
8713
  // src/videoEffects/utils/colorUtils.ts
8706
- function hexToRgba(hex, alpha2) {
8714
+ function hexToRgba2(hex, alpha2) {
8707
8715
  hex = hex.replace("#", "");
8708
8716
  const r = parseInt(hex.substring(0, 2), 16);
8709
8717
  const g = parseInt(hex.substring(2, 4), 16);
@@ -12490,7 +12498,7 @@ function getCanvasTemplatesByCategory(category) {
12490
12498
  // src/textTemplates/contract.ts
12491
12499
  var TEXT_TEMPLATE_KIND = "text-template";
12492
12500
  var TEXT_TEMPLATE_SCHEMA_VERSION = 4;
12493
- var TEXT_TEMPLATE_RENDERER_VERSION = "1.5.0";
12501
+ var TEXT_TEMPLATE_RENDERER_VERSION = "1.6.0";
12494
12502
 
12495
12503
  // src/textTemplates/normalize.ts
12496
12504
  function clone2(value) {
@@ -13018,7 +13026,90 @@ function resolveTemplateControlValues(artifact, optionsOrCustomization, fallback
13018
13026
  // src/textTemplates/validator.ts
13019
13027
  var MAX_NODES = 500;
13020
13028
  var MAX_CONTROLS = 100;
13021
- function validateTextTemplateArtifact(artifact) {
13029
+ var MAX_ADDITIONAL_TARGETS = 16;
13030
+ var UNSAFE_PATH_PARTS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
13031
+ function readPath(target, path) {
13032
+ if (!path || path.split(".").some((part) => UNSAFE_PATH_PARTS.has(part))) return { exists: false };
13033
+ let cursor = target;
13034
+ for (const part of path.split(".")) {
13035
+ if (!cursor || typeof cursor !== "object" || !Object.prototype.hasOwnProperty.call(cursor, part)) return { exists: false };
13036
+ cursor = cursor[part];
13037
+ }
13038
+ return { exists: true, value: cursor };
13039
+ }
13040
+ function valueType(value) {
13041
+ if (value === null) return "null";
13042
+ if (typeof value === "string") return "string";
13043
+ if (typeof value === "number") return "number";
13044
+ if (typeof value === "boolean") return "boolean";
13045
+ return "other";
13046
+ }
13047
+ function controlTypeMatches(control, value) {
13048
+ switch (control.type) {
13049
+ case "text":
13050
+ case "color":
13051
+ case "enum":
13052
+ case "font":
13053
+ case "media":
13054
+ return typeof value === "string";
13055
+ case "number":
13056
+ return typeof value === "number" && Number.isFinite(value);
13057
+ case "boolean":
13058
+ return typeof value === "boolean";
13059
+ default:
13060
+ return false;
13061
+ }
13062
+ }
13063
+ function transformedDefaultValue(control, target) {
13064
+ const transform = target.transform;
13065
+ if (!transform) return control.defaultValue;
13066
+ if (transform.kind === "linear" && typeof control.defaultValue === "number") {
13067
+ return control.defaultValue * transform.scale + transform.offset;
13068
+ }
13069
+ if (transform.kind === "enumMap" && typeof control.defaultValue === "string" && Object.prototype.hasOwnProperty.call(transform.values, control.defaultValue)) {
13070
+ return transform.values[control.defaultValue];
13071
+ }
13072
+ return void 0;
13073
+ }
13074
+ function validateTargetTransform(control, target, currentValue, diagnostics, path) {
13075
+ const transform = target.transform;
13076
+ if (!transform) {
13077
+ if (!controlTypeMatches(control, currentValue)) {
13078
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_TYPE", message: `Control ${control.id} value type does not match ${target.propertyPath}`, nodeId: target.nodeId });
13079
+ }
13080
+ return;
13081
+ }
13082
+ if (transform.kind === "linear") {
13083
+ if (control.type !== "number" || typeof currentValue !== "number" || !Number.isFinite(transform.scale) || !Number.isFinite(transform.offset)) {
13084
+ 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 });
13085
+ return;
13086
+ }
13087
+ return;
13088
+ }
13089
+ if (transform.kind === "enumMap") {
13090
+ const values = transform.values;
13091
+ if (control.type !== "enum" || !values || typeof values !== "object" || Array.isArray(values)) {
13092
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap transforms require an enum control and values object`, nodeId: target.nodeId });
13093
+ return;
13094
+ }
13095
+ const targetType = valueType(currentValue);
13096
+ for (const mappedValue of Object.values(values)) {
13097
+ if (valueType(mappedValue) !== targetType) {
13098
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap values must match the target property type`, nodeId: target.nodeId });
13099
+ break;
13100
+ }
13101
+ }
13102
+ if (control.constraints?.options?.some((option) => !Object.prototype.hasOwnProperty.call(values, option.value))) {
13103
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap must cover every enum option`, nodeId: target.nodeId });
13104
+ }
13105
+ if (transformedDefaultValue(control, target) === void 0) {
13106
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} enumMap must map its default value`, nodeId: target.nodeId });
13107
+ }
13108
+ return;
13109
+ }
13110
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} has an unknown transform kind`, nodeId: target.nodeId });
13111
+ }
13112
+ function validateTextTemplateArtifact(artifact, options = {}) {
13022
13113
  const diagnostics = [];
13023
13114
  if (!artifact || artifact.kind !== "text-template") {
13024
13115
  return [{ severity: "error", code: "TEMPLATE_KIND", message: "Artifact kind must be text-template" }];
@@ -13078,16 +13169,51 @@ function validateTextTemplateArtifact(artifact) {
13078
13169
  diagnostics.push({ severity: "error", code: "CONTROL_LIMIT", message: `Template exceeds the ${MAX_CONTROLS}-control limit` });
13079
13170
  }
13080
13171
  const controlIds = /* @__PURE__ */ new Set();
13172
+ const targetOwners = /* @__PURE__ */ new Map();
13081
13173
  for (const control of controls || []) {
13082
13174
  if (controlIds.has(control.id)) {
13083
13175
  diagnostics.push({ severity: "error", code: "DUPLICATE_CONTROL_ID", message: `Duplicate control ID: ${control.id}` });
13084
13176
  }
13085
13177
  controlIds.add(control.id);
13086
- if (!ids.has(control.target.nodeId)) {
13087
- diagnostics.push({ severity: "error", code: "CONTROL_TARGET", message: `Control ${control.id} targets missing node ${control.target.nodeId}` });
13178
+ if (control.target.transform !== void 0) {
13179
+ diagnostics.push({ severity: "error", code: "CONTROL_TRANSFORM", message: `Control ${control.id} primary target cannot have a transform` });
13180
+ }
13181
+ if (control.additionalTargets !== void 0 && !Array.isArray(control.additionalTargets)) {
13182
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGETS", message: `Control ${control.id} additionalTargets must be an array` });
13183
+ continue;
13184
+ }
13185
+ const targets = [
13186
+ { target: control.target, primary: true },
13187
+ ...(control.additionalTargets || []).map((target) => ({ target, primary: false }))
13188
+ ];
13189
+ if ((control.additionalTargets || []).length > MAX_ADDITIONAL_TARGETS) {
13190
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_LIMIT", message: `Control ${control.id} exceeds the ${MAX_ADDITIONAL_TARGETS}-additional-target limit` });
13088
13191
  }
13089
- if (control.target.propertyPath.includes("__proto__") || control.target.propertyPath.includes("constructor") || control.target.propertyPath.includes("prototype")) {
13090
- diagnostics.push({ severity: "error", code: "UNSAFE_TARGET", message: `Control ${control.id} has an unsafe target path` });
13192
+ for (const { target, primary } of targets) {
13193
+ const targetPath = `${control.id}.${primary ? "target" : "additionalTargets"}`;
13194
+ if (!ids.has(target.nodeId)) {
13195
+ diagnostics.push({ severity: "error", code: primary ? "CONTROL_TARGET" : "CONTROL_ADDITIONAL_TARGET", message: `Control ${control.id} targets missing node ${target.nodeId}` });
13196
+ continue;
13197
+ }
13198
+ if (!target.propertyPath || target.propertyPath.split(".").some((part) => UNSAFE_PATH_PARTS.has(part))) {
13199
+ diagnostics.push({ severity: "error", code: "UNSAFE_TARGET", message: `Control ${control.id} has an unsafe target path` });
13200
+ continue;
13201
+ }
13202
+ const key = `${target.nodeId}\0${target.propertyPath}`;
13203
+ const owner = targetOwners.get(key);
13204
+ if (owner) {
13205
+ diagnostics.push({ severity: "error", code: "CONTROL_TARGET_CONFLICT", message: `Controls ${owner} and ${control.id} write the same target ${target.nodeId}.${target.propertyPath}` });
13206
+ } else {
13207
+ targetOwners.set(key, control.id);
13208
+ }
13209
+ const node = (document2.nodes || []).find((candidate) => candidate.id === target.nodeId);
13210
+ const resolved = node ? readPath(node, target.propertyPath) : { exists: false };
13211
+ if (primary && !options.strictControlTargets) continue;
13212
+ if (!resolved.exists) {
13213
+ diagnostics.push({ severity: "error", code: "CONTROL_PROPERTY_PATH", message: `Control ${control.id} targets a missing property ${target.propertyPath}`, nodeId: target.nodeId });
13214
+ continue;
13215
+ }
13216
+ validateTargetTransform(control, target, resolved.value, diagnostics, targetPath);
13091
13217
  }
13092
13218
  }
13093
13219
  const regions = [timing.intro, timing.outro, timing.loop].filter(Boolean);
@@ -13098,13 +13224,34 @@ function validateTextTemplateArtifact(artifact) {
13098
13224
  });
13099
13225
  return diagnostics;
13100
13226
  }
13101
- function assertValidTextTemplateArtifact(artifact) {
13102
- const errors = validateTextTemplateArtifact(artifact).filter((diagnostic) => diagnostic.severity === "error");
13227
+ function assertValidTextTemplateArtifact(artifact, options = {}) {
13228
+ const errors = validateTextTemplateArtifact(artifact, options).filter((diagnostic) => diagnostic.severity === "error");
13103
13229
  if (errors.length) {
13104
13230
  throw new Error(errors.map((diagnostic) => diagnostic.message).join("; "));
13105
13231
  }
13106
13232
  }
13107
13233
 
13234
+ // src/textTemplates/compatibility.ts
13235
+ function versionParts(version) {
13236
+ return version.split(".").map((part) => Number.parseInt(part, 10)).map((part) => Number.isFinite(part) ? part : 0);
13237
+ }
13238
+ function isTextTemplateRendererVersionNewer(required, current = TEXT_TEMPLATE_RENDERER_VERSION) {
13239
+ const requiredParts = versionParts(required);
13240
+ const currentParts = versionParts(current);
13241
+ for (let index = 0; index < Math.max(requiredParts.length, currentParts.length); index += 1) {
13242
+ const requiredPart = requiredParts[index] ?? 0;
13243
+ const currentPart = currentParts[index] ?? 0;
13244
+ if (requiredPart !== currentPart) return requiredPart > currentPart;
13245
+ }
13246
+ return false;
13247
+ }
13248
+ function assertCompatibleTextTemplateRenderer(artifact, current = TEXT_TEMPLATE_RENDERER_VERSION) {
13249
+ const required = artifact.revision?.rendererVersion;
13250
+ if (required && isTextTemplateRendererVersionNewer(required, current)) {
13251
+ throw new Error(`Text template requires renderer ${required}, but this engine supports ${current}`);
13252
+ }
13253
+ }
13254
+
13108
13255
  // src/textTemplates/compiler.ts
13109
13256
  function clone3(value) {
13110
13257
  return JSON.parse(JSON.stringify(value));
@@ -13128,6 +13275,20 @@ function setPath(target, path, value) {
13128
13275
  }
13129
13276
  cursor[parts[parts.length - 1]] = value;
13130
13277
  }
13278
+ function transformTargetValue(value, target, fallback) {
13279
+ const transform = target.transform;
13280
+ if (!transform) return value;
13281
+ if (transform.kind === "linear") {
13282
+ return typeof value === "number" && Number.isFinite(value) ? value * transform.scale + transform.offset : value;
13283
+ }
13284
+ if (transform.kind === "enumMap" && typeof value === "string" && Object.prototype.hasOwnProperty.call(transform.values, value)) {
13285
+ return transform.values[value];
13286
+ }
13287
+ if (transform.kind === "enumMap" && typeof fallback === "string" && Object.prototype.hasOwnProperty.call(transform.values, fallback)) {
13288
+ return transform.values[fallback];
13289
+ }
13290
+ return value;
13291
+ }
13131
13292
  function finite(value, fallback) {
13132
13293
  return typeof value === "number" && Number.isFinite(value) ? value : fallback;
13133
13294
  }
@@ -13175,6 +13336,8 @@ function resolveControls(controls, values) {
13175
13336
  ) : control.defaultValue;
13176
13337
  } else if (control.type === "boolean") {
13177
13338
  resolved[control.id] = value === true;
13339
+ } else if (control.type === "enum" && control.constraints?.options && !control.constraints.options.some((option) => option.value === value)) {
13340
+ resolved[control.id] = control.defaultValue;
13178
13341
  } else {
13179
13342
  resolved[control.id] = value;
13180
13343
  }
@@ -13306,11 +13469,18 @@ function evaluateNodeProperty(node, property, fallback, time, duration) {
13306
13469
  }
13307
13470
  function compileTextTemplate(artifact, context) {
13308
13471
  assertValidTextTemplateArtifact(artifact);
13472
+ assertCompatibleTextTemplateRenderer(artifact);
13309
13473
  const resolvedControls = resolveControls(artifact.controls, context.controlValues);
13310
13474
  const document2 = clone3(artifact.document);
13311
13475
  for (const control of artifact.controls || []) {
13312
13476
  const node = findNode(document2.nodes, control.target.nodeId);
13313
13477
  if (node) setPath(node, control.target.propertyPath, resolvedControls[control.id]);
13478
+ for (const target of control.additionalTargets || []) {
13479
+ const additionalNode = findNode(document2.nodes, target.nodeId);
13480
+ if (additionalNode) {
13481
+ setPath(additionalNode, target.propertyPath, transformTargetValue(resolvedControls[control.id], target, control.defaultValue));
13482
+ }
13483
+ }
13314
13484
  }
13315
13485
  if (context.controlValues) {
13316
13486
  for (const node of document2.nodes || []) {
@@ -13421,7 +13591,7 @@ function compileTextTemplate(artifact, context) {
13421
13591
  const measuredSize = node.type === "text" ? measureTextNode(node, context.runtime) : void 0;
13422
13592
  const resolvedWidth = flexPos?.width ?? (node.width === "auto" ? measuredSize?.width ?? 1 : evaluateNodeProperty(node, "width", Number(node.width), time, artifact.timing.duration));
13423
13593
  const resolvedHeight = flexPos?.height ?? (node.height === "auto" ? measuredSize?.height ?? 1 : evaluateNodeProperty(node, "height", Number(node.height), time, artifact.timing.duration));
13424
- const baseOpacity = node.type === "shape" ? node.style.fillOpacity ?? 1 : node.type === "media" ? node.style?.opacity ?? 1 : node.type === "container" ? node.style?.opacity ?? 1 : 1;
13594
+ const baseOpacity = node.type === "container" ? node.style?.opacity ?? 1 : 1;
13425
13595
  return {
13426
13596
  id: node.id,
13427
13597
  type: node.type,
@@ -25741,6 +25911,22 @@ function renderCaptionToCanvas(ctx, options) {
25741
25911
  ctx.save();
25742
25912
  const displayText = preset.textTransform === "uppercase" ? box.word.text.toUpperCase() : preset.textTransform === "lowercase" ? box.word.text.toLowerCase() : box.word.text;
25743
25913
  const isActive = box.isActive;
25914
+ if (isActive && preset.activeBackgroundColor) {
25915
+ ctx.save();
25916
+ ctx.fillStyle = preset.activeBackgroundColor;
25917
+ const padX = (preset.activeBackgroundPaddingX ?? 8) * scale;
25918
+ const padY = (preset.activeBackgroundPaddingY ?? 4) * scale;
25919
+ const radius = (preset.activeBackgroundBorderRadius ?? 5) * scale;
25920
+ const bgX = box.x - padX;
25921
+ const bgY = box.y - padY / 2;
25922
+ const bgW = box.width + padX * 2;
25923
+ const bgH = box.height + padY;
25924
+ ctx.beginPath();
25925
+ if (typeof ctx.roundRect === "function") ctx.roundRect(bgX, bgY, bgW, bgH, radius);
25926
+ else ctx.rect(bgX, bgY, bgW, bgH);
25927
+ ctx.fill();
25928
+ ctx.restore();
25929
+ }
25744
25930
  if (isActive && preset.activeScale && preset.activeScale !== 1) {
25745
25931
  const centerX = box.x + box.width / 2;
25746
25932
  const centerY = box.y + box.height / 2;
@@ -25938,6 +26124,7 @@ var VERSION = "1.4.0";
25938
26124
  applyStyleToLottie,
25939
26125
  applyStyleToLottieLayer,
25940
26126
  applyTimelineAtTime,
26127
+ assertCompatibleTextTemplateRenderer,
25941
26128
  assertValidTextTemplateArtifact,
25942
26129
  assetRegistry,
25943
26130
  bakeAnimationIntoLayer,
@@ -26090,6 +26277,7 @@ var VERSION = "1.4.0";
26090
26277
  interpolateValue,
26091
26278
  isKeyframed,
26092
26279
  isMp4ExportSupported,
26280
+ isTextTemplateRendererVersionNewer,
26093
26281
  isWebMExportSupported,
26094
26282
  layerToTextEffectConfig,
26095
26283
  layoutEngine,