Skip to content

okooo5km/SVGift

Repository files navigation

SVGift

Swift CI License: MIT

Swift native implementation of SVGO — the popular Node.js SVG optimizer.

SVGift provides the same optimization capabilities as SVGO, with 49 built-in plugins (34 enabled by default). It is available as both a CLI tool and a Swift library.

Installation

Homebrew

brew install okooo5km/tap/svgift

Or manually:

brew tap okooo5km/tap
brew install svgift

Build from source

git clone https://github.com/okooo5km/SVGift.git
cd svgo-swift
swift build -c release
# Binary at .build/release/svgift

SwiftPM (as a library dependency)

.package(url: "https://github.com/okooo5km/SVGift.git", from: "0.1.0")

Then add "SVGift" to your target's dependencies.

Usage

CLI

# Optimize a single file (output to stdout)
svgift input.svg

# Optimize and write to a file
svgift input.svg -o output.svg

# Read from stdin
cat input.svg | svgift -

# Recursively optimize a directory (in-place)
svgift -r icons/

# Recursively optimize to a different directory
svgift -r icons/ -o optimized/

# Enable multipass optimization
svgift input.svg --multipass -o output.svg

# Pretty-print output
svgift input.svg --pretty

# Use a config file
svgift input.svg --config svgo.config.json -o output.svg

# List available plugins
svgift --show-plugins

Library API

import SVGift

// Basic usage with default plugins
let input = "<svg>...</svg>"
let result = try optimize(input)
print(result.data)

// Custom options
var options = OptimizeOptions(
    plugins: presetDefaultPlugins,
    pluginRegistry: builtinPluginRegistry
)
options.multipass = true
options.js2svg.pretty = true
let result = try optimize(input, options: options)

Configuration

Create a JSON config file to customize plugin behavior:

{
  "multipass": true,
  "plugins": [
    "preset-default",
    {
      "name": "removeAttrs",
      "params": {
        "attrs": ["fill", "stroke"]
      }
    },
    {
      "name": "addAttributesToSVGElement",
      "params": {
        "attributes": {
          "xmlns": "http://www.w3.org/2000/svg"
        }
      }
    }
  ]
}

Disabling a default plugin

{
  "plugins": [
    "preset-default",
    {
      "name": "removeComments",
      "enabled": false
    }
  ]
}

Customizing plugin parameters

{
  "plugins": [
    {
      "name": "preset-default",
      "params": {
        "overrides": {
          "cleanupIds": {
            "minify": false
          }
        }
      }
    }
  ]
}

Plugins

49 built-in plugins are available. See docs/plugins.md for the complete list with descriptions and parameters.

Default plugins (34): removeDoctype, removeXMLProcInst, removeComments, removeDeprecatedAttrs, removeMetadata, removeEditorsNSData, cleanupAttrs, mergeStyles, inlineStyles, minifyStyles, cleanupIds, removeUselessDefs, cleanupNumericValues, convertColors, removeUnknownsAndDefaults, removeNonInheritableGroupAttrs, removeUselessStrokeAndFill, cleanupEnableBackground, removeHiddenElems, removeEmptyText, convertShapeToPath, convertEllipseToCircle, moveElemsAttrsToGroup, moveGroupAttrsToElems, collapseGroups, convertPathData, convertTransform, removeEmptyAttrs, removeEmptyContainers, mergePaths, removeUnusedNS, sortAttrs, sortDefsChildren, removeDesc

Compatibility

SVGift aims for full compatibility with SVGO's optimization output. The test suite validates against 363 SVGO test fixtures with 100% pass rate (L1 byte-exact and L2 normalized matching).

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Run tests (swift test)
  4. Commit your changes
  5. Push to the branch and open a Pull Request

License

MIT License. See LICENSE for details.

Credits

About

SVG Optimizer - a Swift native implementation of SVGO.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors