File Coverage

blib/lib/SVGPDF/Rect.pm
Criterion Covered Total %
statement 48 50 96.0
branch 11 18 61.1
condition 4 9 44.4
subroutine 5 5 100.0
pod 0 1 0.0
total 68 83 81.9


line stmt bran cond sub pod time code
1             #! perl
2              
3 2     2   1464 use v5.26;
  2         9  
4 2     2   11 use Object::Pad;
  2         3  
  2         13  
5 2     2   262 use utf8;
  2         3  
  2         11  
6 2     2   132 use Carp;
  2         4  
  2         473  
7              
8             class SVGPDF::Rect :isa(SVGPDF::Element);
9              
10 12     12 0 26 method process () {
  12         44  
  12         25  
11 12         54 my $atts = $self->atts;
12 12         74 my $xo = $self->xo;
13 12 50       88 return if $atts->{omit}; # for testing/debugging.
14              
15 12         98 my ( $x, $y, $w, $h, $rx, $ry, $tf ) =
16             $self->get_params( $atts, qw( x:H y:V width:H height:V rx:U ry:U transform:s ) );
17              
18 12         90 $self->_dbg( $self->name, " x=$x y=$y w=$w h=$h" );
19 12         68 $self->_dbg( "+ xo save" );
20 12         90 $xo->save;
21              
22 12         833 $self->set_graphics;
23 12 100       60 $self->set_transform($tf) if $tf;
24              
25 12 100 66     65 unless ( $rx || $ry ) {
26 10         80 $xo->rectangle( $x, $y, $x+$w, $y+$h );
27             }
28             else {
29             # https://svgwg.org/svg2-draft/shapes.html#RectElement
30             # Resolve percentages.
31 2 50       8 if ( $rx ) {
32 2 50       13 $rx = $1 * $w if $rx =~ /^([-+,\d]+)\%$/;
33             }
34 2 50       9 if ( $ry ) {
35 2 50       24 $ry = $1 * $h if $ry =~ /^([-+,\d]+)\%$/;
36             }
37             # Default one to the other.
38 2   33     8 $rx ||= $ry; $ry ||= $rx;
  2   33     6  
39             # Maximize to half of the width/height.
40 2 50       11 if ( $rx > $w/2 ) {
41 0         0 $rx = $w/2;
42             }
43 2 50       8 if ( $ry > $h/2 ) {
44 0         0 $ry = $h/2;
45             }
46 2         11 $self->_dbg( $self->name, "(rounded) rx=$rx ry=$ry" );
47              
48 2         22 $xo->move( $x+$rx, $y );
49 2         247 $xo->hline( $x + $w - $rx );
50 2         267 $xo->arc( $x+$w-$rx, $y+$ry, $rx, $ry, -90, 0 );
51 2         2439 $xo->vline( $y + $h - $ry );
52 2         184 $xo->arc( $x+$w-$rx, $y+$h-$ry, $rx, $ry, 0, 90 );
53 2         2124 $xo->hline( $x + $rx );
54 2         178 $xo->arc( $x+$rx, $y+$h-$ry, $rx, $ry, 90, 180 );
55 2         2095 $xo->vline( $y + $ry );
56 2         177 $xo->arc( $x+$rx, $y+$ry, $rx, $ry, 180, 270 );
57 2         2119 $xo->close;
58             }
59              
60 12         2051 $self->_paintsub->();
61              
62 12         737 $self->_dbg( "- xo restore" );
63 12         72 $xo->restore;
64 12         731 $self->css_pop;
65             }
66              
67             1;