gfx/tests/process-textruns.pl

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial