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