Drawable.pm

filter: Statementstmt 14.7% 51 / 345 Branchbran 8.4% 15 / 178 Conditioncond 15.8% 16 / 101 MC/DCmcdc 15.3% 8 / 52 Subroutinesub 28.5% 12 / 42 Podpod 69.2% 9 / 13 totaltotal 15.1% 111 / 731 CC 127 SCAR 45.2
CC
127
Cov
13%
Image_Base_Other_rectangles
306.0
xy
156.0
rectangle
156.0
CRAP 92.2
« Window.pm ↑ Summary Pixmap.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2017 Kevin Ryde
2  
3 # This file is part of Image-Base-X11-Protocol.
4 #
5 # Image-Base-X11-Protocol is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Image-Base-X11-Protocol is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-X11-Protocol. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # /usr/share/doc/x11proto-core-dev/x11protocol.txt.gz
20 #
21  
22 package Image::Base::X11::Protocol::Drawable;
23 4 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use POSIX 'floor';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use X11::Protocol 0.56; # version 0.56 for robust_req() fix
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use X11::Protocol::Other 3; # v.3 for hexstr_to_rgb()
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use vars '@ISA', '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 ▶use Image::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 @ISA = ('Image::Base');
33  
34 4 $VERSION = 15;
35  
36 # uncomment this to run the ### lines
37 # use Smart::Comments '###';
38  
39 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
40 0 my $class = shift;
41 0 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 0 0
42 0 croak "Cannot clone base drawable";
43 }
44 0 return bless {
45 # these not documented as yet
46 -colour_to_pixel => { },
47 -gc_colour => '',
48 -gc_pixel => -1,
49  
50 @_ }, $class;
51 }
52  
53 # This not working yet. Good to CopyArea when screen,depth,colormap permit,
54 # is it worth the trouble though?
55 #
56 # =item C<$new_image = $image-E<gt>new_from_image ($class, key=E<gt>value,...)>
57 #
58 # Create and return a new image of type C<$class>.
59 #
60 # Target class C<Image::Base::X11::Protocol::Pixmap> is recognised and done by
61 # CopyArea of the C<$image> drawable into the new pixmap. Other classes are
62 # left to the plain C<Image::Base> C<new_from_image>.
63 #
64 # sub new_from_image {
65 # my $self = shift;
66 # my $new_class = shift;
67 #
68 # if (! ref $new_class
69 # && $new_class->isa('Image::Base::X11::Protocol::Pixmap')) {
70 # my %param = @_;
71 # my $X = $self->{'-X'};
72 # if ($param{'-X'} == $X) {
73 # my ($depth, $width, $height, $colormap)
74 # = $self->get('-screen','-depth','-width','-height');
75 # my ($new_screen, $new_depth)
76 # = $new_class->_new_params_screen_and_depth(\%params);
77 # if ($new_screen == $screen
78 # && $new_depth == $depth
79 # && $new_colormap == $colormap) {
80 #
81 # my $new_image = $new_class->new (%param);
82 #
83 # ### copy to new Pixmap
84 # my ($width, $height) = $self->get('-width','-height');
85 # my ($new_width, $new_height) = $new_image->get('-width','-height');
86 # $X->CopyArea ($self->{'-drawable'}, # src
87 # $new_image->{'-drawable'}, # dst
88 # _gc_created($self),
89 # 0,0, # src x,y
90 # min ($width,$new_width), min ($height,$new_height)
91 # 0,0); # dst x,y
92 # return $new_image;
93 # }
94 # }
95 # }
96 # return $self->SUPER::new_from_image ($new_class, @_);
97 # }
98 # sub _gc_created {
99 # my ($self) = @_;
100 # return ($self->{'-gc_created'} ||= do {
101 # my $gc = $self->{'-X'}->new_rsrc;
102 # ### CreateGC: $gc
103 # $self->{'-X'}->CreateGC ($gc, $self->{'-drawable'});
104 # $gc
105 # });
106 # }
107  
108 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
109 0 my ($self) = @_;
110 ### X11-Protocol-Drawable DESTROY
111 0 _free_gc_created ($self);
112 0 shift->SUPER::DESTROY (@_);
113 }
114 0 ▶sub _free_gc_created {
Subroutine _free_gc_created: not called CC=2 SCAR=17.9
115 0 my ($self) = @_;
116 0 ▶ if (my $gc = delete $self->{'-gc_created'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gc = delete $self->{"-gc_created"}) 0 0 0
117 ### FreeGC: $gc
118 0 $self->{'-X'}->FreeGC ($gc);
119 }
120 }
121  
122 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($self) = @_;
124 0 local $self->{'_during_get'} = {};
125 0 return shift->SUPER::get(@_);
126 }
127 0 ▶my %get_geometry = (-depth => sub{$_[1]->{'root_depth'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 ▶ -root => sub{$_[1]->{'root'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 ▶ -x => sub{0},
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ -y => sub{0},
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ -width => sub{$_[1]->{'width_in_pixels'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ -height => sub{$_[1]->{'height_in_pixels'}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ -border_width => sub{0},
Subroutine __ANON__: not called CC=1 SCAR=6.9
134  
135 # and with extra crunching
136 0 ▶ -screen => sub{$_[0]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
137  
138 0 ▶sub _get {
Subroutine _get: not called CC=7 SCAR=40.3
139 0 my ($self, $key) = @_;
140 ### X11-Protocol-Drawable _get(): $key
141  
142 0 ▶ if (! exists $self->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{$key} and defined(my $rsubr = $get_geometry{$key})) 0 0 0
Condition0%0/3
not exists $self->{$key} and defined(my $rsubr = $get_geometry{$key})
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $self->{$key} and defined(my $rsubr = $get_geometry{$key})
not exists $self->{$key} defined(my $rsubr = $get_geometry{$key})
Truth table0%0/3
not(A) and B
A= not exists $self->{$key}B= defined(my $rsubr = $get_geometry{$key})
ABresult
0X0
100
111
143 && defined (my $rsubr = $get_geometry{$key})) {
144 0 my $X = $self->{'-X'};
145 0 my $drawable = $self->{'-drawable'};
146  
147 0 ▶ if (defined (my $screen = X11::Protocol::Other::root_to_screen ($X, $drawable))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $screen = X11::Protocol::Other::root_to_screen($X, $drawable))) 0 0 0
148 # $drawable is a root window, grab info out of $X
149 0 &$rsubr ($screen, $X->{'screens'}->[$screen]);
150 }
151  
152 0 my %geom = $X->GetGeometry ($self->{'-drawable'});
153 0 foreach my $gkey (keys %get_geometry) {
154 0 ▶ if (! defined $self->{$gkey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$gkey}) 0 0 0
155 0 $self->{$gkey} = $geom{substr($gkey,1)};
156 }
157 }
158 0 ▶ if (! defined $self->{'-screen'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-screen"}) 0 0 0
159 0 $self->{'-screen'} = X11::Protocol::Other::root_to_screen ($X, $geom{'root'});
160 }
161 }
162 0 return $self->SUPER::_get($key);
163 }
164  
165 0 ▶sub set {
Subroutine set: not called CC=6 SCAR=37.4
Pod documented
166 0 my ($self, %params) = @_;
167  
168 0 ▶ if (exists $params{'-pixmap'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-pixmap"}) 0 0 0
169 0 $params{'-drawable'} = delete $params{'-pixmap'};
170 }
171 0 ▶ if (exists $params{'-window'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-window"}) 0 0 0
172 0 $params{'-drawable'} = delete $params{'-window'};
173 }
174  
175 0 ▶ if (exists $params{'-drawable'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-drawable"}) 0 0 0
176 0 _free_gc_created ($self);
177 # purge these cached values, %params can supply new ones if desired
178 0 delete @{$self}{keys %get_geometry}; # hash slice
179 }
180 0 ▶ if (exists $params{'-colormap'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-colormap"}) 0 0 0
181 0 %{$self->{'-colour_to_pixel'}} = (); # clear
182 }
183 0 ▶ if (exists $params{'-gc'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"-gc"}) 0 0 0
184 # no longer know what colour is in the gc, or not unless included in
185 # %params
186 0 $self->{'-gc_colour'} = '';
187 0 $self->{'-gc_pixel'} = -1;
188 }
189  
190 0 %$self = (%$self, %params);
191 }
192  
193 #------------------------------------------------------------------------------
194 # drawing
195  
196 0 ▶sub xy {
Subroutine xy: not called CC=12 SCAR=50.5
Pod documented
197 0 my ($self, $x, $y, $colour) = @_;
198 ### xy
199 ### $x
200 ### $y
201 ### $colour
202  
203 0 ▶ if ($x < 0 || $y < 0 || $x > 0x7FFF || $y > 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0 or $x > 32767 or $y > 32767) 0 0 0
Condition0%0/9
$x < 0 or $y < 0 or $x > 32767 or $y > 32767
l!l&&r!l&&!r
000
$x < 0 or $y < 0 or $x > 32767
l!l&&r!l&&!r
000
$x < 0 or $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$x < 0 or $y < 0 or $x > 32767 or $y > 32767
$x < 0 $y < 0 $x > 32767 $y > 32767
Truth table0%0/5
A or B or C or D
A= $x < 0B= $y < 0C= $x > 32767D= $y > 32767
ABCDresult
1XXX1
01XX1
001X1
00011
00000
204 ### outside max drawable, don't overflow INT16 ...
205 0 return undef; # fetch or store
206 }
207  
208 0 my $X = $self->{'-X'};
209 0 my $drawable = $self->{'-drawable'};
210 0 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) 0 0 0
211 # store colour
212 0 $X->PolyPoint ($drawable, _gc_colour($self,$colour), 'Origin', $x,$y);
213 0 return;
214 }
215  
216 # fetch colour
217 0 my @reply = $X->robust_req ('GetImage', $drawable,
218 $x, $y, 1, 1, 0xFFFFFFFF, 'ZPixmap');
219 0 ▶ if (! ref $reply[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $reply[0]) 0 0 0
220 0 ▶ if ($reply[0] eq 'Match') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply[0] eq "Match") 0 0 0
221 ### Match error reading offscreen
222 0 return '';
223 }
224 0 croak "Error reading pixel: ",join(' ',@reply);
225 }
226 0 my ($depth, $visual, $bytes) = @{$reply[0]};
227 0 ▶ if (! defined $self->{'-depth'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"-depth"}) 0 0 0
228 0 $self->{'-depth'} = $depth;
229 }
230 ### $depth
231 ### $visual
232  
233 # X11::Protocol 0.56 shows named 'LeastSiginificant' in the pod, but the
234 # code gives raw number '0'. Let num() crunch either.
235 0 ▶ if ($X->num('Significance',$X->{'image_byte_order'}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($X->num("Significance", $X->{"image_byte_order"}) == 0) 0 0 0
236 #### reverse for LSB image format
237 0 $bytes = reverse $bytes;
238 }
239 ### $bytes
240 0 my $pixel = unpack ('N', $bytes);
241  
242 # not sure what the protocol says about extra bits or bytes in the reply
243 # data, have seen a freebsd server giving garbage, so mask the extras
244 0 $pixel &= (1 << $depth) - 1;
245  
246 ### pixel: sprintf '%X', $pixel
247 ### pixel_to_colour: $self->pixel_to_colour($pixel)
248 0 ▶ if (defined ($colour = $self->pixel_to_colour($pixel))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($colour = $self->pixel_to_colour($pixel))) 0 0 0
249 0 return $colour;
250 }
251 0 ▶ if (my $colormap = $self->{'-colormap'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $colormap = $self->{"-colormap"}) 0 0 0
252 #### query: $X->QueryColors ($self->get('-colormap'), $pixel)
253 0 my ($rgb) = $X->QueryColors ($self->get('-colormap'), $pixel);
254 #### $rgb
255 0 return sprintf('#%04X%04X%04X', @$rgb);
256 }
257 0 return $pixel;
258 }
259 0 ▶sub Image_Base_Other_xy_points {
Subroutine Image_Base_Other_xy_points: not called CC=8 SCAR=42.8
Pod undocumented
260 0 my $self = shift;
261 0 my $colour = shift;
262 0 my $gc = _gc_colour($self,$colour);
263 0 my $X = $self->{'-X'};
264  
265 # PolyPoint is 3xCARD32 for drawable,gc,mode then room for maxlen-3 words
266 # of X,Y values. X and Y are INT16 each, hence room for (maxlen-3)*2
267 # individual points. Is there any merit sending smaller chunks though?
268 # 250kbytes is a typical server limit.
269 #
270 0 my $maxpoints = 2*($X->{'maximum_request_length'} - 3);
271 ### $maxpoints
272  
273 0 my @points;
274 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
275 0 ▶ if (@points >= $maxpoints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points >= $maxpoints) 0 0 0
276 0 $X->PolyPoint ($self->{'-drawable'}, $gc, 'Origin', @points);
277 0 $#points = -1; # empty
278 }
279 0 my $x = shift;
280 0 my $y = shift;
281 0 ▶ if ($x >= 0 && $y >= 0 && $x <= 0x7FFF && $y <= 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0 and $y >= 0 and $x <= 32767 and $y <= 32767) 0 0 0
Condition0%0/9
$x >= 0 and $y >= 0 and $x <= 32767 and $y <= 32767
!ll&&!rl&&r
000
$x >= 0 and $y >= 0 and $x <= 32767
!ll&&!rl&&r
000
$x >= 0 and $y >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x >= 0 and $y >= 0 and $x <= 32767 and $y <= 32767
$x >= 0 $y >= 0 $x <= 32767 $y <= 32767
Truth table0%0/5
A and B and C and D
A= $x >= 0B= $y >= 0C= $x <= 32767D= $y <= 32767
ABCDresult
0XXX0
10XX0
110X0
11100
11111
282 # within max drawable ...
283 0 push @points, $x,$y;
284 }
285 }
286 0 ▶ if (@points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points) 0 0 0
287 0 $X->PolyPoint ($self->{'-drawable'}, $gc, 'Origin', @points);
288 }
289 }
290  
291 0 ▶sub line {
Subroutine line: not called CC=2 SCAR=17.9
Pod documented
292 0 my ($self, $x1, $y1, $x2, $y2, $colour) = @_ ;
293  
294 0 ▶ ($x1,$y1, $x2,$y2) = _line_clip ($x1,$y1, $x2,$y2)
Branch: 1 branch
BranchTrueFalseTotal
() = _line_clip($x1, $y1, $x2, $y2) or return 0 0 0
295 or return; # nothing left after clipping
296  
297 0 $self->{'-X'}->PolySegment ($self->{'-drawable'}, _gc_colour($self,$colour),
298 $x1,$y1, $x2,$y2);
299 }
300  
301 0 ▶sub rectangle {
Subroutine rectangle: not called CC=12 SCAR=50.5
Pod documented
302 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
303 ### X11-Protocol-Drawable rectangle
304  
305 0 ▶ unless ($x2 >= 0 && $y2 >= 0 && $x1 <= 0x7FFF && $y1 <= 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767) 0 0 0
Condition0%0/9
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
$x2 >= 0 $y2 >= 0 $x1 <= 32767 $y1 <= 32767
Truth table0%0/5
A and B and C and D
A= $x2 >= 0B= $y2 >= 0C= $x1 <= 32767D= $y1 <= 32767
ABCDresult
0XXX0
10XX0
110X0
11100
11111
306 ### entirely outside max possible drawable ...
307 0 return;
308 }
309  
310 # Don't underflow INT16 -0x8000 x,y in request. But retain negativeness so
311 # as not to bring top and left sides of an unfilled rect into view.
312 0 ▶ if ($x1 < -1) { $x1 = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < -1) 0 0 0
313 0 ▶ if ($y1 < -1) { $y1 = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < -1) 0 0 0
314  
315 # Don't overflow CARD16 width,height in request. Together with x1,y1 >=
316 # -1 this makes w,h <= 0x8002. It doesn't bring the unfilled right and
317 # bottom sides into view even if the drawable is 0 to 0x7FFF.
318 0 ▶ if ($x2 > 0x8000) { $x2 = 0x8000; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 > 32768) 0 0 0
319 0 ▶ if ($y2 > 0x8000) { $y2 = 0x8000; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 > 32768) 0 0 0
320  
321 0 ▶ if ($x1 == $x2 || $y1 == $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 == $x2 or $y1 == $y2) { } 0 0 0
Condition0%0/3
$x1 == $x2 or $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/2
$x1 == $x2 or $y1 == $y2
$x1 == $x2 $y1 == $y2
Truth table0%0/3
A or B
A= $x1 == $x2B= $y1 == $y2
ABresult
1X1
011
000
322 # single pixel wide or high, must treat as filled since PolyRectangle()
323 # draws nothing if passed width==0 or height==0
324 0 $fill = 1;
325 } else {
326 0 $fill = !!$fill; # 0 or 1 for arithmetic
327 }
328 ### coords: [ $x1, $y1, $x2-$x1, $y2-$y1 ]
329  
330 $self->{'-X'}->request ($fill ? 'PolyFillRectangle' : 'PolyRectangle',
331 0 ▶ $self->{'-drawable'},
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
332 _gc_colour($self,$colour),
333 [ $x1, $y1, $x2-$x1+$fill, $y2-$y1+$fill ]);
334 }
335  
336 0 ▶sub Image_Base_Other_rectangles {
Subroutine Image_Base_Other_rectangles: not called CC=17 SCAR=57.2
Pod undocumented
337 ### X11-Protocol-Drawable rectangles()
338 ### count: scalar(@_)
339 0 my $self = shift;
340 0 my $colour = shift;
341 0 my $fill = !! shift; # 0 or 1
342  
343 0 ▶ my $method = ($fill ? 'PolyFillRectangle' : 'PolyRectangle');
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
344 ### $method
345  
346 ### coords count: scalar(@_)
347 ### coords: @_
348  
349 0 my @rects;
350 my @filled;
351 0 ▶ while (my ($x1,$y1, $x2,$y2) = splice @_,0,4) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 4)) 0 0 0
352 ### quad: ($x1,$y1, $x2,$y2)
353  
354 0 ▶ unless ($x2 >= 0 && $y2 >= 0 && $x1 <= 0x7FFF && $y1 <= 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767) 0 0 0
Condition0%0/9
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
$x2 >= 0 $y2 >= 0 $x1 <= 32767 $y1 <= 32767
Truth table0%0/5
A and B and C and D
A= $x2 >= 0B= $y2 >= 0C= $x1 <= 32767D= $y1 <= 32767
ABCDresult
0XXX0
10XX0
110X0
11100
11111
355 ### entirely outside max possible drawable ...
356 0 next;
357 }
358 # don't underflow INT16 -0x8000 x,y in request
359 # but retain negativeness so as not to bring unfilled sides into view
360 0 ▶ if ($x1 < -1) { $x1 = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < -1) 0 0 0
361 0 ▶ if ($y1 < -1) { $y1 = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 < -1) 0 0 0
362 # don't overflow CARD16 width,height in request
363 0 ▶ if ($x2 > 0x8000) { $x2 = 0x8000; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 > 32768) 0 0 0
364 0 ▶ if ($y2 > 0x8000) { $y2 = 0x8000; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 > 32768) 0 0 0
365  
366 0 ▶ if (! $fill && ($x1 == $x2 || $y1 == $y2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fill and $x1 == $x2 || $y1 == $y2) { } 0 0 0
Condition0%0/6
not $fill and $x1 == $x2 || $y1 == $y2
!ll&&!rl&&r
000
$x1 == $x2 or $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/3
not $fill and $x1 == $x2 || $y1 == $y2
not $fill $x1 == $x2 $y1 == $y2
Truth table0%0/4
not(A) and B or C
A= not $fillB= $x1 == $x2C= $y1 == $y2
ABCresult
0XX0
1000
11X1
1011
367 # single pixel wide or high
368 0 push @filled, [ $x1, $y1, $x2-$x1+1, $y2-$y1+1 ];
369 } else {
370 0 push @rects, [ $x1, $y1, $x2-$x1+$fill, $y2-$y1+$fill ];
371 }
372 }
373 ### @rects
374  
375 0 my $X = $self->{'-X'};
376 0 my $gc = _gc_colour($self,$colour);
377  
378 # PolyRectangle is 3xCARD32 header,drawable,gc then room for maxlen-3
379 # words of X,Y,WIDTH,HEIGHT values. X,Y are INT16 and WIDTH,HEIGHT are
380 # CARD16 each, hence room for floor((maxlen-3)/2) rectangles. Is there
381 # any value sending somewhat smaller chunks though? 250kbytes is a
382 # typical server limit. Xlib ZRCTSPERBATCH is just 256 thin line rects,
383 # or WRCTSPERBATCH 10 wides.
384 #
385 0 my $maxrects = int (($X->{'maximum_request_length'} - 3) / 2);
386 ### $maxrects
387  
388 0 foreach my $aref (\@rects, \@filled) {
389 0 ▶ if (@$aref) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref) 0 0 0
390 0 my $drawable = $self->{'-drawable'};
391 0 ▶ while (@$aref > $maxrects) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref > $maxrects) 0 0 0
392 ### splice down from: scalar(@$aref)
393 0 $X->$method ($drawable, $gc, splice @$aref, 0,$maxrects);
394 }
395 ### final: $method, @$aref
396 0 $X->$method ($drawable, $gc, @$aref);
397 }
398 0 $method = 'PolyFillRectangle';
399 }
400 }
401  
402 # The Arc requests take the bounding region at
403 # left x, y+(h/2)
404 # right x+w, y+(h/2)
405 # top x+(w/2), y
406 # bottom x+(w/2), y+h
407 # with w=x2-x1, h=y2-y1.
408 #
409 # For PolyArc a 1-wide line makes each of those pixels drawn, but a
410 # PolyFillArc is only the inside, not the extra 0.5 around the outside,
411 # which means the bottom and right endmost pixels not drawn, and others a
412 # bit smaller than PolyArc.
413 #
414 # For now try a PolyArc on top of the PolyFillArc to get the extra 0.5
415 # around the outside. Can it be done better? Prima has this, as long as
416 # the drawing mode isn't xor etc where duplicated pixels are bad.
417 #
418 # One possibility would be to set line width lw=min(w/2,h/2) rounded up to
419 # next odd integer, and shrink the bounding box by (lw-1)/2, so a PolyLine
420 # centred there goes out to the very edges of the x1,y1,x2,y2 box, not just
421 # the centres of those pixels, and being w/2 or h/2 will extend in to cover
422 # the centre. The disadvantage would be changing the line width for each
423 # draw, or keep another gc, and that might take away the option for the user
424 # to set in a '-gc' option to choose between zero-width fast lines and
425 # 1-width exact lines. An advantage though would be a single draw operation
426 # meaning an "xor" mode in the gc would cover the right pixels. There's
427 # something in the PolyArc spec about the bounding box being implementation
428 # dependent if width!=height, so maybe this wouldn't work always.
429 #
430 # The same bounding box centred on the pixels happens in rectangle(), but
431 # can be handled there by +1 on the width and height. A +1 doesn't make a
432 # filled ellipse come out the same as an outlined ellipse though.
433 #
434 # same in Window.pm for shape stuff
435 0 ▶sub ellipse {
Subroutine ellipse: not called CC=12 SCAR=50.5
Pod documented
436 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
437 ### Drawable ellipse(): $x1, $y1, $x2, $y2, $colour
438  
439 0 my $w = $x2 - $x1;
440 0 my $h = $y2 - $y1;
441 0 ▶ if ($w <= 1 || $h <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w <= 1 or $h <= 1) 0 0 0
Condition0%0/3
$w <= 1 or $h <= 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$w <= 1 or $h <= 1
$w <= 1 $h <= 1
Truth table0%0/3
A or B
A= $w <= 1B= $h <= 1
ABresult
1X1
011
000
442 # 1 or 2 pixels wide or high
443 0 shift->rectangle(@_);
444 0 return;
445 }
446  
447 0 ▶ unless ($x2 >= 0 && $y2 >= 0 && $x1 <= 0x7FFF && $y1 <= 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767) 0 0 0
Condition0%0/9
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767
!ll&&!rl&&r
000
$x2 >= 0 and $y2 >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
$x2 >= 0 and $y2 >= 0 and $x1 <= 32767 and $y1 <= 32767
$x2 >= 0 $y2 >= 0 $x1 <= 32767 $y1 <= 32767
Truth table0%0/5
A and B and C and D
A= $x2 >= 0B= $y2 >= 0C= $x1 <= 32767D= $y1 <= 32767
ABCDresult
0XXX0
10XX0
110X0
11100
11111
448 ### entirely outside max possible drawable ...
449 0 return;
450 }
451  
452 0 ▶ if ($x1 < -0x8000 || $x2 > 0x7FFF || $y1 < -0x8000 || $y2 > 0x7FFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 < -32768 or $x2 > 32767 or $y1 < -32768 or $y2 > 32767) 0 0 0
Condition0%0/9
$x1 < -32768 or $x2 > 32767 or $y1 < -32768 or $y2 > 32767
l!l&&r!l&&!r
000
$x1 < -32768 or $x2 > 32767 or $y1 < -32768
l!l&&r!l&&!r
000
$x1 < -32768 or $x2 > 32767
l!l&&r!l&&!r
000
MC/DC0%0/4
$x1 < -32768 or $x2 > 32767 or $y1 < -32768 or $y2 > 32767
$x1 < -32768 $x2 > 32767 $y1 < -32768 $y2 > 32767
Truth table0%0/5
A or B or C or D
A= $x1 < -32768B= $x2 > 32767C= $y1 < -32768D= $y2 > 32767
ABCDresult
1XXX1
01XX1
001X1
00011
00000
453 ### coordinates would overflow, use superclass ...
454 0 shift->SUPER::ellipse(@_);
455 0 return;
456 }
457  
458 ### PolyArc: $x1, $y1, $x2-$x1+1, $y2-$y1+1, 0, 360*64
459 0 my @args = ($self->{'-drawable'}, _gc_colour($self,$colour),
460 [ $x1, $y1, $w, $h, 0, 360*64 ]);
461 0 my $X = $self->{'-X'};
462 0 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) 0 0 0
463 0 $X->PolyFillArc (@args);
464 }
465 0 $X->PolyArc (@args);
466 }
467  
468 0 ▶sub diamond {
Subroutine diamond: not called CC=3 SCAR=24.8
Pod documented
469 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
470 ### Drawable diamond(): $x1, $y1, $x2, $y2, $colour
471  
472 0 ▶ if ($x1==$x2 && $y1==$y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 == $x2 and $y1 == $y2) 0 0 0
Condition0%0/3
$x1 == $x2 and $y1 == $y2
!ll&&!rl&&r
000
MC/DC0%0/2
$x1 == $x2 and $y1 == $y2
$x1 == $x2 $y1 == $y2
Truth table0%0/3
A and B
A= $x1 == $x2B= $y1 == $y2
ABresult
0X0
100
111
473 # 1x1 polygon draws nothing, do it as a point instead
474 0 $self->xy($x1,$y1, $colour);
475 0 return;
476 }
477  
478 _diamond_drawable ($self->{'-X'},
479 0 $self->{'-drawable'},
480 _gc_colour($self,$colour),
481 $x1,$y1, $x2,$y2, $fill);
482 }
483  
484 # shared by Image::Base::X11::Protocol::Window::diamond()
485 0 ▶sub _diamond_drawable {
Subroutine _diamond_drawable: not called CC=7 SCAR=40.3
486 0 my ($X, $drawable, $gc, $x1, $y1, $x2, $y2, $fill) = @_;
487 ### _diamond_drawable() ...
488  
489 0 my $xh = int( ($x2 - $x1)/2 );
490 0 my $yh = int( ($y2 - $y1)/2 );
491 0 my $xmid_floor = $x1 + $xh;
492 0 my $xmid_ceil = $x2 - $xh;
493 0 my $ymid_floor = $y1 + $yh;
494 0 my $ymid_ceil = $y2 - $yh;
495  
496 0 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) { } 0 0 0
497 # 1-0
498 # /
499 # 2 7
500 # 3 6
501 # \ /
502 # 4-5
503 0 ▶ my @xy =(# top
Branch: 4 branches
BranchTrueFalseTotal
$xmid_floor == $xmid_ceil ? : 0 0 0
$ymid_floor == $ymid_ceil ? : 0 0 0
$xmid_floor == $xmid_ceil ? : 0 0 0
$ymid_floor == $ymid_ceil ? : 0 0 0
504 ($xmid_floor == $xmid_ceil ? () : ($xmid_ceil, $y1)),
505 $xmid_floor, $y1,
506  
507 # left
508 $x1, $ymid_floor,
509 ($ymid_floor == $ymid_ceil ? () : ($x1, $ymid_ceil)),
510  
511 # bottom
512 $xmid_floor, $y2,
513 ($xmid_floor == $xmid_ceil ? () : ($xmid_ceil, $y2)),
514  
515 # right
516 ($ymid_floor == $ymid_ceil ? () : ($x2, $ymid_ceil)),
517 $x2, $ymid_floor,
518 );
519 0 _convex_poly_clip(\@xy);
520 ### clipped: @xy
521 0 ▶ if (@xy) {
Branch: 1 branch
BranchTrueFalseTotal
if (@xy) 0 0 0
522 0 push @xy, $xy[0],$xy[1]; # back to start
523 0 $X->FillPoly ($drawable, $gc, 'Convex', 'Origin', @xy);
524 0 $X->PolyLine ($drawable, $gc, 'Origin', @xy);
525 }
526  
527 } else {
528 # unfilled
529 0 $X->PolySegment ($drawable, $gc,
530  
531 # NW A .
532 # / \
533 # B .
534 _line_clip ($xmid_floor, $y1, $x1, $ymid_floor),
535  
536 # SW B .
537 # \ /
538 # A .
539 _line_clip ($xmid_floor, $y2, $x1, $ymid_ceil),
540  
541 # SE . B
542 # \ /
543 # . A
544 _line_clip ($xmid_ceil, $y2, $x2, $ymid_ceil),
545  
546 # NE . A
547 # / \
548 # . B
549 _line_clip ($xmid_ceil, $y1, $x2, $ymid_floor));
550 }
551 }
552  
553 #------------------------------------------------------------------------------
554  
555 # not yet a documented feature ...
556 0 ▶sub pixel_to_colour {
Subroutine pixel_to_colour: not called CC=2 SCAR=17.9
Pod undocumented
557 0 my ($self,$pixel) = @_;
558 0 ▶ my $hash = ($self->{'-pixel_to_colour'} ||= do {
Condition0%0/3
$self->{"-pixel_to_colour"} ||= do { +{reverse(%{$self->{"-colour_to_pixel"};})} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"-pixel_to_colour"} ||= do { +{reverse(%{$self->{"-colour_to_pixel"};})} }
$self->{"-pixel_to_colour"} do { +{reverse(%{$self->{"-colour_to_pixel"};})} }
Truth table0%0/3
A ||= B
A= $self->{"-pixel_to_colour"}B= do { +{reverse(%{$self->{"-colour_to_pixel"};})} }
ABresult
1X1
011
000
559 ### colour_to_pixel hash: $self->{'-colour_to_pixel'}
560 0 ({ reverse %{$self->{'-colour_to_pixel'}} }) # force anon hash
561 });
562 0 return $hash->{$pixel};
563 }
564  
565 # return a gc XID which is set to draw in $colour
566 0 ▶sub _gc_colour {
Subroutine _gc_colour: not called CC=6 SCAR=37.4
567 0 my ($self, $colour) = @_;
568 0 ▶ if ($colour eq 'None') {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour eq "None") 0 0 0
569 0 $colour = 'black';
570 }
571 0 ▶ my $gc = $self->{'-gc'} || $self->{'-gc_created'};
Condition0%0/3
$self->{'-gc'} || $self->{'-gc_created'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'-gc'} || $self->{'-gc_created'}
$self->{'-gc'} $self->{'-gc_created'}
Truth table0%0/3
A || B
A= $self->{'-gc'}B= $self->{'-gc_created'}
ABresult
1X1
011
000
572 0 ▶ if ($colour ne $self->{'-gc_colour'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour ne $self->{"-gc_colour"}) 0 0 0
573 ### X11-Protocol-Drawable -gc_colour() change: $colour
574 0 my $pixel = $self->colour_to_pixel ($colour);
575 0 $self->{'-gc_colour'} = $colour;
576  
577 0 ▶ if ($pixel != $self->{'-gc_pixel'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel != $self->{"-gc_pixel"}) 0 0 0
578 0 $self->{'-gc_pixel'} = $pixel;
579 0 my $X = $self->{'-X'};
580 0 ▶ if ($gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gc) { } 0 0 0
581 ### ChangeGC to pixel: $pixel
582 0 $X->ChangeGC ($gc, foreground => $pixel);
583 } else {
584 0 $gc = $self->{'-gc_created'} = $X->new_rsrc;
585 ### CreateGC with pixel ...
586 ### $gc
587 ### $pixel
588 0 $X->CreateGC ($gc, $self->{'-drawable'}, foreground => $pixel);
589 }
590 }
591 }
592 0 return $gc;
593 }
594  
595 # return an allocated pixel number
596 # not yet a documented feature ...
597 0 ▶sub colour_to_pixel {
Subroutine colour_to_pixel: not called CC=5 SCAR=34.0
Pod undocumented
598 0 my ($self, $colour) = @_;
599 ### X11-Protocol-Drawable _colour_to_pixel(): $colour
600 0 ▶ if ($colour =~ /^^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour =~ /^^\d+$/) 0 0 0
601 0 return $colour; # numeric pixel value
602 }
603 0 ▶ if ($colour eq 'set') {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour eq "set") 0 0 0
604 # ENHANCE-ME: maybe all bits set if depth > 1
605 0 return 1;
606 }
607 0 ▶ if ($colour eq 'clear') {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour eq "clear") 0 0 0
608 0 return 0;
609 }
610 0 ▶ if (defined (my $pixel = $self->{'-colour_to_pixel'}->{$colour})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pixel = $self->{"-colour_to_pixel"}{$colour})) 0 0 0
611 0 return $pixel;
612 }
613 0 $self->add_colours ($colour);
614 0 return $self->{'-colour_to_pixel'}->{$colour};
615 }
616  
617 4 my %colour_to_screen_field
618 = ('black' => 'black_pixel',
619 '#000000' => 'black_pixel',
620 '#000000000000' => 'black_pixel',
621 'white' => 'white_pixel',
622 '#FFFFFF' => 'white_pixel',
623 '#FFFFFFFFFFFF' => 'white_pixel',
624 '#ffffff' => 'white_pixel',
625 '#ffffffffffff' => 'white_pixel',
626 );
627  
628 0 ▶sub add_colours {
Subroutine add_colours: not called CC=11 SCAR=48.8
Pod documented
629 0 my $self = shift;
630 ### add_colours: @_
631 0 my $X = $self->{'-X'};
632 0 ▶ my $colormap = $self->get('-colormap')
Condition0%0/2
$self->get('-colormap') || croak('No -colormap to add colours to')
l!l
00
MC/DC0%0/1
$self->get('-colormap') || croak('No -colormap to add colours to')
$self->get('-colormap')
Truth table0%0/2
A
A= $self->get('-colormap')
Aresult
11
00
633 || croak 'No -colormap to add colours to';
634 0 my $colour_to_pixel = $self->{'-colour_to_pixel'};
635 0 my $pixel_to_colour = $self->{'-pixel_to_colour'};
636  
637 0 my @queued;
638 my @failed_colours;
639  
640 0 my $old_error_handler = $X->{'error_handler'};
641 0 ▶ my $wait_queue = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
642 0 my $elem = shift @queued;
643 0 my $seq = $elem->{'seq'};
644 0 my $colour = $elem->{'colour'};
645  
646 0 my $err;
647 0 ▶ local $X->{'error_handler'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
648 0 my ($X, $data) = @_;
649 0 my ($type, $err_seq) = unpack("xCSLSCxxxxxxxxxxxxxxxxxxxxx", $data);
650 0 ▶ if ($err_seq != $seq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err_seq != $seq) 0 0 0
651 0 goto &$old_error_handler;
652 }
653 0 $err = 1;
654 0 };
655  
656 ### handle: $seq
657 0 $X->handle_input_for ($seq);
658 0 $X->delete_reply ($seq);
659 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
660 0 push @failed_colours, $colour;
661 0 return;
662 }
663  
664 ### reply: $X->unpack_reply($elem->{'request_type'}, $elem->{'reply'})
665  
666 0 my ($pixel) = $X->unpack_reply ($elem->{'request_type'}, $elem->{'reply'});
667 0 $colour_to_pixel->{$colour} = $pixel;
668 0 ▶ if ($pixel_to_colour) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel_to_colour) 0 0 0
669 0 $pixel_to_colour->{$pixel} = $colour;
670 }
671 0 };
672  
673 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
674 0 my $colour = shift;
675 0 ▶ next if defined $colour_to_pixel->{$colour}; # already known
Branch: 1 branch
BranchTrueFalseTotal
if defined $colour_to_pixel->{$colour} 0 0 0
676 0 delete $self->{'-pixel_to_colour'};
677  
678 # black_pixel or white_pixel of a default colormap
679 0 ▶ if (my $field = $colour_to_screen_field{$colour}) { # "black" or "white"
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $colour_to_screen_field{$colour}) 0 0 0
680 0 ▶ if (my $screen_info = X11::Protocol::Other::default_colormap_to_screen_info($X,$colormap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $screen_info = X11::Protocol::Other::default_colormap_to_screen_info($X, $colormap)) 0 0 0
681 0 my $pixel = $colour_to_pixel->{$colour} = $screen_info->{$field};
682 0 ▶ if ($pixel_to_colour) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel_to_colour) 0 0 0
683 0 $pixel_to_colour->{$pixel} = $colour;
684 }
685 0 next;
686 }
687 }
688  
689 0 my $elem = { colour => $colour };
690 0 my @req;
691 # Crib: [:xdigit:] new in 5.6, so only 0-9A-F, and in any case as of
692 # perl 5.12.4 [:xdigit:] matches some wide chars but hex() doesn't
693 # accept them
694 0 ▶ if (my @rgb = X11::Protocol::Other::hexstr_to_rgb($colour)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rgb) = X11::Protocol::Other::hexstr_to_rgb($colour)) { } 0 0 0
695 0 @req = ('AllocColor', $colormap, map {hex} @rgb);
696 } else {
697 0 @req = ('AllocNamedColor', $colormap, $colour);
698 }
699 0 $elem->{'request_type'} = $req[0];
700 0 my $seq = $elem->{'seq'} = $X->send(@req);
701 0 $X->add_reply ($seq, \$elem->{'reply'});
702  
703 ### $elem
704 0 push @queued, $elem;
705 0 ▶ if (@queued > 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queued > 256) 0 0 0
706 0 &$wait_queue();
707 }
708 }
709 0 ▶ while (@queued) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queued) 0 0 0
710 0 &$wait_queue();
711 }
712  
713 0 ▶ if (@failed_colours) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failed_colours) 0 0 0
714 0 die "Unknown colour(s): ",join(', ', @failed_colours);
715 }
716 }
717  
718 #------------------------------------------------------------------------------
719 # clipping to signed 16-bit parameters
720  
721 4 ▶use constant _LO => -0x8000; # -32768
Subroutine BEGIN: called CC=1 SCAR=0.0
722 4 ▶use constant _HI => 0x7FFF; # +32767
Subroutine BEGIN: called CC=1 SCAR=0.0
723  
724 # $x1,$y1, $x2,$y2 are the endpoints of a line.
725 # Return new endpoints which are clipped to within -0x8000 to +0x7FFF which is
726 # signed 16-bits for X protocol.
727 # If given line is entirely outside the signed 16-bit rectangle then return
728 # an empty list.
729 #
730 0 ▶sub _line_clip {
Subroutine _line_clip: not called CC=4 SCAR=30.0
731 0 my ($x1,$y1, $x2,$y2) = @_;
732 ### _line_clip_16bit(): "$x1,$y1, $x2,$y2"
733  
734 0 ▶ unless (_line_any_positive($x1,$y1, $x2,$y2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_line_any_positive($x1, $y1, $x2, $y2)) 0 0 0
735 ### nothing positive ...
736 0 return;
737 }
738  
739 my ($x1new,$y1new) = _line_end_clip($x1,$y1, $x2,$y2)
740 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
() = _line_end_clip($x1, $y1, $x2, $y2) or do { return } 0 0 0
741 ### x1,y1 end nothing in range ...
742 0 return;
743 };
744 0 ▶ ($x2,$y2) = _line_end_clip($x2,$y2, $x1,$y1)
Branch: 1 branch
BranchTrueFalseTotal
() = _line_end_clip($x2, $y2, $x1, $y1) or return 0 0 0
745 or return;
746 0 return ($x1new,$y1new, $x2,$y2);
747 }
748  
749 # $x1,$y1, $x2,$y2 are the endpoints of a line.
750 # Return new values for the $x2,$y2 end which clips it to within
751 # LO <= x2 <= HI
752 # LO <= y2 <= HI
753 #
754 # If the line is entirely outside LO to HI then return an empty list.
755 # If x2,y2 is already within LO to HI then return them unchanged.
756 #
757 # x1,y1
758 # /
759 # +-------- if x2 outside
760 # | / then
761 # |/ move it to x2new=LO
762 # x2new,y2new * and y2new=corresponding pos on line
763 # /|
764 # / |
765 # x2,y2 +--------
766 # LO
767 #
768 # +---------
769 # | if y2 outside,
770 # | x1,y1 including moved y2new outside
771 # | / then
772 # +--*----- move it to y2new=LO
773 # /x2new, and x2new=corresponding pos on line
774 # / y2new
775 # first y2new *
776 # /
777 # /
778 # x2,y2
779 #
780 11 ▶sub _line_end_clip {
Subroutine _line_end_clip: called CC=11 SCAR=24.1
781 11 my ($x1,$y1, $x2,$y2) = @_;
782 ### _line_end_clip(): "$x1,$y1, $x2,$y2"
783  
784 11 my ($x2new, $y2new);
785 11 ▶ if ($x2 < _LO || $x2 > _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < -32768 or $x2 > 32767) { } 7 4 11
Condition100%3/3
$x2 < -32768 or $x2 > 32767
l!l&&r!l&&!r
344
MC/DC100%2/2
$x2 < -32768 or $x2 > 32767
$x2 < -32768 $x2 > 32767
Truth table100%3/3
A or B
A= $x2 < -32768B= $x2 > 32767
ABresult
1X1
011
000
786 # x2 is outside LO to HI, clip to x2=LOorHI and y2 set to corresponding
787 7 ▶ my $xlen = $x2 - $x1
Branch: 1 branch
BranchTrueFalseTotal
my $xlen = $x2 - $x1 or return 5 2 7
788 or return; # xlen==0 means x1==x2 so entirely outside LO to HI
789 5 ▶ $x2new = ($x2 < _LO ? _LO : _HI);
Branch: 1 branch
BranchTrueFalseTotal
$x2 < -32768 ? : 2 3 5
790 5 $y2new = floor(($y2*($x2new-$x1) + $y1*($x2-$x2new)) / $xlen + 0.5);
791  
792 ### x clip: "to $x2new,$y2new frac ".($y2*($x2new-$x1) + $y1*($x2-$x2new))." / $xlen"
793 } else {
794 4 $x2new = $x2;
795 4 $y2new = $y2;
796 }
797  
798 9 ▶ if ($y2new < _LO || $y2new > _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2new < -32768 or $y2new > 32767) 4 5 9
Condition100%3/3
$y2new < -32768 or $y2new > 32767
l!l&&r!l&&!r
225
MC/DC100%2/2
$y2new < -32768 or $y2new > 32767
$y2new < -32768 $y2new > 32767
Truth table100%3/3
A or B
A= $y2new < -32768B= $y2new > 32767
ABresult
1X1
011
000
799 4 ▶ my $ylen = $y2 - $y1
Branch: 1 branch
BranchTrueFalseTotal
my $ylen = $y2 - $y1 or return 2 2 4
800 or return; # ylen==0 means y1==y2 so entirely outside LO to HI
801 2 ▶ $y2new = ($y2 < _LO ? _LO : _HI);
Branch: 1 branch
BranchTrueFalseTotal
$y2 < -32768 ? : 1 1 2
802 2 $x2new = floor(($x2*($y2new-$y1) + $x1*($y2-$y2new)) / $ylen + 0.5);
803 ### y clip: "to $x2new,$y2new left ".($y2new-$y1)." right ".($y2-$y2new)
804 2 ▶ if ($x2new < _LO || $x2new > _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2new < -32768 or $x2new > 32767) 0 2 2
Condition33%1/3
$x2new < -32768 or $x2new > 32767
l!l&&r!l&&!r
002
MC/DC0%0/2
$x2new < -32768 or $x2new > 32767
$x2new < -32768 $x2new > 32767
Truth table33%1/3
A or B
A= $x2new < -32768B= $x2new > 32767
ABresult
1X1
011
000
805 ### x2new outside ...
806 0 return;
807 }
808 }
809  
810 7 return ($x2new,$y2new);
811 }
812  
813 # x2,y2
814 # /
815 # /\
816 # / \
817 # / +---------
818 # x1,y1 |
819 # |
820 #
821 # perp X= -1-pos, Y=-1 -pos*(x2-x1)/(y2-y1)
822 # -pos = X+1
823 # Y = (X+1)*(x2-x1)/(y2-y1) - 1
824 #
825 # intersect
826 # (X+1)*(x2-x1)/(y2-y1) - 1 = (X-x1)/(x2-x1)*(y2-y1) + y1
827 # (X+1)*(x2-x1)/(y2-y1) = (X-x1)/(x2-x1)*(y2-y1) + (y1+1)
828 # (X+1)*(x2-x1) = (X-x1)/(x2-x1)*(y2-y1)*(y2-y1) + (y1+1)*(y2-y1)
829 # (X+1)*(x2-x1)*(x2-x1) = (X-x1)*(y2-y1)*(y2-y1) + (y1+1)*(y2-y1)*(x2-x1)
830 # X*(x2-x1)^2 + (x2-x1)^2 = X*(y2-y1)^2 - x1*(y2-y1)^2 + (y1+1)*(y2-y1)*(x2-x1)
831 # X*(x2-x1)^2 - X*(y2-y1)^2 = -(x2-x1)^2 - x1*(y2-y1)^2 + (y1+1)*(y2-y1)*(x2-x1)
832 #
833 # line X=x1+pos, Y=y1 + pos*(y2-y1)/(x2-x1)
834 # Y=y1 + (X-x1)/(x2-x1)*(y2-y1)
835 # eg. X=x1 Y=y1 + 0
836 # eg. X=x2 Y=y1 + 1*(y2-y1) = y2
837 # Y-y1 = (X-x1)/(x2-x1)*(y2-y1)
838 # (Y-y1)*(x2-x1) = (X-x1)*(y2-y1)
839 #
840 # line at X=0 is
841 # Y = (-x1)/(x2-x1)*(y2-y1) + y1
842 # for Y <= -1
843 # (-x1)/(x2-x1)*(y2-y1) + y1 <= -1
844 # (-x1)/(x2-x1)*(y2-y1) <= -1-y1
845 # (-x1)*(y2-y1) <= (-1-y1)*(x2-x1) would swap if x2<x1
846 # x1*(y2-y1) >= (y1+1)*(x2-x1)
847 # eg. x1=-1;y1=-1; x2=1;y2=1 Y = 0 -2>=0
848 #
849 # eg. y1=y2=y 0 < (-1-y)*(x2-x1)
850 # (x1+1)*(y2-y1) > (y1+1)*(x2-x1)
851 # eg. x1=x2=5 -5*(y2-y1) > (y1+1)*0 no
852 #
853 # | 5,-10
854 # /|
855 # -----/----------
856 # -10,5 |
857 # eg. x1=-10;y1=5; x2=5;y2=-10; x1*(y2-y1); (y1+1)*(x2-x1)
858 # is 150 < 90
859 #
860 # | 10,-5
861 # -------/-----
862 # |/
863 # /|
864 # -5,10|
865 # eg. x1=-5;y1=10; x2=10;y2=-5; x1*(y2-y1); (y1+1)*(x2-x1)
866 # is 75 < 165
867 #
868 # eg. x1=5;y1=-10; x2=5;y2=10; x1*(y2-y1); (y1+1)*(x2-x1)
869 # is 100 < 0
870 #
871 18 ▶sub _line_any_positive {
Subroutine _line_any_positive: called CC=5 SCAR=16.1
872 18 my ($x1,$y1, $x2,$y2) = @_;
873  
874 # swap ends to x1 <= x2
875 18 ▶ ($x1,$y1, $x2,$y2) = ($x2,$y2, $x1,$y1) if $x2 < $x1;
Branch: 1 branch
BranchTrueFalseTotal
if $x2 < $x1 7 11 18
876 ### _line_any_positive() swapped to: "$x1, $y1, $x2, $y2"
877  
878 return (# must have x2 positive, otherwise all X negative
879 18 ▶ $x2 > -1
Condition100%9/9
$x2 > -1 && ($y2 > -1 || $y1 > -1 && $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1))
!ll&&!rl&&r
2412
$y2 > -1 or $y1 > -1 and $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1)
l!l&&r!l&&!r
1024
$y1 > -1 and $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1)
!ll&&!rl&&r
222
MC/DC100%4/4
$x2 > -1 && ($y2 > -1 || $y1 > -1 && $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1))
$x2 > -1 $y2 > -1 $y1 > -1 $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1)
Truth table100%5/5
A && B or C and D
A= $x2 > -1B= $y2 > -1C= $y1 > -1D= $x1 * ($y2 - $y1) < ($y1 + 1) * ($x2 - $x1)
ABCDresult
0XXX0
100X0
10100
11XX1
10111
880 &&
881 (# if y2 positive then x2,y2 end both positive so line positive
882 $y2 > -1
883 ||
884 (# else must have y1 positive, otherwise y1 and y2 both negative
885 $y1 > -1
886 # now | x2,y2 | x2,y2 x2 pos, y2 neg
887 # --------- ---------
888 # x1,y1 | | x1,y1 x1 pos or neg, y1 pos
889 # see if the X position corresponding to Y=0 is >= -1
890 &&
891 $x1*($y2-$y1) < ($y1+1)*($x2-$x1))));
892 }
893  
894 # (xnew-xp)/(x-xp) = (ylo-yp)/(y-yp)
895 # xnew-xp = (ylo-yp)/(y-yp)*(x-xp)
896 # xnew = (ylo-yp)/(y-yp)*(x-xp) + xp
897 # = x*(ylo-yp)/(y-yp) - xp*(ylo-yp)/(y-yp) + xp
898 # = x*(ylo-yp)/(y-yp) + xp*(1 - (ylo-yp)/(y-yp))
899 # = x*(ylo-yp)/(y-yp) + xp*(((y-yp) - (ylo-yp))/(y-yp)
900 # = [ x*(ylo-yp) + xp*(y - yp - ylo + yp) ]/(y-yp)
901 # = [ x*(ylo-yp) + xp*(y-ylo) ]/(y-yp)
902 #
903 # x,y
904 # / \
905 # / \
906 # / \
907 # xnew,ynew=ylo ------------------
908 # / \
909 # / \
910 # xprev,yprev xnext,ynext
911 #
912 # x,y
913 # / \
914 # / __* xnext,ynext
915 # /__--
916 # xnew,ynew=ylo --*---------------
917 # /
918 # /
919 # xprev,yprev
920 #
921 # -8,-8 7,-7
922 # *---- ----*
923 # | | | |
924 # ----* *----
925 # 7,7 -8,8
926 #
927  
928 # _convex_poly_clip() takes $aref is an arrayref of vertex coordinates
929 # $aref = [ $x1,$y1, $x2,$y2, ..., $xn,$yn ].
930 #
931 # The polygon is line segment $x1,$y1 to $x2,$y2, etc, and final
932 # $xn,$yn back to $x1,$y1 start.
933 #
934 # Modify the array contents to clip the polygon to signed 16-bit.
935 # This might either increase or decrease the total number of vertices.
936 # If the polygon is entirely outside 16-bits then leave an empty array.
937 #
938 0 ▶sub _convex_poly_clip {
Subroutine _convex_poly_clip: not called CC=9 SCAR=45.0
939 0 my ($aref) = @_;
940 ### _convex_poly_clip(): $aref
941  
942 0 foreach (1 .. 4) { # each side
943 ### side: $_
944  
945 0 ▶ for (my $i = 0; $i < $#$aref && $#$aref >= 3; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $aref->$#* and $aref->$#* >= 3) 0 0 0
Condition0%0/3
$i < $aref->$#* and $aref->$#* >= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $aref->$#* and $aref->$#* >= 3
$i < $aref->$#* $aref->$#* >= 3
Truth table0%0/3
A and B
A= $i < $aref->$#*B= $aref->$#* >= 3
ABresult
0X0
100
111
946 ### at: "i=$i of ".scalar(@$aref)." ".join(', ',@$aref)
947 0 my $y = $aref->[$i+1];
948 0 ▶ if ($y <= _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= 32767) { } 0 0 0
949 # This vertex is below the _HI limit, keep it unchanged.
950 0 $i += 2;
951  
952 } else {
953 # This vertex is outside the _HI limit, replace it by zero, one or
954 # two new clipped points.
955 0 my ($x,$y) = splice @$aref, $i,2;
956  
957 {
958 0 my $yprev = $aref->[$i-1]; # with possible wrap back to $xn,$yn
959 0 ▶ if ($yprev <= _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yprev <= 32767) { } 0 0 0
960 0 my $xprev = $aref->[$i-2];
961 0 my $xnew = int(($x*(_HI - $yprev) + $xprev*($y - _HI)) / ($y-$yprev)
962 + 0.5);
963 0 splice @$aref, $i,0, $xnew,_HI;
964 0 $i += 2;
965 } else {
966 # $yprev and $y both above _HI limit, so nothing for segment
967 # $yprev to $y, just leave $yprev for the next vertex to
968 # consider. (This case only occurs when $i==0 and so $yprev is
969 # wrapped back to the last vertex $yn. Any later $i will have
970 # $yprev already clipped to $yprev<=_HI.)
971 }
972 }
973  
974 {
975 0 my $inext = $i % scalar(@$aref);
976 0 my $ynext = $aref->[$inext+1];
977 0 ▶ if ($ynext <= _HI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ynext <= 32767) { } 0 0 0
978 0 my $xnext = $aref->[$inext];
979 0 my $xnew = int(($x*(_HI - $ynext) + $xnext*($y - _HI)) / ($y-$ynext)
980 + 0.5);
981 0 splice @$aref, $i,0, $xnew,_HI;
982 0 $i += 2;
983 } else {
984 # $y and $ynext both above _HI limit, so nothing for segment $y
985 # to $ynext
986 }
987 }
988 }
989 }
990  
991 # rotate 90
992 0 ▶ for (my $i = 0; $i < $#$aref; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $aref->$#*) 0 0 0
993 0 ($aref->[$i],$aref->[$i+1]) = ($aref->[$i+1], -1 - $aref->[$i]);
994 }
995 }
996 0 ▶ if (@$aref == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref == 2) 0 0 0
997 0 @$aref = ();
998 }
999 }
1000  
1001  
1002 4 1;
1003 __END__
« Window.pm ↑ Summary Pixmap.pm »