@emasoft/svg-matrix 1.3.0 → 1.3.1

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/src/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * SVG path conversion, and 2D/3D affine transformations using Decimal.js.
6
6
  *
7
7
  * @module @emasoft/svg-matrix
8
- * @version 1.3.0
8
+ * @version 1.3.1
9
9
  * @license MIT
10
10
  *
11
11
  * @example
@@ -135,7 +135,7 @@ Decimal.set({ precision: 80 });
135
135
  * Library version
136
136
  * @constant {string}
137
137
  */
138
- export const VERSION = '1.3.0';
138
+ export const VERSION = "1.3.1";
139
139
 
140
140
  /**
141
141
  * Default precision for path output (decimal places)
package/src/matrix.js CHANGED
@@ -96,7 +96,7 @@ export class Matrix {
96
96
  throw new Error("size must be a positive integer");
97
97
  const out = Array.from({ length: n }, (_, i) =>
98
98
  Array.from({ length: n }, (_, j) =>
99
- i === j ? new Decimal(1) : new Decimal(0),
99
+ (i === j ? new Decimal(1) : new Decimal(0)),
100
100
  ),
101
101
  );
102
102
  return new Matrix(out);
@@ -400,11 +400,11 @@ export class Matrix {
400
400
  // Create augmented matrix [A | I]
401
401
  const aug = Array.from({ length: n }, (_, i) =>
402
402
  Array.from({ length: 2 * n }, (_, j) =>
403
- j < n
403
+ (j < n
404
404
  ? new Decimal(this.data[i][j])
405
405
  : j - n === i
406
406
  ? new Decimal(1)
407
- : new Decimal(0),
407
+ : new Decimal(0)),
408
408
  ),
409
409
  );
410
410
  // Gauss-Jordan elimination
@@ -499,6 +499,49 @@ export function splitBezier(curve) {
499
499
  ];
500
500
  }
501
501
 
502
+ /**
503
+ * Sample a cubic Bezier curve at regular intervals to generate an array of points.
504
+ *
505
+ * Evaluates the curve at `samples` evenly-spaced t values from 0 to 1,
506
+ * producing a polyline approximation of the curve. This is useful for
507
+ * converting bezier curves to polygon representations for clipping operations.
508
+ *
509
+ * @param {Array<{x: Decimal, y: Decimal}>} curve - Array of 4 control points [p0, p1, p2, p3]
510
+ * @param {number} [samples=20] - Number of sample points to generate (minimum 2)
511
+ * @returns {Array<{x: Decimal, y: Decimal}>} Array of points sampled along the curve
512
+ *
513
+ * @example
514
+ * // Sample a curve with 10 points
515
+ * const curve = [point(0,0), point(100,200), point(200,200), point(300,0)];
516
+ * const points = sampleBezierCurve(curve, 10);
517
+ * // Returns: 10 points evenly distributed along the curve from t=0 to t=1
518
+ *
519
+ * @example
520
+ * // High-resolution sampling for smooth approximation
521
+ * const points = sampleBezierCurve(curve, 50);
522
+ * // Returns: 50 points for a smoother polyline representation
523
+ */
524
+ export function sampleBezierCurve(curve, samples = 20) {
525
+ if (!Array.isArray(curve))
526
+ throw new Error("sampleBezierCurve: curve must be an array");
527
+ if (curve.length !== 4)
528
+ throw new Error(
529
+ `sampleBezierCurve: curve must have exactly 4 points, got ${curve.length}`,
530
+ );
531
+ if (typeof samples !== "number" || samples < 2)
532
+ throw new Error("sampleBezierCurve: samples must be a number >= 2");
533
+
534
+ const [p0, p1, p2, p3] = curve;
535
+ const points = [];
536
+
537
+ for (let i = 0; i < samples; i++) {
538
+ const t = i / (samples - 1); // t goes from 0 to 1 inclusive
539
+ points.push(evalCubicBezier(p0, p1, p2, p3, t));
540
+ }
541
+
542
+ return points;
543
+ }
544
+
502
545
  // ============================================================================
503
546
  // Coons Patch Evaluation
504
547
  // ============================================================================
@@ -19,6 +19,7 @@ import { Matrix } from "./matrix.js";
19
19
  import * as Transforms2D from "./transforms2d.js";
