NAME
Image::CairoSVG - render SVG into a Cairo surface
SYNOPSIS
This example converts an SVG into a PNG:
use FindBin '$Bin';
use Cairo;
use Image::CairoSVG;
my $surface = Cairo::ImageSurface->create ('argb32', 450, 200);
my $cairosvg = Image::CairoSVG->new (
surface => $surface,
);
$cairosvg->render ("$Bin/locust.svg");
$surface->write_to_png ("$Bin/locust.png");
This renders the following image:
VERSION
This documents version 0.05 of Image::CairoSVG corresponding to git commit 7b8e40d92d1932621b7c836f3bd6ec797c5d67f7 released on Thu Sep 15 10:41:26 2016 +0900.
DESCRIPTION
This module renders some kinds of SVG ("Scalable Vector Graphics") instructions into a Cairo surface.
METHODS
new
my $cairosvg = Image::CairoSVG->new ();
If a surface is not provided, this creates a Cairo::ImageSurface of dimensions 100 by 100 of rgba format.
The user can supply a surface:
my $cairosvg = Image::CairoSVG->new (surface => $surface);
For example,
my $cairosvg = Image::CairoSVG->new (
surface => Cairo::ImageSurface->create ('argb32', 100, 100)
);
render
$cairosvg->render ('some.svg');
Draw an SVG file into a Cairo surface.
DRAWING METHODS
All of these methods take the attributes of the specific element after which they're named. So, for example, if you have an SVG line
element, you can parse its attributes with an XML parser, then send the hash of key/value pairs in the attributes to "line".
line
$cairosvg->line (%attr);
Render an SVG line onto the surface specified by $cairosvg
. Given SVG input of the form <line >
, this renders it onto the Cairo surface.
path
$cairosvg->path (%attr);
Given an SVG path element, send its attribute key / value pairs as %attr
to render into the Cairo surface of $cairosvg
. This uses Image::SVG::Path to render the "d" attibute of the path. It also converts quadratic bezier curves into cubic bezier curves.
rect
$cairosvg->rect (%attr);
ellipse
$cairosvg->ellipse (%attr);
circle
$cairosvg->circle (%attr);
polygon
$cairosvg->polygon (%attr);
DEPENDENCIES
- Cairo
-
Cairo is used for rendering the image.
- Carp
-
Carp is used for reporting errors.
- Image::SVG::Path
-
Image::SVG::Path is used for parsing the "path" information of the SVG.
- XML::Parser
-
XML::Parser is used for parsing SVG files.
SEE ALSO
- CairoSVG
-
CairoSVG is a Python SVG renderer in Cairo. Parts of the code here are based on it.
Other CPAN modules related to SVG
- Image::SVG::Path
-
This is for reading the "d" attribute of SVG paths.
- MarpaX::Languages::SVG::Parser
-
This extends the Marpa::R2 parser to parse SVG.
- SVG
-
This is for generating SVG documents.
- Image::LibRSVG
-
Perl extension for a Gnome library called librsvg which converts SVG to PNG or JPEG, etc. I have not tested this library.
- SVG::Rasterize
-
Rasterize SVG content to pixel graphics.
More information
- Perl Maven article
-
SVG - Scalable Vector Graphics with Perl - article at Perl Maven
BUGS
This module is a "least effort" attempt to get the parts of SVG which the author needs rendered, rendered. It doesn't even pretend to be a full SVG renderer. So, if you find the module doesn't do some part of SVG which you want done, please add that to the module, and contribute your addition to this module via the github repository.
AUTHOR
Ben Bullock, <bkb@cpan.org>
Request
If you'd like to see this module continued, let me know that you're using it. For example, send an email, write a bug report, star the project's github repository, add a patch, add a ++
on Metacpan.org, or write a rating at CPAN ratings. It really does make a difference. Thanks.
COPYRIGHT & LICENCE
This package and associated files are copyright (C) 2014-2016 Ben Bullock.
You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.
cairosvg licence
Some parts of the module (specifically the SVG arc drawing code) are translations from the above-mentioned Python program "cairosvg", which is under the "GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007". I'm not really sure how or if this affects the code, but just in case it causes legal issues for someone downstream, I'm mentioning it here.