Rule Builder – Configure SVG Style – C#

SVG Style

The SVG <style> element is used to define internal styles within an SVG document. It allows developers to apply CSS rules directly to SVG elements, providing a convenient way to control the appearance of SVG graphics.

Aspose.SVG Builder API offers the SVGStyleElementBuilder class, which is an element builder for constructing an SVG <style> element. This class facilitates the creation and configuration of an SVG <style> element with CSS rules.

This article is about the RuleBuilder class, which is a builder class for constructing CSS style rules. This class is used to dynamically build a string of CSS styles by setting various attributes and their values.

Rule Builder - RuleBuilder Class

The Rule Builder enables developers to apply CSS rules for SVG documents programmatically within C# code, offering flexibility and control over the visual appearance of SVG elements. This example shows how to create an SVG <style> element, add a CSS rule @font-face to it, and use the AddRule() method to define font styles for text elements within an SVG document:

 1using Aspose.Svg.Builder;
 2using System.Drawing;
 3using System.IO;
 4...
 5
 6    using (var document = new SVGDocument())
 7    {
 8        var svg = new SVGSVGElementBuilder()
 9            
10            .AddDefs(def => def
11                .AddStyle(st => st
12                    .Type("text/css")
13                    .AddRule("@font-face", r => r
14                        .FontFamily("VeinlineRegular")
15                        .Attribute("src", "url(https://assets.ithillel.ua/wiki/VeinlineRegular.ttf)")
16                    )
17                    .AddRule("text", t => t
18                        .FontFamily("VeinlineRegular")
19                        .FontSize(30, LengthType.Pt)
20                    )
21                )
22            )
23                .AddText(t => t
24                .X(20).Y(60).Fill(Color.DarkRed).FontWeight(FontWeight.Bold)
25                .AddContent("Configure SVG Style")
26                )
27                .AddText(t => t
28                .X(20).Y(150)
29                .AddContent("Set your SVG style with Rule Builder!")
30                )
31            .Build(document.FirstChild as SVGSVGElement);
32        document.Save(Path.Combine(OutputDir, "font-face.svg"));
33    }

Text “font-face.svg file visualization”

@font-face Rule

The @font-face CSS rule allows you to specify fonts for displaying text on web pages that can be downloaded either from a remote server or from the user’s computer. The rule defines a custom font family name and specifies the source URL of the font file.

In the example above, the @font-face rule defines a custom font family named "VeinlineRegular". This rule ensures that the font is loaded and available for use within the SVG document.

AddRule() Method

The AddRule() method is part of the Rule Builder, which allows you to create CSS rules in SVG documents. It takes two parameters: the name of the CSS selector and a lambda expression defining the properties of the style using RuleBuilder.

In the C# example above, two rules are added:

Builders within Builders

SVG Builder API introduces syntax sugar to further refine the process of SVG creation and manipulation. This includes nested builders for various SVG elements, providing a more intuitive and efficient way to define complex SVG structures. A “builder within a builder” pattern involves the use of multiple builder classes, where one builder is nested within another to facilitate the construction of complex objects or structures.

The following code snippet demonstrates how to use RuleBuilder, which is an example of a builder within a builder, to create and style SVG graphics programmatically:

 1using Aspose.Svg.Builder;
 2using System.Drawing;
 3using System.IO;
 4...
 5    // Initialize an SVG document
 6    using (var document = new SVGDocument())
 7    {
 8        // Create an <svg> element
 9        var svg = new SVGSVGElementBuilder()
10
11            .AddStyle(st => st
12                .AddRule("circle", r => r
13                    .Fill(paint: Paint.None)
14                    .Stroke(color: Color.DarkRed)
15                    .StrokeWidth(60)
16                )
17                .AddRule("text", t => t
18                    .Fill(color: Color.Red)
19                    .Stroke(color: Color.Teal)
20                    .StrokeWidth(1)
21                    .FontFamily("Arial")
22                    .FontSize(30, LengthType.Pt)
23                )
24            )
25            .AddG(g => g
26                .AddCircle(circle => circle.Cx(100).Cy(130).R(60).StrokeDashArray(2, 14))
27                .AddText("Rule Builder", x: 230, y: 140)
28            )
29            .Build(document.FirstChild as SVGSVGElement);
30
31        // Save the SVG document
32        document.Save(Path.Combine(OutputDir, "rule-builder.svg"));
33    }

Conclusion

Aspose.SVG for .NET provides a Rule Builder feature that allows developers to programmatically define CSS rules for SVG elements using the SVG Builder API.

See also

  • The Element Builders article delves into the Element Builders used in SVG Builder API. You will learn about the SVGElementBuilder class, its specialized builders, and how they simplify SVG programming.
  • Please see the Path Builder article to learn more about the PathBuilder class, designed to simplify the creation and manipulation of SVG paths. The article showcases how the Path Builder offers an intuitive syntax for defining SVG paths programmatically, enabling developers to streamline the process of creating intricate shapes and curves.
  • The Paint Builder article is about PaintBuilder, a builder class for creating paint values for SVG elements. This class is used to specify the value of the stroke or fill attributes for various SVG shapes and elements when filling them with paint, pattern, or gradient.
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.