]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
78b53ce2ee853c9ac17f738b939a29596d49e2d3
[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  $ltm_tc->assert_valid($_) for @_;
48
49  push @{$set->_mods}, @_;
50
51  $set;
52 }
53
54 {
55  our %mods;
56  our $last_mod = 0;
57
58  sub mods_unique {
59   my ($set) = @_;
60
61   my (@mods, $last_layer);
62 MOD:
63   for my $mod ($set->mods) {
64    my $is_layer = $ltml_tc->check($mod);
65    $last_layer  = $mod if $is_layer;
66    my $tag = $mod->tag;
67    my $old = $mods{$tag} || [];
68    for (@$old) {
69     next MOD if $_->[0]->cover($mod);
70    }
71    push @{$mods{$tag}}, [ $mod, $last_mod++, $is_layer ];
72    push @mods,          $mod;
73   }
74
75   if ($last_layer) {
76    # Clips and mods don't propagate through layers. Hence if a layer is set,
77    # force their reuse.
78    @mods = $last_layer;
79    push @mods, map $_->[0],
80                 sort { $a->[1] <=> $b->[1] }
81                  grep !$_->[2],
82                   map @$_,
83                    values %mods;
84   }
85
86   return @mods;
87  }
88
89  around 'draw' => sub {
90   my ($orig, $set, $tikz) = @_;
91
92   local $last_mod = $last_mod;
93
94   # Save a deep copy
95   my %saved_idx = map { $_ => $#{$mods{$_}} } keys %mods;
96   my $guard     = Scope::Guard->new(sub {
97    for (keys %mods) {
98     if (exists $saved_idx{$_}) {
99      $#{$mods{$_}} = $saved_idx{$_};
100     } else {
101      delete $mods{$_};
102     }
103    }
104   });
105
106   my @mods = $set->mods_unique;
107
108   my $body = $set->$orig($tikz);
109
110   if (@mods) {
111    $body = LaTeX::TikZ::Scope->new
112                              ->mod(map $_->apply($tikz), @mods)
113                              ->body($body);
114   }
115
116   $body;
117  };
118 }
119
120 sub layer {
121  return $_[0] unless @_ > 1;
122
123  my $layer = $_[1];
124
125  $_[0]->mod(
126   $ltml_tc->check($layer) ? $layer
127                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
128  )
129 }
130
131 sub clip {
132  return $_[0] unless @_ > 1;
133
134  $_[0]->mod(
135   map {
136    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
137   } @_[1 .. $#_]
138  )
139 }
140
141 =head1 AUTHOR
142
143 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
144
145 You can contact me by mail or on C<irc.perl.org> (vincent).
146
147 =head1 BUGS
148
149 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>.
150 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
151
152 =head1 SUPPORT
153
154 You can find documentation for this module with the perldoc command.
155
156     perldoc LaTeX::TikZ
157
158 =head1 COPYRIGHT & LICENSE
159
160 Copyright 2010 Vincent Pit, all rights reserved.
161
162 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
163
164 =cut
165
166 1; # End of LaTeX::TikZ::Set