Re: Gnomecanvas/TextItem question



On Sunday, January 18, 2004, at 09:23 PM, Chris Debenham - SSA Systems Engineer wrote:

Previously I did it by creating a Gtk::Gdk::Font with the font set
accordingly then used string_width to find wrapping and fudged a value
for string_height.
This doesn't seem to work anymore.

indeed it does not work anymore -- GdkFont was deprecated in favor of Pango, and thus no GdkFont-related stuff is bound into Gtk2.


Basically, how do I find the height of a arbitrary string for a given
font and given maximum width?

i'm not sure exactly of what to do, but i would start with $pango_layout->get_pixel_size. http://developer.gnome.org/doc/API/2.0/pango/pango-Layout- Objects.html#pango-layout-get-pixel-size

here's the result of messing about for a few minutes with this.


#!/usr/bin/perl -w

use Gtk2 -init;
use Gtk2::Pango;
use Gnome2::Canvas;

$canvas = Gnome2::Canvas->new;
$window = Gtk2::Window->new;
$window->add ($canvas);
$window->show_all;
$window->signal_connect (delete_event => sub {Gtk2->main_quit});
$window->set_size_request (300,200);

$rich_text = Gnome2::Canvas::Item->new ($canvas->root,
                                        'Gnome2::Canvas::RichText',
                                        wrap_mode => 'word',
                                        editable => 0,
                                        cursor_visible => 0,
                                        x => 0, y => 0);
$rich_text->show;

update ();

$window->signal_connect (button_release_event => sub {update (); FALSE});

Gtk2->main;

sub update {
        my $text = `/usr/games/fortune`;
        my $layout = $canvas->create_pango_layout ($text);
        $layout->set_width (175 * PANGO_SCALE);
        my ($w, $h) = $layout->get_pixel_size;

        print "text dims : $w x $h\n";

        $rich_text->set (text => $text, width => $w, height => $h);
        $canvas->set_scroll_region (0, 0, $w, $h);

        if ($window->allocation->width < $w ||
            $window->allocation->height < $h) {
                $window->set_size_request ($w, $h);
                $window->queue_resize;
        }
}


--
Brian: If i recall correctly, this is the physics department.
Chris: That explains all that gravity.
        -- Family Guy, "The Story on Page One"




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