summaryrefslogtreecommitdiffstats
path: root/share/extensions/template_seamless_pattern.py
blob: f10af39806c810d0000f79198f612a3a98cf6ef3 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# coding=utf-8

# Written by Jabiertxof
# V.06

import os

import inkex
from inkex import load_svg

class SeamlessPattern(inkex.TemplateExtension):
    """Generate a seamless pattern template"""
    multi_inx = True

    @classmethod
    def get_template(cls):
        name = "seamless_pattern.svg"
        path = os.path.dirname(os.path.realpath(__file__))
        return load_svg(os.path.join(path, name))

    def set_namedview(self, width, height, unit):
        width = self.options.width
        height = self.options.height
        factor = width / height
        clip_rect = self.svg.getElementById("clipPathRect")
        clip_rect.set("width", str(width))
        clip_rect.set("height", str(height))

        scale = inkex.Transform(scale=(width / 100, height / 100))
        self.svg.getElementById("designTop").transform = scale
        self.svg.getElementById("designBottom").transform = scale

        scale = (1, factor) if factor <= 1 else (1 / factor, 1)
        for child in self.svg.getElementById("designTop"):
            child.transform = inkex.Transform(scale=scale)

        text_preview = self.svg.getElementById('textPreview')
        if text_preview is not None:
            x = width / 100.0 / factor
            y = height / 1000.0
            if factor <= 1:
                x *= factor
                y *= factor
            text_preview.transform = inkex.Transform(translate=(int(width) * 2, 0), scale=(x, y))

        info_group = self.svg.getElementById('infoGroup')
        if info_group is not None:
            scale = 100 if factor <= 1 else 1000
            info_group.transform = inkex.Transform(scale=(width / scale, height / scale * factor))

        sides = [(x, y) for y in (-height, 0, height) for x in (-width, 0, width)]
        for i, (x, y) in enumerate(sides):
            top = self.svg.getElementById('top{i}'.format(i=i+1))
            bottom = self.svg.getElementById('bottom{i}'.format(i=i+1))
            if top is not None and bottom is not None:
                bottom.transform = top.transform = inkex.Transform(translate=(x, y))

        clones = [(x, y) for x in (0, width, width * 2) for y in (0, height, height * 2)]
        for i, (x, y) in enumerate(clones):
            preview = self.svg.getElementById("clonePreview{i}".format(i=i))
            if preview is not None:
                preview.transform = inkex.Transform(translate=(x, y))

        pattern_generator = self.svg.getElementById('fullPatternClone')
        if pattern_generator is not None:
            pattern_generator.transform = inkex.Transform(translate=(width * 2, -height))
            pattern_generator.set("inkscape:tile-cx", width / 2)
            pattern_generator.set("inkscape:tile-cy", height / 2)
            pattern_generator.set("inkscape:tile-w", width)
            pattern_generator.set("inkscape:tile-h", height)
            pattern_generator.set("inkscape:tile-x0", width)
            pattern_generator.set("inkscape:tile-y0", height)
            pattern_generator.set("width", width)
            pattern_generator.set("height", height)

        namedview = self.svg.namedview
        namedview.set('inkscape:document-units', 'px')
        namedview.set('inkscape:cx', (width * 5.5) / 2)
        namedview.set('inkscape:cy', "0")
        namedview.set('inkscape:zoom', 1 / (width / 100))

if __name__ == '__main__':
    SeamlessPattern().run()