Skip to content

Deprecate passing Type1Font effects as dict #15896

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

Closed
Show file tree
Hide file tree
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
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 @@ -59,3 +59,8 @@ Passing raw data via parameters *data* and *lut* to `.register_cmap()` is
deprecated. Instead, explicitly create a `.LinearSegmentedColormap` and pass
it via the *cmap* parameter:
``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.

``Type1Font.transform`` effect parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Passing effect parameters to `.Type1Font.transform` as dict is deprecated.
Use the respective keyword arguments instead.
62 changes: 45 additions & 17 deletions lib/matplotlib/type1font.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

>>> font = Type1Font(filename)
>>> clear_part, encrypted_part, finale = font.parts
>>> slanted_font = font.transform({'slant': 0.167})
>>> extended_font = font.transform({'extend': 1.2})
>>> slanted_font = font.transform(slant=0.167)
>>> extended_font = font.transform(extend=1.2)

Sources:

Expand All @@ -30,6 +30,8 @@

import numpy as np

from matplotlib import cbook


# token types
_TokenType = enum.Enum('_TokenType',
Expand All @@ -54,8 +56,13 @@ class Type1Font:

def __init__(self, input):
"""
Initialize a Type-1 font. *input* can be either the file name of
a pfb file or a 3-tuple of already-decoded Type-1 font parts.
Initialize a Type-1 font.

Parameters
----------
input : str or 3-tuple
Either a pfb file name, or a 3-tuple of already-decoded Type-1
font `~Type1Font.parts`.
"""
if isinstance(input, tuple) and len(input) == 3:
self.parts = input
Expand All @@ -67,9 +74,7 @@ def __init__(self, input):
self._parse()

def _read(self, file):
"""
Read the font from a file, decoding into usable parts.
"""
"""Read the font from a file, decoding into usable parts."""
rawdata = file.read()
if not rawdata.startswith(b'\x80'):
return rawdata
Expand Down Expand Up @@ -304,17 +309,40 @@ def suppress(tokens):
else:
yield value

def transform(self, effects):
def transform(self, effects=None, *, slant=0, extend=1):
"""
Transform the font by slanting or extending. *effects* should
be a dict where ``effects['slant']`` is the tangent of the
angle that the font is to be slanted to the right (so negative
values slant to the left) and ``effects['extend']`` is the
multiplier by which the font is to be extended (so values less
than 1.0 condense). Returns a new :class:`Type1Font` object.
Return a new font that is slanted or extended.

Parameters
----------
effects : dict
*Deprecated*. Use the keyword arguments *slanted* and *extended*
instead.

A dict with optional keys 'slant' and 'extend'. The effect is the
same as for the respective keyword arguments. If given, the
keyword arguments take precedence over the dict values.
slant : float, default: 0
Tangent of the angle that the font is to be slanted to the right.
Negative values slant to the left.
extend : float, default: 1
Scaling factor for the font width. Values less than 1 condense the
glyphs.

Returns
-------
font : `Type1Font`
"""
if effects is not None:
cbook.warn_deprecated(
"3.3",
message="The passing effects as a dict is deprecated. Use "
"keyword arguments slant and extend instead.")
if slant == 0 and effects is not None:
slant = effects.get('slant', slant)
if extend == 1 and effects is not None:
extend = effects.get('extend', slant)

tokenizer = self._tokens(self.parts[0])
transformed = self._transformer(tokenizer,
slant=effects.get('slant', 0.0),
extend=effects.get('extend', 1.0))
transformed = self._transformer(tokenizer, slant=slant, extend=extend)
return Type1Font((b"".join(transformed), self.parts[1], self.parts[2]))