]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
075dcb88b9ed88f48a29794a475df6aaeb507fc8
[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 Mouse::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    mods => [ map $_->apply($tikz), @mods ],
99    body => $body,
100   );
101  }
102
103  $body;
104 };
105
106 =head2 C<layer $layer>
107
108 Puts the current set in the corresponding layer.
109 This is a shortcut for C<< $set->mod(Tikz->layer($layer)) >>.
110
111 =cut
112
113 sub layer {
114  my $set = shift;
115
116  return $set unless @_;
117
118  my $layer = $_[0];
119  $set->mod(
120   $ltml_tc->check($layer) ? $layer
121                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
122  )
123 }
124
125 =head2 C<clip $path>
126
127 Clips the current set by the path given by C<$path>.
128 This is a shortcut for C<< $set->mod(Tikz->clip($path)) >>.
129
130 =cut
131
132 sub clip {
133  my $set = shift;
134
135  return $set unless @_;
136
137  $set->mod(
138   map {
139    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
140   } @_
141  )
142 }
143
144 =head1 SEE ALSO
145
146 L<LaTeX::TikZ>.
147
148 =head1 AUTHOR
149
150 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
151
152 You can contact me by mail or on C<irc.perl.org> (vincent).
153
154 =head1 BUGS
155
156 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>.
157 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
158
159 =head1 SUPPORT
160
161 You can find documentation for this module with the perldoc command.
162
163     perldoc LaTeX::TikZ
164
165 =head1 COPYRIGHT & LICENSE
166
167 Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
168
169 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
170
171 =cut
172
173 1; # End of LaTeX::TikZ::Set