Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
functions to external file
Browse files Browse the repository at this point in the history
  • Loading branch information
sambody committed Feb 20, 2015
1 parent 262f11a commit 2079072
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 135 deletions.
92 changes: 92 additions & 0 deletions extensions/guidetools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
'''
Library functions for Guide Tools (grid guides, center guides, margin guides)
'''

import inkex
import gettext
_ = gettext.gettext

# To show error to user (or for debugging)
def show(string):
inkex.errormsg(_(str(string)))

# Draw single guide
# parameters: position (single length), orientation ("horizontal/vertical"), parent (namedview)
def drawGuide(position, orientation, parent):

if (orientation == "vertical"):
orientationString = "1,0"
positionString = str(position) + ",0"
else:
orientationString = "0,1"
positionString = "0," + str(position)

# Create a sodipodi:guide node
inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString})

# Adding color to guide is not working in 0.91, as Inkscape doesn't read the value in the xml, due to a bug.
# Let's wait for 0.92 to implement this then. Here is the code to use:
# inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString, '{http://www.inkscape.org/namespaces/inkscape}color':"rgb(255,0,0)"})

# Delete all/vertical/horizontal guides
def deleteGuidesByOrientation(document, orientation):

# getting the parent's tag of the guides
namedview = 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)

# depending on which type of guide to remove, remove them
if (orientation == 'all'):
for element in children:
namedview.remove(element)
elif (orientation == 'horizontal'):
for element in children:
if (element.get('orientation') == '0,1'):
namedview.remove(element)
elif (orientation == 'vertical'):
for element in children:
if (element.get('orientation') == '1,0'):
namedview.remove(element)



# Draw series of guides with or without gutter - same function called for columns and rows
def drawDoubleGuides(colsRows, width, gutter, start_pos, has_outer_gutter, orientation, parent):

# position of guide
position = start_pos

# Draw series of double guides (or single guides when no gutter)
for i in range(0, colsRows+1):

# if first or last gutter
if ( i == 0 or i == colsRows ):

# if no gutter, draw single guide; if gutter, draw single or double guide
if gutter == 0:
drawGuide(position, orientation, parent)
position += width
else:
if has_outer_gutter == False:
drawGuide(position, orientation, parent)
position += width
else:
drawGuide(position, orientation, parent)
position += gutter
drawGuide(position, orientation, parent)
position += width

# other gutter (not first/last)
else:
if gutter == 0:
drawGuide(position, orientation, parent)
position += width
else:
drawGuide(position, orientation, parent)
position += gutter
drawGuide(position, orientation, parent)
position += width

2 changes: 2 additions & 0 deletions extensions/guidetools_add_centered_guides.inx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<inkscape-extension>
<_name>Add Centered Guides to Page or Selection</_name>
<id>samplify.guidetools_add_centered_guides</id>
<dependency type="executable" location="extensions">guidetools.py</dependency>
<dependency type="executable" location="extensions">guidetools_add_centered_guides.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>

<param name="extension_description" type="description">This will add a horizontal and a vertical guide through the center of the selected object or the page</param>
<param name="target" type="enum" _gui-text="Add centered guides to ">
Expand Down
22 changes: 3 additions & 19 deletions extensions/guidetools_add_centered_guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import inkex
import gettext
_ = gettext.gettext
import guidetools
try:
from subprocess import Popen, PIPE
except ImportError:
Expand All @@ -42,23 +43,6 @@
# To show debugging output, error messages, use
# inkex.debug( _(str(string)) )

# Draw single guide
# parameters: position (single length), orientation ("horizontal/vertical"), parent
def drawGuide(position, orientation, parent):

if (orientation == "vertical"):
orientationString = "1,0"
positionString = str(position) + ",0"
else:
orientationString = "0,1"
positionString = "0," + str(position)

# Create a sodipodi:guide node
inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString})

# Adding color to guide - not working in 0.91, as Inkscape doesn't read it, although it is written in the xml.
# Let's wait for 0.92 to implement this then.
# inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString, '{http://www.inkscape.org/namespaces/inkscape}color':"rgb(255,0,0)"})


class addCenteredGuides(inkex.Effect):
Expand Down Expand Up @@ -127,8 +111,8 @@ def effect(self):
center_y = canvas_height/2

# call the function. Output.
drawGuide(center_x, "vertical", namedview)
drawGuide(center_y, "horizontal", namedview)
guidetools.drawGuide(center_x, "vertical", namedview)
guidetools.drawGuide(center_y, "horizontal", namedview)


