blib/lib/Svg/Simple.pm

filter: Statementstmt 64.0% 82 / 128 Branchbran 55.8% 19 / 34 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 3 totaltotal 59.5% 115 / 193 CC 27 SCAR 26.3
CC
27
Cov
61%
gridLines
30.0
AUTOLOAD
16.9
print
11.1
CRAP 13.8
↑ Summary Build »
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/
2 #-------------------------------------------------------------------------------
3 # Write SVG using Perl syntax.
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc., 2017-2020
5 #-------------------------------------------------------------------------------
6 # podDocumentation
7 package Svg::Simple;
8 1 require v5.34;
9 1 our $VERSION = 20231118;
10 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 makeDieConfess;
17  
18 #D1 Constructors # Construct and print a new L<svg> object.
19  
20 sub new(%) # Create a new L<svg> object.
21 1 ▶ {my (%options) = @_; # Svg options
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 genHash(__PACKAGE__,
23 code => [], # Svg code generated
24 mX => 0, # Maximum X coordinate encountered
25 mY => 0, # Maximum Y coordinate encountered
26 defaults => $options{defaults}, # Default attributes to be applied to each statement
27 grid => $options{grid}, # Grid of specified size requested if non zero
28 1 z => {0=>1}, # Values of z encountered: the elements will be drawn in passes in Z order.
29 );
30 }
31  
32 sub gridLines($$$$) # Draw a grid.
33 0 ▶ {my ($svg, $x, $y, $g) = @_; # Svg, maximum X, maximum Y, grid square size
Subroutine gridLines: not called CC=5 SCAR=34.0
Pod undocumented
34 0 my @s;
35 0 my $X = int($x / $g); my $Y = int($y / $g); # Steps in X and Y
36 0 my $f = $g / 4; # Font size
37 0 my $w = $f / 16; # Line width
38 0 my @w = (opacity=>0.2, font_size=>$f, stroke_width=>$w, stroke=>"black", # Font for grid
39 text_anchor => "start", dominant_baseline => "hanging");
40 0 my @f = (@w, opacity=>1, fill=>'black');
41  
42 0 for my $i(0..$X) # X lines
43 0 {my $c = $i*$g;
44 0 $svg->line(x1=>$c, x2=>$c, y1=>0, y2=>$y, @w);
45 0 ▶ $svg->text(@f, x => $c, y => 0, cdata => $i) unless $i == $X;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $X 0 0 0
46 }
47  
48 0 for my $i(0..$Y) # Y lines
49 0 {my $c = $i*$g;
50 0 $svg->line(y1=>$c, y2=>$c, x1=>0, x2=>$x, @w);
51 0 ▶ $svg->text(@f, x => 0, y => $c, cdata => $i) unless $i == $Y;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $Y 0 0 0
52 }
53 }
54  
55 sub print($%) # Print resulting L<svg> string.
56 2 ▶ {my ($svg, %options) = @_; # Svg, svg options
Subroutine print: called CC=9 SCAR=24.0
Pod undocumented
57 2 ▶ my $X = $options{width} // $svg->mX; # Maximum width
Condition33%1/3
$options{"width"} // $svg->mX
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{"width"} // $svg->mX
$options{"width"} $svg->mX
Truth table33%1/3
A // B
A= $options{"width"}B= $svg->mX
ABresult
1X1
011
000
58 2 ▶ my $Y = $options{height} // $svg->mY; # Maximum height
Condition33%1/3
$options{"height"} // $svg->mY
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{"height"} // $svg->mY
$options{"height"} $svg->mY
Truth table33%1/3
A // B
A= $options{"height"}B= $svg->mY
ABresult
1X1
011
000
59 2 ▶ my $g = $svg->grid ? $svg->gridLines($X, $Y, $svg->grid) : ''; # Draw a grid if requested
Branch: 1 branch
BranchTrueFalseTotal
$svg->grid ? : 0 2 2
60 2 my $e = q(</svg>);
61  
62 2 my @C = $svg->code->@*; # Elements
63 2 my @c; # Elements reordered by z index
64 2 for my $Z(sort {$a <=> $b} keys $svg->z->%*) # Reorder elements by z from low to high
65 2 {for my $C(@C) # Scan svg elements
66 4 {my ($c, $z) = @$C; # Element, z order
67 4 ▶ if ($z == $Z) # Matching z order
Branch: 1 branch
BranchTrueFalseTotal
if ($z == $Z) 4 0 4
68 4 {push @c, $c;
69 }
70 }
71 }
72  
73 2 my $s = join "\n", @c;
74  
75 2 my $S = <<"END";
76 <svg height="100%" viewBox="0 0 $X $Y" width="100%" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
77 $g
78 $s
79 $e
80 END
81  
82 2 my $H = <<"END";
83 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
84 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
85 END
86  
87 2 ▶ my $t = $options{inline} ? $S : $H.$S; # Write without headers for inline usage
Branch: 1 branch
BranchTrueFalseTotal
$options{'inline'} ? : 0 2 2
88  
89 2 ▶ if (my $f = $options{svg}) # Write to file
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $options{"svg"}) 0 2 2
90 0 {owf(fpe($f, q(svg)), $t)
91 }
92 $t
93 2 }
94  
95 1 our $AUTOLOAD; # The method to be autoloaded appears here
96  
97 sub AUTOLOAD($%) #P L<svg> methods.
98 3 ▶ {my ($svg, %options) = @_; # Svg object, options
Subroutine AUTOLOAD: called CC=15 SCAR=28.3
99 3 my @s;
100  
101 my %o;
102 3 ▶ %o = ($svg->defaults->%*) if $svg->defaults; # Add any default values
Branch: 1 branch
BranchTrueFalseTotal
if $svg->defaults 0 3 3
103 3 %o = (%o, %options); # Add supplied options
104  
105 3 for my $k(sort keys %o) # Process each option
106 13 {my $v = $o{$k};
107 13 my $K = $k =~ s(_) (-)r; # Underscore _ in option names becomes hyphen -
108 13 ▶ next if $k =~ m(\Acdata\Z)i;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\Acdata\Z/i 1 12 13
109 12 push @s, qq($K="$v");
110 }
111  
112 3 my $n = $AUTOLOAD =~ s(\A.*::) ()r; # Name of element
113  
114 eval # Maximum extent of the Svg
115 3 {my $X = $svg->mY;
116 3 my $Y = $svg->mY;
117 3 ▶ my $w = $options{stroke} ? $options{stroke_width} // $options{"stroke-width"} // 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options{'stroke'} ? : 1 2 3
Condition40%2/5
($options{"stroke_width"} // $options{"stroke-width"}) // 1
l!l
01
$options{"stroke_width"} // $options{"stroke-width"}
l!l&&r!l&&!r
001
MC/DC0%0/2
($options{"stroke_width"} // $options{"stroke-width"}) // 1
$options{"stroke_width"} $options{"stroke-width"}
Truth table33%1/3
A // B
A= $options{"stroke_width"}B= $options{"stroke-width"}
ABresult
1X1
011
000
118 3 ▶ if ($n =~ m(\Acircle\Z)i)
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\Acircle\Z/i) 1 2 3
119 1 {$X = max $X, $w + $options{cx}+$options{r};
120 1 $Y = max $Y, $w + $options{cy}+$options{r};
121 }
122 3 ▶ if ($n =~ m(\Aline\Z)i)
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\Aline\Z/i) 0 3 3
123 0 {$X = max $X, $w + $options{$_} for qw(x1 x2);
124 0 $Y = max $Y, $w + $options{$_} for qw(y1 y2);
125 }
126 3 ▶ if ($n =~ m(\Arect\Z)i)
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\Arect\Z/i) 0 3 3
127 0 {$X = max $X, $w + $options{x}+$options{width};
128 0 $Y = max $Y, $w + $options{y}+$options{height};
129 }
130 3 ▶ if ($n =~ m(\Atext\Z)i)
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /\Atext\Z/i) 1 2 3
131 1 {$X = max $X, $options{x} + $w * length($options{cdata});
132 1 $Y = max $Y, $options{y};
133 }
134 3 $svg->mX = max $svg->mX, $X;
135 3 $svg->mY = max $svg->mY, $Y;
136 };
137  
138 3 my $z = 0; # Default z order
139 3 ▶ if (defined(my $Z = $options{z})) # Override Z order
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $Z = $options{"z"})) 0 3 3
140 0 {$svg->z->{$z = $Z}++;
141 }
142  
143 3 my $p = join " ", @s; # Options
144 3 ▶ if (defined(my $t = $options{cdata}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $t = $options{'cdata'})) { } 1 2 3
145 1 {push $svg->code->@*, ["<$n $p>$t</$n>", $z] # Internal text
146 }
147 else
148 2 {push $svg->code->@*, ["<$n $p/>", $z] # No internal text
149 }
150 3 $svg
151 }
152  
153 #D0
154 #-------------------------------------------------------------------------------
155 # Export - eeee
156 #-------------------------------------------------------------------------------
157  
158 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 # containingFolder
163  
164 1 @ISA = qw(Exporter);
165 1 @EXPORT = qw();
166 1 @EXPORT_OK = qw();
167 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
168  
169 # podDocumentation
170  
171 =pod
172  
173 =encoding utf-8
174  
175 =for html <p><a href="https://github.com/philiprbrenan/SvgSimple"><img src="https://github.com/philiprbrenan/SvgSimple/workflows/Test/badge.svg"></a>
176  
177 =head1 Name
178  
179 Svg::Simple - Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax.
180  
181 =head1 Synopsis
182  
183 Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax as in:
184  
185 my $s = Svg::Simple::new();
186  
187 $s->text(x=>10, y=>10,
188 cdata =>"Hello World",
189 text_anchor =>"middle",
190 alignment_baseline=>"middle",
191 font_size => 3.6,
192 font_family =>"Arial",
193 fill =>"black");
194  
195 $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);
196  
197 say STDERR $s->print;
198  
199 =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/test.svg">
200  
201 A B<-> in an L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics>
202 keyword can be replaced with B<_> to reduce line noise.
203  
204 The L<print> method automatically creates an
205 L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> to wrap around
206 all the L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics>
207 statements specified. The image so created will fill all of the available
208 space in the browser if the image is shown by itself, else it will fill all of
209 the available space in the parent tag containing the
210 L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> statements if the
211 L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> is inlined in
212 L<html> .
213  
214 This package automatically tracks the dimensions of the objects specified in
215 the L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> statements
216 and creates a viewport wide enough and high enough to display them fully in
217 whatever space the browser allocates to the
218 L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> image.
219  
220 If you wish to set these dimensions yourself, call the L<print> method with
221 overriding values as in:
222  
223 say STDERR $s->print(width=>2000, height=>1000);
224  
225 If you wish to inline the generated L<html|https://en.wikipedia.org/wiki/HTML>
226 you should remove the first two lines of the generated code using a regular
227 expression to remove the superfluous L<xml|https://en.wikipedia.org/wiki/XML>
228 headers.
229  
230 =head1 Description
231  
232 Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax.
233  
234  
235 Version 20231118.
236  
237  
238 The following sections describe the methods in each functional area of this
239 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
240  
241  
242  
243 =head1 Constructors
244  
245 Construct and print a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object.
246  
247 =head2 newย (%options)
248  
249 Create a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object.
250  
251 Parameter Description
252 1 %options Svg options
253  
254 B<Example:>
255  
256  
257 if (1)
258  
259 {my $s = Svg::Simple::new(); # ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ
260  
261  
262 $s->text(x=>10, y=>10,
263 cdata =>"Hello World",
264 text_anchor =>"middle",
265 alignment_baseline=>"middle",
266 font_size => 3.6,
267 font_family =>"Arial",
268 fill =>"black");
269  
270 $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);
271  
272 my $t = $s->print(svg=>q(svg/new)); # ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ
273  
274 ok($t =~ m(circle));
275 }
276  
277  
278 =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/new.svg">
279  
280  
281 =head2 gridLinesย ย ย ($svg, $x, $y, $g)
282  
283 Draw a grid.
284  
285 Parameter Description
286 1 $svg Svg
287 2 $x Maximum X
288 3 $y Maximum Y
289 4 $g Grid square size
290  
291 B<Example:>
292  
293  
294 if (1)
295 {my $s = Svg::Simple::new(grid=>10);
296 $s->rect(x=>10, y=>10, width=>40, height=>30, stroke=>"blue", fill=>'transparent');
297 my $t = $s->print(svg=>q(svg/grid));
298 is_deeply(scalar(split /line/, $t), 32);
299 }
300  
301  
302 =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/grid.svg">
303  
304  
305 =head2 printย ย ย ($svg, %options)
306  
307 Print resulting L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> string.
308  
309 Parameter Description
310 1 $svg Svg
311 2 %options Svg options
312  
313 B<Example:>
314  
315  
316 if (1)
317 {my $s = Svg::Simple::new();
318  
319 my @d = (width=>8, height=>8, stroke=>"blue", fill=>"transparent"); # Default values
320 $s->rect(x=>1, y=>1, z=>1, @d, stroke=>"blue"); # Defined earlier but drawn above because of z order
321 $s->rect(x=>4, y=>4, z=>0, @d, stroke=>"red");
322  
323 my $t = $s->print(svg=>q(svg/rect)); # ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ
324  
325 is_deeply(scalar(split /rect/, $t), 3);
326 }
327  
328  
329 =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/rect.svg">
330  
331  
332  
333 =head1 Private Methods
334  
335 =head2 AUTOLOAD($svg, %options)
336  
337 L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> methods.
338  
339 Parameter Description
340 1 $svg Svg object
341 2 %options Options
342  
343  
344 =head1 Index
345  
346  
347 1 L<AUTOLOAD|/AUTOLOAD> - L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> methods.
348  
349 2 L<gridLines|/gridLines> - Draw a grid.
350  
351 3 L<new|/new> - Create a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object.
352  
353 4 L<print|/print> - Print resulting L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> string.
354  
355 =head1 Installation
356  
357 This module is written in 100% Pure Perl and, thus, it is easy to read,
358 comprehend, use, modify and install via B<cpan>:
359  
360 sudo cpan install Svg::Simple
361  
362 =head1 Author
363  
364 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
365  
366 L<http://www.appaapps.com|http://www.appaapps.com>
367  
368 =head1 Copyright
369  
370 Copyright (c) 2016-2023 Philip R Brenan.
371  
372 This module is free software. It may be used, redistributed and/or modified
373 under the same terms as Perl itself.
374  
375 =cut
376  
377  
378  
379 #D0 Tests # Tests and examples
380 1 ▶goto finish if caller; # Skip testing if we are being called as a module
Branch: 1 branch
BranchTrueFalseTotal
if caller 1 0 1
381 0 eval "use Test::More qw(no_plan)";
382 0 ▶eval "Test::More->builder->output('/dev/null')" if -e q(/home/phil/);
Branch: 1 branch
BranchTrueFalseTotal
if -e "/home/phil/" 0 0 0
383 0 eval {goto latest};
384  
385 #Svg https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/
386  
387 0 if (1) #Tnew
388 0 {my $s = Svg::Simple::new();
389  
390 0 $s->text(x=>10, y=>10,
391 cdata =>"Hello World",
392 text_anchor =>"middle",
393 alignment_baseline=>"middle",
394 font_size => 3.6,
395 font_family =>"Arial",
396 fill =>"black");
397  
398 0 $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);
399 0 my $t = $s->print(svg=>q(svg/new));
400 0 ok($t =~ m(circle));
401 }
402  
403 0 if (1) #TgridLines
404 0 {my $s = Svg::Simple::new(grid=>10);
405 0 $s->rect(x=>10, y=>10, width=>40, height=>30, stroke=>"blue", fill=>'transparent');
406 0 my $t = $s->print(svg=>q(svg/grid));
407 0 is_deeply(scalar(split /line/, $t), 32);
408 }
409  
410 0 if (1) #Tprint
411 0 {my $s = Svg::Simple::new();
412  
413 0 my @d = (width=>8, height=>8, stroke=>"blue", fill=>"transparent"); # Default values
414 0 $s->rect(x=>1, y=>1, z=>1, @d, stroke=>"blue"); # Defined earlier but drawn above because of z order
415 0 $s->rect(x=>4, y=>4, z=>0, @d, stroke=>"red");
416 0 my $t = $s->print(svg=>q(svg/rect));
417 0 is_deeply(scalar(split /rect/, $t), 3);
418 }
419  
420 0 done_testing();
421 1 finish: 1;
↑ Summary Build »