/[Frey]/trunk/lib/Frey/IconBrowser.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/IconBrowser.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 553 by dpavlin, Thu Nov 27 15:45:00 2008 UTC revision 608 by dpavlin, Fri Nov 28 23:28:12 2008 UTC
# Line 3  use Moose; Line 3  use Moose;
3    
4  extends 'Frey';  extends 'Frey';
5  with 'Frey::Web';  with 'Frey::Web';
6  with 'Frey::Path';  with 'Frey::Web::CombineImages';
7  with 'Frey::Storage';  
8    use Data::Dump qw/dump/;
9    
10  has path => (  has path => (
11          is => 'rw',          is => 'rw',
# Line 13  has path => ( Line 14  has path => (
14          default => 'static/icons/fugue/',          default => 'static/icons/fugue/',
15  );  );
16    
17  use GD;  has show_names => (
18  use Digest::MD5 qw/md5_hex/;          is => 'rw',
19  use File::Slurp;          isa => 'Bool',
20  use Data::Dump qw/dump/;          required => 1,
21            default => 1,
22  =head2 combine_image  );
   
 Combine images into CSS sprites  
   
   my ( $combined_path, $styles ) = $self->combine_image( @paths );  
   
 =cut  
   
 sub combine_image {  
         my $self = shift;  
         my @images = @_;  
   
         my $path = 'var/combine/' . md5_hex( join(' ',@images ) );  
         my $style_path = $path . '.yaml';  
         $path .= '.png';  
   
         return ( $path, $self->load( $style_path ) ) if -e $path;  
   
         warn "# path $path";  
         $self->mkbasepath( $path );  
   
         # fixed dimensions of images combined  
         my ( $w, $h ) = ( 16, 16 );  
         my $num_images = $#images + 1;  
   
         my $combined = GD::Image->new( $w, $num_images * $h )  
                 || die "can't create $w x ",$num_images * $h," combined image";  
         $combined->trueColor( 1 );  
         $combined->saveAlpha( 1 );  
         my $white = $combined->colorAllocate(255,255,255);  
         $combined->transparent($white);  
   
         my $y = 0;  
   
         my $style;  
   
         foreach my $image_path ( @images ) {  
                 my $i = GD::Image->new( $image_path ) || die "can't open $image_path: $!";  
                 $i->trueColor( 1 );  
                 $i->saveAlpha( 1 );  
 #               $i->transparent($white);  
   
                 die "with not $w" unless $i->width == $w;  
                 die "with not $h" unless $i->height == $h;  
   
                 warn "# copy $image_path 0 x $y";  
                 $combined->copy( $i, 0, $y, 0, 0, $w, $h );  
   
                 $style->{$image_path}  
                         = qq|background:url(/$path) no-repeat; padding:0 0 0 | . ( $h - 1 ) . qq|px; background-position:0 |  
                         . ( $y ? -$y . 'px' : 0 )  
                         ;  
   
                 $y += $h;  
         }  
   
         write_file( $path, $combined->png );  
         $self->store( $style_path, $style );  
         warn  
                 "# combined $num_images into ",  
                 $combined->width, "x", $combined->height, " pixels ",  
                 $path, " ", -s $path , " bytes ",  
                 " style $style_path ", -s $style_path, " bytes";  
   
         return ( $path, $style );  
 }  
23    
24  sub as_markup {  sub as_markup {
25          my ($self) = @_;          my ($self) = @_;
26    
27            $self->title( 'icons - ' . $self->path );
28    
29          my $extension = '\.(?:png)$';          my $extension = '\.(?:png)$';
30    
31          opendir(my $dir, $self->path) || die "can't opendir ", $self->path, ": $!";          opendir(my $dir, $self->path) || die "can't opendir ", $self->path, ": $!";
32          my @icons = sort grep { m/$extension/ } readdir($dir);          my @icons = sort grep { m/$extension/ } readdir($dir);
33          closedir $dir;          closedir $dir;
34    
35          my ( $combined_path, $styles ) = $self->combine_image( map { $self->path . '/' . $_ } @icons );          my ( $combined_path, $styles ) = $self->combine_images( map { $self->path . '/' . $_ } @icons );
36    
37          my $html;          my $width = 0;
38            foreach ( @icons ) {
39                    my $l = length($_);
40                    $width = $l if $l > $width;
41            }
42    
43            $width = 2 if ! $self->show_names;
44    
45            $self->add_css(qq|
46                    h1 { font-size: 20px }
47                    /* group */
48                    div.g {
49                            float: left;
50                            clear: both;
51                    }
52                    /* individual icon */
53                    div.i {
54                            color: #888;
55                            width: ${width}ex;
56                            float: left;
57                    }
58            |);
59    
60            my $html = '<div class="g">';
61          my $base;          my $base;
62    
63          foreach my $icon ( @icons ) {          foreach my $icon ( @icons ) {
64                  my $name = $icon;                  my $name = $icon;
65                  $name =~ s{$extension}{};                  $name =~ s{$extension}{};
66    
67                  $base ||= $name; # seed with first                  if ( ! $base ) {
68                  my $desc;                          $base = $name;
69                            $html .= qq|<h1>$name</h1>|;
70                    }
71    
72                    my $desc = '';
73    
74                  my $bl = length $base;                  my $bl = length $base;
75    
76                  if ( substr($name, 0, $bl) eq $base ) {                  if ( $name =~ m{_} && substr($name, 0, $bl) eq $base ) {
77                          ($name,$desc) = (                          ($name,$desc) = (
78                                  substr($name,0,$bl),                                  substr($name,0,$bl),
79                                  substr($name,$bl)                                  substr($name,$bl)
80                          );                          );
81                  } else {                  } else {
82                          $base = $name;                          $base = $name;
83                          warn "# new base $base";                          warn "# new base $base" if $self->debug;
84                          $html .= qq|<hr>\n|;                          $html .= qq|
85                                    </div>
86                                    <div class="g">
87                                    <h1>$name</h1>
88                            |;
89                  }                  }
90    
91  #               $html .= qq|<img src="/| . $self->path . qq|/$icon" alt="$icon"> <b>$name</b>$desc<br/>\n|;  #               $html .= qq|<img src="/| . $self->path . qq|/$icon" alt="$icon"> <b>$name</b>$desc<br/>\n|;
# Line 124  sub as_markup { Line 93  sub as_markup {
93    
94                  my $path = $self->path . '/' . $icon;                  my $path = $self->path . '/' . $icon;
95    
96                  my $style = $styles->{$path} || die "can't find style for $path in ",dump( $styles );                  my $pic = $styles->{$path} || die "can't find pic for $path in ",dump( $styles );
97                  $html .= qq|<span style="$style" title="$icon"> <b>$name</b>$desc</span>\n|;                  $html .= ''
98                            . qq|<div title="$icon" class="i">$pic|
99                            . ( $self->show_names ? qq|<b>$name</b>$desc| : '' )
100                            . qq|</div>\n|
101                            ;
102          }          }
103    
104    
105            $html .= '</div>';
106  #       $html .= qq|<img src="$combined_path">|;  #       $html .= qq|<img src="$combined_path">|;
107          return $html;          return $html;
108  }  }

Legend:
Removed from v.553  
changed lines
  Added in v.608

  ViewVC Help
Powered by ViewVC 1.1.26