[scribus-dev] Where is the text frame word wrap code?

Andreas Vox avox at arcor.de
Thu Jun 3 10:38:54 CEST 2010


Hi Drew!

>Von: Drew Vogel [mailto:drewpvogel at gmail.com] 
>Gesendet: Mittwoch, 2. Juni 2010 20:08
>An: scribus-dev at lists.scribus.net
>Betreff: [scribus-dev] Where is the text frame word wrap code?
>
>I am working on an application that will generate a scribus document from a
database of survey >questions. The output of each question will look a bit
like this image: >http://imgur.com/ha2ag.png
>
>The question text is inside the text frame itself. The response options are
in text frames of >their own. In order to determine the appropriate vertical
position for the first response >option, I need to determine where scribus
will wrap the text inside the text frame (the width >of the text frame is
pre-determined). I first thought that I could use
>QFontMetrics::boundingRect() to determine where the text would need to be
wrapped. However >that seems dependent on the DPI of the QPaintDevice that
the text is drawn on and, in my >testing, scribus wraps text independent of
the monitor DPI. Is that true?

Yes, Scribus has ist own text layout code. Unfortunately that's one of the
most ugly parts of Scribus. :-(

>Thus it seems that I need to emulate the word wrap logic in scribus itself
but I'm having >trouble locating that part of the scribus code. It seems
logical to me that the word wrap code >would either be in or called from
PageItem_TextFrame::DrawObj_
>Item() but if it's there, it's not obvious to me. Could someone help me
locate the word wrap >logic?

It's in PageItem_TextFrame::layout(). Unfortunately that particular method
is 1500 lines long, so I'll outline the basic stuff for you.

Without taking into account Dropcaps, Tabs, Optical Margins, Glyph Extension
or changes to Word Spacing, Scribus relies solely on the width of the
characters. After calling PageItem::layoutGlyphs() these are stored in
GlyphLayout.xadvance. The method GlyphLayout::wide() calculates the width in
case the given characters is associated with more than one glyph (that
happens only to pagenumbers and tabs at the moment). After that Scribus just
fills the line with as many words as fit. In case of hyphenation, the
possible hyphenation points are marked with "ScText::effects() |
TextFlag_Hyphenation_Possible" (or something like that).

If you use glyph extension and/or modified word spacing, you can do the
same, but in addition to the break position you get that way, you also have
to check the next break opportunity that normally wouldn't fit on this line.
Then multiply all blank widths with the minimal word spacing and all other
char widths with the minimal glyph extension. If the resulting linewidth is
closer to the wanted linewidth than the linewidth of the original break
position, the new one is taken.

I'm currently rewriting all this code; and in 1.5.0 we will export a
function to scripter that can determine the needed space for a given text.
For now, the above description is all I can offer.

Cheers,
Andreas 




More information about the scribus-dev mailing list