X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FStemma.pm;fp=lib%2FText%2FTradition%2FStemma.pm;h=d6f964620de7048e772ee97ad3abee8ee6a1259c;hb=c57be097fd091d43c1e3d19d2e1fef918a3e4b3b;hp=e7e3946a31fbb30aca81098bd807108290d3fac0;hpb=e4b73942dd7d58e6e3cb7a42b1f348b56021192e;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Stemma.pm b/lib/Text/Tradition/Stemma.pm index e7e3946..d6f9646 100644 --- a/lib/Text/Tradition/Stemma.pm +++ b/lib/Text/Tradition/Stemma.pm @@ -107,7 +107,7 @@ has graph => ( isa => 'Graph', predicate => 'has_graph', ); - + sub BUILD { my( $self, $args ) = @_; # If we have been handed a dotfile, initialize it into a graph. @@ -349,8 +349,27 @@ sub as_svg { # Get rid of width and height attributes to allow scaling. my $parser = XML::LibXML->new(); my $svgdoc = $parser->parse_string( decode_utf8( $svg ) ); - $svgdoc->documentElement->removeAttribute('width'); - $svgdoc->documentElement->removeAttribute('height'); + if( $opts->{'size'} ) { + my( $ew, $eh ) = @{$opts->{'size'}}; + # If the graph is wider than it is tall, set width to ew and remove height. + # Otherwise set height to eh and remove width. + my $width = $svgdoc->documentElement->getAttribute('width'); + my $height = $svgdoc->documentElement->getAttribute('height'); + $width =~ s/\D+//g; + $height =~ s/\D+//g; + my( $remove, $keep, $val ); + if( $width > $height ) { + $remove = 'height'; + $keep = 'width'; + $val = $ew . 'px'; + } else { + $remove = 'width'; + $keep = 'height'; + $val = $eh . 'px'; + } + $svgdoc->documentElement->removeAttribute( $remove ); + $svgdoc->documentElement->setAttribute( $keep, $val ); + } # Return the result return decode_utf8( $svgdoc->toString ); }