# APPLY
Expand Down
1 change: 1 addition & 0 deletions extensions/guidetools_add_grid_guides.inx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<_name>Add Grid Guides</_name>
<id>samplify.guidetools_add_grid_guides</id>

<dependency type="executable" location="extensions">guidetools.py</dependency>
<dependency type="executable" location="extensions">guidetools_add_grid_guides.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>

Expand Down
98 changes: 9 additions & 89 deletions extensions/guidetools_add_grid_guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,87 +32,7 @@
from simplestyle import *
import gettext
_ = gettext.gettext

# FUNCTIONS

# To show error to user (or for debugging)
def printError(string):
inkex.errormsg(_(str(string)))

# Delete all/vertical/horizontal guides
def deleteGuidesByOrientation(document, orientation):

# getting the parent's tag of the guides
namedview = 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)

# depending on which type of guide to remove, remove them
if (orientation == 'all'):
for element in children:
namedview.remove(element)
elif (orientation == 'horizontal'):
for element in children:
if (element.get('orientation') == '0,1'):
namedview.remove(element)
elif (orientation == 'vertical'):
for element in children:
if (element.get('orientation') == '1,0'):
namedview.remove(element)

# Draw single guide
# based on position (length), orientation ("horizontal/vertical"), parent
def drawGuide(position, orientation, parent):

if orientation == "vertical":
orientationString = "1,0"
positionString = str(position) + ",0"

if orientation == "horizontal":
orientationString = "0,1"
positionString = "0," + str(position)

# Create a sodipodi:guide node
inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString})


# Draw series of guides with or without gutter - same function called for columns and rows
def drawDoubleGuides(colsRows, width, gutter, start_pos, has_outer_gutter, orientation, parent):

# position of guide
position = start_pos

# Draw series of double guides (or single guides when no gutter)
for i in range(0, colsRows+1):

# if first or last gutter
if ( i == 0 or i == colsRows ):

# if no gutter, draw single guide; if gutter, draw single or double guide
if gutter == 0:
drawGuide(position, orientation, parent)
position += width
else:
if has_outer_gutter == False:
drawGuide(position, orientation, parent)
position += width
else:
drawGuide(position, orientation, parent)
position += gutter
drawGuide(position, orientation, parent)
position += width

# other gutter (not first/last)
else:
if gutter == 0:
drawGuide(position, orientation, parent)
position += width
else:
drawGuide(position, orientation, parent)
position += gutter
drawGuide(position, orientation, parent)
position += width
import guidetools


# CLASS
Expand Down Expand Up @@ -294,7 +214,7 @@ def effect(self):

# Delete existing vertical guides
if (delete_vert):
deleteGuidesByOrientation(self.document, 'vertical')
guidetools.deleteGuidesByOrientation(self.document, 'vertical')

# Set horizontal starting position, depending on grid alignment
if (col_alignment == 'left'):
Expand All @@ -307,7 +227,7 @@ def effect(self):
hor_start = canvas_width - total_col_width + col_offset

# Create column guides with column_spacings
drawDoubleGuides(cols, col_width, col_gut, hor_start, has_outer_col_gutter, "vertical", namedview)
guidetools.drawDoubleGuides(cols, col_width, col_gut, hor_start, has_outer_col_gutter, "vertical", namedview)

# Center guides columns
if (has_outer_col_gutter == True):
Expand All @@ -318,16 +238,16 @@ def effect(self):
center_hor_start = hor_start + (col_gut/2) + col_width
# Draw centered guides if necessary
if col_gut > 0 and has_center_col_guides == True:
drawDoubleGuides(center_cols, col_width + col_gut, 0, center_hor_start, has_outer_col_gutter, "vertical", namedview)
guidetools.drawDoubleGuides(center_cols, col_width + col_gut, 0, center_hor_start, has_outer_col_gutter, "vertical", namedview)

# Give total width in original units
if (show_total_width == True):
printError("Total width of grid will be: " + str(total_col_width/col_factor) + " " + self.options.column_unit)
guidetools.show("Total width of grid will be: " + str(total_col_width/col_factor) + " " + self.options.column_unit)

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

# Delete existing horizontal guides
if (delete_hor): deleteGuidesByOrientation(self.document, 'horizontal')
if (delete_hor): guidetools.deleteGuidesByOrientation(self.document, 'horizontal')

