]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Clip.pm
Remove magic LaTeX::TikZ::Interface->import
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Mod / Clip.pm
1 package LaTeX::TikZ::Mod::Clip;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Mod::Clip - A modifier that clips sequences with a path.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Sub::Name ();
19
20 use LaTeX::TikZ::Formatter;
21 use LaTeX::TikZ::Mod::Formatted;
22
23 use LaTeX::TikZ::Interface;
24 use LaTeX::TikZ::Functor;
25
26 use LaTeX::TikZ::Tools;
27
28 use Any::Moose;
29
30 with 'LaTeX::TikZ::Mod';
31
32 has clip => (
33  is       => 'ro',
34  does     => 'LaTeX::TikZ::Set::Op',
35  required => 1,
36 );
37
38 my $default_formatter = LaTeX::TikZ::Formatter->new(
39  unit   => 'cm',
40  format => '%.07f',
41  scale  => 1,
42 );
43
44 sub tag { ref $_[0] }
45
46 my $get_tc = do {
47  my %tc;
48
49  Sub::Name::subname('get_tc' => sub {
50   my ($class) = @_;
51
52   return $tc{$class} if exists $tc{class};
53
54   my $tc = LaTeX::TikZ::Tools::type_constraint($class);
55   return unless defined $tc;
56
57   $tc{$class} ||= $tc;
58  })
59 };
60
61 my $cover_rectangle = Sub::Name::subname('cover_rectangle' => sub {
62  my ($old, $new, $self_tc) = @_;
63
64  my $p = $new->from;
65  my $q = $new->to;
66
67  my $x = $p->x;
68  my $y = $p->y;
69  my $X = $q->x;
70  my $Y = $q->y;
71
72  ($x, $X) = ($X, $x) if $x > $X;
73  ($y, $Y) = ($Y, $y) if $y > $Y;
74
75  if ($self_tc->check($old)) {
76   # The old rectangle covers the new one if and only if it's inside the new.
77
78   for ($old->from, $old->to) {
79    my $r = $_->x;
80    return 0 if LaTeX::TikZ::Tools::numcmp($r, $x) < 0
81             or LaTeX::TikZ::Tools::numcmp($X, $r) < 0;
82    my $i = $_->y;
83    return 0 if LaTeX::TikZ::Tools::numcmp($i, $y) < 0
84             or LaTeX::TikZ::Tools::numcmp($Y, $i) < 0;
85   }
86
87   return 1;
88  }
89
90  return 0;
91 });
92
93 my $cover_circle = Sub::Name::subname('cover_circle' => sub {
94  my ($old, $new, $self_tc) = @_;
95
96  my $c2 = $new->center;
97  my $r2 = $new->radius;
98
99  if ($self_tc->check($old)) {
100   # The old circle covers the new one if and only if it's inside the new.
101
102   my $c1 = $old->center;
103   my $r1 = $old->radius;
104
105   my $d = abs($c1 - $c2);
106
107   return    LaTeX::TikZ::Tools::numcmp($d, $r2)       <= 0
108          && LaTeX::TikZ::Tools::numcmp($d + $r1, $r2) <= 0;
109  }
110
111  return 0;
112 });
113
114 my @handlers = (
115  [ 'LaTeX::TikZ::Set::Rectangle' => $cover_rectangle ],
116  [ 'LaTeX::TikZ::Set::Circle'    => $cover_circle    ],
117 );
118
119 sub cover {
120  my ($old, $new) = map $_->clip, @_[0, 1];
121
122  for (@handlers) {
123   my $tc = $get_tc->($_->[0]);
124   next unless defined $tc and $tc->check($new);
125   return $_->[1]->($old, $new, $tc);
126  }
127
128  $old->path($default_formatter) eq $new->path($default_formatter);
129 }
130
131 sub declare { }
132
133 sub apply {
134  my ($self) = @_;
135
136  LaTeX::TikZ::Mod::Formatted->new(
137   type    => 'clip',
138   content => $_[0]->clip->path($_[1]),
139  )
140 }
141
142 LaTeX::TikZ::Interface->register(
143  clip => sub {
144   shift;
145
146   __PACKAGE__->new(clip => $_[0]);
147  },
148 );
149
150 LaTeX::TikZ::Functor->default_rule(
151  (__PACKAGE__) => sub {
152   my ($functor, $mod, @args) = @_;
153   $mod->new(clip => $mod->clip->$functor(@args))
154  }
155 );
156
157 __PACKAGE__->meta->make_immutable;
158
159 =head1 AUTHOR
160
161 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
162
163 You can contact me by mail or on C<irc.perl.org> (vincent).
164
165 =head1 BUGS
166
167 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>.
168 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
169
170 =head1 SUPPORT
171
172 You can find documentation for this module with the perldoc command.
173
174     perldoc LaTeX::TikZ
175
176 =head1 COPYRIGHT & LICENSE
177
178 Copyright 2010 Vincent Pit, all rights reserved.
179
180 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
181
182 =cut
183
184 1; # End of LaTeX::TikZ::Mod::Clip