#SVG Class Reference

The Victor::SVG (and Victor::SVGBase) class provides a foundation for creating SVG graphics in Ruby. It supports custom templates, CSS styling, and flexible SVG element generation.

#Initialization

##initialize

Creates a new instance of SVG.

Parameters
ParameterTypeDescription
attributesHash, optionalInitial attributes for the SVG, such as :width, :height, and :viewBox.
blockProc, optionalA block that defines the SVG content.
Example
svg = Victor::SVGBase.new viewBox: '0 0 200 200' svg.circle cx: '100', cy: '100', r: '50' # Initialize with a block svg = Victor::SVGBase.new viewBox: '0 0 200 200' do circle cx: '100', cy: '100', r: '50' end

#Building

##tag / #element

Adds an SVG element to the content.

Parameters
ParameterTypeDescription
nameSymbol or StringThe tag name of the SVG element.
valueString, optionalThe content inside the element.
attributesHash, optionalAttributes for the SVG element.
Notes
  • If value is a Hash, it's treated as attributes.
  • If name ends with !, the content is not XML-escaped.
  • This method is the target for method_missing, enabling dynamic element names.
Example
svg.element :rect, x: '10', y: '10', width: '100', height: '100' svg.tag :text, 'Hello, World!', x: '50', y: '50'

##build

Evaluates a block in the context of the SVG instance, allowing dynamic SVG content creation.

Parameters
ParameterTypeDescription
blockProc, optionalA block that defines the SVG content.
Example
svg.build do circle cx: '100', cy: '100', r: '50' text 'Centered Text', x: '100', y: '100' end

##append / #embed / #<<

Adds content from another source to the SVG. This can be used to compose SVG using other SVG objects.

Parameters
ParameterTypeDescription
additional_contentObjectAny object that responds to to_s. Usually this is another Victor::SVG object.
Example
host = SVG.new guest = SVG.new host << guest host.append guest host.embed guest

##css

Manages CSS definitions for the SVG.

Parameters
ParameterTypeDescription
defsHash, optionalCSS definitions to set.
Return Value
TypeDescription
HashThe current CSS definitions.
Example
svg.css = { '.hint': { color: :red, font_size: '12px' } } svg['.hint'] = { color: :red, font_size: '12px' }

#Rendering / Saving

##save

Saves the SVG content to a file.

Parameters
ParameterTypeDescription
filenameStringThe name of the file to save.
templateSymbol or String, optionalThe template to use.
glueString, optionalThe glue string to use between SVG elements.
Example
svg.save 'outfile' svg.save 'outfile', template: :minimal svg.save 'outfile', template: 'path/to/template' svg.save 'outfile.svg', template: 'path/to/template', glue: ''

##render

Renders the full SVG string, including its CSS and SVG header.

Parameters
ParameterTypeDescription
templateSymbol or String, optionalThe template to use.
glueString, optionalThe glue string to use between SVG elements.
Example
puts svg.render

##to_s

Converts the SVG content to a string. This method is used internally to join the SVG content with the specified glue.

Example
puts svg

#Internal / Special Use

##setup

Sets up the initial attributes for the SVG.

This method is used internally, and by the DSL and typically not required for basic usage.

Parameters
ParameterTypeDescription
attributesHashInitial attributes for the SVG, such as :width, :height, and :viewBox.

##content

Manages the array of SVG lines. This is used internally to assemble the SVG content.

##svg_attributes

Holds the attributes for the SVG element, such as width, height, and other SVG attributes.

Example
svg.svg_attributes.width = '200' svg.svg_attributes.height = '200'

##template

Specifies the SVG template to use. This is rarely needed as the default template is often sufficient.

Example
svg.template = :minimal svg.template = 'path/to/template'

##glue

Specifies the string to use between SVG elements. This is rarely needed as the default glue is often sufficient.