Difference between revisions of "Making layout grid with guides"

From Scribus Wiki
Jump to: navigation, search
(Scribus script to make layout grid with guides)
m
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This script make a "layout grid" useful for newspapers and magazines.
+
{{Scripting Index}}
  
 +
This is my first Python script. (Sorry if it is ugly, but my native language is Rebol). It makes a "layout grid" useful for designing newspapers and magazines.
 +
 +
José Antonio Meira da Rocha<br>
 +
joseantoniorocha@gmail.com<br>
 +
http://meiradarocha.jor.br/news/about
 
<pre>
 
<pre>
 
#!/usr/bin/env python2.3
 
#!/usr/bin/env python2.3
 
# -*- coding: utf-8 -*-
 
# -*- coding: utf-8 -*-
 
+
"""Generate a grid of guides for layout porpouses.
 +
Ask user for number of columns, number of rows and gap measure.
 +
"""
 
import sys
 
import sys
  
Line 22: Line 29:
 
defaultNumColumns = '5'
 
defaultNumColumns = '5'
 
defaultNumRows = '8'
 
defaultNumRows = '8'
defaultGap = '14.1732' #In points, due a bug in getPageMargins
+
defaultGap = '14.1732' #5mm in points, due a bug in getPageMargins
  
 
""" You don't need change nothing below this line """
 
""" You don't need change nothing below this line """
Line 32: Line 39:
 
      
 
      
 
from scribus import UNIT_POINTS,BUTTON_OK,ICON_WARNING
 
from scribus import UNIT_POINTS,BUTTON_OK,ICON_WARNING
 +
 +
def odd(num):
 +
    return num % 2
  
 
def main(argv):
 
def main(argv):
Line 43: Line 53:
 
         scribus.setUnit(UNIT_POINTS) # working in points, due a bug in getPageMargins
 
         scribus.setUnit(UNIT_POINTS) # working in points, due a bug in getPageMargins
 
          
 
          
         pageSize = scribus.getPageSize()
+
         pageWidth, pageHeight = scribus.getPageSize()
         pageWidth = pageSize[0]
+
         # The margins order is wrong in Scripter documentation
         pageHeight = pageSize[1]
+
         topMargin, leftMargin, rightMargin, bottomMargin = scribus.getPageMargins()
       
 
        pageMargins = scribus.getPageMargins()
 
        topMargin    = pageMargins[0]
 
        leftMargin  = pageMargins[1]
 
        rightMargin  = pageMargins[2]
 
        bottomMargin = pageMargins[3]
 
 
          
 
          
         printAreaWidth = pageWidth - leftMargin - rightMargin
+
         printAreaWidth = pageWidth - leftMargin - rightMargin
         printAreaHeight = pageHeight - topMargin - bottomMargin  
+
         printAreaHeight = pageHeight - topMargin - bottomMargin  
 
          
 
          
 
         numVgaps = numColumns-1
 
         numVgaps = numColumns-1
 
         numHgaps = numRows-1
 
         numHgaps = numRows-1
         columnWidth = ((printAreaWidth-(numVgaps*gap)) / numColumns)
+
         columnWidth = (printAreaWidth-(numVgaps*gap)) / numColumns
         rowHeight = ((printAreaHeight-(numHgaps*gap)) / numRows )
+
         rowHeight = (printAreaHeight-(numHgaps*gap)) / numRows
 
          
 
          
 
         VGuides = []
 
         VGuides = []
 
         HGuides = []
 
         HGuides = []
 +
        # getPageMargins() return only odd page margins.
 +
        # This correct even margins
 +
        if not odd(scribus.currentPage()):
 +
            leftMargin = rightMargin
 +
        # Build vertical guides
 
         for i in range(1,numColumns):
 
         for i in range(1,numColumns):
 
             res = leftMargin+(i*columnWidth)+((i-1)*gap)
 
             res = leftMargin+(i*columnWidth)+((i-1)*gap)
 
             VGuides.append(res)
 
             VGuides.append(res)
 
             VGuides.append(res+gap)
 
             VGuides.append(res+gap)
 +
        # Build horizontal guides
 
         scribus.setVGuides(VGuides)
 
         scribus.setVGuides(VGuides)
 
         for i in range(1,numRows):  
 
         for i in range(1,numRows):  
