Debian Codegraph @

tools/simpleCffCompressor.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 import argparse
18 from compreffor import pyCompressor
19
20 class SimpleSubstringFinder(pyCompressor.SubstringFinder):
21 def get_substrings(self, min_freq=2, check_positive=True, sort_by_length=False):
22 movetos = set()
23 for idx, tok in enumerate(self.rev_keymap):
24 if isinstance(tok, basestring) and tok[-6:] == "moveto":
25 movetos.add(idx)
26 try:
27 hmask = self.rev_keymap.index("hintmask")
28 except ValueError:
29 hmask = None
30
31 matches = {}
32
33 for glyph_idx, program in enumerate(self.data):
34 cur_start = 0
35 last_op = -1
36 for pos, tok in enumerate(program):
37 if tok in movetos:
38 stop = last_op + 1
39 if stop - cur_start > 0:
40 if program[cur_start:stop] in matches:
41 matches[program[cur_start:stop]].freq += 1
42 else:
43 span = pyCompressor.CandidateSubr(stop - cur_start,
44 (glyph_idx, cur_start),
45 1,
46 self.data,
47 self.cost_map)
48 matches[program[cur_start:stop]] = span
49 cur_start = pos + 1
50 elif tok == hmask:
51 last_op = pos + 1
52 elif type(self.rev_keymap[tok]) == str:
53 last_op = pos
54
55 constraints = lambda s: (s.freq >= min_freq and
56 (s.subr_saving() > 0 or not check_positive))
57 self.substrings = filter(constraints, matches.values())
58 if sort_by_length:
59 self.substrings.sort(key=lambda s: len(s))
60 else:
61 self.substrings.sort(key=lambda s: s.subr_saving(), reverse=True)
62 return self.substrings
63
64 if __name__ == '__main__':
65 parser = argparse.ArgumentParser(description='Subroutinize a font.')
66 parser.add_argument('filename', help='Where to find the font', nargs='*')
67 parser.add_argument('-t', required=False, action='store_true',
68 dest='test', default=False)
69 parser.add_argument('-v', required=False, action='store_true',
70 dest='verbose_test', default=False)
71 parser.add_argument('-c', required=False, action='store_true',
72 dest='check', default=False)
73
74 kwargs = vars(parser.parse_args())
75
76 pyCompressor.SubstringFinder = SimpleSubstringFinder
77 pyCompressor.main(**kwargs)