Re:Text with transparent background



Hi, 
Muppet, thank you for pointing me in the right direction. 
I know now a little more on the GDK gray area.

I wanted a ?or something
like OSD text, which shows only the text and no background
at all??


Included is what I ended up with. Its just a pity that the 
"$pixbuf = $pixbuf->add_alpha (TRUE, 0, 0 , 0);"

does not take out all of the background.

Hope this may help someone who want to do similar tricks

Comments welcome

--------
#!/usr/bin/perl -w

use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

#add a timer to run each second
Glib::Timeout->add (1000,\&new_screen);


#create a popup window -> this allow us th modify its size
to 
#that of a Gtk::Gdk::Pixmap class.
my $window= Gtk2::Window->new('popup');
        
        #standard packing widget
        my $vbox = Gtk2::VBox->new(FALSE,0);

                #create a image containing nothing, this will be set each
time 
                #the &new_screen sub runs
                my $img = Gtk2::Image->new_from_pixmap (undef, undef);
        
        $vbox->pack_start($img,FALSE,FALSE,0);
        
$window->add($vbox);

$window->set_position('center-always');
#show all BEFORE we add $drawing_area - we do not want to
show the drawing
#area, we only use it as a working surface.
$window->show_all();

        #Create a drawing area, and set is big enough to hold the
maximum size our 
        #text will ever be.
        my $drawing_area = Gtk2::DrawingArea->new;
        $drawing_area->set_size_request (1600, 1600);
        $vbox->pack_start($drawing_area,FALSE,FALSE,0);
        
        #realize it, since it will not be displayed, but we need
to 
        #get hold of the Gdk classes that needs it to be shown, or
realized
        $drawing_area->realize;
                #create a new pango layout for this drawing area
                my $pango_layout =
$drawing_area->create_pango_layout("");
                #get the defalt colormap (will be needed later on)
                my $colormap = Gtk2::Gdk::Colormap->get_system;
        #initial paint of screen
        &new_screen;
Gtk2->main;
        
sub new_screen {

        #this sub will get a new value of text, and display it on
the screen in a shaped 
        #window, to get there needs a few steps

        #get the text that we want to display on the screen.
        my $ts = ret_time();
        
        #Set our pango layout, to reflect the text and format that
we want.
        #NOTE We delebritely set the background to a certain color
to convert 
        #that color to a alpha channel, that will give us the
"clear" background. 
        $pango_layout->set_markup("<span background = '#000000'
foreground = '#FF0000' size = '20000' weight ='heavy'>Exact
time: </span><span background = '#000000' foreground=
'#00FF00' size='30000' weight =
'ultralight'><i><u>$ts</u></i></span>");
        
        #Get the size of this layout after the text was set.
        my($pango_w,$pango_h)=$pango_layout->get_pixel_size;
        
        #Now we have the size, we can create a pixmap that will be
the
        #'Gtk2::Gdk::Drawable' that we will draw upon
        my $pixmap = Gtk2::Gdk::Pixmap->new
($drawing_area->window,
                                    $pango_w,
                                    $pango_h,
                                    -1);
        #draw the pango layout on the drawable.
        $pixmap->draw_layout($drawing_area->style->black_gc,0,0,$pango_layout);
        
        #create a Gtk2::Gdk::Pixbuf that we will use to grab the
pango text from the
        #drawable (Gtk2::Gdk::Pixmap which is a
Gtk2::Gdk::Drawable) 
        my $pixbuf = Gtk2::Gdk::Pixbuf->new ('rgb', TRUE, 8,
$pango_w, $pango_h);
        
        #here we get create a pixbuff from the drawable, this is
where we need the colormap
        $pixbuf->get_from_drawable ($pixmap, $colormap, 0, 0, 0,0,
$pango_w, $pango_h);
                
                #Remove the background (we use a the color we specified
as the pango text's
                #background
                $pixbuf = $pixbuf->add_alpha (TRUE, 0, 0 , 0);
                
                #create a pixmap and mask from the Gtk2::Gdk::Pixbuf
                my ($pm, $m) = $pixbuf->render_pixmap_and_mask (255);
        #shrink our window to make sure the mask and pixmap will
be on the same spot.
        $window->resize(4,4);
        #replace the old $img with the new pixmap and mask
        $img->set_from_pixmap ($pm, $m);
        
        #shape our window accordingly
        $window->shape_combine_mask ($m, 0, 0);
        
        return 1;
        
}


sub ret_time {
        #sub to return a string containing the time
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=
localtime(time+3600);
        my @lst =("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
        my $thisday =$lst[$wday];
        $year = $year + 1900;
  
        ($sec =~ m/\d{2}/)||($sec = "0".$sec);
        ($min =~ m/\d{2}/)||($min = "0".$min);
        ($hour =~ m/\d{2}/)||($hour = "0".$hour);
        $mon = $mon +1;
        #print("\n");
        my $ts = "$thisday $mday/$mon/$year $hour:$min:$sec";
        
        return $ts;
}

----------------------



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]