8000 PDF speed optimization. · matplotlib/matplotlib@ac2da58 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac2da58

Browse files
committed
PDF speed optimization.
svn path=/trunk/matplotlib/; revision=6349
1 parent 73decf0 commit ac2da58

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def fill(strings, linelen=75):
112112
result.append(' '.join(strings[lasti:]))
113113
return '\n'.join(result)
114114

115-
115+
_string_escape_regex = re.compile(r'([\\()])')
116116
def pdfRepr(obj):
117117
"""Map Python objects to PDF syntax."""
118118

@@ -138,7 +138,7 @@ def pdfRepr(obj):
138138
# simpler to escape them all. TODO: cut long strings into lines;
139139
# I believe there is some maximum line length in PDF.
140140
elif is_string_like(obj):
141-
return '(' + re.sub(r'([\\()])', r'\\\1', obj) + ')'
141+
return '(' + _string_escape_regex.sub(r'\\\1', obj) + ')'
142142

143143
# Dictionaries. The keys must be PDF names, so if we find strings
144144
# there, we make Name objects from them. The values may be
@@ -207,12 +207,13 @@ def write(self, contents, file):
207207

208208
class Name:
209209
"""PDF name object."""
210+
_regex = re.compile(r'[^!-~]')
210211

211212
def __init__(self, name):
212213
if isinstance(name, Name):
213214
self.name = name.name
214215
else:
215-
self.name = re.sub(r'[^!-~]', Name.hexify, name)
216+
self.name = self._regex.sub(Name.hexify, name)
216217

217218
def __repr__(self):
218219
return "<Name %s>" % self.name

0 commit comments

Comments
 (0)
0