8
Inkscape - a helper for Inkscape extensions writen in PERL
13
my ($w, $h) = $inkscape->getCanvasSize;
14
$svg->setElAttribute {tag=>'svg',pos=>0}, 'width', $w * 2;
18
This package try to do the common initial work in inkscape extensions
19
and provide a collection of helper methods about inkscape interaction
20
and SVG basic manipulation.
22
If you want more power to SVG manipulation, try use SVG::DOM together.
23
http://search.cpan.org/~ronan/SVG-2.44/lib/SVG/DOM.pm
27
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
29
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
32
our $VERSION = "0.01";
36
The B<%args> hash gives to you all arguments provided by Inkscape.
38
$svg->setElAttribute 'svg', 'width', $w * $args{zoom};
40
The B<id> will allways be an array reference. Also if it is empty.
44
#use vars qw($VERSION $inkscape %args @ISA @EXPORT %EXPORT_TAGS $TODO);
47
#my $inkscape = Inkscape->new;
48
#@EXPORT = qw( $inkscape );
51
if ( defined $_[1] && $_[1] eq 'AUTO_LOAD' ) {
52
#$inkscape->getArgs(@ARGV);
53
if ( $#ARGV > -1 && -f $ARGV[$#ARGV] ) {
54
#$inkscape->getSVG($ARGV[$#ARGV]);
62
@argv = @ARGV if ( $#argv == -1 );
63
my %args = (id => []);
65
if ( m/^--([^=]+)=(.*)$/ ) {
68
$val = 1 if $val eq 'true';
69
$val = 0 if $val eq 'false';
70
if ( defined $args{$key} ) {
71
if ( ref($args{$key}) ne 'ARRAY' ) {
72
$args{$key} = [ $args{$key} ];
74
push( @{$args{$key}}, $val );
86
SVGHelper->new( $file );
89
=head2 Inkscape Methods
91
The $inkscape auto defined object is a helper to use non interactive
92
inkscape interface. You allways need to provide an SVG file path to
101
#$self->{args} = ( id => [] );
102
$singleton ||= bless $self, $class;
105
} # end package Inkscape
108
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
110
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
113
our $VERSION = "0.01";
115
=head2 SVGHelper Methods
117
The $svg auto defined object is a helper to access the SVG file provided
118
by the Inkscape. That is not a singleton, so if you want manipulate other
119
SVG files, you can instanciate a new SVGHelper.
123
my $mysvg = SVGHelper->new( '/path/to/my.svg' );
125
Instantiates SVGHelper with a SVG file.
130
my ( $class, $file ) = @_;
132
$file = $ARGV[$#ARGV] if ( ! $file );
133
open SVG, $file or die "Can't open \"$file\": $!\n";
134
$self->{file} = $file;
135
$self->{code} = join('',<SVG>);
140
=head3 generateSearchRegExp
142
if ( $svg->{code} =~ m/$svg->generateSearchRegExp({class=>"hot"})/ )
143
print "Yes! there are Hot elements!"
145
Generates the regexp string to localize tags in the SVG, based in the arguments
146
on the localizator hash.
148
Arguments to localize the tags:
149
B<{id}> = "<some tag id>"
150
Select only one tag with this id. You don't need to use other select
151
arguments with this one.
153
When you use inprecise arguments for selection the position will select
154
only one tag in the defined position at the finded list of tags.
155
B<{tag}> = "<tag_name>"
156
Select by tag name, may select a list if you don't use {pos}.
157
B<{>B<<any tag attribute>>B<}> = <a valid attribute value>
162
sub generateSearchRegExp {
163
# '.*' in a value will break all. we must change '.' by '[^"]'
167
=head3 setElAttribute
169
# Setting an atribute in a tag localizated it's id:
170
$svg->setElAttribute {id=>'myDrawing'}, 'width', $w * 2;
171
# Setting an atribute in a tag localizated it's position:
172
$svg->setElAttribute {tag=>'svg',pos=>0}, 'width', $w * 2;
173
# Setting an atribute in some tags localizated by atributes:
174
$svg->setElAttribute {tag=>'circle',r=>10}, 'r', 50;
176
This method will set an atribute in a tag or in a colection of tags, selected
177
by the localizator hash.
183
my ( $searchArgs, $att, $val ) = @_;
184
print $self->generateSearchRegExp( $searchArgs );
189
$_[0] =~ m/^([.0-9]*)\s*([^ ]*)/;
191
my $unFrom = $2 || 'px';
193
my $appendUnit = $_[2];
194
# From http://www.w3.org/TR/SVG/coords.html#Units
195
# "1pt" equals "1.25px" (and therefore 1.25 user units)
196
# "1pc" equals "15px" (and therefore 15 user units)
197
# "1mm" would be "3.543307px" (3.543307 user units)
198
# "1cm" equals "35.43307px" (and therefore 35.43307 user units)
199
# "1in" equals "90px" (and therefore 90 user units)
208
( ( $num * $equivPX{$unFrom} ) / $equivPX{$unTo} ) . ( $appendUnit ? $unTo : '' );
213
my ($w, $h) = $svg->getCanvasSize;
215
Get the B<width> and B<height> values of the B<<svg>> tag.
222
$self->{code} =~ m/<svg\s[^>]*width="([^"]*)"[^>]*height="([^"]*)"[^>]*>/;
225
( $self->convertUnit($1, $unitTo), $self->convertUnit($2, $unitTo) ) :
229
my $sysNULL = ( -e '/dev/null' )? '/dev/null' : 'NIL';
233
my $x = `inkscape --query-id=$_[0] --query-x "$self->{file}" 2>$sysNULL`;
234
my $y = `inkscape --query-id=$_[0] --query-y "$self->{file}" 2>$sysNULL`;
235
return ( $x )? ( $x, $y ) : undef;
240
my $w = `inkscape --query-id=$_[0] --query-width "$self->{file}" 2>$sysNULL`;
241
my $h = `inkscape --query-id=$_[0] --query-height "$self->{file}" 2>$sysNULL`;
242
return ( $w )? ( $w, $h ) : undef;
245
} # end package SVGHelper
249
Aurelio A. Heckert <aurium@gmail.com>
253
Copyright (C) 2008 Aurelio A. Heckert <aurium@gmail.com>
255
This PERL module is a free software licenced under LGPL v3
256
http://www.gnu.org/licenses/lgpl-3.0-standalone.html