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 $cairosvg = Image::CairoSVG->new ();
my $surface = $cairosvg->render ("$Bin/locust.svg");
$surface->write_to_png ("$Bin/locust.png");

This renders the following image:

(This example is included as synopsis.pl in the distribution. The input and output files are also included.)

VERSION

This documents Image::CairoSVG version 0.16 corresponding to git commit 6bab4288745016723b4513f54789fe5b1b454899 released on Mon May 3 13:29:36 2021 +0900.

DESCRIPTION

This module renders some kinds of SVG ("Scalable Vector Graphics") instructions into a Cairo surface.

METHODS

new

my $cairosvg = Image::CairoSVG->new (%arguments);

Arguments are as follows

surface

The user can supply a Cairo surface:

my $cairosvg = Image::CairoSVG->new (surface => $surface);

For example,

my $cairosvg = Image::CairoSVG->new (
    surface => Cairo::ImageSurface->create ('argb32', 100, 100)
);
context

The user can also supply a Cairo context:

my $cairosvg = Image::CairoSVG->new (context => $cr);

If a Cairo context and a surface are both supplied, the value of surface is ignored, a warning is printed, and the image is drawn using only the context value.

For simple drawing tasks, we recommend using the default surface generated by this module, which is the return value from "render". Only supply a surface if the module gets the dimensions of your image wrong. Only use a Cairo context if you want to include the image in some other image or rescale it. See "Scaling the output" for an example.

render

my $surface = $cairosvg->render ('some.svg');

Draw an SVG file or scalar into a Cairo surface. The return value is the surface drawn into. If the call value is a scalar containing what looks like XML, it is parsed from the scalar instead. This method guesses whether its argument is a file or a scalar by looking for a pair of angle brackets (<>) in its first argument. If it does not find angle brackets, it assumes the argument is a file name.

If the user did not supply a context or a surface to "new", a new Cairo::ImageSurface object is generated. If the user supplies a context argument with "new", the return value of render should be ignored. If the user supplied a surface with "new", the return value is just that surface.

If the user does not specify a surface, the generated surface returned by render is based on the attributes of the <svg> element, specifically either the width and height attributes, or the width and height specified in the viewBox attribute.

Calling with a scalar containing XML was added in version 0.08.

EXAMPLES

Scaling the output

This example shows how to scale the output image using Cairo commands.

use FindBin '$Bin';
use Cairo;
use Image::CairoSVG;
# Using defaults
my $dcairosvg = Image::CairoSVG->new ();
my $dsurface = $dcairosvg->render ("$Bin/urn.svg");
$dsurface->write_to_png ("$Bin/durn.png");
# Scale to 200 pixels
my $size = 200;
my $twsize = 36;
my $surface = Cairo::ImageSurface->create ('argb32', $size, $size);
my $context = Cairo::Context->create ($surface);
my $cairosvg = Image::CairoSVG->new (context => $context);
$context->scale ($size/$twsize, $size/$twsize);
$cairosvg->render ("$Bin/urn.svg");
$surface->write_to_png ("$Bin/urn.png");

The default size looks like this:

The scaled image looks like this:

(This example is included as scale.pl in the distribution. The original image is part of the Twitter Emoji collection, 1f3fa.svg, representing U+1F3FA AMPHORA, used under CC-BY 4.0)

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, since Cairo does not support quadratic bezier curves.

rect

$cairosvg->rect (%attr);

ellipse

$cairosvg->ellipse (%attr);

circle

$cairosvg->circle (%attr);

polygon

$cairosvg->polygon (%attr);

Draws a polygon based on the points attribute, closing the path.

polyline

$cairosvg->polyline (%attr);

The same as "polygon" except that it doesn't close the path.

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.

Math::Trig

Used for the value of pi and various functions related to drawing arcs.

XML::Parser

XML::Parser is used for parsing SVG files.

SEE ALSO

Demonstration website - Super Tiny Icons

The demonstration website https://benkasminbullock.github.io/supertinyiconscairosvg/ shows the output of Image::CairoSVG on the Super Tiny Icons collection and the Twitter Emoji Collection.

Other software

CairoSVG

CairoSVG is a Python SVG renderer in Cairo. Originally the elliptical arc drawing part of Image::CairoSVG was based on it, but this was replaced in version 0.14 of this module by code based on the W3 Consortium standards.

Image::LibRSVG

Perl extension for a Gnome library called librsvg which converts SVG to PNG or JPEG, etc. We have not tested this library.

Image::Magick

Does SVG to PNG conversion.

Image::SVG::Path

This is a specialised module for reading the "d" attribute of SVG paths.

Image::SVG::Transform

Read the "transform" attribute of an SVG element.

MarpaX::Languages::SVG::Parser

This extends the Marpa::R2 parser to parse SVG.

SVG

This is for generating SVG documents.

SVG::Parser

Parse the XML for SVG files. It can use either XML::Parser or XML::SAX but XML::Parser seems not to work. See https://rt.cpan.org/Ticket/Display.html?id=136404.

SVG::Rasterize

Rasterize SVG content to pixel graphics. We couldn't get this to produce output. See https://rt.cpan.org/Ticket/Display.html?id=136405.

More information

Perl Maven article

SVG - Scalable Vector Graphics with Perl - article at Perl Maven

Perl Cairo tutorial

This Perl Cairo tutorial may be useful if you need to go beyond defaults.

BUGS

Clip paths

Does not support clip paths (clipping the output image to a given path).

Copied paths

Does not support xlink:href (copied paths).

Elliptical arcs

Elliptical arcs are not handled correctly. There are no primitives in Cairo for these shapes so they will need to be implemented via Cairo transforms.

fill-rule

The fill-rule attribute is not supported.

Gradients

Gradients are not supported.

Groups

Groups are not supported, although inherited fill/stroke and other attributes are supported.

Inherited fill / stroke values

The handling of fill and stroke inherited from parent elements may be somewhat patchy.

stroke-dasharray

stroke-dasharray is not supported.

Transforms

Some transforms are not handled.

Units

Units have been added haphazardly as they have been found in actual SVG documents. Although pixel units seem easy enough to understand, we're not exactly sure what the correct treatment of units such as millimetres or percents should be, so there is some guesswork at the moment.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

This package and associated files are copyright (C) 2014-2021 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.