~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to share/extensions/Inkscape.pm

  • Committer: Ted Gould
  • Date: 2008-11-21 05:24:08 UTC
  • Revision ID: ted@canonical.com-20081121052408-tilucis2pjrrpzxx
MergeĀ fromĀ fe-moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
 
 
6
=head1 NAME
 
7
 
 
8
Inkscape - a helper for Inkscape extensions writen in PERL
 
9
 
 
10
=head1 SYNOPSIS
 
11
 
 
12
    use Inkscape;
 
13
    my ($w, $h) = $inkscape->getCanvasSize;
 
14
    $svg->setElAttribute {tag=>'svg',pos=>0}, 'width', $w * 2;
 
15
 
 
16
=head1 DESCRIPTION
 
17
 
 
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.
 
21
 
 
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
 
24
 
 
25
=cut
 
26
 
 
27
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
28
#                           Inkscape Package                                #
 
29
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
30
{
 
31
package Inkscape;
 
32
our $VERSION = "0.01";
 
33
 
 
34
=head2 The %args hash
 
35
 
 
36
The B<%args> hash gives to you all arguments provided by Inkscape.
 
37
 
 
38
    $svg->setElAttribute 'svg', 'width', $w * $args{zoom};
 
39
 
 
40
The B<id> will allways be an array reference. Also if it is empty.
 
41
 
 
42
=cut
 
43
 
 
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 );
 
49
 
 
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
}
 
58
 
 
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
}
 
82
 
 
83
sub getSVG{
 
84
  my $self = shift;
 
85
  my $file = shift;
 
86
  SVGHelper->new( $file );
 
87
}
 
88
 
 
89
=head2 Inkscape Methods
 
90
 
 
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.
 
94
 
 
95
=cut
 
96
 
 
97
my $singleton;
 
98
sub new {
 
99
  my $class = shift;
 
100
  my $self  = {};
 
101
  #$self->{args} = ( id => [] );
 
102
  $singleton ||= bless $self, $class;
 
103
}
 
104
 
 
105
} # end package Inkscape
 
106
 
 
107
 
 
108
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
109
#                            SVG Package                                    #
 
110
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 
111
{
 
112
package SVGHelper;
 
113
our $VERSION = "0.01";
 
114
 
 
115
=head2 SVGHelper Methods
 
116
 
 
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.
 
120
 
 
121
=head3 new
 
122
 
 
123
    my $mysvg = SVGHelper->new( '/path/to/my.svg' );
 
124
 
 
125
Instantiates SVGHelper with a SVG file.
 
126
 
 
127
=cut
 
128
 
 
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;
 
138
}
 
139
 
 
140
=head3 generateSearchRegExp
 
141
 
 
142
    if ( $svg->{code} =~ m/$svg->generateSearchRegExp({class=>"hot"})/ )
 
143
      print "Yes! there are Hot elements!"
 
144
 
 
145
Generates the regexp string to localize tags in the SVG, based in the arguments
 
146
on the localizator hash.
 
147
 
 
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
 
159
 
 
160
=cut
 
161
 
 
162
sub generateSearchRegExp {
 
163
  # '.*' in a value will break all. we must change '.' by '[^"]'
 
164
  
 
165
}
 
166
 
 
167
=head3 setElAttribute
 
168
 
 
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;
 
175
 
 
176
This method will set an atribute in a tag or in a colection of tags, selected
 
177
by the localizator hash.
 
178
 
 
179
=cut
 
180
 
 
181
sub setElAttribute {
 
182
  my $self = shift;
 
183
  my ( $searchArgs, $att, $val ) = @_;
 
184
  print $self->generateSearchRegExp( $searchArgs );
 
185
}
 
186
 
 
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 : '' );
 
209
}
 
210
 
 
211
=head3 getCanvasSize
 
212
 
 
213
    my ($w, $h) = $svg->getCanvasSize;
 
214
 
 
215
Get the B<width> and B<height> values of the B<<svg>> tag.
 
216
 
 
217
=cut
 
218
 
 
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 );
 
227
}
 
228
 
 
229
my $sysNULL = ( -e '/dev/null' )? '/dev/null' : 'NIL';
 
230
 
 
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;
 
236
}
 
237
 
 
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;
 
243
}
 
244
 
 
245
} # end package SVGHelper
 
246
 
 
247
=head1 AUTHOR
 
248
 
 
249
Aurelio A. Heckert <aurium@gmail.com>
 
250
 
 
251
=head1 COPYRIGHT
 
252
 
 
253
Copyright (C) 2008 Aurelio A. Heckert <aurium@gmail.com>
 
254
 
 
255
This PERL module is a free software licenced under LGPL v3
 
256
http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 
257
 
 
258
=cut
 
259
 
 
260
1;