Source code for quill.exporter.cairo_surface
"""
Draw to a Cairo surface
"""
import cairo
from quill.exporter.base2 import ExporterBase2
[docs]class CairoSurface(ExporterBase2):
def __init__(self, surface):
self._surface = surface
[docs] def begin_export(self):
self._first_page = True
cr = self._context = cairo.Context(self._surface)
cr.set_line_cap(cairo.LINE_CAP_ROUND)
cr.set_line_join(cairo.LINE_JOIN_ROUND)
[docs] def end_export(self):
if not self._first_page:
self._context.show_page()
self._surface.flush()
[docs] def new_page(self, page):
if not self._first_page:
self._context.show_page()
self._first_page = False
_pen_scale_factor = float(5.0/0.003)
[docs] def stroke(self, stroke):
cr = self._context
cr.set_source_rgb(*stroke.rgb())
points = stroke.points()
if stroke.has_pressure():
# width changes, each segment is its own stroke
for i in xrange(len(points) - 1):
p0 = points[i]
p1 = points[i+1]
cr.set_line_width(stroke.thickness()
/ self._pen_scale_factor
* (p0[2] + p1[2])/2)
cr.move_to(p0[0], p0[1])
cr.line_to(p1[0], p1[1])
cr.stroke()
else:
# constant width, join all segment into one stroke
cr.set_line_width(stroke.thickness() / self._pen_scale_factor)
for i in xrange(len(points) - 1):
p0 = points[i]
p1 = points[i+1]
cr.move_to(p0[0], p0[1])
cr.line_to(p1[0], p1[1])
cr.stroke()
[docs] def image(self, image):
cr = self._context