Code

Translations. French translation minor update.
[inkscape.git] / share / extensions / Inkscape.pm
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 =head1 NAME
8 Inkscape - a helper for Inkscape extensions writen in PERL
10 =head1 SYNOPSIS
12     use Inkscape;
13     my ($w, $h) = $inkscape->getCanvasSize;
14     $svg->setElAttribute {tag=>'svg',pos=>0}, 'width', $w * 2;
16 =head1 DESCRIPTION
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
25 =cut
27 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
28 #                           Inkscape Package                                #
29 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30 {
31 package Inkscape;
32 our $VERSION = "0.01";
34 =head2 The %args hash
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.
42 =cut
44 #use vars qw($VERSION $inkscape %args @ISA @EXPORT %EXPORT_TAGS $TODO);
45 #require Exporter;
46 #@ISA = qw(Exporter);
47 #my $inkscape = Inkscape->new;
48 #@EXPORT = qw( $inkscape );
50 sub import {
51   if ( defined $_[1] && $_[1] eq 'AUTO_LOAD' ) {
52     #$inkscape->getArgs(@ARGV);
53     if ( $#ARGV > -1 && -f $ARGV[$#ARGV] ) {
54       #$inkscape->getSVG($ARGV[$#ARGV]);
55     }
56   }
57 }
59 sub getArgs {
60   my $self = shift;
61   my @argv = @_;
62   @argv = @ARGV if ( $#argv == -1 );
63   my %args = (id => []);
64   foreach ( @argv ) {
65     if ( m/^--([^=]+)=(.*)$/ ) {
66       my $key = $1;
67       my $val = $2;
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} ];
73         }
74         push( @{$args{$key}}, $val );
75       } else {
76         $args{$key} = $val;
77       }
78     }
79   }
80   %args;
81 }
83 sub getSVG{
84   my $self = shift;
85   my $file = shift;
86   SVGHelper->new( $file );
87 }
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
93 the methods.
95 =cut
97 my $singleton;
98 sub new {
99   my $class = shift;
100   my $self  = {};
101   #$self->{args} = ( id => [] );
102   $singleton ||= bless $self, $class;
105 } # end package Inkscape
108 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
109 #                            SVG Package                                    #
110 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
112 package SVGHelper;
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.
121 =head3 new
123     my $mysvg = SVGHelper->new( '/path/to/my.svg' );
125 Instantiates SVGHelper with a SVG file.
127 =cut
129 sub new {
130   my ( $class, $file ) = @_;
131   my $self  = {};
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>);
136   close SVG;
137   bless $self, $class;
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.
152     B<{pos}> = <number>
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>
158         Select by
160 =cut
162 sub generateSearchRegExp {
163   # '.*' in a value will break all. we must change '.' by '[^"]'
164   
167 =head3 setElAttribute
169     # Setting an attribute in a tag localizated it's id:
170     $svg->setElAttribute {id=>'myDrawing'}, 'width', $w * 2;
171     # Setting an attribute in a tag localizated it's position:
172     $svg->setElAttribute {tag=>'svg',pos=>0}, 'width', $w * 2;
173     # Setting an attribute in some tags localizated by attributes:
174     $svg->setElAttribute {tag=>'circle',r=>10}, 'r', 50;
176 This method will set an attribute in a tag or in a colection of tags, selected
177 by the localizator hash.
179 =cut
181 sub setElAttribute {
182   my $self = shift;
183   my ( $searchArgs, $att, $val ) = @_;
184   print $self->generateSearchRegExp( $searchArgs );
187 sub convertUnit {
188   my $self = shift;
189   $_[0] =~ m/^([.0-9]*)\s*([^ ]*)/;
190   my $num = $1;
191   my $unFrom = $2 || 'px';
192   my $unTo = $_[1];
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)
200   my %equivPX = (
201         px => 1,
202         pt => 1.25,
203         pc => 15,
204         mm => 3.543307,
205         cm => 35.43307,
206         in => 90
207       );
208   ( ( $num * $equivPX{$unFrom} ) / $equivPX{$unTo} ) . ( $appendUnit ? $unTo : '' );
211 =head3 getCanvasSize
213     my ($w, $h) = $svg->getCanvasSize;
215 Get the B<width> and B<height> values of the B<<svg>> tag.
217 =cut
219 sub getCanvasSize {
220   my $self = shift;
221   my $unitTo = $_[0];
222   $self->{code} =~ m/<svg\s[^>]*width="([^"]*)"[^>]*height="([^"]*)"[^>]*>/;
223   my ( $w, $h ) =
224        ( $unitTo ) ?
225        ( $self->convertUnit($1, $unitTo), $self->convertUnit($2, $unitTo) ) :
226        ( $1, $2 );
229 my $sysNULL = ( -e '/dev/null' )? '/dev/null' : 'NIL';
231 sub getElPosition {
232   my $self = shift;
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;
238 sub getElSize {
239   my $self = shift;
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
247 =head1 AUTHOR
249 Aurelio A. Heckert <aurium@gmail.com>
251 =head1 COPYRIGHT
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
258 =cut
260 1;