SvgSpriteBuilder Class
Handles building or extracting of an SVG sprite, i.e. a .svg file with child <symbol> tags.
Namespace: DotMake.SvgSpriteAssembly: DotMake.SvgSprite (in DotMake.SvgSprite.dll) Version: 1.0.0
public class SvgSpriteBuilder
- Inheritance
- Object SvgSpriteBuilder
//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");
//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");
//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);
}
//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);
}
//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);