]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Clip.pm
Initial commit
[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::Tools;
24
25 use Any::Moose;
26
27 with 'LaTeX::TikZ::Mod';
28
29 has clip => (
30  is       => 'ro',
31  isa      => 'LaTeX::TikZ::Set::Op',
32  required => 1,
33 );
34
35 my $default_formatter = LaTeX::TikZ::Formatter->new(
36  unit   => 'cm',
37  format => '%.07f',
38  scale  => 1,
39 );
40
41 sub tag { ref $_[0] }
42
43 my $get_tc = do {
44  my %tc;
45
46  Sub::Name::subname('get_tc' => sub {
47   my ($class) = @_;
48
49   return $tc{$class} if exists $tc{class};
50
51   my $tc = LaTeX::TikZ::Tools::type_constraint($class);
52   return unless defined $tc;
53
54   $tc{$class} ||= $tc;
55  })
56 };
57
58 my $cover_rectangle = Sub::Name::subname('cover_rectangle' => sub {
59  my ($old, $new, $self_tc) = @_;
60
61  my $p = $new->from;
62  my $q = $new->to;
63
64  my $x = $p->x;
65  my $y = $p->y;
66  my $X = $q->x;
67  my $Y = $q->y;
68
69  ($x, $X) = ($X, $x) if $x > $X;
70  ($y, $Y) = ($Y, $y) if $y > $Y;
71
72  if ($self_tc->check($old)) {
73   # The old rectangle covers the new one if and only if it's inside the new.
74
75   for ($old->from, $old->to) {
76    my $r = $_->x;
77    return 0 if LaTeX::TikZ::Tools::numcmp($r, $x) < 0
78             or LaTeX::TikZ::Tools::numcmp($X, $r) < 0;
79    my $i = $_->y;
80    return 0 if LaTeX::TikZ::Tools::numcmp($i, $y) < 0
81             or LaTeX::TikZ::Tools::numcmp($Y, $i) < 0;
82   }
83
84   return 1;
85  }
86
87  return 0;
88 });
89
90 my $cover_circle = Sub::Name::subname('cover_circle' => sub {
91  my ($old, $new, $self_tc) = @_;
92
93  my $c2 = $new->center;
94  my $r2 = $new->radius;
95
96  if ($self_tc->check($old)) {
97   # The old circle covers the new one if and only if it's inside the new.
98
99   my $c1 = $old->center;
100   my $r1 = $old->radius;
101
102   my $d = abs($c1 - $c2);
103
104   return    LaTeX::TikZ::Tools::numcmp($d, $r2)       <= 0
105          && LaTeX::TikZ::Tools::numcmp($d + $r1, $r2) <= 0;
106  }
107
108  return 0;
109 });
110
111 my @handlers = (
112  [ 'LaTeX::TikZ::Set::Rectangle' => $cover_rectangle ],
113  [ 'LaTeX::TikZ::Set::Circle'    => $cover_circle    ],
114 );
115
116 sub cover {
117  my ($old, $new) = map $_->clip, @_[0, 1];
118
119  for (@handlers) {
120   my $tc = $get_tc->($_->[0]);
121   next unless defined $tc and $tc->check($new);
122   return $_->[1]->($old, $new, $tc);
123  }
124
125  $old->path($default_formatter) eq $new->path($default_formatter);
126 }
127
128 sub declare { }
129
130 sub apply {
131  my ($self) = @_;
132
133  LaTeX::TikZ::Mod::Formatted->new(
134   type    => 'clip',
135   content => $_[0]->clip->path($_[1]),
136  )
137 }
138
139 use LaTeX::TikZ::API clip => sub {
140  shift;
141
142  __PACKAGE__->new(clip => $_[0]);
143 };
144
145 __PACKAGE__->meta->make_immutable;
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::Mod::Clip