1 package LaTeX::TikZ::Scope;
8 LaTeX::TikZ::Scope - An object modeling a TikZ scope or layer.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Tools;
32 isa => 'Maybe[ArrayRef[LaTeX::TikZ::Mod::Formatted]]',
34 default => sub { [ ] },
37 sub mods { @{$_[0]->_mods} }
39 has '_mods_cache' => (
41 isa => 'Maybe[HashRef[LaTeX::TikZ::Mod::Formatted]]',
43 default => sub { +{ } },
52 isa => 'LaTeX::TikZ::Scope|ArrayRef[Str]',
56 my $my_tc = LaTeX::TikZ::Tools::type_constraint(__PACKAGE__);
57 my $ltmf_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Formatted');
58 my $_body_tc = __PACKAGE__->meta->find_attribute_by_name('_body')
70 my $cache = $scope->_mods_cache;
73 my $mod = $ltmf_tc->coerce($_);
74 $ltmf_tc->assert_valid($mod);
76 next if exists $cache->{$tag};
77 $cache->{$tag} = $mod;
78 push @{$scope->_mods}, $mod;
100 '@{}' => 'dereference',
111 my $body = $scope->_body;
112 return $scope unless $my_tc->check($body);
114 ->mod ($scope->mods, $body->mods)
119 my $inter = Sub::Name::subname('inter' => sub {
122 my (@left, @common, @right);
125 --$where{$_} for keys %$lh;
126 ++$where{$_} for keys %$rh;
128 while (my ($key, $where) = each %where) {
130 push @left, $lh->{$key};
131 } elsif ($where > 0) {
132 push @right, $rh->{$key};
134 push @common, $rh->{$key};
138 return \@left, \@common, \@right;
141 =head2 C<instantiate>
148 $scope = $scope->flatten;
150 my ($layer, @clips, @raw_mods);
153 if ($type eq 'clip') {
154 unshift @clips, $_->content;
155 } elsif ($type eq 'layer') {
156 confess("Can't apply two layers in a row") if defined $layer;
157 $layer = $_->content;
159 push @raw_mods, $_->content;
163 my @body = $scope->body;
165 my $mods_string = @raw_mods ? ' [' . join(',', @raw_mods) . ']' : undef;
167 if (@raw_mods and @body == 1 and $body[0] =~ /^\s*\\draw\b\s*([^\[].*)\s*$/) {
168 $body[0] = "\\draw$mods_string $1"; # Has trailing semicolon
169 $mods_string = undef; # Done with mods
173 my $clip = $clips[$_];
174 my $clip_string = "\\clip $clip ;";
175 my $mods_string = ($_ == $#clips and defined $mods_string)
177 unshift @body, "\\begin{scope}$mods_string",
179 push @body, "\\end{scope}",
182 if (not @clips and defined $mods_string) {
183 unshift @body, "\\begin{scope}$mods_string";
184 push @body, "\\end{scope}";
187 if (defined $layer) {
188 unshift @body, "\\begin{pgfonlayer}{$layer}";
189 push @body, "\\end{pgfonlayer}";
195 =head2 C<dereference>
199 sub dereference { [ $_[0]->instantiate ] }
206 my ($left, $right, $rev) = @_;
210 if ($my_tc->check($left)) {
211 $left = $left->flatten;
213 if ($my_tc->check($right)) {
214 $right = $right->flatten;
216 my ($only_left, $common, $only_right) = $inter->(
221 my $has_different_layers;
223 if ($_->type eq 'layer') {
224 $has_different_layers = 1;
228 unless ($has_different_layers) {
230 if ($_->type eq 'layer') {
231 $has_different_layers = 1;
237 if (!$has_different_layers and @$common) {
240 ->body($left->_body);
243 ->body($right->_body);
246 ->body(fold($x, $y, $rev));
248 @right = $right->instantiate;
251 $_body_tc->assert_valid($right);
255 @left = $left->instantiate;
257 if ($my_tc->check($right)) {
258 return fold($right, $left, 1);
260 $_body_tc->assert_valid($_) for $left, $right;
266 $rev ? [ @right, @left ] : [ @left, @right ];
269 __PACKAGE__->meta->make_immutable;
277 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
279 You can contact me by mail or on C<irc.perl.org> (vincent).
283 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>.
284 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
288 You can find documentation for this module with the perldoc command.
292 =head1 COPYRIGHT & LICENSE
294 Copyright 2010 Vincent Pit, all rights reserved.
296 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
300 1; # End of LaTeX::TikZ::Scope