summaryrefslogtreecommitdiffstats
path: root/share/extensions/inkex/elements/_use.py
blob: b5a38c563753348b85d41e30f8a198946eac249c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020 Martin Owens <doctormo@gmail.com>
#                    Thomas Holder <thomas.holder@schrodinger.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Interface for the Use and Symbol elements
"""

from ..transforms import Transform

from ._groups import Group, GroupBase
from ._base import ShapeElement

class Symbol(GroupBase):
    """SVG symbol element"""
    tag_name = 'symbol'

class Use(ShapeElement):
    """A 'use' element that links to another in the document"""
    tag_name = 'use'

    @classmethod
    def new(cls, elem, x, y, **attrs): # pylint: disable=arguments-differ
        ret = super(Use, cls).new(x=x, y=y, **attrs)
        ret.href = elem
        return ret

    def get_path(self):
        """Returns the path of the cloned href plus any transformation"""
        path = self.href.path
        path.transform(self.href.transform)
        return path

    def effective_style(self):
        """Href's style plus this object's own styles"""
        style = self.href.effective_style()
        style.update(self.style)
        return style

    def unlink(self):
        """Unlink this clone, replacing it with a copy of the original"""
        copy = self.href.copy()
        if isinstance(copy, Symbol):
            group = Group(**copy.attrib)
            group.extend(copy)
            copy = group
        copy.transform *= self.transform
        copy.style = self.style + copy.style
        self.replace_with(copy)
        copy.set_random_ids()
        return copy

    def shape_box(self, transform=None):
        effective_transform = Transform(transform) * self.transform
        return self.href.bounding_box(effective_transform)