[reportlab-users] Styling within platypus flowables

Robin Becker robin at reportlab.com
Thu Feb 13 08:41:06 EST 2014

On 13/02/2014 10:38, Nick Moffitt wrote:

> Given that we're now clearer on what sort of page layout I'm describing,

> is there any advice on how best to handle this sort of layout with

> platypus? Or should I just stick to writing to the canvas directly, and

> do my own reflowing and line/page breaking by hand?


I don't think paragraphs are going to be much use to you directly. Conceivably a
pre-processor could detect the [X] tags and convert them in to so-called
callback definitions which are inserted into the text of a normal paragraph.

Swing [D]low, sweet [G]chari[D]ot,

can be converted into

Swing <onDraw name="myDrawChord" label="D"/>low, sweet <onDraw
name="myDrawChord" label="G"/>chari<onDraw name="myDrawChord" label="D"/>ot,

where drawChord has to be the name of a callable attribute on the canvas being
used to render the paragraph. When platypus is rendering the paragraph and sees
the representation of the <onDraw> tag it calls out to the callable thusly


here tx is the textobject that's being used to render the para. So effectively
the onDraw callable has to have a signature like this

def myDrawChord(canv,kind,label):
canv is our canvas
kind == 'onDraw'
label = whatever label was set in the paragraph onDraw label attribute
#extract positional information
_curr_tx_info = canv._curr_tx_info
tx = _curr_tx_info['tx']
cur_x = _curr_tx_info['cur_x']
cur_y = _curr_tx_info['cur_y']
leading = _curr_tx_info['leading']
xs = _curr_tx_info['xs']

#do whatever you want in terms of drawing the marks etc etc

The XtraState thing xs has these elements

xs.cur_y at start of rendering (I think)
xs.f fragment
xs.style the style object we're using
xs.underlines list of underline positions
xs.strikes list of strike positions
xs.links list of link positions
xs.link f.link
xs.backColors list of backgrounds to be painted

for most purposes you should only need the cur_x/cur_y from the tx rendering
info, _curr_tx_info. Then I guess you need to make the leading large enough to
allow drawing of the label above or below the original text line.

I'm not sure exactly how the splitting will work with this scheme as our
fragment algorithm is quite contorted about where it likes to split when these
zero width (onDraw) items appear ie they're not transparent so far as the
linebreaking algoritm is concerned.
Robin Becker

More information about the reportlab-users mailing list