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
|
#!/usr/bin/env python
'''
Guides Creator v2.0 (25/11/2008)
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
## 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; 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., 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')
# We will use the inkex module with the predefined Effect base class.
import inkex
from simplestyle import *
from xml.etree import ElementTree as ET
# for golden number formulae
from math import sqrt
# for printing debugging output
import gettext
_ = gettext.gettext
def printDebug(string):
inkex.errormsg(_(string))
def drawVerticalGuides(division,w,h,edges,parent):
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),4)) + ",0"
orientation = str(round(h,4)) + ",0"
createGuide(position,orientation,parent)
def drawHorizontalGuides(division,w,h,edges,parent):
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),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
class Guides_Creator(inkex.Effect):
def __init__(self):
"""
Constructor.
Defines options of the script.
"""
# Call the base class constructor.
inkex.Effect.__init__(self)
# Define string option "--preset" with default value 'custom'.
self.OptionParser.add_option('--preset',
action = 'store',type = 'string',
dest = '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 string 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 string 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')
def effect(self):
# Get script's options value.
from_edges = self.options.start_from_edges
delete_existing = self.options.delete_existing_guides
h_division = int(self.options.horizontal_guides)
v_division = int(self.options.vertical_guides)
preset = self.options.preset
# Get access to main SVG document element and get its dimensions.
svg = self.document.getroot()
# getting the width and height attributes of the canvas
width = inkex.unittouu(svg.get('width'))
height = inkex.unittouu(svg.attrib['height'])
# getting the parent tag of the guide
nv = self.document.xpath('/svg:svg/sodipodi:namedview',namespaces=inkex.NSS)[0]
if (delete_existing):
# getting all the guides
children = self.document.xpath('/svg:svg/sodipodi:namedview/sodipodi:guide',namespaces=inkex.NSS)
# removing each guides
for element in children:
nv.remove(element)
if (preset == 'custom'):
# creating vertical guides
drawVerticalGuides(v_division,width,height,from_edges,nv)
# creating horizontal guides
drawHorizontalGuides(h_division,width,height,from_edges,nv)
elif (preset == 'golden'):
gold = (1 + sqrt(5)) / 2
# horizontal golden guides
position1 = '0,' + str(height / gold)
position2 = '0,'+ str(height - (height / gold))
h_orientation = '0,' + str(round(width,4))
createGuide(position1,h_orientation,nv)
createGuide(position2,h_orientation,nv)
# vertical golden guides
position1 = str(width / gold) + ',0'
position2 = str(width - (width / gold)) + ',0'
v_orientation = str(round(height,4)) + ',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)
# horizontal borders
createGuide('0,' + str(width),v_orientation,nv)
createGuide(str(width) + ',0',v_orientation,nv)
else:
v_division = getVerticalDivisionsFromPreset(preset)
h_division = getHorizontalDivisionsFromPreset(preset)
drawVerticalGuides(v_division,width,height,from_edges,nv)
drawHorizontalGuides(h_division,width,height,from_edges,nv)
if __name__ == '__main__': #pragma: no cover
# Create effect instance and apply it.
effect = Guides_Creator()
effect.affect()
## end of file guide_creator.py ##
|