Svg-Simple

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


Svg::Simple - Write [Scalar Vector Graphics](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) using Perl syntax.

# Synopsis

Write [Scalar Vector Graphics](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) using Perl syntax as in:

    my $s = Svg::Simple::new();

    $s->text(x=>10, y=>10,
      cdata             =>"Hello World",
      text_anchor       =>"middle",
      alignment_baseline=>"middle",
      font_size         => 3.6,
      font_family       =>"Arial",
      fill              =>"black");

    $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);

    say STDERR $s->print;

README.md  view on Meta::CPAN

    1  %options   Svg options

**Example:**

    if (1)                                                                          
    
     {my $s = Svg::Simple::new();  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

    
      $s->text(x=>10, y=>10,
        cdata             =>"Hello World",
        text_anchor       =>"middle",
        alignment_baseline=>"middle",
        font_size         => 3.6,
        font_family       =>"Arial",
        fill              =>"black");
    
      $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);
    
      my $t = $s->print(svg=>q(svg/new));  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

lib/Svg/Simple.pm  view on Meta::CPAN

  my $X = int($x / $g); my $Y = int($y / $g);                                   # Steps in X and Y
  my $f =     $g /  4;                                                          # Font size
  my $w =     $f / 16;                                                          # Line width
  my @w = (opacity=>0.2, font_size=>$f, stroke_width=>$w, stroke=>"black",      # Font for grid
           text_anchor => "start", dominant_baseline => "hanging");
  my @f = (@w, opacity=>1, fill=>'black');

  for my $i(0..$X)                                                              # X lines
   {my $c = $i*$g;
    $svg->line(x1=>$c, x2=>$c, y1=>0, y2=>$y, @w);
    $svg->text(@f, x => $c, y => 0, cdata => $i) unless $i == $X;
   }

  for my $i(0..$Y)                                                              # Y lines
   {my $c = $i*$g;
    $svg->line(y1=>$c, y2=>$c, x1=>0, x2=>$x, @w);
    $svg->text(@f, x => 0, y => $c, cdata => $i) unless $i == $Y;
   }
 }

sub print($%)                                                                   # Print resulting L<svg> string.
 {my ($svg, %options) = @_;                                                     # Svg, svg options
  my $X = $options{width}  // $svg->mX;                                         # Maximum width
  my $Y = $options{height} // $svg->mY;                                         # Maximum height
  my $g = $svg->grid ? $svg->gridLines($X, $Y, $svg->grid) : '';                # Draw a grid if requested
  my $e = q(</svg>);

lib/Svg/Simple.pm  view on Meta::CPAN

 {my ($svg, %options) = @_;                                                     # Svg object, options
  my @s;

  my %o;
     %o = ($svg->defaults->%*) if $svg->defaults;                               # Add any default values
     %o = (%o, %options);                                                       # Add supplied options

  for my $k(sort keys %o)                                                       # Process each option
   {my $v = $o{$k};
    my $K = $k =~ s(_) (-)r;                                                    # Underscore _ in option names becomes hyphen -
    next if $k =~ m(\Acdata\Z)i;
    push @s, qq($K="$v");
   }

  my $n = $AUTOLOAD =~ s(\A.*::) ()r;                                           # Name of element

  eval                                                                          # Maximum extent of the Svg
   {my $X = $svg->mY;
    my $Y = $svg->mY;
    my $w = $options{stroke} ? $options{stroke_width} // $options{"stroke-width"} // 1 : 0;
    if ($n =~ m(\Acircle\Z)i)

lib/Svg/Simple.pm  view on Meta::CPAN

     }
    if ($n =~ m(\Aline\Z)i)
     {$X = max $X, $w + $options{$_} for qw(x1 x2);
      $Y = max $Y, $w + $options{$_} for qw(y1 y2);
     }
    if ($n =~ m(\Arect\Z)i)
     {$X = max $X, $w + $options{x}+$options{width};
      $Y = max $Y, $w + $options{y}+$options{height};
     }
    if ($n =~ m(\Atext\Z)i)
     {$X = max $X, $options{x} + $w * length($options{cdata});
      $Y = max $Y, $options{y};
     }
    $svg->mX = max $svg->mX, $X;
    $svg->mY = max $svg->mY, $Y;
   };

  my $z = 0;                                                                    # Default z order
  if (defined(my $Z = $options{z}))                                             # Override Z order
   {$svg->z->{$z = $Z}++;
   }

  my $p = join " ", @s;                                                         # Options
  if (defined(my $t = $options{cdata}))
   {push $svg->code->@*, ["<$n $p>$t</$n>", $z]                                 # Internal text
   }
  else
   {push $svg->code->@*, ["<$n $p/>",       $z]                                 # No internal text
   }
  $svg
 }

#D0
#-------------------------------------------------------------------------------

lib/Svg/Simple.pm  view on Meta::CPAN


Svg::Simple - Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax.

=head1 Synopsis

Write L<Scalar Vector Graphics|https://en.wikipedia.org/wiki/Scalable_Vector_Graphics> using Perl syntax as in:

  my $s = Svg::Simple::new();

  $s->text(x=>10, y=>10,
    cdata             =>"Hello World",
    text_anchor       =>"middle",
    alignment_baseline=>"middle",
    font_size         => 3.6,
    font_family       =>"Arial",
    fill              =>"black");

  $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);

  say STDERR $s->print;

lib/Svg/Simple.pm  view on Meta::CPAN


B<Example:>


  if (1)

   {my $s = Svg::Simple::new();  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


    $s->text(x=>10, y=>10,
      cdata             =>"Hello World",
      text_anchor       =>"middle",
      alignment_baseline=>"middle",
      font_size         => 3.6,
      font_family       =>"Arial",
      fill              =>"black");

    $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);

    my $t = $s->print(svg=>q(svg/new));  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

lib/Svg/Simple.pm  view on Meta::CPAN

eval "use Test::More qw(no_plan)";
eval "Test::More->builder->output('/dev/null')" if -e q(/home/phil/);
eval {goto latest};

#Svg https://raw.githubusercontent.com/philiprbrenan/SvgSimple/main/lib/Svg/

if (1)                                                                          #Tnew
 {my $s = Svg::Simple::new();

  $s->text(x=>10, y=>10,
    cdata             =>"Hello World",
    text_anchor       =>"middle",
    alignment_baseline=>"middle",
    font_size         => 3.6,
    font_family       =>"Arial",
    fill              =>"black");

  $s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent", opacity=>0.5);
  my $t = $s->print(svg=>q(svg/new));
  ok($t =~ m(circle));
 }

test.pl  view on Meta::CPAN

#-------------------------------------------------------------------------------
use warnings FATAL => qw(all);
use strict;
use Svg::Simple;
use Data::Dump qw(dump);
use Data::Table::Text qw(:all);
use Test::More tests => 2;

my $s = Svg::Simple::new();
$s->text(x=>10, y=>10, font_size=>4,
  cdata             =>"Hello World",
  text_anchor       =>"middle",
  alignment_baseline=>"middle",
  font_size         =>"20",
  font_family       =>"Arial",
  fill              =>"black");
$s->circle(cx=>10, cy=>10, r=>8, stroke=>"blue", fill=>"transparent");
ok $s->print =~ m(text);
ok $s->print =~ m(circle);



( run in 0.386 second using v1.01-cache-2.11-cpan-454fe037f31 )