def draw(self, cr, layout, width, dpi_x, dpi_y):
from gi.repository import Gtk, Gdk
img_width = self._width * dpi_x / 2.54
img_height = self._height * dpi_y / 2.54
if self._style == 'right':
l_margin = width - img_width
elif self._style == 'center':
l_margin = (width - img_width) / 2.0
else:
l_margin = 0
# load the image and get its extents
pixbuf = resize_to_buffer(self._filename, [img_width, img_height],
self._crop)
pixbuf_width = pixbuf.get_width()
pixbuf_height = pixbuf.get_height()
# calculate the scale to fit image into the set extents
scale = min(img_width / pixbuf_width, img_height / pixbuf_height)
# draw the image
cr.save()
cr.translate(l_margin, 0)
cr.scale(scale, scale)
Gdk.cairo_set_source_pixbuf(cr, pixbuf,
(img_width / scale - pixbuf_width) / 2,
(img_height / scale - pixbuf_height) / 2)
cr.rectangle(0 , 0, img_width / scale, img_height / scale)
##gcr.set_source_pixbuf(pixbuf,
##(img_width - pixbuf_width) / 2,
##(img_height - pixbuf_height) / 2)
##cr.rectangle(0 , 0, img_width, img_height)
##cr.scale(scale, scale)
cr.fill()
cr.restore()
if DEBUG:
cr.set_line_width(0.1)
cr.set_source_rgb(1.0, 0, 0)
cr.rectangle(l_margin, 0, img_width, img_height)
cr.stroke()
return (img_height)
评论列表
文章目录