[reportlab-users] Hebrew Support Patch
Moshe Wagner
moshe.wagner at gmail.com
Fri Jun 5 03:02:57 EDT 2009
Thanks for your corrections.
As I said, I'm new to python, so I didn't realize you could use loops that way.
This line:
"if type(line) in (FragLine, ParaLines):"
didn't seem to work, as type(line) always returned "instance", so I
kept it the way I had it.
Another thing I did wrong is implementing '.reverse()' manually, not
realizing it exists.
So here's the new code. Clearly looks 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 line in blPara.lines:
if line.__class__.__name__ in ('FragLine', 'ParaLines'):
"""When the line is a FragLine or ParaLines, It's
text attribute of each of it's words 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 """
for word in line.words:
word.text = pyfribidi.log2vis (word.text, base_direction = pyfribidi.ON)
line.words.reverse()
elif line.__class__.__name__ == 'tuple':
"""When the line is just a tuple whose second value is the text.
since I coulden't directly change it's value,
it's done by merging the words, flipping them,
and re-entering them one by one to the second attribute """
s = ' '.join(line[1])
s = pyfribidi.log2vis( s, base_direction=pyfribidi.ON)
line[1][:] = s.split()
else:
print line.__class__.__name__
#print self.blPara.lines[i][1][1:]
except ImportError:
print "Fribidi module not found; You will not have rtl support for
this paragraph"
######################
Is there any chance this could be added to the official code?
Moshe
On Thu, Jun 4, 2009 at 11:36 PM, Tim Roberts<timr at probo.com> wrote:
> 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.
>
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
>
More information about the reportlab-users
mailing list