| 1 | package Gadabout::Imlib2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use POSIX; | ||
| 5 | use Image::Imlib2; | ||
| 6 | use Gadabout; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | use vars qw(@ISA $VERSION); | ||
| 10 | |||
| 11 | @ISA = qw(Gadabout); | ||
| 12 | $VERSION = '1.0001'; | ||
| 13 | |||
| 14 | sub new{ | ||
| 15 | my $self = shift; | ||
| 16 | my $class = ref($self) || $self; | ||
| 17 | $self = $class->SUPER::new; | ||
| 18 | bless $self, $class; | ||
| 19 | $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | sub InitImage{ | ||
| 24 | my $self = shift; | ||
| 25 | $self->{im} = Image::Imlib2->new($self->{image_width}, $self->{image_height}); | ||
| 26 | $self->AddFontPath('.'); | ||
| 27 | $self->SetFont("arial/8"); | ||
| 28 | $self->{im}->set_colour(255, 255, 255, 255); | ||
| 29 | $self->{im}->fill_rectangle(0,0,$self->{image_width},$self->{image_height}); | ||
| 30 | } #sub InitImage | ||
| 31 | |||
| 32 | sub SetFont{ | ||
| 33 | my $self = shift; | ||
| 34 | $self->{font} = $self->GetImageFont(shift); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub AddFontPath { | ||
| 38 | my $self = shift; | ||
| 39 | for (@_) { | ||
| 40 | $self->{im}->add_font_path($_); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | sub GetImageFont{ | ||
| 44 | my $self = shift; | ||
| 45 | my $font = shift; | ||
| 46 | return $self->{im}->load_font($font); | ||
| 47 | } # sub GetImageFont | ||
| 48 | |||
| 49 | |||
| 50 | sub DrawLine{ | ||
| 51 | my $self = shift; | ||
| 52 | my $x1 = shift; | ||
| 53 | my $y1 = shift; | ||
| 54 | my $x2 = shift; | ||
| 55 | my $y2 = shift; | ||
| 56 | my $color = shift; | ||
| 57 | $color = $self->{color}{$color}; | ||
| 58 | |||
| 59 | if (defined($color->{"red"})){ | ||
| 60 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 61 | $self->{im}->draw_line($x1, $y1, $x2, $y2); | ||
| 62 | return 1; | ||
| 63 | } else { | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | } # sub DrawLine | ||
| 67 | |||
| 68 | sub DrawDashedLine{ | ||
| 69 | my $self = shift; | ||
| 70 | my $x1 = shift; | ||
| 71 | my $y1 = shift; | ||
| 72 | my $x2 = shift; | ||
| 73 | my $y2 = shift; | ||
| 74 | my $dash_length = shift; | ||
| 75 | my $dash_space = shift; | ||
| 76 | my $color = shift; | ||
| 77 | $color = $self->{color}{$color}; | ||
| 78 | |||
| 79 | my $line_length = $self->max(ceil (sqrt((($x2 - $x1)**2) + (($y2 - $y1)**2)) ),2); | ||
| 80 | |||
| 81 | my $cosTheta = ($x2 - $x1) / $line_length; | ||
| 82 | my $sinTheta = ($y2 - $y1) / $line_length; | ||
| 83 | my $lastx = $x1; | ||
| 84 | my $lasty = $y1; | ||
| 85 | |||
| 86 | for (my $i = 0; $i < $line_length; $i += ($dash_length + $dash_space)) { | ||
| 87 | my $x = ($dash_length * $cosTheta) + $lastx; | ||
| 88 | my $y = ($dash_length * $sinTheta) + $lasty; | ||
| 89 | |||
| 90 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 91 | $self->{im}->draw_line($lastx,$lasty,$x,$y); | ||
| 92 | $lastx = $x + ($dash_space * $cosTheta); | ||
| 93 | $lasty = $y + ($dash_space * $sinTheta); | ||
| 94 | } | ||
| 95 | } #sub DrawDashedLine | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | sub GetTextSize{ | ||
| 100 | my $self = shift; | ||
| 101 | my $string = shift; | ||
| 102 | my $direction = shift; | ||
| 103 | my $font = shift; | ||
| 104 | |||
| 105 | if (!defined($font)) { | ||
| 106 | $font = $self->{font}; | ||
| 107 | } | ||
| 108 | my ($width, $height) = $self->{im}->get_text_size($string, $direction); | ||
| 109 | |||
| 110 | return {"width" => $width, "height" => $height}; | ||
| 111 | } # sub GetTextSize | ||
| 112 | |||
| 113 | |||
| 114 | sub DrawText{ | ||
| 115 | my $self = shift; | ||
| 116 | my $font = shift; | ||
| 117 | my $x = shift; | ||
| 118 | my $y = shift; | ||
| 119 | my $text = shift; | ||
| 120 | my $color = shift; | ||
| 121 | my $direction = shift; | ||
| 122 | $color = $self->{color}{$color}; | ||
| 123 | |||
| 124 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 125 | $self->{im}->draw_text($x,$y,$text,$direction); | ||
| 126 | } # sub DrawText | ||
| 127 | |||
| 128 | |||
| 129 | sub DrawPolygon{ | ||
| 130 | my $self = shift; | ||
| 131 | my $vertices = shift; | ||
| 132 | my $color = shift; | ||
| 133 | my $closed = shift; | ||
| 134 | if (!defined($closed)){ | ||
| 135 | $closed = 1; | ||
| 136 | } | ||
| 137 | $color = $self->{color}{$color}; | ||
| 138 | |||
| 139 | if (scalar(@$vertices) > 2) { | ||
| 140 | my $poly = new Image::Imlib2::Polygon(); | ||
| 141 | for(my $i = 0; $i < scalar(@$vertices); $i++){ | ||
| 142 | my $coords = $vertices->[$i]; | ||
| 143 | $poly->add_point($coords->{"x"},$coords->{"y"}); | ||
| 144 | } | ||
| 145 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 146 | $self->{im}->draw_polygon($poly,$closed); | ||
| 147 | |||
| 148 | undef $poly; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub DrawFilledPolygon{ | ||
| 154 | my $self = shift; | ||
| 155 | my $vertices = shift; | ||
| 156 | my $color = shift; | ||
| 157 | $color = $self->{color}{$color}; | ||
| 158 | my $poly = new Image::Imlib2::Polygon(); | ||
| 159 | |||
| 160 | if (scalar(@$vertices) > 2) { | ||
| 161 | my $poly = new Image::Imlib2::Polygon(); | ||
| 162 | for(my $i = 0; $i < scalar(@$vertices); $i++){ | ||
| 163 | my $coords = $vertices->[$i]; | ||
| 164 | $poly->add_point($coords->{"x"},$coords->{"y"}); | ||
| 165 | } | ||
| 166 | |||
| 167 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 168 | $poly->fill(); | ||
| 169 | undef $poly; | ||
| 170 | } | ||
| 171 | } # sub DrawFilledPolygon | ||
| 172 | |||
| 173 | |||
| 174 | sub DrawRectangle{ | ||
| 175 | my $self = shift; | ||
| 176 | my $x = shift; | ||
| 177 | my $y = shift; | ||
| 178 | my $width = shift; | ||
| 179 | my $height = shift; | ||
| 180 | my $color = shift; | ||
| 181 | $color = $self->{color}{$color}; | ||
| 182 | |||
| 183 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 184 | $self->{im}->draw_rectangle($x,$y,$width,$height); | ||
| 185 | } #sub DrawRectangle | ||
| 186 | |||
| 187 | sub DrawGradientRectangle{ | ||
| 188 | my $self = shift; | ||
| 189 | my $x = shift; | ||
| 190 | my $y = shift; | ||
| 191 | my $width = shift; | ||
| 192 | my $height = shift; | ||
| 193 | my $color = shift; | ||
| 194 | |||
| 195 | my $cr = Image::Imlib2::ColorRange->new(); | ||
| 196 | |||
| 197 | my $r = $self->{color}{$color}{"red"}; | ||
| 198 | my $g = $self->{color}{$color}{"green"}; | ||
| 199 | my $b = $self->{color}{$color}{"blue"}; | ||
| 200 | my $a = $self->{color}{$color}{"alpha"}; | ||
| 201 | |||
| 202 | $x = int($x+.5); | ||
| 203 | $y = int($y+.5); | ||
| 204 | $width = int($width+.5); | ||
| 205 | $height = int($height+.5); | ||
| 206 | |||
| 207 | if ($height == 1) { | ||
| 208 | $height++; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $colorWidth = floor($width / 2); | ||
| 212 | my $cr = Image::Imlib2::ColorRange->new(); | ||
| 213 | |||
| 214 | $cr->add_color(-$colorWidth,$r,$g,$b,$a); | ||
| 215 | $cr->add_color(0,255,255,255,$a); | ||
| 216 | $cr->add_color($colorWidth,$r,$g,$b,$a); | ||
| 217 | |||
| 218 | $self->{im}->fill_color_range_rectangle($cr,$x,$y,$width,$height,90); | ||
| 219 | undef $cr; | ||
| 220 | |||
| 221 | } #sub DrawGradientRectangle | ||
| 222 | |||
| 223 | sub DrawEllipse{ | ||
| 224 | my $self = shift; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub DrawBrush{ | ||
| 228 | my $self = shift; | ||
| 229 | my $x = shift; | ||
| 230 | my $y = shift; | ||
| 231 | my $shape = shift; | ||
| 232 | my $color = shift; | ||
| 233 | |||
| 234 | $color = $self->{color}{$color}; | ||
| 235 | my @vert; | ||
| 236 | my $numvert = 0; | ||
| 237 | |||
| 238 | if ($shape eq "triangle"){ | ||
| 239 | my $poly = Image::Imlib2::Polygon->new(); | ||
| 240 | $poly->add_point($x + 2, $y + 2); | ||
| 241 | $poly->add_point($x, $y - 2); | ||
| 242 | $poly->add_point($x - 2, $y + 2); | ||
| 243 | |||
| 244 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 245 | $poly->fill(); | ||
| 246 | undef $poly; | ||
| 247 | } | ||
| 248 | else{ | ||
| 249 | if ($shape eq "circle"){ | ||
| 250 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 251 | $self->{im}->draw_ellipse($x,$y,2,2); | ||
| 252 | } | ||
| 253 | else{ | ||
| 254 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 255 | $self->{im}->fill_rectangle($x-2,$y-2,5,5); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | }#sub DrawBrush | ||
| 259 | |||
| 260 | sub DrawFilledRectangle{ | ||
| 261 | my $self = shift; | ||
| 262 | my $x = shift; | ||
| 263 | my $y = shift; | ||
| 264 | my $width = shift; | ||
| 265 | my $height = shift; | ||
| 266 | my $color = shift; | ||
| 267 | |||
| 268 | $color = $self->{color}{$color}; | ||
| 269 | $self->{im}->set_colour($color->{"red"}, $color->{"green"}, $color->{"blue"}, $color->{"alpha"}); | ||
| 270 | $self->{im}->fill_rectangle($x,$y,$width,$height); | ||
| 271 | } #sub DrawFilledRectangle | ||
| 272 | |||
| 273 | sub InitPieChart{ | ||
| 274 | my $self = shift; | ||
| 275 | my %pieProps; | ||
| 276 | $pieProps{"xAngle"} = 30; | ||
| 277 | $pieProps{"yAngle"} = -20; | ||
| 278 | $pieProps{"zAngle"} = 20; | ||
| 279 | |||
| 280 | $pieProps{"dMod"} = 2; # How many pixels height per polygon (one pixel for polygon, x for spacing)? | ||
| 281 | $pieProps{"radMod"} = 0; # Default distance from center of the circle. Keep this under 200 | ||
| 282 | $pieProps{"startHeightMod"} = 4; # Do we want the pie stair stepping upward? | ||
| 283 | $pieProps{"edgeColor"} = "black"; | ||
| 284 | |||
| 285 | $pieProps{"maxDepth"} = -10; | ||
| 286 | |||
| 287 | return \%pieProps; | ||
| 288 | }#sub InitPieChart | ||
| 289 | |||
| 290 | |||
| 291 | sub ShowGraph{ | ||
| 292 | my $self = shift; | ||
| 293 | my $filename = shift; | ||
| 294 | $self->showDebug(); | ||
| 295 | $self->{im}->save($filename); | ||
| 296 | undef $self->{im}; | ||
| 297 | } # sub Finalize | ||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | __END__ |