| 1 | # wrong angle on coming back after multiple skipped off screen, Kirshenbaum BHP | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | # x1,y1 x2,inf -> x1,y1 | ||
| 6 | |||
| 7 | # x1,-inf x2,inf -> mean x1,x2 | ||
| 8 | |||
| 9 | # x1,y1 inf,inf -> 45deg to corner | ||
| 10 | |||
| 11 | # x1,-inf inf,inf -> mean is inf, offscreen | ||
| 12 | |||
| 13 | |||
| 14 | # segments drawing with clip, for annotation lines | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | # Copyright 2007, 2008, 2009, 2010 Kevin Ryde | ||
| 20 | |||
| 21 | # This file is part of Chart. | ||
| 22 | # | ||
| 23 | # Chart is free software; you can redistribute it and/or | ||
| 24 | # modify it under the terms of the GNU General Public License as published | ||
| 25 | # by the Free Software Foundation; either version 3, or (at your option) any | ||
| 26 | # later version. | ||
| 27 | # | ||
| 28 | # Chart is distributed in the hope that it will be useful, | ||
| 29 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 31 | # Public License for more details. | ||
| 32 | # | ||
| 33 | # You should have received a copy of the GNU General Public License along | ||
| 34 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 35 | |||
| 36 | package App::Chart::Gtk2::Ex::LineClipper; | ||
| 37 | use 5.008; | ||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | use Carp; | ||
| 41 | use Gtk2; | ||
| 42 | use List::Util qw(min max); | ||
| 43 | |||
| 44 | use constant DEBUG => 0; | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my ($class, %self) = @_; | ||
| 48 | |||
| 49 | if (! exists $self{'drawable'}) { | ||
| 50 | croak "Missing drawable for LineClipper"; | ||
| 51 | } | ||
| 52 | |||
| 53 | # default yes for draw_points | ||
| 54 | if (! exists $self{'draw_points'}) { | ||
| 55 | $self{'draw_points'} = 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | $self{'points'} = []; | ||
| 59 | return bless \%self, $class; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub add { | ||
| 63 | my ($self, $gc, $x, $y) = @_; | ||
| 64 | my $points = $self->{'points'}; | ||
| 65 | |||
| 66 | # if the next segment is a new gc and there's more than a single point, | ||
| 67 | # then draw the previous segments with the previous gc | ||
| 68 | if (@$points > 2 && $gc != $self->{'gc'}) { | ||
| 69 | my $x1 = $points->[-2]; | ||
| 70 | my $y1 = $points->[-1]; | ||
| 71 | $self->end; | ||
| 72 | @$points = ($x1, $y1, $x, $y); | ||
| 73 | } else { | ||
| 74 | push @$points, $x, $y; | ||
| 75 | } | ||
| 76 | $self->{'gc'} = $gc; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub end { | ||
| 80 | my ($self) = @_; | ||
| 81 | |||
| 82 | my $points = $self->{'points'}; | ||
| 83 | if (! @$points) { | ||
| 84 | # nothing to draw at all | ||
| 85 | return | ||
| 86 | } | ||
| 87 | |||
| 88 | if (@$points == 2 && ! $self->{'draw_point'}) { | ||
| 89 | # only a single point, and single point drawing not wanted | ||
| 90 | @$points = (); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | clipped_draw_lines ($self->{'drawable'}, | ||
| 95 | $self->{'gc'}, | ||
| 96 | max (1, $self->{'line_width'} || 1), | ||
| 97 | $points); | ||
| 98 | @$points = (); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub clipped_draw_lines { | ||
| 102 | my ($drawable, $gc, $line_width, $points) = @_; | ||
| 103 | |||
| 104 | my ($width, $height) = $drawable->get_size; | ||
| 105 | my $clip_x_low = 0 - $line_width; | ||
| 106 | my $clip_x_high = $width + $line_width; | ||
| 107 | my $clip_y_low = 0 - $line_width; | ||
| 108 | my $clip_y_high = $height + $line_width; | ||
| 109 | |||
| 110 | if (@$points == 2) { | ||
| 111 | # single point | ||
| 112 | my $x = $points->[0]; | ||
| 113 | my $y = $points->[1]; | ||
| 114 | if (DEBUG) { print "point $x,$y $line_width\n"; } | ||
| 115 | if ($x < $clip_x_low || $x > $clip_x_high | ||
| 116 | || $y < $clip_y_low || $y > $clip_y_high) { | ||
| 117 | # outside the drawable, do nothing | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($line_width <= 1) { | ||
| 122 | $drawable->draw_point ($gc, $x, $y); | ||
| 123 | } else { | ||
| 124 | my $offset = int ($line_width / 2); # round down | ||
| 125 | $drawable->draw_arc ($gc, 1, | ||
| 126 | $x - $offset, $y - $offset, | ||
| 127 | $line_width, $line_width, | ||
| 128 | 0, 23040); # 360*64 for full circle | ||
| 129 | } | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | if (DEBUG) { print "draw $gc ", scalar(@$points), " coords\n"; } | ||
| 134 | |||
| 135 | my @draw = (); | ||
| 136 | my $x1 = $points->[0]; | ||
| 137 | my $y1 = $points->[1]; | ||
| 138 | |||
| 139 | for (my $i = 2; $i+1 <= $#$points; $i += 2) { | ||
| 140 | my $x2 = $points->[$i]; | ||
| 141 | my $y2 = $points->[$i+1]; | ||
| 142 | |||
| 143 | if ( ($x1 <= $clip_x_low && $x2 <= $clip_x_low) | ||
| 144 | || ($x1 >= $clip_x_high && $x2 <= $clip_x_high)) { | ||
| 145 | if (DEBUG) { print " X coords outside drawable, skip: $x1 $x2\n"; } | ||
| 146 | $x1 = $x2; | ||
| 147 | $y1 = $y2; | ||
| 148 | next; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $end_clipped = 0; | ||
| 152 | |||
| 153 | if ($x1 <= $clip_x_low && $x2 > $clip_x_low) { | ||
| 154 | $y1 = ($y1 * ($x2 - $clip_x_low) + $y2 * ($clip_x_low - $x1)) | ||
| 155 | / ($x2 - $x1); | ||
| 156 | $x1 = $clip_x_low; | ||
| 157 | } elsif ($x2 <= $clip_x_low && $x1 > $clip_x_low) { | ||
| 158 | $y2 = ($y2 * ($x1 - $clip_x_low) + $y1 * ($clip_x_low - $x2)) | ||
| 159 | / ($x1 - $x2); | ||
| 160 | $x2 = $clip_x_low; | ||
| 161 | $end_clipped = 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | if ($x1 <= $clip_x_high && $x2 > $clip_x_high) { | ||
| 165 | $y2 = ($y2 * ($x1 - $clip_x_high) + $y1 * ($clip_x_high - $x2)) | ||
| 166 | / ($x1 - $x2); | ||
| 167 | $x2 = $clip_x_high; | ||
| 168 | $end_clipped = 1; | ||
| 169 | } elsif ($x2 <= $clip_x_high && $x1 > $clip_x_high) { | ||
| 170 | $y1 = ($y1 * ($x2 - $clip_x_high) + $y2 * ($clip_x_high - $x1)) | ||
| 171 | / ($x2 - $x1); | ||
| 172 | $x1 = $clip_x_high; | ||
| 173 | } | ||
| 174 | |||
| 175 | if ( ($y1 <= $clip_y_low && $y2 <= $clip_y_low) | ||
| 176 | || ($y1 >= $clip_y_high && $y2 >= $clip_y_high)) { | ||
| 177 | if (DEBUG) { print " Y coords outside drawable, skip: $y1 $y2\n"; } | ||
| 178 | $x1 = $x2; | ||
| 179 | $y1 = $y2; | ||
| 180 | next; | ||
| 181 | } | ||
| 182 | |||
| 183 | if ($y1 <= $clip_y_low && $y2 > $clip_y_low) { | ||
| 184 | $x1 = ($x1 * ($y2 - $clip_y_low) + $x2 * ($clip_y_low - $y1)) | ||
| 185 | / ($y2 - $y1); | ||
| 186 | $y1 = $clip_y_low; | ||
| 187 | } elsif ($y2 <= $clip_y_low && $y1 > $clip_y_low) { | ||
| 188 | $x2 = ($x2 * ($y2 - $clip_y_low) + $x1 * ($clip_y_low - $y1)) | ||
| 189 | / ($y2 - $y1); | ||
| 190 | $y2 = $clip_y_low; | ||
| 191 | $end_clipped = 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | if ($y1 <= $clip_y_high && $y2 > $clip_y_high) { | ||
| 195 | $x2 = ($x2 * ($y2 - $clip_y_high) + $x1 * ($clip_y_high - $y1)) | ||
| 196 | / ($y2 - $y1); | ||
| 197 | $y2 = $clip_y_high; | ||
| 198 | $end_clipped = 1; | ||
| 199 | } elsif ($y2 <= $clip_y_high && $y1 > $clip_y_high) { | ||
| 200 | $x1 = ($x1 * ($y2 - $clip_y_high) + $x2 * ($clip_y_high - $y1)) | ||
| 201 | / ($y2 - $y1); | ||
| 202 | $y1 = $clip_y_high; | ||
| 203 | } | ||
| 204 | |||
| 205 | if (! @draw) { | ||
| 206 | push @draw, $x1, $y1; | ||
| 207 | } | ||
| 208 | push @draw, $x2, $y2; | ||
| 209 | if ($end_clipped) { | ||
| 210 | if (DEBUG) { print " end clipped, draw ", join(' ', @draw), "\n"; } | ||
| 211 | $drawable->draw_lines ($gc, @draw); | ||
| 212 | @draw = (); | ||
| 213 | } | ||
| 214 | $x1 = $x2; | ||
| 215 | $y1 = $y2; | ||
| 216 | } | ||
| 217 | if (DEBUG) { print " final draw ", join(' ', @draw), "\n"; } | ||
| 218 | if (@draw) { | ||
| 219 | $drawable->draw_lines ($gc, @draw); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | sub DESTROY { | ||
| 224 | my ($self) = @_; | ||
| 225 | $self->end; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | 1; | ||
| 231 | __END__ | ||
| 232 | |||
| 233 | =for stopwords Gdk pixmap Eg GC LineClipper ie GCs | ||
| 234 | |||
| 235 | =head1 NAME | ||
| 236 | |||
| 237 | App::Chart::Gtk2::Ex::LineClipper -- accumulate and/or draw connected line segments | ||
| 238 | |||
| 239 | =for test_synopsis my ($drawable, $gc, $line_width, $points_list, $x1, $y1, $x2, $y2) | ||
| 240 | |||
| 241 | =head1 SYNOPSIS | ||
| 242 | |||
| 243 | use App::Chart::Gtk2::Ex::LineClipper; | ||
| 244 | |||
| 245 | # direct draw | ||
| 246 | App::Chart::Gtk2::Ex::LineClipper::clipped_draw_lines | ||
| 247 | ($drawable, $gc, $line_width, $points_list); | ||
| 248 | |||
| 249 | # OOP accumulator | ||
| 250 | my $linedrawer = App::Chart::Gtk2::Ex::LineClipper->new (drawable => $drawable); | ||
| 251 | $linedrawer->add ($gc, $x1, $y1); | ||
| 252 | $linedrawer->add ($gc, $x2, $y2); | ||
| 253 | $linedrawer->end; | ||
| 254 | |||
| 255 | =head1 DESCRIPTION | ||
| 256 | |||
| 257 | C<App::Chart::Gtk2::Ex::LineClipper> helps you draw connected line segments like | ||
| 258 | C<< Gtk2::Drawable->draw_lines >> does, but with benefits of clipping wild | ||
| 259 | coordinates (and not even sent to the server if not visible), and an | ||
| 260 | accumulator mechanism to build line sequences. | ||
| 261 | |||
| 262 | Clipping wild X,Y values on the client side is important because Gdk | ||
| 263 | silently takes just the low 16 bits of each. For example if you draw a line | ||
| 264 | from 100,50 to 65736,50 you'll be unpleasantly surprised to find X=65736 | ||
| 265 | comes out as X=200 (its low 16 bits), instead of extending to the far right | ||
| 266 | hand end of the window. | ||
| 267 | |||
| 268 | The object-oriented accumulator is helpful if you've got a tricky loop | ||
| 269 | generating points and colours for each segment and want someone else to keep | ||
| 270 | track of what, if anything, you build up to draw. | ||
| 271 | |||
| 272 | =head1 DIRECT DRAWING | ||
| 273 | |||
| 274 | =over 4 | ||
| 275 | |||
| 276 | =item C<< App::Chart::Gtk2::Ex::LineClipper::clipped_draw_lines ($drawable, $gc, $line_width, $points) >> | ||
| 277 | |||
| 278 | Draw lines connecting the X,Y points in C<$points>, like | ||
| 279 | C<Gtk2::Drawable::draw_lines> does, but clipping to the size of the drawable | ||
| 280 | so huge coordinate values don't wrap around, and indeed are not sent to the | ||
| 281 | server at all if completely off-screen. | ||
| 282 | |||
| 283 | C<$points> is a reference to an array of X,Y values, one after the other. | ||
| 284 | C<$drawable> is a C<Gtk2::Gdk::Drawable> (window, pixmap, etc) and C<$gc> is | ||
| 285 | a C<Gtk2::Gdk::GC>. Eg. | ||
| 286 | |||
| 287 | App::Chart::Gtk2::Ex::LineClipper::clipped_draw_lines | ||
| 288 | ($drawable, $gc, 1, [ 100,100, 200,200, 300,100 ]); | ||
| 289 | |||
| 290 | C<$line_width> should be the width in pixels of the lines C<$gc> will draw. | ||
| 291 | This is used to know how far off the drawable the clipping must extend so | ||
| 292 | the C<cap_style> doesn't show. C<clipped_draw_lines> doesn't simply read | ||
| 293 | C<$line_width> from C<< $gc->get_values >> because that call does a | ||
| 294 | round-trip to the X server. A width bigger than actually in use is fine; | ||
| 295 | for instance you might just pass 20 pixels if you know you never draw lines | ||
| 296 | wider than that. A width 0 is interpreted as a 1 pixel "thin line", the | ||
| 297 | same as happens in the GC. | ||
| 298 | |||
| 299 | If C<$points> is an empty array, or an array of just one X,Y point, then | ||
| 300 | nothing at all is drawn. This handling of one point adds certainty to what | ||
| 301 | plain C<< $win->draw_lines >> does; for it a single X,Y of a 0-width "thin | ||
| 302 | line" might or might not be drawn, depending on the server. | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =head1 ACCUMULATOR OBJECT | ||
| 307 | |||
| 308 | =over 4 | ||
| 309 | |||
| 310 | =item C<< App::Chart::Gtk2::Ex::LineClipper->new (drawable => $drawable, ...) >> | ||
| 311 | |||
| 312 | Create and return a new LineClipper object to accumulate and draw connected | ||
| 313 | line segments on C<$drawable> using C<clipped_draw_lines> above. | ||
| 314 | C<$drawable> is a C<Gtk2::Gdk::Drawable> object as above, and a | ||
| 315 | C<line_width> parameter can be passed to set that for the clipping (again as | ||
| 316 | above). | ||
| 317 | |||
| 318 | my $linedrawer = App::Chart::Gtk2::Ex::LineClipper->new | ||
| 319 | (drawable => $drawable, | ||
| 320 | line_width => 5); # pixels | ||
| 321 | |||
| 322 | By default a single solitary X,Y point is not drawn, on the principle that | ||
| 323 | it's not a line segment, but the C<draw_point> option can be set true to | ||
| 324 | have it shown as a circle of the given C<line_width>. Eg. | ||
| 325 | |||
| 326 | my $linedrawer = App::Chart::Gtk2::Ex::LineClipper->new | ||
| 327 | (drawable => $drawable, | ||
| 328 | line_width => 5, # pixels | ||
| 329 | draw_point => 1); # true | ||
| 330 | |||
| 331 | If you're wondering that C<line_width> used as a diameter won't come out | ||
| 332 | circular if the pixels aren't square, well, yes, but the same is true of the | ||
| 333 | line drawing. The single width used vertically and horizontally for the | ||
| 334 | line segments makes them appear wider or narrower according to their angle. | ||
| 335 | |||
| 336 | =item C<< $linedrawer->add ($gc, $x, $y) >> | ||
| 337 | |||
| 338 | Add a point to C<$linedrawer>. The points accumulated will have connected line | ||
| 339 | segments drawn from each to the next, in the order they're added. | ||
| 340 | |||
| 341 | C<$gc> is a C<Gtk2::Gdk::GC> for the new segment, ie. from the current | ||
| 342 | endpoint to the new C<$x>,C<$y>. Different GCs can be used for different | ||
| 343 | segments and the LineClipper takes care of passing runs of the same GC to | ||
| 344 | C<clipped_draw_lines>. | ||
| 345 | |||
| 346 | =item C<< $linedrawer->end() >> | ||
| 347 | |||
| 348 | Draw the line segments accumulated, if any, and set C<$linedrawer> back to | ||
| 349 | empty, ready to start a new completely separate sequence of points. | ||
| 350 | |||
| 351 | You can use this to force a gap between points, ie. draw everything | ||
| 352 | accumulated so far and make subsequent points a new run. | ||
| 353 | |||
| 354 | C<end> is called automatically when C<$linedrawer> is destroyed, which means | ||
| 355 | that if you've got a complicated loop generating the points then you can | ||
| 356 | just C<return> or jump out from multiple places, confident that letting the | ||
| 357 | LineClipper go out of scope will flush what you've accumulated. | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | =head1 OTHER NOTES | ||
| 362 | |||
| 363 | The choice between the direct C<clipped_draw_lines> and the accumulator | ||
| 364 | object is purely a matter of convenience. If you've got all your values in | ||
| 365 | an array and want to draw just one colour (one GC) then the best idea is to | ||
| 366 | C<map> or whatever to scale and build an X,Y array, then call | ||
| 367 | C<clipped_draw_lines>. But if you're going to make decisions about skipping | ||
| 368 | some points or changing colours or leaving gaps while passing over the data | ||
| 369 | then you may find the accumulator easier. | ||
| 370 | |||
| 371 | When switching to a different GC with C<add>, the join between the lines in | ||
| 372 | the old and new GCs doesn't use the join style (C<Gtk2::Gdk::JoinStyle>) of | ||
| 373 | either GC, but rather merely gets the cap style (C<Gtk2::Gdk::CapStyle>) of | ||
| 374 | each where the C<draw_lines> from one ends and the other begins. This means | ||
| 375 | you can't get a nice mitre or bevel when changing colours etc. For now it | ||
| 376 | seems far too much trouble to try to do anything about that. The suggestion | ||
| 377 | would be to use a cap style of C<round> which comes out looking nice at any | ||
| 378 | angle. Or if you only do GC switching with lines a few pixels wide then one | ||
| 379 | or two slightly off where they meet will be hardly noticeable. | ||
| 380 | |||
| 381 | =head1 SEE ALSO | ||
| 382 | |||
| 383 | L<Gtk2::Gdk::Drawable>, L<Gtk2::Gdk::GC>, L<Gtk2::Ex::WidgetBits> | ||
| 384 | |||
| 385 | =cut |