gfx/tests/process-textruns.pl

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/tests/process-textruns.pl	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +#!/usr/bin/perl -w
     1.5 +
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +# This script is a bunch of utilities for computing statistics about the textruns
    1.11 +# created during a Gecko run.
    1.12 +#
    1.13 +# Usage:
    1.14 +# 1) Uncomment #define DUMP_TEXT_RUNS in gfxAtsuiFonts.cpp
    1.15 +# 2) Build
    1.16 +# 3) Run over some test set, redirecting stdout to a file
    1.17 +# 4) Pipe that file through this script
    1.18 +
    1.19 +# --exclude-spaces-only: ignore all textruns that consistent of zero or more
    1.20 +# spaces
    1.21 +my $exclude_spaces = grep(/^--exclude-spaces-only$/, @ARGV);
    1.22 +
    1.23 +# --dump-runs: process textruns into a format that can be used by
    1.24 +# gfxTextRunPerfTest, print that on standard output, and do nothing else
    1.25 +my $dump_runs = grep(/^--dump-runs$/, @ARGV);
    1.26 +
    1.27 +# --obfuscate: ROTL13 the textrun text
    1.28 +my $obfuscate = grep(/^--obfuscate$/, @ARGV);
    1.29 +
    1.30 +my @textruns = ();
    1.31 +
    1.32 +while (<STDIN>) {
    1.33 +  if (/^0x(\w+)\((.*)\) TEXTRUN "(.*)" ENDTEXTRUN$/) {
    1.34 +    my %tr = ( fontgroup => $1,
    1.35 +               families => $2,
    1.36 +               text => $3 );
    1.37 +    push(@textruns, \%tr);
    1.38 +  } elsif (/^0x(\w+)\((.*)\) TEXTRUN "(.*)$/) {
    1.39 +    my %tr = ( fontgroup => $1,
    1.40 +               families => $2 );
    1.41 +    my $text = $3."\n";
    1.42 +    while (<STDIN>) {
    1.43 +      if (/^(.*)" ENDTEXTRUN$/) {
    1.44 +        $text .= $1;
    1.45 +        last;
    1.46 +      }
    1.47 +      $text .= $_;
    1.48 +    }
    1.49 +    $tr{text} = $text;
    1.50 +    push(@textruns, \%tr);
    1.51 +  }
    1.52 +}
    1.53 +
    1.54 +my %quote = ( "\\" => 1, "\"" => 1 );
    1.55 +
    1.56 +sub quote_str {
    1.57 +    my ($text) = @_;
    1.58 +    my @chars = split(//, $text);
    1.59 +    my @strs = ();
    1.60 +    foreach my $c (@chars) {
    1.61 +      if (ord($c) >= 0x80) {
    1.62 +        $c = "\\x".sprintf("%x",ord($c)).'""';
    1.63 +      } elsif ($quote{$c}) {
    1.64 +        $c = "\\$c";
    1.65 +      } elsif ($c eq "\n") {
    1.66 +        $c = " ";
    1.67 +      }
    1.68 +      push(@strs, $c);
    1.69 +    }
    1.70 +    return '"'.join("", @strs).'"';
    1.71 +}
    1.72 +
    1.73 +if ($dump_runs) {
    1.74 +  foreach my $tr (@textruns) {
    1.75 +    print "{ ", &quote_str($tr->{families}), ",\n";
    1.76 +    my $text = $tr->{text};
    1.77 +    if ($obfuscate) {
    1.78 +        $text =~ tr/a-mA-Mn-zN-Z/n-zN-Za-mA-M/;
    1.79 +    }
    1.80 +    print "  ", &quote_str($text), " },\n";
    1.81 +  }
    1.82 +  exit(0);
    1.83 +}
    1.84 +
    1.85 +my %trs_by_text = ();
    1.86 +my %trs_by_text_and_fontgroup = ();
    1.87 +my %trs_by_trimmed_text_and_fontgroup = ();
    1.88 +my @tr_lengths = ();
    1.89 +
    1.90 +$trs_by_text{" "} = [];
    1.91 +$trs_by_text{""} = [];
    1.92 +
    1.93 +sub trim {
    1.94 +  my ($s) = @_;
    1.95 +  $s =~ s/^ *//g;
    1.96 +  $s =~ s/ *$//g;
    1.97 +  return $s;
    1.98 +}
    1.99 +
   1.100 +my $total_textruns = 0;
   1.101 +
   1.102 +foreach my $tr (@textruns) {
   1.103 +  if ($exclude_spaces && $tr->{text} =~ /^ *$/) {
   1.104 +    next;
   1.105 +  }
   1.106 +  ++$total_textruns;
   1.107 +  push(@{$trs_by_text{$tr->{text}}}, $tr);
   1.108 +  push(@{$trs_by_text_and_fontgroup{$tr->{fontgroup}.$tr->{text}}}, $tr);
   1.109 +  push(@{$trs_by_trimmed_text_and_fontgroup{$tr->{fontgroup}.&trim($tr->{text})}}, $tr);
   1.110 +  if (1 < scalar(@{$trs_by_trimmed_text_and_fontgroup{$tr->{fontgroup}.&trim($tr->{text})}})) {
   1.111 +    $tr_lengths[length($tr->{text})]++;
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +print "Number of textruns:\t$total_textruns\n";
   1.116 +print "Number of textruns which are one space:\t", scalar(@{$trs_by_text{" "}}), "\n";
   1.117 +print "Number of textruns which are empty:\t", scalar(@{$trs_by_text{""}}), "\n";
   1.118 +
   1.119 +my $count = 0;
   1.120 +foreach my $k (keys(%trs_by_text)) {
   1.121 +  if ($k =~ /^ *$/) {
   1.122 +    $count += @{$trs_by_text{$k}};
   1.123 +  }
   1.124 +}
   1.125 +print "Number of textruns which are zero or more spaces:\t$count\n";
   1.126 +
   1.127 +print "Number of unique textruns by text and fontgroup:\t", scalar(keys(%trs_by_text_and_fontgroup)), "\n";
   1.128 +print "Number of unique textruns by trimmed text and fontgroup:\t", scalar(keys(%trs_by_trimmed_text_and_fontgroup)), "\n";
   1.129 +
   1.130 +my $sum = 0;
   1.131 +my $weighted_sum = 0;
   1.132 +if (1) {
   1.133 +  print "Textrun length distribution:\n";
   1.134 +  for my $i (0..(scalar(@tr_lengths)-1)) {
   1.135 +    my $amount = defined($tr_lengths[$i])?$tr_lengths[$i]:0;
   1.136 +    $sum += $amount;
   1.137 +    $weighted_sum += $i*$amount;
   1.138 +    print "$i\t$sum\t$weighted_sum\n";
   1.139 +  }
   1.140 +}

mercurial