Skip to content

Commit 2e2d2d5

Browse files
authored
Merge pull request #25769 from QuLogic/ps-level3
Set PostScript language level to 3
2 parents 266939f + dec8072 commit 2e2d2d5

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``backend_ps.psDefs``
2+
~~~~~~~~~~~~~~~~~~~~~
3+
4+
The ``psDefs`` module-level variable in ``backend_ps`` is deprecated with no
5+
replacement.

lib/matplotlib/backends/backend_ps.py

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class __getattr__:
4949
# module-level deprecations
5050
ps_backend_helper = _api.deprecated("3.7", obj_type="")(
5151
property(lambda self: PsBackendHelper()))
52+
psDefs = _api.deprecated("3.8", obj_type="")(property(lambda self: _psDefs))
5253

5354

5455
papersize = {'letter': (8.5, 11),
@@ -87,8 +88,8 @@ def _get_papertype(w, h):
8788
return 'a0'
8889

8990

90-
def _nums_to_str(*args):
91-
return " ".join(f"{arg:1.3f}".rstrip("0").rstrip(".") for arg in args)
91+
def _nums_to_str(*args, sep=" "):
92+
return sep.join(f"{arg:1.3f}".rstrip("0").rstrip(".") for arg in args)
9293

9394

9495
def _move_path_to_path_or_stream(src, dst):
@@ -292,16 +293,16 @@ def _is_transparent(self, rgb_or_rgba):
292293

293294
def set_color(self, r, g, b, store=True):
294295
if (r, g, b) != self.color:
295-
self._pswriter.write(f"{r:1.3f} setgray\n"
296+
self._pswriter.write(f"{_nums_to_str(r)} setgray\n"
296297
if r == g == b else
297-
f"{r:1.3f} {g:1.3f} {b:1.3f} setrgbcolor\n")
298+
f"{_nums_to_str(r, g, b)} setrgbcolor\n")
298299
if store:
299300
self.color = (r, g, b)
300301

301302
def set_linewidth(self, linewidth, store=True):
302303
linewidth = float(linewidth)
303304
if linewidth != self.linewidth:
304-
self._pswriter.write("%1.3f setlinewidth\n" % linewidth)
305+
self._pswriter.write(f"{_nums_to_str(linewidth)} setlinewidth\n")
305306
if store:
306307
self.linewidth = linewidth
307308

@@ -337,8 +338,7 @@ def set_linedash(self, offset, seq, store=True):
337338
if np.array_equal(seq, oldseq) and oldo == offset:
338339
return
339340

340-
self._pswriter.write(f"[{_nums_to_str(*seq)}]"
341-
f" {_nums_to_str(offset)} setdash\n"
341+
self._pswriter.write(f"[{_nums_to_str(*seq)}] {_nums_to_str(offset)} setdash\n"
342342
if seq is not None and len(seq) else
343343
"[] 0 setdash\n")
344344
if store:
@@ -406,7 +406,7 @@ def _get_clip_cmd(self, gc):
406406
clip = []
407407
rect = gc.get_clip_rectangle()
408408
if rect is not None:
409-
clip.append("%s clipbox\n" % _nums_to_str(*rect.size, *rect.p0))
409+
clip.append(f"{_nums_to_str(*rect.p0, *rect.size)} rectclip\n")
410410
path, trf = gc.get_clip_path()
411411
if path is not None:
412412
key = (path, id(trf))
@@ -473,9 +473,9 @@ def draw_markers(
473473
ps_color = (
474474
None
475475
if self._is_transparent(rgbFace)
476-
else '%1.3f setgray' % rgbFace[0]
476+
else f'{_nums_to_str(rgbFace[0])} setgray'
477477
if rgbFace[0] == rgbFace[1] == rgbFace[2]
478-
else '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3])
478+
else f'{_nums_to_str(*rgbFace[:3])} setrgbcolor')
479479

480480
# construct the generic marker command:
481481

@@ -581,7 +581,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
581581
w, h, bl = self.get_text_width_height_descent(s, prop, ismath="TeX")
582582
fontsize = prop.get_size_in_points()
583583
thetext = 'psmarker%d' % self.textcnt
584-
color = '%1.3f,%1.3f,%1.3f' % gc.get_rgb()[:3]
584+
color = _nums_to_str(*gc.get_rgb()[:3], sep=',')
585585
fontcmd = {'sans-serif': r'{\sffamily %s}',
586586
'monospace': r'{\ttfamily %s}'}.get(
587587
mpl.rcParams['font.family'][0], r'{\rmfamily %s}')
@@ -783,8 +783,8 @@ def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
783783
if hatch:
784784
hatch_name = self.create_hatch(hatch)
785785
write("gsave\n")
786-
write("%f %f %f " % gc.get_hatch_color()[:3])
787-
write("%s setpattern fill grestore\n" % hatch_name)
786+
write(_nums_to_str(*gc.get_hatch_color()[:3]))
787+
write(f" {hatch_name} setpattern fill grestore\n")
788788

789789
if stroke:
790790
write("stroke\n")
@@ -914,19 +914,20 @@ def print_figure_impl(fh):
914914
f"%%DocumentPaperSizes: {papertype}\n"
915915
f"%%Pages: 1\n",
916916
end="", file=fh)
917-
print(f"{dsc_comments}\n"
917+
print(f"%%LanguageLevel: 3\n"
918+
f"{dsc_comments}\n"
918919
f"%%Orientation: {orientation.name}\n"
919920
f"{get_bbox_header(bbox)[0]}\n"
920921
f"%%EndComments\n",
921922
end="", file=fh)
922923

923-
Ndict = len(psDefs)
924+
Ndict = len(_psDefs)
924925
print("%%BeginProlog", file=fh)
925926
if not mpl.rcParams['ps.useafm']:
926927
Ndict += len(ps_renderer._character_tracker.used)
927928
print("/mpldict %d dict def" % Ndict, file=fh)
928929
print("mpldict begin", file=fh)
929-
print("\n".join(psDefs), file=fh)
930+
print("\n".join(_psDefs), file=fh)
930931
if not mpl.rcParams['ps.useafm']:
931932
for font_path, chars \
932933
in ps_renderer._character_tracker.used.items():
@@ -951,8 +952,7 @@ def print_figure_impl(fh):
951952
print("%s translate" % _nums_to_str(xo, yo), file=fh)
952953
if rotation:
953954
print("%d rotate" % rotation, file=fh)
954-
print("%s clipbox" % _nums_to_str(width*72, height*72, 0, 0),
955-
file=fh)
955+
print(f"0 0 {_nums_to_str(width*72, height*72)} rectclip", file=fh)
956956

957957
# write the figure
958958
print(self._pswriter.getvalue(), file=fh)
@@ -1024,18 +1024,19 @@ def _print_figure_tex(
10241024
tmppath.write_text(
10251025
f"""\
10261026
%!PS-Adobe-3.0 EPSF-3.0
1027+
%%LanguageLevel: 3
10271028
{dsc_comments}
10281029
{get_bbox_header(bbox)[0]}
10291030
%%EndComments
10301031
%%BeginProlog
1031-
/mpldict {len(psDefs)} dict def
1032+
/mpldict {len(_psDefs)} dict def
10321033
mpldict begin
1033-
{"".join(psDefs)}
1034+
{"".join(_psDefs)}
10341035
end
10351036
%%EndProlog
10361037
mpldict begin
10371038
{_nums_to_str(xo, yo)} translate
1038-
{_nums_to_str(width*72, height*72)} 0 0 clipbox
1039+
0 0 {_nums_to_str(width*72, height*72)} rectclip
10391040
{self._pswriter.getvalue()}
10401041
end
10411042
showpage
@@ -1196,7 +1197,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
11961197
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
11971198
tmpfile, tmppdf], _log)
11981199
cbook._check_and_log_subprocess(
1199-
["pdftops", "-paper", "match", "-level2", tmppdf, tmpps], _log)
1200+
["pdftops", "-paper", "match", "-level3", tmppdf, tmpps], _log)
12001201
shutil.move(tmpps, tmpfile)
12011202
if eps:
12021203
pstoeps(tmpfile)
@@ -1295,7 +1296,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
12951296

12961297
# The usage comments use the notation of the operator summary
12971298
# in the PostScript Language reference manual.
1298-
psDefs = [
1299+
_psDefs = [
12991300
# name proc *_d* -
13001301
# Note that this cannot be bound to /d, because when embedding a Type3 font
13011302
# we may want to define a "d" glyph using "/d{...} d" which would locally
@@ -1313,20 +1314,6 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
13131314
"/cl { closepath } _d",
13141315
# *ce* -
13151316
"/ce { closepath eofill } _d",
1316-
# w h x y *box* -
1317-
"""/box {
1318-
m
1319-
1 index 0 r
1320-
0 exch r
1321-
neg 0 r
1322-
cl
1323-
} _d""",
1324-
# w h x y *clipbox* -
1325-
"""/clipbox {
1326-
box
1327-
clip
1328-
newpath
1329-
} _d""",
13301317
# wx wy llx lly urx ury *setcachedevice* -
13311318
"/sc { setcachedevice } _d",
13321319
]

0 commit comments

Comments
 (0)