self._doc.SaveToFile(self._filename, self)
class PageCount(platypus.Flowable):
+ def __init__(self, story_count=0):
+ platypus.Flowable.__init__(self)
+ self.story_count = story_count
+
def draw(self):
- self.canv.beginForm("pageCount")
+ self.canv.beginForm("pageCount%d" % (self.story_count))
self.canv.setFont("Helvetica", utils.unit_get(str(8)))
self.canv.drawString(0, 0, str(self.canv.getPageNumber()))
self.canv.endForm()
class PageReset(platypus.Flowable):
def draw(self):
- self.canv._pageNumber = 0
+ self.canv._doPageReset = True
class _rml_styles(object,):
def __init__(self, nodes, localcontext):
if n.tag == 'pageCount':
if x or y:
self.canvas.translate(x,y)
- self.canvas.doForm('pageCount')
+ self.canvas.doForm('pageCount%s' % (self.canvas._storyCount,))
if x or y:
self.canvas.translate(-x,-y)
if n.tag == 'pageNumber':
ActionFlowable.__init__(self,('frameEnd',resume))
class TinyDocTemplate(platypus.BaseDocTemplate):
+
+ def beforeDocument(self):
+ # Store some useful value directly inside canvas, so it's available
+ # on flowable drawing (needed for proper PageCount handling)
+ self.canv._doPageReset = False
+ self.canv._storyCount = 0
+
def ___handle_pageBegin(self):
self.page = self.page + 1
self.pageTemplate.beforeDrawPage(self.canv,self)
self.frame = f
break
self.handle_frameBegin()
- def afterFlowable(self, flowable):
- if isinstance(flowable, PageReset):
- self.canv._pageCount=self.page
- self.page=0
- self.canv._flag=True
+
+ def afterPage(self):
+ if self.canv._doPageReset:
+ # Following a <pageReset/> tag:
+ # - we reset page number to 0
+ # - we add an new PageCount flowable (relative to the current
+ # story number), but not for NumeredCanvas at is handle page
+ # count itself)
+ # NOTE: _rml_template render() method add a PageReset flowable at end
+ # of each story, so we're sure to pass here at least once per story.
+ if not isinstance(self.canv, NumberedCanvas):
+ self.handle_flowable([ PageCount(story_count=self.canv._storyCount) ])
+ self.canv._pageCount = self.page
+ self.page = 0
+ self.canv._flag = True
self.canv._pageNumber = 0
+ self.canv._doPageReset = False
+ self.canv._storyCount += 1
class _rml_template(object):
def __init__(self, localcontext, out, node, doc, images=None, path='.', title=None):
self.doc_tmpl.afterFlowable(fis)
self.doc_tmpl.build(fis,canvasmaker=NumberedCanvas)
else:
- fis.append(PageCount())
self.doc_tmpl.build(fis)
def parseNode(rml, localcontext=None, fout=None, images=None, path='.', title=None):