[scribus] centering text vertically in a frame

Gregory Pittman gpittman at iglou.com
Thu Jul 5 01:58:49 UTC 2012


Here is a script I wrote which accomplishes this task, albeit with some 
limitations.
The main one is that you must be using fixed linespacing. This seems to 
be because when you use the getLineSpacing(), Scribus has no idea you 
are using anything else.

The script works by getting the linespacing, the number of lines of 
text, the height of the frame, then using 1/2 the difference between the 
frame height and total space taken by lines, resets the Top distance.

The other curious thing I haven't figured out is that when the script 
runs it dutifully makes the new Top distance, but the text stays put. I 
couldn't figure out any command in Scripter that would fix this. What I 
did find out is that if you save the file, close it, and reload, the 
text has moved to the middle. But it's not just a display issue, since 
if you save to PDF before fixing in some way, the PDF shows the text 
unshifted.
I did find other workarounds, such as setting the Line Color or Fill 
Color to something else, then back to where it was, then the frame is 
redrawn and the text moves like it's should have.
Also, clicking the Edit Shape button in Properties, then closing fixed it.

In the end, the simplest thing to do which works is to just run the 
script again, in which case THEN the text is shifted. (weird)

So, try it out, find other ways it doesn't work (I'm sure if you have 
mixed up fonts in the frame it won't do a very good job then either).

Greg

*****script below*****
#!/usr/bin/env python
# -*- coding: utf-8  -*-
# centervert.py

try:
     import scribus
except ImportError:
     print "Unable to import the 'scribus' module. This script will only 
run within"
     print "the Python interpreter embedded in Scribus. Try 
Script->Execute Script."
     sys.exit(1)

if not scribus.haveDoc():
     scribus.messageBox('Scribus - Script Error', "No document open", 
scribus.ICON_WARNING, scribus.BUTTON_OK)
     sys.exit(1)

if scribus.selectionCount() == 0:
     scribus.messageBox('Scribus - Script Error',
             "There is no object selected.\nPlease select a text frame 
and try again.",
             scribus.ICON_WARNING, scribus.BUTTON_OK)
     sys.exit(2)
if scribus.selectionCount() > 1:
     scribus.messageBox('Scribus - Script Error',
             "You have more than one object selected.\nPlease select one 
text frame and try again.",
             scribus.ICON_WARNING, scribus.BUTTON_OK)
     sys.exit(2)
textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
scribus.setRedraw(False)

for item in pageitems:
     if (item[0] == textbox):
         if (item[1] != 4):
             scribus.messageBox('Scribus - Script Error',
                           "This is not a textframe. Try again.", 
scribus.ICON_WARNING, scribus.BUTTON_OK)
             sys.exit(2)

         lines = scribus.getTextLines(textbox)
         distances = scribus.getTextDistances(textbox)
         linespace = scribus.getLineSpacing(textbox)
         dimensions = scribus.getSize(textbox) # (width, height)
         if (scribus.textOverflows(textbox, 1) > 0):
             scribus.messageBox('User Error', "This frame is already 
overflowing", scribus.ICON_WARNING, scribus.BUTTON_OK)
             sys.exit(2)
         newtopdist = (dimensions[1] - linespace * lines)/2
scribus.setTextDistances(distances[0],distances[1],newtopdist,distances[3],textbox)

scribus.setRedraw(True)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scribus.net/pipermail/scribus/attachments/20120704/3ce1750c/attachment.html>


More information about the scribus mailing list