1 package LaTeX::TikZ::Scope;
8 LaTeX::TikZ::Scope - An object modeling a TikZ scope or layer.
16 our $VERSION = '0.02';
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]',
57 my $my_tc = LaTeX::TikZ::Tools::type_constraint(__PACKAGE__);
58 my $ltmf_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Formatted');
59 my $body_tc = __PACKAGE__->meta->find_attribute_by_name('body')
62 around 'BUILDARGS' => sub {
63 my ($orig, $class, %args) = @_;
65 my $mods = $args{mods};
66 if (defined $mods and ref $mods eq 'ARRAY') {
67 for my $mod (@$mods) {
68 $mod = $ltmf_tc->coerce($mod);
78 my $cache = $scope->_mods_cache;
81 for my $mod ($scope->mods) {
83 next if exists $cache->{$tag};
84 $cache->{$tag} = $mod;
85 push @unique_mods, $mod;
87 $scope->_mods(\@unique_mods);
100 my $body = $scope->body;
101 return $scope unless $my_tc->check($body);
102 $scope = $scope->new(
103 mods => [ $scope->mods, $body->mods ],
109 my $inter = Sub::Name::subname('inter' => sub {
112 my (@left, @common, @right);
115 --$where{$_} for keys %$lh;
116 ++$where{$_} for keys %$rh;
118 while (my ($key, $where) = each %where) {
120 push @left, $lh->{$key};
121 } elsif ($where > 0) {
122 push @right, $rh->{$key};
124 push @common, $rh->{$key};
128 return \@left, \@common, \@right;
131 =head2 C<instantiate>
138 $scope = $scope->flatten;
140 my ($layer, @clips, @raw_mods);
143 if ($type eq 'clip') {
144 unshift @clips, $_->content;
145 } elsif ($type eq 'layer') {
146 confess("Can't apply two layers in a row") if defined $layer;
147 $layer = $_->content;
149 push @raw_mods, $_->content;
153 my @body = @{$scope->body};
155 my $mods_string = @raw_mods ? ' [' . join(',', @raw_mods) . ']' : undef;
157 if (@raw_mods and @body == 1 and $body[0] =~ /^\s*\\draw\b\s*([^\[].*)\s*$/) {
158 $body[0] = "\\draw$mods_string $1"; # Has trailing semicolon
159 $mods_string = undef; # Done with mods
163 my $clip = $clips[$_];
164 my $clip_string = "\\clip $clip ;";
165 my $mods_string = ($_ == $#clips and defined $mods_string)
167 unshift @body, "\\begin{scope}$mods_string",
169 push @body, "\\end{scope}",
172 if (not @clips and defined $mods_string) {
173 unshift @body, "\\begin{scope}$mods_string";
174 push @body, "\\end{scope}";
177 if (defined $layer) {
178 unshift @body, "\\begin{pgfonlayer}{$layer}";
179 push @body, "\\end{pgfonlayer}";
190 my ($left, $right, $rev) = @_;
194 if ($my_tc->check($left)) {
195 $left = $left->flatten;
197 if ($my_tc->check($right)) {
198 $right = $right->flatten;
200 my ($only_left, $common, $only_right) = $inter->(
205 my $has_different_layers;
206 for (@$only_left, @$only_right) {
207 if ($_->type eq 'layer') {
208 $has_different_layers = 1;
213 if (!$has_different_layers and @$common) {
220 body => $right->body,
224 body => fold($x, $y, $rev),
227 @right = $right->instantiate;
230 $body_tc->assert_valid($right);
234 @left = $left->instantiate;
236 if ($my_tc->check($right)) {
237 return fold($right, $left, 1);
239 $body_tc->assert_valid($_) for $left, $right;
245 $rev ? [ @right, @left ] : [ @left, @right ];
249 '@{}' => sub { [ $_[0]->instantiate ] },
252 __PACKAGE__->meta->make_immutable;
260 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
262 You can contact me by mail or on C<irc.perl.org> (vincent).
266 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>.
267 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
271 You can find documentation for this module with the perldoc command.
275 =head1 COPYRIGHT & LICENSE
277 Copyright 2010 Vincent Pit, all rights reserved.
279 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
283 1; # End of LaTeX::TikZ::Scope