diff musiccutter.py @ 17:c50427f1da2d

- Draw vertical time lines. - Draw box around the page. - Refactor text drawing code.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 20 Apr 2016 08:35:44 +0930
parents 20337b22977d
children cf93a76eda92
line wrap: on
line diff
--- a/musiccutter.py	Thu Apr 14 09:48:08 2016 +0930
+++ b/musiccutter.py	Wed Apr 20 08:35:44 2016 +0930
@@ -89,34 +89,20 @@
         print 'Playable count:', playablecount
         print 'Unplayable count:', unplayablecount
 
-        for i in range(len(pdfs)):
-            pdf = pdfs[i]
+        for pindx in range(len(pdfs)):
+            pdf = pdfs[pindx]
             # Add title and page number
-            tobj = pdf.beginText()
-            tobj.setTextOrigin(50 * mm, 1 * mm)
-            tobj.setFont(self.fontname, self.fontsize)
-            tobj.setFillColor(ENGRAVE_COLOUR)
-            tobj.setStrokeColor(ENGRAVE_COLOUR)
-            tobj.textLine('%s (%d / %d)' % (title, i + 1, npages))
-            pdf.drawText(tobj)
+            Midi2PDF.textHelper(pdf, 25 * mm, 1 * mm, ENGRAVE_COLOUR, True, self.fontname, self.fontsize, '%s (%d / %d)' % (title, pindx + 1, npages))
 
-            # Draw rect around page
-            pdf.saveState()
+            pdf.saveState() # Not really necessary since this is last thing we do
             pdf.setLineWidth(0)
-            #pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True)
+
             # Draw lines per note
             for slot in sorted(self.slot2note.keys()):
                 ofs = (self.offset + slot * self.pitch) * mm
                 pdf.line(0, ofs, self.pagewidth * mm, ofs)
                 # Note name
-                tobj = pdf.beginText()
-                tobj.setTextRenderMode(1)
-                tobj.setTextOrigin(0 * mm, ofs + 1 * mm)
-                tobj.setFont(self.fontname, self.fontsize)
-                tobj.setFillColor(ENGRAVE_COLOUR)
-                tobj.setStrokeColor(ENGRAVE_COLOUR)
-                tobj.textLine('%s' % (self.slot2note[slot]))
-                pdf.drawText(tobj)
+                Midi2PDF.textHelper(pdf, 0 * mm, ofs + 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot])
             pdf.restoreState()
             pdf.save()
 
@@ -183,5 +169,19 @@
         pdf.rect(x * mm, y * mm, w * mm, h * mm, fill = False, stroke = True)
         pdf.restoreState()
 
+    @staticmethod
+    def textHelper(pdf, x, y, colour, fill, fontname, fontsize, text):
+        tobj = pdf.beginText()
+        tobj.setTextOrigin(x, y)
+        tobj.setFont(fontname, fontsize)
+        tobj.setStrokeColor(colour)
+        tobj.setFillColor(colour)
+        if fill:
+            tobj.setTextRenderMode(0)
+        else:
+            tobj.setTextRenderMode(1)
+        tobj.textLine(text)
+        pdf.drawText(tobj)
+
 if __name__ == '__main__':
     main()