19 May 2022 17:03:48 UTC
Distribution: SVG
Module version: 2.87
Source (raw)
Browse (raw)
Changes
How to Contribute
Repository
Issues (11)
Testers (970 / 0 / 0)
Kwalitee
Bus factor: 1
49.97% Coverage
License: perl_5
Perl: v5.6.0
Activity
24 month
Tools
Download (53.99KB)
MetaCPAN Explorer
Permissions
Subscribe to distribution

    Dependencies
    Exporter
    Scalar::Util
    constant
    parent
    strict
    warnings
    and possibly others
    Reverse dependencies
    CPAN Testers List
    Dependency graph

Permalinks
This version
Latest version
++ed by:
DELON EMILPER CONTRA SHLOMIF KEEDI
7 PAUSE users
11 non-PAUSE users

Mohammad S Anwar  / 
SVG-2.87
/ SVG
Contents

    NAME
    SYNOPSIS
        Converting SVG to PNG and other raster image formats
    EXAMPLES
    DESCRIPTION
        General Steps to generating an SVG document
        -standalone
        -inline
        EXPORTS
        Default SVG tag
    METHODS
        new (constructor)
        xmlify (alias: to_xml render serialise serialize)
        tag (alias: element)
        anchor
            optional anchor attributes
        -href required
        -type optional
        -role optional
        -title optional
        -show optional
        -arcrole optional
        -actuate optional
        target optional
        circle
        ellipse
        rectangle (alias: rect)
        image
        use
        polygon
        polyline
        line
        text
        title
        desc
        comment
        pi (Processing Instruction)
        script
        path
        get_path
        animate
        group
        defs
        style
        mouseaction
        attrib
        cdata
        cdata_noxmlesc
        CDATA
        xmlescp and xmlescape
        filter
        fe
        * feBlend
        * feColorMatrix
        * feComponentTransfer
        * feComposite
        * feConvolveMatrix
        * feDiffuseLighting
        * feDisplacementMap
        * feDistantLight
        * feFlood
        * feFuncA
        * feFuncB
        * feFuncG
        * feFuncR
        * feGaussianBlur
        * feImage
        * feMerge
        * feMergeNode
        * feMorphology
        * feOffset
        * fePointLight
        * feSpecularLighting
        * feSpotLight
        * feTile
        * feTurbulence
        pattern
        set
        stop
        gradient
    GENERIC ELEMENT METHODS
        * 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
    METHODS IMPORTED BY SVG::DOM
        * getChildren
        * getFirstChild
        * getNextChild
        * getLastChild
        * getParent
        * getParentElement
        * getSiblings
        * getElementByID
        * getElementID
        * getElements
        * getElementName
        * getType
        * getAttributes
        * getAttribute
        * setAttributes
        * setAttribute
        * insertBefore
        * insertAfter
        * insertSiblingBefore
        * insertSiblingAfter
        * replaceChild
        * removeChild
        * cloneNode
    Methods
        new (constructor)
        xmlify (alias: to_xml render serialize serialise )
        perlify ()
        toperl ()
    AUTHOR
    MAINTAINER
    CREDITS
    COPYRIGHT & LICENSE
    ARTICLES
    SEE ALSO

NAME

SVG - Perl extension for generating Scalable Vector Graphics (SVG) documents.
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.
Converting SVG to PNG and other raster image formats

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.
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>

SVG example circle

(The image was converted to png using Image::LibRSVG. See the svg2png.pl script in the examples directory.)

SVG example circle

See also the examples directory in this distribution which contains several fully documented examples.
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.
General Steps to generating an SVG document

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"
    );

Default SVG tag

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
      -->

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.