[reportlab-users] Long word in paragraph in table cell

Simon Metson s.metson at bristol.ac.uk
Thu Aug 30 22:49:54 EDT 2007


Hello,


>> If I were doing it, I'd probably write my own paragraph class derived

>> from Paragraph, and check for these long words by hand. It

>> wouldn't be

>> easy, but it could be done.

>

> Ok, will see what I can come up with. Is there an easy way to work

> out how long the string is once it becomes rendered? The wrapOn

> seems to do it, guess I could make a (fake) canvas object the size

> I want the HyphenatedParagraph to fill and wrap the text with that.

> Will give that a go tonight...


FYI, I've done this by subclassing the Paragraph object as
HyphenatedParagraph and overriding the breakLines method.
HyphenatedParagraph take an additional argument to its constructor,
which gets stored as self.blockwidth (the width of the area the text
should fill).
The over ridden method is essentially the same I just replaced the
for loop at line 681-699 (for word in words:) with the following:

newwords = []
for word in words:
#Make a new word array, splitting long words
wordWidth = stringWidth(word, fontName, fontSize,
self.encoding)

#Hyphenate long words in the paragraph to fit an
area of width self.blockwidth
charwidth = int(ceil(wordWidth / len(word)))
charinline = int(floor(self.blockwidth / charwidth))

for i in range(0, 1 + int(ceil(self.width /
self.blockwidth))):
newwords.append(word[i * charinline:(i + 1) *
charinline])

for word in newwords:
newWidth = currentWidth + spaceWidth + wordWidth
if newWidth <= maxWidth or not len(cLine):
# fit one more on this line
cLine.append(word)
currentWidth = newWidth
else:
if currentWidth > self.width: self.width =
currentWidth
#end of line
lines.append((maxWidth - currentWidth, cLine))
cLine = [word]
currentWidth = wordWidth
lineno += 1
try:
maxWidth = maxWidths[lineno]
except IndexError:
maxWidth = maxWidths[-1] # use the last one


I also had to import split, strip and _handleBulletWidth from
reportlab.platypus.paragraph, as well as import ceil and floor from
math. There were a load of other imports, copied from Paragraph, but
I don't think I need them all, will clean them tomorrow. I think this
could be simplified further by using the wordSplit method (maybe
quicker??) in reportlab.lib.textsplit - again something for tomorrow.
It would be trivial to add in a hyphenating character ('-', '_' etc)
but for me a new line is fine. I guess it should also be modified for
the other cases in the breaklines method, but this works for what I'm
doing, if people wanted to use it fully...
Cheers
Simon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20070831/cc3414af/attachment-0001.html>


More information about the reportlab-users mailing list