Bug Report - Figure Class with wrong alignment

11 views
Skip to first unread message

Roberto Rebello

unread,
Aug 29, 2024, 2:10:40 PMAug 29
to reportlab-users
Hi,

I don't know it this is how patches should be suggested, so please tell me of there is a better way.

I was creating a subclass of FlexFigure and was having problems while trying to use center alignment. The problem is on Figure's draw function. It is translating the canvas to, what it believes to be, the center of the frame, but an previous translation has already occurred on the call to drawOn.

If I understood the documentations correctly, the draw function of Flowables should not be responsible for canvas translation, just rendering.

This will be called to ask the flowable to actually render itself. The Flowable class does not implement draw. The calling code should ensure that the flowable has an attribute canv which is the pdfgen. Canvas which should be drawn to an that the Canvas is in an appropriate state (as regards translations rotations, etc). Normally this method will only be called internally by the drawOn method. Derived classes must implement this method.

The problem can be demonstrated by running the test function already available on the figures.py file.

The fix would be to remove the calculations of dx from Figure's wrap function and the canvas translation based on dx from Figure's draw function. This way both TA_CENTER and TA_RIGHT will work as expected.

def wrap(self, availWidth, availHeight):
# try to get the caption aligned
if self.caption:
self._getCaptionPara()
w, h = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
self.captionHeight = h + self.captionGap
self.height = self.captionHeight + self.figureHeight
if w>self.width: self.width = w
else:
self.height = self.figureHeight
return (self.width, self.height)

def draw(self):
if self.caption and self.captionPosition=='bottom':
self.canv.translate(0, self.captionHeight)
if self.background:
self.drawBackground()
if self.border:
self.drawBorder()
self.canv.saveState()
self.drawFigure()
self.canv.restoreState()
if self.caption:
if self.captionPosition=='bottom':
self.canv.translate(0, -self.captionHeight)
else:
self.canv.translate(0, self.figureHeight+self.captionGap)
self._getCaptionPara()
self.drawCaption()

Let me know if I failed to be clear on explaining the problem and the solution.

Cheers,

Roberto.

Reply all
Reply to author
Forward
0 new messages