SVGPDF/Path.pm

filter: Statementstmt 98.6% 141 / 143 Branchbran 81.8% 36 / 44 Conditioncond 40.0% 6 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 86.5% 193 / 223 CC 28 SCAR 31.4
CC
28
Cov
91%
process
29.5
BEGIN
1.0
BEGIN
1.0
CRAP 23.2
« SVGPDF/Image.pm ↑ Summary SVGPDF/Tspan.pm »
1 #! perl
2  
3 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class SVGPDF::Path :isa(SVGPDF::Element);
9  
10 2 ▶use SVGPDF::Contrib::PathExtract qw( extract_path_info );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 41 ▶method process () {
Subroutine process: called CC=28 SCAR=33.9
Pod undocumented
13 41 my $atts = $self->atts;
14 41 my $xo = $self->xo;
15 41 ▶ return if $atts->{omit}; # for testing/debugging.
Branch: 1 branch
BranchTrueFalseTotal
if $atts->{"omit"} 0 41 41
16  
17 41 ▶ if ( defined $atts->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $atts->{"id"}) 2 39 41
18 2 $self->root->defs->{ "#" . $atts->{id} } = $self;
19 # MathJax uses curves to draw glyphs. These glyphs are filles
20 # *and* stroked with a very small stroke-width. According to
21 # the PDF specs, this should yield a 1-pixel (device pixel)
22 # stroke, which results in fat glyphs on screen.
23 # To avoid this, disable stroke when drawing MathJax glyphs.
24 2 ▶ if ( $atts->{id} =~ /^MJX-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts->{"id"} =~ /^MJX-/u) 0 2 2
25 0 $atts->{stroke} = 'none';
26 }
27 }
28  
29 41 my ( $d, $tf ) = $self->get_params( $atts, "d:!", "transform:s" );
30  
31 41 ( my $t = $d ) =~ s/\s+/ /g;
32 41 ▶ $t = substr($t,0,20) . "..." if length($t) > 20;
Branch: 1 branch
BranchTrueFalseTotal
if length $t > 20 37 4 41
33 41 ▶ $self->_dbg( $self->name, " d=\"$t\"", $tf ? " tf=\"$tf\"" : "" );
Branch: 1 branch
BranchTrueFalseTotal
$tf ? : 0 41 41
34 41 ▶ return unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 41 41
35  
36 41 $self->_dbg( "+ xo save" );
37 41 $xo->save;
38 41 $self->set_transform($tf);
39 41 $self->set_graphics;
40  
41 # Get path info, turning relative coordinates into absolute
42 # and eliminate S and T curves.
43 41 my @d = extract_path_info( $d, { absolute => 1, no_smooth => 1 } );
44  
45 41 my $open; # path is open
46  
47 41 my $paint = $self->_paintsub;
48  
49 # Initial x,y for path, if open. See 'z'.
50 41 my ( $ix, $iy );
51  
52 # Current point. Starting point of this path.
53 # Since we're always use absolute coordinates, this is the
54 # starting point for subpaths as well.
55 41 my ( $cx, $cy ) = ( 0, 0 );
56  
57 # For debugging: collect control points.
58 41 my @cp;
59  
60 41 my $id = -1; # for diagnostics
61 41 ▶ while ( @d ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@d) 123 41 164
62 123 my $d = shift(@d);
63 123 my $op = $d->{svg_key};
64 123 $id++;
65  
66 # Reset starting point for the subpath.
67 123 my ( $x, $y ) = ( 0, 0 );
68  
69 # Remember initial point of path.
70 123 ▶ $ix = $cx, $iy = $cy unless $open++ || $op eq "Z";
Branch: 1 branch
BranchTrueFalseTotal
unless $open++ or $op eq "Z" 41 82 123
Condition66%2/3
$open++ or $op eq "Z"
l!l&&r!l&&!r
82041
MC/DC50%1/2
$open++ or $op eq "Z"
$open++ $op eq "Z"
Truth table66%2/3
A or B
A= $open++B= $op eq "Z"
ABresult
1X1
011
000
71  
72 123 ▶ warn(sprintf("%s[%d] x=%.2f,y=%.2f cx=%.2f,cy=%.2f ix=%.2f,iy=%.2f\n",
Branch: 1 branch
BranchTrueFalseTotal
if 0 & ($x || $y || $ix || $iy) 0 123 123
Condition33%3/9
$x || $y || $ix || $iy
l!l&&r!l&&!r
00123
$x or $y or $ix
l!l&&r!l&&!r
00123
$x or $y
l!l&&r!l&&!r
00123
MC/DC0%0/4
$x || $y || $ix || $iy
$x $y $ix $iy
Truth table20%1/5
A or B or C || D
A= $xB= $yC= $ixD= $iy
ABCDresult
1XXX1
01XX1
001X1
00011
00000
73 $op, $id, $x, $y, $cx, $cy, $ix, $iy))
74 if 0 & ($x || $y || $ix || $iy);
75  
76 # MoveTo
77 123 ▶ if ( $op eq "M" ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($op eq 'M') { } 43 80 123
elsif ($op eq 'H') { } 1 79 80
elsif ($op eq 'V') { } 1 78 79
elsif ($op eq 'L') { } 29 49 78
elsif ($op eq 'C') { } 11 38 49
elsif ($op eq 'Q') { } 10 28 38
elsif ($op eq 'A') { } 22 6 28
elsif ($op eq 'Z') { } 6 0 6
78 43 $x += $d->{point}->[0];
79 43 $y += $d->{point}->[1];
80 43 $self->_dbg( "xo move(%.2f,%.2f)", $x, $y );
81 43 $xo->move( $x, $y );
82 }
83  
84 # Horizontal LineTo.
85 elsif ( $op eq "H" ) {
86 1 $x += $d->{x};
87 1 $y = $cy;
88 1 $self->_dbg( "xo hline(%.2f)", $x );
89 1 $xo->hline($x);
90 }
91  
92 # Vertical LineTo.
93 elsif ( $op eq "V" ) {
94 1 $x = $cx;
95 1 $y += $d->{y};
96 1 $self->_dbg( "xo vline(%.2f)", $y );
97 1 $xo->vline($y);
98 }
99  
100 # Generic LineTo.
101 elsif ( $op eq "L" ) {
102 29 $x += $d->{point}->[0];
103 29 $y += $d->{point}->[1];
104 29 $self->_dbg( "xo line(%.2f,%.2f)", $x, $y );
105 29 $xo->line( $x, $y );
106 }
107  
108 # Cubic Bézier curves.
109 elsif ( $op eq "C" ) {
110 my @c = ( # control point 1
111 $x + $d->{control1}->[0],
112 $y + $d->{control1}->[1],
113 # control point 2
114 $x + $d->{control2}->[0],
115 $y + $d->{control2}->[1],
116 # end point
117 $x + $d->{end}->[0],
118 11 $y + $d->{end}->[1],
119 );
120 11 $self->_dbg( "xo curve(%.2f,%.2f %.2f,%.2f %.2f,%.2f)", @c );
121 11 $xo->curve(@c);
122 11 push( @cp, [ $cx, $cy, $c[0], $c[1] ] );
123 11 push( @cp, [ $c[4], $c[5], $c[2], $c[3] ] );
124 11 $x = $c[4]; $y = $c[5]; # end point
125 }
126  
127 # Quadratic Bézier curves.
128 elsif ( $op eq "Q" ) {
129 my @c = ( # control point 1
130 $x + $d->{control}->[0],
131 $y + $d->{control}->[1],
132 # end point
133 $x + $d->{end}->[0],
134 10 $y + $d->{end}->[1],
135 );
136 10 $self->_dbg( "xo spline(%.2f,%.2f %.2f,%.2f)", @c );
137 10 $xo->spline(@c);
138 10 push( @cp, [ $cx, $cy, $c[0], $c[1] ] );
139 10 push( @cp, [ $c[2], $c[3], $c[0], $c[1] ] );
140 10 $x = $c[2]; $y = $c[3]; # end point
141 }
142  
143 # Arcs.
144 elsif ( $op eq "A" ) {
145 22 my $rx = $d->{rx}; # radius 1
146 22 my $ry = $d->{ry}; # radius 2
147 22 my $rot = $d->{x_axis_rotation}; # rotation
148 22 my $large = $d->{large_arc_flag}; # select larger arc
149 22 my $sweep = $d->{sweep_flag}; # clockwise
150 22 my $ex = $x + $d->{x}; # end point
151 22 my $ey = $y + $d->{y};
152 22 $self->_dbg( "xo arc(%.2f,%.2f %.2f %d,%d %.2f,%.2f)",
153 $rx, $ry, $rot, $large, $sweep, $ex, $ey );
154  
155 # for circular arcs.
156 22 ▶ if ( $rx == $ry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx == $ry) { } 11 11 22
157 11 $self->_dbg( "circular_arc(%.2f,%.2f %.2f,%.2f %.2f ".
158 "move=%d large=%d dir=%d rot=%.2f)",
159 $cx, $cy, $ex, $ey, $rx,
160 0, $large, $sweep, $rot );
161 11 $self->circular_arc( $cx, $cy, $ex, $ey, $rx,
162 move => 0,
163 large => $large,
164 rotate => $rot,
165 dir => $sweep );
166 }
167 else {
168 11 $self->_dbg( "elliptic_arc(%.2f,%.2f %.2f,%.2f %.2f,%.2f ".
169 "move=%d large=%d dir=%d rot=%.2f)",
170 $cx, $cy, $ex, $ey, $rx, $ry,
171 0, $large, $sweep, $rot );
172 11 $self->elliptic_arc( $cx, $cy, $ex, $ey,
173 $rx, $ry,
174 move => 0,
175 large => $large,
176 rotate => $rot,
177 dir => $sweep );
178 }
179 22 ( $x, $y ) = ( $ex, $ey ); # end point
180 }
181  
182 # Close path and paint.
183 elsif ( $op eq "Z" ) {
184 6 $self->_dbg( "xo z" );
185 6 ▶ if ( $open ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open) 6 0 6
186 6 $xo->close;
187 6 $open = 0;
188 # currentpoint becomes the initial point.
189 6 $x = $ix;
190 6 $y = $iy;
191 }
192 6 ▶ if ( @d && $d[0]->{svg_key} eq 'M' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@d and $d[0]{'svg_key'} eq 'M') { } 0 6 6
Condition33%1/3
@d and $d[0]{"svg_key"} eq "M"
!ll&&!rl&&r
600
MC/DC0%0/2
@d and $d[0]{"svg_key"} eq "M"
@d $d[0]{"svg_key"} eq "M"
Truth table33%1/3
A and B
A= @dB= $d[0]{"svg_key"} eq "M"
ABresult
0X0
100
111
193 # Close is followed by a move -> do not paint yet.
194 }
195 else {
196 6 $paint->();
197 }
198 }
199  
200 # Unidenfied subpath element.
201 else {
202 0 croak("Unidenfied subpath element[$id] $op");
203 }
204  
205 123 ▶ ( $cx, $cy ) = ( $x, $y ) unless $op eq "Z";
Branch: 1 branch
BranchTrueFalseTotal
unless $op eq "Z" 117 6 123
206 }
207  
208 41 ▶ $paint->() if $open;
Branch: 1 branch
BranchTrueFalseTotal
if $open 35 6 41
209 41 $self->_dbg( "- xo restore" );
210 41 $xo->restore;
211  
212 # Show collected control points.
213 41 if ( 0 && $self->root->debug && @cp ) {
214 $xo->save;
215 $xo->stroke_color('lime');
216 $xo->line_width(0.5);
217 for ( @cp ) {
218 $self->_dbg( "xo line(%.2f %.2f %.2f %.2f)", @$_ );
219 $xo->move( $_->[0], $_->[1] );
220 $xo->line( $_->[2], $_->[3] );
221 }
222 $xo->stroke;
223 $xo->restore;
224 }
225  
226 41 $self->css_pop;
227 2 }
228  
229 189 ▶method curve ( @points ) {
Subroutine curve: called CC=1 SCAR=0.0
Pod undocumented
230 189 $self->_dbg( "+ xo curve( %.2f,%.2f %.2f,%.2f %.2f,%.2f )", @points );
231 189 $self->xo->curve(@points);
232 189 $self->_dbg( "-" );
233 2 }
234  
235 11 ▶method elliptic_arc( $x1,$y1, $x2,$y2, $rx,$ry, %opts) {
Subroutine elliptic_arc: called CC=1 SCAR=0.0
Pod undocumented
236 11 require SVGPDF::Contrib::Bogen;
237  
238 11 SVGPDF::Contrib::Bogen::bogen_ellip
239 ( $self, $x1,$y1, $x2,$y2, $rx,$ry, %opts );
240 2 }
241  
242 11 ▶method circular_arc( $x1,$y1, $x2,$y2, $r, %opts) {
Subroutine circular_arc: called CC=1 SCAR=0.0
Pod undocumented
243 11 require SVGPDF::Contrib::Bogen;
244  
245 SVGPDF::Contrib::Bogen::bogen
246 ( $self, $x1,$y1, $x2,$y2, $r,
247 11 $opts{move}, $opts{large}, $opts{dir} );
248 2 }
249  
250 2 1;
« SVGPDF/Image.pm ↑ Summary SVGPDF/Tspan.pm »