Commits

Anonymous committed 3973fa7

important: split pdf writing into two templates, one for the title page, another for later pages

Comments (0)

Files changed (9)

notalonlib/pdf/writer.py

 from reportlab.platypus.frames import Frame
 from reportlab.rl_config import defaultPageSize
 
-# Globally register Droid Serif family of fonts
-registerFont(TTFont("Droid", os.path.join('res', "DroidSerif-Regular.ttf")))
-registerFont(TTFont("Droid-B", os.path.join('res', "DroidSerif-Bold.ttf")))
-registerFont(TTFont("Droid-I", os.path.join('res', "DroidSerif-Italic.ttf")))
-registerFont(TTFont("Droid-BI", os.path.join('res', "DroidSerif-BoldItalic.ttf")))
-fonts.addMapping('Droid', 0, 0, 'Droid')
-fonts.addMapping('Droid', 0, 1, 'Droid-I')
-fonts.addMapping('Droid', 1, 0, 'Droid-B')
-fonts.addMapping('Droid', 1, 1, 'Droid-BI')
-fonts._ps2tt_map.update({'Droid': ('Droid', 0, 0)})
+
+# Globally register Gentium family of fonts
+registerFont(TTFont("Gentium", os.path.join('res', "GenBkBasR.ttf")))
+registerFont(TTFont("Gentium-B", os.path.join('res', "GenBkBasB.ttf")))
+registerFont(TTFont("Gentium-I", os.path.join('res', "GenBkBasI.ttf")))
+registerFont(TTFont("Gentium-BI", os.path.join('res', "GenBkBasBI.ttf")))
+fonts.addMapping('Gentium', 0, 0, 'Gentium')
+fonts.addMapping('Gentium', 0, 1, 'Gentium-I')
+fonts.addMapping('Gentium', 1, 0, 'Gentium-B')
+fonts.addMapping('Gentium', 1, 1, 'Gentium-BI')
+fonts._ps2tt_map.update({'Gentium': ('Gentium', 0, 0)})
 
 registerFont(TTFont("Optimus-Princeps-SemiBold", os.path.join('res', "OptimusPrincepsSemiBold.ttf")))
 
 TITLE_FONTNAME = "Optimus-Princeps-SemiBold"
-HEAD_FONTNAME = "Times-Roman" # either "Times-Roman" or "Droid"
+HEAD_FONTNAME = "Gentium" # either "Times-Roman" or "Gentium"
 BODY_FONTNAME = HEAD_FONTNAME
 
 PAGE_HEIGHT = defaultPageSize[1]
 PAGE_WIDTH = defaultPageSize[0]
 
+def _doNothing(canvas, doc):
+    """Dummy callback for page template.
+
+    """
+    pass
+
 def bullet(text):
     """Prefix a unicode bullet to the text.
 
         add the default page template (:meth:`.myPageTemplate`).
 
         """
-        self.x1 = .5 * inch
-        self.x2 = 7.5 * inch
-        self.y1 = .7 * inch
-        self.y2 = 10 * inch
+        firstLeftMargin = 0.5 * inch
+        firstBottomMargin = 0.5 * inch
+        firstWidth = 7.5*inch
+        firstHeight = 10.2*inch
 
-        textframe = Frame(self.x1, self.y1, self.x2, self.y2, id = 'TF', showBoundary=drawborders)
+        laterLeftMargin = firstLeftMargin
+        laterBottomMargin = firstBottomMargin
+        laterWidth = firstWidth
+        laterHeight = firstHeight + 0.45*inch # slightly more room, since there's no title anymore
+
+        firstFrame = Frame(firstLeftMargin, firstBottomMargin, firstWidth, firstHeight, id = 'firstFrame', showBoundary=drawborders)
+        laterFrame = Frame(laterLeftMargin, laterBottomMargin, laterWidth, laterHeight, id = 'laterFrame', showBoundary=drawborders)
         
-        self.allowSplitting = 1
-        apply(BaseDocTemplate.__init__,(self,filename),kw)
+        apply(BaseDocTemplate.__init__, (self,filename), kw)
 
         self.title = title
         self.author = author
         self.fontsize = fontsize
 
-        self.addPageTemplates(PageTemplate("Normal", textframe, onPage=self.myPageTemplate))
+        self.addPageTemplates([PageTemplate("First", firstFrame, onPage=self.firstPageTemplate), 
+                               PageTemplate("Later", laterFrame, onPage=self.laterPageTemplate)])
 
-    def myPageTemplate(self, canvas, doc):
-        """Default simple Notalon page template.
+    def handle_pageBegin(self):
+        """Override base to add a change of page template after the first page.
+
+        """
+        self._handle_pageBegin()
+        self._handle_nextPageTemplate('Later')
+
+    def firstPageTemplate(self, canvas, doc):
+        """Default Notalon title page template.
 
         Draws headings on the left and corresponding text to the right, a title
-        at the top center and name at the top right.
+        at the top center, with name and date at the top right.
 
         """
         PAGE_WIDTH, PAGE_HEIGHT = canvas._pagesize
         now = datetime.datetime.now()
         when = "%s %d, %s" % (now.strftime("%B"), now.day, now.strftime("%Y"))
         canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - 60 - self.fontsize, when)
+
+        canvas.setFont(TITLE_FONTNAME, self.fontsize)
+        canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" % doc.page)
+        
+        # Set the document info
+        # Viewable in File Properties from the PDF viewer
+        canvas.setAuthor(self.author)
+        canvas.setTitle(self.title)
+        canvas.setSubject("Notalon document")
+
+        canvas.restoreState()
+
+    def build(self, flowables, firstPageTemplate=_doNothing, laterPageTemplate=_doNothing):
+        """Custom build method to separate first page and later page templates.
+
+        """
+        self._calc() # in case we changed margin sizes
+
+        if firstPageTemplate is _doNothing and hasattr(self, 'firstPageTemplate'):
+            self.pageTemplates[0].beforeDrawPage = self.firstPageTemplate
+        if laterPageTemplate is _doNothing and hasattr(self, 'laterPageTemplate'):
+            self.pageTemplates[1].beforeDrawPage = self.laterPageTemplate
+
+        BaseDocTemplate.build(self, flowables)
+
+    def getFrameSize(self):
+        return ((self.x1, self.y1), (self.x2, self.y2))
+
+    def laterPageTemplate(self, canvas, doc):
+        """Default Notalon non-title page template.
+
+        Draws headings on the left and corresponding text to the right.
+
+        """
+        PAGE_WIDTH, PAGE_HEIGHT = canvas._pagesize
+        canvas.saveState()
+
+        canvas.setFillColorRGB(0, 0, 0)
         canvas.setFont(TITLE_FONTNAME, self.fontsize)
         canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" % doc.page)
         

res/DroidSerif-Bold.ttf

Binary file removed.

res/DroidSerif-BoldItalic.ttf

Binary file removed.

res/DroidSerif-Italic.ttf

Binary file removed.

res/DroidSerif-Regular.ttf

Binary file removed.

res/GenBkBasB.ttf

Binary file added.

res/GenBkBasBI.ttf

Binary file added.

res/GenBkBasI.ttf

Binary file added.

res/GenBkBasR.ttf

Binary file added.