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   1321 use v5.26;
  2         6  
4 2     2   9 use Object::Pad;
  2         3  
  2         10  
5 2     2   166 use utf8;
  2         4  
  2         9  
6 2     2   36 use Carp;
  2         65  
  2         350  
7              
8             class SVGPDF::Rect :isa(SVGPDF::Element);
9              
10 12     12 0 18 method process () {
  12         26  
  12         14  
11 12         34 my $atts = $self->atts;
12 12         33 my $xo = $self->xo;
13 12 50       30 return if $atts->{omit}; # for testing/debugging.
14              
15 12         42 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         38 $self->_dbg( $self->name, " x=$x y=$y w=$w h=$h" );
19 12         43 $self->_dbg( "+ xo save" );
20 12         55 $xo->save;
21              
22 12         573 $self->set_graphics;
23 12 100       35 $self->set_transform($tf) if $tf;
24              
25 12 100 66     49 unless ( $rx || $ry ) {
26 10         55 $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       6 if ( $rx ) {
32 2 50       11 $rx = $1 * $w if $rx =~ /^([-+,\d]+)\%$/;
33             }
34 2 50       5 if ( $ry ) {
35 2 50       8 $ry = $1 * $h if $ry =~ /^([-+,\d]+)\%$/;
36             }
37             # Default one to the other.
38 2   33     29 $rx ||= $ry; $ry ||= $rx;
  2   33     5  
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       7 if ( $ry > $h/2 ) {
44 0         0 $ry = $h/2;
45             }
46 2         8 $self->_dbg( $self->name, "(rounded) rx=$rx ry=$ry" );
47              
48 2         13 $xo->move( $x+$rx, $y );
49 2         189 $xo->hline( $x + $w - $rx );
50 2         184 $xo->arc( $x+$w-$rx, $y+$ry, $rx, $ry, -90, 0 );
51 2         1640 $xo->vline( $y + $h - $ry );
52 2         93 $xo->arc( $x+$w-$rx, $y+$h-$ry, $rx, $ry, 0, 90 );
53 2         1168 $xo->hline( $x + $rx );
54 2         88 $xo->arc( $x+$rx, $y+$h-$ry, $rx, $ry, 90, 180 );
55 2         1588 $xo->vline( $y + $ry );
56 2         127 $xo->arc( $x+$rx, $y+$ry, $rx, $ry, 180, 270 );
57 2         1337 $xo->close;
58             }
59              
60 12         928 $self->_paintsub->();
61              
62 12         395 $self->_dbg( "- xo restore" );
63 12         44 $xo->restore;
64 12         445 $self->css_pop;
65             }
66              
67             1;