Cairo - Perl interface to the cairo 2d vector graphics library
use Cairo; my $surface = Cairo::ImageSurface->create ('argb32', 100, 100); my $cr = Cairo::Context->create ($surface); $cr->rectangle (10, 10, 40, 40); $cr->set_source_rgb (0, 0, 0); $cr->fill; $cr->rectangle (50, 50, 40, 40); $cr->set_source_rgb (1, 1, 1); $cr->fill; $cr->show_page; $surface->write_to_png ('output.png');
Cairo provides Perl bindings for the vector graphics library cairo. It supports multiple output targets, including PNG, PDF and SVG. Cairo produces identical output on all those targets.
This is a listing of the API Cairo provides. For more verbose information, refer to the cairo manual at http://cairographics.org/manual/.
Cairo::Context is the main object used when drawing with Cairo. To draw with Cairo, you create a Cairo::Context, set the target surface, and drawing options for the Cairo::Context, create shapes with methods like $cr->move_to and $cr->line_to, and then draw shapes with $cr->stroke or $cr->fill.
$cr->move_to
$cr->line_to
$cr->stroke
$cr->fill
Cairo::Context's can be pushed to a stack via $cr->save. They may then safely be changed, without loosing the current state. Use $cr->restore to restore to the saved state.
$cr->save
$cr->restore
$path = [ { type => "move-to", points => [[1, 2]] }, { type => "line-to", points => [[3, 4]] }, { type => "curve-to", points => [[5, 6], [7, 8], [9, 10]] }, ... { type => "close-path", points => [] }, ];
Cairo::Path is a data structure for holding a path. This data structure serves as the return value for $cr->copy_path and $cr->copy_path_flat as well the input value for $cr->append_path.
$cr->copy_path
$cr->copy_path_flat
$cr->append_path
Cairo::Path is represented as an array reference that contains path elements, represented by hash references with two keys: type and points. The value for type can be either of the following:
move-to
line-to
curve-to
close-path
The value for points is an array reference which contains zero or more points. Points are represented as array references that contain two doubles: x and y. The necessary number of points depends on the type of the path element:
line_to
The semantics and ordering of the coordinate values are consistent with $cr->move_to, $cr->line_to, $cr->curve_to, and $cr->close_path.
$cr->curve_to
$cr->close_path
Note that the paths returned by Cairo are implemented as tied array references which do not support adding, removing or shuffling of path segments. For these operations, you need to make a shallow copy first:
my @path_clone = @{$path}; # now you can alter @path_clone which ever way you want
The points of a single path element can be changed directly, however, without the need for a shallow copy:
$path->[$i]{points} = [[3, 4], [5, 6], [7, 8]];
A color stop is represented as an array reference with five elements: offset, red, green, blue, and alpha.
Glyphs are represented as anonymous hash references with three keys: index, x and y. Example:
my @glyphs = ({ index => 1, x => 2, y => 3 }, { index => 2, x => 3, y => 4 }, { index => 3, x => 4, y => 5 });
If your cairo library supports it, the FreeType integration allows you to load font faces from font files. You can query for this capability with Cairo::HAS_FT_FONT. To actually use this, you'll need the Font::FreeType module.
Cairo::HAS_FT_FONT
This method allows you to create a Cairo::FontFace from a Font::FreeType::Face. To obtain the latter, you can for example load it from a file:
my $file = '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf'; my $ft_face = Font::FreeType->new->face ($file); my $face = Cairo::FtFontFace->create ($ft_face);
For hysterical reasons, you can also use the following syntax:
$similar = $other->create_similar ($content, $width, $height)
These two functions return the version of libcairo that the program is currently running against.
Returns the version of libcairo that Cairo was compiled against.
Encodes the version $major.$minor.$micro as an integer suitable for comparison against Cairo->lib_version and Cairo->LIB_VERSION.
$major.$minor.$micro
Cairo->lib_version
Cairo->LIB_VERSION
Lists many available resources including tutorials and examples
Contains the reference manual
Copyright (C) 2004-2013 by the cairo perl team
To install Cairo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cairo
CPAN shell
perl -MCPAN -e shell install Cairo
For more information on module installation, please visit the detailed CPAN module installation guide.