@emasoft/svg-matrix 1.0.9 → 1.0.11

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/README.md CHANGED
@@ -56,41 +56,88 @@ node test/benchmark-precision.js
56
56
 
57
57
  ### Node.js (Local Installation)
58
58
 
59
- **Step 1: Install the package**
59
+ **What does "local installation" mean?** It means downloading the library to your computer so you can use it in your Node.js projects. This is what most developers do.
60
+
61
+ #### Step 1: Install the package
62
+
63
+ Open your terminal and run:
60
64
 
61
65
  ```bash
62
66
  npm install @emasoft/svg-matrix
63
67
  ```
64
68
 
65
- **Step 2: Import what you need**
69
+ This downloads the library into a folder called `node_modules` in your project.
70
+
71
+ #### Step 2: Create a JavaScript file
72
+
73
+ Create a new file called `example.js` in your project folder.
74
+
75
+ #### Step 3: Import what you need
66
76
 
67
- You can import specific modules (recommended - smaller bundle):
77
+ There are two ways to import the library:
78
+
79
+ **Way 1: Pick only what you need (recommended)**
80
+
81
+ This is like ordering specific items from a menu:
68
82
 
69
83
  ```js
84
+ // example.js
85
+
86
+ // Import only the modules you need
70
87
  import { Matrix, Vector, Transforms2D } from '@emasoft/svg-matrix';
71
88
 
72
- const rotation = Transforms2D.rotate(Math.PI / 4);
73
- console.log(rotation);
89
+ // Now you can use them directly
90
+ const v = Vector.from([1, 2, 3]);
91
+ const rotation = Transforms2D.rotate(Math.PI / 4); // 45 degrees
92
+
93
+ console.log('Vector:', v.toNumberArray());
94
+ console.log('Rotation matrix created!');
74
95
  ```
75
96
 
76
- Or import everything as a single namespace:
97
+ **Way 2: Import everything at once**
98
+
99
+ This is like getting the entire toolbox:
77
100
 
78
101
  ```js
102
+ // example.js
103
+
104
+ // Import EVERYTHING as one big object called "SVGMatrix"
79
105
  import * as SVGMatrix from '@emasoft/svg-matrix';
80
106
 
81
- // Now use SVGMatrix.ModuleName
107
+ // Now use SVGMatrix.ModuleName to access each tool
82
108
  const v = SVGMatrix.Vector.from([1, 2, 3]);
83
109
  const m = SVGMatrix.Matrix.identity(3);
84
110
  const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
85
111
  const pathData = SVGMatrix.GeometryToPath.circleToPathData(100, 100, 50);
112
+
113
+ // See everything that's available:
114
+ console.log('Available modules:', Object.keys(SVGMatrix));
115
+ ```
116
+
117
+ **What's inside the toolbox?** When you import everything, you get:
118
+ - `SVGMatrix.Matrix` - For matrix math
119
+ - `SVGMatrix.Vector` - For vector math
120
+ - `SVGMatrix.Transforms2D` - For 2D rotations, translations, scaling
121
+ - `SVGMatrix.Transforms3D` - For 3D transformations
122
+ - `SVGMatrix.SVGFlatten` - For flattening SVG transforms
123
+ - `SVGMatrix.GeometryToPath` - For converting shapes to paths
124
+ - `SVGMatrix.PolygonClip` - For polygon boolean operations
125
+ - ...and many more!
126
+
127
+ #### Step 4: Run your code
128
+
129
+ ```bash
130
+ node example.js
86
131
  ```
87
132
 
88
- **Complete Node.js Example**
133
+ #### Complete Working Example
89
134
 
90
- Save this as `example.js` and run with `node example.js`:
135
+ Save this as `my-first-example.js`:
91
136
 
