svg-vectordrawable 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ibrahima Dansoko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # svg-vectordrawable
2
+
3
+ Convert SVG to **Android VectorDrawable** XML — robustly, across SVGs from any editor.
4
+
5
+ Built on [svgo](https://github.com/svg/svgo) (v4) for parsing and normalization, with a hand‑written
6
+ walker that emits the VectorDrawable. Notably handles **gradients including `gradientTransform`**
7
+ (linear & radial), which the popular but unmaintained `svg2vectordrawable` gets wrong.
8
+
9
+ ```
10
+ npm i svg-vectordrawable
11
+ ```
12
+
13
+ ## Why another one?
14
+
15
+ `svg2vectordrawable` is unmaintained (last release 2022), pins `svgo@2`, and ignores
16
+ `gradientTransform` (Figma radial gradients render at the wrong place). This library is on
17
+ `svgo@4`, bakes `gradientTransform` into the Android coordinates, and **fails loud** instead of
18
+ emitting plausible‑but‑wrong output.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { convert } from 'svg-vectordrawable';
24
+
25
+ const { xml, warnings } = convert(svgString, {
26
+ optimize: true, // run svgo normalization first (recommended)
27
+ currentColor: '#000', // value substituted for `currentColor`
28
+ floatPrecision: 3,
29
+ fillBlackForUnfilled: true, // unfilled paths get black (SVG default)
30
+ xmlTag: false, // prepend <?xml ...?>
31
+ tint: '#FFFFFFFF', // android:tint on <vector> (Android color literal)
32
+ strict: false, // throw on unsupported constructs instead of warning
33
+ onWarn: (w) => console.warn(w.code, w.message),
34
+ });
35
+ ```
36
+
37
+ `convert` is synchronous and returns `{ xml, warnings }`.
38
+
39
+ Node file helpers:
40
+
41
+ ```ts
42
+ import { convertFile, convertDir } from 'svg-vectordrawable';
43
+ convertFile('icon.svg', 'res/drawable/icon.xml');
44
+ convertDir('svg/', 'res/drawable/');
45
+ ```
46
+
47
+ ### CLI
48
+
49
+ ```
50
+ svgvd icon.svg # writes icon.xml next to it
51
+ svgvd icons/ -o out/ # batch a directory
52
+ svgvd icon.svg --stdout # print to stdout
53
+ svgvd -s '<svg>…</svg>' # convert an inline SVG string
54
+ svgvd icon.svg --xml-tag --tint '#FFFFFFFF'
55
+ svgvd icon.svg --strict # fail on anything not representable
56
+ ```
57
+
58
+ ### Browser
59
+
60
+ A browser build (svgo's browser bundle, no Node built-ins) is published under the `./browser`
61
+ subpath:
62
+
63
+ ```ts
64
+ import { convert } from 'svg-vectordrawable/browser';
65
+ const { xml } = convert(svgString);
66
+ ```
67
+
68
+ ## What it handles
69
+
70
+ | Feature | Status |
71
+ | ---------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
72
+ | Paths, `fill`, `stroke` (width/cap/join/miter), `fill-rule` → `fillType` | ✅ |
73
+ | Shapes (`rect`/`circle`/`ellipse`/`line`/`poly*`) → path | ✅ |
74
+ | Colors: `#rgb[a]`, `#rrggbb[aa]`, `rgb()/rgba()`, `hsl()/hsla()`, named, `currentColor` | ✅ |
75
+ | Attribute inheritance (incl. presentation attrs on the `<svg>` root and `<g>`) | ✅ |
76
+ | Inline `style="…"` and `<style>` (via svgo) | ✅ |
77
+ | Linear & radial **gradients**, `gradientTransform`, **`objectBoundingBox`** (via path bbox), `href` sharing, `spreadMethod` → `tileMode` | ✅ |
78
+ | `<g transform>` → `<group>` (translate/rotate/scale); **skew/shear baked into geometry** | ✅ |
79
+ | **`<use>` / `<symbol>`** references — inlined before conversion | ✅ |
80
+ | `clip-path` → `<clip-path>` | ✅ (basic) |
81
+ | `opacity` folded into `fillAlpha`/`strokeAlpha` | ✅ |
82
+
83
+ ## Known limitations
84
+
85
+ A VectorDrawable simply cannot represent some SVG features. These are **warned** (or throw in
86
+ `strict` mode), never silently mis‑rendered:
87
+
88
+ - `<mask>`, `<filter>`, `<pattern>`, `<image>`, `<text>` — not representable.
89
+ - `stroke-dasharray` and gradient **strokes** — dropped (VectorDrawable supports neither).
90
+ - `gradientUnits="objectBoundingBox"` is resolved from the path's bounding box; only when that box
91
+ is unavailable does it fall back to the viewport (with a warning).
92
+ - `clip-path` on a `<g>` (vs. on a drawable element) is not applied.
93
+
94
+ ## Robustness
95
+
96
+ Validated against ~10,700 real icons (Feather, Bootstrap Icons, Heroicons, Tabler) plus ~900
97
+ Figma‑exported brand assets: 100% converted with the correct paint model (stroke icons stay
98
+ strokes, filled icons stay fills). A sample (incl. gradients, `gradientTransform`, `objectBoundingBox`,
99
+ `<use>`, sheared groups, clip-path) is **compiled with `aapt2`** — Android's own toolchain accepts
100
+ the output — and a golden-snapshot suite locks the exact XML against regressions.
101
+
102
+ ## License
103
+
104
+ MIT