Debian Codegraph @

tools/analyzer.py

raw
1 #!/usr/bin/env python
2 #
3 # Copyright 2015 Google Inc. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """
18 Quick analysis tool for the compreffor. Compresses a directory of
19 fonts and writes a CSV containing various stats about the compression.
20
21 Usage (command line):
22 >>> ./analyzer.py /path/to/font/dir
23 ...
24
25 A CSV will be written to the current working directory with
26 the name data.csv.
27 """
28
29 import csv
30 import os
31 import time
32 from compreffor import cxxCompressor
33
34 from fontTools import subset
35 from fontTools.ttLib import TTFont
36
37 def sum_subrs(font):
38 td = font['CFF '].cff.topDictIndex[0]
39 ans = len(td.GlobalSubrs)
40 try:
41 ans += sum(len(fd.Private.Subrs) if hasattr(fd.Private, 'Subrs') else 0 for fd in td.FDArray)
42 except AttributeError:
43 pass
44 return ans
45
46 if __name__ == '__main__':
47 names = []
48 times = []
49 orig_sizes = []
50 full_sizes = []
51 compressed_sizes = []
52 nsubrs = []
53 nsubrs_orig = []
54
55 for root, dirs, files in os.walk(os.argv[1]):
56 for filename in files:
57 if os.path.splitext(filename)[1] == '.otf':
58 fname = os.path.join(root, filename)
59 print "Handling %s" % filename
60
61 # decompress
62 print("\tDecompressing...")
63 font = TTFont(fname)
64 orig_subrs = sum_subrs(font)
65 orig_size = os.path.getsize(fname)
66
67 options = subset.Options()
68 options.decompress = True
69 subsetter = subset.Subsetter(options=options)
70 subsetter.populate(glyphs=font.getGlyphOrder())
71 subsetter.subset(font)
72 name_parts = os.path.splitext(fname)
73 new_fname = name_parts[0] + '-decomp' + name_parts[1]
74 font.save(new_fname)
75 full_size = os.path.getsize(new_fname)
76
77 print("\tSubroutinizing...")
78 print("----")
79 start_time = time.time()
80 cxxCompressor.main(filename=new_fname, verbose=True)
81 times.append(time.time() - start_time)
82 print("----")
83
84 print("\tTabulating results...")
85 comp_fname = name_parts[0] + '-decomp.compressed' + name_parts[1]
86 comp_subrs = sum_subrs(TTFont(comp_fname))
87 comp_size = os.path.getsize(comp_fname)
88
89 orig_sizes.append(orig_size)
90 full_sizes.append(full_size)
91 compressed_sizes.append(comp_size)
92 names.append(filename)
93 nsubrs_orig.append(orig_subrs)
94 nsubrs.append(comp_subrs)
95
96 with open('data.csv', 'w') as csvf:
97 nwriter = csv.writer(csvf)
98 nwriter.writerow(['Name', 'Time to compress', 'Original Size',
99 'Expanded Size', 'Compressed Size',
100 'Original # of Subrs', 'Compressed # of Subrs'])
101 nwriter.writerows(zip(names, times, orig_sizes, full_sizes,
102 compressed_sizes, nsubrs_orig, nsubrs))