|
37 | 37 | import textwrap
|
38 | 38 | import os
|
39 | 39 |
|
| 40 | +try: |
| 41 | + from functools import lru_cache |
| 42 | +except ImportError: # Py2 |
| 43 | + from backports.functools_lru_cache import lru_cache |
| 44 | + |
40 | 45 | if six.PY3:
|
41 | 46 | def ord(x):
|
42 | 47 | return x
|
@@ -1056,36 +1061,19 @@ def find_tex_file(filename, format=None):
|
1056 | 1061 | 'debug')
|
1057 | 1062 | return result.decode('ascii')
|
1058 | 1063 |
|
| 1064 | + |
1059 | 1065 | # With multiple text objects per figure (e.g., tick labels) we may end
|
1060 | 1066 | # up reading the same tfm and vf files many times, so we implement a
|
1061 | 1067 | # simple cache. TODO: is this worth making persistent?
|
1062 | 1068 |
|
1063 |
| -_tfmcache = {} |
1064 |
| -_vfcache = {} |
1065 |
| - |
1066 |
| - |
1067 |
| -def _fontfile(texname, class_, suffix, cache): |
1068 |
| - try: |
1069 |
| - return cache[texname] |
1070 |
| - except KeyError: |
1071 |
| - pass |
1072 |
| - |
| 1069 | +@lru_cache() |
| 1070 | +def _fontfile(cls, suffix, texname): |
1073 | 1071 | filename = find_tex_file(texname + suffix)
|
1074 |
| - if filename: |
1075 |
| - result = class_(filename) |
1076 |
| - else: |
1077 |
| - result = None |
1078 |
| - |
1079 |
| - cache[texname] = result |
1080 |
| - return result |
1081 |
| - |
1082 |
| - |
1083 |
| -def _tfmfile(texname): |
1084 |
| - return _fontfile(texname, Tfm, '.tfm', _tfmcache) |
| 1072 | + return cls(filename) if filename else None |
1085 | 1073 |
|
1086 | 1074 |
|
1087 |
| -def _vffile(texname): |
1088 |
| - return _fontfile(texname, Vf, '.vf', _vfcache) |
| 1075 | +_tfmfile = partial(_fontfile, Tfm, ".tfm") |
| 1076 | +_vffile = partial(_fontfile, Vf, ".vf") |
1089 | 1077 |
|
1090 | 1078 |
|
1091 | 1079 | if __name__ == '__main__':
|
|
0 commit comments