[reportlab-users] Truncate table cell content to end with ellipsis

Robin Becker robin at reportlab.com
Thu Apr 16 09:35:14 EDT 2009


Yves,

if you cannot use the broken fragline stuff to locate the position where the
text should be ended then you'll need to brute force the search. A good approach
uses a binary search to determine where the split should occur eg

from reportlab.pdfbase.pdfmetrics import stringWidth
def locateSplit(s,maxWidth,fontName='Times-Roman',fontSize=12):
def width(i,L=(len(s)+1)*[None]):
if L[i] is None:
L[i]=stringWidth(s[:i],fontName,fontSize)
return L[i]
def brak(lo,hi):
i = (lo+hi)>>1
print lo,hi,i,width(i)
if lo==i: return lo
if width(i)>=maxWidth:
return brak(lo,i)
else:
return brak(i,hi)
i=brak(0,len(s))
if width(i)>maxWidth:
i-=1
return i, width(i),width(i+1),s[:i]

s='Hello Brave World and Goodbye'
print locateSplit(s,72).
--
Robin Becker


More information about the reportlab-users mailing list