### Install Svgcleaner with Cargo Source: https://github.com/razrfalcon/svgcleaner/blob/master/README.rst Instructions for Rust programmers to install the svgcleaner tool globally using Cargo. ```bash cargo install svgcleaner ``` -------------------------------- ### SVG Path Notation Examples Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/testing_notes.rst Examples of valid SVG path notations that may cause compatibility issues with some rendering applications. ```text M 10 -20 A 5.5 0.3 -4 1 1 0 -0.1 ``` ```text M10-20A5.5.3-4 110-.1 ``` -------------------------------- ### Install svgrender dependencies Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/svgrender/README.md Install the required puppeteer package via npm. ```bash npm i puppeteer ``` -------------------------------- ### Configure Svgcleaner Options Source: https://github.com/razrfalcon/svgcleaner/blob/master/README.rst Example of how to change default options for svgcleaner, such as indentation and path coordinate precision. ```bash svgcleaner --indent=2 --paths-coordinates-precision=5 --join-arcto-flags=yes in.svg out.svg ``` -------------------------------- ### SVG Path Segment Optimization Example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/convert-segments.adoc Demonstrates the transformation of path data into a more compact format using supported segment conversions. ```xml ``` ```xml ``` -------------------------------- ### SVG Cleaner CLI Usage Source: https://github.com/razrfalcon/svgcleaner/blob/master/data/help.txt Basic usage and examples for the svgcleaner command-line tool. ```APIDOC ## SVG Cleaner CLI Usage ### Description svgcleaner is a command-line tool designed to clean up SVG files by removing unnecessary data and optimizing their structure. It offers a wide range of options to customize the cleaning process. ### Method Command Line Interface ### Endpoint N/A ### Parameters #### Path Parameters - **in-file** (string) - Required - The input SVG file path. - **out-file** (string) - Required - The output SVG file path. #### Query Parameters N/A #### Request Body N/A ### Flags - **-h, --help** (boolean) - Print help information. - **-V, --version** (boolean) - Print version information. - **-c, --stdout** (boolean) - Print result to the standard output. - **--no-defaults** (boolean) - Reset default flags to 'no'. - **--multipass** (boolean) - Clean a file multiple times. - **--allow-bigger-file** (boolean) - Allow bigger file. - **--copy-on-error** (boolean) - Copy an original file to the destination on error. - **--quiet** (boolean) - Show only warnings and errors. ### Options #### Elements - **--remove-comments** (FLAG) - Remove XML comments [default: yes] - **--remove-declarations** (FLAG) - Remove XML declarations [default: yes] - **--remove-nonsvg-elements** (FLAG) - Remove non-SVG elements [default: yes] - **--remove-unused-defs** (FLAG) - Remove unused referenced elements [default: yes] - **--convert-shapes** (FLAG) - Convert basic shapes into paths [default: yes] - **--remove-title** (FLAG) - Remove 'title' element [default: yes] - **--remove-desc** (FLAG) - Remove 'desc' element [default: yes] - **--remove-metadata** (FLAG) - Remove 'metadata' element [default: yes] - **--remove-dupl-lineargradient** (FLAG) - Remove duplicated 'linearGradient' elements [default: yes] - **--remove-dupl-radialgradient** (FLAG) - Remove duplicated 'radialGradient' elements [default: yes] - **--remove-dupl-fegaussianblur** (FLAG) - Remove duplicated 'feGaussianBlur' elements [default: yes] - **--ungroup-groups** (FLAG) - Ungroup groups [default: yes] - **--ungroup-defs** (FLAG) - Ungroup 'defs' element [default: yes] - **--group-by-style** (FLAG) - Group elements by equal styles [default: yes] - **--merge-gradients** (FLAG) - Merge gradients [default: yes] - **--regroup-gradient-stops** (FLAG) - Regroup gradient 'stop' elements [default: yes] - **--remove-invalid-stops** (FLAG) - Remove invalid 'stop' elements [default: yes] - **--remove-invisible-elements** (FLAG) - Remove invisible elements [default: yes] - **--resolve-use** (FLAG) - Resolve 'use' elements [default: yes] #### Attributes - **--remove-version** (FLAG) - Remove 'version' and 'baseProfile' attributes [default: yes] - **--remove-nonsvg-attributes** (FLAG) - Remove non-SVG attributes [default: yes] - **--remove-unreferenced-ids** (FLAG) - Remove unreferenced 'id' attributes [default: yes] - **--trim-ids** (FLAG) - Trim 'id' attributes [default: yes] - **--remove-text-attributes** (FLAG) - Remove text-related attributes if there is no text [default: yes] - **--remove-unused-coordinates** (FLAG) - Remove unused coordinate attributes [default: yes] - **--remove-default-attributes** (FLAG) - Remove attributes with default values [default: yes] - **--remove-xmlns-xlink-attribute** (FLAG) - Remove an unused 'xmlns:xlink' attribute [default: yes] - **--remove-needless-attributes** (FLAG) - Remove attributes that doesn't belong to this element [default: yes] - **--remove-gradient-attributes** (FLAG) - Remove inheritable gradient attributes [default: no] - **--join-style-attributes** (VALUE) - Join presentational attributes [values: no, some, all] [default: some] - **--apply-transform-to-gradients** (FLAG) - Apply transformations to gradients [default: yes] - **--apply-transform-to-shapes** (FLAG) - Apply transformations to shapes [default: yes] - **--remove-unresolved-classes** (FLAG) - Remove unresolved classes from 'class' attributes [default: yes] #### Paths - **--paths-to-relative** (FLAG) - Convert path segments into relative ones [default: yes] - **--remove-unused-segments** (FLAG) - Remove unused path segments [default: yes] - **--convert-segments** (FLAG) - Convert path segments into shorter ones [default: yes] - **--apply-transform-to-paths** (FLAG) - Apply transformations to paths [default: no] - **--trim-paths** (FLAG) - Use compact notation for paths [default: yes] - **--join-arcto-flags** (FLAG) - Join ArcTo flags [default: no] ### Request Example ```bash svgcleaner in.svg out.svg svgcleaner --indent=2 --paths-coordinates-precision=5 --join-arcto-flags=yes in.svg out.svg svgcleaner --no-defaults --remove-comments=yes in.svg out.svg cat in.svg | svgcleaner -c - > out.svg ``` ### Response N/A (This is a command-line tool, output is to stdout or a file.) ``` -------------------------------- ### Generate comparative statistics for different tools Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/stats/README.md Examples for running statistics against different versions of svgcleaner or svgo. Ensure the --type flag matches the tool being executed. ```bash # svgcleaner 0.7.0 stats \ --workdir=/tmp/svg/ \ --input-data=/path_to_svg_dir \ --cleaner=/path_to_svgcleaner \ --type=svgcleaner \ --threshold=2 # svgcleaner 0.6.2 stats \ --workdir=/tmp/svg/ \ --input-data=/path_to_svg_dir \ --cleaner=/path_to_old_svgcleaner \ --type=svgcleaner-old \ --threshold=2 # svgo stats \ --workdir=/tmp/svg/ \ --input-data=/path_to_svg_dir \ --cleaner=/path_to_svgo \ --type=svgo \ --threshold=2 ``` -------------------------------- ### Clean SVG files via CLI Source: https://github.com/razrfalcon/svgcleaner/blob/master/data/help.txt Examples of using the svgcleaner command to process SVG files with different configuration options. ```bash svgcleaner in.svg out.svg ``` ```bash svgcleaner --indent=2 --paths-coordinates-precision=5 --join-arcto-flags=yes in.svg out.svg ``` ```bash svgcleaner --no-defaults --remove-comments=yes in.svg out.svg ``` ```bash cat in.svg | svgcleaner -c - > out.svg ``` -------------------------------- ### Group elements by equal styles example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/group-by-style.adoc Demonstrates the transformation of individual circle elements with identical fill attributes into a grouped structure. ```xml ``` ```xml ``` -------------------------------- ### SVG basic shapes conversion Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/convert-shapes.adoc Example showing the transformation of standard SVG shapes into path elements. ```xml ``` ```xml ``` -------------------------------- ### Trim id attributes example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/trim-ids.adoc Demonstrates the transformation of long id attributes into shorter, encoded identifiers. ```xml ``` ```xml ``` -------------------------------- ### SVG with Radial Gradient Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.rst Example of an SVG structure containing a radial gradient definition and its application to a circle. ```XML ``` -------------------------------- ### Remove metadata element from SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-metadata.adoc Example showing an SVG file before and after the removal of the metadata element. ```xml image/svg+xml ``` ```xml ``` -------------------------------- ### Run SVG statistics processing Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/stats/README.md Executes the stats command with required configuration parameters. Requires ImageMagick's compare utility to be installed on the system. ```bash stats \ # path to folder where all temp files will be generated --workdir=/tmp/svg/ \ --input-data=/path_to_dir_with_svg_files \ # one of: svgcleaner, svgcleaner-old, svgo --type=svgcleaner \ --cleaner=/path_to_cleaner_exe \ # 2% (0..100) --threshold=2 ``` -------------------------------- ### Remove unused path segments example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-unused-segments.adoc Demonstrates the transformation of an SVG path with redundant segments into a simplified version. Requires the --paths-to-relative flag. ```xml ``` ```xml ``` -------------------------------- ### Remove inheritable gradient attributes example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-gradient-attributes.adoc Demonstrates the transformation of SVG gradients where redundant gradientUnits attributes are removed from child elements. ```xml ``` ```xml ``` -------------------------------- ### SVG Number List Separator Transformation Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/list-separator.adoc Example showing the transformation of a polygon points attribute from comma-separated to space-separated values. ```xml ``` ```xml ``` -------------------------------- ### Ungroup SVG Groups Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/ungroup-groups.adoc This example shows an SVG with nested groups that can be simplified by ungrouping. Use this when groups are empty, contain only one child, or lack important attributes. ```svg ``` ```svg ``` -------------------------------- ### SVG Gradient Without Transformation Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/apply-transform-to-gradients.adoc This example shows a basic SVG linear gradient without any `gradientTransform` attribute, serving as a baseline for comparison. ```svg ``` -------------------------------- ### SVG Elliptical Arc with Correct Flags Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/join-arcto-flags.adoc This example shows a correctly formatted elliptical arc command with space-separated flags. Use this format for compatibility with most SVG renderers. ```svg ``` -------------------------------- ### Run SVG Cleaner Stats Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/files-testing/README.md Use this command to clean all SVG files in a selected folder and verify their cleaning status. Ensure you have the necessary dependencies like imagemagick and prebuilt svgcleaner tools installed. ```bash stats \ # path to folder where all temp files will be generated --workdir=/var/svg/svg/ \ --input-data=/path_to_dir_with_svg_files \ # (optional) path to config that contains list of ignored files # see ./data for examples --input-data-config=config.json \ # each input-data must have own db --cache-db=/path_to_cache_file/stats.sqlite ``` -------------------------------- ### Original SVG with Duplicate Gradients Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/regroup-gradient-stops.adoc This SVG contains two separate linearGradient definitions with identical stop elements. Use this as a starting point for optimization. ```svg ``` -------------------------------- ### SVG Elliptical Arc with Compact Flags Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/join-arcto-flags.adoc This example demonstrates an elliptical arc command where flags are concatenated without spaces. SVG Cleaner can process this format, but it may be unsupported by older SVG renderers. ```svg ``` -------------------------------- ### Configure custom cleaning options via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Fine-tune the optimization process by adjusting precision, indentation, or toggling specific features. ```bash # Set custom indentation and precision options svgcleaner --indent=2 --paths-coordinates-precision=5 --join-arcto-flags=yes input.svg output.svg # Disable specific optimizations svgcleaner --remove-title=no --remove-desc=no input.svg output.svg # Apply transform to paths (disabled by default, may increase file size) svgcleaner --apply-transform-to-paths=yes input.svg output.svg ``` -------------------------------- ### Generate svgcleaner-gui documentation Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/docgen/README.md Generates documentation specifically for the GUI component, outputting to the designated directory. ```bash docgen --docdir=/svgcleaner/docs/ --for-gui --outdir=/tmp ``` -------------------------------- ### Execute minimal cleaning via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Disable all default optimizations and selectively enable only required features. ```bash # Reset all defaults to 'no', then enable specific options svgcleaner --no-defaults --remove-comments=yes --trim-paths=yes input.svg output.svg # Minimal cleaning - only remove comments and declarations svgcleaner --no-defaults --remove-comments=yes --remove-declarations=yes input.svg output.svg ``` -------------------------------- ### Remove text-related attributes from SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-text-attributes.adoc Example showing the removal of the 'font' attribute from a circle element when it is unused. ```xml Text ``` ```xml Text ``` -------------------------------- ### Remove non-SVG attributes example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-nonsvg-attributes.adoc Demonstrates the transformation of an SVG element by removing the custom 'my-attribute' attribute. ```xml ``` ```xml ``` -------------------------------- ### Generate svgcleaner documentation Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/docgen/README.md Creates the svgcleaner.adoc file in the specified directory. ```bash docgen --docdir=/svgcleaner/docs/ ``` -------------------------------- ### Render SVG to PNG Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/svgrender/README.md Execute the screenshot script with input file, output file, and width parameters. ```bash node screenshot.js in.svg out.png 500 ``` -------------------------------- ### SVG Path Before Removing Duplicate Commands Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.adoc Illustrates an SVG path with redundant 'L' commands before optimization. ```xml ``` -------------------------------- ### Configure XML node indentation Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/indent.adoc Demonstrates the difference between indented and non-indented XML output. ```xml ``` ```xml ``` -------------------------------- ### Remove unreferenced id attributes example Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-unreferenced-ids.adoc Demonstrates the transformation of an SVG document by removing the id attribute from elements that are not referenced by other parts of the SVG. ```xml ``` ```xml ``` -------------------------------- ### Run Svgcleaner CLI Source: https://github.com/razrfalcon/svgcleaner/blob/master/README.rst Basic usage of the svgcleaner command-line tool to process an SVG file. ```bash svgcleaner in.svg out.svg ``` -------------------------------- ### Clean SVG files via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Basic usage for cleaning SVG files using default optimization settings. ```bash # Basic usage - clean file with default options svgcleaner input.svg output.svg # Output shows compression ratio # Your image is 45.23% smaller now. ``` -------------------------------- ### Complete SVG Cleaning Pipeline in Rust Source: https://context7.com/razrfalcon/svgcleaner/llms.txt This Rust function demonstrates the full SVG cleaning process, from loading a file to parsing, cleaning with extensive options, and writing the optimized output. It calculates and returns the compression ratio. Ensure input and output file paths are valid. ```rust use svgcleaner::cleaner; use svgcleaner::{CleaningOptions, StyleJoinMode, ParseOptions, WriteOptions}; use svgdom::{Indent, ListSeparator}; use std::str; fn clean_svg(input_path: &str, output_path: &str) -> Result> { // Load input let raw_data = cleaner::load_file(input_path)?; let input_size = raw_data.len(); // Configure parsing (skip elements we want to remove) let parse_opts = ParseOptions { parse_comments: false, parse_declarations: false, parse_unknown_elements: false, parse_unknown_attributes: false, parse_px_unit: false, skip_unresolved_classes: true, skip_invalid_attributes: false, skip_invalid_css: false, skip_paint_fallback: false, }; // Parse document let mut doc = cleaner::parse_data(&raw_data, &parse_opts)?; // Configure cleaning with all optimizations let cleaning_opts = CleaningOptions { remove_unused_defs: true, convert_shapes: true, remove_title: true, remove_desc: true, remove_metadata: true, remove_dupl_linear_gradients: true, remove_dupl_radial_gradients: true, remove_dupl_fe_gaussian_blur: true, ungroup_groups: true, ungroup_defs: true, group_by_style: true, merge_gradients: true, regroup_gradient_stops: true, remove_invalid_stops: true, remove_invisible_elements: true, resolve_use: true, remove_version: true, remove_unreferenced_ids: true, trim_ids: true, remove_text_attributes: true, remove_unused_coordinates: true, remove_default_attributes: true, remove_xmlns_xlink_attribute: true, remove_needless_attributes: true, remove_gradient_attributes: false, join_style_attributes: StyleJoinMode::Some, apply_transform_to_gradients: true, apply_transform_to_shapes: true, paths_to_relative: true, remove_unused_segments: true, convert_segments: true, apply_transform_to_paths: false, append_newline: false, coordinates_precision: 6, properties_precision: 6, paths_coordinates_precision: 8, transforms_precision: 8, }; // Configure output let write_opts = WriteOptions { indent: Indent::None, attributes_indent: Indent::None, use_single_quote: false, trim_hex_colors: true, write_hidden_attributes: false, remove_leading_zero: true, use_compact_path_notation: true, join_arc_to_flags: false, remove_duplicated_path_commands: true, use_implicit_lineto_commands: true, simplify_transform_matrices: true, list_separator: ListSeparator::Space, ..Default::default() }; // Clean document cleaner::clean_doc(&mut doc, &cleaning_opts, &write_opts)?; // Write output let mut buffer: Vec = Vec::new(); cleaner::write_buffer(&doc, &write_opts, &mut buffer); // Check if cleaned file is smaller if buffer.len() > input_size { return Err("Cleaned file is larger than original".into()); } cleaner::save_file(&buffer, output_path)?; // Calculate compression ratio let ratio = 100.0 - (buffer.len() as f64) / (input_size as f64) * 100.0; Ok(ratio) } fn main() { match clean_svg("input.svg", "output.svg") { Ok(ratio) => println!("Image is {:.2}% smaller", ratio), Err(e) => eprintln!("Error: {}", e), } } ``` ```rust use svgcleaner::cleaner; use svgcleaner::{CleaningOptions, StyleJoinMode, ParseOptions, WriteOptions}; use svgdom::{Indent, ListSeparator}; use std::str; fn clean_svg(input_path: &str, output_path: &str) -> Result> { // Load input let raw_data = cleaner::load_file(input_path)?; let input_size = raw_data.len(); // Configure parsing (skip elements we want to remove) let parse_opts = ParseOptions { parse_comments: false, parse_declarations: false, parse_unknown_elements: false, parse_unknown_attributes: false, parse_px_unit: false, skip_unresolved_classes: true, skip_invalid_attributes: false, skip_invalid_css: false, skip_paint_fallback: false, }; // Parse document let mut doc = cleaner::parse_data(&raw_data, &parse_opts)?; // Configure cleaning with all optimizations let cleaning_opts = CleaningOptions { remove_unused_defs: true, convert_shapes: true, remove_title: true, remove_desc: true, remove_metadata: true, remove_dupl_linear_gradients: true, remove_dupl_radial_gradients: true, remove_dupl_fe_gaussian_blur: true, ungroup_groups: true, ungroup_defs: true, group_by_style: true, merge_gradients: true, regroup_gradient_stops: true, remove_invalid_stops: true, remove_invisible_elements: true, resolve_use: true, remove_version: true, remove_unreferenced_ids: true, trim_ids: true, remove_text_attributes: true, remove_unused_coordinates: true, remove_default_attributes: true, remove_xmlns_xlink_attribute: true, remove_needless_attributes: true, remove_gradient_attributes: false, join_style_attributes: StyleJoinMode::Some, apply_transform_to_gradients: true, apply_transform_to_shapes: true, paths_to_relative: true, remove_unused_segments: true, convert_segments: true, apply_transform_to_paths: false, append_newline: false, coordinates_precision: 6, properties_precision: 6, paths_coordinates_precision: 8, transforms_precision: 8, }; // Configure output let write_opts = WriteOptions { indent: Indent::None, attributes_indent: Indent::None, use_single_quote: false, trim_hex_colors: true, write_hidden_attributes: false, remove_leading_zero: true, use_compact_path_notation: true, join_arc_to_flags: false, remove_duplicated_path_commands: true, use_implicit_lineto_commands: true, simplify_transform_matrices: true, list_separator: ListSeparator::Space, ..Default::default() }; // Clean document cleaner::clean_doc(&mut doc, &cleaning_opts, &write_opts)?; // Write output let mut buffer: Vec = Vec::new(); cleaner::write_buffer(&doc, &write_opts, &mut buffer); // Check if cleaned file is smaller if buffer.len() > input_size { return Err("Cleaned file is larger than original".into()); } cleaner::save_file(&buffer, output_path)?; // Calculate compression ratio let ratio = 100.0 - (buffer.len() as f64) / (input_size as f64) * 100.0; Ok(ratio) } fn main() { match clean_svg("input.svg", "output.svg") { Ok(ratio) => println!("Image is {:.2}% smaller", ratio), Err(e) => eprintln!("Error: {}", e), } } ``` -------------------------------- ### Optimize SVG Path Segments Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-dupl-cmd-in-paths.adoc Demonstrates the reduction of redundant 'L' commands in an SVG path definition. ```xml ``` ```xml ``` -------------------------------- ### Build Svgcleaner with Cargo Source: https://github.com/razrfalcon/svgcleaner/blob/master/README.rst Command to build the Svgcleaner project in release mode using Cargo, the Rust package manager. ```bash cargo build --release ``` -------------------------------- ### Remove XML Comments from SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-comments.adoc This example demonstrates removing XML comments from an SVG document. All comments, including those within the style attribute, will be removed. ```xml ``` ```xml ``` -------------------------------- ### Perform multipass cleaning via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Run multiple optimization passes to achieve maximum file size reduction. ```bash # Enable multipass for maximum compression svgcleaner --multipass input.svg output.svg # Allow output file to be larger than input (useful with multipass) svgcleaner --multipass --allow-bigger-file input.svg output.svg ``` -------------------------------- ### Convert #RRGGBB to #RGB notation Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/trim-colors.adoc Demonstrates the transformation of a full hex color code to its shorthand equivalent. ```xml ``` ```xml ``` -------------------------------- ### Compact SVG Path Notation Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/trim-paths.adoc Comparison of standard SVG path notation versus the optimized compact version. ```xml ``` ```xml ``` -------------------------------- ### Remove SVG Title Element Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-title.adoc This example shows an SVG with a title element and the resulting SVG after the title element is removed. The title element is not rendered and its removal is optional. ```svg svgcleaner ``` ```svg ``` -------------------------------- ### SVG with Grouped Elements by Style Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.rst Demonstrates how SVG Cleaner can group elements with identical styles into a single group element for optimization. ```XML ``` ```XML ``` -------------------------------- ### Remove Unused Coordinate Attributes in SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-unused-coordinates.adoc This example demonstrates how unused coordinate attributes like 'ry' can be removed as they can be calculated from neighbor attributes. Use this to simplify SVG files. ```svg ``` ```svg ``` -------------------------------- ### SVG After Removing Unresolved Classes Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/remove-unresolved-classes.adoc This is an example of an SVG where unresolved classes have been removed from the 'class' attribute. If the 'removeUnresolvedClass' option was applied to the previous SVG, the 'class' attribute would be simplified. ```xml ``` -------------------------------- ### SVG Rendering Tool Reference Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/testing_notes.rst Reference to the svgrender tool used for CLI SVG rendering. ```text tools/svgrender ``` -------------------------------- ### Apply Gradient Transform in SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/apply-transform-to-gradients.adoc Use the `gradientTransform` attribute on a `` element to apply transformations such as translate, rotate, or proportional scale. This example shows a gradient with a translation applied. ```svg ``` -------------------------------- ### Process SVG streams via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Integrate SVGCleaner into pipelines using stdin and stdout. ```bash # Read from stdin and write to stdout cat input.svg | svgcleaner -c - > output.svg # Pipe from another tool curl -s https://example.com/image.svg | svgcleaner -c - > cleaned.svg # Chain with other processing svgcleaner -c input.svg | gzip > output.svg.gz ``` -------------------------------- ### SVG with Merged Linear Gradients Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.rst Illustrates the optimization of SVG gradients by merging a gradient definition that is referenced by another. ```XML ``` ```XML ``` -------------------------------- ### Convert SVGZ to SVG Source: https://github.com/razrfalcon/svgcleaner/blob/master/tools/files-testing/README.md This script converts all .svgz files in the current directory and its subdirectories to .svg format using 7zip, then deletes the original .svgz files. This is particularly useful for icon themes like Oxygen. ```bash find . -name "*.svgz" | while read filename; do 7za e -so "$filename" > "${filename%.svgz}.svg"; done; find . -name "*.svgz" -type f -delete ``` -------------------------------- ### Image Comparison Command Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/testing_notes.rst Command used to compare original and cleaned images using ImageMagick to detect rendering errors. ```bash compare -metric AE -fuzz 10% orig.png cleaned.png diff.png ``` -------------------------------- ### Optimize definitions (CLI) Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Command-line options to optimize SVG definitions by removing unused or duplicate elements such as linear gradients, radial gradients, and feGaussianBlur filters. These options are enabled by default. ```bash # Optimize definitions --remove-unused-defs=yes # Remove unused referenced elements [default: yes] --remove-dupl-lineargradient=yes # Remove duplicate linearGradient [default: yes] --remove-dupl-radialgradient=yes # Remove duplicate radialGradient [default: yes] --remove-dupl-fegaussianblur=yes # Remove duplicate feGaussianBlur [default: yes] ``` -------------------------------- ### SVG Path After Removing Duplicate Commands Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.adoc Shows the same SVG path after duplicate 'L' commands have been removed by the optimizer. ```xml ``` -------------------------------- ### Handle errors during batch processing via CLI Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Configure behavior for error scenarios during batch operations. ```bash # Copy original file to destination on error (useful for batch processing) svgcleaner --copy-on-error input.svg output.svg # Suppress output except warnings and errors svgcleaner --quiet input.svg output.svg ``` -------------------------------- ### Use implicit LineTo commands Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/svgcleaner.rst Treats subsequent coordinate pairs after a moveto as implicit lineto commands per SVG specification. ```XML ``` ```XML ``` -------------------------------- ### Apply Transformations to Paths Source: https://github.com/razrfalcon/svgcleaner/blob/master/docs/src/apply-transform-to-paths.adoc Transformations like translate, rotate, and proportional scale can be applied to paths. This is disabled by default as it usually increases file size but can be useful in specific cases. ```xml ``` ```xml ``` -------------------------------- ### Configure SVG Cleaning Options Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Apply a comprehensive set of cleaning operations to a parsed SVG document. Configure options for element removal, attribute cleaning, and path optimizations. ```rust use svgcleaner::cleaner; use svgcleaner::{CleaningOptions, StyleJoinMode, ParseOptions, WriteOptions}; fn main() { let svg_content = cleaner::load_file("input.svg").unwrap(); let parse_opts = ParseOptions::default(); let mut doc = cleaner::parse_data(&svg_content, &parse_opts).unwrap(); // Configure cleaning options let cleaning_opts = CleaningOptions { // Element removal remove_unused_defs: true, convert_shapes: true, remove_title: true, remove_desc: true, remove_metadata: true, remove_dupl_linear_gradients: true, remove_dupl_radial_gradients: true, remove_dupl_fe_gaussian_blur: true, ungroup_groups: true, ungroup_defs: true, group_by_style: true, merge_gradients: true, regroup_gradient_stops: true, remove_invalid_stops: true, remove_invisible_elements: true, resolve_use: true, // Attribute cleaning remove_version: true, remove_unreferenced_ids: true, trim_ids: true, remove_text_attributes: true, remove_unused_coordinates: true, remove_default_attributes: true, remove_xmlns_xlink_attribute: true, remove_needless_attributes: true, remove_gradient_attributes: false, // Disabled by default join_style_attributes: StyleJoinMode::Some, apply_transform_to_gradients: true, apply_transform_to_shapes: true, // Path optimizations paths_to_relative: true, remove_unused_segments: true, convert_segments: true, apply_transform_to_paths: false, // Disabled - may increase size append_newline: false, // Precision settings (1-12) coordinates_precision: 6, properties_precision: 6, paths_coordinates_precision: 8, transforms_precision: 8, }; let write_opts = WriteOptions::default(); // Clean the document cleaner::clean_doc(&mut doc, &cleaning_opts, &write_opts) .expect("Failed to clean SVG"); println!("Document cleaned successfully"); } ``` -------------------------------- ### Remove XML structural elements (CLI) Source: https://context7.com/razrfalcon/svgcleaner/llms.txt Command-line options to control the removal of XML comments and declarations. Defaults are set to 'yes' for removal. ```bash # Remove XML structural elements --remove-comments=yes # Remove XML comments [default: yes] --remove-declarations=yes # Remove XML declarations [default: yes] --remove-nonsvg-elements=yes # Remove non-SVG elements [default: yes] ```