Matthew Turk avatar Matthew Turk committed 44db04f

Adding PDF writing support. Moving surface generation into an abstract
property.

Comments (0)

Files changed (5)

examples/hello_world.py

         ])
     presentation.append(s4)
 
-output = backends.PNGWriter("output/slide_%04i.png")
+#output = backends.PNGWriter("output/slide_%04i.png")
+output = backends.PDFWriter("output/output.pdf")
 output.interactor = EnterKeyPresser()
 presentation.display(output)

vanhoek/backends/api.py

 from .backend_base import BackendBase
 from .png_writer import PNGWriter
+from .pdf_writer import PDFWriter

vanhoek/backends/backend_base.py

 
     interactor = None
 
+    @abc.abstractproperty
+    def surface(self):
+        pass
+
     @abc.abstractmethod
     def draw(self, slide, i):
         if self.interactor is not None: self.interactor.wait(i)
-        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1024, 768)
+        surface = self.surface
         context = cairo.Context(surface)
         slide.render(context)
         return surface

vanhoek/backends/pdf_writer.py

+import cairo
+from .backend_base import BackendBase
+
+class PDFWriter(BackendBase):
+    _surface = None
+    def __init__(self, filename = "output.pdf"):
+        self.filename = filename
+
+    def draw(self, slide, i):
+        surface = BackendBase.draw(self, slide, i)
+        surface.flush()
+
+    @property
+    def surface(self):
+        if self._surface is None:
+            self._surface = cairo.PDFSurface(self.filename, 1024, 768)
+        else:
+            self._surface.show_page()
+        return self._surface

vanhoek/backends/png_writer.py

+import cairo
 from .backend_base import BackendBase
 
 class PNGWriter(BackendBase):
     def draw(self, slide, i):
         surface = BackendBase.draw(self, slide, i)
         surface.write_to_png(self.format_string % i)
+
+    @property
+    def surface(self):
+        return cairo.ImageSurface(cairo.FORMAT_ARGB32, 1024, 768)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.