Hi,

The version which I currently use isn't too long. Effectively, it is a hack, 
but it does what I need now. Here is a diff for TTFMetrics.pm.

I think a major rework were possible. It could work by so, that we read the the 
whole ttf in a step. We build a hash for the glyphes, and another for the 
character -> glyph conversion. It will use a little bit more memory, but can be 
even 1000 times faster.

But this hack made the width generation 100 times faster.

In my current project, I create big tables, even with hundred of thousands of 
strings. And it _does_ matter, that a table generation lasts 30 sec or needs 50 
minutes.

Thanks,

Akos

--- /home/cardiff2/adhoak0/tool/lib/perl5/site_perl/5.14.2/Font/TTFMetrics.pm   
2003-06-12 17:44:29.000000000 +0200
+++ TTFMetrics.pm       2012-04-11 14:32:23.975126000 +0200
@@ -122,6 +122,8 @@
 use Carp;
 use strict;

+#use Devel::NYTProf;
+
 my @glyph_name_index = ();
 my @post_glyph_name  = ();
 my @mac_glyph_name   = ();
@@ -168,7 +170,7 @@
     $self->{encoding}      = 1;
     $self->{subfamily}     = undef;
     $self->{glyph_index}   = [];
-    $self->{advance_width} = [];
+    $self->{advance_width} = ();
     $self->{lsb}           = [];

     #   $self->{number_of_glyphs} = undef;
@@ -266,7 +268,9 @@
     my $fh   = $self->get_file_handle();
     my $buf  = "";

-    eval { read( $fh, $buf, 12 ) };
+    #eval { read( $fh, $buf, 12 ) };
+    read( $fh, $buf, 12 ) == 12 or die "can't read in font path";
+
     if ($@) {
         croak "Read error in Pastel::Font::TTF::make_directory_entry\n";
     }
@@ -322,7 +326,6 @@
     }
     my $index = $self->get_glyph_index($ord);
     return $self->get_advance_width($index);
-
 }

 =head2 string_width()
@@ -802,6 +805,11 @@
 sub get_advance_width {
     my $self  = shift;
     my $index = shift;                      # glyph index
+
+    if (exists $self->{advance_width}{$index}) {
+        return $self->{advance_width}{$index};
+    }
+
     my $fh    = $self->get_file_handle();
     my $buf;

@@ -830,10 +838,13 @@

     #if ( $index > @lsb ) { $index = @lsb; }
     my $a =
-      $advanced_width[$index] - ( $advanced_width[$index] > 32768 ? 65536 : 0 
);
-    #my $l = $lsb[$index] - ( $lsb[$index] > 32768 ? 65536 : 0 );
+      $advanced_width[$index] - ( $advanced_width[$index] >= 32768 ? 65536 : 0 
);
+    #my $l = $lsb[$index] - ( $lsb[$index] >= 32768 ? 65536 : 0 );

     #return $a, $l;
+    if ($a) {
+        $self->{advance_width}{$index} = $a;
+    }
     return $a ? $a : undef;
 }


-----Ursprüngliche Nachricht-----
Von: Malay Basu [mailto:malaykb...@gmail.com] 
Gesendet: Mittwoch, 11. April 2012 19:02
An: Horvath, Akos
Cc: Matt S Trout; modu...@cpan.org; ma...@bioinformatics.org
Betreff: Re: warning: false alarm, possible bug in Font::TTFMetrics

Akos,

Send me the code (email is the best). I'll check that it does not
break anything. As you can understand, It's a very complex module.
I'll update the module to the next version with proper credit.

Thanks for being so vigilant.

-Malay

On Wed, Apr 11, 2012 at 12:57 PM, Horvath, Akos
<horvath.a...@siemens.com> wrote:
> 1. adding some comments :-)
>
> 2. this library is for calculating the lengths of ttf strings. It works okay, 
> but is slow, because it reads and reads parts of the ttf file in every call. 
> This needs optimiziation (effectively, a cache in a bottleneck), which I 
> already did, only want to upload.
>
> Thanks,
>
> Akos
>
> -----Ursprüngliche Nachricht-----
> Von: Matt S Trout [mailto:m...@shadowcat.co.uk]
> Gesendet: Mittwoch, 11. April 2012 18:48
> An: Malay Basu
> Cc: Horvath, Akos; modu...@cpan.org; ma...@bioinformatics.org
> Betreff: Re: warning: false alarm, possible bug in Font::TTFMetrics
>
> On Wed, Apr 11, 2012 at 12:43:20PM -0400, Malay Basu wrote:
>> Thanks Matt,
>>
>> I don't know what Akos is talking about. There is no unchecked eval in
>> the code. The only eval is to check that the input file is readable.
>> If the read call fails then the modules exits gracefully. There is
>> *no* problem with this eval.
>
> Oh. I see Akos forgot to show us the next line where it actually checks
> the exception. Ooops :)
>
> Even so, he clearly does care. Perhaps you should get Akos to add comments
> to make the code more clear so nobody gets confused like this again?
>
> Akos, would that be something you might enjoy doing?
>
> --
> Matt S Trout - Shadowcat Systems - Perl consulting with a commit bit and a 
> clue
>
> http://shadowcat.co.uk/blog/matt-s-trout/   http://twitter.com/shadowcat_mst/
>
> Email me now on mst (at) shadowcat.co.uk and let's chat about how our Catalyst
> commercial support, training and consultancy packages could help your team.

Reply via email to