Render PNG with Pango
- From: Steven Mullins <sbmullins ntelos net>
- To: gtk-perl-list gnome org
- Subject: Render PNG with Pango
- Date: Sat, 10 May 2008 17:10:54 -0400
I need to render some utf-8 text with pango to a PNG. I can do this, but not
without flashing the image on the screen for an instant to draw the pixmap.
does anyone know how to do this without displaying the image to the screen?
A short example would be great. I'm including my working code. It's a mess,
sorry.
Thanks,
Steve
--begin code---
#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;
my $xsize = 320; # maxsize = 32767
my $ysize = 320;
my $xsizenew;
my $ysizenew;
my $pixmap = undef;
my $gc = undef;
my $colormap = undef;
my %allocated_colors;
my ($x0,$y0,$x1,$y1,$width,) = (0,0,0,0);
# Create the window
my $window = new Gtk2::Window ( "toplevel" );
$window->signal_connect ("delete_event", sub { Gtk2->main_quit; });
$window->set_border_width (10);
$window->set_size_request(640,480);
$window->set_position('center');
my $vbox = Gtk2::VBox->new( 0, 0 );
$window->add($vbox);
$vbox->set_border_width(2);
my $hbox = Gtk2::HBox->new( 0, 0 );
$vbox->pack_start($hbox,1,1,0);
$hbox->set_size_request(320,240);
$hbox->set_border_width(2);
my $hbox1 = Gtk2::HBox->new( 0, 0 );
$vbox->pack_start($hbox1,0,0,0);
$hbox1->set_border_width(2);
my $scwin = Gtk2::ScrolledWindow->new();
my $ha1 = $scwin->get_hadjustment;
$scwin->set_policy('always','never');
my $vp = Gtk2::Viewport->new (undef,undef);
$scwin->add($vp);
$hbox->pack_start($scwin,1,1,0);
# Create the drawing area.
my $area = new Gtk2::DrawingArea; #don't confuse with Gtk2::Drawable
$area->size ($xsize, $ysize);
$vp->add($area);
$area->set_events ([qw/exposure-mask
leave-notify-mask
button-press-mask
pointer-motion-mask
pointer-motion-hint-mask/]);
$area->signal_connect (button_press_event => \&button_press_event);
# Signals used to handle backing pixmap
$area->signal_connect( expose_event => \&expose_event );
$area->signal_connect( configure_event => \&configure_event );
$window->show_all;
draw();
save_all();
exit;
###########################################
sub draw{
# get current window size and freeze it, so x y scaling is constant
# in the pixmap
my (undef, undef, $width0, $height0, undef) = $window->window->get_geometry;
$window->set_size_request($width0,$height0);
$window->set_resizable(0);
(undef, undef, $xsizenew, $ysizenew, undef) = $area->window->get_geometry;
print "$xsizenew $ysizenew\n";
start_drawing($pixmap);
}
########################################
# Draw a line in the expose callback
sub start_drawing {
my $pixmap = shift;
&draw_ptext($pixmap);
#without this line the screen won't be updated until a screen action
$area->queue_draw;
}
######################################
sub draw_ptext{
my($widget) = @_;
# see Gdk::Gdk::Window, Gtk2::Gdk::Drawable, Gtk2::Gdk::GC
my $drawable = $widget;
my $gc = new Gtk2::Gdk::GC ($drawable);
my $pango_layout = $area->create_pango_layout("");
my $font_desc = Gtk2::Pango::FontDescription->from_string("Ezra SIL 26");
$pango_layout->set_font_description($font_desc);
$pango_layout->set_markup ("Tango Pango
\nיִשְׂרָאֵל
אֱלֹהִים");
$drawable->draw_layout($gc,0,0, $pango_layout);
$area->queue_draw;
}
######################################
sub save_all{
my ($width, $height) = $pixmap->get_size();
print "$width $height\n";
# create blank pixbuf to hold the whole pixmap
my $lpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb',
0,
8,
$width,
$height);
$lpixbuf->get_from_drawable ($pixmap,
undef, 0, 0, 0, 0, $width, $height);
#only jpeg and png is supported !!!! it's 'jpeg', not 'jpg'
$lpixbuf->save ("test.png", 'png');
return FALSE;
}
##########################################
# Create a new backing pixmap of the appropriate size
sub configure_event {
my $widget = shift; # GtkWidget *widget
my $event = shift; # GdkEventConfigure *event
$pixmap = Gtk2::Gdk::Pixmap->new(
$widget->window,
$widget->allocation->width,
$widget->allocation->height, -1
);
$pixmap->draw_rectangle(
$widget->style->white_gc, # or black_gc
TRUE,
0, 0,
$widget->allocation->width,
$widget->allocation->height
);
$gc = Gtk2::Gdk::GC->new( $pixmap );
$colormap = $pixmap->get_colormap;
# set a default foreground
$gc->set_foreground( get_color( $colormap, 'red' ) );
return TRUE;
}
##########################################
# Redraw the screen from the backing pixmap
sub expose_event {
my $widget = shift; # GtkWidget *widget
my $event = shift; # GdkEventExpose *event
$widget->window->draw_drawable(
$widget->style->fg_gc( $widget->state ), $pixmap,
$event->area->x, $event->area->y,
$event->area->x, $event->area->y,
$event->area->width, $event->area->height
);
return FALSE;
}
##########################################
---end code---
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]