Hi monksp,
You can use $font->configure(-size => $new_size) to change the size of an existing font.
Unfortunately, looking at the Tk documentation, I don't
see any clear-cut way to query a Font for its current size. But if you save the size of the font when you create it, you could then add to or subtract from that size, and call configure on the font to make a relative change.
Here's an example program concocted to show how you could manage the font with a wrapper. You pass the subroutine manage_font() either an array reference (to create a new font), or a hash reference (representing a previously-created "font wrapper"). An optional final parameter specifies a delta size (eg. "-4" or "+8") which changes the point size of the font (either of the new font, or the existing font).
For a new font, you pass an array reference containing the widget to apply the font to, the font family, and the font face:
my $h = manage_font([ $mw, $font_family, $font_size ]);
You can do the same thing, but adding a "delta size" to adjust the font size, eg.:
# Create a font 1 point smaller than $font_size
my $h = manage_font([ $mw, $font_family, $font_size ], "-1");
which I think addresses the question you asked.
Finally, you can change the size of the font later by using the "font wrapper" with a delta size:
manage_font($h, "+7") # Increase font size by 7 points
The convenience of changing the font size later is demonstrated in the program with the 2 buttons that decrease or decrease the current font size for the font wrapped by $h_font. The accelerator keys '-' and '+' can also be used to invoke these buttons.
Here's the full program:
use strict;
use warnings;
use Tk;
use Tk::Font;
use Data::Dumper;
my $font_family = "tahoma";
my $font_size = "24";
my $mw = new MainWindow();
my $h_font = manage_font([ $mw, $font_family, $font_size ]);
my $b1 = $mw->Button(-text => "Decrease Font Size (-)", -bg => 'lightg
+reen');
my $b2 = $mw->Button(-text => "Increase Font Size (+)", -bg => 'lightg
+reen');
$b1->pack($b2);
$b1->configure(-font => $h_font->{'font'});
$b2->configure(-font => $h_font->{'font'});
$b1->configure(-command => sub { manage_font($h_font, "-1") });
$b2->configure(-command => sub { manage_font($h_font, "+1") });
my $h_font1 = manage_font([ $mw, $font_family, $font_size ], "-8");
my $h_font2 = manage_font([ $mw, $font_family, $font_size ], "-4");
my $h_font3 = manage_font([ $mw, $font_family, $font_size ], "+0");
my $h_font4 = manage_font([ $mw, $font_family, $font_size ], "+4");
my $h_font5 = manage_font([ $mw, $font_family, $font_size ], "+8");
my $l1 = $mw->Label(-text => "Tiny font", -bg => 'cyan', -relief =>
+'ridge');
my $l2 = $mw->Label(-text => "Small font", -bg => 'cyan', -relief =>
+'ridge');
my $l3 = $mw->Label(-text => "Medium font", -bg => 'cyan', -relief =>
+'ridge');
my $l4 = $mw->Label(-text => "Large font", -bg => 'cyan', -relief =>
+'ridge');
my $l5 = $mw->Label(-text => "Huge font", -bg => 'cyan', -relief =>
+'ridge');
$l1->pack($l2, $l3, $l4, $l5);
$l1->configure(-font => $h_font1->{'font'});
$l2->configure(-font => $h_font2->{'font'});
$l3->configure(-font => $h_font3->{'font'});
$l4->configure(-font => $h_font4->{'font'});
$l5->configure(-font => $h_font5->{'font'});
$mw->bind("<Escape>" => sub { exit });
$mw->bind("<minus>" => sub { $b1->invoke });
$mw->bind("<plus>" => sub { $b2->invoke });
MainLoop;
sub manage_font {
my ($details, $modify) = @_;
my $h_font = { };
if (ref $details eq "ARRAY") {
# Create a new font
my ($w, $family, $size) = @$details;
$h_font->{'family'} = $family;
$h_font->{'size'} = $size;
$h_font->{'font'} = $w->Font(-family => $family, -size => $s
+ize);
} elsif (ref $details eq "HASH") {
# Modify the existing font
$h_font = $details;
}
if (($modify || 0) and $modify =~ /^[-+]\d+$/) {
my $new_size = $h_font->{'size'} + $modify;
if ($new_size > 0 and $new_size < 100) {
$h_font->{'font'}->configure(-size => $new_size);
$h_font->{'size'} = $new_size;
}
}
return $h_font;
}
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
|