Convert Figma logo to code with AI

RazrFalcon logosvgcleaner

svgcleaner could help you to clean up your SVG files from the unnecessary data.

1,624
93
1,624
104

Top Related Projects

20,817

⚙️ Node.js tool for optimizing SVG files

Project Website: https://inkscape.org - Code Repository: https://gitlab.com/inkscape/inkscape - Draw freely. 🖌

3,645

JavaScript SVG parser and renderer on Canvas

Quick Overview

SVGCleaner is a tool for cleaning and optimizing SVG files. It removes unnecessary data, reduces file size, and improves rendering performance while preserving the original appearance of the image. SVGCleaner is written in Rust, making it fast and efficient.

Pros

  • High performance due to Rust implementation
  • Extensive cleaning and optimization options
  • Supports both CLI and GUI interfaces
  • Actively maintained and regularly updated

Cons

  • May occasionally remove necessary data in complex SVGs
  • Limited support for some advanced SVG features
  • Requires Rust knowledge for contributing or modifying
  • GUI version not available for all platforms

Code Examples

SVGCleaner is primarily a standalone tool, not a code library. However, it can be integrated into Rust projects using its library interface. Here are a few examples:

use svgcleaner::{clean_doc, CleaningOptions};

let svg_content = r#"<svg>...</svg>"#;
let mut options = CleaningOptions::default();
options.remove_unused_defs = true;

let cleaned_svg = clean_doc(svg_content, &options).unwrap();
println!("Cleaned SVG: {}", cleaned_svg);

This example demonstrates basic usage of SVGCleaner in a Rust program.

use svgcleaner::{clean_doc, CleaningOptions};

let svg_content = r#"<svg>...</svg>"#;
let mut options = CleaningOptions::default();
options.remove_metadata = true;
options.remove_comments = true;
options.compact_paths = true;

let cleaned_svg = clean_doc(svg_content, &options).unwrap();
println!("Cleaned SVG: {}", cleaned_svg);

This example shows how to customize cleaning options for more specific optimization needs.

Getting Started

To use SVGCleaner as a command-line tool:

  1. Install Rust: https://www.rust-lang.org/tools/install
  2. Install SVGCleaner:
    cargo install svgcleaner
    
  3. Clean an SVG file:
    svgcleaner input.svg output.svg
    

To use SVGCleaner in a Rust project:

  1. Add to your Cargo.toml:
    [dependencies]
    svgcleaner = "0.9.5"
    
  2. Use in your Rust code as shown in the code examples above.

Competitor Comparisons

20,817

⚙️ Node.js tool for optimizing SVG files

Pros of svgo

  • More actively maintained with frequent updates
  • Broader feature set, including plugins for custom optimizations
  • Supports both CLI and API usage, offering greater flexibility

Cons of svgo

  • Slower performance compared to svgcleaner
  • May occasionally produce invalid SVG output in complex cases
  • Larger codebase, potentially more difficult to contribute to or customize

Code comparison

svgo:

const svgo = require('svgo');

const result = svgo.optimize(svgString, {
  plugins: ['removeDoctype', 'removeXMLProcInst', 'removeComments']
});
console.log(result.data);

svgcleaner:

use svgcleaner::clean_from_str;

let (cleaned_svg, _) = clean_from_str(svg_string, &opt).unwrap();
println!("{}", cleaned_svg);

Both tools aim to optimize SVG files, but svgo offers more customization options through its plugin system, while svgcleaner focuses on performance and simplicity. svgo is written in JavaScript, making it more accessible for web developers, whereas svgcleaner is implemented in Rust, providing better performance but potentially higher barrier to entry for contributions.

Project Website: https://inkscape.org - Code Repository: https://gitlab.com/inkscape/inkscape - Draw freely. 🖌

Pros of Inkscape

  • Full-featured vector graphics editor with a wide range of tools and capabilities
  • Supports complex SVG features and advanced editing functions
  • Active community and regular updates

