2

I use MWLIB and ReportLab to convert MediaWiki articles to PDF.

I got this really long link that for whatever reason causes the sentence above to have really long spaces between the words. I think the link makes such a long word that it just draws out the sentence above.

See the picture here: http://imageshack.us/photo/my-images/543/tzfo.png/

Is there anyway to force word breaking on words longer than a certain set of characters in ReportLab? I think that would fix it.

PS; Here's some code:

The method def breakLinesCJK() in reportlab/paragraph.py. It uses the method wordSplit() from reportlab.lib.textsplit.py

def breakLinesCJK(self, width):
    """Initially, the dumbest possible wrapping algorithm.
    Cannot handle font variations."""

    if not isinstance(width,(list,tuple)): maxWidths = [width]
    else: maxWidths = width
    style = self.style
    self.height = 0

    #for bullets, work out width and ensure we wrap the right amount onto line one
    _handleBulletWidth(self.bulletText, style, maxWidths)
    frags = self.frags
    nFrags = len(frags)
    if nFrags==1 and not hasattr(frags[0],'cbDefn'):
        f = frags[0]
        if hasattr(self,'blPara') and getattr(self,'_splitpara',1):
            return f.clone(kind=0, lines=self.blPara.lines)
        #single frag case
        lines = []
        lineno = 0
        if hasattr(f,'text'):
            text = f.text
        else:
            text = ''.join(getattr(f,'words',[]))

        print "USE WORDSPLIT ELSE TREPORTLAB EXT = '',JOIN"

        from reportlab.lib.textsplit import wordSplit
        lines = wordSplit(text, maxWidths[20], f.fontName, f.fontSize)
        #the paragraph drawing routine assumes multiple frags per line, so we need an
        #extra list like this
        #  [space, [text]]
        #
        wrappedLines = [(sp, [line]) for (sp, line) in lines]
        return f.clone(kind=0, lines=wrappedLines, ascent=f.fontSize, descent=-0.2*f.fontSize)
    elif nFrags<=0:
        return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
                        textColor=style.textColor, lines=[],ascent=style.fontSize,descent=-0.2*style.fontSize)

    #general case nFrags>1 or special
    if hasattr(self,'blPara') and getattr(self,'_splitpara',0):
        return self.blPara
    autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
    calcBounds = autoLeading not in ('','off')
    return cjkFragSplit(frags, maxWidths, calcBounds)

The code in textplit.py is also significant but it's just too much too copy, but just like paragraph, anyone with reportlab should have this file.

user1531921
  • 1,372
  • 4
  • 18
  • 36
  • 1
    There are ways, both built in and custom-built, to handle word wrapping in ReportLab, but without any code of what you're doing it's hard to recommend how to proceed. – Gordon Seidoh Worley Aug 09 '13 at 13:56
  • I use the script files that comes with ReportLab and MWLIB. I would have to show you all of them for it to be any help, unless you could tell me a specific script file you'd want to see. I just wanted to know if there's any built-in variable you can change to make the word-breaking more aggressive. – user1531921 Aug 12 '13 at 07:52
  • Or more specifically, I just want to know whether there is any way of forcing words longer than a certain amount of characters to break so I can fix the problem in the picture. Anyway, I updated my main post with some code from scripts I think are significant. – user1531921 Aug 12 '13 at 08:11

0 Answers0