Skip to content

Commit

Permalink
Merge pull request matplotlib#16683 from anntzer/gluespec
Browse files Browse the repository at this point in the history
API: Turn mathtext.GlueSpec into a (private) namedtuple.
  • Loading branch information
tacaswell authored Mar 6, 2020
2 parents 943444a + 1e7db60 commit dbc35a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,8 @@ The following related APIs are also deprecated:
``matplotlib.test(recursionlimit=...)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The *recursionlimit* parameter of ``matplotlib.test`` is deprecated.

mathtext glues
~~~~~~~~~~~~~~
The *copy* parameter of ``mathtext.Glue`` is deprecated (the underlying glue
spec is now immutable). ``mathtext.GlueSpec`` is deprecated.
55 changes: 32 additions & 23 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,41 +1849,57 @@ def __init__(self, state):
Rule.__init__(self, thickness, np.inf, np.inf, state)


_GlueSpec = namedtuple(
"_GlueSpec", "width stretch stretch_order shrink shrink_order")
_GlueSpec._named = {
'fil': _GlueSpec(0., 1., 1, 0., 0),
'fill': _GlueSpec(0., 1., 2, 0., 0),
'filll': _GlueSpec(0., 1., 3, 0., 0),
'neg_fil': _GlueSpec(0., 0., 0, 1., 1),
'neg_fill': _GlueSpec(0., 0., 0, 1., 2),
'neg_filll': _GlueSpec(0., 0., 0, 1., 3),
'empty': _GlueSpec(0., 0., 0, 0., 0),
'ss': _GlueSpec(0., 1., 1, -1., 1),
}


class Glue(Node):
"""
Most of the information in this object is stored in the underlying
`GlueSpec` class, which is shared between multiple glue objects.
``_GlueSpec`` class, which is shared between multiple glue objects.
(This is a memory optimization which probably doesn't matter anymore, but
it's easier to stick to what TeX does.)
"""

@cbook.deprecated("3.3")
@property
def glue_subtype(self):
return "normal"

@cbook._delete_parameter("3.3", "copy")
def __init__(self, glue_type, copy=False):
Node.__init__(self)
self.glue_subtype = 'normal'
if isinstance(glue_type, str):
glue_spec = GlueSpec.factory(glue_type)
elif isinstance(glue_type, GlueSpec):
glue_spec = _GlueSpec._named[glue_type]
elif isinstance(glue_type, _GlueSpec):
glue_spec = glue_type
else:
raise ValueError("glue_type must be a glue spec name or instance")
if copy:
glue_spec = glue_spec.copy()
self.glue_spec = glue_spec
self.glue_spec = glue_spec

def shrink(self):
Node.shrink(self)
if self.size < NUM_SIZE_LEVELS:
if self.glue_spec.width != 0.:
self.glue_spec = self.glue_spec.copy()
self.glue_spec.width *= SHRINK_FACTOR
g = self.glue_spec
self.glue_spec = g._replace(width=g.width * SHRINK_FACTOR)

def grow(self):
Node.grow(self)
if self.glue_spec.width != 0.:
self.glue_spec = self.glue_spec.copy()
self.glue_spec.width *= GROW_FACTOR
g = self.glue_spec
self.glue_spec = g._replace(width=g.width * GROW_FACTOR)


@cbook.deprecated("3.3")
class GlueSpec:
"""See `Glue`."""

Expand All @@ -1908,16 +1924,9 @@ def factory(cls, glue_type):
return cls._types[glue_type]


GlueSpec._types = {
'fil': GlueSpec(0., 1., 1, 0., 0),
'fill': GlueSpec(0., 1., 2, 0., 0),
'filll': GlueSpec(0., 1., 3, 0., 0),
'neg_fil': GlueSpec(0., 0., 0, 1., 1),
'neg_fill': GlueSpec(0., 0., 0, 1., 2),
'neg_filll': GlueSpec(0., 0., 0, 1., 3),
'empty': GlueSpec(0., 0., 0, 0., 0),
'ss': GlueSpec(0., 1., 1, -1., 1)
}
with cbook._suppress_matplotlib_deprecation_warning():
GlueSpec._types = {k: GlueSpec(**v._asdict())
for k, v in _GlueSpec._named.items()}


# Some convenient ways to get common kinds of glue
Expand Down

0 comments on commit dbc35a9

Please sign in to comment.