20
20
  import * as PolygonClip from "./polygon-clip.js";
21
21
  import * as ClipPathResolver from "./clip-path-resolver.js";
22
+ import { parseTransformAttribute } from "./svg-flatten.js";
22
23
 
23
24
  Decimal.set({ precision: 80 });
24
25
 
@@ -1229,8 +1230,8 @@ export function parsePatternTransform(transformStr) {
1229
1230
  return Matrix.identity(3);
1230
1231
  }
1231
1232
 
1232
- // Use ClipPathResolver's transform parser
1233
- return ClipPathResolver.parseTransform(transformStr);
1233
+ // Use svg-flatten's transform parser (parseTransformAttribute)
1234
+ return parseTransformAttribute(transformStr);
1234
1235
  }
1235
1236
 
1236
1237
  export default {
@@ -5,7 +5,7 @@
5
5
  * Works in both Node.js and browser environments.
6
6
  *
7
7
  * @module svg-matrix-lib
8
- * @version 1.3.0
8
+ * @version 1.3.1
9
9
  * @license MIT
10
10
  *
11
11
  * @example Browser usage:
@@ -32,7 +32,7 @@ Decimal.set({ precision: 80 });
32
32
  /**
33
33
  * Library version
34
34
  */
35
- export const VERSION = "1.3.0";
35
+ export const VERSION = "1.3.1";
36
36
 
37
37
  // Export core classes
38
38
  export { Decimal, Matrix, Vector };
@@ -56,7 +56,8 @@ export const scale3D = Transforms3D.scale;
56
56
  export const applyTransform3D = Transforms3D.applyTransform;
57
57
 
58
58
  /**
59
- * Default export for browser global (window.SVGMatrix)
59
+ * Default export for browser global (window.SVGMatrixLib)
60
+ * Note: Named SVGMatrixLib to avoid conflict with native browser SVGMatrix
60
61
  */
61
62
  const SVGMatrix = {
62
63
  VERSION,
package/src/svg-parser.js CHANGED
@@ -7,10 +7,6 @@
7
7
  * @module svg-parser
8
8
  */
9
9
 
10
- import Decimal from "decimal.js";
11
-
12
- Decimal.set({ precision: 80 });
13
-
14
10
  /**
15
11
  * Recursively set ownerDocument on an element and all its descendants.
16
12
  * @param {SVGElement} el - Element to set ownerDocument on
@@ -5,7 +5,7 @@
5
5
  * Provides 69+ operations for cleaning, optimizing, and transforming SVG files.
6
6
  *
7
7
  * @module svg-toolbox-lib
8
- * @version 1.3.0
8
+ * @version 1.3.1
9
9
  * @license MIT
10
10
  *
11
11
  * @example Browser usage:
@@ -34,7 +34,7 @@ import * as SVGToolboxModule from "./svg-toolbox.js";
34
34
  /**
35
35
  * Library version
36
36
  */
37
- export const VERSION = "1.3.0";
37
+ export const VERSION = "1.3.1";
38
38
 
39
39
  /**
40
40
  * Default export for browser global (window.SVGToolbox)
@@ -606,7 +606,7 @@ export const VALID_CHILDREN = {
606
606
  "set",
607
607
  "title",
608
608
  ],
609
- font: ["desc", "glyph", "hkern", "metadata", "title", "vkern"],
609
+ font: ["desc", "font-face", "glyph", "hkern", "metadata", "missing-glyph", "title", "vkern"],
610
610
  "font-face": ["desc", "font-face-src", "metadata", "title"],
611
611
  "font-face-src": ["font-face-name", "font-face-uri"],
612
612
  "font-face-uri": ["font-face-format"],
package/src/svgm-lib.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * comprehensive SVG manipulation (SVGToolbox). Works in Node.js and browser.
6
6
  *
7
7
  * @module svgm-lib
8
- * @version 1.3.0
8
+ * @version 1.3.1
9
9
  * @license MIT
10
10
  *
11
11
  * @example Browser usage:
@@ -49,7 +49,7 @@ Decimal.set({ precision: 80 });
49
49
  /**
50
50
  * Library version
51
51
  */
52
- export const VERSION = "1.3.0";
52
+ export const VERSION = "1.3.1";
53
53
 
54
54
  // Export math classes
55
55
  export { Decimal, Matrix, Vector };