SVG - Perl extension for generating Scalable Vector Graphics (SVG) documents.
Contents
Articles
SVG using Perl <http://szabgab.com/svg-using-perl.html>
SVG - Scalable Vector Graphics with Perl <http://perlmaven.com/scalable-vector-graphics-with-perl>
Combining SVG and PSGI <http://perlmaven.com/combining-svg-and-psgi>
Copyright & License
Copyright 2001- Ronan Oger
The modules in the SVG distribution are distributed under the same license as Perl itself. It is provided
free of warranty and may be re-used freely.
Credits
I would like to thank the following people for contributing to this module with patches, testing,
suggestions, and other nice tidbits:
Peter Wainwright, Excellent ideas, beta-testing, writing SVG::Parser and much of SVG::DOM. Fredo,
http://www.penguin.at0.net/~fredo/ - provided example code and initial feedback for early SVG.pm versions
and the idea of a simplified svg generator. Adam Schneider Brial Pilpré Ian Hickson Steve Lihn Allen Day
Martin Owens - SVG::DOM improvements in version 3.34
Description
SVG is a 100% Perl module which generates a nested data structure containing the DOM representation of an
SVG (Scalable Vector Graphics) image. Using SVG, you can generate SVG objects, embed other SVG instances
into it, access the DOM object, create and access javascript, and generate SMIL animation content.
GeneralStepstogeneratinganSVGdocument
Generating SVG is a simple three step process:
1 Construct a new SVG object with "new".
2 Call element constructors such as "circle" and "path" to create SVG elements.
3 Render the SVG object into XML using the "xmlify" method.
The "xmlify" method takes a number of optional arguments that control how SVG renders the object into
XML, and in particular determine whether a standalone SVG document or an inline SVG document fragment is
generated:
-standalone
A complete SVG document with its own associated DTD. A namespace for the SVG elements may be optionally
specified.
-inline
An inline SVG document fragment with no DTD that is embedded within other XML content. As with standalone
documents, an alternate namespace may be specified.
No XML content is generated until the third step is reached. Up until this point, all constructed element
definitions reside in a DOM-like data structure from which they can be accessed and modified.
EXPORTS
None. However, SVG permits both options and additional element methods to be specified in the import
list. These options and elements are then available for all SVG instances that are created with the "new"
constructor. For example, to change the indent string to two spaces per level:
use SVG (-indent => " ");
With the exception of -auto, all options may also be specified to the "new" constructor. The currently
supported options and their default value are:
# processing options
-auto => 0, # permit arbitrary autoloading of all unrecognised elements
-printerror => 1, # print error messages to STDERR
-raiseerror => 1, # die on errors (implies -printerror)
# rendering options
-indent => "\t", # what to indent with
-elsep => "\n", # element line (vertical) separator
# (note that not all agents ignor trailing blanks)
-nocredits => 0, # enable/disable credit note comment
-namespace => '', # The root element's (and it's children's) namespace prefix
# XML and Doctype declarations
-inline => 0, # inline or stand alone
-docroot => 'svg', # The document's root element
-version => '1.0',
-extension => '',
-encoding => 'UTF-8',
-xml_svg => 'http://www.w3.org/2000/svg', # the svg xmlns attribute
-xml_xlink => 'http://www.w3.org/1999/xlink', # the svg tag xmlns:xlink attribute
-standalone => 'yes',
-pubid => "-//W3C//DTD SVG 1.0//EN", # formerly -identifier
-sysid => 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd', # the system id
SVG also allows additional element generation methods to be specified in the import list. For example to
generate 'star' and 'planet' element methods:
use SVG qw(star planet);
or:
use SVG ("star","planet");
This will add 'star' to the list of elements supported by SVG.pm (but not of course other SVG
parsers...). Alternatively the '-auto' option will allow any unknown method call to generate an element
of the same name:
use SVG (-auto => 1, "star", "planet");
Any elements specified explicitly (as 'star' and 'planet' are here) are predeclared; other elements are
defined as and when they are seen by Perl. Note that enabling '-auto' effectively disables compile-time
syntax checking for valid method names.
use SVG (
-auto => 0,
-indent => " ",
-raiseerror => 0,
-printerror => 1,
"star", "planet", "moon"
);
DefaultSVGtag
The Default SVG tag will generate the following XML:
$svg = SVG->new;
print $svg->xmlify;
Resulting XML snippet:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg height="100%" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!--
Generated using the Perl SVG Module V2.50
by Ronan Oger
-->
Examples
examples/circle.pl generates the following image:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg height="200" width="200" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title >I am a title</title>
<g id="group_y" style="fill: green; stroke: red">
<circle cx="100" cy="100" id="circle_in_group_y" r="50" />
<!-- This is a comment -->
</g>
</svg>
That you can either embed directly into HTML or can include it using:
<object data="file.svg" type="image/svg+xml"></object>
(The image was converted to png using Image::LibRSVG. See the svg2png.pl script in the examples
directory.)
See also the examples directory in this distribution which contains several fully documented examples.
Generic Element Methods
The following elements are generically supported by SVG:
*altGlyph*altGlyphDef*altGlyphItem*clipPath*color-profile*cursor*definition-src*font-face-format*font-face-name*font-face-src*font-face-url*foreignObject*glyph*glyphRef*hkern*marker*mask*metadata*missing-glyph*mpath*switch*symbol*tref*view*vkern
See e.g. "pattern" for an example of the use of these methods.
Maintainer
Gabor Szabo <http://szabgab.com/>
Methods
SVG provides both explicit and generic element constructor methods. Explicit generators are generally
(with a few exceptions) named for the element they generate. If a tag method is required for a tag
containing hyphens, the method name replaces the hyphen with an underscore. ie: to generate tag
<column-heading id="new"> you would use method $svg->column_heading(id=>'new').
All element constructors take a hash of element attributes and options; element attributes such as 'id'
or 'border' are passed by name, while options for the method (such as the type of an element that
supports multiple alternate forms) are passed preceded by a hyphen, e.g '-type'. Both types may be freely
intermixed; see the "fe" method and code examples throughout the documentation for more examples.
new(constructor)
$svg = SVG->new(%attributes)
Creates a new SVG object. Attributes of the document SVG element be passed as an optional list of key
value pairs. Additionally, SVG options (prefixed with a hyphen) may be set on a per object basis:
my $svg1 = SVG->new;
my $svg2 = SVG->new(id => 'document_element');
my $svg3 = SVG->new(
-printerror => 1,
-raiseerror => 0,
-indent => ' ',
-elsep => "\n", # element line (vertical) separator
-docroot => 'svg', # default document root element (SVG specification assumes svg). Defaults to 'svg' if undefined
-xml_xlink => 'http://www.w3.org/1999/xlink', # required by Mozilla's embedded SVG engine
-sysid => 'abc', # optional system identifier
-pubid => "-//W3C//DTD SVG 1.0//EN", # public identifier default value is "-//W3C//DTD SVG 1.0//EN" if undefined
-namespace => 'mysvg',
-inline => 1
id => 'document_element',
width => 300,
height => 200,
);
Default SVG options may also be set in the import list. See "EXPORTS" above for more on the available
options.
Furthermore, the following options:
-version
-encoding
-standalone
-namespace
-inline
-pubid (formerly -identifier)
-sysid (standalone)
may also be set in xmlify, overriding any corresponding values set in the SVG->new declaration
xmlify(alias:to_xmlrenderserializeserialise)
$string = $svg->xmlify(%attributes);
Returns xml representation of svg document.
XMLDeclaration
Name Default Value
-version '1.0'
-encoding 'UTF-8'
-standalone 'yes'
-namespace 'svg' - namespace prefix for elements.
Can also be used in any element method to over-ride
the current namespace prefix. Make sure to have
declared the prefix before using it.
-inline '0' - If '1', then this is an inline document.
-pubid '-//W3C//DTD SVG 1.0//EN';
-sysid 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'
perlify()
return the perl code which generates the SVG document as it currently exists.
toperl()
Alias for method perlify()Methods Imported By Svg::Dom
The following SVG::DOM elements are accessible through SVG:
*getChildren*getFirstChild*getNextChild*getLastChild*getParent*getParentElement*getSiblings*getElementByID*getElementID*getElements*getElementName*getType*getAttributes*getAttribute*setAttributes*setAttribute*insertBefore*insertAfter*insertSiblingBefore*insertSiblingAfter*replaceChild*removeChild*cloneNodeName
SVG - Perl extension for generating Scalable Vector Graphics (SVG) documents.
See Also
SVG::DOM, SVG::XML, SVG::Element, SVG::Parser, SVG::Extension
For Commercial Perl/SVG development, refer to the following sites: SVG at the W3C
<http://www.w3c.org/Graphics/SVG/>.
perl v5.34.0 2022-05-21 SVG(3pm)
Synopsis
#!/usr/bin/perl
use strict;
use warnings;
use SVG;
# create an SVG object
my $svg= SVG->new( width => 200, height => 200);
# use explicit element constructor to generate a group element
my $y = $svg->group(
id => 'group_y',
style => {
stroke => 'red',
fill => 'green'
},
);
# add a circle to the group
$y->circle( cx => 100, cy => 100, r => 50, id => 'circle_in_group_y' );
# or, use the generic 'tag' method to generate a group element by name
my $z = $svg->tag('g',
id => 'group_z',
style => {
stroke => 'rgb(100,200,50)',
fill => 'rgb(10,100,150)'
}
);
# create and add a circle using the generic 'tag' method
$z->tag('circle', cx => 50, cy => 50, r => 100, id => 'circle_in_group_z');
# create an anchor on a rectangle within a group within the group z
my $k = $z->anchor(
id => 'anchor_k',
-href => 'http://test.hackmare.com/',
target => 'new_window_0'
)->rectangle(
x => 20, y => 50,
width => 20, height => 30,
rx => 10, ry => 5,
id => 'rect_k_in_anchor_k_in_group_z'
);
# now render the SVG object, implicitly use svg namespace
print $svg->xmlify;
# or render a child node of the SVG object without rendering the entire object
print $k->xmlify; #renders the anchor $k above containing a rectangle, but does not
#render any of the ancestor nodes of $k
# or, explicitly use svg namespace and generate a document with its own DTD
print $svg->xmlify(-namespace=>'svg');
# or, explicitly use svg namespace and generate an inline docunent
print $svg->xmlify(
-namespace => "svg",
-pubid => "-//W3C//DTD SVG 1.0//EN",
-inline => 1
);
See the other modules in this distribution: SVG::DOM, SVG::XML, SVG::Element, and SVG::Extension.
See SVG::Parser for reading SVG files as "SVG" objects.
ConvertingSVGtoPNGandotherrasterimageformats
The convert command of <http://www.imagemagick.org/> (also via Image::Magick ) can convert SVG files to
PNG and other formats.
Image::LibRSVG can convert SVG to other format.
