]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
First cut at the documentation
[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 =head1 ATTRIBUTES
27
28 =head2 C<mods>
29
30 Returns the list of the L<LaTeX::TikZ::Mod> objects associated with the current set.
31
32 =cut
33
34 has '_mods' => (
35  is       => 'ro',
36  isa      => 'Maybe[ArrayRef[LaTeX::TikZ::Mod]]',
37  init_arg => 'mods',
38  default  => sub { [ ] },
39  lazy     => 1,
40 );
41
42 sub mods { @{$_[0]->_mods} }
43
44 =head1 METHODS
45
46 This method is required by the interface :
47
48 =over 4
49
50 =item *
51
52 C<draw>
53
54 =back
55
56 =cut
57
58 requires qw(
59  draw
60 );
61
62 =head2 C<mod @mods>
63
64 Apply the given list of L<LaTeX::TikZ::Mod> objects to the current set.
65
66 =cut
67
68 my $ltm_tc  = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod');
69 my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer');
70 my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip');
71
72 sub mod {
73  my $set = shift;
74
75  my @mods = map $ltm_tc->coerce($_), @_;
76  $ltm_tc->assert_valid($_) for @mods;
77
78  push @{$set->_mods}, @mods;
79
80  $set;
81 }
82
83 {
84  our %mods;
85  our $last_mod = 0;
86
87  around 'draw' => sub {
88   my ($orig, $set, $tikz) = @_;
89
90   local $last_mod = $last_mod;
91
92   # Save a deep copy
93   my %saved_idx = map { $_ => $#{$mods{$_}} } keys %mods;
94   my $guard     = Scope::Guard->new(sub {
95    for (keys %mods) {
96     if (exists $saved_idx{$_}) {
97      $#{$mods{$_}} = $saved_idx{$_};
98     } else {
99      delete $mods{$_};
100     }
101    }
102   });
103
104   my (@mods, $last_layer);
105 MOD:
106   for my $mod ($set->mods) {
107    my $is_layer = $ltml_tc->check($mod);
108    $last_layer  = $mod if $is_layer;
109    my $tag = $mod->tag;
110    my $old = $mods{$tag} || [];
111    for (@$old) {
112     next MOD if $_->[0]->cover($mod);
113    }
114    push @{$mods{$tag}}, [ $mod, $last_mod++, $is_layer ];
115    push @mods,          $mod;
116   }
117
118   if ($last_layer) {
119    # Clips and mods don't propagate through layers. Hence if a layer is set,
120    # force their reuse.
121    @mods = $last_layer;
122    push @mods, map $_->[0],
123                 sort { $a->[1] <=> $b->[1] }
124                  grep !$_->[2],
125                   map @$_,
126                    values %mods;
127   }
128
129   my $body = $set->$orig($tikz);
130
131   if (@mods) {
132    $body = LaTeX::TikZ::Scope->new
133                              ->mod(map $_->apply($tikz), @mods)
134                              ->body($body);
135   }
136
137   $body;
138  };
139 }
140
141 =head2 C<layer $layer>
142
143 Puts the current set in the corresponding layer.
144 This is a shortcut for C<< $set->mod(Tikz->layer($layer)) >>.
145
146 =cut
147
148 sub layer {
149  return $_[0] unless @_ > 1;
150
151  my $layer = $_[1];
152
153  $_[0]->mod(
154   $ltml_tc->check($layer) ? $layer
155                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
156  )
157 }
158
159 =head2 C<clip $path>
160
161 Clips the current set by the path given by C<$path>.
162 This is a shortcut for C<< $set->mod(Tikz->clip($path)) >>.
163
164 =cut
165
166 sub clip {
167  return $_[0] unless @_ > 1;
168
169  $_[0]->mod(
170   map {
171    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
172   } @_[1 .. $#_]
173  )
174 }
175
176 =head1 AUTHOR
177
178 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
179
180 You can contact me by mail or on C<irc.perl.org> (vincent).
181
182 =head1 BUGS
183
184 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>.
185 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
186
187 =head1 SUPPORT
188
189 You can find documentation for this module with the perldoc command.
190
191     perldoc LaTeX::TikZ
192
193 =head1 COPYRIGHT & LICENSE
194
195 Copyright 2010 Vincent Pit, all rights reserved.
196
197 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
198
199 =cut
200
201 1; # End of LaTeX::TikZ::Set