Skip to content

Small cleanups to backend_ps. #20445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import codecs
import datetime
from enum import Enum
import functools
import glob
from io import StringIO
import logging
Expand Down Expand Up @@ -39,8 +40,7 @@
_log = logging.getLogger(__name__)

backend_version = 'Level II'

debugPS = 0
debugPS = False


class PsBackendHelper:
Expand Down Expand Up @@ -214,6 +214,20 @@ def _font_to_ps_type3(font_path, glyph_ids):
return preamble + "\n".join(entries) + postamble


def _log_if_debug_on(meth):
"""
Wrap `RendererPS` method *meth* to emit a PS comment with the method name,
if the global flag `debugPS` is set.
"""
@functools.wraps(meth)
def wrapper(self, *args, **kwargs):
if debugPS:
self._pswriter.write(f"% {meth.__name__}\n")
return meth(self, *args, **kwargs)

return wrapper


class RendererPS(_backend_pdf_ps.RendererPDFPSBase):
"""
The renderer handles all the drawing primitives using a graphics
Expand Down Expand Up @@ -254,11 +268,9 @@ def __init__(self, width, height, pswriter, imagedpi=72):

def set_color(self, r, g, b, store=True):
if (r, g, b) != self.color:
if r == g and r == b:
self._pswriter.write("%1.3f setgray\n" % r)
else:
self._pswriter.write(
"%1.3f %1.3f %1.3f setrgbcolor\n" % (r, g, b))
self._pswriter.write(f"{r:1.3f} setgray\n"
if r == g == b else
f"{r:1.3f} {g:1.3f} {b:1.3f} setrgbcolor\n")
if store:
self.color = (r, g, b)

Expand Down Expand Up @@ -301,11 +313,9 @@ def set_linedash(self, offset, seq, store=True):
if np.array_equal(seq, oldseq) and oldo == offset:
return

if seq is not None and len(seq):
s = "[%s] %d setdash\n" % (_nums_to_str(*seq), offset)
self._pswriter.write(s)
else:
self._pswriter.write("[] 0 setdash\n")
self._pswriter.write(f"[{_nums_to_str(*seq)}] {offset:d} setdash\n"
if seq is not None and len(seq) else
"[] 0 setdash\n")
if store:
self.linedash = (offset, seq)

Expand Down Expand Up @@ -389,6 +399,7 @@ def _get_clip_cmd(self, gc):
clip.append(f"{custom_clip_cmd}\n")
return "".join(clip)

@_log_if_debug_on
def draw_image(self, gc, x, y, im, transform=None):
# docstring inherited

Expand Down Expand Up @@ -431,20 +442,19 @@ def draw_image(self, gc, x, y, im, transform=None):
grestore
""")

@_log_if_debug_on
def draw_path(self, gc, path, transform, rgbFace=None):
# docstring inherited
clip = rgbFace is None and gc.get_hatch_path() is None
simplify = path.should_simplify and clip
ps = self._convert_path(path, transform, clip=clip, simplify=simplify)
self._draw_ps(ps, gc, rgbFace)

@_log_if_debug_on
def draw_markers(
self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
# docstring inherited

if debugPS:
self._pswriter.write('% draw_markers \n')

ps_color = (
None
if _is_transparent(rgbFace)
Expand Down Expand Up @@ -493,6 +503,7 @@ def draw_markers(
ps = '\n'.join(ps_cmd)
self._draw_ps(ps, gc, rgbFace, fill=False, stroke=False)

@_log_if_debug_on
def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
Expand Down Expand Up @@ -537,6 +548,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,

self._path_collection_id += 1

@_log_if_debug_on
def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
# docstring inherited
if not hasattr(self, "psfrag"):
Expand Down Expand Up @@ -573,12 +585,10 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
""")
self.textcnt += 1

@_log_if_debug_on
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
# docstring inherited

if debugPS:
self._pswriter.write("% text\n")

if _is_transparent(gc.get_rgb()):
return # Special handling for fully transparent.

Expand Down Expand Up @@ -630,11 +640,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
grestore
""")

@_log_if_debug_on
def draw_mathtext(self, gc, x, y, s, prop, angle):
"""Draw the math text using matplotlib.mathtext."""
if debugPS:
self._pswriter.write("% mathtext\n")

width, height, descent, glyphs, rects = \
self._text2path.mathtext_parser.parse(
s, 72, prop,
Expand All @@ -661,10 +669,12 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
self._pswriter.write(f"{ox} {oy} {w} {h} rectfill\n")
self._pswriter.write("grestore\n")

@_log_if_debug_on
def draw_gouraud_triangle(self, gc, points, colors, trans):
self.draw_gouraud_triangles(gc, points.reshape((1, 3, 2)),
colors.reshape((1, 3, 4)), trans)

@_log_if_debug_on
def draw_gouraud_triangles(self, gc, points, colors, trans):
assert len(points) == len(colors)
assert points.ndim == 3
Expand Down Expand Up @@ -708,20 +718,16 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
grestore
""")

def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
def _draw_ps(self, ps, gc, rgbFace, *, fill=True, stroke=True):
"""
Emit the PostScript snippet 'ps' with all the attributes from 'gc'
applied. 'ps' must consist of PostScript commands to construct a path.
Emit the PostScript snippet *ps* with all the attributes from *gc*
applied. *ps* must consist of PostScript commands to construct a path.

The fill and/or stroke kwargs can be set to False if the
'ps' string already includes filling and/or stroking, in
which case _draw_ps is just supplying properties and
clipping.
The *fill* and/or *stroke* kwargs can be set to False if the *ps*
string already includes filling and/or stroking, in which case
`_draw_ps` is just supplying properties and clipping.
"""
# local variable eliminates all repeated attribute lookups
write = self._pswriter.write
if debugPS and command:
write("% "+command+"\n")
mightstroke = (gc.get_linewidth() > 0
and not _is_transparent(gc.get_rgb()))
if not mightstroke:
Expand All @@ -740,7 +746,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):

write(self._get_clip_cmd(gc))

# Jochen, is the strip necessary? - this could be a honking big string
write(ps.strip())
write("\n")

Expand Down