Merge b2g-inbound to m-c. a=merge
[gecko.git] / gfx / tests / process-textruns.pl
blob5fa85e7bfb6ffc0f267a4bb2673b8c07faa5ad57
1 #!/usr/bin/perl -w
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # This script is a bunch of utilities for computing statistics about the textruns
8 # created during a Gecko run.
10 # Usage:
11 # 1) Uncomment #define DUMP_TEXT_RUNS in gfxAtsuiFonts.cpp
12 # 2) Build
13 # 3) Run over some test set, redirecting stdout to a file
14 # 4) Pipe that file through this script
16 # --exclude-spaces-only: ignore all textruns that consistent of zero or more
17 # spaces
18 my $exclude_spaces = grep(/^--exclude-spaces-only$/, @ARGV);
20 # --dump-runs: process textruns into a format that can be used by
21 # gfxTextRunPerfTest, print that on standard output, and do nothing else
22 my $dump_runs = grep(/^--dump-runs$/, @ARGV);
24 # --obfuscate: ROTL13 the textrun text
25 my $obfuscate = grep(/^--obfuscate$/, @ARGV);
27 my @textruns = ();
29 while (<STDIN>) {
30 if (/^0x(\w+)\((.*)\) TEXTRUN "(.*)" ENDTEXTRUN$/) {
31 my %tr = ( fontgroup => $1,
32 families => $2,
33 text => $3 );
34 push(@textruns, \%tr);
35 } elsif (/^0x(\w+)\((.*)\) TEXTRUN "(.*)$/) {
36 my %tr = ( fontgroup => $1,
37 families => $2 );
38 my $text = $3."\n";
39 while (<STDIN>) {
40 if (/^(.*)" ENDTEXTRUN$/) {
41 $text .= $1;
42 last;
44 $text .= $_;
46 $tr{text} = $text;
47 push(@textruns, \%tr);
51 my %quote = ( "\\" => 1, "\"" => 1 );
53 sub quote_str {
54 my ($text) = @_;
55 my @chars = split(//, $text);
56 my @strs = ();
57 foreach my $c (@chars) {
58 if (ord($c) >= 0x80) {
59 $c = "\\x".sprintf("%x",ord($c)).'""';
60 } elsif ($quote{$c}) {
61 $c = "\\$c";
62 } elsif ($c eq "\n") {
63 $c = " ";
65 push(@strs, $c);
67 return '"'.join("", @strs).'"';
70 if ($dump_runs) {
71 foreach my $tr (@textruns) {
72 print "{ ", &quote_str($tr->{families}), ",\n";
73 my $text = $tr->{text};
74 if ($obfuscate) {
75 $text =~ tr/a-mA-Mn-zN-Z/n-zN-Za-mA-M/;
77 print " ", &quote_str($text), " },\n";
79 exit(0);
82 my %trs_by_text = ();
83 my %trs_by_text_and_fontgroup = ();
84 my %trs_by_trimmed_text_and_fontgroup = ();
85 my @tr_lengths = ();
87 $trs_by_text{" "} = [];
88 $trs_by_text{""} = [];
90 sub trim {
91 my ($s) = @_;
92 $s =~ s/^ *//g;
93 $s =~ s/ *$//g;
94 return $s;
97 my $total_textruns = 0;
99 foreach my $tr (@textruns) {
100 if ($exclude_spaces && $tr->{text} =~ /^ *$/) {
101 next;
103 ++$total_textruns;
104 push(@{$trs_by_text{$tr->{text}}}, $tr);
105 push(@{$trs_by_text_and_fontgroup{$tr->{fontgroup}.$tr->{text}}}, $tr);
106 push(@{$trs_by_trimmed_text_and_fontgroup{$tr->{fontgroup}.&trim($tr->{text})}}, $tr);
107 if (1 < scalar(@{$trs_by_trimmed_text_and_fontgroup{$tr->{fontgroup}.&trim($tr->{text})}})) {
108 $tr_lengths[length($tr->{text})]++;
112 print "Number of textruns:\t$total_textruns\n";
113 print "Number of textruns which are one space:\t", scalar(@{$trs_by_text{" "}}), "\n";
114 print "Number of textruns which are empty:\t", scalar(@{$trs_by_text{""}}), "\n";
116 my $count = 0;
117 foreach my $k (keys(%trs_by_text)) {
118 if ($k =~ /^ *$/) {
119 $count += @{$trs_by_text{$k}};
122 print "Number of textruns which are zero or more spaces:\t$count\n";
124 print "Number of unique textruns by text and fontgroup:\t", scalar(keys(%trs_by_text_and_fontgroup)), "\n";
125 print "Number of unique textruns by trimmed text and fontgroup:\t", scalar(keys(%trs_by_trimmed_text_and_fontgroup)), "\n";
127 my $sum = 0;
128 my $weighted_sum = 0;
129 if (1) {
130 print "Textrun length distribution:\n";
131 for my $i (0..(scalar(@tr_lengths)-1)) {
132 my $amount = defined($tr_lengths[$i])?$tr_lengths[$i]:0;
133 $sum += $amount;
134 $weighted_sum += $i*$amount;
135 print "$i\t$sum\t$weighted_sum\n";