Cons of Inkscape

  • Larger file size and resource-intensive compared to svgcleaner
  • May be overkill for simple SVG optimization tasks
  • Slower processing speed for batch operations

Code Comparison

Inkscape (Python script for CLI usage):

import inkex

class SimplifyPath(inkex.EffectExtension):
    def add_arguments(self, pars):
        pars.add_argument("--threshold", type=float, default=0.1)

    def effect(self):
        for elem in self.svg.selected.values():
            if elem.tag == inkex.addNS('path', 'svg'):
                elem.path = elem.path.simplify(self.options.threshold)

if __name__ == '__main__':
    SimplifyPath().run()

svgcleaner (Rust code for path simplification):

use svgcleaner::cleaner;

fn main() {
    let input = std::fs::read_to_string("input.svg").unwrap();
    let mut opt = cleaner::Options::default();
    opt.simplify_paths = true;
    let output = cleaner::clean_from_str(&input, &opt).unwrap();
    std::fs::write("output.svg", output).unwrap();
}
3,645

JavaScript SVG parser and renderer on Canvas

Pros of canvg

  • Renders SVG to Canvas, enabling broader browser support and performance optimization
  • Supports parsing and rendering SVG from strings or existing DOM elements
  • Offers a more comprehensive SVG rendering solution, including CSS styling and animations

Cons of canvg

  • Larger project scope and codebase, potentially more complex to integrate and maintain
  • May have higher performance overhead for simple SVG processing tasks
  • Not specifically designed for SVG optimization or cleaning

Code Comparison

svgcleaner:

let mut opt = Cleaner::default();
opt.set_remove_unused_defs(true);
opt.set_convert_shapes(true);
let result = cleaner::clean_doc(&doc, &opt)?;

canvg:

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const v = await canvg.Canvg.from(ctx, svgString);
await v.render();

While svgcleaner focuses on optimizing and cleaning SVG files, canvg is primarily designed for rendering SVG to Canvas. svgcleaner's code demonstrates configuration for cleaning operations, whereas canvg's code shows the process of rendering SVG to a canvas element. The projects serve different primary purposes, with svgcleaner being more suitable for SVG optimization tasks and canvg for SVG rendering and broader compatibility.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

========================= svgcleaner |build status|

.. |build status| image:: https://travis-ci.org/RazrFalcon/svgcleaner.svg?branch=master :target: https://travis-ci.org/RazrFalcon/svgcleaner

svgcleaner helps you clean up your SVG files, keeping them free from unnecessary data.

|

.. contents:: Table of Contents

Purpose

The main purpose of svgcleaner is to losslessly reduce the size of an SVG image, created in a vector editing application, before publishing.

Usually more than half of an SVG image's data is useless for rendering. For example:

  • Temporary data used by the vector editing application
  • Non-optimal SVG structure representation
  • Unused and invisible graphical elements

... Are all unnecessary in a published SVG.

Goals

#. Correctness - svgcleaner should not break an SVG file #. Cleaning ratio - Higher is better #. Performance - An average SVG file processing time should be closer to ~1ms on a modern PC

Alternatives

  • svgo_
  • scour_

.. _svgo: https://github.com/svg/svgo .. _scour: https://github.com/scour-project/scour

In addition to the main advantages of svgcleaner — like correctness, cleaning ratio and performance, which are described below — there are some more nuances to be aware of:

#. svgcleaner cleans only one SVG file. It doesn't process SVGZ files. It doesn't process directories. It doesn't do anything else. Just one task*. #. svgcleaner is strictly lossless by default. There are no destructive cleaning options enabled by default. #. svgcleaner is portable. You can build it into a single executable without any external dependencies.

* You can get all of these features using the GUI <https://github.com/RazrFalcon/svgcleaner-gui>_.

Charts

See Testing notes <docs/testing_notes.rst>_ for details.

All stats were collected using the latest release version.

Correctness


*Less is better.*

.. image:: docs/images/correctness_chart_W3C_SVG_11_TestSuite.svg

