本文整理汇总了Python中cairo.SVGSurface方法的典型用法代码示例。如果您正苦于以下问题:Python cairo.SVGSurface方法的具体用法?Python cairo.SVGSurface怎么用?Python cairo.SVGSurface使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo
的用法示例。
在下文中一共展示了cairo.SVGSurface方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_svg
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def export_svg(self, filename, size=None):
"""
Saves the contents of the widget to svg file. The size of the image
will be the size of the widget.
@type filename: string
@param filename: The path to the file where you want the chart to be saved.
@type size: tuple
@param size: Optional parameter to give the desired height and width of the image.
"""
if size is None:
rect = self.get_allocation()
width = rect.width
height = rect.height
else:
width, height = size
old_alloc = self.get_allocation
self.get_allocation = lambda: gtk.gdk.Rectangle(0, 0, width, height)
surface = cairo.SVGSurface(filename, width, height)
ctx = cairo.Context(surface)
context = pangocairo.CairoContext(ctx)
self.draw(context)
surface.finish()
if size is not None:
self.get_allocation = old_alloc
示例2: start
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def start(self, fn, w, h,):
self.fn = fn
if OUTPUT_FMT == 'PNG' or not fn:
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
int(w + 1), int(h + 1))
elif OUTPUT_FMT == 'SVG':
self.surface = cairo.SVGSurface(self.fn, int(w + 1), int(h + 1))
elif OUTPUT_FMT == 'PDF':
self.surface = cairo.PDFSurface(self.fn, int(w + 1), int(h + 1))
elif OUTPUT_FMT == 'PS':
self.surface = cairo.PSSurface(self.fn, int(w + 1), int(h + 1))
else:
raise AttributeError("no such output format: '%s'" % OUTPUT_FMT)
return self.surface
示例3: create_svg
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def create_svg(text, output_path, **kwargs):
"""Creates an SVG image from the given text."""
setup_fonts_conf()
params = make_drawparams(**kwargs)
temp_surface = cairo.SVGSurface(None, 0, 0)
calculated_height = draw_on_surface(temp_surface, text, params)
real_surface = cairo.SVGSurface(output_path, params.width, calculated_height)
print("writing", output_path)
draw_on_surface(real_surface, text, params)
real_surface.flush()
real_surface.finish()
示例4: render
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def render(self, canvas, transparent=False):
x0,y0,x1,y1 = canvas.bbox('all')
self.markers = canvas.markers
W = int((x1 - x0 + 2*self.padding) * self.scale)
H = int((y1 - y0 + 2*self.padding) * self.scale)
ext = os.path.splitext(self.fname)[1].lower()
if ext == '.svg':
surf = cairo.SVGSurface(self.fname, W, H)
elif ext == '.pdf':
surf = cairo.PDFSurface(self.fname, W, H)
elif ext in ('.ps', '.eps'):
surf = cairo.PSSurface(self.fname, W, H)
if ext == '.eps':
surf.set_eps(True)
else: # Bitmap
surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, W, H)
self.ctx = cairo.Context(surf)
ctx = self.ctx
if not transparent:
# Fill background
ctx.rectangle(0,0, W,H)
ctx.set_source_rgba(1.0,1.0,1.0)
ctx.fill()
ctx.scale(self.scale, self.scale)
ctx.translate(-x0 + self.padding, -y0 + self.padding)
if self.draw_bbox:
last = len(canvas.shapes)
for s in canvas.shapes[:last]:
bbox = s.bbox
r = canvas.create_rectangle(*bbox, line_color=(255,0,0, 127), fill=(0,255,0,90))
for s in canvas.shapes:
self.draw_shape(s)
if ext in ('.svg', '.pdf', '.ps', '.eps'):
surf.show_page()
else:
surf.write_to_png(self.fname)
示例5: _take_screenshot
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def _take_screenshot(self, dummy_button):
#print "Cheese!"
file_name = self._get_export_file_name()
if file_name is None:
return
# figure out the correct bounding box for what is visible on screen
x1 = self._scrolled_window.get_hadjustment().value
y1 = self._scrolled_window.get_vadjustment().value
x2 = x1 + self._scrolled_window.get_hadjustment().page_size
y2 = y1 + self._scrolled_window.get_vadjustment().page_size
bounds = goocanvas.Bounds()
bounds.x1, bounds.y1 = self.canvas.convert_from_pixels(x1, y1)
bounds.x2, bounds.y2 = self.canvas.convert_from_pixels(x2, y2)
dest_width = bounds.x2 - bounds.x1
dest_height = bounds.y2 - bounds.y1
#print bounds.x1, bounds.y1, " -> ", bounds.x2, bounds.y2
dummy, extension = os.path.splitext(file_name)
extension = extension.lower()
if extension == '.eps':
surface = cairo.PSSurface(file_name, dest_width, dest_height)
elif extension == '.pdf':
surface = cairo.PDFSurface(file_name, dest_width, dest_height)
elif extension == '.svg':
surface = cairo.SVGSurface(file_name, dest_width, dest_height)
else:
dialog = gtk.MessageDialog(parent = self.canvas.get_toplevel(),
flags = gtk.DIALOG_DESTROY_WITH_PARENT,
type = gtk.MESSAGE_ERROR,
buttons = gtk.BUTTONS_OK,
message_format = "Unknown extension '%s' (valid extensions are '.eps', '.svg', and '.pdf')"
% (extension,))
dialog.run()
dialog.destroy()
return
# draw the canvas to a printing context
cr = cairo.Context(surface)
cr.translate(-bounds.x1, -bounds.y1)
self.canvas.render(cr, bounds, self.zoom.value)
cr.show_page()
surface.finish()
示例6: _save
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, str):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
raise ValueError("Unknown format: {!r}".format(fmt))
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()
示例7: _save
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, str):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
warnings.warn("unknown format: %s" % fmt, stacklevel=2)
return
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()
示例8: _save
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import SVGSurface [as 别名]
def _save(self, fo, fmt, **kwargs):
# save PDF/PS/SVG
orientation = kwargs.get('orientation', 'portrait')
dpi = 72
self.figure.dpi = dpi
w_in, h_in = self.figure.get_size_inches()
width_in_points, height_in_points = w_in * dpi, h_in * dpi
if orientation == 'landscape':
width_in_points, height_in_points = (
height_in_points, width_in_points)
if fmt == 'ps':
if not hasattr(cairo, 'PSSurface'):
raise RuntimeError('cairo has not been compiled with PS '
'support enabled')
surface = cairo.PSSurface(fo, width_in_points, height_in_points)
elif fmt == 'pdf':
if not hasattr(cairo, 'PDFSurface'):
raise RuntimeError('cairo has not been compiled with PDF '
'support enabled')
surface = cairo.PDFSurface(fo, width_in_points, height_in_points)
elif fmt in ('svg', 'svgz'):
if not hasattr(cairo, 'SVGSurface'):
raise RuntimeError('cairo has not been compiled with SVG '
'support enabled')
if fmt == 'svgz':
if isinstance(fo, six.string_types):
fo = gzip.GzipFile(fo, 'wb')
else:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
else:
warnings.warn("unknown format: %s" % fmt)
return
# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(width_in_points, height_in_points)
renderer.set_ctx_from_surface(surface)
ctx = renderer.gc.ctx
if orientation == 'landscape':
ctx.rotate(np.pi / 2)
ctx.translate(0, -height_in_points)
# Perhaps add an '%%Orientation: Landscape' comment?
self.figure.draw(renderer)
ctx.show_page()
surface.finish()
if fmt == 'svgz':
fo.close()