[reportlab-users] Hebrew Support Patch
Tim Roberts
timr at probo.com
Thu Jun 4 16:36:57 EDT 2009
Moshe Wagner wrote:
> After using the code I've written some more, I found a few bugs.
> So this should be the code for - paragraph.h, in the 'wrap' function:
>
> Still isn't written so well, but should work better:
> ######################
> # Hebrew text patch, Moshe Wagner, June 2009
> # <moshe.wagner at gmail.com>
>
>
> #This code fixes paragraphs with RTL text
>
> # It does it by flipping each line seperatly.
> # (Depending on the type of the line)
>
> # If fribidi cant be imported, it does nothing
>
> try:
> import pyfribidi
>
> for i in range(len(blPara.lines)):
> if blPara.lines[i].__class__.__name__ == FragLine.__name__ \
> or blPara.lines[i].__class__.__name__ == ParaLines.__name__:
> for j in range(len(blPara.lines[i].words)):
> """When the line is a FragLine or ParaLines, It's
> text attribute is flipped.
> Then, the order of the words is flipped too,
> So that 2 word parts on the same line
> will be in the right order """
>
> s = blPara.lines[i].words[j].text
> s = pyfribidi.log2vis (s, base_direction = pyfribidi.ON)
> blPara.lines[i].words[j].text = s
>
Much of this can by simplified, and made easier to read (and a bit more
efficient) by using a style this:
try:
import pyfribidi
for line in blPara.lines:
if type(line) in (FragLine, ParaLines):
for word in line.words:
s = pyfribidi.log2vis( word.text,
base_direction=pyfribidi.ON)
word.text = s
...
elif type(line) == tuple:
s = ' '.join(line[1])
s = pyfribidi.log2vis( s, base_direction=pyfribidi.ON)
line[1][:] = s.split()
else:
print type(line)
Almost any time you have "for i in range(line(k))", you should think
about it as "for i in k" instead.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the reportlab-users
mailing list