How to use the cairosvg.parser.Node function in CairoSVG

To help you get started, we’ve selected a few CairoSVG examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Kozea / CairoSVG / cairosvg / parser.py View on Github external
child_element = cssselect2.ElementWrapper.from_xml_root(child)
            else:
                child_node = Node(
                    child_element, self.style, self.url_fetcher, parent=self,
                    unsafe=self.unsafe)
            child_preserve = child_node.get(space) == 'preserve'
            child_node.text = handle_white_spaces(child.text, child_preserve)
            child_node.children, trailing_space = child_node.text_children(
                child_element, trailing_space)
            trailing_space = child_node.text.endswith(' ')
            if original_rotate and 'rotate' not in child_node:
                pop_rotation(child_node, original_rotate, rotate)
            children.append(child_node)
            if child.tail:
                anonymous_etree = Element('{http://www.w3.org/2000/svg}tspan')
                anonymous = Node(
                    cssselect2.ElementWrapper.from_xml_root(anonymous_etree),
                    self.style, self.url_fetcher, parent=self,
                    unsafe=self.unsafe)
                anonymous.text = handle_white_spaces(child.tail, preserve)
                if original_rotate:
                    pop_rotation(anonymous, original_rotate, rotate)
                if trailing_space and not preserve:
                    anonymous.text = anonymous.text.lstrip(' ')
                if anonymous.text:
                    trailing_space = anonymous.text.endswith(' ')
                children.append(anonymous)

        if text_root and not children and not preserve:
            self.text = self.text.rstrip(' ')

        return children, trailing_space
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
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(
                    child_element, self.style, self.url_fetcher, parent=self,
                    unsafe=self.unsafe)
            child_preserve = child_node.get(space) == 'preserve'
            child_node.text = handle_white_spaces(child.text, child_preserve)
            child_node.children, trailing_space = child_node.text_children(
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
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(
                    child_element, self.style, self.url_fetcher, parent=self,
                    unsafe=self.unsafe)
            child_preserve = child_node.get(space) == 'preserve'
            child_node.text = handle_white_spaces(child.text, child_preserve)
            child_node.children, trailing_space = child_node.text_children(
                child_element, trailing_space)
            trailing_space = child_node.text.endswith(' ')
            if original_rotate and 'rotate' not in child_node:
                pop_rotation(child_node, original_rotate, rotate)
            children.append(child_node)
            if child.tail:
                anonymous_etree = Element('{http://www.w3.org/2000/svg}tspan')
                anonymous = Node(
                    cssselect2.ElementWrapper.from_xml_root(anonymous_etree),
                    self.style, self.url_fetcher, parent=self,
                    unsafe=self.unsafe)
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
# Manage text by creating children
        if self.tag in ('text', 'textPath', 'a'):
            self.children, _ = self.text_children(
                element, trailing_space=True, text_root=True)

        if parent_children:
            self.children = [
                Node(child.element, style, self.url_fetcher, parent=self,
                     unsafe=self.unsafe)
                for child in parent.children]
        elif not self.children:
            self.children = []
            for child in element.iter_children():
                if match_features(child.etree_element):
                    self.children.append(
                        Node(child, style, self.url_fetcher, parent=self,
                             unsafe=self.unsafe))
                    if self.tag == 'switch':
                        break
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
def __new__(cls, **kwargs):
        tree_cache = kwargs.get('tree_cache')
        if tree_cache and kwargs.get('url'):
            parsed_url = parse_url(kwargs['url'])
            element_id = parsed_url.fragment
            parent = kwargs.get('parent')
            unsafe = kwargs.get('unsafe')
            if any(parsed_url[:-1]):
                url = urlunparse(parsed_url[:-1] + ('',))
            elif parent:
                url = parent.url
            else:
                url = None
            if url and (url, element_id) in tree_cache:
                cached_tree = tree_cache[(url, element_id)]
                new_tree = Node(
                    cached_tree.element, cached_tree.style,
                    cached_tree.url_fetcher, parent, unsafe=unsafe)
                new_tree.xml_tree = cached_tree.xml_tree
                new_tree.url = url
                new_tree.tag = cached_tree.tag
                new_tree.root = True
                return new_tree
        return super().__new__(cls)
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
for attribute in [
                attribute for attribute in self
                if self[attribute] == 'inherit']:
            if parent is not None and attribute in parent:
                self[attribute] = parent.get(attribute)
            else:
                del self[attribute]

        # Manage text by creating children
        if self.tag in ('text', 'textPath', 'a'):
            self.children, _ = self.text_children(
                element, trailing_space=True, text_root=True)

        if parent_children:
            self.children = [
                Node(child.element, style, self.url_fetcher, parent=self,
                     unsafe=self.unsafe)
                for child in parent.children]
        elif not self.children:
            self.children = []
            for child in element.iter_children():
                if match_features(child.etree_element):
                    self.children.append(
                        Node(child, style, self.url_fetcher, parent=self,
                             unsafe=self.unsafe))
                    if self.tag == 'switch':
                        break
github Kozea / CairoSVG / cairosvg / parser.py View on Github external
if trailing_space and not preserve:
                    anonymous.text = anonymous.text.lstrip(' ')
                if anonymous.text:
                    trailing_space = anonymous.text.endswith(' ')
                children.append(anonymous)

        if text_root and not children and not preserve:
            self.text = self.text.rstrip(' ')

        return children, trailing_space

    def get_href(self):
        return self.get('{http://www.w3.org/1999/xlink}href', self.get('href'))


class Tree(Node):
    """SVG tree."""
    def __new__(cls, **kwargs):
        tree_cache = kwargs.get('tree_cache')
        if tree_cache and kwargs.get('url'):
            parsed_url = parse_url(kwargs['url'])
            element_id = parsed_url.fragment
            parent = kwargs.get('parent')
            unsafe = kwargs.get('unsafe')
            if any(parsed_url[:-1]):
                url = urlunparse(parsed_url[:-1] + ('',))
            elif parent:
                url = parent.url
            else:
                url = None
            if url and (url, element_id) in tree_cache:
                cached_tree = tree_cache[(url, element_id)]