X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=blobdiff_plain;f=lib%2FLaTeX%2FTikZ%2FSet.pm;h=e719643a3a7fea5d64c81efd4cd873da5ee2fd4e;hp=dcaab09494b69c2971e619e21a787f9e19c757da;hb=cb3b916e0590c6a0b70b60f30304921385462faa;hpb=aba26c835113a0332c5480e99f86bfa67111de84 diff --git a/lib/LaTeX/TikZ/Set.pm b/lib/LaTeX/TikZ/Set.pm index dcaab09..e719643 100644 --- a/lib/LaTeX/TikZ/Set.pm +++ b/lib/LaTeX/TikZ/Set.pm @@ -15,7 +15,9 @@ Version 0.01 our $VERSION = '0.01'; -use LaTeX::TikZ::Set::Mod; +use Scope::Guard (); + +use LaTeX::TikZ::Scope; use LaTeX::TikZ::Tools; @@ -25,28 +27,89 @@ requires qw( draw ); -sub mod { - my $set = $_[0]; +has '_mods' => ( + is => 'ro', + isa => 'Maybe[ArrayRef[LaTeX::TikZ::Mod]]', + init_arg => 'mods', + default => sub { [ ] }, + lazy => 1, +); - return $set unless @_ > 1; +sub mods { @{$_[0]->_mods} } - # If $set is already a Tikz::Set::Mod object, the overridden method is - # called instead. This ensures that you can't have two T::S::M objects in a - # row. +my $ltm_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod'); +my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer'); +my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip'); - # Prepend a new set with the mods - my $new = LaTeX::TikZ::Set::Mod->new( - set => $set, - mods => [ @_[1 .. $#_] ], - ); +sub mod { + my $set = shift; - $_[0] = $new unless defined wantarray; + push @{$set->_mods}, + map { $ltm_tc->check($_) ? $_ : $ltm_tc->coerce($_) } + @_; - $new; + $set; } -my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer'); -my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip'); +{ + our %mods; + our $last_mod = 0; + + sub mods_unique { + my ($set) = @_; + + my (@mods, $has_layer); +MOD: + for my $mod ($set->mods) { + $has_layer = 1 if $ltml_tc->check($mod); + my $tag = $mod->tag; + my $old = $mods{$tag} || []; + for (@$old) { + next MOD if $_->[0]->cover($mod); + } + push @{$mods{$tag}}, [ $mod, $last_mod++ ]; + push @mods, $mod; + } + + if ($has_layer) { + # Clips and mods don't propagate through layers. Hence if a layer is set, + # force their reuse. + @mods = map $_->[0], sort { $a->[1] <=> $b->[1] } map @$_, values %mods; + } + + return @mods; + } + + around 'draw' => sub { + my ($orig, $set, $tikz) = @_; + + local $last_mod = $last_mod; + + # Save a deep copy + my %saved_idx = map { $_ => $#{$mods{$_}} } keys %mods; + my $guard = Scope::Guard->new(sub { + for (keys %mods) { + if (exists $saved_idx{$_}) { + $#{$mods{$_}} = $saved_idx{$_}; + } else { + delete $mods{$_}; + } + } + }); + + my @mods = $set->mods_unique; + + my $body = $set->$orig($tikz); + + if (@mods) { + $body = LaTeX::TikZ::Scope->new + ->mod(map $_->apply($tikz), @mods) + ->body($body); + } + + $body; + }; +} sub layer { return $_[0] unless @_ > 1;