SvgSpriteBuilder Class

Handles building or extracting of an SVG sprite, i.e. a .svg file with child <symbol> tags.

Definition

Namespace: DotMake.SvgSprite
Assembly: DotMake.SvgSprite (in DotMake.SvgSprite.dll) Version: 1.0.0
C#
public class SvgSpriteBuilder
Inheritance
Object    SvgSpriteBuilder

Example

C#
//Build an SVG sprite file from input SVG files

var svgDocument = new SvgDocument();
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);

foreach (var file in Directory.EnumerateFiles(@"inputs\", "*.svg"))
{
    var svgDocumentToAdd = new SvgDocument(file);
    var symbolId = Path.GetFileNameWithoutExtension(file);

    svgSpriteBuilder.AddSymbol(svgDocumentToAdd, symbolId);
}

svgDocument.Save(@"sprite.svg");
C#
//Build an SVG sprite file from input SVG files with custom options

var svgDocument = new SvgDocument();
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
    //These are default values for options
    AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
    ElementsToPreserve = new[] { "*" },
    IdForMissing = "symbol",
    IdReplacementChar = '-',
    IdLowerCased = true
};

foreach (var file in Directory.EnumerateFiles(@"inputs\", "*.svg"))
{
    var svgDocumentToAdd = new SvgDocument(file);
    var symbolId = Path.GetFileNameWithoutExtension(file);

    svgSpriteBuilder.AddSymbol(svgDocumentToAdd, symbolId, svgSymbolOptions);
}

svgDocument.Save(@"sprite.svg");
C#
//Extract symbols from an SVG sprite file to individual SVG files

var svgDocument = new SvgDocument(@"sprite.svg");
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var outputDirectory = @"outputs\";

foreach (var symbolId in svgSpriteBuilder.GetSymbolIds())
{
    var svgDocumentToExtract = svgSpriteBuilder.ExtractSymbol(symbolId);
    var svgFile = Path.Combine(outputDirectory, Path.ChangeExtension(symbolId, ".svg"));

    Directory.CreateDirectory(outputDirectory);
    svgDocumentToExtract.Save(svgFile);
}
C#
//Extract symbols from an SVG sprite file to individual SVG files with custom options

var svgDocument = new SvgDocument(@"sprite.svg");
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
    //These are default values for options
    AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
    ElementsToPreserve = new[] { "*" },
    IdForMissing = "symbol",
    IdReplacementChar = '-',
    IdLowerCased = true
};
var outputDirectory = @"outputs\";

foreach (var symbolId in svgSpriteBuilder.GetSymbolIds())
{
    var svgDocumentToExtract = svgSpriteBuilder.ExtractSymbol(symbolId, svgSymbolOptions);
    var svgFile = Path.Combine(outputDirectory, Path.ChangeExtension(symbolId, ".svg"));

    Directory.CreateDirectory(outputDirectory);
    svgDocumentToExtract.Save(svgFile);
}
C#
//Create an HTML page for previewing the symbols inside the SVG sprite

var svgDocument = new SvgDocument(@"sprite.svg");
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);

var html = svgSpriteBuilder.CreatePreviewPage();
File.WriteAllText(@"preview.html", html);

Constructors

SvgSpriteBuilder Initializes a new instance of the SvgSpriteBuilder class.
SvgSpriteBuilder(SvgDocument) Initializes a new instance of the SvgSpriteBuilder class from an SVG document.

Methods

AddSymbol Adds an SVG document as a symbol to current SVG document.
CreatePreviewPage Creates an HTML page for previewing the symbols inside the SVG sprite.
ExtractSymbol(String, SvgSymbolOptions) Extracts a symbol by id from current SVG document, to a new SVG document.
ExtractSymbol(XElement, SvgSymbolOptions) Extracts a symbol from current SVG document, to a new SVG document.
GetSymbol Gets a symbol by id from current SVG document.
GetSymbolIds Gets all symbol ids from current SVG document.
GetSymbols Gets all symbols from current SVG document.
RemoveSymbol Removes a symbol by id from current SVG document.

See Also