]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
e673097ec2f20e7a9349e8cabff8cdef3cfb84c3
[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.02
13
14 =cut
15
16 our $VERSION = '0.02';
17
18 use LaTeX::TikZ::Context;
19 use LaTeX::TikZ::Scope;
20
21 use LaTeX::TikZ::Tools;
22
23 use Any::Moose 'Role';
24
25 =head1 ATTRIBUTES
26
27 =head2 C<mods>
28
29 Returns the list of the L<LaTeX::TikZ::Mod> objects associated with the current set.
30
31 =cut
32
33 has '_mods' => (
34  is       => 'ro',
35  isa      => 'Maybe[ArrayRef[LaTeX::TikZ::Mod]]',
36  init_arg => 'mods',
37  default  => sub { [ ] },
38  lazy     => 1,
39 );
40
41 sub mods { @{$_[0]->_mods} }
42
43 =head1 METHODS
44
45 This method is required by the interface :
46
47 =over 4
48
49 =item *
50
51 C<draw $formatter, $context>
52
53 Returns an array reference of TikZ code lines required to effectively draw the current set object, formatted by the L<LaTeX::TikZ::Formatter> object C<$formatter>.
54 The current evaluation context is passed as the L<LaTeX::TikZ::Context> object C<$context>.
55
56 =back
57
58 =cut
59
60 requires qw<
61  draw
62 >;
63
64 =head2 C<mod @mods>
65
66 Apply the given list of L<LaTeX::TikZ::Mod> objects to the current set.
67
68 =cut
69
70 my $ltm_tc  = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod');
71 my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer');
72 my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip');
73
74 sub mod {
75  my $set = shift;
76
77  my @mods = map $ltm_tc->coerce($_), @_;
78  $ltm_tc->assert_valid($_) for @mods;
79
80  push @{$set->_mods}, @mods;
81
82  $set;
83 }
84
85 around 'draw' => sub {
86  my ($orig, $set, $tikz, $pcxt) = @_;
87
88  my $cxt = LaTeX::TikZ::Context->new(
89   parent => $pcxt,
90   mods   => [ $set->mods ],
91  );
92
93  my $body = $set->$orig($tikz, $cxt);
94
95  my @mods = $cxt->effective_mods;
96  if (@mods) {
97   $body = LaTeX::TikZ::Scope->new
98                             ->mod(map $_->apply($tikz), @mods)
99                             ->body($body);
100  }
101
102  $body;
103 };
104
105 =head2 C<layer $layer>
106
107 Puts the current set in the corresponding layer.
108 This is a shortcut for C<< $set->mod(Tikz->layer($layer)) >>.
109
110 =cut
111
112 sub layer {
113  my $set = shift;
114
115  return $set unless @_;
116
117  my $layer = $_[0];
118  $set->mod(
119   $ltml_tc->check($layer) ? $layer
120                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
121  )
122 }
123
124 =head2 C<clip $path>
125
126 Clips the current set by the path given by C<$path>.
127 This is a shortcut for C<< $set->mod(Tikz->clip($path)) >>.
128
129 =cut
130
131 sub clip {
132  my $set = shift;
133
134  return $set unless @_;
135
136  $set->mod(
137   map {
138    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
139   } @_
140  )
141 }
142
143 =head1 SEE ALSO
144
145 L<LaTeX::TikZ>.
146
147 =head1 AUTHOR
148
149 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
150
151 You can contact me by mail or on C<irc.perl.org> (vincent).
152
153 =head1 BUGS
154
155 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>.
156 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
157
158 =head1 SUPPORT
159
160 You can find documentation for this module with the perldoc command.
161
162     perldoc LaTeX::TikZ
163
164 =head1 COPYRIGHT & LICENSE
165
166 Copyright 2010 Vincent Pit, all rights reserved.
167
168 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
169
170 =cut
171
172 1; # End of LaTeX::TikZ::Set