# Set vertical starting position, depending on grid alignment
# 0,0 is at BOTTOM left of document, guides will be drawn bottom up
Expand All @@ -341,7 +261,7 @@ def effect(self):
vert_start = -row_offset

# Create row guides
drawDoubleGuides(rows, row_height, row_gut, vert_start, has_outer_row_gutter, "horizontal", namedview)
guidetools.drawDoubleGuides(rows, row_height, row_gut, vert_start, has_outer_row_gutter, "horizontal", namedview)

# Center guides rows
if (has_outer_row_gutter == True):
Expand All @@ -352,11 +272,11 @@ def effect(self):
center_vert_start = vert_start + (row_gut/2) + row_height
# Draw centered guides if necessary
if row_gut > 0 and has_center_row_guides == True:
drawDoubleGuides(center_rows, row_height + row_gut, 0, center_vert_start, has_outer_row_gutter, "horizontal", namedview)
guidetools.drawDoubleGuides(center_rows, row_height + row_gut, 0, center_vert_start, has_outer_row_gutter, "horizontal", namedview)

# Give total height in original units
if (show_total_height == True):
printError("Total height of grid will be: " + str(total_row_height/row_factor) + " " + self.options.row_unit)
guidetools.show("Total height of grid will be: " + str(total_row_height/row_factor) + " " + self.options.row_unit)


# Create effect instance and apply it.
Expand Down
2 changes: 2 additions & 0 deletions extensions/guidetools_add_margin_guides.inx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<inkscape-extension>
<_name>Add Margin Guides to Page or Selection</_name>
<id>samplify.guidetools_add_margin_guides</id>
<dependency type="executable" location="extensions">guidetools.py</dependency>
<dependency type="executable" location="extensions">guidetools_add_margin_guides.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>

<param name="tab" type="notebook">
<page name="margin-guides" _gui-text="Margin Guides">
Expand Down
33 changes: 9 additions & 24 deletions extensions/guidetools_add_margin_guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import inkex
import gettext
_ = gettext.gettext
import guidetools
try:
from subprocess import Popen, PIPE
except ImportError:
Expand All @@ -43,22 +44,6 @@

# To show debugging output or error messages, use: inkex.debug( _(str(string)) )

# Draw single guide
# parameters: position (single length), orientation ("horizontal/vertical"), parent
def drawGuide(position, orientation, parent):

if orientation == "vertical":
orientationString = "1,0"
positionString = str(position) + ",0"

if orientation == "horizontal":
orientationString = "0,1"
positionString = "0," + str(position)

# Create a sodipodi:guide node
inkex.etree.SubElement(parent,'{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide',{'position':positionString,'orientation':orientationString})


# CLASS

class addMarginGuides(inkex.Effect):
Expand Down Expand Up @@ -187,10 +172,10 @@ def effect(self):

# Draw the four margin guides
# TODO: only draw if not on border
drawGuide(top_pos, "horizontal", namedview)
drawGuide(right_pos, "vertical", namedview)
drawGuide(bottom_pos, "horizontal", namedview)
drawGuide(left_pos, "vertical", namedview)
guidetools.drawGuide(top_pos, "horizontal", namedview)
guidetools.drawGuide(right_pos, "vertical", namedview)
guidetools.drawGuide(bottom_pos, "horizontal", namedview)
guidetools.drawGuide(left_pos, "vertical", namedview)

else:

Expand All @@ -207,10 +192,10 @@ def effect(self):
left_pos = left_margin

# Draw the four margin guides (if margin exists)
if top_pos != canvas_height: drawGuide(top_pos, "horizontal", namedview)
if right_pos != canvas_width: drawGuide(right_pos, "vertical", namedview)
if bottom_pos != 0: drawGuide(bottom_pos, "horizontal", namedview)
if left_pos != 0: drawGuide(left_pos, "vertical", namedview)
if top_pos != canvas_height: guidetools.drawGuide(top_pos, "horizontal", namedview)
if right_pos != canvas_width: guidetools.drawGuide(right_pos, "vertical", namedview)
if bottom_pos != 0: guidetools.drawGuide(bottom_pos, "horizontal", namedview)
if left_pos != 0: guidetools.drawGuide(left_pos, "vertical", namedview)


# APPLY
Expand Down
Loading

0 comments on commit 2079072

Please sign in to comment.