.. image:: docs/images/correctness_chart_oxygen.svg

Cleaning ratio

More is better.

.. image:: docs/images/ratio_chart_W3C_SVG_11_TestSuite.svg

* scour_ creates bigger files (-18.78%).

.. image:: docs/images/ratio_chart_oxygen.svg

Cleaning time


*Less is better.*

.. image:: docs/images/performance_chart_W3C_SVG_11_TestSuite.svg

.. image:: docs/images/performance_chart_oxygen.svg

Other
`````

=========================  =================  ================  =========
Collection                 Size Before (MiB)  Size After (MiB)  Ratio (%)
=========================  =================  ================  =========
`Breeze icons theme`_      21.72              11.09             48.91
`Ardis icons theme`_       11.82              5.23              55.74
`Humanity icons theme`_    8.56               3.08              64.01
`Open Icon Library`_       207.45             69.44             66.53
`Elementary icons theme`_  17.72              7.09              59.95
`Adwaita icons theme`_     2.21               0.37              83.08
`Faience icon theme`_      22.35              11.14             50.16
`GCP Icons`_               0.344              0.082             75.9
=========================  =================  ================  =========

.. _`Breeze icons theme`: https://github.com/KDE/breeze-icons
.. _`Ardis icons theme`: https://github.com/Nitrux/ardis-icon-theme
.. _`Humanity icons theme`: https://wiki.ubuntu.com/Artwork/Incoming/Karmic/Humanity_Icons?action=AttachFile&do=view&target=humanity_2.1.tar.gz
.. _`Open Icon Library`: https://sourceforge.net/projects/openiconlibrary
.. _`Elementary icons theme`: https://github.com/elementary/icons
.. _`Adwaita icons theme`: https://github.com/GNOME/adwaita-icon-theme
.. _`Faience icon theme`: https://github.com/csharris/faience-icon-theme
.. _`GCP Icons`: https://cloud.google.com/icons/files/google-cloud-icons.zip

Documentation
-------------

Documentation can be found `here <docs/svgcleaner.adoc>`_.

Limitations
-----------

*svgcleaner* shouldn't change your file unless you tell it to, but there are still
things that can't be preserved. So even if you disable all cleaning options there are still things
that will be changed, such as:

- Original indent is not preserved
- All colors will be formatted as #RRGGBB and #RGB
- DOCTYPE, CDATA will be processed and removed
- CSS support is minimal
- CSS from the ``style`` element will be extracted and processes. The ``style`` element will be removed.
- The ``style`` attribute will be split into attributes
- The ``class`` attribute will be processed and removed
- Paths and transformations will be reformatted
- ``currentColor`` and ``inherit`` attributes values will be resolved
- Referenced elements will be moved to the ``defs`` element
- IRI and FuncIRI attributes that reference non-existing objects will be removed
- If the ``offset`` attribute value of the ``stop`` element is represented as percentage - it will be
  converted into a decimal number

Usage
-----

CLI
```

::

  svgcleaner in.svg out.svg


Change default options:

::

  svgcleaner --indent=2 --paths-coordinates-precision=5 --join-arcto-flags=yes in.svg out.svg

Use ``--help`` for a list of the cleaning options and see the
`documentation <docs/svgcleaner.adoc>`_ for more details.

GUI
```

You can get a GUI `here <https://github.com/RazrFalcon/svgcleaner-gui>`_.

Downloads
---------

You can get prebuilt packages `here <https://github.com/RazrFalcon/svgcleaner-gui/releases>`_.

Building
--------

Dependency: `Rust <https://www.rust-lang.org/>`_

::

  cargo build --release


If you're a Rust programmer, you can install *svgcleaner* using:

::

  cargo install svgcleaner

Contributing and Issues
-----------------------

See `CONTRIBUTING.md <CONTRIBUTING.md>`_ for details.

FAQ
---

See `FAQ.md <FAQ.md>`_ for details.

License
-------

*svgcleaner* is licensed under the `GPL-2.0 <https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html>`_.