92
137
  ```js
93
- // example.js
138
+ // my-first-example.js
139
+ // A complete example showing the main features
140
+
94
141
  import {
95
142
  Matrix,
96
143
  Vector,
@@ -99,46 +146,72 @@ import {
99
146
  SVGFlatten
100
147
  } from '@emasoft/svg-matrix';
101
148
 
102
- // 1. Vector operations
149
+ console.log('=== Vector Example ===');
103
150
  const v = Vector.from([1, 2, 3]);
104
151
  const w = Vector.from([4, 5, 6]);
105
- console.log('Dot product:', v.dot(w).toString());
106
- console.log('Cross product:', v.cross(w).toNumberArray());
152
+ console.log('Vector v:', v.toNumberArray());
153
+ console.log('Vector w:', w.toNumberArray());
154
+ console.log('Dot product (v · w):', v.dot(w).toString());
107
155
 
108
- // 2. Matrix operations
156
+ console.log('\n=== Matrix Example ===');
109
157
  const A = Matrix.from([[1, 2], [3, 4]]);
158
+ console.log('Matrix A:', A.toNumberArray());
110
159
  console.log('Determinant:', A.determinant().toString());
111
- console.log('Inverse:', A.inverse().toNumberArray());
112
160
 
113
- // 3. 2D Transforms
114
- const transform = Transforms2D.translation(100, 50)
161
+ console.log('\n=== Transform Example ===');
162
+ // Create a transform: move 100 right, rotate 45°, scale 2x
163
+ const transform = Transforms2D.translation(100, 0)
115
164
  .mul(Transforms2D.rotate(Math.PI / 4))
116
165
  .mul(Transforms2D.scale(2));
117
166
 
118
- const [x, y] = Transforms2D.applyTransform(transform, 10, 10);
119
- console.log(`Transformed point: (${x}, ${y})`);
167
+ // Apply to a point
168
+ const [x, y] = Transforms2D.applyTransform(transform, 10, 0);
169
+ console.log(`Point (10, 0) after transform: (${x.toFixed(2)}, ${y.toFixed(2)})`);
120
170
 
121
- // 4. SVG path transformation
171
+ console.log('\n=== SVG Path Example ===');
172
+ // Convert a circle to a path
122
173
  const circlePath = GeometryToPath.circleToPathData(0, 0, 50);
123
- const transformedPath = SVGFlatten.transformPathData(circlePath, transform);
124
- console.log('Transformed circle path:', transformedPath);
174
+ console.log('Circle as path:', circlePath.substring(0, 50) + '...');
175
+
176
+ console.log('\nDone! Everything works!');
177
+ ```
178
+
179
+ Run it:
180
+ ```bash
181
+ node my-first-example.js
182
+ ```
183
+
184
+ #### Troubleshooting
185
+
186
+ **"Cannot use import statement outside a module"**
187
+
188
+ Add `"type": "module"` to your `package.json`:
189
+
190
+ ```json
191
+ {
192
+ "name": "my-project",
193
+ "type": "module"
194
+ }
125
195
  ```
126
196
 
127
- **Using with CommonJS (require)**
197
+ Or rename your file from `.js` to `.mjs`.
128
198
 
129
- This library uses ES modules. If you need CommonJS:
199
+ **Using older Node.js syntax (require)?**
200
+
201
+ This library uses modern ES modules. If you have old code using `require()`:
130
202
 
131
203
  ```js
132
- // Option 1: Dynamic import (recommended)
204
+ // OLD WAY (doesn't work directly)
205
+ // const { Matrix } = require('@emasoft/svg-matrix'); // ❌ Error!
206
+
207
+ // NEW WAY (use dynamic import)
133
208
  async function main() {
134
209
  const { Matrix, Vector } = await import('@emasoft/svg-matrix');
210
+
135
211
  const v = Vector.from([1, 2, 3]);
136
212
  console.log(v.norm().toString());
137
213
  }
138
214
  main();
139
-
140
- // Option 2: Add "type": "module" to your package.json
141
- // Then you can use import statements directly
142
215
  ```
143
216
 
144
217
  ### Browser Usage (CDN)
@@ -200,25 +273,43 @@ Popular CDN with good caching.
200
273
  </script>
