Drawable.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 290 CC 33 SCAR 38.8
CC
33
Cov
0%
set
110.0
colour_to_colorobj
72.0
xy
42.0
CRAP 48.5
1 # Copyright 2010, 2011, 2012 Kevin Ryde
2  
3 # This file is part of Image-Base-Gtk2.
4 #
5 # Image-Base-Gtk2 is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Gtk2 is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Gtk2. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::Gtk2::Gdk::Drawable;
20 use 5.008;
21 use strict;
22 use warnings;
23 use Carp;
24  
25 use Image::Base;
26 our @ISA = ('Image::Base');
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 our $VERSION = 11;
32  
33 sub new {
34 my ($class, %params) = @_;
35 my $self = bless { _gc_colour => '',
36 _gc_colour_pixel => -1,
37 }, $class;
38 ### Image-Base-Gtk2-Gdk-Drawable new: $self
39 $self->set (%params);
40 return $self;
41 }
42  
43 my %attr_to_get_method = (-colormap => 'get_colormap',
44 -depth => 'get_depth',
45 -screen => 'get_screen',
46 # no get_width method or property, just
47 # get_size()
48 -width => sub { ($_[0]->get_size)[0] },
49 -height => sub { ($_[0]->get_size)[1] },
50 );
51 sub _get {
52 my ($self, $key) = @_;
53  
54 if (my $method = $attr_to_get_method{$key}) {
55 return $self->{'-drawable'}->$method;
56 }
57  
58 if ($key eq '-pixmap' || $key eq '-window') { # aliasing
59 $key = '-drawable';
60 }
61 return $self->SUPER::_get($key);
62 }
63  
64 sub set {
65 my ($self, %params) = @_;
66 ### Image-Base-Gtk2-Gdk-Drawable set: \%params
67  
68 foreach my $key ('-depth', '-screen') {
69 if (exists $params{$key}) {
70 croak "Attribute $key is read-only";
71 }
72 }
73  
74 if (exists $params{'-gc'}) {
75 # when setting -gc no longer assume the current foreground pixel
76 $params{'_gc_colour'} = '';
77 $params{'_gc_pixel'} = -1;
78 }
79  
80 # aliasing
81 if (exists $params{'-pixmap'}) {
82 $params{'-drawable'} = delete $params{'-pixmap'};
83 }
84 if (exists $params{'-window'}) {
85 $params{'-drawable'} = delete $params{'-window'};
86 }
87  
88 # set -drawable now so as to apply -colormap and size to possible new one
89 if (exists $params{'-drawable'}) {
90 $self->{'-drawable'} = delete $params{'-drawable'};
91 }
92  
93 if (exists $params{'-colormap'}) {
94 $self->{'-drawable'}->set_colormap (delete $params{'-colormap'});
95 }
96  
97 my $width = delete $params{'-width'};
98 my $height = delete $params{'-height'};
99 if (defined $width || defined $height) {
100 if (! defined $width) { $width = ($self->{'-drawable'}->get_size)[0]; }
101 if (! defined $height) { $height = ($self->{'-drawable'}->get_size)[1]; }
102 $self->{'-drawable'}->resize ($width, $height);
103 }
104  
105 %$self = (%$self, %params);
106 ### set leaves: $self
107 }
108  
109 #------------------------------------------------------------------------------
110 # drawing
111  
112 sub xy {
113 my ($self, $x, $y, $colour) = @_;
114  
115 if (@_ >= 4) {
116 ### Image-Gtk2GdkDrawable xy: "$x, $y, $colour"
117 $self->{'-drawable'}->draw_point ($self->gc_for_colour($colour), $x, $y);
118  
119 } else {
120 # fetch using Pixbuf as it looks up colours
121  
122 if ($x < 0 || $y < 0) {
123 ### get_from_drawable() won't read negative points ...
124 return undef; # fetch or store
125 }
126 my $drawable = $self->{'-drawable'};
127 my ($width, $height) = $drawable->get_size;
128 if ($x >= $width || $y >= $height) {
129 ### get_from_drawable() won't read outside drawable size ...
130 return undef;
131 }
132  
133 # $drawable->get_image would be an alternative, giving the full 16-bit
134 # GdkColor components, but Gtk2::Gdk::Image data not accessible as of
135 # Perl-Gtk 1.221
136 my $pixbuf = Gtk2::Gdk::Pixbuf->new ('rgb',
137 0, # has alpha
138 8, # bits per sample
139 1,1);
140 $pixbuf->get_from_drawable ($self->{'-drawable'}, undef, $x,$y, 0,0, 1,1);
141 ### pixbuf get_pixels: length($pixbuf->get_pixels), $pixbuf->get_pixels
142 my @rgb = unpack('CCC', $pixbuf->get_pixels);
143 ### @rgb
144 if ($drawable->isa('Gtk2::Gdk::Pixmap')
145 && $drawable->get_depth == 1
146 && ! $drawable->get_colormap) {
147 return ($rgb[0]||$rgb[1]||$rgb[2] ? 1 : 0);
148 } else {
149 return sprintf '#%02X%02X%02X', @rgb
150 }
151 }
152 }
153  
154 # Crib note: no limit on how many points passed to draw_points(). The
155 # underlying xlib XDrawPoints() automatically splits into multiple PolyPoint
156 # requests as necessary.
157 #
158 sub Image_Base_Other_xy_points {
159 my $self = shift;
160 my $colour = shift;
161 ### Image_Base_Other_xy_points $colour
162 ### len: scalar(@_)
163 @_ or return;
164  
165 ### drawable: $self->{'-drawable'}
166 ### gc: $self->gc_for_colour($colour)
167 unshift @_, $self->{'-drawable'}, $self->gc_for_colour($colour);
168 ### len: scalar(@_)
169 ### $_[0]
170 ### $_[1]
171  
172 # shift/unshift changes the first two args from self,colour to drawable,gc
173 # does that save stack copying?
174 my $code = $self->{'-drawable'}->can('draw_points');
175 goto &$code;
176  
177 # the plain equivalent ...
178 # $self->{'-drawable'}->draw_points ($self->gc_for_colour($colour), @_);
179 }
180  
181 sub line {
182 my ($self, $x1,$y1, $x2,$y2, $colour) = @_;
183 ### Image-Gtk2GdkDrawable line()
184 $self->{'-drawable'}->draw_line ($self->gc_for_colour($colour),
185 $x1,$y1, $x2,$y2);
186 }
187  
188 # $x1==$x2 and $y1==$y2 on $fill==false may or may not draw that x,y point
189 # outline with gc line_width==0
190 # or alternately $drawable->draw_point ($gc, $x1,$y1);
191 #
192 sub rectangle {
193 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
194 # ### Image-Gtk2GdkDrawable rectangle: "$x1, $y1, $x2, $y2, $colour, $fill"
195  
196 $fill = !! $fill;
197 $fill ||= ($x1 == $x2 || $y1 == $y2);
198 $self->{'-drawable'}->draw_rectangle ($self->gc_for_colour($colour), $fill,
199 $x1, $y1,
200 $x2-$x1+$fill, $y2-$y1+$fill);
201  
202 # or with WidgetBits,
203 # Gtk2::Ex::GdkBits::draw_rectangle_corners
204 # ($self->{'-drawable'}
205 # $self->gc_for_colour($colour),
206 # $fill, $x1,$y1, $x2,$y2);
207  
208 }
209  
210 # Per notes in Image::Base::X11::Protocol::Drawable, a filled arc ellipse is
211 # effectively 0.5 pixel smaller. To make sure rightmost and bottom pixels
212 # are drawn for now try an unfilled on top of a filled to get that extra 0.5
213 # around the outside. Can it be done better?
214 #
215 sub ellipse {
216 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
217 ### Image-Gtk2GdkDrawable ellipse: "$x1, $y1, $x2, $y2, $colour, ".($fill?1:0)
218 my $drawable = $self->{'-drawable'};
219 my $gc = $self->gc_for_colour($colour);
220 my $w = $x2 - $x1;
221 my $h = $y2 - $y1;
222 if ($w <= 1 || $h <= 1) {
223 # 1 or 2 pixels high or wide
224 $self->{'-drawable'}->draw_rectangle ($gc, 1, # filled
225 $x1, $y1, $w+1, $h+1);
226 } else {
227 foreach my $fillarg (0 .. !!$fill) {
228 $drawable->draw_arc ($gc, $fillarg,
229 $x1, $y1, $w, $h,
230 0, 360*64); # angles in 64ths of a 360 degrees
231 }
232 }
233 }
234  
235 sub diamond {
236 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
237 ### Image-Gtk2GdkDrawable diamond: "$x1, $y1, $x2, $y2, $colour, ".($fill?1:0)
238 my $drawable = $self->{'-drawable'};
239 my $gc = $self->gc_for_colour($colour);
240  
241 if ($x1==$x2 && $y1==$y2) {
242 # 1x1 polygon draws nothing, do it as a point instead
243 $drawable->draw_point ($gc, $x1,$y1);
244  
245 } else {
246 my $xh = ($x2 - $x1);
247 my $yh = ($y2 - $y1);
248 my $xeven = ($xh & 1);
249 my $yeven = ($yh & 1);
250 $xh = int($xh / 2);
251 $yh = int($yh / 2);
252 ### assert: $x1+$xh+$xeven == $x2-$xh
253 ### assert: $y1+$yh+$yeven == $y2-$yh
254  
255 my @points = ($x1+$xh, $y1, # top centre
256  
257 # left
258 $x1, $y1+$yh,
259 ($yeven ? ($x1, $y2-$yh) : ()),
260  
261 # bottom
262 $x1+$xh, $y2,
263 ($xeven ? ($x2-$xh, $y2) : ()),
264  
265 # right
266 ($yeven ? ($x2, $y2-$yh) : ()),
267 $x2, $y1+$yh,
268  
269 ($xeven ? ($x2-$xh, $y1) : ()),
270 $x1+$xh, $y1); # back to start
271 foreach my $fillarg (0 .. !!$fill) {
272 $drawable->draw_polygon ($gc,
273 $fillarg,
274 @points);
275 }
276 }
277 }
278  
279 # return '-gc' with its foreground set to $colour
280 # -gc is created if not already set
281 # the colour set is recorded to save work if the next drawing is the same
282 #
283 sub gc_for_colour {
284 my ($self, $colour) = @_;
285 my $gc = $self->{'-gc'};
286 if ($colour ne $self->{'_gc_colour'}) {
287 ### gc_for_colour change: $colour
288  
289 my $colorobj = $self->colour_to_colorobj($colour);
290 ### pixel: sprintf("%#X",$colorobj->pixel)
291  
292 $self->{'_gc_colour'} = $colour;
293 if ($colorobj->pixel ne $self->{'_gc_colour_pixel'}) {
294 $self->{'_gc_colour_pixel'} = $colorobj->pixel;
295 if (! $gc) {
296 return ($self->{'-gc'}
297 = Gtk2::Gdk::GC->new ($self->{'-drawable'},
298 { foreground => $colorobj }));
299 }
300 $gc->set_foreground ($colorobj);
301 }
302 }
303 return $gc;
304 }
305  
306 sub colour_to_colorobj {
307 my ($self, $colour) = @_;
308 if ($colour =~ /^\d+$/) {
309 return Gtk2::Gdk::Color->new (0,0,0, $colour);
310 }
311 if ($colour eq 'set') {
312 return Gtk2::Gdk::Color->new (0,0,0, 1);
313 }
314 if ($colour eq 'clear') {
315 return Gtk2::Gdk::Color->new (0,0,0, 0);
316 }
317  
318 my $drawable = $self->{'-drawable'};
319 my $colormap = $drawable->get_colormap;
320 if (! $colormap) {
321 if ($drawable->get_depth == 1) {
322 if ($colour =~ /^#(000)+$/) {
323 return Gtk2::Gdk::Color->new (0,0,0, 0);
324 } elsif ($colour =~ /^#(FFF)+$/i) {
325 return Gtk2::Gdk::Color->new (0,0,0, 1);
326 }
327 }
328 croak "No colormap to interpret colour: $colour";
329 }
330  
331 # think parse and rgb_find are client-side operations, no need to cache
332 # the results
333 #
334 my $colorobj = Gtk2::Gdk::Color->parse ($colour)
335 || croak "Cannot parse colour: $colour";
336 $colormap->rgb_find_color ($colorobj);
337 return $colorobj;
338 }
339  
340 1;
341 __END__