DC.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 192 CC 18 SCAR 26.7
CC
18
Cov
0%
diamond
42.0
_dc_fill
20.0
set
12.0
CRAP 14.4
1 # Copyright 2011, 2012, 2019 Kevin Ryde
2  
3 # This file is part of Image-Base-Wx.
4 #
5 # Image-Base-Wx 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-Wx 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-Wx. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Image::Base::Wx::DC;
19 use 5.008;
20 use strict;
21 use Carp;
22 use Wx;
23 our $VERSION = 6;
24  
25 use Image::Base;
26 our @ISA = ('Image::Base');
27  
28 # uncomment this to run the ### lines
29 # use Smart::Comments;
30  
31  
32 sub new {
33 my ($class, %params) = @_;
34 ### Wx-DC new(): %params
35  
36 if (ref $class) {
37 die 'Cannot clone Image::Base::Wx::DC';
38 }
39 my $self = bless { _pen_colour => '',
40 _brush_colour => '',
41 }, $class;
42 $self->set (%params);
43 return $self;
44 }
45  
46 my %attr_to_get_method = (-width => sub { ($_[0]->GetSizeWH)[0] },
47 -height => sub { ($_[0]->GetSizeWH)[1] },
48 );
49 sub _get {
50 my ($self, $key) = @_;
51  
52 if (my $method = $attr_to_get_method{$key}) {
53 return $self->{'-dc'}->$method();
54 }
55 return $self->SUPER::_get($key);
56 }
57  
58 sub set {
59 my ($self, %params) = @_;
60 ### Image-Base-Wx-DC set: \%params
61  
62 foreach my $key ('-width','-height') {
63 if (exists $params{$key}) {
64 croak "Attribute $key is read-only";
65 }
66 }
67  
68 if (exists $params{'-dc'}) {
69 $params{'_pen_colour'} = '';
70 $params{'_brush_colour'} = '';
71  
72 ### dc pen apply CAP_PROJECTING ...
73 my $dc = $params{'-dc'};
74 my $pen = $dc->GetPen;
75 $pen->SetCap(Wx::wxCAP_PROJECTING());
76 $dc->SetPen($pen);
77 }
78  
79 %$self = (%$self, %params);
80 ### set leaves: $self
81 }
82  
83 #------------------------------------------------------------------------------
84 # drawing
85  
86 sub xy {
87 my ($self, $x, $y, $colour) = @_;
88 my $dc = $self->{'-dc'};
89 if (@_ >= 4) {
90 ### Image-DC xy: "$x, $y, $colour"
91 _dc_pen($self,$colour)->DrawPoint ($x, $y);
92 } else {
93 ### Image-DC xy() fetch: "$x, $y"
94 my $c = $self->{'-dc'}->GetPixel ($x,$y);
95 ### pixel: $c
96 ### c str: $c->GetAsString(4)
97 ### c Red : $c->Red
98 ### c Green: $c->Green
99 ### c Blue : $c->Blue
100 ### c Alpha: $c->Alpha
101 return ($c && $c->GetAsString(Wx::wxC2S_HTML_SYNTAX()));
102 }
103 }
104  
105 # sub Image_Base_Other_xy_points {
106 # my $self = shift;
107 # my $colour = shift;
108 # ### Image_Base_Other_xy_points $colour
109 # ### len: scalar(@_)
110 # @_ or return;
111 #
112 # ### dc: $self->{'-dc'}
113 # ### brush: $self->brush_for_colour($colour)
114 # unshift @_, $self->{'-dc'}, $self->brush_for_colour($colour);
115 # ### len: scalar(@_)
116 # ### $_[0]
117 # ### $_[1]
118 #
119 # # shift/unshift changes the first two args from self,colour to dc,brush
120 # # does that save stack copying?
121 # my $code = $self->{'-dc'}->can('draw_points');
122 # goto &$code;
123 #
124 # # the plain equivalent ...
125 # # $self->{'-dc'}->draw_points ($self->brush_for_colour($colour), @_);
126 # }
127  
128 sub line {
129 my ($self, $x1,$y1, $x2,$y2, $colour) = @_;
130 ### Image-DC line()
131  
132 # 1x1 DrawLine() draws nothing, so use DrawPoint() for that
133 my $dc = _dc_pen($self,$colour);
134 if ($x1 == $x2 && $y1 == $y2) {
135 $dc->DrawPoint ($x1, $y1);
136 } else {
137 $dc->DrawLine ($x1,$y1, $x2,$y2);
138 }
139 }
140  
141 # $x1==$x2 and $y1==$y2 on $fill==false may or may not draw that x,y point
142 # outline with brush line_width==0
143 # or alternately $dc->draw_point ($brush, $x1,$y1);
144 #
145 sub rectangle {
146 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
147 # ### Image-DC rectangle: "$x1, $y1, $x2, $y2, $colour, $fill"
148  
149 # Under msdos a 1x1 rectangle seems to draw no pixels, neither for filled
150 # or unfilled. Try DrawPoint() for that case.
151 if ($x1==$x2 && $y1==$y2) {
152 _dc_pen($self,$colour)->DrawPoint ($x1, $y1);
153 } else {
154 _dc_fill($self,$colour,$fill)->DrawRectangle ($x1, $y1,
155 $x2-$x1+1, $y2-$y1+1);
156 }
157 }
158  
159 my $ellipse_x_extra = 0;
160 my $ellipse_y_extra = 0;
161 # {
162 # my $wxbitmap = Wx::Bitmap->new(20,10);
163 # my $dc = Wx::MemoryDC->new;
164 # $dc->SelectObject($wxbitmap);
165 # {
166 # my $pen = $dc->GetPen;
167 # my $colour_obj = Wx::Colour->new('#FF00FF');
168 # $colour_obj->IsOk or die;
169 # $pen->SetColour($colour_obj);
170 # $dc->SetPen($pen);
171 # }
172 # for ($ellipse_x_extra = -3; $ellipse_x_extra <= 2; $ellipse_x_extra++) {
173 # $dc->DrawEllipse(0,0, 6+$ellipse_x_extra, 6+$ellipse_y_extra);
174 # my $colour_obj = $dc->GetPixel(5,2);
175 # if ($colour_obj->GetAsString(Wx::wxC2S_HTML_SYNTAX()) eq '#FF00FF') {
176 # last;
177 # }
178 # }
179 # for ($ellipse_y_extra = -3; $ellipse_y_extra <= 2; $ellipse_y_extra++) {
180 # $dc->DrawEllipse(0,0, 6+$ellipse_x_extra, 6+$ellipse_y_extra);
181 # my $colour_obj = $dc->GetPixel(2,5);
182 # if ($colour_obj->GetAsString(Wx::wxC2S_HTML_SYNTAX()) eq '#FF00FF') {
183 # last;
184 # }
185 # }
186 # }
187 ### $ellipse_x_extra
188 ### $ellipse_y_extra
189  
190 sub ellipse {
191 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
192 ### Image-DC ellipse: "$x1, $y1, $x2, $y2, $colour, ".($fill||0)
193  
194 # Something fishy happens when width=0 or height=0 to DrawEllipse() where
195 # the last pixel is not drawn. Might be the usual X11 left/above rule, or
196 # wx not coping with that rule. In any case Nx1 and 1xN done as
197 # rectangle() (and it in turn handles 1x1 case).
198 #
199 my $w = $x2-$x1;
200 my $h = $y2-$y1;
201 if ($w == 0 || $h == 0) {
202 $self->rectangle ($x1,$y1, $x2,$y2, $colour, 1);
203 } else {
204 _dc_fill($self,$colour,$fill)->DrawEllipse ($x1,$y1,
205 $w + $ellipse_x_extra,
206 $h + $ellipse_y_extra);
207 }
208 }
209  
210 sub diamond {
211 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
212 ### Image-DC diamond: "$x1, $y1, $x2, $y2, $colour, ".($fill||0)
213  
214 if ($x1==$x2 && $y1==$y2) {
215 # Under msdos a polygon with all points the same seems to draw no pixels.
216 # Try DrawPoint() instead.
217 _dc_pen($self,$colour)->DrawPoint ($x1, $y1);
218 return;
219 }
220  
221 my $xh = ($x2 - $x1);
222 my $yh = ($y2 - $y1);
223 my $xeven = ($xh & 1);
224 my $yeven = ($yh & 1);
225 $xh = int($xh / 2);
226 $yh = int($yh / 2);
227 ### assert: $x1+$xh+$xeven == $x2-$xh
228 ### assert: $y1+$yh+$yeven == $y2-$yh
229  
230 _dc_fill($self,$colour,$fill)->DrawPolygon
231 ([
232 Wx::Point->new($x1+$xh, $y1), # top centre
233  
234 # left
235 Wx::Point->new($x1, $y1+$yh),
236 ($yeven ? Wx::Point->new($x1, $y2-$yh) : ()),
237  
238 # bottom
239 Wx::Point->new($x1+$xh, $y2),
240 ($xeven ? Wx::Point->new($x2-$xh, $y2) : ()),
241  
242 # right
243 ($yeven ? Wx::Point->new($x2, $y2-$yh) : ()),
244 Wx::Point->new($x2, $y1+$yh),
245  
246 ($xeven ? Wx::Point->new($x2-$xh, $y1) : ()),
247 Wx::Point->new($x1+$xh, $y1), # back to start
248 ],
249 0,0);
250 }
251  
252 #------------------------------------------------------------------------------
253 # colours
254  
255 sub _dc_fill {
256 my ($self, $colour, $fill) = @_;
257  
258 my $dc = _dc_pen($self,$colour);
259 if ($fill) {
260 if ($colour ne $self->{'_brush_colour'}) {
261 ### _dc_fill() change brush: $colour, $fill
262  
263 my $brush = $dc->GetBrush;
264 $brush->SetColour(Wx::Colour->new($colour));
265 $brush->SetStyle (Wx::wxSOLID());
266 $dc->SetBrush($brush);
267  
268 $self->{'_brush_colour'} = $colour;
269 }
270 } else {
271 if ($self->{'_brush_colour'} ne 'None') {
272 ### _dc_fill() change brush transparent ...
273  
274 # or ...
275 # $dc->SetBrush (Wx::wxTRANSPARENT_BRUSH());
276  
277 my $brush = $dc->GetBrush;
278 $brush->SetStyle (Wx::wxTRANSPARENT());
279 $dc->SetBrush($brush);
280  
281 $self->{'_brush_colour'} = 'None';
282 }
283 }
284 return $dc;
285 }
286  
287 sub _dc_pen {
288 my ($self, $colour) = @_;
289 my $dc = $self->{'-dc'};
290 if ($colour ne $self->{'_pen_colour'}) {
291 ### _dc_pen() change: $colour
292  
293 my $pen = $dc->GetPen;
294 my $colour_obj = Wx::Colour->new($colour);
295 $colour_obj->IsOk
296 or croak "Unrecognised colour ",$colour;
297 $pen->SetColour($colour_obj);
298 $dc->SetPen($pen);
299  
300 ### $colour_obj
301 ### colour_obj Red : $colour_obj->Red
302 ### colour_obj Green: $colour_obj->Green
303 ### colour_obj Blue : $colour_obj->Blue
304  
305 $self->{'_pen_colour'} = $colour;
306 }
307 return $dc;
308 }
309  
310 1;
311 __END__