| 1 | #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/ -I/home/phil/perl/cpan/Math-Intersection-Circle-Line/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 | require v5.34; | ||
| 9 | our $VERSION = 20260909; | ||
| 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 | 0 | ▶ | use Math::Intersection::Circle::Line; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use utf8; | ||
| 17 | |||
| 18 | makeDieConfess; | ||
| 19 | |||
| 20 | #D1 Constructors # Construct and print a new L<svg> object. | ||
| 21 | |||
| 22 | sub new(%) # Create a new L<svg> object. | ||
| 23 | {my (%options) = @_; # Svg options | ||
| 24 | genHash(__PACKAGE__, | ||
| 25 | code => [], # Svg code generated | ||
| 26 | mX => 0, # Maximum X coordinate encountered | ||
| 27 | mY => 0, # Maximum Y coordinate encountered | ||
| 28 | defaults => $options{defaults}, # Default attributes to be applied to each statement | ||
| 29 | grid => $options{grid}, # Grid of specified size requested if non zero | ||
| 30 | z => {0=>1}, # Values of z encountered: the elements will be drawn in passes in Z order. | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub gridLines($$$$) # Draw a grid. | ||
| 35 | {my ($svg, $x, $y, $g) = @_; # Svg, maximum X, maximum Y, grid square size | ||
| 36 | @_ == 4 or confess "Four parameters"; | ||
| 37 | my @s; | ||
| 38 | my $X = int($x / $g); my $Y = int($y / $g); # Steps in X and Y | ||
| 39 | my $f = $g / 4; # Font size | ||
| 40 | my $w = $f / 16; # Line width | ||
| 41 | my @w = (opacity=>0.2, font_size=>$f, stroke_width=>$w, stroke=>"black", # Font for grid | ||
| 42 | text_anchor => "start", dominant_baseline => "hanging"); | ||
| 43 | my @f = (@w, opacity=>1, fill=>'black'); | ||
| 44 | |||
| 45 | for my $i(0..$X) # X lines | ||
| 46 | {my $c = $i*$g; | ||
| 47 | $svg->line(x1=>$c, x2=>$c, y1=>0, y2=>$y, @w); | ||
| 48 | $svg->text(@f, x => $c, y => 0, cdata => $i) unless $i == $X; | ||
| 49 | } | ||
| 50 | |||
| 51 | for my $i(0..$Y) # Y lines | ||
| 52 | {my $c = $i*$g; | ||
| 53 | $svg->line(y1=>$c, y2=>$c, x1=>0, x2=>$x, @w); | ||
| 54 | $svg->text(@f, x => 0, y => $c, cdata => $i) unless $i == $Y; | ||
| 55 | } | ||
| 56 | |||
| 57 | for my $i(1..$X) # X lines | ||
| 58 | {my $x = $i*$g; | ||
| 59 | for my $j(1..$Y) # Y lines | ||
| 60 | {my $y = $j*$g; | ||
| 61 | if ($i % 10 == 0 or $j % 10 == 0) | ||
| 62 | {$svg->text(@f, x => $x, y => $y, cdata => "$i.$j"); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub print($%) # Print resulting L<svg> string. | ||
| 69 | {my ($svg, %options) = @_; # Svg, svg options | ||
| 70 | my $X = $options{x} // 0; # Maximum width | ||
| 71 | my $Y = $options{y} // 0; # Maximum height | ||
| 72 | my $W = $options{width} // $svg->mX; # Maximum width | ||
| 73 | my $H = $options{height} // $svg->mY; # Maximum height | ||
| 74 | my $I = $options{inline}; # Do not headers | ||
| 75 | my $F = $options{svg}; # Optional file to write to | ||
| 76 | my $g = $svg->grid ? $svg->gridLines($W, $H, $svg->grid) : ''; # Draw a grid if requested | ||
| 77 | my $e = q(</svg>); | ||
| 78 | |||
| 79 | my @C = $svg->code->@*; # Elements | ||
| 80 | my @c; # Elements reordered by z index | ||
| 81 | for my $Z(sort {$a <=> $b} keys $svg->z->%*) # Reorder elements by z from low to high | ||
| 82 | {for my $C(@C) # Scan svg elements | ||
| 83 | {my ($c, $z) = @$C; # Element, z order | ||
| 84 | if ($z == $Z) # Matching z order | ||
| 85 | {push @c, $c; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | my $s = join "\n", @c; | ||
| 91 | |||
| 92 | my $S = <<"END"; | ||
| 93 | <svg height="100%" viewBox="$X $Y $W $H" width="100%" background-color="none" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
| 94 | $g | ||
| 95 | $s | ||
| 96 | $e | ||
| 97 | END | ||
| 98 | |||
| 99 | my $x = <<"END"; # XML header | ||
| 100 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| 101 | <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> | ||
| 102 | END | ||
| 103 | |||
| 104 | my $t = $I ? $S : $x.$S; # Write without headers for inline usage | ||
| 105 | writeFile($F, $t) if $F; # Write to file | ||
| 106 | $t | ||
| 107 | } | ||
| 108 | |||
| 109 | our $AUTOLOAD; # The method to be autoloaded appears here | ||
| 110 | |||
| 111 | sub AUTOLOAD($%) #P L<svg> methods. | ||
| 112 | {my ($svg, %options) = @_; # Svg object, options | ||
| 113 | my @s; | ||
| 114 | |||
| 115 | my %o; | ||
| 116 | %o = ($svg->defaults->%*) if $svg->defaults; # Add any default values | ||
| 117 | %o = (%o, %options); # Add supplied options | ||
| 118 | |||
| 119 | for my $k(sort keys %o) # Process each option | ||
| 120 | {my $v = $o{$k}; | ||
| 121 | my $K = $k =~ s(_) (-)r; # Underscore _ in option names becomes hyphen - | ||
| 122 | next if $k =~ m(\Acdata\Z)i; # Skip text keyword as the text will be placed inline | ||
| 123 | next if $k =~ m(\Asub\Z)i; # Skip sub keyword as the subroutine will be called and its content placed inside this tag | ||
| 124 | push @s, qq($K="$v"); | ||
| 125 | } | ||
| 126 | |||
| 127 | my $n = $AUTOLOAD =~ s(\A.*::) ()r; # Name of element | ||
| 128 | |||
| 129 | eval # Maximum extent of the Svg | ||
| 130 | {my $X = $svg->mX; | ||
| 131 | my $Y = $svg->mY; | ||
| 132 | my $w = $options{stroke} ? $options{stroke_width} // $options{"stroke-width"} // 1 : 0; | ||
| 133 | |||
| 134 | my sub option($) {$options{$_[0]} // 0} # Get an option or default to zero if not present. This avoids the problem of validating the SVG parameters which the browser will do more effectively. | ||
| 135 | |||
| 136 | if ($n =~ m(\Acircle\Z)i) | ||
| 137 | {$X = max $X, $w + option("cx")+option("r"); | ||
| 138 | $Y = max $Y, $w + option("cy")+option("r"); | ||
| 139 | } | ||
| 140 | if ($n =~ m(\Aline\Z)i) # Lines | ||
| 141 | {$X = max $X, $w + option("$_") for qw(x1 x2); | ||
| 142 | $Y = max $Y, $w + option("$_") for qw(y1 y2); | ||
| 143 | } | ||
| 144 | if ($n =~ m(\Arect\Z)i) # Rectangles | ||
| 145 | {$X = max $X, $w + option("x")+option("width"); | ||
| 146 | $Y = max $Y, $w + option("y")+option("height"); | ||
| 147 | } | ||
| 148 | if ($n =~ m(\Atext\Z)i) | ||
| 149 | {$X = max $X, option("x") + $w * length(option("cdata")); | ||
| 150 | $Y = max $Y, option("y"); | ||
| 151 | } | ||
| 152 | $svg->mX = max $svg->mX, $X; | ||
| 153 | $svg->mY = max $svg->mY, $Y; | ||
| 154 | }; | ||
| 155 | if ($@) # Failed to evaluate the preceding code | ||
| 156 | {say STDERR $@; | ||
| 157 | exit; | ||
| 158 | } | ||
| 159 | |||
| 160 | my $z = 0; # Default z order | ||
| 161 | if (defined(my $Z = $options{z})) # Override Z order | ||
| 162 | {$svg->z->{$z = $Z}++; | ||
| 163 | } | ||
| 164 | |||
| 165 | my $p = join " ", @s; # Options | ||
| 166 | if (defined(my $t = $options{cdata})) | ||
| 167 | {push $svg->code->@*, ["<$n $p>$t</$n>", $z] # Internal text | ||
| 168 | } | ||
| 169 | elsif (defined(my $s = $options{sub})) # Group or other command that brackets statements | ||
| 170 | {push $svg->code->@*, ["<$n $p>", $z]; # Opening xml | ||
| 171 | &$s($svg); # Internal xml | ||
| 172 | push $svg->code->@*, ["</$n>", $z] # Closing xml | ||
| 173 | } | ||
| 174 | else | ||
| 175 | {push $svg->code->@*, ["<$n $p/>", $z] # No internal text | ||
| 176 | } | ||
| 177 | $svg | ||
| 178 | } | ||
| 179 | |||
| 180 | #D1 Utility functions # Extra features to make using Svg easier | ||
| 181 | |||
| 182 | sub arcPath($$$$$$$$) # Arc through three points along the circumference of a circle from the first point through the middle point to the last point | ||
| 183 | {my ($svg, $N, $x1, $y1, $x2, $y2, $x3, $y3) = @_; # Svg, number of points on path, start x, start y, middle x, middle y, end x, end y | ||
| 184 | @_ == 8 or confess "Eight parameters"; | ||
| 185 | |||
| 186 | Math::Intersection::Circle::Line::circumCircle # Draw a circle clockwise through the three points | ||
| 187 | {my ($cx, $cy) = @_; | ||
| 188 | my $r = Math::Intersection::Circle::Line::vectorLength($x1, $y1, $cx, $cy); # Radius of circle | ||
| 189 | |||
| 190 | my sub inter($$) # Point on the circumference of a circle where the line drawn through the specified point and the center of the circle intersect the circle closest to the specified point | ||
| 191 | {my ($x, $y) = @_; # Options | ||
| 192 | |||
| 193 | Math::Intersection::Circle::Line::intersectionCircleLine | ||
| 194 | {my ($x1, $y1, $x2, $y2) = @_; # Intersection points | ||
| 195 | my $l1 = Math::Intersection::Circle::Line::vectorLength($x,$y, $x1,$y1); # One point on the circumference | ||
| 196 | my $l2 = Math::Intersection::Circle::Line::vectorLength($x,$y, $x2,$y2); # The opposite point | ||
| 197 | ($x1, $y1) = ($x2, $y2) if $l2 < $l1; # The point on the circumference closest to the point being mapped | ||
| 198 | ($x1, $y1) # Return closest intersection point | ||
| 199 | } $cx, $cy, $r, $cx, $cy, $x, $y; | ||
| 200 | } | ||
| 201 | |||
| 202 | my @p; # Path | ||
| 203 | for my $i(0..$N) # Step along first line segment mapping it on to the nearest part of the circle | ||
| 204 | {my $dx = ($x2 - $x1) * $i / $N; | ||
| 205 | my $dy = ($y2 - $y1) * $i / $N; | ||
| 206 | push @p, join " ", "L", inter $x1+$dx, $y1+$dy; | ||
| 207 | } | ||
| 208 | for my $i(0..$N) # Step along second line segment mapping it on to the nearest part of the circle | ||
| 209 | {my $dx = ($x3 - $x2) * $i / $N; | ||
| 210 | my $dy = ($y3 - $y2) * $i / $N; | ||
| 211 | push @p, join " ", "L", inter $x2+$dx, $y2+$dy; | ||
| 212 | } | ||
| 213 | |||
| 214 | my $p = join " ", @p; # Construct the SVG path string running along the circle circumference from the first point, through the middle point to the last point | ||
| 215 | $p =~ s(\AL) (M)r # Move to the start of the curve | ||
| 216 | } $x1, $y1, $x2, $y2, $x3, $y3; | ||
| 217 | } | ||
| 218 | |||
| 219 | #D0 | ||
| 220 | #----------------------------------------------------------------------------------------------------------------------- | ||
| 221 | # Export - eeee | ||
| 222 | #----------------------------------------------------------------------------------------------------------------------- | ||
| 223 | |||
| 224 | use Exporter qw(import); | ||
| 225 | |||
| 226 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 227 | |||
| 228 | # containingFolder | ||
| 229 | |||
| 230 | @ISA = qw(Exporter); | ||
| 231 | @EXPORT = qw(); | ||
| 232 | @EXPORT_OK = qw(); | ||
| 233 | %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); | ||
| 234 | |||
| 235 | # podDocumentation | ||
| 236 | |||
| 237 | =pod | ||
| 238 | |||
| 239 | =encoding utf-8 | ||
| 240 | |||
| 241 | =for html <p><a href="https://github.com/philiprbrenan/SvgSimple"><img src="https://github.com/philiprbrenan/SvgSimple/workflows/Test/badge.svg"></a> | ||
| 242 | |||
| 243 | =head1 Name | ||
| 244 | |||
| 245 | Svg::Simple - Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax. | ||
| 246 | |||
| 247 | =head1 Synopsis | ||
| 248 | |||
| 249 | Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax as in: | ||
| 250 | |||
| 251 | my $s = Svg::Simple::new(); | ||
| 252 | |||
| 253 | $s->g(id=>"g1", sub=>sub | ||
| 254 | {$s->text(x=>10, y=>10, | ||
| 255 | cdata =>"Hello World", | ||
| 256 | text_anchor =>"middle", | ||
| 257 | alignment_baseline=>"middle", | ||
| 258 | font_size => 3.6, | ||
| 259 | font_family =>"Arial", | ||
| 260 | fill =>"black"); | ||
| 261 | |||
| 262 | $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5); | ||
| 263 | }); | ||
| 264 | say STDERR $s->print; | ||
| 265 | |||
| 266 | =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/test.svg"> | ||
| 267 | |||
| 268 | A B<-> in an L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> | ||
| 269 | keyword can be replaced with B<_> to reduce line noise. | ||
| 270 | |||
| 271 | A B<cdata=>"text"> keyword value pair will placed the text inside an open and closing pair of tags. | ||
| 272 | |||
| 273 | A B<sub=\>sub{}> keyword value pair will create an open tag, call the supplied sub and then create a close tag to | ||
| 274 | bracket svg statements together, | ||
| 275 | |||
| 276 | A B<-> in an L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> | ||
| 277 | keyword can be replaced with B<_> to reduce line noise. | ||
| 278 | |||
| 279 | The L<print> method automatically creates an | ||
| 280 | L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> to wrap around | ||
| 281 | all the L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> | ||
| 282 | statements specified. The image so created will fill all of the available | ||
| 283 | space in the browser if the image is shown by itself, else it will fill all of | ||
| 284 | the available space in the parent tag containing the | ||
| 285 | L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> statements if the | ||
| 286 | L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> is inlined in | ||
| 287 | L<html> . | ||
| 288 | |||
| 289 | This package automatically tracks the dimensions of the objects specified in | ||
| 290 | the L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> statements | ||
| 291 | and creates a viewport wide enough and high enough to display them fully in | ||
| 292 | whatever space the browser allocates to the | ||
| 293 | L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> image. | ||
| 294 | |||
| 295 | If you wish to set these dimensions yourself, call the L<print> method with | ||
| 296 | overriding values as in: | ||
| 297 | |||
| 298 | say STDERR $s->print(x=>-100, y=>-100, width=>2000, height=>1000); | ||
| 299 | |||
| 300 | =head1 Description | ||
| 301 | |||
| 302 | Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax. | ||
| 303 | |||
| 304 | Version 20240308. | ||
| 305 | |||
| 306 | The following sections describe the methods in each functional area of this | ||
| 307 | module. For an alphabetic listing of all methods by name see L<Index|/Index>. | ||
| 308 | |||
| 309 | =head1 Constructors | ||
| 310 | |||
| 311 | Construct and print a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object. | ||
| 312 | |||
| 313 | =head2 newย (%options) | ||
| 314 | |||
| 315 | Create a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object. | ||
| 316 | |||
| 317 | Parameter Description | ||
| 318 | 1 %options Svg options | ||
| 319 | |||
| 320 | B<Example:> | ||
| 321 | |||
| 322 | |||
| 323 | if (1) | ||
| 324 | |||
| 325 | {my $s = Svg::Simple::new(); # ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ | ||
| 326 | |||
| 327 | |||
| 328 | $s->text(x=>10, y=>10, | ||
| 329 | cdata =>"Hello World", | ||
| 330 | text_anchor =>"middle", | ||
| 331 | alignment_baseline=>"middle", | ||
| 332 | font_size => 3.6, | ||
| 333 | font_family =>"Arial", | ||
| 334 | fill =>"black"); | ||
| 335 | |||
| 336 | $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5); | ||
| 337 | |||
| 338 | my $t = $s->print(svg=>q(svg/new)); # ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ | ||
| 339 | |||
| 340 | ok($t =~ m(circle)); | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/new.svg"> | ||
| 345 | |||
| 346 | |||
| 347 | =head2 gridLinesย ย ย ($svg, $x, $y, $g) | ||
| 348 | |||
| 349 | Draw a grid. | ||
| 350 | |||
| 351 | Parameter Description | ||
| 352 | 1 $svg Svg | ||
| 353 | 2 $x Maximum X | ||
| 354 | 3 $y Maximum Y | ||
| 355 | 4 $g Grid square size | ||
| 356 | |||
| 357 | B<Example:> | ||
| 358 | |||
| 359 | |||
| 360 | if (1) | ||
| 361 | {my $s = Svg::Simple::new(grid=>10); | ||
| 362 | $s->rect(x=>10, y=>10, width=>40, height=>30, stroke=>"blue", fill=>'transparent'); | ||
| 363 | my $t = $s->print(svg=>q(svg/grid)); | ||
| 364 | is_deeply(scalar(split /line/, $t), 32); | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/grid.svg"> | ||
| 369 | |||
| 370 | |||
| 371 | =head2 printย ย ย ($svg, %options) | ||
| 372 | |||
| 373 | Print resulting L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> string. | ||
| 374 | |||
| 375 | Parameter Description | ||
| 376 | 1 $svg Svg | ||
| 377 | 2 %options Svg options | ||
| 378 | |||
| 379 | B<Example:> | ||
| 380 | |||
| 381 | |||
| 382 | if (1) | ||
| 383 | {my $s = Svg::Simple::new(); | ||
| 384 | |||
| 385 | my @d = (width=>8, height=>8, stroke=>"blue", fill=>"transparent"); # Default values | ||
| 386 | $s->rect(x=>1, y=>1, z=>1, @d, stroke=>"blue"); # Defined earlier but drawn above because of z order | ||
| 387 | $s->rect(x=>4, y=>4, z=>0, @d, stroke=>"red"); | ||
| 388 | |||
| 389 | my $t = $s->print(svg=>q(svg/rect)); # ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ | ||
| 390 | |||
| 391 | is_deeply(scalar(split /rect/, $t), 3); | ||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/rect.svg"> | ||
| 396 | |||
| 397 | |||
| 398 | =head1 Utility functions | ||
| 399 | |||
| 400 | Extra features to make using Svg easier | ||
| 401 | |||
| 402 | =head2 arcPathย ($svg, $N, $x1, $y1, $x2, $y2, $x3, $y3) | ||
| 403 | |||
| 404 | Arc through three points along the circumference of a circle from the first point through the middle point to the last point | ||
| 405 | |||
| 406 | Parameter Description | ||
| 407 | 1 $svg Svg | ||
| 408 | 2 $N Number of points on path | ||
| 409 | 3 $x1 Start x | ||
| 410 | 4 $y1 Start y | ||
| 411 | 5 $x2 Middle x | ||
| 412 | 6 $y2 Middle y | ||
| 413 | 7 $x3 End x | ||
| 414 | 8 $y3 End y | ||
| 415 | |||
| 416 | B<Example:> | ||
| 417 | |||
| 418 | |||
| 419 | if (1) | ||
| 420 | {my $d = {width=>8, height=>8, stroke_width=>0.1, stroke=>"blue", fill=>"transparent"}; # Default values | ||
| 421 | my $s = Svg::Simple::new(defaults=>$d); | ||
| 422 | |||
| 423 | my $p = $s->arcPath(64, 1,1, 3,2, 1, 3); # ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ | ||
| 424 | |||
| 425 | $s->path(d=>"M 1 1 $p Z"); | ||
| 426 | $s->print(svg=>q(svg/arc1), width=>10, height=>10); | ||
| 427 | } | ||
| 428 | |||
| 429 | |||
| 430 | =for html <img src="https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/svg/arc1.svg"> | ||
| 431 | |||
| 432 | |||
| 433 | |||
| 434 | =head1 Private Methods | ||
| 435 | |||
| 436 | =head2 AUTOLOAD($svg, %options) | ||
| 437 | |||
| 438 | L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> methods. | ||
| 439 | |||
| 440 | Parameter Description | ||
| 441 | 1 $svg Svg object | ||
| 442 | 2 %options Options | ||
| 443 | |||
| 444 | |||
| 445 | =head1 Index | ||
| 446 | |||
| 447 | |||
| 448 | 1 L<arcPath|/arcPath> - Arc through three points along the circumference of a circle from the first point through the middle point to the last point | ||
| 449 | |||
| 450 | 2 L<AUTOLOAD|/AUTOLOAD> - L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> methods. | ||
| 451 | |||
| 452 | 3 L<gridLines|/gridLines> - Draw a grid. | ||
| 453 | |||
| 454 | 4 L<new|/new> - Create a new L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> object. | ||
| 455 | |||
| 456 | 5 L<print|/print> - Print resulting L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> string. | ||
| 457 | |||
| 458 | =head1 Installation | ||
| 459 | |||
| 460 | This module is written in 100% Pure Perl and, thus, it is easy to read, | ||
| 461 | comprehend, use, modify and install via B<cpan>: | ||
| 462 | |||
| 463 | sudo cpan install Svg::Simple | ||
| 464 | |||
| 465 | =head1 Author | ||
| 466 | |||
| 467 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||
| 468 | |||
| 469 | L<http://prb.appaapps.com|http://prb.appaapps.com> | ||
| 470 | |||
| 471 | =head1 Copyright | ||
| 472 | |||
| 473 | Copyright (c) 2016-2023 Philip R Brenan. | ||
| 474 | |||
| 475 | This module is free software. It may be used, redistributed and/or modified | ||
| 476 | under the same terms as Perl itself. | ||
| 477 | |||
| 478 | =cut | ||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | #D0 Tests # Tests and examples | ||
| 483 | goto finish if caller; # Skip testing if we are being called as a module | ||
| 484 | clearFolder(q(svg), 99); # Clear the output svg folder | ||
| 485 | eval "use Test::More qw(no_plan)"; | ||
| 486 | eval "Test::More->builder->output('/dev/null')" if -e q(/home/phil/); | ||
| 487 | eval {goto latest}; | ||
| 488 | |||
| 489 | #Svg https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/ | ||
| 490 | |||
| 491 | if (1) #Tnew | ||
| 492 | {my $s = Svg::Simple::new(); | ||
| 493 | |||
| 494 | $s->text(x=>10, y=>10, | ||
| 495 | cdata =>"Hello World", | ||
| 496 | text_anchor =>"middle", | ||
| 497 | alignment_baseline=>"middle", | ||
| 498 | font_size => 3.6, | ||
| 499 | font_family =>"Arial", | ||
| 500 | fill =>"black"); | ||
| 501 | |||
| 502 | $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5); | ||
| 503 | my $t = $s->print(svg=>q(svg/new)); | ||
| 504 | ok($t =~ m(circle)); | ||
| 505 | } | ||
| 506 | |||
| 507 | if (1) #TgridLines | ||
| 508 | {my $s = Svg::Simple::new(grid=>10); | ||
| 509 | $s->rect(x=>10, y=>10, width=>40, height=>30, stroke=>"blue", fill=>'transparent'); | ||
| 510 | my $t = $s->print(svg=>q(svg/grid)); | ||
| 511 | is_deeply(scalar(split /line/, $t), 32); | ||
| 512 | } | ||
| 513 | |||
| 514 | if (1) #Tprint | ||
| 515 | {my $s = Svg::Simple::new(); | ||
| 516 | |||
| 517 | my @d = (width=>8, height=>8, stroke=>"blue", fill=>"transparent"); # Default values | ||
| 518 | $s->rect(x=>1, y=>1, z=>1, @d, stroke=>"blue"); # Defined earlier but drawn above because of z order | ||
| 519 | $s->rect(x=>4, y=>4, z=>0, @d, stroke=>"red"); | ||
| 520 | my $t = $s->print(svg=>q(svg/rect)); | ||
| 521 | is_deeply(scalar(split /rect/, $t), 3); | ||
| 522 | } | ||
| 523 | |||
| 524 | if (1) #TarcPath | ||
| 525 | {my $d = {width=>8, height=>8, stroke_width=>0.1, stroke=>"blue", fill=>"transparent"}; # Default values | ||
| 526 | my $s = Svg::Simple::new(defaults=>$d); | ||
| 527 | my $p = $s->arcPath(64, 1,1, 3,2, 1, 3); | ||
| 528 | $s->path(d=>"M 1 1 $p Z"); | ||
| 529 | $s->print(svg=>q(svg/arc1), width=>10, height=>10); | ||
| 530 | } | ||
| 531 | |||
| 532 | if (1) | ||
| 533 | {my $s = Svg::Simple::new(); | ||
| 534 | |||
| 535 | $s->g(id=>"group1", sub=>sub | ||
| 536 | {$s->text(x=>10, y=>10, | ||
| 537 | cdata =>"Hello World", | ||
| 538 | text_anchor =>"middle", | ||
| 539 | alignment_baseline=>"middle", | ||
| 540 | font_size => 3.6, | ||
| 541 | font_family =>"Arial", | ||
| 542 | fill =>"black"); | ||
| 543 | |||
| 544 | $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5); | ||
| 545 | }); | ||
| 546 | |||
| 547 | &is_deeply($s->print, <<'END'); | ||
| 548 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| 549 | <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> | ||
| 550 | <svg height="100%" viewBox="0 0 19 19" width="100%" background-color="none" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
| 551 | |||
| 552 | <g id="group1"> | ||
| 553 | <text alignment-baseline="middle" fill="black" font-family="Arial" font-size="3.6" text-anchor="middle" x="10" y="10">Hello World</text> | ||
| 554 | <circle cx="10" cy="10" fill="transparent" opacity="0.5" r="8" stroke="blue"/> | ||
| 555 | </g> | ||
| 556 | </svg> | ||
| 557 | END | ||
| 558 | } | ||
| 559 | |||
| 560 | done_testing(); | ||
| 561 | finish: 1; | ||