Adjust a text frame to fit its content

From Scribus Wiki
Revision as of 21:38, 31 January 2009 by Ale (talk | contribs)
Jump to: navigation, search

adjust the size of existing text frames

<script>

  1. !/usr/bin/env python

""" this script adjust the height of a text frame to exactly fit its content, but not be longer than the page margin @author: alessandro rimoldi @version: 1.0 / 20090131 @copyright (c) 2009 alessandro rimoldi under the mit license

          http://www.opensource.org/licenses/mit-license.html

""" import sys try:

  import scribus

except ImportError:

  print "This script only works from within Scribus"
  sys.exit(1)
  1. check that the selection is one text frame and get that frame

frame_n = scribus.selectionCount() if frame_n == 0 :

   scribus.messageBox('Error:', 'No frame selected');
   sys.exit(1)

elif frame_n > 1 :

   scribus.messageBox('Error:', 'You may select only one frame');
   sys.exit(1)

frame = scribus.getSelectedObject(0) try:

   char_n = scribus.getTextLength(frame)

except scribus.WrongFrameTypeError:

   scribus.messageBox('Error:', 'You may only adjust text frames');
   sys.exit(1)

if char_n == 0 :

   scribus.messageBox('Error:', 'You can\'t adjust an empty frame');
   sys.exit(1)
  1. get some page and frame measure

(x, ph) = scribus.getPageSize(); (x, x, x, pm) = scribus.getPageMargins(); # warning: gets the margins from document setup not for the current page

(x, y) = scribus.getPosition(frame)

bottom = (ph - pm) - y

(w, h) = scribus.getSize(frame)

  1. if the frame doesn't overflow, shorten it to make it overflow

while (((scribus.textOverflows(frame) == 0) or (h > bottom)) and (h > 0)) :

   h -= 10
   scribus.sizeObject(w, h, frame)
  1. resize the frame in 10pt steps

while ((scribus.textOverflows(frame) > 0) and (h < bottom)) :

   h += 10
   scribus.sizeObject(w, h, frame)
  1. undo the latest 10pt step and fine adjust in 1pt steps

h -= 10 scribus.sizeObject(w, h, frame)

while ((scribus.textOverflows(frame) > 0) and (h < bottom)) :

   h += 1
   scribus.sizeObject(w, h, frame)

</script>


import a text at the right size

  • read a text file
  • filter it line by line
  • put it in a frame
  • fit the height of the frame to the length of its content
#!/usr/bin/env python
import sys
try:
   import scribus
except ImportError:
   print "This script only works from within Scribus"
   sys.exit(1)


# read the content of the file line by line into the text frame
file_name = 'mytext.txt'
file_name = 'abc.txt'
file = open(file_name, 'r')

text = "";

for line in open(file_name, 'r') :
    if line != '\n':
        # put here some text handling/filtering
        text += unicode(line, 'iso-8859-2')

# create a text frame and put the text into it
x = 60
y = 100
w = 300
h = 10

frame = scribus.createText(x, y, w, h)
scribus.setText(text, frame)

# resize the frame in 10pt steps
while (scribus.textOverflows(frame) > 0) :
    h += 10
    scribus.sizeObject(w, h, frame)

# undo the latest 10pt step and fine adjust in 1pt steps
h -= 10
scribus.sizeObject(w, h, frame)

while (scribus.textOverflows(frame) > 0) :
    h += 1
    scribus.sizeObject(w, h, frame)