Code

new version of Guides Creator extension from Jonas Termeau
authorprokoudine <prokoudine@users.sourceforge.net>
Thu, 27 Nov 2008 22:23:28 +0000 (22:23 +0000)
committerprokoudine <prokoudine@users.sourceforge.net>
Thu, 27 Nov 2008 22:23:28 +0000 (22:23 +0000)
share/extensions/guides_creator.inx
share/extensions/guides_creator.py

index 24aebab674e0e58b2f6729c25953a42eb27dc823..1ec0a6584122bf4735d866e3d263f50926eae309 100644 (file)
@@ -4,7 +4,12 @@
        <id>org.inkscape.effect.guidescreator</id>
        <dependency type="executable" location="extensions">guides_creator.py</dependency>
        <dependency type="executable" location="extensions">inkex.py</dependency>
-       <param name="vertical_guides" type="optiongroup" appearance="minimal" _gui-text="Vertical guides each">
+       <param name="preset" type="optiongroup" appearance="minimal" _gui-text="Preset">
+               <_option value="custom">Custom...</_option>
+                <_option value="golden">Golden ratio</_option>
+                <_option value="3;3">Rule-of-third</_option>
+        </param>
+        <param name="vertical_guides" type="optiongroup" appearance="minimal" _gui-text="Vertical guide each">
                <_option value="0">None</_option>
                <_option value="2">1/2</_option>
                <_option value="3">1/3</_option>\r
@@ -16,7 +21,7 @@
                 <_option value="9">1/9</_option>
                <_option value="10">1/10</_option>
        </param>
-       <param name="horizontal_guides" type="optiongroup" appearance="minimal" _gui-text="Horizontal guides each">
+       <param name="horizontal_guides" type="optiongroup" appearance="minimal" _gui-text="Horizontal guide each">
                <_option value="0">None</_option>
                <_option value="2">1/2</_option>
                <_option value="3">1/3</_option>\r
index 2807089f3c842f4ad6c8edad35f89c6dc93d07c2..45fae9352a297e8449b9edf1867a280a3fd81948 100755 (executable)
@@ -1,8 +1,11 @@
 #!/usr/bin/env python
 '''
-Guides Creator v1.0 (21/11/2008)
+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
+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.
 
@@ -28,8 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 # see also http://codespeak.net/lxml/dev/tutorial.html#namespaces for XML namespaces manipulation
 
 # # # # # # # #
-# TODO:  [v2.0]
-#        -implement vertical and horizontal golden number proportions (987/610) based guides
+# TODO:  See http://code.google.com/p/inkscape-guides-creator/wiki/Roadmap
 #
 # # # # # # # #
  
@@ -47,7 +49,67 @@ 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):
         
@@ -58,29 +120,36 @@ class Guides_Creator(inkex.Effect):
                 """
                 # Call the base class constructor.
                 inkex.Effect.__init__(self)
-                # Define string option "--vertical_guides" with default value 0.
+
+                # 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.
+                # 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 "--delete_existing_guides" with default value 0.
-                self.OptionParser.add_option('--delete_existing_guides',
-                        action = 'store',type = 'inkbool',
-                        dest = 'delete_existing_guides',default = False,
-                        help = 'Delete existing guides')
-
-                # Define string option "--start_from_edges" with default value 0.
+                # 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):
 
@@ -89,6 +158,7 @@ class Guides_Creator(inkex.Effect):
                 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()
@@ -108,35 +178,52 @@ class Guides_Creator(inkex.Effect):
                         for element in children:
                                 nv.remove(element)
 
-                if (from_edges):
-                        var = 1
-                else:
-                        var = 0
-                
-                # creating vertical guides
-                if (v_division > 0):
-                        for v in range(0,v_division-1+2*var):
-                                # setting up the guide's attributes (id is generated automatically)
-                                position = str(round((width / v_division) + (v - var) * (width / v_division),4)) + ",0"
-                                orientation = str(round(height,4)) + ",0"
-                                
-                                # 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(nv,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':position,'orientation':orientation})
-
-                # creating horizontal guides
-                if (h_division > 0):
-                        for h in range(0,h_division-1+2*var):
-                                # setting up the guide's attributes (id is generated automatically)
-                                position = "0," + str(round((height / h_division) + (h - var) * (height / h_division),4))
-                                orientation = "0," + str(round(width,4))
+                if (preset == 'custom'):
+
+                        # creating vertical guides
+                        drawVerticalGuides(v_division,width,height,from_edges,nv)
                                 
-                                # 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(nv,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':position,'orientation':orientation})
+                        # 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)
+                                
 # Create effect instance and apply it.
 effect = Guides_Creator()
 effect.affect()