SVGPDF/CSS.pm

filter: Statementstmt 81.9% 150 / 183 Branchbran 65.4% 55 / 84 Conditioncond 69.0% 29 / 42 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 11 totaltotal 70.5% 256 / 363 CC 71 SCAR 35.3
CC
71
Cov
76%
read_string
66.1
push
27.2
merge
8.6
CRAP 34.2
« SVGPDF/Element.pm ↑ Summary SVGPDF/Contrib/Bogen.pm »
1 #! perl
2  
3 8 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class SVGPDF::CSS;
9  
10 10 ▶field $css :accessor;
Subroutine css: called CC=2 SCAR=7.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $css = shift() 0 10 10
11 0 ▶field $errstr :accessor;
Subroutine errstr: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $errstr = shift() 0 0 0
12 12 ▶field $base :mutator;
Subroutine base: called CC=1 SCAR=0.0
Pod undocumented
13 2 ▶field $ctx :accessor;
Subroutine ctx: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $ctx = shift() 0 2 2
14 8 field @stack;
15 0 ▶field $ffam :accessor;
Subroutine ffam: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $ffam = shift() 0 0 0
16  
17 BUILD {
18 $css = {};
19 $base =
20 { 'font-family' => 'serif',
21 'font-size' => '10',
22 'color' => 'black',
23 'background-color' => 'none',
24 'fill' => 'currentColor',
25 'stroke' => 'none',
26 'line-width' => 1,
27 };
28 $ctx = {};
29 $ffam = [];
30 $self->push( @_ ) if @_;
31 }
32  
33 # Parse a string with one or more styles. Augments.
34 16 ▶method read_string ( $string ) {
Subroutine read_string: called CC=33 SCAR=41.9
Pod undocumented
35  
36 16 state $ffi = "face000"; # for unique font-face ids
37  
38 16 ▶ $css->{'*'} //= $base;
Condition66%2/3
$css->{"*"} //= $base
l!l&&r!l&&!r
1060
MC/DC0%0/2
$css->{"*"} //= $base
$css->{"*"} $base
Truth table66%2/3
A //= B
A= $css->{"*"}B= $base
ABresult
1X1
011
000
39  
40 # Flatten whitespace and remove /* comment */ style comments.
41 16 $string =~ s/\s+/ /g;
42 16 $string =~ s!/\*.*?\*\/!!g;
43  
44 # Hide semicolon in url(data:application/octet-stream;base64,...)
45 16 $string =~ s/(url\(['"]data:.*?\/.*?);(.*?),/$1\x{ff1b}$2,/g;
46  
47 # Split into styles.
48 16 foreach ( grep { /\S/ } split /(?<=\})/, $string ) {
49 31 ▶ unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*([^{]+?)\s*\{(.*)\}\s*$/u) 0 31 31
50 0 $errstr = "Invalid or unexpected style data '$_'";
51 0 return;
52 }
53  
54 # Split in such a way as to support grouped styles.
55 31 my $style = $1;
56 31 my $properties = $2;
57 31 $style =~ s/\s{2,}/ /g;
58 my @styles =
59 33 grep { s/\s+/ /g; 1; }
60 31 grep { /\S/ }
61 split( /\s*,\s*/, $style );
62 31 foreach ( @styles ) {
63 # Give @font-face rules an unique id.
64 33 ▶ if ( $_ eq '@font-face' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "\@font-face") 0 33 33
65 0 $_ = '@font-'.$ffi;
66 0 $ffi++;
67 }
68 33 ▶ $css->{$_} //= {};
Condition100%2/2
$css->{$_} //= {}
l!l
627
MC/DC100%1/1
$css->{$_} //= {}
$css->{$_}
Truth table100%2/2
A
A= $css->{$_}
Aresult
11
00
69 }
70  
71 # Split into properties.
72 31 foreach ( grep { /\S/ } split /\;/, $properties ) {
73 35 ▶ unless ( /^\s*(\*?[\w._-]+)\s*:\s*(.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*(\*?[\w._-]+)\s*:\s*(.*?)\s*$/u) 0 35 35
74 0 $errstr = "Invalid or unexpected property '$_' in style '$style'";
75 0 return;
76 }
77  
78 35 my $s = lc($1);
79 35 my %s = ( $s => $2 );
80  
81 # Split font shorthand.
82 35 ▶ if ( $s eq "font" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s eq 'font') { } 9 26 35
elsif ($s eq 'outline') { } 0 26 26
83 8 ▶ use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 9 my @spec = shellwords($s{$s});
85  
86 9 foreach my $spec ( @spec ) {
87 26 $spec =~ s/;$//;
88 26 ▶ if ( $spec =~ /^([.\d]+)px/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($spec =~ /^([.\d]+)px/u) { } 7 19 26
elsif ($spec eq 'bold') { } 2 17 19
elsif ($spec eq 'italic') { } 0 17 17
elsif ($spec eq 'bolditalic') { } 0 17 17
elsif ($spec =~ /^(?:text,)?serif$/iu) { } 2 15 17
elsif ($spec =~ /^(?:text,)?sans(?:-serif)?$/iu) { } 5 10 15
elsif ($spec =~ /^abc2svg(?:\.ttf)?$/iu) { } 2 8 10
elsif (lc($spec) =~ /^musejazz\s*text$/iu) { } 0 8 8
89 7 $s{'font-size'} = $1;
90 }
91 elsif ( $spec eq "bold" ) {
92 2 $s{'font-weight'} = "bold";
93 }
94 elsif ( $spec eq "italic" ) {
95 0 $s{'font-style'} = "italic";
96 }
97 elsif ( $spec eq "bolditalic" ) {
98 0 $s{'font-weight'} = "bold";
99 0 $s{'font-style'} = "italic";
100 }
101 elsif ( $spec =~ /^(?:text,)?serif$/i ) {
102 2 $s{'font-family'} = "serif";
103 }
104 elsif ( $spec =~ /^(?:text,)?sans(?:-serif)?$/i ) {
105 5 $s{'font-family'} = "sans";
106 }
107  
108 # These are for ABC SVG processing.
109 elsif ( $spec =~ /^abc2svg(?:\.ttf)?$/i ) {
110 2 $s{'font-family'} = "abc2svg";
111 }
112 elsif ( lc($spec) =~ /^musejazz\s*text$/i ) {
113 0 $s{'font-family'} = "musejazztext";
114 }
115 else {
116 8 $s{'font-family'} = $spec;
117 }
118 }
119  
120 # Remove the shorthand if we found something.
121 9 ▶ delete($s{$s}) if keys(%s) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %s > 1 9 0 9
122 }
123  
124 # Split outline shorthand.
125 elsif ( $s eq "outline" ) {
126 8 ▶ use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 0 my @spec = shellwords($s{$s});
128  
129 0 foreach my $spec ( @spec ) {
130 0 $spec =~ s/;$//;
131 0 ▶ if ( $spec =~ /^([.\d]+)px/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /^([.\d]+)px/u) { } 0 0 0
elsif ($spec =~ /^(dotted|dashed|solid|double|groove|ridge|inset|outset)$/iu) { } 0 0 0
132 0 $s{'outline-width'} = $1;
133 }
134 elsif ( $spec =~ /^(dotted|dashed|solid|double|groove|ridge|inset|outset)$/i ) {
135 0 $s{'outline-style'} = $1;
136 }
137 else {
138 0 $s{'outline-color'} = $spec;
139 }
140 }
141  
142 # Remove the shorthand if we found something.
143 0 ▶ delete($s{$s}) if keys(%s) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %s > 1 0 0 0
144 }
145  
146 35 foreach my $k ( keys %s ) {
147 44 foreach ( @styles ) {
148 48 $css->{$_}->{$k} = $s{$k};
149 }
150 }
151 }
152 }
153  
154 16 my @keys = keys( %$css );
155 16 for my $k ( @keys ) {
156 69 ▶ if ( $k =~ /^\@font-face/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\@font-face/u) 0 69 69
157 # Unhide semicolons.
158 0 s/\x{ff1b}/;/g for values( %{$css->{$k}} );
159 0 push( @$ffam, $css->{$k} );
160 0 delete $css->{$k};
161 }
162 }
163 16 for my $k ( @keys ) {
164 69 ▶ next unless $k =~ /^(\S+)\s+(\S+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(\S+)\s+(\S+)$/u 66 3 69
165 3 ▶ $css->{$1}->{" $2"} //= {};
Condition50%1/2
$css->{$1}{" $2"} //= {}
l!l
03
MC/DC0%0/1
$css->{$1}{" $2"} //= {}
$css->{$1}{" $2"}
Truth table50%1/2
A
A= $css->{$1}{" $2"}
Aresult
11
00
166 3 $self->merge( $css->{$1}->{" $2"}, $css->{$k} );
167 3 delete ( $css->{$k} );
168 }
169  
170 16 1;
171 8 }
172  
173 # Merge hashes (and only hashes), recursive.
174 772 ▶method merge ( $left, $right ) {
Subroutine merge: called CC=8 SCAR=21.6
Pod undocumented
175 772 ▶ return unless defined $right;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $right 0 772 772
176 772 ▶ if ( ref($left) eq 'HASH' && ref($right) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left eq "HASH" and ref $right eq "HASH") 772 0 772
Condition33%1/3
ref $left eq "HASH" and ref $right eq "HASH"
!ll&&!rl&&r
00772
MC/DC0%0/2
ref $left eq "HASH" and ref $right eq "HASH"
ref $left eq "HASH" ref $right eq "HASH"
Truth table33%1/3
A and B
A= ref $left eq "HASH"B= ref $right eq "HASH"
ABresult
0X0
100
111
177 772 for ( keys %$right ) {
178 4293 ▶ if ( exists $left->{$_}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $left->{$_} and ref $left->{$_} eq 'HASH' and ref $right->{$_} eq 'HASH') { } 226 4067 4293
Condition83%5/6
exists $left->{$_} and ref $left->{$_} eq "HASH" and ref $right->{$_} eq "HASH"
!ll&&!rl&&r
40670226
exists $left->{$_} and ref $left->{$_} eq "HASH"
!ll&&!rl&&r
7193348226
MC/DC66%2/3
exists $left->{$_} and ref $left->{$_} eq "HASH" and ref $right->{$_} eq "HASH"
exists $left->{$_} ref $left->{$_} eq "HASH" ref $right->{$_} eq "HASH"
Truth table75%3/4
A and B and C
A= exists $left->{$_}B= ref $left->{$_} eq "HASH"C= ref $right->{$_} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
179 && ref($left->{$_}) eq 'HASH'
180 && ref($right->{$_}) eq 'HASH' ) {
181 226 $self->merge( $left->{$_}, $right->{$_} );
182 }
183 else {
184 4067 $left->{$_} = $right->{$_};
185 }
186 }
187 772 return;
188 }
189 0 croak("Cannot merge " . ref($left) . " and " . ref($right));
190 8 }
191  
192 80 ▶method find ( $arg ) {
Subroutine find: called CC=3 SCAR=11.0
Pod undocumented
193 80 ▶ $css->{'*'} //= $base;
Condition33%1/3
$css->{"*"} //= $base
l!l&&r!l&&!r
8000
MC/DC0%0/2
$css->{"*"} //= $base
$css->{"*"} $base
Truth table33%1/3
A //= B
A= $css->{"*"}B= $base
ABresult
1X1
011
000
194 80 my $ret = { %{$css->{'*'}} };
195 80 ▶ if ( exists( $css->{_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $css->{"_"}) 78 2 80
196 78 $self->merge( $ret, $css->{_} );
197 }
198 80 $ctx = $ret;
199 80 $ret->{$arg};
200 8 }
201  
202 221 ▶method push ( @args ) {
Subroutine push: called CC=25 SCAR=33.0
Pod undocumented
203 221 ▶ my $args = ref($args[0]) eq 'HASH' ? $args[0] : { @args };
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 1 220 221
204 221 ▶ $css->{'*'} //= $base;
Condition66%2/3
$css->{"*"} //= $base
l!l&&r!l&&!r
189320
MC/DC0%0/2
$css->{"*"} //= $base
$css->{"*"} $base
Truth table66%2/3
A //= B
A= $css->{"*"}B= $base
ABresult
1X1
011
000
205 221 my $ret;
206  
207 # CSS defaults.
208 221 ▶ while ( my($k,$v) = each %{$css->{'*'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$css{"*"};}) 1547 221 1768
209 1547 ▶ $ret->{$k} //= $v;
Condition33%1/3
$ret->{$k} //= $v
l!l&&r!l&&!r
015470
MC/DC0%0/2
$ret->{$k} //= $v
$ret->{$k} $v
Truth table33%1/3
A //= B
A= $ret->{$k}B= $v
ABresult
1X1
011
000
210 }
211  
212 ## Parent.
213 221 ▶ if ( exists( $css->{_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $css->{"_"}) 183 38 221
214 183 $self->merge( $ret, $css->{_} );
215 }
216  
217 ## Presentation attributes.
218 221 for ( keys %$args ) {
219 494 ▶ next if /^(element|class|style|id)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(element|class|style|id)$/u 258 236 494
220 236 $ret->{$_} = $args->{$_};
221 }
222  
223 ## Tag style.
224 221 ▶ if ( $args->{element} && exists( $css->{$args->{element}} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"element"} and exists $$css{$args->{"element"}}) 6 215 221
Condition100%3/3
$args->{"element"} and exists $$css{$args->{"element"}}
!ll&&!rl&&r
32126
MC/DC100%2/2
$args->{"element"} and exists $$css{$args->{"element"}}
$args->{"element"} exists $$css{$args->{"element"}}
Truth table100%3/3
A and B
A= $args->{"element"}B= exists $$css{$args->{"element"}}
ABresult
0X0
100
111
225 6 $self->merge( $ret, $css->{$args->{element}} );
226 }
227 221 ▶ if ( $args->{element} && exists( $css->{_}->{" ".$args->{element}} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"element"} and exists $css->{"_"}{' ' . $args->{"element"}}) 3 218 221
Condition100%3/3
$args->{"element"} and exists $css->{"_"}{' ' . $args->{"element"}}
!ll&&!rl&&r
32153
MC/DC100%2/2
$args->{"element"} and exists $css->{"_"}{' ' . $args->{"element"}}
$args->{"element"} exists $css->{"_"}{' ' . $args->{"element"}}
Truth table100%3/3
A and B
A= $args->{"element"}B= exists $css->{"_"}{' ' . $args->{"element"}}
ABresult
0X0
100
111
228 3 $self->merge( $ret, $css->{_}->{" ".$args->{element}} );
229 }
230  
231 ## Class style.
232 221 ▶ if ( $args->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"class"}) 21 200 221
233 21 for ( split( ' ', $args->{class} ), "svg" ) {
234 $self->merge( $ret, $css->{".$_"} )
235 68 ▶ if exists( $css->{".$_"} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $$css{".$_"} 44 24 68
236 $self->merge( $ret, $css->{$args->{element}.".$_"} )
237 68 ▶ if $args->{element} && exists( $css->{$args->{element}.".$_"} );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"element"} and exists $$css{$args->{"element"} . ".$_"} 0 68 68
Condition66%2/3
$args->{"element"} and exists $$css{$args->{"element"} . ".$_"}
!ll&&!rl&&r
8600
MC/DC0%0/2
$args->{"element"} and exists $$css{$args->{"element"} . ".$_"}
$args->{"element"} exists $$css{$args->{"element"} . ".$_"}
Truth table66%2/3
A and B
A= $args->{"element"}B= exists $$css{$args->{"element"} . ".$_"}
ABresult
0X0
100
111
238 }
239 }
240  
241 ## ID (generic).
242 221 ▶ if ( $args->{id} && exists( $css->{ "#" . $args->{id} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"id"} and exists $$css{'#' . $args->{"id"}}) 0 221 221
Condition66%2/3
$args->{"id"} and exists $$css{'#' . $args->{"id"}}
!ll&&!rl&&r
210110
MC/DC0%0/2
$args->{"id"} and exists $$css{'#' . $args->{"id"}}
$args->{"id"} exists $$css{'#' . $args->{"id"}}
Truth table66%2/3
A and B
A= $args->{"id"}B= exists $$css{'#' . $args->{"id"}}
ABresult
0X0
100
111
243 0 $self->merge( $ret, $css->{ "#" . $args->{id} } );
244 }
245  
246 ## ID (specific).
247 221 ▶ if ( $args->{id} && exists( $css->{ $args->{element} . "#" . $args->{id} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"id"} and exists $$css{$args->{"element"} . '#' . $args->{"id"}}) 0 221 221
Condition66%2/3
$args->{"id"} and exists $$css{$args->{"element"} . '#' . $args->{"id"}}
!ll&&!rl&&r
210110
MC/DC0%0/2
$args->{"id"} and exists $$css{$args->{"element"} . '#' . $args->{"id"}}
$args->{"id"} exists $$css{$args->{"element"} . '#' . $args->{"id"}}
Truth table66%2/3
A and B
A= $args->{"id"}B= exists $$css{$args->{"element"} . '#' . $args->{"id"}}
ABresult
0X0
100
111
248 0 $self->merge( $ret, $css->{ $args->{element} . "#" . $args->{id} } );
249 }
250  
251 ## Style attribute.
252 221 ▶ if ( $args->{style} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"style"}) 8 213 221
253 8 ▶ $self->read_string( "__ {" . $args->{style} . "}" )
Branch: 1 branch
BranchTrueFalseTotal
$self->read_string('__ {' . $args->{"style"} . '}') or croak($errstr) 8 0 8
254 or croak($errstr);
255 8 $self->merge( $ret, delete $css->{__} );
256 }
257  
258 221 ▶ $ret->{'@font-face'} = $ffam if $ffam;
Branch: 1 branch
BranchTrueFalseTotal
if $ffam 221 0 221
259 221 ▶ push( @stack, { %{$css->{_}//{}} } );
Condition100%2/2
$css->{"_"} // {}
l!l
2192
MC/DC100%1/1
$css->{"_"} // {}
$css->{"_"}
Truth table100%2/2
A
A= $css->{"_"}
Aresult
11
00
260 221 $self->merge( $css, { _ => $ret } );
261 221 $ctx = $ret;
262 8 }
263  
264 215 ▶method pop () {
Subroutine pop: called CC=2 SCAR=7.0
Pod undocumented
265 215 ▶ Carp::croak("CSS stack underflow") unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 0 215 215
266 215 $ctx = $css->{_} = pop(@stack);
267 8 }
268  
269 0 ▶method level () {
Subroutine level: not called CC=1 SCAR=6.9
Pod undocumented
270 0 0+@stack;
271 8 }
272  
273 8 1;
« SVGPDF/Element.pm ↑ Summary SVGPDF/Contrib/Bogen.pm »