| 1 | package Tk::AppWindow::Ext::Art; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow::Ext::Art - Use icon libraries quick & easy | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION="0.23"; | ||
| 14 | use Config; | ||
| 15 | my $mswin = 0; | ||
| 16 | my $osname = $Config{'osname'}; | ||
| 17 | $mswin = 1 if $osname eq 'MSWin32'; | ||
| 18 | |||
| 19 | use base qw( Tk::AppWindow::BaseClasses::Extension ); | ||
| 20 | |||
| 21 | use File::Basename; | ||
| 22 | use File::MimeInfo::Magic qw(mimetype); | ||
| 23 | use Imager; | ||
| 24 | use MIME::Base64; | ||
| 25 | require FreeDesktop::Icons; | ||
| 26 | require Tk::Compound; | ||
| 27 | require Tk::Photo; | ||
| 28 | use Tk::PNG; | ||
| 29 | |||
| 30 | #setting up support for File::MimeInfo; | ||
| 31 | eval 'use File::MimeInfo::Magic qw(mimetype);' unless $mswin; | ||
| 32 | eval 'use File::MimeInfo::Simple qw(mimetype);' if $mswin; | ||
| 33 | |||
| 34 | #testing if Image::LibRSVG is installed. | ||
| 35 | my $svgsupport = 0; | ||
| 36 | eval 'use Image::LibRSVG'; | ||
| 37 | $svgsupport = 1 unless $@; | ||
| 38 | |||
| 39 | #checking support for text2image. | ||
| 40 | my $t; | ||
| 41 | eval '$t = `fc-list :family=xxxx:style=yyyy`' unless $mswin; | ||
| 42 | my $fc_list_supported = defined $t; | ||
| 43 | $fc_list_supported = 1 if $mswin; | ||
| 44 | |||
| 45 | =head1 SYNOPSIS | ||
| 46 | |||
| 47 | my $app = new Tk::AppWindow(@options, | ||
| 48 | -extensions => ['Art'], | ||
| 49 | ); | ||
| 50 | $app->MainLoop; | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | This extension allows B<Tk::AppWindow> easy access to icon libraries used in desktops | ||
| 55 | like KDE and GNOME. | ||
| 56 | |||
| 57 | if you are not on Windows, it supports libraries containing scalable vector graphics like Breeze. | ||
| 58 | |||
| 59 | On Windows you have to install icon libraries yourself in C:\ProgramData\Icons. | ||
| 60 | You will find plenty of them on Github. Extract an icon set and copy the main | ||
| 61 | folder of the theme (the one that contains the file 'index.theme') to | ||
| 62 | C:\ProgramData\Icons. On Linux you will probably find some icon themes | ||
| 63 | in /usr/share/icons. | ||
| 64 | |||
| 65 | The constructor takes a reference to a list of folders where it finds the icons | ||
| 66 | libraries. If you specify nothing, it will assign default values for: | ||
| 67 | |||
| 68 | Windows: $ENV{ALLUSERSPROFILE} . '\Icons'. Art will not create | ||
| 69 | the folder if it does not exist. | ||
| 70 | |||
| 71 | Others: $ENV{HOME} . '/.local/share/icons', '/usr/share/icons' | ||
| 72 | |||
| 73 | =head1 CONFIG VARIABLES | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item Switch: B<-compoundcolspace> | ||
| 78 | |||
| 79 | Default value 5. Used in the B<createCompound> method to | ||
| 80 | set horizontal spacing. | ||
| 81 | |||
| 82 | =item Switch: B<-iconpath> | ||
| 83 | |||
| 84 | Specify a list of folders where your icon libraries are located. | ||
| 85 | Only available at create time. | ||
| 86 | |||
| 87 | =item Name : B<iconSize> | ||
| 88 | |||
| 89 | =item Class : B<IconSize> | ||
| 90 | |||
| 91 | =item Switch: B<-iconsize> | ||
| 92 | |||
| 93 | Default is 16. | ||
| 94 | |||
| 95 | =item Name : B<iconTheme> | ||
| 96 | |||
| 97 | =item Class : B<IconTheme> | ||
| 98 | |||
| 99 | =item Switch: B<-icontheme> | ||
| 100 | |||
| 101 | Default is Oxygen. | ||
| 102 | |||
| 103 | =item Switch: B<-rawiconpath> I<hookable> | ||
| 104 | |||
| 105 | List of folders where you store your raw icons. Defaults to an empty list. | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =head1 COMMANDS | ||
| 110 | |||
| 111 | The following commands are defined. | ||
| 112 | |||
| 113 | =over 4 | ||
| 114 | |||
| 115 | =item B<available_icon_sizes> | ||
| 116 | |||
| 117 | Returns a list of available icon sizes. | ||
| 118 | |||
| 119 | =item B<available_icon_themes> | ||
| 120 | |||
| 121 | Returns a list of available icon themes. | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 METHODS | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | |||
| 132 | sub new { | ||
| 133 | my $class = shift; | ||
| 134 | my $self = $class->SUPER::new(@_); | ||
| 135 | my $args = $self->GetArgsRef; | ||
| 136 | |||
| 137 | my $ip = delete $args->{'-iconpath'}; | ||
| 138 | $ip = [] unless defined $ip; | ||
| 139 | |||
| 140 | my $fdi = FreeDesktop::Icons->new(@$ip); | ||
| 141 | $self->{CACHE} = {}; | ||
| 142 | $self->{CACHEDISABLED} = 0; | ||
| 143 | $self->{FDI} = $fdi; | ||
| 144 | $self->{THEMEPOOL} = {}; | ||
| 145 | $self->{THEMES} = {}; | ||
| 146 | # $self->{ICONSIZE} = undef; | ||
| 147 | |||
| 148 | $self->cmdConfig( | ||
| 149 | available_icon_sizes => ['AvailableSizesCurrentTheme', $self], | ||
| 150 | available_icon_themes => ['AvailableThemes', $self], | ||
| 151 | ); | ||
| 152 | |||
| 153 | $self->addPreConfig( | ||
| 154 | -compoundcolspace =>['PASSIVE', undef, undef, 3], | ||
| 155 | -iconsize => ['PASSIVE', 'iconSize', 'IconSize', 16], | ||
| 156 | -icontheme => ['PASSIVE', 'iconTheme', 'IconTheme', 'Oxygen'], | ||
| 157 | ); | ||
| 158 | |||
| 159 | $self->configInit( | ||
| 160 | -rawiconpath => ['rawpath', $fdi], | ||
| 161 | ); | ||
| 162 | |||
| 163 | $self->addPostConfig('DoPostConfig', $self); | ||
| 164 | |||
| 165 | return $self; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _cache { | ||
| 169 | my $self = shift; | ||
| 170 | $self->{CACHE} = shift if @_; | ||
| 171 | return $self->{CACHE} | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _fdi { | ||
| 175 | return $_[0]->{FDI} | ||
| 176 | } | ||
| 177 | |||
| 178 | sub AvailableContexts { | ||
| 179 | my $self = shift; | ||
| 180 | return $self->_fdi->availableContexts(@_); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub AvailableIcons { | ||
| 184 | my $self = shift; | ||
| 185 | return $self->_fdi->availableIcons(@_); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub AvailableSizes { | ||
| 189 | my $self = shift; | ||
| 190 | return $self->_fdi->availableSizes(@_); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub AvailableSizesCurrentTheme { | ||
| 194 | my $self = shift; | ||
| 195 | return $self->AvailableSizes($self->configGet('-icontheme')); | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | sub AvailableThemes { | ||
| 200 | my $self = shift; | ||
| 201 | my $fdi = $self->_fdi; | ||
| 202 | return $fdi->availableThemes | ||
| 203 | } | ||
| 204 | |||
| 205 | sub cacheAdd { | ||
| 206 | my ($self, $name, $image) = @_; | ||
| 207 | my $cache = $self->_cache; | ||
| 208 | unless ($self->cacheExists($name)) { | ||
| 209 | $cache->{$name} = $image; | ||
| 210 | } else { | ||
| 211 | warn "Art::cache '$name' already exists" | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | =item B<cacheDisabled>I<(?$flag?)> | ||
| 216 | |||
| 217 | Sets and returns the state of the cache. | ||
| 218 | If the cache is disabled, images will allways be loaded from disk. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub cacheDisabled { | ||
| 223 | my $self = shift; | ||
| 224 | $self->{CACHEDISABLED} = shift if @_; | ||
| 225 | return $self->{CACHEDISABLED} | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | sub cacheExists { | ||
| 230 | my ($self, $name) = @_; | ||
| 231 | my $cache = $self->_cache; | ||
| 232 | return exists $cache->{$name} | ||
| 233 | } | ||
| 234 | |||
| 235 | =item B<cacheClear> | ||
| 236 | |||
| 237 | Clears the cache. | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub cacheClear { | ||
| 242 | my $self = shift; | ||
| 243 | $self->_cache({}) | ||
| 244 | } | ||
| 245 | |||
| 246 | sub cacheGet { | ||
| 247 | my ($self, $name) = @_; | ||
| 248 | my $cache = $self->_cache; | ||
| 249 | return $cache->{$name} if $self->cacheExists($name); | ||
| 250 | warn "Art::cache '$name' does not exist" | ||
| 251 | } | ||
| 252 | |||
| 253 | sub cacheName { | ||
| 254 | my ($self, $file, $width, $height) = @_; | ||
| 255 | $width = '' unless defined $width; | ||
| 256 | $height = $width unless defined $height; | ||
| 257 | return "$file-$width-$height" | ||
| 258 | } | ||
| 259 | |||
| 260 | =item B<canRotateText> | ||
| 261 | |||
| 262 | Returns true if facilities for rotating text are in place. | ||
| 263 | On linux this means the command 'fc-list' works. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub canRotateText { | ||
| 268 | my $self = shift; | ||
| 269 | return 1 if $mswin; | ||
| 270 | return $fc_list_supported; | ||
| 271 | } | ||
| 272 | |||
| 273 | =item B<createCompound>I<(%args)> | ||
| 274 | |||
| 275 | Creates and returns a compound image. | ||
| 276 | %args can have the following keys; | ||
| 277 | |||
| 278 | B<-image> Reference to a Tk::Image object. | ||
| 279 | |||
| 280 | B<-textrotate> An angle in degrees. | ||
| 281 | |||
| 282 | B<-text> Text to be displayed. | ||
| 283 | |||
| 284 | B<textside> Can be 'left', 'right', 'top' or 'bottom'. Default is 'right'. | ||
| 285 | |||
| 286 | =cut | ||
| 287 | |||
| 288 | sub createCompound { | ||
| 289 | my $self = shift; | ||
| 290 | my %args = (@_); | ||
| 291 | |||
| 292 | my $side = delete $args{'-textside'}; | ||
| 293 | my $text = delete $args{'-text'}; | ||
| 294 | my $image = delete $args{'-image'}; | ||
| 295 | my $rotate = delete $args{'-textrotate'}; | ||
| 296 | my $textimage; | ||
| 297 | $textimage = $self->text2image($text, $rotate) if defined $rotate; | ||
| 298 | $side = 'right' unless defined $side; | ||
| 299 | my $compound = $self->Compound; | ||
| 300 | if ($side eq 'left') { | ||
| 301 | if (defined $textimage) { | ||
| 302 | $compound->Image(-image => $textimage); | ||
| 303 | } else { | ||
| 304 | $compound->Text(-text => $text, -anchor => 'c') if defined $text; | ||
| 305 | } | ||
| 306 | if (defined $image) { | ||
| 307 | $compound->Space(-width => $self->configGet('-compoundcolspace')); | ||
| 308 | $compound->Image(-image => $image); | ||
| 309 | } | ||
| 310 | } elsif ($side eq 'right') { | ||
| 311 | $compound->Image(-image => $image) if defined $image; | ||
| 312 | if (defined $textimage) { | ||
| 313 | $compound->Space(-width => $self->configGet('-compoundcolspace')); | ||
| 314 | $compound->Image(-image => $textimage); | ||
| 315 | } elsif (defined $text) { | ||
| 316 | $compound->Space(-width => $self->configGet('-compoundcolspace')); | ||
| 317 | $compound->Text(-text => $text, -anchor => 'c'); | ||
| 318 | } | ||
| 319 | } elsif ($side eq 'top') { | ||
| 320 | if (defined $textimage) { | ||
| 321 | $compound->Image(-image => $textimage); | ||
| 322 | } else { | ||
| 323 | $compound->Text(-text => $text, -anchor => 'c') if defined $text; | ||
| 324 | } | ||
| 325 | if (defined $image) { | ||
| 326 | $compound->Line(-pady => $self->configGet('-compoundcolspace')); | ||
| 327 | $compound->Image(-image => $image); | ||
| 328 | } | ||
| 329 | } elsif ($side eq 'bottom') { | ||
| 330 | $compound->Image(-image => $image) if defined $image; | ||
| 331 | if (defined $textimage) { | ||
| 332 | $compound->Line(-pady => $self->configGet('-compoundcolspace')); | ||
| 333 | $compound->Image(-image => $textimage); | ||
| 334 | } elsif (defined $text) { | ||
| 335 | $compound->Line(-pady => $self->configGet('-compoundcolspace')); | ||
| 336 | $compound->Text(-text => $text, -anchor => 'c'); | ||
| 337 | } | ||
| 338 | } elsif ($side eq 'none') { | ||
| 339 | $compound->Image(-image => $image) if defined $image; | ||
| 340 | } else { | ||
| 341 | warn "illegal value $side for -textside. Should be 'left', 'right' 'top', bottom' or 'none'" | ||
| 342 | } | ||
| 343 | return $compound; | ||
| 344 | } | ||
| 345 | |||
| 346 | =item B<createEmptyImage>I<(?$width?, ?$height?)> | ||
| 347 | |||
| 348 | Creates and returns aan empty image. Nothing to see, just taking up space. | ||
| 349 | If $width is not specified it defaults to the B<-iconsize> config variable. | ||
| 350 | If $height is not specified it defaults to $width. | ||
| 351 | |||
| 352 | =cut | ||
| 353 | |||
| 354 | sub createEmptyImage { | ||
| 355 | my ($self, $width, $height) = @_; | ||
| 356 | $width = $self->configGet('-iconsize') unless defined $width; | ||
| 357 | $height = $width unless defined $height; | ||
| 358 | my $empty = '/* XPM */ | ||
| 359 | static char * new_xpm[] = { | ||
| 360 | "' . "$width $height" . ' 3 1", | ||
| 361 | " c None", | ||
| 362 | ". c #000000", | ||
| 363 | "+ c #FFFFFF", | ||
| 364 | '; | ||
| 365 | |||
| 366 | my $line = ''; | ||
| 367 | for (1 ..$width) { $line = "$line " } | ||
| 368 | $line = "\"$line\"\n"; | ||
| 369 | for (1 .. $height) { | ||
| 370 | $empty = $empty . $line | ||
| 371 | } | ||
| 372 | return $self->Pixmap(-data => $empty); | ||
| 373 | } | ||
| 374 | |||
| 375 | sub DoPostConfig { | ||
| 376 | my $self = shift; | ||
| 377 | |||
| 378 | #Fixing name problem. Gtk init files specify the used icon library | ||
| 379 | #as their folder name instead of the name in their index. | ||
| 380 | my $theme = $self->configGet('-icontheme'); | ||
| 381 | my $fdi = $self->_fdi; | ||
| 382 | unless ($fdi->themeExists($theme)) { | ||
| 383 | for ($fdi->availableThemes) { | ||
| 384 | my $test = $_; | ||
| 385 | my $path = $fdi->getPath($test); | ||
| 386 | if ($path =~ /$theme$/) { | ||
| 387 | $self->configPut(-icontheme => $test); | ||
| 388 | last; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | #Fixing cases of specified iconsize not matching any of the | ||
| 393 | #available iconsizes. | ||
| 394 | my $size = $self->configGet('-iconsize'); | ||
| 395 | $size = $self->getAlternateSize($size); | ||
| 396 | $self->configPut(-iconsize => $size); | ||
| 397 | # $self->pause(150); | ||
| 398 | } | ||
| 399 | |||
| 400 | sub fontFile { | ||
| 401 | my ($self, $font) = @_; | ||
| 402 | my $family = $self->fontActual($font, '-family'); | ||
| 403 | $family =~ s/\s//g; #remove spaces | ||
| 404 | my $slant = $self->fontActual($font, '-slant'); | ||
| 405 | my $weight = $self->fontActual($font, '-weight'); | ||
| 406 | $slant = '' if $slant eq 'roman'; | ||
| 407 | $weight = '' if $weight eq 'normal'; | ||
| 408 | my $style = $weight . $slant; | ||
| 409 | if ($style eq '') { | ||
| 410 | for ('regular', 'normal', 'roman') { | ||
| 411 | my $f = $self->fontFind($family, $_); | ||
| 412 | if (defined $f) { | ||
| 413 | last | ||
| 414 | return $f | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } else { | ||
| 418 | return $self->fontFind($family, $style); | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | sub fontFind { | ||
| 423 | my ($self, $family, $style) = @_; | ||
| 424 | my $res = `fc-list :family=$family:style=$style`; | ||
| 425 | if ($res =~ s/([^\:]*)\:.*\n//) { | ||
| 426 | return $1 | ||
| 427 | } | ||
| 428 | return undef | ||
| 429 | } | ||
| 430 | |||
| 431 | =item B<getAlternateSize>I<($size>)> | ||
| 432 | |||
| 433 | Tests if $size is available in the current itecontheme. Returns | ||
| 434 | the first size that is larger than $size if it is not. | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | sub getAlternateSize { | ||
| 439 | my ($self,$size) = @_; | ||
| 440 | my $theme = $self->configGet('-icontheme'); | ||
| 441 | my $fdi = $self->_fdi; | ||
| 442 | my @sizes = $fdi->availableSizes($theme); | ||
| 443 | my ($index) = grep { $sizes[$_] eq $size } 0..$#sizes; | ||
| 444 | unless (defined $index) { | ||
| 445 | for (@sizes) { | ||
| 446 | if ($size < $_) { | ||
| 447 | $size = $_; | ||
| 448 | last; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | return $size | ||
| 453 | } | ||
| 454 | |||
| 455 | |||
| 456 | =item B<getFileIcon>I<($file>, [ I<$size, $context> ] I<);> | ||
| 457 | |||
| 458 | Determines the mime type of $file and returns the belonging | ||
| 459 | icon. If it can not determine the mime type, the default file | ||
| 460 | icon is returned. | ||
| 461 | |||
| 462 | =cut | ||
| 463 | |||
| 464 | my %genicons = ( | ||
| 465 | audio => 'audio-x-generic', | ||
| 466 | image => 'image-x-generic', | ||
| 467 | package => 'package-x-generic', | ||
| 468 | video => 'video-x-generic', | ||
| 469 | ); | ||
| 470 | |||
| 471 | =item B<getFileIcon>I<($file>);> | ||
| 472 | |||
| 473 | Returns the mime type of I<$file>. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub getFileIcon { | ||
| 478 | my $self = shift; | ||
| 479 | my $file = shift; | ||
| 480 | if (-e $file) { | ||
| 481 | my $mime = $self->getFileMimeInfo($file); | ||
| 482 | if (defined $mime) { | ||
| 483 | if ($mime =~ /^([^\/]+)\//) { | ||
| 484 | my $ico = $genicons{$1}; | ||
| 485 | if (defined $ico) { | ||
| 486 | my $icon = $self->getIcon($ico, @_); | ||
| 487 | return $icon if defined $icon | ||
| 488 | } | ||
| 489 | } | ||
| 490 | $mime =~ s/\//-/; | ||
| 491 | my $icon = $self->getIcon($mime, @_); | ||
| 492 | return $icon if defined $icon; | ||
| 493 | } | ||
| 494 | return $self->getIcon('text-x-generic', @_) if -T $file; | ||
| 495 | } | ||
| 496 | return $self->getIcon('text-plain', @_); | ||
| 497 | } | ||
| 498 | |||
| 499 | sub getFileMimeInfo { | ||
| 500 | my ($self, $file) = @_; | ||
| 501 | return mimetype($file); | ||
| 502 | } | ||
| 503 | |||
| 504 | =item B<getIcon>I<($name>, [ I<$size, $context> ] I<);> | ||
| 505 | |||
| 506 | Returns a Tk::Photo object. If you do not specify I<$size> or the icon does | ||
| 507 | not exist in the specified size, it will find the largest possible icon and | ||
| 508 | scale it to the requested size. I<$force> can be 0 or 1. It is 0 by default. | ||
| 509 | If you set it to 1 a missing icon image is returned instead of undef when the | ||
| 510 | icon cannot be found. | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | sub getIcon { | ||
| 515 | my ($self, $name, $size, $context) = @_; | ||
| 516 | $size = $self->configGet('-iconsize') unless defined $size; | ||
| 517 | my $fdi = $self->_fdi; | ||
| 518 | $fdi->theme($self->configGet('-icontheme')); | ||
| 519 | my $resize = 0; | ||
| 520 | my $file = $fdi->get($name, $size, $context, \$resize); | ||
| 521 | if (defined $file) { | ||
| 522 | return $self->loadImage($file, $size); | ||
| 523 | } | ||
| 524 | return undef | ||
| 525 | } | ||
| 526 | |||
| 527 | =item B<imagerFont>I<($tkfontobject);> | ||
| 528 | |||
| 529 | Takes a Tk::Font object and returns it as a Imager::Font object. | ||
| 530 | |||
| 531 | =cut | ||
| 532 | |||
| 533 | sub imagerFont { | ||
| 534 | my ($self, $fnt) = @_; | ||
| 535 | my $font; | ||
| 536 | my $fontsize = abs($self->fontActual($fnt, '-size')); | ||
| 537 | if ($mswin) { | ||
| 538 | my $fontdesc = $self->fontActual($fnt, '-family'); | ||
| 539 | my $fontweight = $self->fontActual($fnt, '-weight'); | ||
| 540 | $fontdesc = "$fontdesc bold" if $fontweight eq 'bold'; | ||
| 541 | my $fontslant = $self->fontActual($fnt, '-slant'); | ||
| 542 | $fontdesc = "$fontdesc italic" if $fontslant eq 'italic'; | ||
| 543 | $font = Imager::Font->new( | ||
| 544 | face => $fontdesc, | ||
| 545 | size => $fontsize, | ||
| 546 | ) or die Imager->errstr; | ||
| 547 | } else { | ||
| 548 | my $fontfile = $self->fontFile($fnt); | ||
| 549 | $font = Imager::Font->new( | ||
| 550 | file => $fontfile, | ||
| 551 | size => $fontsize, | ||
| 552 | ) or die Imager->errstr; | ||
| 553 | } | ||
| 554 | return $font | ||
| 555 | } | ||
| 556 | |||
| 557 | sub isSVG { | ||
| 558 | my ($self, $file) = @_; | ||
| 559 | return $file =~ /\.svg$/i | ||
| 560 | } | ||
| 561 | |||
| 562 | =item B<loadImage>I<($file, ?$width?, ?$height?)> | ||
| 563 | |||
| 564 | Loads image I<$file> and returns it as a Tk::Photo object. It will | ||
| 565 | resize the image to I<$width> and I<$height> if they are specified. | ||
| 566 | if only I<$width> is specified height is set equal to width. | ||
| 567 | |||
| 568 | =cut | ||
| 569 | |||
| 570 | sub loadImage { | ||
| 571 | my ($self, $file, $width, $height) = @_; | ||
| 572 | if (defined $width) { | ||
| 573 | $height = $width unless defined $height | ||
| 574 | } | ||
| 575 | |||
| 576 | my $load = 0; | ||
| 577 | |||
| 578 | #create chache name | ||
| 579 | my $cachename = $self->cacheName($file, $width, $height); | ||
| 580 | |||
| 581 | if ($self->cacheExists($cachename)) { | ||
| 582 | return $self->cacheGet($cachename) unless $self->cacheDisabled; | ||
| 583 | } | ||
| 584 | if (-e $file) { | ||
| 585 | my $data; | ||
| 586 | unless ($self->isSVG($file)) { | ||
| 587 | my $img = Imager->new(file=>$file); | ||
| 588 | if (defined $img) { | ||
| 589 | $width = $img->getwidth unless defined $width; | ||
| 590 | $height = $img->getheight unless defined $height; | ||
| 591 | if (($width ne $img->getwidth) or ($height ne $img->getheight)) { | ||
| 592 | $img = $img->scale(xpixels => $width, ypixels => $height) | ||
| 593 | } | ||
| 594 | $img->write(data => \$data, type => 'png'); | ||
| 595 | } | ||
| 596 | } else { | ||
| 597 | unless ($svgsupport) { | ||
| 598 | warn "Svg images not supported on this system"; | ||
| 599 | return undef; | ||
| 600 | } | ||
| 601 | my $renderer = Image::LibRSVG->new; | ||
| 602 | $renderer->loadFromFileAtSize($file, $width, $height); | ||
| 603 | $data = $renderer->getImageBitmap("png", 100); | ||
| 604 | } | ||
| 605 | if (defined $data) { | ||
| 606 | my $i = $self->GetAppWindow->Photo( | ||
| 607 | -data => encode_base64($data), | ||
| 608 | -format => 'png', | ||
| 609 | ); | ||
| 610 | $self->cacheAdd($cachename, $i) unless $self->cacheDisabled; | ||
| 611 | return $i | ||
| 612 | } | ||
| 613 | } else { | ||
| 614 | warn "image file $file not found \n"; | ||
| 615 | } | ||
| 616 | return undef | ||
| 617 | } | ||
| 618 | |||
| 619 | |||
| 620 | =item B<text2image>I<($text, ?$rotate?)> | ||
| 621 | |||
| 622 | Returns a Tk::Photo class with $text as image. $rotate specifies | ||
| 623 | how much the text must be rotated in degrees. On linux this only works | ||
| 624 | if the command 'fc-list' is available. | ||
| 625 | |||
| 626 | =cut | ||
| 627 | |||
| 628 | sub text2image { | ||
| 629 | my ($self, $text, $rotate) = @_; | ||
| 630 | return undef unless $fc_list_supported; | ||
| 631 | my $l = $self->Label; | ||
| 632 | my $fnt = $l->cget('-font'); | ||
| 633 | my $color = $l->cget('-foreground'); | ||
| 634 | $color = '#000000' if $color eq 'SystemButtonText'; | ||
| 635 | my $fontsize = abs($self->fontActual($fnt, '-size')); | ||
| 636 | my $width = $self->fontMeasure($fnt, $text) + 2; | ||
| 637 | my $height = $self->fontMetrics($fnt, '-linespace') + 2; | ||
| 638 | my $desc = $self->fontMetrics($fnt, '-descent'); | ||
| 639 | my $ypos = $height - $desc - 1; | ||
| 640 | $l->destroy; | ||
| 641 | |||
| 642 | my $img = Imager->new(xsize => $width, ysize => $height, channels => 4); | ||
| 643 | |||
| 644 | $color = Imager::Color->new($color) or die "Color"; | ||
| 645 | |||
| 646 | my $font = $self->imagerFont($fnt); | ||
| 647 | |||
| 648 | $img->string( | ||
| 649 | x => 1, | ||
| 650 | 'y' => $ypos, | ||
| 651 | font => $font, | ||
| 652 | string => $text, | ||
| 653 | color => $color, | ||
| 654 | aa => 1, | ||
| 655 | ) or die "String"; | ||
| 656 | |||
| 657 | if (defined $rotate) { | ||
| 658 | my $new = $img->rotate(degrees => $rotate); | ||
| 659 | $img = $new; | ||
| 660 | } | ||
| 661 | |||
| 662 | my $data; | ||
| 663 | $img->write(data => \$data, type => 'png'); | ||
| 664 | return $self->Photo( | ||
| 665 | -format => 'png', | ||
| 666 | -data => encode_base64($data) | ||
| 667 | ) | ||
| 668 | } | ||
| 669 | |||
| 670 | =back | ||
| 671 | |||
| 672 | =head1 LICENSE | ||
| 673 | |||
| 674 | Same as Perl. | ||
| 675 | |||
| 676 | =head1 AUTHOR | ||
| 677 | |||
| 678 | Hans Jeuken (hanje at cpan dot org) | ||
| 679 | |||
| 680 | =head1 BUGS | ||
| 681 | |||
| 682 | Unknown. If you find any, please contact the author. | ||
| 683 | |||
| 684 | =head1 SEE ALSO | ||
| 685 | |||
| 686 | =over 4 | ||
| 687 | |||
| 688 | =item L<Tk::AppWindow> | ||
| 689 | |||
| 690 | =item L<Tk::AppWindow::BaseClasses::Extension> | ||
| 691 | |||
| 692 | =back | ||
| 693 | |||
| 694 | =cut | ||
| 695 | |||
| 696 | 1; | ||
| 697 | |||
| 698 | |||
| 699 | |||
| 700 | |||
| 701 | |||
| 702 |