Set font size of a label using Pango attributes

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Set font size of a label using Pango attributes

Håkon Hægland
Hello,

I am trying to set the font size of a label using Pango attributes:


use strict;
use warnings;
use Gtk3 -init;
my $window = Gtk3::Window->new( 'toplevel' );
$window->signal_connect( destroy  => sub { Gtk3->main_quit() } );
my $label = Gtk3::Label->new('Hello world!');
my $attrlist = Pango::AttrList->new();
my $attr = Pango::AttrSize->new(1000);
Pango::AttrList::insert($attrlist, $attr);
Gtk3::Label::set_attributes($label, $attrlist);
$window->add( $label );
$window->set_default_size( 200, 200 );
$window->set_position('center_always');
$window->show_all();
Gtk3->main();
    

But unfortunately this does not compile. I get the following error:

Can't locate object method "new" via package "Pango::AttrSize" 

I have checked out the .xs file .../Pango-1.227/xs/PangoAttributes.xs 
but could not find any clue here where I went wrong.

I am using Ubuntu 17.04,
Perl version 5.26,
Pango Perl version 1.227,
Gtk3 Perl version 0.033.

Best regards,
Håkon Hægland


_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Set font size of a label using Pango attributes

oldtechaa
Hi Håkon,

I just tested this on Debian 9 "Stretch". The first problem is you need to 'use Pango'. Gtk3 does not automatically 'use Pango', so it will not find parts of Pango. I'm far from being an expert, so I'm not sure why it can skip 'Pango::AttrList->new()' without a problem, but it may expose certain parts of Pango through Gtk3.

The second problem is regarding font size. As it is currently, your font will be 1-point font. You may want something larger, like 8000, which approximately corresponds to 8-point font, which is still quite small. You can find out more about the font size and scaling in these parts of the docs:
https://developer.gnome.org/pango/stable/pango-Text-Attributes.html#pango-attr-size-new
https://developer.gnome.org/pango/stable/pango-Glyph-Storage.html#PANGO-SCALE:CAPS

The final code is:

use strict;
use warnings;
use Gtk3 -init;
use Pango;
my $window = Gtk3::Window->new( 'toplevel' );
$window->signal_connect( destroy  => sub { Gtk3->main_quit() } );
my $label = Gtk3::Label->new('Hello world!');
my $attrlist = Pango::AttrList->new();
my $attr = Pango::AttrSize->new(8000);
Pango::AttrList::insert($attrlist, $attr);
Gtk3::Label::set_attributes($label, $attrlist);
$window->add( $label );
$window->set_default_size( 200, 200 );
$window->set_position('center_always');
$window->show_all();
Gtk3->main();

Hope this helps,
oldtechaa

On Fri, Dec 29, 2017 at 3:00 PM, Håkon Hægland <[hidden email]> wrote:
Hello,

I am trying to set the font size of a label using Pango attributes:


use strict;
use warnings;
use Gtk3 -init;
my $window = Gtk3::Window->new( 'toplevel' );
$window->signal_connect( destroy  => sub { Gtk3->main_quit() } );
my $label = Gtk3::Label->new('Hello world!');
my $attrlist = Pango::AttrList->new();
my $attr = Pango::AttrSize->new(1000);
Pango::AttrList::insert($attrlist, $attr);
Gtk3::Label::set_attributes($label, $attrlist);
$window->add( $label );
$window->set_default_size( 200, 200 );
$window->set_position('center_always');
$window->show_all();
Gtk3->main();
    

But unfortunately this does not compile. I get the following error:

Can't locate object method "new" via package "Pango::AttrSize" 

I have checked out the .xs file .../Pango-1.227/xs/PangoAttributes.xs 
but could not find any clue here where I went wrong.

I am using Ubuntu 17.04,
Perl version 5.26,
Pango Perl version 1.227,
Gtk3 Perl version 0.033.

Best regards,
Håkon Hægland


_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list



_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list