aboutsummaryrefslogtreecommitdiff
path: root/src/SVG/SVG2zinc/Backend/Image.pm.k
blob: bfd78511b40cc954ef0adc6ea3588a3acb37167e (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
package SVG::SVG2zinc::Backend::Image;
#	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 to generate image files
# 
#	Copyright 2003
#	Centre d'Études de la Navigation Aérienne
#
#	Author: Christophe Mertz <mertz at intuilab dot com>
#
# $Id: Image.pm.k,v 1.1.1.1 2006-10-20 13:34:31 merlin Exp $
#############################################################################

use SVG::SVG2zinc::Backend;

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

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

use strict;
use Carp;
use Tk::Zinc::SVGExtension;

eval (require Tk::Zinc);
if ($@) {
    print "$@\nSVG::SVG2zinc::Backend requires Tk::Zinc to be installed\n";
}

sub new {
    # testing that 'import' is available
    # is this test portable?
    my $ret = `which import`;
    croak ("## You need the 'import' command from 'imagemagic' package to use the Image backend.\n")
	if !$ret;
    
    my ($class, %passed_options) = @_;
    my $self = {};
    bless $self, $class;
    $self->_initialize(%passed_options);
    return $self;
}

my $zinc;
sub _initialize {
    my ($self, %passed_options) = @_;
    if (defined $passed_options{-ratio}) {
	if ($passed_options{-ratio} !~ /^\d+%$/) {
	    croak ("## -ratio should look like nn%");
	} else {
	    $self->{-ratio} = delete $passed_options{-ratio};
	}
    }
    if (defined $passed_options{-width}) {
	$self->{-width} = delete $passed_options{-width};
    }
    if (defined $passed_options{-height}) {
	$self->{-height} = delete $passed_options{-height};
    }

    $self->SUPER::_initialize(%passed_options);

    my $mw = MainWindow->new();
    my $svgfile = $self->{-in};
    $mw->title($svgfile);
    my $render = (defined $self->{-render}) ? $self->{-render} : 1;
    $zinc = $mw->Zinc(-borderwidth => 0,
		      -render => $render,
		      -backcolor => "white", ## why white?
		      )->pack(qw/-expand yes -fill both/);
}


sub treatLines {
    my ($self,@lines) = @_;
    my $verbose = $self->{-verbose};
    foreach my $l (@lines) {
	my $expr = $l;
	$expr =~ s/->/\$zinc->/g;
	my $unused = $zinc; ## due to a perl bug, this is needed so that $zinc will be known in $expr
	my $r = eval ($expr);
	if ($@) {
#	    &myWarn ("While evaluationg:\n$expr\nAn Error occured: $@\n");
	    print ("While evaluationg:\n$expr\nAn Error occured: $@\n");
	} elsif ($verbose) {
	    if ($l =~ /^->add/) {
		print "$r == $expr\n" if $verbose;
	    } else {
		print "$expr\n" if $verbose;
	    }
	}
    }
}


sub fileHeader {
#    my ($self) = @_;
}


sub fileTail {
    my ($self) = @_;
    my $outfile = $self->{-out};

    # to find the top group containing width and height
    my $svgGroup = $zinc->find('withtag', 'svg_top') ; 

    my $tags = join " ", $zinc->gettags($svgGroup);
#    print "svgGroup=$svgGroup  => $tags\n";
    my ($width) = $tags =~ /width=(\d+)/ ;
    my ($height) = $tags =~ /height=(\d+)/ ;
#    print "height => $height width => $width\n";
    
    $zinc->configure (-width => $width, -height => $height);
    $zinc->update;

    my $requiredWidth = $self->{-width};
    my $requiredHeigth = $self->{-height};
    my $importParams="";
    if (defined $requiredWidth and defined $requiredHeigth) {
	$importParams=" -resize $requiredWidth"."x$requiredHeigth";
    } elsif (defined $self->{-ratio}) {
	$importParams=" -resize ".$self->{-ratio};
    }
#    print "importParams=$importParams\n";

    ## following are for comments:
    my ($svg2zincPackage) = caller;
    my $VERSION = eval ( "\$".$svg2zincPackage."::VERSION" );
    my $svgfile = $self->{-in};

    my $command = "import -window " . $zinc->id . $importParams ." -comment 'created with SVG::SVG2zinc from $svgfile v$VERSION (c) CENA 2003 C.Mertz.' $outfile";
#    print "command=$command\n";
    my $return = system ($command);

    if ($return) {
	## -1 when import is not available
	print "## To use the Image Backend you need the 'import' command\n";
	print "## from the 'imagemagick' package on your system\n";
    }
}


1;


__END__

=head1 NAME

SVG:SVG2zinc::Backend::Image - a backend class for generating image file from SVG file

=head1 DESCRIPTION

SVG:SVG2zinc::Backend::Image is a backend class for generating image file from SVG files. It uses the 'import' command included in the ImageMagick package.

For more information, you should look at SVG:SVG2zinc::Backend(3pm).

The new method accepts parameters described in the SVG:SVG2zinc::Backend class and the following additionnal parameters:

=over

=item B<none>

=back

=head1 SEE ALSO

SVG::SVG2zinc::Backend(3pm) and SVG::SVG2zinc(3pm)

=head1 BUGS and LIMITATIONS

This backend generates images files from the content of a displayed Tk::Zinc window. The size (in pixels) of the generated image is thus limited to the maximal size of a window on your system. 

=head1 AUTHORS

Christophe Mertz <mertz at intuilab dot com>

=head1 COPYRIGHT
    
CENA (C) 2003-2004 IntuiLab 2004

This program is free software; you can redistribute it and/or modify it under the term of the LGPL licence.

=cut