8000 Bug fix: properly quote dangerous characters in PostScript string · matplotlib/matplotlib@f0468e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0468e4

Browse files
committed
Bug fix: properly quote dangerous characters in PostScript string
constants. svn path=/trunk/matplotlib/; revision=895
1 parent 6b82326 commit f0468e4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from matplotlib.numerix import fromstring, UInt8, Float32, equal, alltrue
2525
import binascii
26-
26+
import re
2727

2828
backend_version = 'Level II'
2929
defaultPaperSize = 8.5,11 # TODO: make this configurable
@@ -50,6 +50,14 @@ def _num_to_str(val):
5050
def _nums_to_str(*args):
5151
return ' '.join(map(_num_to_str,args))
5252

53+
def quote_ps_string(s):
54+
"Quote dangerous characters of S for use in a PostScript string constant."
55+
s=s.replace("\\", "\\\\")
56+
s=s.replace("(", "\\(")
57+
s=s.replace(")", "\\)")
58+
s=re.sub(r"[^ -~\n]", lambda x: r"\%03o"%ord(x.group()), s)
59+
return s
60+
5361

5462
_fontd = {}
5563
_type42 = []
@@ -350,7 +358,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
350358
descent = font.get_descent() / 64.0
351359
if descent:
352360
self._pswriter.write("0 %s rmoveto\n"%_num_to_str(descent))
353-
self._pswriter.write("(%s) show\n"%s)
361+
self._pswriter.write("(%s) show\n"%quote_ps_string(s))
354362
if angle:
355363
self._pswriter.write("grestore\n")
356364

0 commit comments

Comments
 (0)
0