1 package LaTeX::TikZ::Mod::Clip;
8 LaTeX::TikZ::Mod::Clip - A modifier that clips sequences with a path.
16 our $VERSION = '0.02';
20 use LaTeX::TikZ::Formatter;
21 use LaTeX::TikZ::Mod::Formatted;
23 use LaTeX::TikZ::Interface;
24 use LaTeX::TikZ::Functor;
26 use LaTeX::TikZ::Tools;
32 This class consumes the L<LaTeX::TikZ::Mod> role, and as such implements the L</tag>, L</covers>, L</declare> and L</apply> methods.
36 with 'LaTeX::TikZ::Mod';
42 The path that specifies the clipped area.
48 does => 'LaTeX::TikZ::Set::Path',
52 my $default_formatter = LaTeX::TikZ::Formatter->new(
73 Sub::Name::subname('get_tc' => sub {
76 return $tc{$class} if exists $tc{class};
78 my $tc = LaTeX::TikZ::Tools::type_constraint($class);
79 return unless defined $tc;
85 my $cover_rectangle = Sub::Name::subname('cover_rectangle' => sub {
86 my ($old, $new, $self_tc) = @_;
96 ($x, $X) = ($X, $x) if $x > $X;
97 ($y, $Y) = ($Y, $y) if $y > $Y;
99 if ($self_tc->check($old)) {
100 # The old rectangle covers the new one if and only if it's inside the new.
102 for ($old->from, $old->to) {
104 return 0 if LaTeX::TikZ::Tools::numcmp($r, $x) < 0
105 or LaTeX::TikZ::Tools::numcmp($X, $r) < 0;
107 return 0 if LaTeX::TikZ::Tools::numcmp($i, $y) < 0
108 or LaTeX::TikZ::Tools::numcmp($Y, $i) < 0;
117 my $cover_circle = Sub::Name::subname('cover_circle' => sub {
118 my ($old, $new, $self_tc) = @_;
120 my $c2 = $new->center;
121 my $r2 = $new->radius;
123 if ($self_tc->check($old)) {
124 # The old circle covers the new one if and only if it's inside the new.
126 my $c1 = $old->center;
127 my $r1 = $old->radius;
129 my $d = abs($c1 - $c2);
131 return LaTeX::TikZ::Tools::numcmp($d, $r2) <= 0
132 && LaTeX::TikZ::Tools::numcmp($d + $r1, $r2) <= 0;
139 [ 'LaTeX::TikZ::Set::Rectangle' => $cover_rectangle ],
140 [ 'LaTeX::TikZ::Set::Circle' => $cover_circle ],
144 my ($old, $new) = map $_->clip, @_[0, 1];
147 my $tc = $get_tc->($_->[0]);
148 next unless defined $tc and $tc->check($new);
149 return $_->[1]->($old, $new, $tc);
152 $old->path($default_formatter) eq $new->path($default_formatter);
168 LaTeX::TikZ::Mod::Formatted->new(
170 content => $_[0]->clip->path($_[1]),
174 LaTeX::TikZ::Interface->register(
178 __PACKAGE__->new(clip => $_[0]);
182 LaTeX::TikZ::Functor->default_rule(
183 (__PACKAGE__) => sub {
184 my ($functor, $mod, @args) = @_;
185 $mod->new(clip => $mod->clip->$functor(@args))
189 __PACKAGE__->meta->make_immutable;
193 L<LaTeX::TikZ>, L<LaTeX::TikZ::Mod>.
197 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
199 You can contact me by mail or on C<irc.perl.org> (vincent).
203 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>.
204 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
208 You can find documentation for this module with the perldoc command.
212 =head1 COPYRIGHT & LICENSE
214 Copyright 2010 Vincent Pit, all rights reserved.
216 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
220 1; # End of LaTeX::TikZ::Mod::Clip