201
274
  ```
202
275
 
203
- #### Import Everything as a Namespace
276
+ #### Import Everything at Once
204
277
 
205
- If you want all modules under one name:
278
+ **What if I don't know what I need yet?** You can import the entire library as one big object. This is like getting the whole toolbox instead of picking individual tools.
206
279
 
207
280
  ```html
208
281
  <script type="module">
209
- // Import everything as "SVGMatrix"
282
+ // Import EVERYTHING as one object called "SVGMatrix"
283
+ // The "* as" means "everything as"
210
284
  import * as SVGMatrix from 'https://esm.sh/@emasoft/svg-matrix';
211
285
 
212
- // Now use SVGMatrix.ModuleName
286
+ // Now use SVGMatrix.ModuleName to access each tool
213
287
  const v = SVGMatrix.Vector.from([1, 2, 3]);
214
288
  const m = SVGMatrix.Matrix.identity(3);
215
289
  const rotation = SVGMatrix.Transforms2D.rotate(Math.PI / 4);
216
290
  const [x, y] = SVGMatrix.Transforms2D.applyTransform(rotation, 10, 0);
217
291
 
218
- console.log('All modules available:', Object.keys(SVGMatrix));
292
+ // See everything that's available:
293
+ console.log('All modules:', Object.keys(SVGMatrix));
294
+ // Output: ['Matrix', 'Vector', 'Transforms2D', 'Transforms3D', 'SVGFlatten', ...]
219
295
  </script>
