Skip to content
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

src/svgutils/compose.py: SVG.__init__ now accepts SVGFigure #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 30 additions & 16 deletions src/svgutils/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from svgutils import transform as _transform
from svgutils.common import Unit
from svgutils.templates import SVGFigure

CONFIG = {
"svg.file_path": ".",
Expand Down Expand Up @@ -109,24 +110,37 @@ class SVG(Element):
replace pt units with px units to fix files created with matplotlib
"""

def __init__(self, fname=None, fix_mpl=False):
def __init__(self, svg: SVGFigure, fix_mpl=False):
if fix_mpl:
w, h = svg.get_size()
svg.set_size((w.replace("pt", ""), h.replace("pt", "")))
super(SVG, self).__init__(svg.getroot().root)

# if height/width is in % units, we can't store the absolute values
if svg.width.endswith("%"):
self._width = None
else:
self._width = Unit(svg.width).to("px")
if svg.height.endswith("%"):
self._height = None
else:
self._height = Unit(svg.height).to("px")

@classmethod
def fromfile(cls, fname=None, fix_mpl=False):
if fname:
fname = os.path.join(CONFIG["svg.file_path"], fname)
svg = _transform.fromfile(fname)
if fix_mpl:
w, h = svg.get_size()
svg.set_size((w.replace("pt", ""), h.replace("pt", "")))
super(SVG, self).__init__(svg.getroot().root)

# if height/width is in % units, we can't store the absolute values
if svg.width.endswith("%"):
self._width = None
else:
self._width = Unit(svg.width).to("px")
if svg.height.endswith("%"):
self._height = None
else:
self._height = Unit(svg.height).to("px")
return SVG(svg, fix_mpl)
else:
raise TypeError('fname is None!')

@classmethod
def fromstring(cls, xml_string: str=None, fix_mpl=False):
if xml_string:
svg = _transform.fromstring(xml_string)
return SVG(svg, fix_mpl)
else:
raise TypeError('xml_string is None!')

@property
def width(self):
Expand Down