[reportlab-users] RE: hard splitting a paragraph

Henning von Bargen H.vonBargen at t-p.com
Fri Apr 7 03:41:03 EDT 2006


Hello Dick,

this should go in the FAQ.
The paragraph implementation does not support hyphenation,
not even simple splitting of too long words.

You might check a project I once started, called deco-cow.
See http://deco-cow.sourceforge.net. However I do not
maintain it any more. Someone else is maintaining it now;
he wanted to improve it but I think there hasn't been any
update to the sourceforge page.

deco-cow contains a modified version of the paragraph Module
which support hyphenation and word-breaking - this is what you want.

The basic idea for the integration of deco-cow with ReportLab
is quite simple - see the example test_hyphenation.py:
* You assign a language code to a paragraph, i.e. "DE" or "NL".
  To achieve this, there are two new attributes for paragraphs:
    language: default None,
    hyphenation: default False
* You assign a hyphenator instance to each language code.
  For German text, you might find the SiSiSiHyphenator more or less
  useful.
  For English text, you can also use the PyHnjHyphenator - this might
  also work for other languages, but it crashes Python for German :-(
  PyHnjHyphenator uses the generic hyphenation algorithm that is used 
  in LaTex (I think - but I'm not quite sure anymore).
  For all other languages, i.e. Dutch, you can use the BaseHyphenator
  as a fallback.
  The BaseHyphenator tries to hyphenate at "-" or the shy hyphenation
  character (thats code 160: like a minus sign, but usually not visible)
  and some other points that make sense.

Thats all YOU have to do.

The magic is then done in the modified version of paragraph.py together
with the Hyphenator instance:

The modified version of the Paragraph class has a more sophisticated
concept for line-breaking than that used in the standard ReportLab code.
Basically, if the next word does not fit in the current line any more,
three approaches are tested:
- SQUEEZE the text a little to make it fit (decrease inter-word spacing).
- STRETCH the text a little to make it fit (increase inter-word spacing).
- HYPHENATE the word using the language and the corresponding Hyphenator
            instance.
If all of the above fail, then
- if the line is still empty (<=> the next word would be the 
  ONLY ONE in the current line <=> the word is too long to fit on a
  single line), then make a HARD-BREAK - just put some characters 
  into the current line and the rest into the next.
- otherwise just start a new line.

Just give it a try!

HTH Henning

> Date: Thu, 6 Apr 2006 17:18:33 +0200
> From: Dick Kniep <dick at kniep.nl>
> Subject: [reportlab-users] hard splitting a paragraph
> To: reportlab-users at reportlab.com
> Message-ID: <200604061718.33549.dick at kniep.nl>
> 
> Hi list,
> 
> We have developed a system that generates reports from Python 
> lists. This 
> works nicely, however, sometimes a field is longer than the 
> designated room 
> in a column, and spills over to the next column. This only 
> happens if there 
> is no white space between words and the word itself is too 
> big. If there is 
> white space, the line is split correctly, and a new line is printed.
> 
> In fact I want a flowable that splits the lines according to 
> the normal rules 
> of the paragraph, but if there is too little room, splits it 
> on a certain 
> point no matter what.
> 
> Anyone any idea how to do this?
> 
> Thanks in advance.
> 
> Cheers,
> Dick Kniep
> 
> 



More information about the reportlab-users mailing list