1 #####################################################################
2 # Python test script for Inkscape DBus API.
3 #
4 # Contains many examples of functions and various stress tests etc.
5 # Multiple tests can be run at once but the output will be a bit chaotic.
6 # List of test functions can be found at the bottom of the script.
7 #####################################################################
9 import dbus
10 import random
12 #####################################################################
13 # Various test functions, called at bottom of script
14 #####################################################################
16 def randomrect (document):
17 document.rectangle( random.randint(0,1000),
18 random.randint(0,1000),
19 random.randint(1,100),
20 random.randint(1,100))
22 def lottarects ( document ):
23 document.pause_updates()
24 listy = []
25 for x in range(1,2000):
26 if x == 1000:
27 print "HALFWAY"
28 if x == 1:
29 print "BEGUN"
30 document.rectangle( 0, 0, 100, 100)
31 #randomrect(document)
32 print "DONE"
33 for x in listy:
34 print x
35 selection_set(x)
36 document.resume_updates()
38 def lottaverbs (doc):
39 doc.pause_updates()
40 doc.document_set_css ("fill:#ff0000;fill-opacity:.5;stroke:#0000ff;stroke-width:5;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none")
41 doc.rectangle( 0, 0, 100, 100)
42 doc.select_all()
43 doc.selection_copy()
44 for x in range(1,2000):
45 if x == 1000:
46 print "HALFWAY"
47 if x == 1:
48 print "BEGUN"
49 doc.selection_paste()
50 #doc.rectangle( 0, 0, 100, 100)
51 doc.resume_updates()
53 def testDrawing (doc):
54 doc.document_set_css ("fill:#000000;fill-opacity:.5;stroke:#000000;stroke-width:5;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none")
55 doc.ellipse( 0, 0, 100, 100)
56 doc.select_all()
57 doc.selection_copy()
58 for x in range(1,2000):
59 if x == 1000:
60 print "HALFWAY"
61 if x == 1:
62 print "BEGUN"
63 doc.selection_paste()
64 newrect = doc.selection_get()[0]
65 doc.set_color(newrect, 255 - x%255, 0, 200, True)
66 doc.set_color(newrect, 0, 255 - x%75, x%75, False)
67 doc.mark_as_unmodified()
70 def testcopypaste (document ):
71 #document.pause_updates()
72 print document.rectangle (400, 500, 100, 100)
73 print document.rectangle (200, 200, 100, 100)
74 document.select_all()
75 document.selection_copy()
76 document.selection_paste()
77 #document.resume_updates()
79 def testShapes (doc):
80 doc.rectangle (0, 0, 100, 100)
81 doc.ellipse (100, 100, 100, 100)
82 doc.star (250, 250, 50, 25, 5, False, .9, 1.4)
83 doc.polygon (350, 350, 50, 0, 5)
84 doc.line (400,400,500,500)
85 doc.spiral (550,550,50,3)
87 def testMovement (doc):
88 rect1 = doc.rectangle (0, 0, 100, 100)
89 rect2 = doc.rectangle (0, 100, 100, 100)
90 rect3 = doc.rectangle (0, 200, 100, 100)
91 doc.select_all()
92 doc.move(rect2, 100,100)
94 def testImport (doc):
95 # CHANGE TO LOCAL SVG FILE!
96 img1 = doc.image(0,0, "/home/soren/chavatar.jpg")
97 doc.selection_add(img1)
98 doc.selection_scale(500)
99 doc.transform(img1, "rotate(30)")
101 def testSelections (doc):
102 rect1 = doc.rectangle (0, 0, 100, 100)
103 rect2 = doc.rectangle (0, 100, 100, 100)
104 rect3 = doc.rectangle (0, 200, 100, 100)
105 rect4 = doc.rectangle (0, 300, 100, 100)
107 doc.selection_add (rect1)
108 center = doc.selection_get_center()
109 for d in center:
110 print d
111 doc.selection_to_path()
112 doc.get_path(rect1)
113 doc.selection_move(100.0, 100.0)
114 doc.selection_set(rect2)
115 doc.selection_move_to(0.0,0.0)
116 doc.selection_set(rect3)
117 doc.move(rect4, 500.0, 500.0)
118 doc.select_all()
119 doc.selection_to_path()
120 result = doc.selection_get()
121 print len(result)
122 for d in result:
123 print d
125 def testLevels (doc):
126 rect1 = doc.rectangle (0, 0, 100, 100)
127 rect2 = doc.rectangle (20, 20, 100, 100)
128 rect3 = doc.rectangle (40, 40, 100, 100)
129 rect4 = doc.rectangle (60, 60, 100, 100)
131 doc.selection_set(rect1)
132 doc.selection_change_level("raise")
134 doc.selection_set(rect4)
135 doc.selection_change_level("to_bottom")
137 def testCombinations (doc):
138 rect1 = doc.rectangle (0, 0, 100, 100)
139 rect2 = doc.rectangle (20, 20, 100, 100)
140 rect3 = doc.rectangle (40, 40, 100, 100)
141 rect4 = doc.rectangle (60, 60, 100, 100)
142 rect5 = doc.rectangle (80, 80, 100, 100)
143 rect6 = doc.rectangle (100, 100, 100, 100)
144 rect7 = doc.rectangle (120, 120, 100, 100)
145 rect8 = doc.rectangle (140, 140, 100, 100)
146 rect9 = doc.rectangle (160, 160, 100, 100)
147 rect10 = doc.rectangle (180, 180, 100, 100)
149 doc.selection_set_list([rect1, rect2])
150 print doc.selection_combine("union")
151 doc.selection_set_list([rect3, rect4])
152 print doc.selection_combine("intersection")
153 doc.selection_set_list([rect5, rect6])
154 print doc.selection_combine("difference")
155 doc.selection_set_list([rect7, rect8])
156 print doc.selection_combine("exclusion")
157 doc.selection_set_list([rect9, rect10])
158 for d in doc.selection_divide():
159 print d
161 def testTransforms (doc):
162 rect1 = doc.rectangle (0, 0, 100, 100)
163 rect2 = doc.rectangle (20, 20, 100, 100)
164 doc.set_attribute(rect1, "transform", "matrix(0.08881734,0.94288151,-0.99604793,0.68505564,245.36153,118.60315)")
165 doc.selection_set(rect1)
167 doc.selection_move_to(200, 200)
169 def testLayer (doc):
170 rect1 = doc.rectangle (0, 0, 100, 100)
171 print doc.new_layer()
172 rect2 = doc.rectangle (20, 20, 100, 100)
174 def testGetSelection (doc):
175 rect1 = doc.rectangle (0, 0, 100, 100)
176 rect2 = doc.rectangle (20, 20, 100, 100)
177 rect3 = doc.rectangle (40, 40, 100, 100)
178 doc.select_all()
179 result = doc.selection_get()
180 print result
181 print len(result)
182 for d in result:
183 print d
186 def testDocStyle (doc):
187 rect1 = doc.rectangle (0, 0, 100, 100)
188 doc.document_set_css ("fill:#ff0000;fill-opacity:.5;stroke:#0000ff;stroke-width:5;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none")
189 rect2 = doc.rectangle (20, 20, 100, 100)
190 doc.document_set_css ("fill:#ffff00;fill-opacity:1;stroke:#009002;stroke-width:5;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none")
191 rect3 = doc.rectangle (40, 40, 100, 100)
192 doc.document_set_css ("fill:#00ff00;fill-opacity:1")
193 rect4 = doc.rectangle (60, 60, 100, 100)
195 def testStyle (doc):
196 doc.document_set_css ("fill:#ffff00;fill-opacity:1;stroke:#009002;stroke-width:5;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none")
197 rect1 = doc.rectangle (0, 0, 100, 100)
198 rect2 = doc.rectangle (20, 20, 100, 100)
199 rect3 = doc.rectangle (40, 40, 100, 100)
200 rect4 = doc.rectangle (60, 60, 100, 100)
202 doc.modify_css (rect3, "fill-opacity", ".5")
203 doc.merge_css (rect4, "fill:#0000ff;fill-opacity:.25;")
204 print doc.get_css (rect4)
206 def testLayers (doc):
207 rect1 = doc.rectangle (0, 0, 100, 100)
208 layer1 = doc.layer_new()
209 layer2 = doc.layer_new()
210 rect2 = doc.rectangle (20, 20, 100, 100)
211 rect3 = doc.rectangle (40, 40, 100, 100)
212 doc.selection_add(rect3)
213 doc.selection_move_to_layer(layer1)
215 def testLoadSave (doc):
216 doc.load("/home/soren/testfile.svg")
217 rect2 = doc.rectangle (0, 0, 200, 200)
218 doc.save_as("/home/soren/testsave.svg")
219 rect1 = doc.rectangle (20, 20, 200, 200)
220 doc.save()
221 rect3 = doc.rectangle (40, 40, 200, 200)
222 doc.save_as("/home/soren/testsave2.svg")
224 def testArray (doc):
225 rect1 = doc.rectangle (0, 0, 100, 100)
226 rect2 = doc.rectangle (20, 20, 100, 100)
227 rect3 = doc.rectangle (40, 40, 100, 100)
228 doc.selection_set_list([rect1, rect2, rect3])
230 def testPath (doc):
231 cr1 = doc.ellipse(0,0,50,50)
232 print doc.get_path(cr1)
233 doc.object_to_path(cr1)
234 print doc.get_path(cr1)
235 #doc.get_node_coordinates(cr1)
237 # Needs work.
238 def testText(doc):
239 print doc.text(200, 200, "svg:text")
242 #####################################################################
243 # Setup bus connection, create documents.
244 #####################################################################
246 # Connect to bus
247 bus = dbus.SessionBus()
249 # Get interface for default document
250 inkdoc1 = bus.get_object('org.inkscape', '/org/inkscape/desktop_0')
251 doc1 = dbus.Interface(inkdoc1, dbus_interface="org.inkscape.document")
253 # Create new window and get the interface for that. (optional)
254 inkapp = bus.get_object('org.inkscape',
255 '/org/inkscape/application')
256 desk2 = inkapp.desktop_new(dbus_interface='org.inkscape.application')
257 inkdoc2 = bus.get_object('org.inkscape', desk2)
258 doc2 = dbus.Interface(inkdoc2, dbus_interface="org.inkscape.document")
261 #####################################################################
262 # Call desired test functions
263 #####################################################################
265 #lottaverbs (doc1)
266 #lottarects (doc1)
267 #testDrawing (doc1)
269 #doc1.pause_updates()
271 testShapes(doc1)
272 #testMovement(doc1)
273 #testImport(doc1) # EDIT FUNCTION TO OPEN EXISTING FILE!
274 #testcopypaste (doc1)
275 #testTransforms (doc1)
276 #testDocStyle(doc1)
277 #testLayers(doc1)
278 #testLoadSave(doc1)
279 #testArray(doc1)
280 #testSelections(doc1)
281 #testCombinations(doc1)
282 #testText(doc1)
283 #testPath(doc1)
285 #doc1.resume_updates
288 # Prevents asking if you want to save when closing document.
289 doc1.mark_as_unmodified()