openfig-cli 0.3.35 → 0.3.37

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.
@@ -21,6 +21,7 @@
21
21
 
22
22
  import { readFileSync } from 'fs';
23
23
  import { join } from 'path';
24
+ import { extractRenderableGradientFill, resolveGradientGeometry } from 'openfig-core';
24
25
  import { hashToHex } from '../core/image-helpers.mjs';
25
26
  import { nid } from '../core/node-helpers.mjs';
26
27
 
@@ -56,49 +57,34 @@ function resolveFill(fillPaints) {
56
57
  * w, h = element dimensions in pixels (needed for userSpaceOnUse coordinates).
57
58
  * Returns { defs: string, fill: string } where fill is 'url(#grad-N)'. */
58
59
  function resolveGradientSvg(paint, w, h) {
60
+ const gradient = extractRenderableGradientFill([paint]);
61
+ if (!gradient) return null;
62
+ const geometry = resolveGradientGeometry(gradient, w, h);
63
+ if (!geometry) return null;
64
+
59
65
  const id = `grad-${++_svgIdSeq}`;
60
- const stops = (paint.stops ?? []).map(s => {
66
+ const stops = gradient.stops.map(s => {
61
67
  const color = cssColor(s.color ?? {});
62
68
  return `<stop offset="${s.position}" stop-color="${color}"/>`;
63
69
  }).join('');
64
- const opacityAttr = (paint.opacity ?? 1) !== 1 ? ` opacity="${paint.opacity}"` : '';
65
-
66
- // Figma's paint.transform maps from NODE space to GRADIENT space.
67
- // We need the inverse: gradient space → node normalized space → pixels.
68
- const t = paint.transform ?? {};
69
- const ga = t.m00 ?? 1, gc = t.m01 ?? 0, ge = t.m02 ?? 0;
70
- const gb = t.m10 ?? 0, gd = t.m11 ?? 1, gf = t.m12 ?? 0;
71
- const det = ga * gd - gb * gc;
72
- // Inverse affine: paint.transform maps node→gradient; we need gradient→node
73
- const ia = gd / det, ic = -gc / det, ie = (gc * gf - gd * ge) / det;
74
- const ib = -gb / det, iid = ga / det, iif = (gb * ge - ga * gf) / det;
75
- const tx = (gx, gy) => (ia * gx + ic * gy + ie) * w;
76
- const ty = (gx, gy) => (ib * gx + iid * gy + iif) * h;
70
+ const opacityAttr = gradient.opacity !== 1 ? ` opacity="${gradient.opacity}"` : '';
77
71
  const f = v => +v.toFixed(2);
78
72
 
79
- if (paint.type === 'GRADIENT_LINEAR') {
80
- // Linear gradient line: (0, 0.5) → (1, 0.5) in gradient space
81
- return {
82
- defs: `<linearGradient id="${id}" x1="${f(tx(0,0.5))}" y1="${f(ty(0,0.5))}" x2="${f(tx(1,0.5))}" y2="${f(ty(1,0.5))}" gradientUnits="userSpaceOnUse">${stops}</linearGradient>`,
83
- fill: `url(#${id})`,
84
- opacityAttr,
85
- };
86
- }
87
- if (paint.type === 'GRADIENT_RADIAL') {
88
- // Radial gradient: center (0.5, 0.5), radius mapped through transform
89
- const cx = f(tx(0.5, 0.5)), cy = f(ty(0.5, 0.5));
90
- // Radius along the gradient's x-axis: distance from center to (1, 0.5)
91
- const rx = f(Math.hypot(tx(1, 0.5) - tx(0.5, 0.5), ty(1, 0.5) - ty(0.5, 0.5)));
92
- const ry = f(Math.hypot(tx(0.5, 1) - tx(0.5, 0.5), ty(0.5, 1) - ty(0.5, 0.5)));
93
- // Rotation angle from the transform
94
- const angle = f(Math.atan2(ty(1, 0.5) - ty(0.5, 0.5), tx(1, 0.5) - tx(0.5, 0.5)) * 180 / Math.PI);
73
+ if (geometry.type === 'linear') {
95
74
  return {
96
- defs: `<radialGradient id="${id}" cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" gradientUnits="userSpaceOnUse" gradientTransform="rotate(${angle},${cx},${cy})">${stops}</radialGradient>`,
75
+ defs: `<linearGradient id="${id}" x1="${f(geometry.start.x)}" y1="${f(geometry.start.y)}" x2="${f(geometry.end.x)}" y2="${f(geometry.end.y)}" gradientUnits="userSpaceOnUse">${stops}</linearGradient>`,
97
76
  fill: `url(#${id})`,
98
77
  opacityAttr,
99
78
  };
100
79
  }
101
- return null;
80
+ const cx = f(geometry.center.x), cy = f(geometry.center.y);
81
+ const rx = f(geometry.radiusX), ry = f(geometry.radiusY);
82
+ const angle = f(geometry.angle * 180 / Math.PI);
83
+ return {
84
+ defs: `<radialGradient id="${id}" cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" gradientUnits="userSpaceOnUse" gradientTransform="rotate(${angle},${cx},${cy})">${stops}</radialGradient>`,
85
+ fill: `url(#${id})`,
86
+ opacityAttr,
87
+ };
102
88
  }
103
89
 
104
90
  function appendDefs(defs, extra) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openfig-cli",
3
- "version": "0.3.35",
3
+ "version": "0.3.37",
4
4
  "description": "OpenFig — Open-source tools for Figma file parsing and rendering",
5
5
  "type": "module",
6
6
  "bin": {
@@ -45,7 +45,7 @@
45
45
  "@modelcontextprotocol/sdk": "^1.27.1",
46
46
  "@resvg/resvg-wasm": "^2.6.2",
47
47
  "kiwi-schema": "^0.5.0",
48
- "openfig-core": "^0.3.3",
48
+ "openfig-core": "^0.3.5",
49
49
  "pako": "^2.1.0",
50
50
  "pdf-lib": "^1.17.1",
51
51
  "sharp": "^0.34.5",