[reportlab-users] Fwd: ReportLab problems with CJK

Robin Becker robin at reportlab.com
Wed Feb 16 12:00:48 EST 2011

I've created a small example using roman alphabet and that seems to indicate
that you're right. However, your fix allows the loop to be much cheaper so I
rewrote as

> if endLine:

> extraSpace = maxWidth - widthUsed

> if not lineBreak:

> #This is the most important of the Japanese typography rules.

> #if next character cannot start a line, wrap it up to this line so it hangs

> #in the right margin. We won't do two or more though - that's unlikely and

> #would result in growing ugliness.

> #otherwise we need to push the character back

> #and increase the extra space

> #bug fix contributed by Alexander Vasilenko <alexs.vasilenko at gmail.com>

> if u not in ALL_CANNOT_START:

> i -= 1

> extraSpace += w

> lines.append(makeCJKParaLine(U[lineStartPos:i],extraSpace,calcBounds))

> try:

> maxWidth = maxWidths[len(lines)]

> except IndexError:

> maxWidth = maxWidths[-1] # use the last one


> lineStartPos = i

> widthUsed = 0

which seems to work with this example

from reportlab.lib import textsplit
textsplit.ALL_CANNOT_START += u'M'
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.enums import TA_LEFT

sty = ParagraphStyle('A')
sty.fontSize = 15
sty.leading = sty.fontSize*1.2
sty.fontName = 'Courier'
sty.alignment = TA_LEFT
sty.wordWrap = 'CJK'
p1=Paragraph('AB<font color="red">C</font>DEFGHIJKLMN',sty)

aW = pdfmetrics.stringWidth('ABCD','Courier',15)

canv = Canvas('tcjk.pdf')

On 16/02/2011 15:33, Alexandr Vasilenko wrote:

> Hi Robin,


> Unfortunately, I don't read/speak Japanese too. Just was fixing a bug with

> overlapped characters.

> I think i -= 1 should be added, because on that step the last character is

> out of width already, and extraSpace is corrected by extraSpace += w

> operation (this mean removing the latest character). But pointer wasn't

> changed (i variable is a pointer in this case)


> Image we have ABCD cjk characters, each has width=5, let maxwidth=12.

> If you'll use such example you will see that is will be splitted as:

> ABC (total width=15 instead of maxwidth=12)

> D

> instread of:

> AB (total width=10, which is good)

> CD


> I hope that described clearly the issue.


> Regards,

> Alex


Robin Becker

More information about the reportlab-users mailing list