Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: font measurement

by zentara (Archbishop)
on Jan 30, 2012 at 15:30 UTC ( #950790=note: print w/replies, xml ) Need Help??

in reply to font measurement

See Re: Tk: Set text size in a widget and "perldoc Tk::Font" and "perldoc Tk::X11Font", but using the Canvas is always a good bet.
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=> 28 ); my $c = $mw->Canvas(-bg=>'white')->pack; #---determine font spacing by making a capital W--- my $fonttest = $c->createText(100,100, -fill => 'black', -text => 'W', -font => 'big' ); my ($bx,$by,$bx1,$by1) = $c->bbox($fonttest); $c->{'f_width'} = $bx1 - $bx; $c->{'f_height'} = $by1 - $by; print 'width ',$c->{'f_width'},' ',$c->{'f_height'},"\n"; MainLoop;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: font measurement
by preahkumpii (Novice) on Jan 30, 2012 at 15:47 UTC
    I appears from this code (I am no perl professional) that a window is created for the text. In my case the measuring will only be used as a means to determine the length of the string to keep, and what to remove. So, it is just an intermediate step. So if this code opens a separate window I think that would have a significant performance hit on this already slow program (not the example I gave, but the real program I am working on).
      BrowserUk gave you the preferred answer below, but if you wanted to use my Canvas method, without invoking a window, all you need to do is comment out the line #MainLoop. Then if you run it, it will not show a window.

      Either way, you are loading Tk. You can either load Tk::Text or a Tk::Canvas. Tk is relatively light and fast to load.

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950790]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-05-28 08:13 GMT
Find Nodes?
    Voting Booth?