| 40 | | def _searchForMatch(self, text, deleting=False): |
|---|
| 41 | | # no text |
|---|
| 42 | | if not text: |
|---|
| 43 | | return text, None |
|---|
| 44 | | glyphNames = self._font.keys() |
|---|
| 45 | | match = None |
|---|
| 46 | | # direct match |
|---|
| 47 | | if text in glyphNames: |
|---|
| 48 | | match = text |
|---|
| 49 | | # character entry |
|---|
| 50 | | elif len(text) == 1: |
|---|
| 51 | | uniValue = ord(text) |
|---|
| 52 | | match = self._font.unicodeData.glyphNameForUnicode(uniValue) |
|---|
| 53 | | if match is not None: |
|---|
| 54 | | text = "" |
|---|
| 55 | | # fallback. find closest match |
|---|
| 56 | | if match is None: |
|---|
| 57 | | glyphNames = list(sorted(glyphNames)) |
|---|
| 58 | | if not deleting: |
|---|
| 59 | | for glyphName in glyphNames: |
|---|
| 60 | | if glyphName.startswith(text): |
|---|
| 61 | | match = glyphName |
|---|
| 62 | | break |
|---|
| 63 | | else: |
|---|
| 64 | | for glyphName in glyphNames: |
|---|
| 65 | | if text.startswith(glyphName): |
|---|
| 66 | | match = glyphName |
|---|
| 67 | | elif match is not None: |
|---|
| 68 | | break |
|---|
| 69 | | return text, match |
|---|
| | 41 | def _search(text, font, deleting): |
|---|
| | 42 | """ |
|---|
| | 43 | >>> from fontTools.agl import AGL2UV |
|---|
| | 44 | >>> from defcon import Font |
|---|
| | 45 | >>> font = Font() |
|---|
| | 46 | >>> for glyphName, value in AGL2UV.items(): |
|---|
| | 47 | ... font.newGlyph(glyphName) |
|---|
| | 48 | ... font[glyphName].unicodes = [value] |
|---|
| | 49 | ... font.newGlyph(glyphName + ".alt") |
|---|
| | 50 | ... font.newGlyph(glyphName + ".xxx") |
|---|
| | 51 | |
|---|
| | 52 | >>> _search("e", font, False) |
|---|
| | 53 | ('e', 'e') |
|---|
| | 54 | >>> _search("eg", font, False) |
|---|
| | 55 | ('eg', 'egrave') |
|---|
| | 56 | >>> _search("e.", font, False) |
|---|
| | 57 | ('e.', 'e.alt') |
|---|
| | 58 | |
|---|
| | 59 | >>> _search("eight.al", font, True) |
|---|
| | 60 | ('eight.al', 'eight') |
|---|
| | 61 | """ |
|---|
| | 62 | # no text |
|---|
| | 63 | if not text: |
|---|
| | 64 | return text, None |
|---|
| | 65 | glyphNames = font.keys() |
|---|
| | 66 | match = None |
|---|
| | 67 | # direct match |
|---|
| | 68 | if text in glyphNames: |
|---|
| | 69 | match = text |
|---|
| | 70 | # character entry |
|---|
| | 71 | elif len(text) == 1: |
|---|
| | 72 | uniValue = ord(text) |
|---|
| | 73 | match = font.unicodeData.glyphNameForUnicode(uniValue) |
|---|
| | 74 | if match is not None: |
|---|
| | 75 | text = "" |
|---|
| | 76 | # fallback. find closest match |
|---|
| | 77 | if match is None: |
|---|
| | 78 | glyphNames = list(sorted(glyphNames)) |
|---|
| | 79 | if not deleting: |
|---|
| | 80 | for glyphName in glyphNames: |
|---|
| | 81 | if glyphName.startswith(text): |
|---|
| | 82 | match = glyphName |
|---|
| | 83 | break |
|---|
| | 84 | else: |
|---|
| | 85 | matches = [] |
|---|
| | 86 | for glyphName in glyphNames: |
|---|
| | 87 | if text.startswith(glyphName): |
|---|
| | 88 | matches.append(glyphName) |
|---|
| | 89 | elif match is not None: |
|---|
| | 90 | break |
|---|
| | 91 | diff = None |
|---|
| | 92 | for m in matches: |
|---|
| | 93 | d = len(m) - len(text) |
|---|
| | 94 | if diff is None or d < diff: |
|---|
| | 95 | match = m |
|---|
| | 96 | return text, match |
|---|
| | 97 | |
|---|
| | 98 | |
|---|
| | 99 | if __name__ == "__main__": |
|---|
| | 100 | import doctest |
|---|
| | 101 | doctest.testmod() |
|---|