[scribus] Get the width of a piece of text in a Scribus script?

Adam adam at photosynth.ca
Sat Aug 10 20:04:05 UTC 2013

Thanks for your replies Gregory and Owen! For my application the 
position of the chords needs to be fairly precise, because as the 
project goes through different versions we'll be making changes to the 
database content and I don't want to have to tweak the scribus file any 
more than absolutely necessary.

I've been through scripter documentation fairly thoroughly (I think). 
The key thing that's missing is a way to resize the text frame to match 
the amount of text inside it.

I've found a terribly inefficient workaround for this: I create a very 
narrow text frame and add the appropriately styled text to be measured 
to it using createText(), setFont(), setFontSize() and insertText(). 
Then I incrementally increase the text frame size (by deleting and 
replacing it with a new slightly larger frame) until textOverflows() 
shows that there is no longer any text overflowing the frame. This gives 
me the size of that piece of styled text, which allows placing the chord 
text frame accordingly. Of course this is very inefficient, and seems to 
take several seconds with each snippet of text. I'll be able to speed it 
up a lot with a little bit of heuristic processing (such as starting 
with what Gregory suggested), but it remains to be seen if it will be 
too slow to use in the actual application or not!


On 09/08/2013 5:58 PM, Gregory Pittman wrote:
> On 08/09/2013 06:43 PM, Adam wrote:
>> Good day,
>> I'm working on a script for laying out a song book that includes 
>> lyrics and chords. Chord names need to be placed above each line of 
>> lyrics, at the appropriate spot in the lyrics. I've explored a 
>> variety of approaches to this using typographic options to try to get 
>> the chord letters bumped up above the lyrics line using baseline 
>> shift and kerning adjustments, but so far these have not proved 
>> effective.
>> My present approach is to put each chord in a small text box above 
>> the lyrics line. In order to do this, I need to be able to determine 
>> the horizontal position of that point in the lyrics, as rendered in 
>> the relevant font and type size. Does anyone know of a way to do this?
>> Apparently the ImageFont python module (which is a subset of PIL, or 
>> the Python Imaging Library), can return the width of a text line as 
>> rendered, but I so far have not been able to get this module working 
>> properly (there's a C library involved, which is malfunctioning in my 
>> installation).
>> Does anyone know of any other way to programatically measure the 
>> rendered width of a line of text?
> Here is a fragment from an old script* I wrote for importing pictures 
> from a directory, then creating text frames which contained the file 
> pathname.
>                     Lpiclen = int(5.3 * lenfilename)
> # Lpic is the label for each picture, with position and length adjusted
> # according to the text length, so if you change the font or its size,
> # you may need to adjust this only approximate calculation.
>                     Lpic = scribus.createText(x, y + 193, Lpiclen, 15)
> After some experimenting, I came up with this simple formula of 5.3 
> times the number of characters to approximate the length in points 
> needed for the text frame -- here my constraint was that it had to be 
> long enough (too short was unacceptable, a little bit too long was 
> Ok), so I needed to err a bit on the long side. I considered something 
> more elegant, like considering each letter and allowing for relative 
> sizes of letters, but for this purpose it wasn't worth the work.
> I think you're looking for something that works pretty well most of 
> the time, and then being left with a few scattered instances where you 
> tweak the results. You might even be surprised at how simple a method 
> you can come up with. Python of course has a number of commands for 
> analyzing and manipulating strings, and presumably you're dealing with 
> what amounts to lines of text.
> Greg
> * 
> http://wiki.scribus.net/canvas/Automatic_import_of_images:_Versions_not_requiring_Tkinter
> ___
> Scribus Mailing List: scribus at lists.scribus.net
> Edit your options or unsubscribe:
> http://lists.scribus.net/mailman/listinfo/scribus
> See also:
> http://wiki.scribus.net
> http://forums.scribus.net

More information about the scribus mailing list