aboutsummaryrefslogtreecommitdiff
path: root/src/SVG/SVG2zinc/Backend/PerlClass.pm
blob: 4819b6227a1d50a7f583de944e7af35666605f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package SVG::SVG2zinc::Backend::PerlClass;
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU LGPL Libray General Public License
#	as published by the Free Software Foundation; either version 2
#	of the License, or (at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU Library General Public License for more details.
#
#	You should have received a copy of the GNU Library General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
#	or refer to http://www.gnu.org/copyleft/lgpl.html
#
##################################################################

#	Backend Class for SVG2zinc
# 
#	Copyright 2003-2004
#	Centre d'?tudes de la Navigation A?rienne
#
#	Author: Christophe Mertz <mertz at intuilab dot com>
#
#       An concrete class for code generation for Perl Class
#
# $Id: PerlClass.pm,v 1.4 2007-03-12 10:25:18 merlin Exp $
#############################################################################

use SVG::SVG2zinc::Backend;
use File::Basename;

use Tk::Zinc;

@ISA = qw( SVG::SVG2zinc::Backend );

use vars qw( $VERSION);
($VERSION) = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);

use strict;
use Carp;

our $current_package_name = '';

sub _initialize {
    my ($self, %passed_options) = @_;
    $self->{-topgroup} = '$self->{-topgroup}'; # this code is used by SVG2zinc
    $self->SUPER::_initialize(%passed_options);
    $self -> {delayed_lines} = ();
    $self -> {gradient} = {};
    $self -> {gradient_id} = 0;
    return $self;
}

sub recordGradient {
	my ($self, $name, $def) = @_;
	$self -> {gradient} -> {$name} = $def;
}

sub getGradient {
	my ($self, $name) = @_;
	return $self -> {gradient} -> {$name} -> {stops};
}

sub applyGradient {
	my ($self, $name, $prop) = @_;
	my $grad = $self -> {gradient} -> {$name};
	my $id = $self -> {gradient_id} ++;
	my %hash = %{$grad};
	my @lignes = 'my ($x1, $y1, $x2, $y2) = $_zinc -> bbox ($previous);';
#	push (@lignes, 'my ($parent) = $_zinc -> find(\'ancestors\', $previous);');
	push (@lignes, '($x1, $y1, $x2, $y2) = $_zinc -> transform(\'device\', $parent, [$x1+1, $y1+1, $x2-2, $y2-2]);');
	push (@lignes, "my \$grad = getGradient (");
	push (@lignes, "\t'$current_package_name',");
	push (@lignes, "\t'$id',");
	push (@lignes, "\t'".$hash {type}."',");
	push (@lignes, "\t'".$hash {gradientUnits}."',");
	push (@lignes, "\t".join (',' , @{$hash{coords}}).",");
	push (@lignes, "\t'".join ('|' , @{$hash{stops}})."',");
	push (@lignes, "\t".join (',' , @{$hash{transform}}).",");
	push (@lignes, "\t\$x1, \$y1, \$x2, \$y2");
	push (@lignes, ");");
	push (@lignes, "mconfigure (\$previous, $prop => \$grad);");
	
	$self -> {current_gradient} = \@lignes;
}

sub treatLines {
    my ($self,@lines) = @_;
    foreach my $l (@lines) 
    {
		$l =~ s/^(\s*)->/$1\$_zinc->/g;
		$l =~ s/(\W)->/$1\$_zinc->/g;
		$self -> printLines($l);
    }
}

sub dtreatLines {
    my ($self, @lines) = @_;
    foreach my $l (@lines) 
    {
		$l =~ s/^(\s*)->add/$1\$previous = \$_zinc-> add/g;
		$l =~ s/(\W)->add/$1\$previous = \$_zinc-> add/g;
		
		$l =~ s/^(\s*)->/$1\$_zinc-> /g;
		$l =~ s/(\W)->/$1\$_zinc-> /g;
		
    }
    if (defined @lines)
    {
		my $rule = shift (@lines);
		push (@{$self -> {delayed_lines}}, $rule);
    }
    if (defined $self -> {current_gradient})
    {
	    my @grad = @{$self -> {current_gradient}};
	    foreach my $l (@grad) 
    	{
	    	push (@{$self -> {delayed_lines}}, $l);
    	}
    	$self -> {current_gradient} = undef;
    }
    foreach my $l (@lines) 
    {
    	push (@{$self -> {delayed_lines}}, $l);
    }
}

sub fileHeader {
    my ($self) = @_;
    my $file = $self -> {-in}; # print "file=$file\n";
    my ($svg2zincPackage) = caller;
    my $VERSION = eval ( "\$".$svg2zincPackage."::VERSION" );
    my ($package_name) = $self -> {-out} =~ /(.*)\.pm$/ ;
	$current_package_name = $package_name;
	$self -> printLines("package $package_name;

####### This file has been generated from $file by SVG::SVG2zinc.pm Version: $VERSION

");
    $self->printLines(
<<'HEADER'

use strict;
use MTools;
use MTools::GUI::MGroup;
use vars qw /@ISA @EXPORT @EXPORT_OK/;
use Tk::PNG;
use Tk::JPEG;

BEGIN 
{
	@ISA = qw /MTools::GUI::MGroup/;
}
	      

sub new {
    my ($class, %passed_options) = @_;
    my $self = {};
    bless $self, $class;

    my $_zinc = $passed_options {-zinc};
    croak ("-zinc option is mandatory at instanciation") unless defined $_zinc;

    if (defined $passed_options {-topgroup}) 
    {
		$self -> {-topgroup} = $passed_options {-topgroup};
    }
    else 
    {
		$self -> {-topgroup} = 1;
    }
    
    my $parent = $self -> {-topgroup};
    my @parents = ();
    my $previous = ();
    push (@parents, $parent);

# on now items creation!
HEADER
);
}


sub fileTail {
    my ($self) = @_;
    $self->comment ("", "Tail of SVG2zinc::Backend::PerlScript", "");
    unshift (@{$self -> {delayed_lines}}, '$self -> {instance} = $previous = ');
    $self -> printLines(@{$self -> {delayed_lines}});
    $self -> printLines(
<<'TAIL'
return $self;
}

1;
TAIL
);
    $self->close;
}


1;