Example #1
0
 def render_slide(self, cr, cr_width, cr_height, slide):
     """Renders the slide C{current_slide} onto the given Cairo context C{cr}."""
     
     cr.set_source_rgb(0.0, 0.0, 0.0)
     cr.paint()
     
     if slide.startswith('img::'):
         img_filename = slide.split('img::')[-1].strip()
         if not os.path.isabs(img_filename):
             img_filename = os.path.join(self.base_dir, img_filename)
         
         # load image surface
         image_surface = None
         if img_filename == '.png':
             try:
                 # causes MemoryError if filename doesn't point to a PNG
                 image_surface = imageloader.image_surface_with_cairo(img_filename)
             except MemoryError:
                 image_surface = imageloader.image_surface_with_pil(img_filename)
         else:
             image_surface = imageloader.image_surface_with_pil(img_filename)
             
         # get geometry info
         iw, ih = image_surface.get_width(), image_surface.get_height()
         # scale factor and translation to zoom to center of image
         sf = min(float(cr_width) / iw, float(cr_height) / ih)    # scale factor
         tx = (cr_width - sf * iw) / 2    # translate x
         ty = (cr_height - sf * ih) / 2   # translate y
         
         # paint image
         cr.push_group()
         cr.translate(tx, ty)
         cr.scale(sf, sf)
         cr.set_source_surface(image_surface, 0, 0)
         cr.paint()
         cr.pop_group_to_source()
         cr.paint()
         
     else:
         # render some text (w/ pango)
         pc = pangocairo.CairoContext(cr)
         layout = pc.create_layout()
         layout.set_font_description(pango.FontDescription("Yanone Tagesschrift %d" % int(cr_height/20.)))
         layout.set_markup(slide)
         layout.set_alignment(pango.ALIGN_CENTER)
         layout.set_spacing(int(1./25 * cr_height * pango.SCALE))
         ink_rect, logical_rect = layout.get_pixel_extents()
         tx, ty, tw, th = logical_rect
         cr.move_to((cr_width - tw)/2, (cr_height - th)/2)
         cr.set_source_rgb(1.0, 1.0, 1.0)
         pc.show_layout(layout)
Example #2
0
 def render_slide(cls, cr, cr_width, cr_height, slide):
     """Renders the slide C{current_slide} onto the given Cairo context C{cr}."""
     
     # load image surface
     image_surface = None
     if slide[0][-4:].lower() == '.png':
         try:
             # causes MemoryError if filename doesn't point to a PNG
             image_surface = imageloader.image_surface_with_cairo(slide[0])
         except MemoryError:
             image_surface = imageloader.image_surface_with_pil(slide[0])
     else:
         image_surface = imageloader.image_surface_with_pil(slide[0])
         
     # get geometry info
     iw, ih = image_surface.get_width(), image_surface.get_height()
     # scale factor and translation to zoom to center of image
     sf = max(float(cr_width) / iw, float(cr_height) / ih)    # scale factor
     tx = (cr_width - sf * iw) / 2    # translate x
     ty = (cr_height - sf * ih) / 2   # translate y
     
     # paint image
     cr.push_group()
     cr.translate(tx, ty)
     cr.scale(sf, sf)
     cr.set_source_surface(image_surface, 0, 0)
     cr.paint()
     cr.pop_group_to_source()
     cr.paint()
     
     # render some text (w/ pango)
     pc = pangocairo.CairoContext(cr)
     layout = pc.create_layout()
     layout.set_font_description(pango.FontDescription("Yanone Kaffeesatz Bold %d" % int(cr_height/12.)))
     layout.set_text(slide[1])
     layout.set_indent(int(.03 * cr_width * pango.SCALE))
     layout.set_spacing(int(1./60 * cr_height * pango.SCALE))
     ink_rect, logical_rect = layout.get_pixel_extents()
     cr.rectangle(0, int(.06 * cr_height),
                  int(logical_rect[2] + .06 * cr_width),
                  int(logical_rect[3] + .05 * cr_height)
                  )
     cr.set_source_rgba(.90, .90, .90, .5)
     cr.fill()
     cr.move_to(0, int(.085 * cr_height)) # rect y_offset + rect_margin/2
     cr.set_source_rgb(0.0, 0.0, 0.0)
     pc.show_layout(layout)