Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[archive] CLI-tool svgSLIM #29

Open
tobwen opened this issue Sep 28, 2020 · 0 comments
Open

[archive] CLI-tool svgSLIM #29

tobwen opened this issue Sep 28, 2020 · 0 comments
Labels

Comments

@tobwen
Copy link

tobwen commented Sep 28, 2020

summary

I've written a small CLI slimmer. As you can see, I'm not an advanced JS-coder, but perhaps it helps others.

usage

node svgSlim.js pictogramm.svg > new.svg
<othertool> | node svgSlim.js > new.svg
<othertool> | node svgSlim.js /dev/stdin > new.svg
<othertool> | node svgSlim.js - > new.svg
cat pictogramm.svg | node svgSlim.js > new.svg
< pictogramm.svg node svgSlim.js > new.svg

code

'use strict';

function MergeRecursive(obj1, obj2) {
    // based on https://stackoverflow.com/a/383245 - (c) users Markus etc. under CC BY-SA 3.0
    for (let p in obj2) {
        try {
            // property in destination object set; update its value
            if (obj2[p].constructor == Object) {
                obj1[p] = MergeRecursive(obj1[p], obj2[p]);
            } else {
                obj1[p] = obj2[p];
            }
        } catch(e) {
            // property in destination object not set; create it and set its value
            obj1[p] = obj2[p];
        }
    }
    return obj1;
}

const defaultConfig = {
    "rules": {
        "collapse-g": true,
        "collapse-textwrap": true,
        "combine-path": [ false, {
                "disregardFill": false,
                "disregardOpacity": false
            }
        ],
        "combine-transform": true,
        "compute-path": true,
        "rm-attribute": [ true, {
                "keepAria": false,
                "keepEvent": false
            }
        ],
        "rm-comments": true,
        "rm-doctype": true,
        "rm-hidden": true,
        "rm-irregular-nesting": [ true, {
                "ignore": []
            }
        ],
        "rm-irregular-tag": [ true, {
                "ignore": []
            }
        ],
        "rm-px": true,
        "rm-unnecessary": [ true, {
                "tags": [
                    "desc",
                    "discard",
                    "foreignObject",
                    "video",
                    "audio",
                    "iframe",
                    "canvas",
                    "metadata",
                    "script",
                    "title",
                    "unknown",
                    "image"
                ]
            }
        ],
        "rm-version": true,
        "rm-viewbox": true,
        "rm-xml-decl": true,
        "rm-xmlns": true,
        "shorten-animate": [ true, {
                "remove": false
            }
        ],
        "shorten-class": true,
        "shorten-color": [ true, {
                "rrggbbaa": false
            }
        ],
        "shorten-decimal-digits": true,
        "shorten-defs": true,
        "shorten-filter": true,
        "shorten-id": true,
        "shorten-shape": true,
        "shorten-style-attr": true,
        "shorten-style-tag": [ true, {
                "deepShorten": true
            }
        ],
    },
    "params": {
        "sizeDigit": 2,
        "angelDigit": 2,
        "trifuncDigit": 3,
        "opacityDigit": 3,
        "thinning": 0,
        "straighten": 0,
        "mergePoint": 0,
        "exchangeStyle": false,
        "rmAttrEqDefault": true
    },
    "browsers": ["defaults"]
};

const userConfig = {
    "params": {
        "sizeDigit": 5,
        "angelDigit": 5,
        "trifuncDigit": 5
    }
};

const fs = require('fs');
const svgSlimming = require('svg-slim');
const fileOrStdin = require('file-or-stdin');
const args = process.argv.slice(2);

async function main() {
    const svgFN = (args[0] === '-') ? '/dev/stdin' : args[0];
    
    try {
        const svgcode = await fileOrStdin(svgFN, 'utf8');
        try {
            const config = MergeRecursive(defaultConfig, userConfig);
            const result = await svgSlimming(svgcode, config)
            console.log(result);
        } catch(e) {
            console.error('Error while slimming. ' + e);
        }
    } catch(e) {
        console.error('File not found. ' + e);
    }
    return;
}

main();
@benboba benboba added the archive label Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants