]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - t/22-clip.t
0b0ac328ddfca94b8bd3ea9e1446650cc9ab6198
[perl/modules/LaTeX-TikZ.git] / t / 22-clip.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 8 + 2 * 16;
7
8 use LaTeX::TikZ;
9
10 my $tikz = Tikz->formatter(
11  format => '%d',
12 );
13
14 sub check {
15  my ($set, $desc, $exp) = @_;
16
17  local $Test::Builder::Level = $Test::Builder::Level + 1;
18
19  my ($head, $decl, $body) = eval {
20   $tikz->render(ref $set eq 'ARRAY' ? @$set : $set);
21  };
22  is $@, '', "$desc: no error";
23
24  unless (ref $exp eq 'ARRAY') {
25   $exp = [ split /\n/, $exp ];
26  }
27  unshift @$exp, '\begin{tikzpicture}';
28  push    @$exp, '\end{tikzpicture}';
29
30  is_deeply $body, $exp, $desc;
31 }
32
33 my $clip1 = Tikz->raw('clip1');
34 my $clip2 = Tikz->raw('clip2');
35
36 my $foo = eval {
37  Tikz->raw('foo')
38      ->clip($clip1);
39 };
40 is $@, '', 'creating a clipping a raw path with ->clip doesn\'t croak';
41
42 check $foo, 'one clipped raw set', <<'RES';
43 \begin{scope}
44 \clip clip1 ;
45 \draw foo ;
46 \end{scope}
47 RES
48
49 my $bar = eval {
50  Tikz->raw('bar')
51      ->mod(Tikz->clip(Tikz->raw('clip1')));
52 };
53 is $@, '', 'creating a clipping a raw path with ->mod doesn\'t croak';
54
55 check $bar, 'another clipped raw set', <<'RES';
56 \begin{scope}
57 \clip clip1 ;
58 \draw bar ;
59 \end{scope}
60 RES
61
62 my $seq = Tikz->seq($foo, $bar);
63
64 check $seq, 'mods folding with clips 1', <<'RES';
65 \begin{scope}
66 \clip clip1 ;
67 \draw foo ;
68 \draw bar ;
69 \end{scope}
70 RES
71
72 my $baz = Tikz->raw('baz')
73               ->clip($clip2);
74
75 check Tikz->seq($seq, $baz), 'mods folding with clips 2', <<'RES';
76 \begin{scope}
77 \clip clip1 ;
78 \draw foo ;
79 \draw bar ;
80 \end{scope}
81 \begin{scope}
82 \clip clip2 ;
83 \draw baz ;
84 \end{scope}
85 RES
86
87 check Tikz->seq($baz, $seq), 'mods folding with clips 3', <<'RES';
88 \begin{scope}
89 \clip clip2 ;
90 \draw baz ;
91 \end{scope}
92 \begin{scope}
93 \clip clip1 ;
94 \draw foo ;
95 \draw bar ;
96 \end{scope}
97 RES
98
99 my $seq2 = Tikz->seq($seq, $baz)
100                ->clip($clip1);
101
102 check $seq2, 'mods folding with clips 4', <<'RES';
103 \begin{scope}
104 \clip clip1 ;
105 \draw foo ;
106 \draw bar ;
107 \begin{scope}
108 \clip clip2 ;
109 \draw baz ;
110 \end{scope}
111 \end{scope}
112 RES
113
114 $seq2 = Tikz->seq($seq, $baz)
115             ->clip($clip2);
116
117 check $seq2, 'mods folding with clips 5', <<'RES';
118 \begin{scope}
119 \clip clip2 ;
120 \begin{scope}
121 \clip clip1 ;
122 \draw foo ;
123 \draw bar ;
124 \end{scope}
125 \draw baz ;
126 \end{scope}
127 RES
128
129 $seq2->clip($clip1);
130
131 check $seq2, 'mods folding with clips 6', <<'RES';
132 \begin{scope}
133 \clip clip2 ;
134 \begin{scope}
135 \clip clip1 ;
136 \draw foo ;
137 \draw bar ;
138 \draw baz ;
139 \end{scope}
140 \end{scope}
141 RES
142
143 $seq2->mod(Tikz->color('red'));
144
145 check $seq2, 'mods folding with clips 7', <<'RES';
146 \begin{scope} [color=red]
147 \clip clip2 ;
148 \begin{scope}
149 \clip clip1 ;
150 \draw foo ;
151 \draw bar ;
152 \draw baz ;
153 \end{scope}
154 \end{scope}
155 RES
156
157 $seq2->layer('top');
158 $seq->layer('bottom');
159
160 check $seq2, 'mods folding with clips and layers', <<'RES';
161 \begin{pgfonlayer}{top}
162 \begin{scope} [color=red]
163 \clip clip2 ;
164 \begin{scope}
165 \clip clip1 ;
166 \begin{pgfonlayer}{bottom}
167 \begin{scope} [color=red]
168 \clip clip2 ;
169 \begin{scope}
170 \clip clip1 ;
171 \draw foo ;
172 \draw bar ;
173 \end{scope}
174 \end{scope}
175 \end{pgfonlayer}
176 \draw baz ;
177 \end{scope}
178 \end{scope}
179 \end{pgfonlayer}
180 RES
181
182 my $a = Tikz->point;
183 my $b = Tikz->point(4, 2);
184 my $c = Tikz->point(1, 3);
185 my $d = Tikz->point(2, 1);
186
187 my $r1 = Tikz->rectangle($a, $b);
188 my $r2 = Tikz->rectangle($c, $d);
189
190 $seq = eval {
191  Tikz->seq(
192   Tikz->raw("foo")
193        ->clip($r1)
194  )->clip($r2);
195 };
196 is $@, '', 'two intersecting rectangle clips doesn\'t croak';
197
198 check $seq, 'two intersecting rectangle clips', <<'RES';
199 \begin{scope}
200 \clip (1cm,3cm) rectangle (2cm,1cm) ;
201 \begin{scope}
202 \clip (0cm,0cm) rectangle (4cm,2cm) ;
203 \draw foo ;
204 \end{scope}
205 \end{scope}
206 RES
207
208 $r2 = Tikz->rectangle($a, $d); # $r2 is a subset of $r1
209
210 $seq = eval {
211  Tikz->seq(
212   Tikz->raw("foo")
213        ->clip($r1)
214  )->clip($r2);
215 };
216 is $@, '', 'two overlapping rectangle clips 1 doesn\'t croak';
217
218 check $seq, 'two overlapping rectangle clips 1', <<'RES';
219 \begin{scope}
220 \clip (0cm,0cm) rectangle (2cm,1cm) ;
221 \draw foo ;
222 \end{scope}
223 RES
224
225 $seq = eval {
226  Tikz->seq(
227   Tikz->raw("foo")
228        ->clip($r2)
229  )->clip($r1);
230 };
231 is $@, '', 'two overlapping rectangle clips 2 doesn\'t croak';
232
233 check $seq, 'two overlapping rectangle clips 2', <<'RES';
234 \begin{scope}
235 \clip (0cm,0cm) rectangle (4cm,2cm) ;
236 \begin{scope}
237 \clip (0cm,0cm) rectangle (2cm,1cm) ;
238 \draw foo ;
239 \end{scope}
240 \end{scope}
241 RES
242
243 my $c1 = Tikz->circle($a, 2);
244 my $c2 = Tikz->circle($d, 3);
245
246 $seq = eval {
247  Tikz->seq(
248   Tikz->raw("foo")
249        ->clip($c1)
250  )->clip($c2);
251 };
252 is $@, '', 'two intersecting circle clips doesn\'t croak';
253
254 check $seq, 'two intersecting circle clips', <<'RES';
255 \begin{scope}
256 \clip (2cm,1cm) circle (3cm) ;
257 \begin{scope}
258 \clip (0cm,0cm) circle (2cm) ;
259 \draw foo ;
260 \end{scope}
261 \end{scope}
262 RES
263
264 $c2 = Tikz->circle($a, 1); # $c2 is a subset of $c1
265
266 $seq = eval {
267  Tikz->seq(
268   Tikz->raw("foo")
269        ->clip($c1)
270  )->clip($c2);
271 };
272 is $@, '', 'two overlapping circle clips 1 doesn\'t croak';
273
274 check $seq, 'two overlapping circle clips 1', <<'RES';
275 \begin{scope}
276 \clip (0cm,0cm) circle (1cm) ;
277 \draw foo ;
278 \end{scope}
279 RES
280
281 $seq = eval {
282  Tikz->seq(
283   Tikz->raw("foo")
284        ->clip($c2)
285  )->clip($c1);
286 };
287 is $@, '', 'two overlapping circle clips 2 doesn\'t croak';
288
289 check $seq, 'two overlapping circle clips 2', <<'RES';
290 \begin{scope}
291 \clip (0cm,0cm) circle (2cm) ;
292 \begin{scope}
293 \clip (0cm,0cm) circle (1cm) ;
294 \draw foo ;
295 \end{scope}
296 \end{scope}
297 RES