scale viewbox along with width/height
Tara L Andrews [Wed, 5 Sep 2012 11:03:01 +0000 (13:03 +0200)]
lib/Text/Tradition/Stemma.pm

index 5ceffaa..f530af0 100644 (file)
@@ -445,18 +445,24 @@ sub as_svg {
                my $height = $svgdoc->documentElement->getAttribute('height');
                $width =~ s/\D+//g;
                $height =~ s/\D+//g;
-               my( $remove, $keep, $val );
+               my( $remove, $keep, $val, $viewbox );
                if( $width > $height ) {
                        $remove = 'height';
                        $keep = 'width';
                        $val = $ew . 'px';
+                       my $vbheight = $width / $ew * $height;
+                       $viewbox = "0.00 0.00 $width.00" . sprintf( "%.2f", $vbheight );
                } else {
                        $remove = 'width';
                        $keep = 'height';
                        $val = $eh . 'px';
+                       my $vbwidth = $height / $eh * $width;
+                       $viewbox = "0.00 0.00 " . sprintf( "%.2f", $vbwidth ) . " $height.00";
                }
                $svgdoc->documentElement->removeAttribute( $remove );
                $svgdoc->documentElement->setAttribute( $keep, $val );
+               $svgdoc->documentElement->removeAttribute( 'viewBox' );
+               $svgdoc->documentElement->setAttribute( 'viewBox', $viewbox );
                $svg = $svgdoc->toString();
        }
     # Return the result