def test_update_program_multiple(self): """Test update_program with two replacements""" program = [7, 2, 10, 4, 8, 7, 0] substr = pyCompressor.CandidateSubr(3, (0, 1)) substr._position = 5 substr._global = True substr2 = pyCompressor.CandidateSubr(2, (0, 5)) substr2._position = 21 substr2._global = True encoding = [(1, substr), (5, substr2)] bias = 0 self.empty_compreffor.update_program(program, encoding, bias, [bias], 0) self.assertEqual(program, [7, 5, "callgsubr", 8, 21, "callgsubr"])
def setUp(self): self.glyph_set = DummyGlyphSet({ 'a': (0, 1, 20, 21, 22, 2), 'b': (7, 0, 1, 20, 21, 22, 2), 'c': (0, 1, 20, 21, 22, 9, 3, 17) }) self.sf = pyCompressor.SubstringFinder(self.glyph_set) self.short_sf = pyCompressor.SubstringFinder( DummyGlyphSet({ 'a': (1, 2, 3), 'b': (8, 1, 4) })) self.rand_gs = DummyGlyphSet() num_glyphs = random.randint(5, 20) for i in range(num_glyphs): length = random.randint(2, 30) self.rand_gs[i] = tuple( random.randint(0, 100) for _ in range(length)) self.random_sf = pyCompressor.SubstringFinder( DummyGlyphSet(self.rand_gs)) length = 3 locations = [(0, 0), (1, 4)] charstrings = [(348, 374, 'rmoveto', 'endchar'), (123, -206, -140, 'hlineto', 348, 374, 'rmoveto', 'endchar')] self.cand_subr = pyCompressor.CandidateSubr(length, locations[0], 2, charstrings) self.empty_compreffor = pyCompressor.Compreffor(None, test_mode=True)
def test_collect_lsubrs_called_from(self): """Test collecting local subrs called from any global subrs""" g1 = pyCompressor.CandidateSubr(3, (0, 10)) g1._global = True g2 = pyCompressor.CandidateSubr(3, (0, 20)) g2._global = True g3 = pyCompressor.CandidateSubr(3, (0, 30)) g3._global = True l1 = pyCompressor.CandidateSubr(3, (0, 40)) l1._global = False l2 = pyCompressor.CandidateSubr(3, (0, 50)) l2._global = False l3 = pyCompressor.CandidateSubr(3, (0, 60)) l3._global = False g1._encoding = [(3, l1)] g2._encoding = [(3, l2), (6, g3)] g3._encoding = [] l1._encoding = [] l2._encoding = [(3, l3)] l3._encoding = [] lsubrs = self.empty_compreffor.collect_lsubrs_called_from([g1, g2, g3]) self.assertSetEqual(lsubrs, {l1, l2, l3})
def test_update_program_global(self): """Test update_program with only one replacement""" program = [7, 2, 10, 4, 8, 7, 0] substr = pyCompressor.CandidateSubr(3, (0, 1)) substr._position = 5 substr._fdidx = [0] substr._global = True encoding = [(1, substr)] bias = 0 self.empty_compreffor.update_program(program, encoding, bias, [bias], 0) self.assertEqual(program, [7, 5, "callgsubr", 8, 7, 0])
def get_substrings(self, min_freq=2, check_positive=True, sort_by_length=False): movetos = set() for idx, tok in enumerate(self.rev_keymap): if isinstance(tok, basestring) and tok[-6:] == "moveto": movetos.add(idx) try: hmask = self.rev_keymap.index("hintmask") except ValueError: hmask = None matches = {} for glyph_idx, program in enumerate(self.data): cur_start = 0 last_op = -1 for pos, tok in enumerate(program): if tok in movetos: stop = last_op + 1 if stop - cur_start > 0: if program[cur_start:stop] in matches: matches[program[cur_start:stop]].freq += 1 else: span = pyCompressor.CandidateSubr( stop - cur_start, (glyph_idx, cur_start), 1, self.data, self.cost_map) matches[program[cur_start:stop]] = span cur_start = pos + 1 elif tok == hmask: last_op = pos + 1 elif type(self.rev_keymap[tok]) == str: last_op = pos constraints = lambda s: (s.freq >= min_freq and (s.subr_saving() > 0 or not check_positive)) self.substrings = filter(constraints, matches.values()) if sort_by_length: self.substrings.sort(key=lambda s: len(s)) else: self.substrings.sort(key=lambda s: s.subr_saving(), reverse=True) return self.substrings