Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Privatize some SVG internal APIs. #21412

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 2 commits into from
Oct 22, 2021
Merged
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
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/deprecations/21412-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``backend_svg.generate_transform`` and ``backend_svg.generate_css``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... are deprecated with no replacement.
84 changes: 45 additions & 39 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,26 +249,34 @@ def flush(self):
pass # replaced by the constructor


def _generate_transform(transform_list):
output = StringIO()
for type, value in transform_list:
if (type == 'scale' and (value == (1,) or value == (1, 1))
or type == 'translate' and value == (0, 0)
or type == 'rotate' and value == (0,)):
continue
if type == 'matrix' and isinstance(value, Affine2DBase):
value = value.to_values()
output.write('%s(%s)' % (
type, ' '.join(short_float_fmt(x) for x in value)))
return output.getvalue()


@_api.deprecated("3.6")
def generate_transform(transform_list=None):
if transform_list:
output = StringIO()
for type, value in transform_list:
if (type == 'scale' and (value == (1,) or value == (1, 1))
or type == 'translate' and value == (0, 0)
or type == 'rotate' and value == (0,)):
continue
if type == 'matrix' and isinstance(value, Affine2DBase):
value = value.to_values()
output.write('%s(%s)' % (
type, ' '.join(short_float_fmt(x) for x in value)))
return output.getvalue()
return ''
return _generate_transform(transform_list or [])


def generate_css(attrib={}):
def _generate_css(attrib):
return "; ".join(f"{k}: {v}" for k, v in attrib.items())


@_api.deprecated("3.6")
def generate_css(attrib=None):
return _generate_css(attrib or {})


_capstyle_d = {'projecting': 'square', 'butt': 'butt', 'round': 'round'}


Expand Down Expand Up @@ -441,7 +449,7 @@ def ensure_metadata(mid):

def _write_default_style(self):
writer = self.writer
default_style = generate_css({
default_style = _generate_css({
'stroke-linejoin': 'round',
'stroke-linecap': 'butt'})
writer.start('defs')
Expand Down Expand Up @@ -528,7 +536,7 @@ def _write_hatches(self):
writer.element(
'path',
d=path_data,
style=generate_css(hatch_style)
style=_generate_css(hatch_style)
)
writer.end('pattern')
writer.end('defs')
Expand Down Expand Up @@ -579,7 +587,7 @@ def _get_style_dict(self, gc, rgbFace):
return attrib

def _get_style(self, gc, rgbFace):
return generate_css(self._get_style_dict(gc, rgbFace))
return _generate_css(self._get_style_dict(gc, rgbFace))

def _get_clip_attrs(self, gc):
cliprect = gc.get_clip_rectangle()
Expand Down Expand Up @@ -682,9 +690,9 @@ def draw_markers(
marker_trans + Affine2D().scale(1.0, -1.0),
simplify=False)
style = self._get_style_dict(gc, rgbFace)
dictkey = (path_data, generate_css(style))
dictkey = (path_data, _generate_css(style))
oid = self._markers.get(dictkey)
style = generate_css({k: v for k, v in style.items()
style = _generate_css({k: v for k, v in style.items()
if k.startswith('stroke')})

if oid is None:
Expand Down Expand Up @@ -842,13 +850,13 @@ def draw_gouraud_triangle(self, gc, points, colors, trans):
writer.element(
'stop',
offset='1',
style=generate_css({
style=_generate_css({
'stop-color': rgb2hex(avg_color),
'stop-opacity': short_float_fmt(rgba_color[-1])}))
writer.element(
'stop',
offset='0',
style=generate_css({'stop-color': rgb2hex(rgba_color),
style=_generate_css({'stop-color': rgb2hex(rgba_color),
'stop-opacity': "0"}))

writer.end('linearGradient')
Expand Down Expand Up @@ -958,7 +966,7 @@ def draw_image(self, gc, x, y, im, transform=None):

self.writer.element(
'image',
transform=generate_transform([
transform=_generate_transform([
('scale', (1, -1)), ('translate', (0, -h))]),
x=short_float_fmt(x),
y=short_float_fmt(-(self.height - y - h)),
Expand All @@ -977,7 +985,7 @@ def draw_image(self, gc, x, y, im, transform=None):
.scale(1.0, -1.0)
.translate(0.0, self.height))

attrib['transform'] = generate_transform(
attrib['transform'] = _generate_transform(
[('matrix', flipped.frozen())])
attrib['style'] = (
'image-rendering:crisp-edges;'
Expand Down Expand Up @@ -1007,7 +1015,7 @@ def _update_glyph_map_defs(self, glyph_map_new):
Path(vertices * 64, codes), simplify=False)
writer.element(
'path', id=char_id, d=path_data,
transform=generate_transform([('scale', (1 / 64,))]))
transform=_generate_transform([('scale', (1 / 64,))]))
writer.end('defs')
self._glyph_map.update(glyph_map_new)

Expand Down Expand Up @@ -1045,8 +1053,8 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
style['opacity'] = short_float_fmt(alpha)
font_scale = fontsize / text2path.FONT_SCALE
attrib = {
'style': generate_css(style),
'transform': generate_transform([
'style': _generate_css(style),
'transform': _generate_transform([
('translate', (x, y)),
('rotate', (-angle,)),
('scale', (font_scale, -font_scale))]),
Expand Down Expand Up @@ -1082,7 +1090,7 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
char_id = self._adjust_char_id(char_id)
writer.element(
'use',
transform=generate_transform([
transform=_generate_transform([
('translate', (xposition, yposition)),
('scale', (scale,)),
]),
Expand Down Expand Up @@ -1125,7 +1133,7 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
style['font'] = ' '.join(font_parts)
if prop.get_stretch() != 'normal':
style['font-stretch'] = prop.get_stretch()
attrib['style'] = generate_css(style)
attrib['style'] = _generate_css(style)

if mtext and (angle == 0 or mtext.get_rotation_mode() == "anchor"):
# If text anchoring can be supported, get the original
Expand All @@ -1151,18 +1159,16 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):

attrib['x'] = short_float_fmt(ax)
attrib['y'] = short_float_fmt(ay)
attrib['style'] = generate_css(style)
attrib['transform'] = "rotate(%s, %s, %s)" % (
short_float_fmt(-angle),
short_float_fmt(ax),
short_float_fmt(ay))
writer.element('text', s, attrib=attrib)
attrib['style'] = _generate_css(style)
attrib['transform'] = _generate_transform([
("rotate", (-angle, ax, ay))])

else:
attrib['transform'] = generate_transform([
attrib['transform'] = _generate_transform([
('translate', (x, y)),
('rotate', (-angle,))])

writer.element('text', s, attrib=attrib)
writer.element('text', s, attrib=attrib)

else:
writer.comment(s)
Expand All @@ -1173,8 +1179,8 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
# Apply attributes to 'g', not 'text', because we likely have some
# rectangles as well with the same style and transformation.
writer.start('g',
style=generate_css(style),
transform=generate_transform([
style=_generate_css(style),
transform=_generate_transform([
('translate', (x, y)),
('rotate', (-angle,))]),
)
Expand All @@ -1199,7 +1205,7 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
style = {'font': ' '.join(font_parts)}
if entry.stretch != 'normal':
style['font-stretch'] = entry.stretch
style = generate_css(style)
style = _generate_css(style)
if thetext == 32:
thetext = 0xa0 # non-breaking space
spans.setdefault(style, []).append((new_x, -new_y, thetext))
Expand Down