Line 73: Line 83:
 
             HGuides.append(res+gap)
 
             HGuides.append(res+gap)
 
         scribus.setHGuides(HGuides)
 
         scribus.setHGuides(HGuides)
       
 
 
         scribus.setUnit(unit) # restore original unit
 
         scribus.setUnit(unit) # restore original unit
       
 
 
     else:
 
     else:
 
         scribus.messageBox(TITLE, noDocMsg, ICON_WARNING, BUTTON_OK)
 
         scribus.messageBox(TITLE, noDocMsg, ICON_WARNING, BUTTON_OK)

Latest revision as of 22:56, 10 November 2008

This article is part of the Scripts series.

This is my first Python script. (Sorry if it is ugly, but my native language is Rebol). It makes a "layout grid" useful for designing newspapers and magazines.

José Antonio Meira da Rocha
joseantoniorocha@gmail.com
http://meiradarocha.jor.br/news/about

#!/usr/bin/env python2.3
# -*- coding: utf-8 -*-
"""Generate a grid of guides for layout porpouses.
Ask user for number of columns, number of rows and gap measure.
"""
import sys

"""Locale strings to be translated """
numColStr1 = 'Número de colunas' #How many columns
numColStr2 = 'Quantas colunas?'  #How many columns?
numLinStr1 = 'Número de linhas'  #How many rows
numLinStr2 = 'Quantas linhas de módulos?' #How many rows?
gapStr1 = 'Largura da valeta' #gap measure
gapStr2 = 'Qual a largura das valetas em pontos?' #gap measure in points?
TITLE = "Gera linhas-guia" #Generate guides
noDocMsg = "<h2>Abra um documento, primeiramente</h2>Ops! Este script só funciona com um documento aberto. <b>Abra um documento</b> antes de rodar o script." # <h2>First, open a doc</h2>This script only work on opened document. <b>Open a document</b> before run this script.
runningMsg = "Rodando o script..." #"Running script..."
noScribusMsg = "This Python script is written for the Scribus scripting interface.\nIt can only be run from within Scribus."

""" Feel free to change defaults"""
defaultNumColumns = '5'
defaultNumRows = '8'
defaultGap = '14.1732' #5mm in points, due a bug in getPageMargins

""" You don't need change nothing below this line """
try:
    import scribus
except ImportError,err:
    print noScribusMsg
    sys.exit(1)
    
from scribus import UNIT_POINTS,BUTTON_OK,ICON_WARNING

def odd(num):
    return num % 2 

def main(argv):
    """Generate guides"""
    if scribus.haveDoc():
        scribus.setRedraw(False)
        numColumns = int(scribus.valueDialog(numColStr1, numColStr2, defaultNumColumns))
        numRows = int(scribus.valueDialog(numLinStr1, numLinStr2, defaultNumRows))
        gap = float(scribus.valueDialog(gapStr1, gapStr2, defaultGap))
        unit = scribus.getUnit()  # save original unit
        scribus.setUnit(UNIT_POINTS) # working in points, due a bug in getPageMargins
        
        pageWidth, pageHeight = scribus.getPageSize()
        # The margins order is wrong in Scripter documentation
        topMargin, leftMargin, rightMargin, bottomMargin = scribus.getPageMargins()
        
        printAreaWidth  = pageWidth  - leftMargin - rightMargin
        printAreaHeight = pageHeight - topMargin  - bottomMargin 
        
        numVgaps = numColumns-1
        numHgaps = numRows-1
        columnWidth = (printAreaWidth-(numVgaps*gap)) / numColumns
        rowHeight = (printAreaHeight-(numHgaps*gap)) / numRows
        
        VGuides = []
        HGuides = []
        # getPageMargins() return only odd page margins. 
        # This correct even margins
        if not odd(scribus.currentPage()):
            leftMargin = rightMargin
        # Build vertical guides
        for i in range(1,numColumns):
            res = leftMargin+(i*columnWidth)+((i-1)*gap)
            VGuides.append(res)
            VGuides.append(res+gap)
        # Build horizontal guides
        scribus.setVGuides(VGuides)
        for i in range(1,numRows): 
            res = topMargin+(i*rowHeight)+((i-1)*gap)
            HGuides.append(res)
            HGuides.append(res+gap)
        scribus.setHGuides(HGuides)
        scribus.setUnit(unit) # restore original unit
    else:
        scribus.messageBox(TITLE, noDocMsg, ICON_WARNING, BUTTON_OK)

def main_wrapper(argv):
    try:
        scribus.statusMessage(runningMsg)
        scribus.progressReset()
        main(argv)
    finally:
        if scribus.haveDoc():
            scribus.setRedraw(True)
        scribus.statusMessage("")
        scribus.progressReset()

if __name__ == '__main__':
    main_wrapper(sys.argv)