1 package LaTeX::TikZ::Mod::Layer;
8 LaTeX::TikZ::Mod::Layer - A modifier that specifies a drawing layer.
16 our $VERSION = '0.01';
21 use LaTeX::TikZ::Mod::Formatted;
23 use LaTeX::TikZ::Interface;
26 use Any::Moose 'Util::TypeConstraints';
30 This class consumes the L<LaTeX::TikZ::Mod> role, and as such implements the L</tag>, L</covers>, L</declare> and L</apply> methods.
34 with 'LaTeX::TikZ::Mod';
52 subtype 'LaTeX::TikZ::Mod::LevelList'
53 => as 'ArrayRef[LaTeX::TikZ::Mod::Layer]';
55 coerce 'LaTeX::TikZ::Mod::LevelList'
57 => via { [ __PACKAGE__->new(name => $_) ] };
59 coerce 'LaTeX::TikZ::Mod::LevelList'
60 => from 'ArrayRef[Str]'
61 => via { [ map __PACKAGE__->new(name => $_), @$_ ] };
65 isa => 'LaTeX::TikZ::Mod::LevelList',
67 default => sub { [ ] },
71 sub above { @{$_[0]->_above} }
79 isa => 'LaTeX::TikZ::Mod::LevelList',
81 default => sub { [ ] },
85 sub below { @{$_[0]->_below} }
92 builder => '_build_score',
101 around 'new' => sub {
102 my ($orig, $self, %args) = @_;
104 my $name = $args{name};
106 $self->meta->find_attribute_by_name('name')
107 ->type_constraint->assert_valid($name);
108 my $layer = $layers{$name};
109 if (defined $layer) {
110 confess("Can't redefine layer '$name'") if keys(%args) > 1;
115 return $self->$orig(%args);
121 my $name = $self->name;
122 $layers{$name} = $self;
123 Scalar::Util::weaken($layers{$name});
129 delete $layers{$self->name};
139 ref($self) . '/' . $self->name;
146 sub covers { $_[0]->name eq $_[1]->name }
158 my $name = $layer->name;
160 return $score{$name} if exists $score{$name};
163 for ($layer->above) {
166 $min = $cur if not defined $min or $min < $cur;
173 for ($layer->below) {
176 $max = $cur if not defined $max or $max < $cur;
184 confess("Order mismatch for $name") unless $min < $max;
185 $score{$name} = ($min + $max) / 2;
187 my $i = List::Util::max(values %score);
188 $score{$_} = ++$i for $name, @higher;
190 } elsif (defined $max) {
191 my $i = List::Util::min(values %score);
192 $score{$_} = --$i for @lower, $name;
195 $score{$_} = ++$i for @lower, $name, @higher;
210 local %score = (main => 0);
214 my @layers = sort { $score{$a} <=> $score{$b} }
215 map { ref() ? $_->name : $_ }
218 my @intro = map "\\pgfdeclarelayer{$_}",
224 "\\pgfsetlayers{" . join(',', @layers) . "}",
236 LaTeX::TikZ::Mod::Formatted->new(
238 content => $self->name,
242 LaTeX::TikZ::Interface->register(
247 __PACKAGE__->new(name => $name, @_);
251 __PACKAGE__->meta->make_immutable(
252 inline_constructor => 0,
257 L<LaTeX::TikZ>, L<LaTeX::TikZ::Mod>.
261 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
263 You can contact me by mail or on C<irc.perl.org> (vincent).
267 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>.
268 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
272 You can find documentation for this module with the perldoc command.
276 =head1 COPYRIGHT & LICENSE
278 Copyright 2010 Vincent Pit, all rights reserved.
280 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
284 1; # End of LaTeX::TikZ::Mod::Layer