Text size

View: New views
7 Messages — Rating Filter:   Alert me  

Text size

by Roode, Eric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How do I compute the size (amount of screen real-estate) that a given
chunk of text will be, in a given font?

The reason I ask is that I am trying to change the text of a label, and
using the Text or Change methods do not resize the control; the control
stays at its original size and truncates any new text. (This behavior is
very annoying!).

Here is an example:

    my $main = Win32::GUI::Window->new (-name => 'Main', -width => 300,
-height => 150);
    my $label = $main->AddLabel (-text => 'foo');
    $label->Text('Much longer now');
    $main->Show;
    Win32::GUI::Dialog;

What displays is "Mu", because that's all that can fit in the space
where "foo" was.

Is there a way to make a Label auto-resize?

Thanks in advance,

Eric

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Re: Text size

by Sean Healy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> How do I compute the size (amount of screen real-estate) that a given
> chunk of text will be, in a given font?
>
> The reason I ask is that I am trying to change the text of a label, and
> using the Text or Change methods do not resize the control; the control
> stays at its original size and truncates any new text. (This behavior is
> very annoying!).

($x, $y) = $Label->GetTextExtentPoint32($string);


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Re: Text size

by Roode, Eric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
>> How do I compute the size (amount of screen real-estate) that a given

>> chunk of text will be, in a given font?
>>
>> The reason I ask is that I am trying to change the text of a label,
>> and using the Text or Change methods do not resize the control; the
>> control stays at its original size and truncates any new text. (This
>> behavior is very annoying!).
>
>($x, $y) = $Label->GetTextExtentPoint32($string);
>

Fantastic, thank you!
Eric

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Re: Text size

by Roode, Eric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 17 July 2008, Sean Healy wrote:
>
> ($x, $y) = $Label->GetTextExtentPoint32($string);

Upon further experimentation, there is something not quite right with
that.  The width it returns is too wide.  There seems to be some sort
of per-character skew -- short strings are very accurate; longer
strings get progressively worse.

Here is a sample program to demonstrate the problem.  I have attached
a screenshot of the resulting output.


  use strict;
  use warnings;
  use Win32::GUI();
 
  my $main = Win32::GUI::Window->new (-name => 'Main', -width => 300,
                                      -height => 150);
 
  my $top = 0;
  for my $str ('', 'A', 'AAAAA', 'This is a test')
  {
      # Demonstrate the width as computed
      my $demo = $main->AddLabel(-text => 'dummy', -top => $top,
                                 -left => 150, -background => 0xFF80FF);
      my ($w, $h) = $demo->GetTextExtentPoint32($str);
 
      $demo->Text($str);
      $demo->Width($w);
 
      # Label the demo output
      my $info = $main->AddLabel(-text => "'$str' => w=$w",
                                 -top => $top, -left => 0);
      $top += $info->Height;
  }
 
  $main->Show;
  Win32::GUI::Dialog;
  exit;
 
  sub Main_Terminate { -1; }


Why do the longer fields have a large error in the computed width?

Thanks,
Eric


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

perlgui.png (10K) Download Attachment

Re: Text size

by Sean Healy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> ($x, $y) = $Label->GetTextExtentPoint32($string);
>
> Upon further experimentation, there is something not quite right with
> that.  The width it returns is too wide.  There seems to be some sort
> of per-character skew -- short strings are very accurate; longer
> strings get progressively worse.

<snip>

> Why do the longer fields have a large error in the computed width?

My guess is that it has to do with kerning. Try it with a monospace font
(or other non-kerning font) and see you still have the problem. If you
don't, there's nothing we can do about it, because the problem is in
Microsoft code, not Win32::GUI code.

If you're not familiar with kerning, look here:
http://en.wikipedia.org/wiki/Kerning

 From MSDN:

The GetTextExtentPoint32 function uses the currently selected font to
compute the dimensions of the string. The width and height, in logical
units, are computed without considering any clipping.

Because some devices kern characters, the sum of the extents of the
characters in a string may not be equal to the extent of the string.



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Re: Text size

by Robert May-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/7/18 Roode, Eric <eroode@...>:
>
> On 17 July 2008, Sean Healy wrote:
>>
>> ($x, $y) = $Label->GetTextExtentPoint32($string);
>
> Upon further experimentation, there is something not quite right with
> that.  The width it returns is too wide.  There seems to be some sort
> of per-character skew -- short strings are very accurate; longer
> strings get progressively worse.

The Win32::GUI GetTextExtentPoint32(...) implementation uses the
default system font sizing if you don't tell it what font to use.  I
think this is not as expected (or intended).

Try this instead as a work around:
my ($x, $y) = $Label->GetTextExtentPoint32( $string, $Label->GetFont() );

It would be much appreciated if you could raise a bug report for this,
as if called as a method against a window without a font specified
then I think the implementation should use the window's current font.

Regards,
Rob.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Re: Text size

by Roode, Eric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 18 July 2008, Robert May wrote:
>
>2008/7/18 Roode, Eric <eroode@...>:
>> On 17 July 2008, Sean Healy wrote:
>>> ($x, $y) = $Label->GetTextExtentPoint32($string);
>>
>> Upon further experimentation, there is something not quite right with

>> that.  The width it returns is too wide.  There seems to be some sort

>> of per-character skew -- short strings are very accurate; longer
>> strings get progressively worse.
>
>The Win32::GUI GetTextExtentPoint32(...) implementation uses the
>default system font sizing if you don't tell it what font to use.  I
>think this is not as expected (or intended).
>
>Try this instead as a work around:
>my ($x, $y) = $Label->GetTextExtentPoint32( $string, $Label->GetFont()
);

Thanks, Rob!  That worked perfectly.  At least in the fonts I tried.

>It would be much appreciated if you could raise a bug report for
>this, as if called as a method against a window without a font
>specified then I think the implementation should use the window's
>current font.

Sure, I can do that. No prob.

--
Eric

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@...
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/
LightInTheBox - Buy quality products at wholesale price