]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Set.pm
Make sure POD headings are linkable
[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>
65
66     $set->mod(@mods)
67
68 Apply the given list of L<LaTeX::TikZ::Mod> objects to the current set.
69
70 =cut
71
72 my $ltm_tc  = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod');
73 my $ltml_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Layer');
74 my $ltmc_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Mod::Clip');
75
76 sub mod {
77  my $set = shift;
78
79  my @mods = map $ltm_tc->coerce($_), @_;
80  $ltm_tc->assert_valid($_) for @mods;
81
82  push @{$set->_mods}, @mods;
83
84  $set;
85 }
86
87 around 'draw' => sub {
88  my ($orig, $set, $tikz, $pcxt) = @_;
89
90  my $cxt = LaTeX::TikZ::Context->new(
91   parent => $pcxt,
92   mods   => [ $set->mods ],
93  );
94
95  my $body = $set->$orig($tikz, $cxt);
96
97  my @mods = $cxt->effective_mods;
98  if (@mods) {
99   $body = LaTeX::TikZ::Scope->new(
100    mods => [ map $_->apply($tikz), @mods ],
101    body => $body,
102   );
103  }
104
105  $body;
106 };
107
108 =head2 C<layer>
109
110     $set->layer($layer)
111
112 Puts the current set in the corresponding layer.
113 This is a shortcut for C<< $set->mod(Tikz->layer($layer)) >>.
114
115 =cut
116
117 sub layer {
118  my $set = shift;
119
120  return $set unless @_;
121
122  my $layer = $_[0];
123  $set->mod(
124   $ltml_tc->check($layer) ? $layer
125                           : LaTeX::TikZ::Mod::Layer->new(name => $layer)
126  )
127 }
128
129 =head2 C<clip>
130
131     $set->clip($path)
132
133 Clips the current set by the path given by C<$path>.
134 This is a shortcut for C<< $set->mod(Tikz->clip($path)) >>.
135
136 =cut
137
138 sub clip {
139  my $set = shift;
140
141  return $set unless @_;
142
143  $set->mod(
144   map {
145    $ltmc_tc->check($_) ? $_ : LaTeX::TikZ::Mod::Clip->new(clip => $_)
146   } @_
147  )
148 }
149
150 =head1 SEE ALSO
151
152 L<LaTeX::TikZ>.
153
154 =head1 AUTHOR
155
156 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
157
158 You can contact me by mail or on C<irc.perl.org> (vincent).
159
160 =head1 BUGS
161
162 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>.
163 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
164
165 =head1 SUPPORT
166
167 You can find documentation for this module with the perldoc command.
168
169     perldoc LaTeX::TikZ
170
171 =head1 COPYRIGHT & LICENSE
172
173 Copyright 2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
174
175 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
176
177 =cut
178
179 1; # End of LaTeX::TikZ::Set