Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_low_level_api():
"""Test the low-level Python API with various parameters."""
_png_filename, svg_filename = FILES[0]
expected_content = cairosvg.svg2png(url=svg_filename)
# Same as above, longer version
tree = cairosvg.parser.Tree(url=svg_filename)
file_like = io.BytesIO()
surface = cairosvg.surface.PNGSurface(tree, file_like, 96)
surface.finish()
assert file_like.getvalue() == expected_content
png_result = png.Reader(bytes=expected_content).read()
expected_width, expected_height, _, _ = png_result
# Abstract surface
surface = cairosvg.surface.PNGSurface(tree, None, 96)
assert surface.width == expected_width
assert surface.height == expected_height
assert cairo.SurfacePattern(surface.cairo).get_surface() is surface.cairo
assert_raises(TypeError, cairo.SurfacePattern, 'Not a cairo.Surface.')
def use(surface, node):
"""Draw the content of another SVG node."""
surface.context.save()
surface.context.translate(
size(surface, node.get('x'), 'x'), size(surface, node.get('y'), 'y'))
if 'x' in node:
del node['x']
if 'y' in node:
del node['y']
if 'viewBox' in node:
del node['viewBox']
if 'mask' in node:
del node['mask']
href = parse_url(node.get_href()).geturl()
tree = Tree(
url=href, url_fetcher=node.url_fetcher, parent=node,
tree_cache=surface.tree_cache, unsafe=node.unsafe)
if not match_features(tree.xml_tree):
surface.context.restore()
return
if tree.tag in ('svg', 'symbol'):
# Explicitely specified
# http://www.w3.org/TR/SVG11/struct.html#UseElement
tree.tag = 'svg'
if 'width' in node and 'height' in node:
tree['width'], tree['height'] = node['width'], node['height']
surface.draw(tree)
node.get('fill', None)
if len(image_bytes) < 5:
return
x, y = size(surface, node.get('x'), 'x'), size(surface, node.get('y'), 'y')
width = size(surface, node.get('width'), 'x')
height = size(surface, node.get('height'), 'y')
if image_bytes[:4] == b'\x89PNG' and not surface.map_image:
png_file = BytesIO(image_bytes)
elif (image_bytes[:5] in (b'
def draw(self, context, concrete_width, concrete_height, _image_rendering):
try:
svg = ScaledSVGSurface(
cairosvg.parser.Tree(
bytestring=self._svg_data, url=self._base_url,
url_fetcher=self._cairosvg_url_fetcher),
output=None, dpi=96, output_width=concrete_width,
output_height=concrete_height)
if svg.width and svg.height:
context.scale(
concrete_width / svg.width, concrete_height / svg.height)
context.set_source_surface(svg.cairo)
context.paint()
except Exception as e:
LOGGER.error(
'Failed to draw an SVG image at %s : %s', self._base_url, e)
self.text = handle_white_spaces(element.etree_element.text, preserve)
if trailing_space and not preserve:
self.text = self.text.lstrip(' ')
original_rotate = rotations(self)
rotate = list(original_rotate)
if original_rotate:
pop_rotation(self, original_rotate, rotate)
if self.text:
trailing_space = self.text.endswith(' ')
for child_element in element.iter_children():
child = child_element.etree_element
if child.tag in ('{http://www.w3.org/2000/svg}tref', 'tref'):
href = child.get(
'{http://www.w3.org/1999/xlink}href', child.get('href'))
url = parse_url(href).geturl()
child_tree = Tree(
url=url, url_fetcher=self.url_fetcher, parent=self,
unsafe=self.unsafe)
child_tree.clear()
child_tree.update(self)
child_node = Node(
child_element, self.style, self.url_fetcher,
parent=child_tree, parent_children=True,
unsafe=self.unsafe)
child_node.tag = 'tspan'
# Retrieve the referenced node and get its flattened text
# and remove the node children.
child = child_tree.xml_tree
child.text = flatten(child)
child_element = cssselect2.ElementWrapper.from_xml_root(child)
else:
child_node = Node(
def use(self, node):
"""Draw the content of another SVG file."""
self.context.save()
self.context.translate(size(node.get("x")), size(node.get("y")))
if "x" in node:
del node["x"]
if "y" in node:
del node["y"]
if "viewBox" in node:
del node["viewBox"]
href = node.get("{http://www.w3.org/1999/xlink}href")
tree = Tree(href, node)
self._set_context_size(*node_format(tree))
self.draw(tree)
self.context.restore()
# Restore twice, because draw does not restore at the end of svg tags
self.context.restore()
if len(image_bytes) < 5:
return
x, y = size(surface, node.get('x'), 'x'), size(surface, node.get('y'), 'y')
width = size(surface, node.get('width'), 'x')
height = size(surface, node.get('height'), 'y')
if image_bytes[:4] == b'\x89PNG':
png_file = BytesIO(image_bytes)
elif (image_bytes[:5] in (b'
def update_def_href(surface, def_name, def_dict):
"""Update the attributes of the def according to its href attribute."""
def_node = def_dict[def_name]
href = parse_url(def_node.get_href()).fragment
if href in def_dict:
update_def_href(surface, href, def_dict)
href_node = def_dict[href]
def_dict[def_name] = Tree(
url='#{}'.format(def_name), url_fetcher=def_node.url_fetcher,
parent=href_node, parent_children=(not def_node.children),
tree_cache=surface.tree_cache, unsafe=def_node.unsafe)
# Inherit attributes generally not inherited
for key, value in href_node.items():
if key not in def_dict[def_name]:
def_dict[def_name][key] = value