1 package LaTeX::TikZ::Set;
8 LaTeX::TikZ::Set - Base role for LaTeX::TikZ set objects.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Scope;
22 use LaTeX::TikZ::Tools;
24 use Any::Moose 'Role';
30 Returns the list of the L<LaTeX::TikZ::Mod> objects associated with the current set.
36 isa => 'Maybe[ArrayRef[LaTeX::TikZ::Mod]]',
38 default => sub { [ ] },
42 sub mods { @{$_[0]->_mods} }
46 This method is required by the interface :
54 Returns an array reference of TikZ code lines required to effectively draw the current set object, formatted by the L<LaTeX::TikZ::Formatter> object C<$formatter>.
66 Apply the given list of L<LaTeX::TikZ::Mod> objects to the current set.
70 my $ltm_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod');
71 my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer');
72 my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip');
77 my @mods = map $ltm_tc->coerce($_), @_;
78 $ltm_tc->assert_valid($_) for @mods;
80 push @{$set->_mods}, @mods;
89 around 'draw' => sub {
90 my ($orig, $set, $tikz) = @_;
92 local $last_mod = $last_mod;
95 my %saved_idx = map { $_ => $#{$mods{$_}} } keys %mods;
96 my $guard = Scope::Guard->new(sub {
98 if (exists $saved_idx{$_}) {
99 $#{$mods{$_}} = $saved_idx{$_};
106 my (@mods, $last_layer);
108 for my $mod ($set->mods) {
109 my $is_layer = $ltml_tc->check($mod);
110 $last_layer = $mod if $is_layer;
112 my $old = $mods{$tag} || [];
114 next MOD if $_->[0]->cover($mod);
116 push @{$mods{$tag}}, [ $mod, $last_mod++, $is_layer ];
121 # Clips and mods don't propagate through layers. Hence if a layer is set,
124 push @mods, map $_->[0],
125 sort { $a->[1] <=> $b->[1] }
131 my $body = $set->$orig($tikz);
134 $body = LaTeX::TikZ::Scope->new
135 ->mod(map $_->apply($tikz), @mods)
143 =head2 C<layer $layer>
145 Puts the current set in the corresponding layer.
146 This is a shortcut for C<< $set->mod(Tikz->layer($layer)) >>.
151 return $_[0] unless @_ > 1;
156 $ltml_tc->check($layer) ? $layer
157 : LaTeX::TikZ::Mod::Layer->new(name => $layer)
163 Clips the current set by the path given by C<$path>.
164 This is a shortcut for C<< $set->mod(Tikz->clip($path)) >>.
169 return $_[0] unless @_ > 1;
173 $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
184 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
186 You can contact me by mail or on C<irc.perl.org> (vincent).
190 Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
191 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
195 You can find documentation for this module with the perldoc command.
199 =head1 COPYRIGHT & LICENSE
201 Copyright 2010 Vincent Pit, all rights reserved.
203 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
207 1; # End of LaTeX::TikZ::Set