react-native-nano-icons 0.2.1 → 1.0.0-beta.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/LICENSES.md +40 -0
- package/README.md +41 -10
- package/android/src/main/java/com/nanoicons/NanoIconFonts.kt +31 -0
- package/android/src/main/java/com/nanoicons/NanoIconView.kt +50 -15
- package/android/src/main/java/com/nanoicons/NanoIconsFontLoaderModule.kt +72 -11
- package/ios/NanoIconFontResolver.h +5 -0
- package/ios/NanoIconFontResolver.mm +32 -0
- package/ios/NanoIconView.h +0 -4
- package/ios/NanoIconView.mm +62 -57
- package/ios/NanoIconsFontLoader.mm +10 -32
- package/lib/commonjs/cli/build.d.ts +11 -3
- package/lib/commonjs/cli/build.js +86 -25
- package/lib/commonjs/cli/config.d.ts +1 -1
- package/lib/commonjs/cli/config.js +1 -1
- package/lib/commonjs/cli/expoConfig.d.ts +3 -2
- package/lib/commonjs/cli/expoConfig.js +22 -9
- package/lib/commonjs/cli/index.d.ts +5 -5
- package/lib/commonjs/cli/index.js +16 -14
- package/lib/commonjs/cli/link.d.ts +4 -3
- package/lib/commonjs/cli/link.js +127 -45
- package/lib/commonjs/cli/logger.js +9 -7
- package/lib/commonjs/plugin/src/buildFonts.d.ts +3 -3
- package/lib/commonjs/plugin/src/buildFonts.js +5 -15
- package/lib/commonjs/plugin/src/index.d.ts +2 -2
- package/lib/commonjs/plugin/src/index.js +2 -2
- package/lib/commonjs/plugin/src/types.d.ts +10 -1
- package/lib/commonjs/plugin/src/withNanoIconsFontLinking.d.ts +1 -1
- package/lib/commonjs/plugin/src/withNanoIconsFontLinking.js +6 -13
- package/lib/commonjs/scripts/cli.js +46 -23
- package/lib/commonjs/src/core/flatten/arcs.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/arcs.js +136 -0
- package/lib/commonjs/src/core/flatten/document.d.ts +35 -0
- package/lib/commonjs/src/core/flatten/document.js +164 -0
- package/lib/commonjs/src/core/flatten/dom.d.ts +24 -0
- package/lib/commonjs/src/core/flatten/dom.js +221 -0
- package/lib/commonjs/src/core/flatten/element.d.ts +12 -0
- package/lib/commonjs/src/core/flatten/element.js +97 -0
- package/lib/commonjs/src/core/flatten/geometry.d.ts +21 -0
- package/lib/commonjs/src/core/flatten/geometry.js +85 -0
- package/lib/commonjs/src/core/flatten/index.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/index.js +11 -0
- package/lib/commonjs/src/core/flatten/inherit.d.ts +13 -0
- package/lib/commonjs/src/core/flatten/inherit.js +249 -0
- package/lib/commonjs/src/core/flatten/path.d.ts +19 -0
- package/lib/commonjs/src/core/flatten/path.js +439 -0
- package/lib/commonjs/src/core/flatten/pathops.d.ts +14 -0
- package/lib/commonjs/src/core/flatten/pathops.js +236 -0
- package/lib/commonjs/src/core/flatten/shape.d.ts +29 -0
- package/lib/commonjs/src/core/flatten/shape.js +415 -0
- package/lib/commonjs/src/core/flatten/stages/clip-path.d.ts +4 -0
- package/lib/commonjs/src/core/flatten/stages/clip-path.js +20 -0
- package/lib/commonjs/src/core/flatten/stages/collapse.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/collapse.js +86 -0
- package/lib/commonjs/src/core/flatten/stages/defs.d.ts +4 -0
- package/lib/commonjs/src/core/flatten/stages/defs.js +49 -0
- package/lib/commonjs/src/core/flatten/stages/discard.d.ts +4 -0
- package/lib/commonjs/src/core/flatten/stages/discard.js +50 -0
- package/lib/commonjs/src/core/flatten/stages/fill-rule.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/fill-rule.js +7 -0
- package/lib/commonjs/src/core/flatten/stages/nested-svg.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/nested-svg.js +131 -0
- package/lib/commonjs/src/core/flatten/stages/opacity.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/opacity.js +7 -0
- package/lib/commonjs/src/core/flatten/stages/shapes.d.ts +7 -0
- package/lib/commonjs/src/core/flatten/stages/shapes.js +37 -0
- package/lib/commonjs/src/core/flatten/stages/stroke.d.ts +3 -0
- package/lib/commonjs/src/core/flatten/stages/stroke.js +34 -0
- package/lib/commonjs/src/core/flatten/stages/styles.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/styles.js +29 -0
- package/lib/commonjs/src/core/flatten/stages/use.d.ts +4 -0
- package/lib/commonjs/src/core/flatten/stages/use.js +80 -0
- package/lib/commonjs/src/core/flatten/stages/validate.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages/validate.js +39 -0
- package/lib/commonjs/src/core/flatten/stages.d.ts +2 -0
- package/lib/commonjs/src/core/flatten/stages.js +40 -0
- package/lib/commonjs/src/core/flatten/transform.d.ts +35 -0
- package/lib/commonjs/src/core/flatten/transform.js +221 -0
- package/lib/commonjs/src/core/font/compile.d.ts +2 -1
- package/lib/commonjs/src/core/font/compile.js +21 -7
- package/lib/commonjs/src/core/font/metrics.js +7 -0
- package/lib/commonjs/src/core/font/quadratic.d.ts +1 -0
- package/lib/commonjs/src/core/font/quadratic.js +31 -0
- package/lib/commonjs/src/core/glyph/merge.d.ts +7 -0
- package/lib/commonjs/src/core/glyph/merge.js +57 -0
- package/lib/commonjs/src/core/glyph/parse.d.ts +26 -0
- package/lib/commonjs/src/core/{svg/svg_dom.js → glyph/parse.js} +21 -77
- package/lib/commonjs/src/core/{svg/layers.d.ts → glyph/placement.d.ts} +1 -13
- package/lib/commonjs/src/core/{svg/layers.js → glyph/placement.js} +0 -17
- package/lib/commonjs/src/core/glyph/validate.d.ts +8 -0
- package/lib/commonjs/src/core/glyph/validate.js +33 -0
- package/lib/commonjs/src/core/pathkit/contours.d.ts +74 -0
- package/lib/commonjs/src/core/{svg/svg_pathops.js → pathkit/contours.js} +72 -312
- package/lib/commonjs/src/core/pathkit/evenodd.d.ts +12 -0
- package/lib/commonjs/src/core/pathkit/evenodd.js +51 -0
- package/lib/commonjs/src/core/pathkit/load.d.ts +2 -0
- package/lib/commonjs/src/core/pathkit/load.js +26 -0
- package/lib/commonjs/src/core/pathkit/types.d.ts +69 -0
- package/lib/commonjs/src/core/pathkit/types.js +2 -0
- package/lib/commonjs/src/core/pipeline/config.d.ts +1 -0
- package/lib/commonjs/src/core/pipeline/iconPool.d.ts +3 -0
- package/lib/commonjs/src/core/pipeline/iconPool.js +51 -0
- package/lib/commonjs/src/core/pipeline/index.d.ts +3 -3
- package/lib/commonjs/src/core/pipeline/index.js +6 -5
- package/lib/commonjs/src/core/pipeline/prepare.d.ts +19 -0
- package/lib/commonjs/src/core/pipeline/prepare.js +56 -0
- package/lib/commonjs/src/core/pipeline/prepareIcon.d.ts +22 -0
- package/lib/commonjs/src/core/pipeline/prepareIcon.js +78 -0
- package/lib/commonjs/src/core/pipeline/prepareWorker.d.ts +1 -0
- package/lib/commonjs/src/core/pipeline/prepareWorker.js +12 -0
- package/lib/commonjs/src/core/pipeline/{run.d.ts → runFontPipeline.d.ts} +7 -4
- package/lib/commonjs/src/core/pipeline/runFontPipeline.js +109 -0
- package/lib/commonjs/src/core/types.d.ts +3 -92
- package/lib/commonjs/src/utils/fingerPrint.d.ts +8 -1
- package/lib/commonjs/src/utils/fingerPrint.js +8 -1
- package/lib/commonjs/src/utils/fontIdentity.d.ts +4 -0
- package/lib/commonjs/src/utils/fontIdentity.js +24 -0
- package/lib/commonjs/src/utils/packageVersion.d.ts +2 -0
- package/lib/commonjs/src/utils/packageVersion.js +37 -0
- package/lib/commonjs/src/utils/parse.d.ts +0 -1
- package/lib/commonjs/src/utils/parse.js +0 -8
- package/lib/commonjs/src/utils/svgPatterns.d.ts +38 -0
- package/lib/commonjs/src/utils/svgPatterns.js +46 -0
- package/lib/module/const/codegenPrimitives.js +1 -2
- package/lib/module/const/colors.js +1 -2
- package/lib/module/createNanoIconsSet.js +1 -2
- package/lib/module/createNanoIconsSet.native.js +11 -4
- package/lib/module/createNanoIconsSet.shared.js +12 -7
- package/lib/module/createNanoIconsSet.web.js +7 -5
- package/lib/module/fontIntegrity.js +99 -0
- package/lib/module/index.js +1 -1
- package/lib/module/index.node.js +1 -2
- package/lib/module/loadDynamicFont.js +14 -7
- package/lib/module/nativeFontLoader.js +3 -0
- package/lib/module/nativeFontLoader.web.js +3 -0
- package/lib/module/specs/NativeNanoIconsFontLoader.js +1 -2
- package/lib/module/types.js +1 -2
- package/lib/module/utils/fingerPrint.js +3 -3
- package/lib/module/utils/fontIdentity.js +20 -0
- package/lib/module/utils/glyphRuntime.js +1 -2
- package/lib/module/utils/packageVersion.js +29 -0
- package/lib/module/utils/parse.js +1 -9
- package/lib/module/utils/runtimeLog.js +12 -0
- package/lib/module/utils/shallowEqualColor.js +1 -2
- package/lib/module/utils/svgPatterns.js +49 -0
- package/lib/typescript/src/core/flatten/arcs.d.ts +3 -0
- package/lib/typescript/src/core/flatten/document.d.ts +36 -0
- package/lib/typescript/src/core/flatten/dom.d.ts +25 -0
- package/lib/typescript/src/core/flatten/element.d.ts +13 -0
- package/lib/typescript/src/core/flatten/geometry.d.ts +22 -0
- package/lib/typescript/src/core/flatten/index.d.ts +3 -0
- package/lib/typescript/src/core/flatten/inherit.d.ts +14 -0
- package/lib/typescript/src/core/flatten/path.d.ts +20 -0
- package/lib/typescript/src/core/flatten/pathops.d.ts +15 -0
- package/lib/typescript/src/core/flatten/shape.d.ts +30 -0
- package/lib/typescript/src/core/flatten/stages/clip-path.d.ts +5 -0
- package/lib/typescript/src/core/flatten/stages/collapse.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages/defs.d.ts +5 -0
- package/lib/typescript/src/core/flatten/stages/discard.d.ts +5 -0
- package/lib/typescript/src/core/flatten/stages/fill-rule.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages/nested-svg.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages/opacity.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages/shapes.d.ts +8 -0
- package/lib/typescript/src/core/flatten/stages/stroke.d.ts +4 -0
- package/lib/typescript/src/core/flatten/stages/styles.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages/use.d.ts +5 -0
- package/lib/typescript/src/core/flatten/stages/validate.d.ts +3 -0
- package/lib/typescript/src/core/flatten/stages.d.ts +3 -0
- package/lib/typescript/src/core/flatten/transform.d.ts +36 -0
- package/lib/typescript/src/core/glyph/merge.d.ts +8 -0
- package/lib/typescript/src/core/glyph/parse.d.ts +27 -0
- package/lib/typescript/src/core/{svg/layers.d.ts → glyph/placement.d.ts} +2 -14
- package/lib/typescript/src/core/glyph/validate.d.ts +9 -0
- package/lib/typescript/src/core/pathkit/contours.d.ts +75 -0
- package/lib/typescript/src/core/pathkit/evenodd.d.ts +13 -0
- package/lib/typescript/src/core/pathkit/load.d.ts +3 -0
- package/lib/typescript/src/core/pathkit/types.d.ts +70 -0
- package/lib/typescript/src/core/types.d.ts +3 -92
- package/lib/typescript/src/fontIntegrity.d.ts +24 -0
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/nativeFontLoader.d.ts +2 -0
- package/lib/typescript/src/nativeFontLoader.web.d.ts +3 -0
- package/lib/typescript/src/specs/NativeNanoIconsFontLoader.d.ts +1 -0
- package/lib/typescript/src/types.d.ts +5 -0
- package/lib/typescript/src/utils/fingerPrint.d.ts +8 -1
- package/lib/typescript/src/utils/fontIdentity.d.ts +5 -0
- package/lib/typescript/src/utils/packageVersion.d.ts +3 -0
- package/lib/typescript/src/utils/parse.d.ts +0 -1
- package/lib/typescript/src/utils/runtimeLog.d.ts +5 -0
- package/lib/typescript/src/utils/svgPatterns.d.ts +39 -0
- package/package.json +13 -11
- package/src/specs/NativeNanoIconsFontLoader.ts +1 -0
- package/lib/commonjs/src/core/pipeline/managers.d.ts +0 -11
- package/lib/commonjs/src/core/pipeline/managers.js +0 -104
- package/lib/commonjs/src/core/pipeline/run.js +0 -189
- package/lib/commonjs/src/core/svg/svg_dom.d.ts +0 -51
- package/lib/commonjs/src/core/svg/svg_pathops.d.ts +0 -32
- package/lib/module/const/codegenPrimitives.js.map +0 -1
- package/lib/module/const/colors.js.map +0 -1
- package/lib/module/core/font/compile.js +0 -81
- package/lib/module/core/font/compile.js.map +0 -1
- package/lib/module/core/font/metrics.js +0 -37
- package/lib/module/core/font/metrics.js.map +0 -1
- package/lib/module/core/pipeline/config.js +0 -20
- package/lib/module/core/pipeline/config.js.map +0 -1
- package/lib/module/core/pipeline/index.js +0 -5
- package/lib/module/core/pipeline/index.js.map +0 -1
- package/lib/module/core/pipeline/managers.js +0 -94
- package/lib/module/core/pipeline/managers.js.map +0 -1
- package/lib/module/core/pipeline/run.js +0 -204
- package/lib/module/core/pipeline/run.js.map +0 -1
- package/lib/module/core/shims/pathops.py +0 -181
- package/lib/module/core/shims/picosvg-0.22.3-py3-none-any.whl +0 -0
- package/lib/module/core/svg/layers.js +0 -85
- package/lib/module/core/svg/layers.js.map +0 -1
- package/lib/module/core/svg/svg_dom.js +0 -170
- package/lib/module/core/svg/svg_dom.js.map +0 -1
- package/lib/module/core/svg/svg_pathops.js +0 -750
- package/lib/module/core/svg/svg_pathops.js.map +0 -1
- package/lib/module/core/tsconfig.json +0 -32
- package/lib/module/core/types.js +0 -2
- package/lib/module/core/types.js.map +0 -1
- package/lib/module/createNanoIconsSet.js.map +0 -1
- package/lib/module/createNanoIconsSet.native.js.map +0 -1
- package/lib/module/createNanoIconsSet.shared.js.map +0 -1
- package/lib/module/createNanoIconsSet.web.js.map +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/index.node.js.map +0 -1
- package/lib/module/loadDynamicFont.js.map +0 -1
- package/lib/module/specs/NativeNanoIconsFontLoader.js.map +0 -1
- package/lib/module/types.js.map +0 -1
- package/lib/module/utils/fingerPrint.js.map +0 -1
- package/lib/module/utils/glyphRuntime.js.map +0 -1
- package/lib/module/utils/parse.js.map +0 -1
- package/lib/module/utils/shallowEqualColor.js.map +0 -1
- package/lib/typescript/__tests__/build.unit.test.d.ts +0 -3
- package/lib/typescript/__tests__/build.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/clippath.e2e.test.d.ts +0 -3
- package/lib/typescript/__tests__/clippath.e2e.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/fingerprint.unit.test.d.ts +0 -3
- package/lib/typescript/__tests__/fingerprint.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/glyphRuntime.unit.test.d.ts +0 -2
- package/lib/typescript/__tests__/glyphRuntime.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/link.unit.test.d.ts +0 -3
- package/lib/typescript/__tests__/link.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/loadDynamicFont.unit.test.d.ts +0 -2
- package/lib/typescript/__tests__/loadDynamicFont.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/pipeline.e2e.test.d.ts +0 -3
- package/lib/typescript/__tests__/pipeline.e2e.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/placement.unit.test.d.ts +0 -3
- package/lib/typescript/__tests__/placement.unit.test.d.ts.map +0 -1
- package/lib/typescript/__tests__/svg_dom.unit.test.d.ts +0 -3
- package/lib/typescript/__tests__/svg_dom.unit.test.d.ts.map +0 -1
- package/lib/typescript/cli/build.d.ts +0 -32
- package/lib/typescript/cli/build.d.ts.map +0 -1
- package/lib/typescript/cli/link.d.ts +0 -12
- package/lib/typescript/cli/link.d.ts.map +0 -1
- package/lib/typescript/cli/logger.d.ts +0 -28
- package/lib/typescript/cli/logger.d.ts.map +0 -1
- package/lib/typescript/src/const/codegenPrimitives.d.ts.map +0 -1
- package/lib/typescript/src/const/colors.d.ts.map +0 -1
- package/lib/typescript/src/core/font/compile.d.ts +0 -21
- package/lib/typescript/src/core/font/compile.d.ts.map +0 -1
- package/lib/typescript/src/core/font/metrics.d.ts +0 -2
- package/lib/typescript/src/core/font/metrics.d.ts.map +0 -1
- package/lib/typescript/src/core/pipeline/config.d.ts +0 -16
- package/lib/typescript/src/core/pipeline/config.d.ts.map +0 -1
- package/lib/typescript/src/core/pipeline/index.d.ts +0 -4
- package/lib/typescript/src/core/pipeline/index.d.ts.map +0 -1
- package/lib/typescript/src/core/pipeline/managers.d.ts +0 -12
- package/lib/typescript/src/core/pipeline/managers.d.ts.map +0 -1
- package/lib/typescript/src/core/pipeline/run.d.ts +0 -15
- package/lib/typescript/src/core/pipeline/run.d.ts.map +0 -1
- package/lib/typescript/src/core/svg/layers.d.ts.map +0 -1
- package/lib/typescript/src/core/svg/svg_dom.d.ts +0 -52
- package/lib/typescript/src/core/svg/svg_dom.d.ts.map +0 -1
- package/lib/typescript/src/core/svg/svg_pathops.d.ts +0 -33
- package/lib/typescript/src/core/svg/svg_pathops.d.ts.map +0 -1
- package/lib/typescript/src/core/types.d.ts.map +0 -1
- package/lib/typescript/src/createNanoIconsSet.d.ts.map +0 -1
- package/lib/typescript/src/createNanoIconsSet.native.d.ts.map +0 -1
- package/lib/typescript/src/createNanoIconsSet.shared.d.ts.map +0 -1
- package/lib/typescript/src/createNanoIconsSet.web.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/lib/typescript/src/index.node.d.ts.map +0 -1
- package/lib/typescript/src/loadDynamicFont.d.ts.map +0 -1
- package/lib/typescript/src/specs/NanoIconViewNativeComponent.d.ts.map +0 -1
- package/lib/typescript/src/specs/NativeNanoIconsFontLoader.d.ts.map +0 -1
- package/lib/typescript/src/types.d.ts.map +0 -1
- package/lib/typescript/src/utils/fingerPrint.d.ts.map +0 -1
- package/lib/typescript/src/utils/glyphRuntime.d.ts.map +0 -1
- package/lib/typescript/src/utils/parse.d.ts.map +0 -1
- package/lib/typescript/src/utils/shallowEqualColor.d.ts.map +0 -1
- package/plugin/src/buildFonts.ts +0 -42
- package/plugin/src/index.ts +0 -21
- package/plugin/src/types.ts +0 -42
- package/plugin/src/withNanoIconsFontLinking.ts +0 -136
- package/plugin/tsconfig.json +0 -9
- package/scripts/cli.ts +0 -63
- package/scripts/tsconfig.json +0 -25
- package/src/const/colors.ts +0 -150
- package/src/core/font/compile.ts +0 -99
- package/src/core/font/metrics.ts +0 -44
- package/src/core/pipeline/config.ts +0 -25
- package/src/core/pipeline/index.ts +0 -3
- package/src/core/pipeline/managers.ts +0 -132
- package/src/core/pipeline/run.ts +0 -274
- package/src/core/shims/pathops.py +0 -181
- package/src/core/shims/picosvg-0.22.3-py3-none-any.whl +0 -0
- package/src/core/svg/layers.ts +0 -110
- package/src/core/svg/svg_dom.ts +0 -194
- package/src/core/svg/svg_pathops.ts +0 -1040
- package/src/core/tsconfig.json +0 -32
- package/src/core/types.ts +0 -157
- package/src/createNanoIconsSet.native.tsx +0 -192
- package/src/createNanoIconsSet.shared.tsx +0 -174
- package/src/createNanoIconsSet.tsx +0 -20
- package/src/createNanoIconsSet.web.tsx +0 -109
- package/src/index.node.ts +0 -14
- package/src/index.ts +0 -6
- package/src/loadDynamicFont.ts +0 -162
- package/src/types.ts +0 -31
- package/src/utils/fingerPrint.ts +0 -20
- package/src/utils/glyphRuntime.ts +0 -46
- package/src/utils/parse.ts +0 -67
- package/src/utils/shallowEqualColor.ts +0 -17
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INHERITABLE_ATTRIB_DEFAULTS = exports.ATTRIB_W_CUSTOM_INHERITANCE = exports.INHERITABLE_ATTRIB = exports.ATTRIB_DEFAULTS = void 0;
|
|
4
|
+
exports.attribDefault = attribDefault;
|
|
5
|
+
exports.attrName = attrName;
|
|
6
|
+
exports.fieldName = fieldName;
|
|
7
|
+
exports.inheritAttrib = inheritAttrib;
|
|
8
|
+
exports.attribToPassOn = attribToPassOn;
|
|
9
|
+
exports.dropDefaultAttrib = dropDefaultAttrib;
|
|
10
|
+
const geometry_1 = require("./geometry");
|
|
11
|
+
const dom_1 = require("./dom");
|
|
12
|
+
const transform_1 = require("./transform");
|
|
13
|
+
// https://www.w3.org/TR/SVG11/paths.html
|
|
14
|
+
exports.ATTRIB_DEFAULTS = {
|
|
15
|
+
'clip-path': '',
|
|
16
|
+
'clip-rule': 'nonzero',
|
|
17
|
+
fill: 'black',
|
|
18
|
+
'fill-opacity': 1.0,
|
|
19
|
+
'fill-rule': 'nonzero',
|
|
20
|
+
stroke: 'none',
|
|
21
|
+
'stroke-width': 1.0,
|
|
22
|
+
'stroke-linecap': 'butt',
|
|
23
|
+
'stroke-linejoin': 'miter',
|
|
24
|
+
'stroke-miterlimit': 4,
|
|
25
|
+
'stroke-dasharray': 'none',
|
|
26
|
+
'stroke-dashoffset': 0.0,
|
|
27
|
+
'stroke-opacity': 1.0,
|
|
28
|
+
opacity: 1.0,
|
|
29
|
+
transform: '',
|
|
30
|
+
style: '',
|
|
31
|
+
display: 'inline',
|
|
32
|
+
d: '',
|
|
33
|
+
id: '',
|
|
34
|
+
};
|
|
35
|
+
function attribDefault(name) {
|
|
36
|
+
const value = exports.ATTRIB_DEFAULTS[name];
|
|
37
|
+
if (value === undefined) {
|
|
38
|
+
throw new Error(`No entry for '${name}' and no default given`);
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
const COMMON_SHAPE_FIELDS = [
|
|
43
|
+
'id',
|
|
44
|
+
'clip_path',
|
|
45
|
+
'clip_rule',
|
|
46
|
+
'fill',
|
|
47
|
+
'fill_opacity',
|
|
48
|
+
'fill_rule',
|
|
49
|
+
'stroke',
|
|
50
|
+
'stroke_width',
|
|
51
|
+
'stroke_linecap',
|
|
52
|
+
'stroke_linejoin',
|
|
53
|
+
'stroke_miterlimit',
|
|
54
|
+
'stroke_dasharray',
|
|
55
|
+
'stroke_dashoffset',
|
|
56
|
+
'stroke_opacity',
|
|
57
|
+
'opacity',
|
|
58
|
+
'transform',
|
|
59
|
+
'style',
|
|
60
|
+
'display',
|
|
61
|
+
];
|
|
62
|
+
// which field names are valid per tag - gates what inherits onto an element
|
|
63
|
+
const VALID_FIELDS = {
|
|
64
|
+
path: new Set([...COMMON_SHAPE_FIELDS, 'd']),
|
|
65
|
+
circle: new Set([...COMMON_SHAPE_FIELDS, 'r', 'cx', 'cy']),
|
|
66
|
+
ellipse: new Set([...COMMON_SHAPE_FIELDS, 'rx', 'ry', 'cx', 'cy']),
|
|
67
|
+
line: new Set([...COMMON_SHAPE_FIELDS, 'x1', 'y1', 'x2', 'y2']),
|
|
68
|
+
polygon: new Set([...COMMON_SHAPE_FIELDS, 'points']),
|
|
69
|
+
polyline: new Set([...COMMON_SHAPE_FIELDS, 'points']),
|
|
70
|
+
rect: new Set([
|
|
71
|
+
...COMMON_SHAPE_FIELDS,
|
|
72
|
+
'x',
|
|
73
|
+
'y',
|
|
74
|
+
'width',
|
|
75
|
+
'height',
|
|
76
|
+
'rx',
|
|
77
|
+
'ry',
|
|
78
|
+
]),
|
|
79
|
+
linearGradient: new Set([
|
|
80
|
+
'id',
|
|
81
|
+
'x1',
|
|
82
|
+
'y1',
|
|
83
|
+
'x2',
|
|
84
|
+
'y2',
|
|
85
|
+
'gradientTransform',
|
|
86
|
+
'gradientUnits',
|
|
87
|
+
'spreadMethod',
|
|
88
|
+
]),
|
|
89
|
+
radialGradient: new Set([
|
|
90
|
+
'id',
|
|
91
|
+
'cx',
|
|
92
|
+
'cy',
|
|
93
|
+
'r',
|
|
94
|
+
'fx',
|
|
95
|
+
'fy',
|
|
96
|
+
'fr',
|
|
97
|
+
'gradientTransform',
|
|
98
|
+
'gradientUnits',
|
|
99
|
+
'spreadMethod',
|
|
100
|
+
]),
|
|
101
|
+
stop: new Set(['offset', 'stop_color', 'stop_opacity']),
|
|
102
|
+
};
|
|
103
|
+
function attrName(fieldName) {
|
|
104
|
+
return fieldName.replace(/_/g, '-');
|
|
105
|
+
}
|
|
106
|
+
function fieldName(attr) {
|
|
107
|
+
return attr.replace(/-/g, '_');
|
|
108
|
+
}
|
|
109
|
+
function attrSupported(el, attr) {
|
|
110
|
+
const tag = (0, dom_1.stripNs)(el.tag);
|
|
111
|
+
const fields = VALID_FIELDS[tag];
|
|
112
|
+
if (fields) {
|
|
113
|
+
return fields.has(fieldName(attr));
|
|
114
|
+
}
|
|
115
|
+
return true; // we don't know
|
|
116
|
+
}
|
|
117
|
+
function inheritCopy(attrib, child, attr) {
|
|
118
|
+
if (child.attrib.has(attr))
|
|
119
|
+
return;
|
|
120
|
+
if (attr in attrib) {
|
|
121
|
+
child.attrib.set(attr, attrib[attr]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function inheritMultiply(attrib, child, attr) {
|
|
125
|
+
if (!(attr in attrib) && !child.attrib.has(attr))
|
|
126
|
+
return;
|
|
127
|
+
let value = parseFloat(attrib[attr] ?? '1');
|
|
128
|
+
value *= parseFloat(child.attrib.get(attr) ?? '1');
|
|
129
|
+
child.attrib.set(attr, (0, geometry_1.ntos)(value));
|
|
130
|
+
}
|
|
131
|
+
function inheritClipPath(attrib, child, _attr) {
|
|
132
|
+
const clips = [
|
|
133
|
+
...(child.attrib.get('clip-path') ?? '').split(','),
|
|
134
|
+
attrib['clip-path'] ?? '',
|
|
135
|
+
].sort();
|
|
136
|
+
child.attrib.set('clip-path', clips.filter((c) => c).join(','));
|
|
137
|
+
}
|
|
138
|
+
function inheritNondefaultOverflow(attrib, child, attr) {
|
|
139
|
+
const value = attrib[attr] ?? 'visible';
|
|
140
|
+
if (value !== 'visible') {
|
|
141
|
+
inheritCopy(attrib, child, attr);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function inheritNondefaultDisplay(attrib, child, attr) {
|
|
145
|
+
const value = attrib[attr] ?? '';
|
|
146
|
+
if (value === 'none') {
|
|
147
|
+
child.attrib.set(attr, value);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
inheritCopy(attrib, child, attr);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function inheritMatrixMultiply(attrib, child, attr) {
|
|
154
|
+
let transform = transform_1.Affine2D.identity();
|
|
155
|
+
if (attr in attrib) {
|
|
156
|
+
transform = transform_1.Affine2D.fromString(attrib[attr]);
|
|
157
|
+
}
|
|
158
|
+
if (child.attrib.has(attr)) {
|
|
159
|
+
transform = transform_1.Affine2D.composeLtr([
|
|
160
|
+
transform_1.Affine2D.fromString(child.attrib.get(attr)),
|
|
161
|
+
transform,
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
if (!transform.equals(transform_1.Affine2D.identity())) {
|
|
165
|
+
child.attrib.set(attr, transform.toString());
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
child.attrib.delete(attr);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const DO_NOT_INHERIT = () => { };
|
|
172
|
+
const INHERIT_ATTRIB_HANDLERS = {
|
|
173
|
+
'clip-rule': inheritCopy,
|
|
174
|
+
color: inheritCopy,
|
|
175
|
+
display: inheritNondefaultDisplay,
|
|
176
|
+
fill: inheritCopy,
|
|
177
|
+
'fill-rule': inheritCopy,
|
|
178
|
+
style: inheritCopy,
|
|
179
|
+
transform: inheritMatrixMultiply,
|
|
180
|
+
stroke: inheritCopy,
|
|
181
|
+
'stroke-width': inheritCopy,
|
|
182
|
+
'stroke-linecap': inheritCopy,
|
|
183
|
+
'stroke-linejoin': inheritCopy,
|
|
184
|
+
'stroke-miterlimit': inheritCopy,
|
|
185
|
+
'stroke-dasharray': inheritCopy,
|
|
186
|
+
'stroke-dashoffset': inheritCopy,
|
|
187
|
+
'stroke-opacity': inheritCopy,
|
|
188
|
+
'fill-opacity': inheritCopy,
|
|
189
|
+
opacity: inheritMultiply,
|
|
190
|
+
'clip-path': inheritClipPath,
|
|
191
|
+
id: DO_NOT_INHERIT,
|
|
192
|
+
'data-name': DO_NOT_INHERIT,
|
|
193
|
+
'enable-background': DO_NOT_INHERIT,
|
|
194
|
+
overflow: inheritNondefaultOverflow,
|
|
195
|
+
};
|
|
196
|
+
exports.INHERITABLE_ATTRIB = new Set(Object.entries(INHERIT_ATTRIB_HANDLERS)
|
|
197
|
+
.filter(([, handler]) => handler !== DO_NOT_INHERIT)
|
|
198
|
+
.map(([name]) => name));
|
|
199
|
+
exports.ATTRIB_W_CUSTOM_INHERITANCE = new Set([
|
|
200
|
+
'clip-path',
|
|
201
|
+
'opacity',
|
|
202
|
+
'transform',
|
|
203
|
+
]);
|
|
204
|
+
// starting attrib for the root context: defaults of plain copy-inherited attrs
|
|
205
|
+
exports.INHERITABLE_ATTRIB_DEFAULTS = Object.fromEntries(Object.entries(INHERIT_ATTRIB_HANDLERS)
|
|
206
|
+
.filter(([name, handler]) => name in exports.ATTRIB_DEFAULTS && handler === inheritCopy)
|
|
207
|
+
.map(([name]) => {
|
|
208
|
+
const value = exports.ATTRIB_DEFAULTS[name];
|
|
209
|
+
return [name, typeof value === 'number' ? (0, geometry_1.ntos)(value) : value];
|
|
210
|
+
}));
|
|
211
|
+
function inheritAttrib(attrib, child, skipUnhandled = false, skips = new Set()) {
|
|
212
|
+
const work = attrib instanceof Map ? Object.fromEntries(attrib) : { ...attrib };
|
|
213
|
+
for (const attr of Object.keys(work).sort()) {
|
|
214
|
+
if (skips.has(attr) || !attrSupported(child, attr)) {
|
|
215
|
+
delete work[attr];
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const handler = INHERIT_ATTRIB_HANDLERS[attr];
|
|
219
|
+
if (!handler) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
handler(work, child, attr);
|
|
223
|
+
delete work[attr];
|
|
224
|
+
}
|
|
225
|
+
if (Object.keys(work).length && !skipUnhandled) {
|
|
226
|
+
throw new Error(`Unable to process attrib ${JSON.stringify(work)}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function attribToPassOn(currentAttrib, el, skips = exports.ATTRIB_W_CUSTOM_INHERITANCE) {
|
|
230
|
+
const attrCatcher = new dom_1.XEl('dummy');
|
|
231
|
+
inheritAttrib(el.attrib, attrCatcher, true, skips);
|
|
232
|
+
inheritAttrib(currentAttrib, attrCatcher, false, skips);
|
|
233
|
+
return Object.fromEntries(attrCatcher.attrib);
|
|
234
|
+
}
|
|
235
|
+
function dropDefaultAttrib(attrib) {
|
|
236
|
+
for (const attr of [...attrib.keys()].sort()) {
|
|
237
|
+
const defaultValue = exports.ATTRIB_DEFAULTS[attr];
|
|
238
|
+
if (defaultValue === undefined)
|
|
239
|
+
continue;
|
|
240
|
+
const value = attrib.get(attr);
|
|
241
|
+
if (typeof defaultValue === 'number') {
|
|
242
|
+
if (parseFloat(value) === defaultValue)
|
|
243
|
+
attrib.delete(attr);
|
|
244
|
+
}
|
|
245
|
+
else if (value === defaultValue) {
|
|
246
|
+
attrib.delete(attr);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Pt } from './geometry';
|
|
2
|
+
export type SvgCommand = [cmd: string, args: number[]];
|
|
3
|
+
export declare function numArgs(cmd: string): number;
|
|
4
|
+
export declare function checkCmd(cmd: string, args: readonly number[]): number;
|
|
5
|
+
export declare function cmdCoords(cmd: string): [number[], number[]];
|
|
6
|
+
export declare function pathSegment(cmd: string, ...args: number[]): string;
|
|
7
|
+
export declare function parseSvgPath(svgPath: string, exploded?: boolean): SvgCommand[];
|
|
8
|
+
export declare function addCmdToD(d: string, cmd: string, args: number[]): string;
|
|
9
|
+
export declare function buildD(cmds: Iterable<SvgCommand>): string;
|
|
10
|
+
export type WalkCallback = (subpathStart: Pt, currPos: Pt, cmd: string, args: number[], prevPos: Pt | null, prevCmd: string | null, prevArgs: number[] | null) => Iterable<SvgCommand>;
|
|
11
|
+
export declare function walkPath(d: string, callback: WalkCallback): string;
|
|
12
|
+
export declare function absolutePath(d: string): string;
|
|
13
|
+
export declare function absoluteMoveto(d: string): string;
|
|
14
|
+
export declare function explicitLines(d: string): string;
|
|
15
|
+
export declare function expandShorthand(d: string): string;
|
|
16
|
+
export declare function arcsToCubics(d: string): string;
|
|
17
|
+
export declare function subpaths(d: string): string[];
|
|
18
|
+
export declare function roundFloatsD(d: string, ndigits: number): string;
|
|
19
|
+
export declare function asCmdSeq(d: string): SvgCommand[];
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.numArgs = numArgs;
|
|
4
|
+
exports.checkCmd = checkCmd;
|
|
5
|
+
exports.cmdCoords = cmdCoords;
|
|
6
|
+
exports.pathSegment = pathSegment;
|
|
7
|
+
exports.parseSvgPath = parseSvgPath;
|
|
8
|
+
exports.addCmdToD = addCmdToD;
|
|
9
|
+
exports.buildD = buildD;
|
|
10
|
+
exports.walkPath = walkPath;
|
|
11
|
+
exports.absolutePath = absolutePath;
|
|
12
|
+
exports.absoluteMoveto = absoluteMoveto;
|
|
13
|
+
exports.explicitLines = explicitLines;
|
|
14
|
+
exports.expandShorthand = expandShorthand;
|
|
15
|
+
exports.arcsToCubics = arcsToCubics;
|
|
16
|
+
exports.subpaths = subpaths;
|
|
17
|
+
exports.roundFloatsD = roundFloatsD;
|
|
18
|
+
exports.asCmdSeq = asCmdSeq;
|
|
19
|
+
const geometry_1 = require("./geometry");
|
|
20
|
+
const arcs_1 = require("./arcs");
|
|
21
|
+
// https://www.w3.org/TR/SVG11/paths.html#PathData
|
|
22
|
+
const CMD_ARGS = {
|
|
23
|
+
m: 2,
|
|
24
|
+
z: 0,
|
|
25
|
+
l: 2,
|
|
26
|
+
h: 1,
|
|
27
|
+
v: 1,
|
|
28
|
+
c: 6,
|
|
29
|
+
s: 4,
|
|
30
|
+
q: 4,
|
|
31
|
+
t: 2,
|
|
32
|
+
a: 7,
|
|
33
|
+
};
|
|
34
|
+
for (const [k, v] of Object.entries(CMD_ARGS)) {
|
|
35
|
+
CMD_ARGS[k.toUpperCase()] = v;
|
|
36
|
+
}
|
|
37
|
+
// per command: indices of x coords and of y coords within the args
|
|
38
|
+
const CMD_COORDS = {
|
|
39
|
+
m: [[0], [1]],
|
|
40
|
+
z: [[], []],
|
|
41
|
+
l: [[0], [1]],
|
|
42
|
+
h: [[0], []],
|
|
43
|
+
v: [[], [0]],
|
|
44
|
+
c: [
|
|
45
|
+
[0, 2, 4],
|
|
46
|
+
[1, 3, 5],
|
|
47
|
+
],
|
|
48
|
+
s: [
|
|
49
|
+
[0, 2],
|
|
50
|
+
[1, 3],
|
|
51
|
+
],
|
|
52
|
+
q: [
|
|
53
|
+
[0, 2],
|
|
54
|
+
[1, 3],
|
|
55
|
+
],
|
|
56
|
+
t: [[0], [1]],
|
|
57
|
+
a: [[5], [6]],
|
|
58
|
+
};
|
|
59
|
+
for (const [k, v] of Object.entries(CMD_COORDS)) {
|
|
60
|
+
CMD_COORDS[k.toUpperCase()] = v;
|
|
61
|
+
}
|
|
62
|
+
function numArgs(cmd) {
|
|
63
|
+
const n = CMD_ARGS[cmd];
|
|
64
|
+
if (n === undefined) {
|
|
65
|
+
throw new Error(`Invalid svg command "${cmd}"`);
|
|
66
|
+
}
|
|
67
|
+
return n;
|
|
68
|
+
}
|
|
69
|
+
function checkCmd(cmd, args) {
|
|
70
|
+
const cmdArgs = numArgs(cmd);
|
|
71
|
+
if (cmdArgs === 0) {
|
|
72
|
+
if (args.length) {
|
|
73
|
+
throw new Error(`${cmd} has no args, ${args.length} invalid`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (args.length % cmdArgs !== 0) {
|
|
77
|
+
throw new Error(`${cmd} has sets of ${cmdArgs} args, ${args.length} invalid`);
|
|
78
|
+
}
|
|
79
|
+
return cmdArgs;
|
|
80
|
+
}
|
|
81
|
+
function cmdCoords(cmd) {
|
|
82
|
+
const coords = CMD_COORDS[cmd];
|
|
83
|
+
if (coords === undefined) {
|
|
84
|
+
throw new Error(`Invalid svg command "${cmd}"`);
|
|
85
|
+
}
|
|
86
|
+
return coords;
|
|
87
|
+
}
|
|
88
|
+
function pathSegment(cmd, ...args) {
|
|
89
|
+
// put commas between coords, spaces otherwise, author readability pref
|
|
90
|
+
const argsPerCmd = checkCmd(cmd, args);
|
|
91
|
+
const strArgs = args.map(geometry_1.ntos);
|
|
92
|
+
const combinedArgs = [];
|
|
93
|
+
const [xCoords, yCoords] = cmdCoords(cmd);
|
|
94
|
+
const xyCoords = new Set(xCoords.map((x, i) => `${x},${yCoords[i]}`).slice(0, yCoords.length));
|
|
95
|
+
if (argsPerCmd) {
|
|
96
|
+
for (let n = 0; n < strArgs.length / argsPerCmd; n++) {
|
|
97
|
+
const subArgs = strArgs.slice(n * argsPerCmd, (n + 1) * argsPerCmd);
|
|
98
|
+
let i = 0;
|
|
99
|
+
while (i < subArgs.length) {
|
|
100
|
+
if (xyCoords.has(`${i},${i + 1}`)) {
|
|
101
|
+
combinedArgs.push(`${subArgs[i]},${subArgs[i + 1]}`);
|
|
102
|
+
i += 2;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
combinedArgs.push(subArgs[i]);
|
|
106
|
+
i += 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return cmd + combinedArgs.join(' ');
|
|
112
|
+
}
|
|
113
|
+
// ---- d-string parsing ----
|
|
114
|
+
const CMD_RE = /([mzlhvcsqtaMZLHVCSQTA])/;
|
|
115
|
+
const SEPARATOR_RE = /[, ]+/;
|
|
116
|
+
const FLOAT_RE = new RegExp('^[-+]?' +
|
|
117
|
+
'(?:' +
|
|
118
|
+
'(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?' + // int or float
|
|
119
|
+
'|' +
|
|
120
|
+
'(?:\\.[0-9]+)' + // float with leading dot (e.g. '.42')
|
|
121
|
+
')' +
|
|
122
|
+
'(?:[eE][-+]?[0-9]+)?' // optional scientific notation
|
|
123
|
+
);
|
|
124
|
+
const BOOL_RE = /^[01]/;
|
|
125
|
+
// arc args: rx ry x-rotation large-arc-flag sweep-flag x y
|
|
126
|
+
const ARC_ARGUMENT_TYPES = [
|
|
127
|
+
FLOAT_RE,
|
|
128
|
+
FLOAT_RE,
|
|
129
|
+
FLOAT_RE,
|
|
130
|
+
BOOL_RE,
|
|
131
|
+
BOOL_RE,
|
|
132
|
+
FLOAT_RE,
|
|
133
|
+
FLOAT_RE,
|
|
134
|
+
];
|
|
135
|
+
// https://www.w3.org/TR/SVG11/paths.html#PathDataMovetoCommands
|
|
136
|
+
// If a moveto is followed by multiple pairs of coordinates,
|
|
137
|
+
// the subsequent pairs are treated as implicit lineto commands
|
|
138
|
+
const IMPLICIT_REPEAT_CMD = { m: 'l', M: 'L' };
|
|
139
|
+
function parseArgs(cmd, rawArgsStr) {
|
|
140
|
+
const rawArgs = rawArgsStr.split(SEPARATOR_RE).filter((s) => s);
|
|
141
|
+
const result = [];
|
|
142
|
+
if (!rawArgs.length) {
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
const argTypes = cmd.toUpperCase() === 'A' ? ARC_ARGUMENT_TYPES : [FLOAT_RE];
|
|
146
|
+
const n = argTypes.length;
|
|
147
|
+
let i = 0;
|
|
148
|
+
let j = 0;
|
|
149
|
+
while (j < rawArgs.length) {
|
|
150
|
+
const arg = rawArgs[j];
|
|
151
|
+
// modulo to wrap around
|
|
152
|
+
const regex = argTypes[i % n];
|
|
153
|
+
const m = regex.exec(arg);
|
|
154
|
+
if (!m) {
|
|
155
|
+
throw new Error(`Invalid path data "${cmd}${rawArgsStr.slice(0, 40)}": argument #${i + 1} (${arg}) is not a number`);
|
|
156
|
+
}
|
|
157
|
+
const end = m[0].length;
|
|
158
|
+
result.push(parseFloat(arg.slice(0, end)));
|
|
159
|
+
if (end < arg.length) {
|
|
160
|
+
rawArgs[j] = arg.slice(end);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
j += 1;
|
|
164
|
+
}
|
|
165
|
+
i += 1;
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
function explodeCmd(argsPerCmd, cmd, args) {
|
|
170
|
+
const cmds = [];
|
|
171
|
+
for (let i = 0; i < Math.floor(args.length / argsPerCmd); i++) {
|
|
172
|
+
if (i > 0) {
|
|
173
|
+
cmd = IMPLICIT_REPEAT_CMD[cmd] ?? cmd;
|
|
174
|
+
}
|
|
175
|
+
cmds.push([cmd, args.slice(i * argsPerCmd, (i + 1) * argsPerCmd)]);
|
|
176
|
+
}
|
|
177
|
+
return cmds;
|
|
178
|
+
}
|
|
179
|
+
// Exploded means repeated params are reported as separate commands,
|
|
180
|
+
// e.g. "M1,1 2,2 3,3" yields three steps when exploded.
|
|
181
|
+
function parseSvgPath(svgPath, exploded = false) {
|
|
182
|
+
const commandTuples = [];
|
|
183
|
+
const parts = svgPath.split(CMD_RE).slice(1);
|
|
184
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
185
|
+
const cmd = parts[i];
|
|
186
|
+
const rawArgs = (parts[i + 1] ?? '').trim();
|
|
187
|
+
const args = parseArgs(cmd, rawArgs);
|
|
188
|
+
const argsPerCmd = checkCmd(cmd, args);
|
|
189
|
+
if (argsPerCmd === 0 || !exploded) {
|
|
190
|
+
commandTuples.push([cmd, args]);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
commandTuples.push(...explodeCmd(argsPerCmd, cmd, args));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return commandTuples;
|
|
197
|
+
}
|
|
198
|
+
// ---- path command machinery ----
|
|
199
|
+
function addCmdToD(d, cmd, args) {
|
|
200
|
+
const snippet = pathSegment(cmd, ...args);
|
|
201
|
+
return d ? `${d} ${snippet}` : snippet;
|
|
202
|
+
}
|
|
203
|
+
function buildD(cmds) {
|
|
204
|
+
let d = '';
|
|
205
|
+
for (const [cmd, args] of cmds) {
|
|
206
|
+
d = addCmdToD(d, cmd, args);
|
|
207
|
+
}
|
|
208
|
+
return d;
|
|
209
|
+
}
|
|
210
|
+
function nextPos(currPos, cmd, cmdArgs) {
|
|
211
|
+
const [xCoordIdxs, yCoordIdxs] = cmdCoords(cmd);
|
|
212
|
+
let newX = currPos.x;
|
|
213
|
+
let newY = currPos.y;
|
|
214
|
+
if (cmd === cmd.toUpperCase()) {
|
|
215
|
+
if (xCoordIdxs.length) {
|
|
216
|
+
newX = 0;
|
|
217
|
+
}
|
|
218
|
+
if (yCoordIdxs.length) {
|
|
219
|
+
newY = 0;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (xCoordIdxs.length) {
|
|
223
|
+
newX += cmdArgs[xCoordIdxs[xCoordIdxs.length - 1]];
|
|
224
|
+
}
|
|
225
|
+
if (yCoordIdxs.length) {
|
|
226
|
+
newY += cmdArgs[yCoordIdxs[yCoordIdxs.length - 1]];
|
|
227
|
+
}
|
|
228
|
+
return (0, geometry_1.pt)(newX, newY);
|
|
229
|
+
}
|
|
230
|
+
function explicitLinesCallback(_subpathStart, currPos, cmd, args) {
|
|
231
|
+
let newArgs;
|
|
232
|
+
if (cmd === 'v') {
|
|
233
|
+
newArgs = [0, args[0]];
|
|
234
|
+
}
|
|
235
|
+
else if (cmd === 'V') {
|
|
236
|
+
newArgs = [currPos.x, args[0]];
|
|
237
|
+
}
|
|
238
|
+
else if (cmd === 'h') {
|
|
239
|
+
newArgs = [args[0], 0];
|
|
240
|
+
}
|
|
241
|
+
else if (cmd === 'H') {
|
|
242
|
+
newArgs = [args[0], currPos.y];
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
return [[cmd, [...args]]]; // nothing changes
|
|
246
|
+
}
|
|
247
|
+
return [[cmd === cmd.toLowerCase() ? 'l' : 'L', newArgs]];
|
|
248
|
+
}
|
|
249
|
+
function rewriteCoords(cmdConverter, coordConverter, currPos, cmd, args) {
|
|
250
|
+
const [xCoordIdxs, yCoordIdxs] = cmdCoords(cmd);
|
|
251
|
+
const desiredCmd = cmdConverter(cmd);
|
|
252
|
+
const newArgs = [...args];
|
|
253
|
+
if (cmd !== desiredCmd) {
|
|
254
|
+
cmd = desiredCmd;
|
|
255
|
+
for (const xCoordIdx of xCoordIdxs) {
|
|
256
|
+
newArgs[xCoordIdx] += coordConverter(currPos.x);
|
|
257
|
+
}
|
|
258
|
+
for (const yCoordIdx of yCoordIdxs) {
|
|
259
|
+
newArgs[yCoordIdx] += coordConverter(currPos.y);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return [cmd, newArgs];
|
|
263
|
+
}
|
|
264
|
+
function relativeToAbsolute(currPos, cmd, args) {
|
|
265
|
+
return rewriteCoords((c) => c.toUpperCase(), (scaler) => scaler, currPos, cmd, args);
|
|
266
|
+
}
|
|
267
|
+
function moveEndpoint(currPos, cmd, cmdArgs, newEndpoint) {
|
|
268
|
+
// we need to be able to alter both axes
|
|
269
|
+
const [[newCmd, newArgs]] = explicitLinesCallback(null, currPos, cmd, cmdArgs);
|
|
270
|
+
cmd = newCmd;
|
|
271
|
+
const args = [...newArgs];
|
|
272
|
+
const [xCoordIdxs, yCoordIdxs] = cmdCoords(cmd);
|
|
273
|
+
if (xCoordIdxs.length || yCoordIdxs.length) {
|
|
274
|
+
let { x: newX, y: newY } = newEndpoint;
|
|
275
|
+
if (cmd === cmd.toLowerCase()) {
|
|
276
|
+
newX = newX - currPos.x;
|
|
277
|
+
newY = newY - currPos.y;
|
|
278
|
+
}
|
|
279
|
+
args[xCoordIdxs[xCoordIdxs.length - 1]] = newX;
|
|
280
|
+
args[yCoordIdxs[yCoordIdxs.length - 1]] = newY;
|
|
281
|
+
}
|
|
282
|
+
return [cmd, args];
|
|
283
|
+
}
|
|
284
|
+
// Walk path and call callback to build potentially new commands.
|
|
285
|
+
// https://www.w3.org/TR/SVG11/paths.html
|
|
286
|
+
function walkPath(d, callback) {
|
|
287
|
+
let currPos = (0, geometry_1.pt)();
|
|
288
|
+
let subpathStartPos = currPos; // where a z will take you
|
|
289
|
+
const newCmds = [];
|
|
290
|
+
// iteration gives us exploded commands
|
|
291
|
+
const exploded = parseSvgPath(d, true);
|
|
292
|
+
for (let idx = 0; idx < exploded.length; idx++) {
|
|
293
|
+
// eslint-disable-next-line prefer-const
|
|
294
|
+
let [cmd, args] = exploded[idx];
|
|
295
|
+
checkCmd(cmd, args);
|
|
296
|
+
if (idx === 0 && cmd === 'm') {
|
|
297
|
+
cmd = 'M';
|
|
298
|
+
}
|
|
299
|
+
let prev = [null, null, null];
|
|
300
|
+
if (newCmds.length) {
|
|
301
|
+
prev = newCmds[newCmds.length - 1];
|
|
302
|
+
}
|
|
303
|
+
for (const [newCmd, newCmdArgs] of callback(subpathStartPos, currPos, cmd, args, ...prev)) {
|
|
304
|
+
let next;
|
|
305
|
+
if (newCmd.toLowerCase() !== 'z') {
|
|
306
|
+
next = nextPos(currPos, newCmd, newCmdArgs);
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
next = subpathStartPos;
|
|
310
|
+
}
|
|
311
|
+
const prevPos = currPos;
|
|
312
|
+
currPos = next;
|
|
313
|
+
if (newCmd.toUpperCase() === 'M') {
|
|
314
|
+
subpathStartPos = currPos;
|
|
315
|
+
}
|
|
316
|
+
newCmds.push([prevPos, newCmd, newCmdArgs]);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
let newD = '';
|
|
320
|
+
for (const [, cmd, args] of newCmds) {
|
|
321
|
+
newD = addCmdToD(newD, cmd, args);
|
|
322
|
+
}
|
|
323
|
+
return newD;
|
|
324
|
+
}
|
|
325
|
+
function rewritePath(d, rewriteFn) {
|
|
326
|
+
return walkPath(d, (subpathStart, currPos, cmd, args) => {
|
|
327
|
+
let [newCmd, newCmdArgs] = rewriteFn(currPos, cmd, args);
|
|
328
|
+
// if we modified cmd to pass *very* close to subpath start snap to it
|
|
329
|
+
// eliminates issues with not-quite-closed shapes due float imprecision
|
|
330
|
+
const next = nextPos(currPos, newCmd, newCmdArgs);
|
|
331
|
+
if (!(0, geometry_1.ptEquals)(next, subpathStart) && (0, geometry_1.ptAlmostEquals)(next, subpathStart)) {
|
|
332
|
+
[newCmd, newCmdArgs] = moveEndpoint(currPos, newCmd, newCmdArgs, subpathStart);
|
|
333
|
+
}
|
|
334
|
+
return [[newCmd, newCmdArgs]];
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
// equivalent path with only absolute commands
|
|
338
|
+
function absolutePath(d) {
|
|
339
|
+
return rewritePath(d, relativeToAbsolute);
|
|
340
|
+
}
|
|
341
|
+
// equivalent path with absolute moveto commands
|
|
342
|
+
function absoluteMoveto(d) {
|
|
343
|
+
return rewritePath(d, (currPos, cmd, args) => {
|
|
344
|
+
if (cmd === 'M' || cmd === 'm') {
|
|
345
|
+
return relativeToAbsolute(currPos, cmd, args);
|
|
346
|
+
}
|
|
347
|
+
return [cmd, [...args]];
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
// replace all vertical/horizontal lines with line to (x,y)
|
|
351
|
+
function explicitLines(d) {
|
|
352
|
+
return walkPath(d, explicitLinesCallback);
|
|
353
|
+
}
|
|
354
|
+
// Rewrite commands that imply knowledge of prior commands arguments;
|
|
355
|
+
// in particular shorthand quadratic and bezier curves become explicit.
|
|
356
|
+
// See https://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands.
|
|
357
|
+
function expandShorthand(d) {
|
|
358
|
+
const shortToLong = { S: 'C', T: 'Q' };
|
|
359
|
+
return walkPath(d, (_, currPos, cmd, args, prevPos, prevCmd, prevArgs) => {
|
|
360
|
+
if (!(cmd.toUpperCase() in shortToLong)) {
|
|
361
|
+
return [[cmd, args]];
|
|
362
|
+
}
|
|
363
|
+
if (cmd === cmd.toLowerCase()) {
|
|
364
|
+
[cmd, args] = relativeToAbsolute(currPos, cmd, args);
|
|
365
|
+
}
|
|
366
|
+
// if there is no prev, or a bad prev, control point coincident current
|
|
367
|
+
let newCp = [currPos.x, currPos.y];
|
|
368
|
+
if (prevCmd) {
|
|
369
|
+
if (prevCmd === prevCmd.toLowerCase()) {
|
|
370
|
+
[prevCmd, prevArgs] = relativeToAbsolute(prevPos, prevCmd, prevArgs);
|
|
371
|
+
}
|
|
372
|
+
if (Object.values(shortToLong).includes(prevCmd)) {
|
|
373
|
+
// reflect 2nd-last x,y pair over curr_pos and make it our first arg
|
|
374
|
+
const prevCpX = prevArgs[prevArgs.length - 4];
|
|
375
|
+
const prevCpY = prevArgs[prevArgs.length - 3];
|
|
376
|
+
newCp = [2 * currPos.x - prevCpX, 2 * currPos.y - prevCpY];
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return [[shortToLong[cmd], [...newCp, ...args]]];
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
// replace all arcs with similar cubics
|
|
383
|
+
function arcsToCubics(d) {
|
|
384
|
+
return walkPath(d, (_subpathStart, currPos, cmd, args) => {
|
|
385
|
+
if (cmd !== 'a' && cmd !== 'A') {
|
|
386
|
+
// no work to do
|
|
387
|
+
return [[cmd, args]];
|
|
388
|
+
}
|
|
389
|
+
const rx = args[0];
|
|
390
|
+
const ry = args[1];
|
|
391
|
+
const xRotation = args[2];
|
|
392
|
+
const large = args[3];
|
|
393
|
+
const sweep = args[4];
|
|
394
|
+
let endX = args[5];
|
|
395
|
+
let endY = args[6];
|
|
396
|
+
if (cmd === 'a') {
|
|
397
|
+
endX += currPos.x;
|
|
398
|
+
endY += currPos.y;
|
|
399
|
+
}
|
|
400
|
+
const result = [];
|
|
401
|
+
for (const [p1, p2, target] of (0, arcs_1.arcToCubic)(currPos, rx, ry, xRotation, large, sweep, (0, geometry_1.pt)(endX, endY))) {
|
|
402
|
+
const { x, y } = target;
|
|
403
|
+
if (p1 !== null) {
|
|
404
|
+
result.push(['C', [p1.x, p1.y, p2.x, p2.y, x, y]]);
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
result.push(['L', [x, y]]);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
// split into independent subpath d strings (movetos made absolute first)
|
|
414
|
+
function subpaths(d) {
|
|
415
|
+
const result = [''];
|
|
416
|
+
walkPath(absoluteMoveto(d), (_subpathStart, _currPos, cmd, args) => {
|
|
417
|
+
if (cmd.toUpperCase() === 'M') {
|
|
418
|
+
result.push('');
|
|
419
|
+
}
|
|
420
|
+
result[result.length - 1] = addCmdToD(result[result.length - 1], cmd, args);
|
|
421
|
+
if (cmd.toUpperCase() === 'Z') {
|
|
422
|
+
result.push('');
|
|
423
|
+
}
|
|
424
|
+
return [[cmd, args]];
|
|
425
|
+
});
|
|
426
|
+
return result.filter((s) => s);
|
|
427
|
+
}
|
|
428
|
+
// round all floats in the d string to the given decimal digits
|
|
429
|
+
function roundFloatsD(d, ndigits) {
|
|
430
|
+
let out = '';
|
|
431
|
+
for (const [cmd, args] of parseSvgPath(d)) {
|
|
432
|
+
out = addCmdToD(out, cmd, args.map((n) => (0, geometry_1.pythonRound)(n, ndigits)));
|
|
433
|
+
}
|
|
434
|
+
return out;
|
|
435
|
+
}
|
|
436
|
+
// hHvV => lL, S/T => C/Q, relative => absolute, arcs => cubics
|
|
437
|
+
function asCmdSeq(d) {
|
|
438
|
+
return parseSvgPath(arcsToCubics(absolutePath(expandShorthand(explicitLines(d)))), true);
|
|
439
|
+
}
|