]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
Always call ->assert_valid after ->coerce
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Set.pm
1 package LaTeX::TikZ::Set;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Set - Base role for LaTeX::TikZ set objects.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Scope::Guard ();
19
20 use LaTeX::TikZ::Scope;
21
22 use LaTeX::TikZ::Tools;
23
24 use Any::Moose 'Role';
25
26 requires qw(
27  draw
28 );
29
30 has '_mods' => (
31  is       => 'ro',
32  isa      => 'Maybe[ArrayRef[LaTeX::TikZ::Mod]]',
33  init_arg => 'mods',
34  default  => sub { [ ] },
35  lazy     => 1,
36 );
37
38 sub mods { @{$_[0]->_mods} }
39
40 my $ltm_tc  = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod');
41 my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer');
42 my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip');
43
44 sub mod {
45  my $set = shift;
46
47  my @mods = map $ltm_tc->coerce($_), @_;
48  $ltm_tc->assert_valid($_) for @mods;
49
50  push @{$set->_mods}, @mods;
51
52  $set;
53 }
54
55 {
56  our %mods;
57  our $last_mod = 0;
58
59  sub mods_unique {
60   my ($set) = @_;
61
62   my (@mods, $last_layer);
63 MOD:
64   for my $mod ($set->mods) {
65    my $is_layer = $ltml_tc->check($mod);
66    $last_layer  = $mod if $is_layer;
67    my $tag = $mod->tag;
68    my $old = $mods{$tag} || [];
69    for (@$old) {
70     next MOD if $_->[0]->cover($mod);
71    }
72    push @{$mods{$tag}}, [ $mod, $last_mod++, $is_layer ];
73    push @mods,          $mod;
74   }
75
76   if ($last_layer) {
77    # Clips and mods don't propagate through layers. Hence if a layer is set,
78    # force their reuse.
79    @mods = $last_layer;
80    push @mods, map $_->[0],
81                 sort { $a->[1] <=> $b->[1] }
82                  grep !$_->[2],
83                   map @$_,
84                    values %mods;
85   }
86
87   return @mods;
88  }
89
90  around 'draw' => sub {
91   my ($orig, $set, $tikz) = @_;
92
93   local $last_mod = $last_mod;
94
95   # Save a deep copy
96   my %saved_idx = map { $_ => $#{$mods{$_}} } keys %mods;
97   my $guard     = Scope::Guard->new(sub {
98    for (keys %mods) {
99     if (exists $saved_idx{$_}) {
100      $#{$mods{$_}} = $saved_idx{$_};
101     } else {
102      delete $mods{$_};
103     }
104    }
105   });
106
107   my @mods = $set->mods_unique;
108
109   my $body = $set->$orig($tikz);
110
111   if (@mods) {
112    $body = LaTeX::TikZ::Scope->new
113                              ->mod(map $_->apply($tikz), @mods)
114                              ->body($body);
115   }
116
117   $body;
118  };
119 }
120
121 sub layer {
122  return $_[0] unless @_ > 1;
123
124  my $layer = $_[1];
125
126  $_[0]->mod(
127   $ltml_tc->check($layer) ? $layer
128                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
129  )
130 }
131
132 sub clip {
133  return $_[0] unless @_ > 1;
134
135  $_[0]->mod(
136   map {
137    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
138   } @_[1 .. $#_]
139  )
140 }
141
142 =head1 AUTHOR
143
144 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
145
146 You can contact me by mail or on C<irc.perl.org> (vincent).
147
148 =head1 BUGS
149
150 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>.
151 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
152
153 =head1 SUPPORT
154
155 You can find documentation for this module with the perldoc command.
156
157     perldoc LaTeX::TikZ
158
159 =head1 COPYRIGHT & LICENSE
160
161 Copyright 2010 Vincent Pit, all rights reserved.
162
163 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
164
165 =cut
166
167 1; # End of LaTeX::TikZ::Set