~inkscape.dev/inkscape/trunk

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python

# Written by Jabiertxof
# V.04

import inkex
import re
import os
from lxml import etree

class C(inkex.Effect):
  def __init__(self):
    inkex.Effect.__init__(self)
    self.OptionParser.add_option("-w", "--width",  action="store", type="int",    dest="desktop_width",  default="100", help="Custom width")
    self.OptionParser.add_option("-z", "--height", action="store", type="int",    dest="desktop_height", default="100", help="Custom height")

  def effect(self):

    width  = self.options.desktop_width
    height = self.options.desktop_height
    path = os.path.dirname(os.path.realpath(__file__))
    self.document = etree.parse(os.path.join(path, "seamless_pattern.svg"))
    root = self.document.getroot()
    root.set("id", "SVGRoot")
    root.set("width",  str(width) + 'px')
    root.set("height", str(height) + 'px')
    root.set("viewBox", "0 0 " + str(width) + " " + str(height) )

    xpathStr = '//svg:rect[@id="clipPathRect"]'
    clipPathRect = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clipPathRect != []:
        clipPathRect[0].set("width", str(width))
        clipPathRect[0].set("height", str(height))

    xpathStr = '//svg:g[@id="designTop"] | //svg:g[@id="designBottom"]'
    designZone = root.xpath(xpathStr, namespaces=inkex.NSS)
    if designZone != []:
        designZone[0].set("transform", "scale(" + str(width/100.) + ","+ str(height/100.) + ")")
        designZone[1].set("transform", "scale(" + str(width/100.) + ","+ str(height/100.) + ")")

    xpathStr = '//svg:use[@id="top1"] | //svg:use[@id="bottom1"]'
    pattern1 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern1 != []:
        pattern1[0].set("transform", "translate(" + str(-width) + "," + str(-height) + ")")
        pattern1[1].set("transform", "translate(" + str(-width) + "," + str(-height) + ")")

    xpathStr = '//svg:use[@id="top2"] | //svg:use[@id="bottom2"]'
    pattern2 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern2 != []:
        pattern2[0].set("transform", "translate(0," + str(-height) +")" )
        pattern2[1].set("transform", "translate(0," + str(-height) +")" )

    xpathStr = '//svg:use[@id="top3"] | //svg:use[@id="bottom3"]'
    pattern3 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern3 != []:
        pattern3[0].set("transform", "translate(" + str(width) + "," + str(-height) + ")" )
        pattern3[1].set("transform", "translate(" + str(width) + "," + str(-height) + ")" )

    xpathStr = '//svg:use[@id="top4"] | //svg:use[@id="bottom4"]'
    pattern4 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern4 != []:
        pattern4[0].set("transform", "translate(" + str(-width) + ",0)" )
        pattern4[1].set("transform", "translate(" + str(-width) + ",0)" )

    xpathStr = '//svg:use[@id="top5"] | //svg:use[@id="bottom5"]'
    pattern5 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern5 != []:
        pattern5[0].set("transform", "translate(0,0)" )
        pattern5[1].set("transform", "translate(0,0)" )

    xpathStr = '//svg:use[@id="top6"] | //svg:use[@id="bottom6"]'
    pattern6 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern6 != []:
        pattern6[0].set("transform", "translate(" + str(width) + ",0)" )
        pattern6[1].set("transform", "translate(" + str(width) + ",0)" )

    xpathStr = '//svg:use[@id="top7"] | //svg:use[@id="bottom7"]'
    pattern7 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern7 != []:
        pattern7[0].set("transform", "translate(" + str(-width) + "," + str(height) + ")" )
        pattern7[1].set("transform", "translate(" + str(-width) + "," + str(height) + ")" )

    xpathStr = '//svg:use[@id="top8"] | //svg:use[@id="bottom8"]'
    pattern8 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern8 != []:
        pattern8[0].set("transform", "translate(0," + str(height) + ")" )
        pattern8[1].set("transform", "translate(0," + str(height) + ")" )

    xpathStr = '//svg:use[@id="top9"] | //svg:use[@id="bottom9"]'
    pattern9 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if pattern9 != []:
        pattern9[0].set("transform", "translate(" + str(width) + "," + str(height) + ")" )
        pattern9[1].set("transform", "translate(" + str(width) + "," + str(height) + ")" )

    xpathStr = '//svg:use[@id="clonePreview1"]'
    clonePreview1 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview1 != []:
        clonePreview1[0].set("transform", "translate(0," + str(height) + ")" )

    xpathStr = '//svg:use[@id="clonePreview2"]'
    clonePreview2 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview2 != []:
        clonePreview2[0].set("transform", "translate(0," + str(height * 2) + ")" )

    xpathStr = '//svg:use[@id="clonePreview3"]'
    clonePreview3 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview3 != []:
        clonePreview3[0].set("transform", "translate(" + str(width) + ",0)" )

    xpathStr = '//svg:use[@id="clonePreview4"]'
    clonePreview4 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview4 != []:
        clonePreview4[0].set("transform", "translate(" + str(width) + "," + str(height) + ")" )

    xpathStr = '//svg:use[@id="clonePreview5"]'
    clonePreview5 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview5 != []:
        clonePreview5[0].set("transform", "translate(" + str(width) + "," + str(height * 2) + ")" )

    xpathStr = '//svg:use[@id="clonePreview6"]'
    clonePreview6 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview6 != []:
        clonePreview6[0].set("transform", "translate(" + str(width*2) + ", 0)" )

    xpathStr = '//svg:use[@id="clonePreview7"]'
    clonePreview7 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview7 != []:
        clonePreview7[0].set("transform", "translate(" + str(width*2) + "," + str(height) + ")" )

    xpathStr = '//svg:use[@id="clonePreview8"]'
    clonePreview8 = root.xpath(xpathStr, namespaces=inkex.NSS)
    if clonePreview8 != []:
        clonePreview8[0].set("transform", "translate(" + str(width*2) + "," + str(height*2) + ")" )

    xpathStr = '//svg:use[@id="fullPatternClone"]'
    patternGenerator = root.xpath(xpathStr, namespaces=inkex.NSS)
    if patternGenerator != []:
        patternGenerator[0].set("transform", "translate(" + str(width * 2) + ",-" + str(height) + ")" )
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-cx", str(width/2))
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-cy", str(height/2))
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-w", str(width))
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-h", str(height))
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-x0", str(width))
        patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-y0", str(height))
        patternGenerator[0].set("width", str(width))
        patternGenerator[0].set("height", str(height))

    namedview = root.find(inkex.addNS('namedview', 'sodipodi'))
    if namedview is None:
        namedview = inkex.etree.SubElement( root, inkex.addNS('namedview', 'sodipodi') );
     
    namedview.set(inkex.addNS('document-units', 'inkscape'), 'px')

    namedview.set(inkex.addNS('cx',        'inkscape'), str((width*5.5)/2.0) )
    namedview.set(inkex.addNS('cy',        'inkscape'), "0" )
    namedview.set(inkex.addNS('zoom',        'inkscape'), str(1.0 / (width/100.0)) )

c = C()
c.affect()