~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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python
'''
Guides Creator v2.31 (05/07/2009)
http://code.google.com/p/inkscape-guides-creator/

Copyright (C) 2008 Jonas Termeau - jonas.termeau **AT** gmail.com

Thanks to:

Bernard Gray - bernard.gray **AT** gmail.com (python helping)
Jamie Heames (english translation issues)
~suv (bug report in v2.3)
http://www.gutenberg.eu.org/publications/ (9x9 margins settings)

## This basic extension allows you to automatically draw guides in inkscape.

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; version 2 of the License.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

# Inspired by hello_world turorial by Blackhex and Rubikcube
# (http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial)

# Making an .INX file : http://wiki.inkscape.org/wiki/index.php/MakingAnINX
# see also http://codespeak.net/lxml/dev/tutorial.html#namespaces for XML namespaces manipulation

# # # # # # # #
# TODO:  See http://code.google.com/p/inkscape-guides-creator/wiki/Roadmap
# # # # # # # #
 

# # # extension's begining # # #

# These two lines are only needed if you don't put the script directly into
# the installation directory
import sys
sys.path.append('/usr/share/inkscape/extensions')

from xml.etree import ElementTree as ET
# for golden number formulae
from math import sqrt

# We will use the inkex module with the predefined Effect base class.
import inkex
from simplestyle import *

# for localized debugging output
inkex.localize()

from xml.etree import ElementTree as ET

# for golden number formula and diagonal guides
from math import sqrt
from math import cos
from math import sin

# for printing debugging output
import gettext
_ = gettext.gettext

def printDebug(string):
        inkex.errormsg(_(str(string)))

def drawVerticalGuides(division,w,h,edges,parent,vertical_shift=0):
        if (division > 0): 
                if (edges):
                        var = 1
                else:
                        var = 0

                for v in range(0,division-1+2*var):
                        # setting up the guide's attributes (id is generated automatically)
                        position = str(round((w / division) + (v - var) * (w / division) + vertical_shift,4)) + ",0"
                        orientation = str(round(h,4)) + ",0"
                        
                        createGuide(position,orientation,parent)                

def drawHorizontalGuides(division,w,h,edges,parent,horizontal_shift=0):
        if (division > 0):
                if (edges):
                        var = 1
                else:
                        var = 0

                for x in range(0,division-1+2*var):
                        # setting up the guide's attributes (id is generated automatically)
                        position = "0," + str(round((h / division) + (x - var) * (h / division) + horizontal_shift,4))
                        orientation = "0," + str(round(w,4))

                        createGuide(position,orientation,parent)

def createGuide(position,orientation,parent):
        # Create a sodipodi:guide node
        # (look into inkex's namespaces to find 'sodipodi' value in order to make a "sodipodi:guide" tag)
        # see NSS array in file inkex.py for the other namespaces
        inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':position,'orientation':orientation})

def getVerticalDivisionsFromPreset(preset):
        # take a "string1;string2" preset
        # and return "string1"
        str_array = preset.split(';')
        result = int(str_array[0])

        return result

def getHorizontalDivisionsFromPreset(preset):
        # take a "string1;string2" preset
        # and return "string2"
        str_array = preset.split(';')
        result = int(str_array[1])

        return result

def deleteAllGuides(document):
        # getting the parent's tag of the guides
        nv = document.xpath('/svg:svg/sodipodi:namedview',namespaces=inkex.NSS)[0]

        # getting all the guides
        children = document.xpath('/svg:svg/sodipodi:namedview/sodipodi:guide',namespaces=inkex.NSS)

        # removing each guides
        for element in children:
                nv.remove(element)

class Guides_Creator(inkex.Effect):
        
        def __init__(self):
                """
                Constructor.
                Defines options of the script.
                """
                # Call the base class constructor.
                inkex.Effect.__init__(self)

                # Define option for the tab.
		self.OptionParser.add_option("--tab",
                        action="store",type="string",
                        dest="tab", default="regular_guides",
                        help="")

                # Define string option "--preset" with default value 'custom'.
                self.OptionParser.add_option('--guides_preset',
                        action = 'store',type = 'string',
                        dest = 'guides_preset',default = 'custom',
                        help = 'Preset')

                # Define string option "--vertical_guides" with default value '0'.
                self.OptionParser.add_option('--vertical_guides',
                        action = 'store',type = 'string',
                        dest = 'vertical_guides',default = 0,
                        help = 'Vertical guides each:')

                # Define string option "--horizontal_guides" with default value '0'.
                self.OptionParser.add_option('--horizontal_guides',
                        action = 'store',type = 'string',
                        dest = 'horizontal_guides',default = 0,
                        help = 'Horizontal guides each:')

                # Define boolean option "--start_from_edges" with default value False.
                self.OptionParser.add_option('--start_from_edges',
                        action = 'store',type = 'inkbool',
                        dest = 'start_from_edges',default = False,
                        help = 'Start from edges')

                # Define boolean option "--delete_existing_guides" with default value False.
                self.OptionParser.add_option('--delete_existing_guides',
                        action = 'store',type = 'inkbool',
                        dest = 'delete_existing_guides',default = False,
                        help = 'Delete existing guides')

                # Define boolean option "--upper_left_corner" with default value False.
                self.OptionParser.add_option('--ul',
                        action = 'store',type = 'inkbool',
                        dest = 'ul',default = False,
                        help = 'Upper left corner')

                # Define boolean option "--upper_right_corner" with default value False.
                self.OptionParser.add_option('--ur',
                        action = 'store',type = 'inkbool',
                        dest = 'ur',default = False,
                        help = 'Upper right corner')

                # Define boolean option "--lower_left_corner" with default value False.
                self.OptionParser.add_option('--ll',
                        action = 'store',type = 'inkbool',
                        dest = 'll',default = False,
                        help = 'Lower left corner')

                # Define boolean option "--upper_left_corner" with default value False.
                self.OptionParser.add_option('--lr',
                        action = 'store',type = 'inkbool',
                        dest = 'lr',default = False,
                        help = 'Lower right corner')

                # Define boolean option "--delete_existing_guides2" with default value False.
                self.OptionParser.add_option('--delete_existing_guides2',
                        action = 'store',type = 'inkbool',
                        dest = 'delete_existing_guides2',default = False,
                        help = 'Delete existing guides')

                # Define string option "--margins_preset" with default value 'custom'.
                self.OptionParser.add_option('--margins_preset',
                        action = 'store',type = 'string',
                        dest = 'margins_preset',default = 'custom',
                        help = 'Margins preset')

                # Define boolean option "--delete_existing_guides3" with default value False.
                self.OptionParser.add_option('--delete_existing_guides3',
                        action = 'store',type = 'inkbool',
                        dest = 'delete_existing_guides3',default = False,
                        help = 'Delete existing guides')

                # Define string option "--vertical_subdivisions" with default value '0'.
                self.OptionParser.add_option('--vertical_subdivisions',
                        action = 'store',type = 'string',
                        dest = 'vertical_subdivisions',default = 0,
                        help = 'Vertical subdivisions')

                # Define string option "--horizontal_subdivisions" with default value '0'.
                self.OptionParser.add_option('--horizontal_subdivisions',
                        action = 'store',type = 'string',
                        dest = 'horizontal_subdivisions',default = 0,
                        help = 'Horizontal subdivisions')

                # Define string option "--header_margin" with default value '6'.
                self.OptionParser.add_option('--header_margin',
                        action = 'store',type = 'string',
                        dest = 'header_margin',default = 6,
                        help = 'Header margin')

                # Define string option "--footer_margin" with default value '6'.
                self.OptionParser.add_option('--footer_margin',
                        action = 'store',type = 'string',
                        dest = 'footer_margin',default = 6,
                        help = 'Footer margin')

                # Define string option "--left_margin" with default value '6'.
                self.OptionParser.add_option('--left_margin',
                        action = 'store',type = 'string',
                        dest = 'left_margin',default = 6,
                        help = 'Left margin')

                # Define string option "--right_margin" with default value '6'.
                self.OptionParser.add_option('--right_margin',
                        action = 'store',type = 'string',
                        dest = 'right_margin',default = 6,
                        help = 'Right margin')

                # Define boolean option "--start_from_edges2" with default value False.
                self.OptionParser.add_option('--start_from_edges2',
                        action = 'store',type = 'inkbool',
                        dest = 'start_from_edges2',default = False,
                        help = 'Start from edges')
        
        def effect(self):

                # Get script's options value.

                tab = self.options.tab

                # first tab
                guides_preset = self.options.guides_preset
                h_division = int(self.options.horizontal_guides)
                v_division = int(self.options.vertical_guides)
                from_edges = self.options.start_from_edges
                delete_existing = self.options.delete_existing_guides

                # second tab
                upper_left = self.options.ul
                upper_right = self.options.ur
                lower_left = self.options.ll
                lower_right = self.options.lr
                delete_existing2 = self.options.delete_existing_guides2

                # third tab
                margins_preset = self.options.margins_preset
                header_margin = int(self.options.header_margin)
                footer_margin = int(self.options.footer_margin)
                left_margin = int(self.options.left_margin)
                right_margin = int(self.options.right_margin)
                h_subdiv = int(self.options.horizontal_subdivisions)
                v_subdiv = int(self.options.vertical_subdivisions)
                from_edges2 = self.options.start_from_edges2
                delete_existing3 = self.options.delete_existing_guides3

                # getting the main SVG document element (canvas)
                svg = self.document.getroot()

                # getting the width and height attributes of the canvas
                width  = self.unittouu(svg.get('width'))/self.unittouu('1px')
                height = self.unittouu(svg.get('height'))/self.unittouu('1px')

                # getting edges coordinates
                h_orientation = '0,' + str(round(width,4))
                v_orientation = str(round(height,4)) + ',0'

                # getting parent tag of the guides
                nv = self.document.find(inkex.addNS('namedview', 'sodipodi'))

                if (tab == "\"regular_guides\""):
                        
                        if (delete_existing):
                                deleteAllGuides(self.document)

                        if (guides_preset == 'custom'):

                                if ((v_division == 0) and (from_edges)):
                                        v_division = 1

                                if ((h_division == 0) and (from_edges)):
                                        h_division = 1

                                # creating vertical guides
                                drawVerticalGuides(v_division,width,height,from_edges,nv)
                                        
                                # creating horizontal guides
                                drawHorizontalGuides(h_division,width,height,from_edges,nv)

                        elif (guides_preset == 'golden'):

                                gold = (1 + sqrt(5)) / 2
                                
                                # horizontal golden guides
                                position1 = '0,' + str(height / gold)
                                position2 = '0,'+  str(height - (height / gold))

                                createGuide(position1,h_orientation,nv)
                                createGuide(position2,h_orientation,nv)

                                # vertical golden guides
                                position1 =  str(width / gold) + ',0'
                                position2 = str(width - (width / gold)) + ',0'

                                createGuide(position1,v_orientation,nv)
                                createGuide(position2,v_orientation,nv)

                                if (from_edges):
                                        # horizontal borders
                                        createGuide('0,' + str(height),h_orientation,nv)
                                        createGuide(str(height) + ',0',h_orientation,nv)

                                        # vertical borders
                                        createGuide('0,' + str(width),v_orientation,nv)
                                        createGuide(str(width) + ',0',v_orientation,nv)


                        else:

                                v_division = getVerticalDivisionsFromPreset(guides_preset)
                                h_division = getHorizontalDivisionsFromPreset(guides_preset)

                                drawVerticalGuides(v_division,width,height,from_edges,nv)
                                drawHorizontalGuides(h_division,width,height,from_edges,nv)

                elif (tab == "\"diagonal_guides\""):

                        if (delete_existing2):
                                deleteAllGuides(self.document)

                        # diagonal
                        angle = 45                        

                        # X axe
                        left = 0
                        right = width

                        # Y axe
                        down = 0
                        up = height

                        ul_corner = str(up) + ',' + str(left)
                        ur_corner = str(right) + ',' + str(up)
                        ll_corner = str(down) + ',' + str(left)
                        lr_corner = str(down) + ',' + str(right)

                        from_ul_to_lr = str(cos(angle)) + ',' + str(cos(angle))
                        from_ur_to_ll = str(-sin(angle)) + ',' + str(sin(angle))
                        from_ll_to_ur = str(-cos(angle)) + ',' + str(cos(angle))
                        from_lr_to_ul = str(-sin(angle)) + ',' + str(-sin(angle))

                        if (upper_left):
                                createGuide(ul_corner,from_ul_to_lr,nv)

                        if (upper_right):
                                createGuide(ur_corner,from_ur_to_ll,nv)

                        if (lower_left):
                                createGuide(ll_corner,from_ll_to_ur,nv)
                        if (lower_right):
                                createGuide(lr_corner,from_lr_to_ul,nv)

                elif (tab == "\"margins\""):

                        if (delete_existing3):
                                deleteAllGuides(self.document)

                        if (from_edges2):

                                        # horizontal borders
                                        createGuide('0,' + str(height),h_orientation,nv)
                                        createGuide(str(height) + ',0',h_orientation,nv)

                                        # vertical borders
                                        createGuide('0,' + str(width),v_orientation,nv)
                                        createGuide(str(width) + ',0',v_orientation,nv)

                        if (margins_preset == 'custom'):

                                y_header = height
                                y_footer = 0
                                x_left = 0
                                x_right = width

                                if (header_margin != 0):
                                        y_header = (height / header_margin) * (header_margin - 1)
                                        createGuide('0,' + str(y_header),h_orientation,nv)

                                if (footer_margin != 0):
                                        y_footer = height / footer_margin
                                        createGuide('0,' + str(y_footer),h_orientation,nv)

                                if (left_margin != 0):
                                        x_left = width / left_margin
                                        createGuide(str(x_left) + ',0',v_orientation,nv)

                                if (right_margin != 0):
                                        x_right = (width / right_margin) * (right_margin - 1)
                                        createGuide(str(x_right) + ',0',v_orientation,nv)
                                        
                        elif (margins_preset == 'book_left'):
                                        # 1/9th header
                                        y_header = (height / 9) * 8
                                        createGuide('0,' + str(y_header),h_orientation,nv)

                                        # 2/9th footer
                                        y_footer = (height / 9) * 2
                                        createGuide('0,' + str(y_footer),h_orientation,nv)

                                        # 2/9th left margin
                                        x_left = (width / 9) * 2
                                        createGuide(str(x_left) + ',0',v_orientation,nv)

                                        # 1/9th right margin
                                        x_right = (width / 9) * 8
                                        createGuide(str(x_right) + ',0',v_orientation,nv)

                        elif (margins_preset == 'book_right'):
                                        # 1/9th header
                                        y_header = (height / 9) * 8
                                        createGuide('0,' + str(y_header),h_orientation,nv)

                                        # 2/9th footer
                                        y_footer = (height / 9) * 2
                                        createGuide('0,' + str(y_footer),h_orientation,nv)

                                        # 2/9th left margin
                                        x_left = (width / 9)
                                        createGuide(str(x_left) + ',0',v_orientation,nv)

                                        # 1/9th right margin
                                        x_right = (width / 9) * 7
                                        createGuide(str(x_right) + ',0',v_orientation,nv)


                        # setting up properties of the rectangle created between guides
                        rectangle_height = y_header - y_footer
                        rectangle_width = x_right - x_left

                        if (h_subdiv != 0):
                                begin_from = y_footer

                                # creating horizontal guides
                                drawHorizontalGuides(h_subdiv,rectangle_width,rectangle_height,0,nv,begin_from)

                        if (v_subdiv != 0):
                                begin_from = x_left

                                # creating vertical guides
                                drawVerticalGuides(v_subdiv,rectangle_width,rectangle_height,0,nv,begin_from)

if __name__ == '__main__':
	# Create effect instance and apply it.
	effect = Guides_Creator()
	effect.affect()

## end of file guide_creator.py ##