From: Vincent Pit Date: Sun, 18 Jul 2010 08:11:39 +0000 (+0200) Subject: Remove string overloading completely for scope objects X-Git-Tag: v0.01~59 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=commitdiff_plain;h=c314e50ea7c67844e6cf5f4d1431d8bf41a39f1a Remove string overloading completely for scope objects It isn't really suited anymore to our situation. ->draw no longer needs to return a scope object : it can now also return a plain array reference. --- diff --git a/lib/LaTeX/TikZ/Formatter.pm b/lib/LaTeX/TikZ/Formatter.pm index 6426607..6592b38 100644 --- a/lib/LaTeX/TikZ/Formatter.pm +++ b/lib/LaTeX/TikZ/Formatter.pm @@ -111,16 +111,12 @@ sub render { ); my @decls; - if (@layers) { - push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers); - } - for (@other_mods) { - push @decls, $_->declare($tikz); - } + push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers; + push @decls, $_->declare($tikz) for @other_mods; my @content = ( "\\begin{tikzpicture}", - $seq->draw($tikz)->instantiate, + @{ $seq->draw($tikz) }, "\\end{tikzpicture}", ); diff --git a/lib/LaTeX/TikZ/Scope.pm b/lib/LaTeX/TikZ/Scope.pm index 05308dd..c072259 100644 --- a/lib/LaTeX/TikZ/Scope.pm +++ b/lib/LaTeX/TikZ/Scope.pm @@ -76,7 +76,7 @@ sub body { } use overload ( - '.' => \&concat, + '@{}' => 'dereference', ); sub flatten { @@ -163,45 +163,54 @@ sub instantiate { return @body; } -sub concat { - my ($left, $right, $rev) = @_; - - $_body_tc->assert_valid($right); +sub dereference { [ $_[0]->instantiate ] } - $left = $left->flatten; +sub fold { + my ($left, $right, $rev) = @_; my (@left, @right); - if ($my_tc->check($right)) { - $right = $right->flatten; - - my ($only_left, $common, $only_right) = $inter->( - $left->_mods_cache, - $right->_mods_cache, - ); - - if (@$common) { - my $x = $left->new - ->mod(@$only_left) - ->body($left->_body); - my $y = $left->new - ->mod(@$only_right) - ->body($right->_body); - ($x, $y) = ($y, $x) if $rev; - return $left->new - ->mod(@$common) - ->body($x . $y); + if ($my_tc->check($left)) { + $left = $left->flatten; + + if ($my_tc->check($right)) { + $right = $right->flatten; + + my ($only_left, $common, $only_right) = $inter->( + $left->_mods_cache, + $right->_mods_cache, + ); + + if (@$common) { + my $x = $left->new + ->mod(@$only_left) + ->body($left->_body); + my $y = $left->new + ->mod(@$only_right) + ->body($right->_body); + return $left->new + ->mod(@$common) + ->body(fold($x, $y, $rev)); + } else { + @right = $right->instantiate; + } } else { - @right = $right->instantiate; + $_body_tc->assert_valid($right); + @right = @$right; } + + @left = $left->instantiate; } else { - @right = @$right; + if ($my_tc->check($right)) { + return fold($right, $left, 1); + } else { + $_body_tc->assert_valid($_) for $left, $right; + @left = @$left; + @right = @$right; + } } - @left = $left->instantiate; - - $left->new - ->body($rev ? [ @right, @left ] : [ @left, @right ]); + $rev ? [ @right, @left ] : [ @left, @right ]; } __PACKAGE__->meta->make_immutable; diff --git a/lib/LaTeX/TikZ/Set/Mod.pm b/lib/LaTeX/TikZ/Set/Mod.pm index 7658340..8444fea 100644 --- a/lib/LaTeX/TikZ/Set/Mod.pm +++ b/lib/LaTeX/TikZ/Set/Mod.pm @@ -128,9 +128,15 @@ MOD: my @mods = $set->mods_unique; - LaTeX::TikZ::Scope->new - ->mod(map $_->apply($tikz), @mods) - ->body($set->_set->draw($tikz)) + my $body = $set->_set->draw($tikz); + + if (@mods) { + $body = LaTeX::TikZ::Scope->new + ->mod(map $_->apply($tikz), @mods) + ->body($body); + } + + $body; } } diff --git a/lib/LaTeX/TikZ/Set/Op.pm b/lib/LaTeX/TikZ/Set/Op.pm index c7f7e1b..2c85f0b 100644 --- a/lib/LaTeX/TikZ/Set/Op.pm +++ b/lib/LaTeX/TikZ/Set/Op.pm @@ -15,8 +15,6 @@ Version 0.01 our $VERSION = '0.01'; -use LaTeX::TikZ::Scope; - use Any::Moose 'Role'; requires qw( @@ -26,8 +24,7 @@ requires qw( sub draw { my $set = shift; - LaTeX::TikZ::Scope->new - ->body([ "\\draw " . $set->path(@_) . ' ;' ]); + [ "\\draw " . $set->path(@_) . ' ;' ]; } =head1 AUTHOR diff --git a/lib/LaTeX/TikZ/Set/Sequence.pm b/lib/LaTeX/TikZ/Set/Sequence.pm index 3f14bb7..7742435 100644 --- a/lib/LaTeX/TikZ/Set/Sequence.pm +++ b/lib/LaTeX/TikZ/Set/Sequence.pm @@ -17,6 +17,8 @@ our $VERSION = '0.01'; use List::Util (); +use LaTeX::TikZ::Scope; + use Any::Moose; use Any::Moose 'Util::TypeConstraints' => [ qw/subtype as where find_type_constraint/ ]; @@ -58,14 +60,14 @@ sub add { sub draw { my $set = shift; - List::Util::reduce { $a . $b } map $_->draw(@_), $set->kids; + List::Util::reduce { LaTeX::TikZ::Scope::fold($a, $b) } + map $_->draw(@_), + $set->kids; } use LaTeX::TikZ::API seq => sub { shift; - die 'wut' if $_[0]->isa('LaTeX::TikZ::Set::Op'); - __PACKAGE__->new(kids => \@_); };