220
296
  ```
221
297
 
298
+ **What's inside?** Here's everything you get:
299
+
300
+ | Module | What it does |
301
+ |--------|--------------|
302
+ | `SVGMatrix.Matrix` | Matrix math (multiply, inverse, determinant) |
303
+ | `SVGMatrix.Vector` | Vector math (add, dot product, cross product) |
304
+ | `SVGMatrix.Transforms2D` | 2D transforms (rotate, scale, translate) |
305
+ | `SVGMatrix.Transforms3D` | 3D transforms (rotate around axes) |
306
+ | `SVGMatrix.SVGFlatten` | Flatten SVG transforms into paths |
307
+ | `SVGMatrix.GeometryToPath` | Convert shapes (circle, rect) to paths |
308
+ | `SVGMatrix.PolygonClip` | Boolean operations on polygons |
309
+ | `SVGMatrix.ClipPathResolver` | Resolve SVG clipPath elements |
310
+ | `SVGMatrix.BrowserVerify` | Verify against browser's SVG engine |
311
+ | `SVGMatrix.Logger` | Control library logging |
312
+
222
313
  #### Pin to a Specific Version
223
314
 
224
315
  To avoid breaking changes, pin to a specific version:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emasoft/svg-matrix",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Arbitrary-precision matrix, vector and affine transformation library for JavaScript using decimal.js",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Test script for postinstall.js Unicode and ASCII fallback support.
4
+ * This tests various terminal environment configurations to ensure
5
+ * proper rendering of box characters.
6
+ */
7
+
8
+ import { spawn } from 'child_process';
9
+
10
+ console.log('\n=== Testing postinstall.js Unicode/ASCII support ===\n');
11
+
12
+ const tests = [
13
+ {
14
+ name: 'Unicode mode (UTF-8 locale)',
15
+ env: { LANG: 'en_US.UTF-8' },
16
+ expectUnicode: true,
17
+ },
18
+ {
19
+ name: 'ASCII fallback (C locale)',
20
+ env: { LANG: 'C' },
21
+ expectUnicode: false,
22
+ },
23
+ {
24
+ name: 'UTF-8 in LC_CTYPE',
25
+ env: { LC_CTYPE: 'en_US.UTF-8', LANG: '' },
26
+ expectUnicode: true,
27
+ },
28
+ ];
29
+
30
+ let passed = 0;
31
+ let failed = 0;
32
+
33
+ async function runTest(test) {
34
+ return new Promise((resolve) => {
35
+ console.log(`▸ Testing: ${test.name}`);
36
+
37
+ // Prepare environment
38
+ const env = { ...process.env, ...test.env };
39
+
40
+ const child = spawn('node', ['scripts/postinstall.js'], {
41
+ env,
42
+ stdio: 'pipe',
43
+ });
44
+
45
+ let output = '';
46
+
47
+ child.stdout.on('data', (data) => {
48
+ output += data.toString();
49
+ });
50
+
51
+ child.on('close', () => {
52
+ // Check for Unicode or ASCII box characters
53
+ const hasUnicodeBox = /[╭╮╰╯─│]/.test(output);
54
+ const hasAsciiBox = /[+\-|]/.test(output);
55
+
56
+ if (test.expectUnicode && hasUnicodeBox) {
57
+ console.log(` ✓ PASS: Unicode box characters detected\n`);
58
+ resolve(true);
59
+ } else if (!test.expectUnicode && hasAsciiBox && !hasUnicodeBox) {
60
+ console.log(` ✓ PASS: ASCII fallback characters detected\n`);
61
+ resolve(true);
62
+ } else {
63
+ console.log(` ✗ FAIL: Expected ${test.expectUnicode ? 'Unicode' : 'ASCII'} but got different output\n`);
64
+ resolve(false);
65
+ }
66
+ });
67
+
68
+ child.on('error', (error) => {
69
+ console.log(` ✗ FAIL: ${error.message}\n`);
70
+ resolve(false);
71
+ });
72
+ });
73
+ }
74
+
75
+ async function runAllTests() {
76
+ for (const test of tests) {
77
+ const result = await runTest(test);
78
+ if (result) {
79
+ passed++;
80
+ } else {
81
+ failed++;
82
+ }
83
+ }
84
+
85
+ console.log('=== Test Results ===');
86
+ console.log(`Passed: ${passed}`);
87
+ console.log(`Failed: ${failed}`);
88
+ console.log(`Total: ${passed + failed}\n`);
89
+
90
+ process.exit(failed > 0 ? 1 : 0);
91
+ }
92
+
93
+ runAllTests();
package/src/index.js CHANGED
@@ -5,15 +5,16 @@
5
5
  * SVG path conversion, and 2D/3D affine transformations using Decimal.js.
6
6
  *
7
7
  * @module @emasoft/svg-matrix
8
- * @version 1.0.6
8
+ * @version 1.0.11
9
9
  * @license MIT
10
10
  *
11
11
  * @example
12
12
  * // ES Module import
13
13
  * import { Decimal, Matrix, Vector, Transforms2D, GeometryToPath } from '@emasoft/svg-matrix';
14
14
  *
15
- * // Set global precision (default is 20, max is 1e9)
16
- * Decimal.set({ precision: 80 });
15
+ * // Precision is already set to 80 by default (max is 1e9)
16
+ * // You can increase it further if needed:
17
+ * // Decimal.set({ precision: 200 });
17
18
  *
18
19
  * // Create and compose 2D transforms
19
20
  * const M = Transforms2D.translation(2, 3)
@@ -45,11 +46,16 @@ import * as MeshGradient from './mesh-gradient.js';
45
46
  import * as TextToPath from './text-to-path.js';
46
47
  import { Logger, LogLevel, setLogLevel, getLogLevel as getLoggerLevel, enableFileLogging, disableFileLogging } from './logger.js';
47
48
 
49
+ // Set high-precision default (80 significant digits) on module load
50
+ // This is the same precision used internally by all svg-matrix modules
51
+ // Users can increase further with setPrecision() or Decimal.set() - max is 1e9
52
+ Decimal.set({ precision: 80 });
53
+
48
54
  /**
49
55
  * Library version
50
56
  * @constant {string}
51
57
  */
52
- export const VERSION = '1.0.9';
58
+ export const VERSION = '1.0.11';
53
59
 
54
60
  /**
55
61
  * Default precision for path output (decimal places)