From 08b64e4cbc201e3caaf783e6ae406d038e8744eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jouni=20K=2E=20Sepp=C3=A4nen?= Date: Wed, 15 Jun 2016 22:02:31 +0300 Subject: [PATCH] Implement dash pattern scaling in pdf Factor the dash pattern scaling into a separate function so GraphicsContextPdf.delta can call it. Combine the linewidth and dash comparisons because now linewidth affects dashing. Fixes #6588 --- lib/matplotlib/backend_bases.py | 21 ++++++++++++++------- lib/matplotlib/backends/backend_pdf.py | 25 ++++++++++++------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index a038860d255c..c46e9cde521f 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -858,6 +858,19 @@ def get_clip_path(self): return self._clippath.get_transformed_path_and_affine() return None, None + @staticmethod + def scale_dashes(linewidth, offset_dashes): + """ + Scale the given offset, dashlist tuple to fit linewidth. + """ + scale = max(1.0, linewidth) + offset, dashes = offset_dashes + if offset is not None: + offset = offset * scale + if dashes is not None: + dashes = [x * scale for x in dashes] + return offset, dashes + def get_dashes(self): """ Return the dash information as an offset dashlist tuple. @@ -874,13 +887,7 @@ def get_dashes(self): if rcParams['_internal.classic_mode']: return self._dashes else: - scale = max(1.0, self.get_linewidth()) - offset, dashes = self._dashes - if offset is not None: - offset = offset * scale - if dashes is not None: - dashes = [x * scale for x in dashes] - return offset, dashes + return self.scale_dashes(self.get_linewidth(), self._dashes) def get_forced_alpha(self): """ diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 21719273c423..e368c98d7241 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2206,15 +2206,17 @@ def capstyle_cmd(self, style): def joinstyle_cmd(self, style): return [self.joinstyles[style], Op.setlinejoin] - def linewidth_cmd(self, width): - return [width, Op.setlinewidth] - - def dash_cmd(self, dashes): - offset, dash = dashes - if dash is None: - dash = [] - offset = 0 - return [list(dash), offset, Op.setdash] + def linewidth_dash_cmd(self, width, dashes): + result = [] + if self.get_linewidth() != width: + result += [width, Op.setlinewidth] + if self._dashes != dashes: + offset, dash = self.scale_dashes(width, dashes) + if offset is None or dash is None: + dash = [] + offset = 0 + result += [list(dash), offset, Op.setdash] + return result def alpha_cmd(self, alpha, forced, effective_alphas): name = self.file.alphaState(effective_alphas) @@ -2288,14 +2290,11 @@ def clip_cmd(self, cliprect, clippath): (('_capstyle',), capstyle_cmd), (('_fillcolor',), fillcolor_cmd), (('_joinstyle',), joinstyle_cmd), - (('_linewidth',), linewidth_cmd), - (('_dashes',), dash_cmd), + (('_linewidth', '_dashes'), linewidth_dash_cmd), (('_rgb',), rgb_cmd), (('_hatch',), hatch_cmd), # must come after fillcolor and rgb ) - # TODO: _linestyle - def delta(self, other): """ Copy properties of other into self and return PDF commands