本文整理匯總了Python中cairocffi.Context方法的典型用法代碼示例。如果您正苦於以下問題:Python cairocffi.Context方法的具體用法?Python cairocffi.Context怎麽用?Python cairocffi.Context使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cairocffi
的用法示例。
在下文中一共展示了cairocffi.Context方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: generate_image_temporal
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def generate_image_temporal(x_norm, x_diff_encoded, scale, stroke_width,
steepness=2, inversed=False):
surface = cairo.ImageSurface(cairo.FORMAT_A8, scale, scale)
curr_x = x_norm[0][0]
curr_y = x_norm[0][1]
spent_t = 0
for i, diff in enumerate(x_diff_encoded):
ctx = cairo.Context(surface)
ctx.move_to(curr_x * scale, curr_y * scale)
ctx.set_line_width(stroke_width)
weight = 1 - spent_t / x_norm[-1][2]
if inversed: weight = 1 - weight
weight = max(weight, 0) ** steepness
if diff[3] == 0: weight /= 2
ctx.set_source_rgba(1, 1, 1, weight)
ctx.line_to((curr_x + diff[0]) * scale, (curr_y + diff[1]) * scale)
ctx.stroke()
curr_x += diff[0]
curr_y += diff[1]
spent_t += diff[2]
return surface_to_array(surface).reshape(scale, scale, 1)
示例2: setupCairo
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def setupCairo(self, outputFormat='png'):
self.outputFormat = outputFormat
if outputFormat == 'png':
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self.width, self.height)
elif outputFormat == 'svg':
self.surfaceData = BytesIO()
self.surface = cairo.SVGSurface(self.surfaceData,
self.width, self.height)
elif outputFormat == 'pdf':
self.surfaceData = BytesIO()
self.surface = cairo.PDFSurface(self.surfaceData,
self.width, self.height)
res_x, res_y = self.surface.get_fallback_resolution()
self.width = float(self.width / res_x) * 72
self.height = float(self.height / res_y) * 72
self.surface.set_size(self.width, self.height)
self.ctx = cairo.Context(self.surface)
示例3: _to_context
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def _to_context(ctx):
if not isinstance(ctx, cairo.Context):
ctx = cairo.Context._from_pointer(
cairo.ffi.cast(
'cairo_t **',
id(ctx) + object.__basicsize__)[0],
incref=True)
return ctx
示例4: __init__
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def __init__(self, dpi):
self.dpi = dpi
self.gc = GraphicsContextCairo(renderer=self)
self.text_ctx = cairo.Context(
cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1))
self.mathtext_parser = MathTextParser('Cairo')
RendererBase.__init__(self)
示例5: set_ctx_from_surface
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def set_ctx_from_surface(self, surface):
self.gc.ctx = cairo.Context(surface)
# Although it may appear natural to automatically call
# `self.set_width_height(surface.get_width(), surface.get_height())`
# here (instead of having the caller do so separately), this would fail
# for PDF/PS/SVG surfaces, which have no way to report their extents.
示例6: main
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def main(hexagon_size, imgsize):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, imgsize, imgsize)
ctx = cairo.Context(surface)
# we will put the center of the hexagon at the origin
a, b, c = hexagon_size
ctx.translate(imgsize / 2.0, imgsize / 2.0)
extent = max(c, a * HALFSQRT3, b * HALFSQRT3) + 1
ctx.scale(imgsize / (extent * 2.0), -imgsize / (extent * 2.0))
ctx.translate(-b * HALFSQRT3, -c / 2.0)
# paint background
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
T = LozengeTiling(hexagon_size)
sample = run_cftp(T)
for key, val in T.get_tiles(sample).items():
for verts in val:
A, B, C, D = square_to_hex(verts)
ctx.move_to(A[0], A[1])
ctx.line_to(B[0], B[1])
ctx.line_to(C[0], C[1])
ctx.line_to(D[0], D[1])
ctx.close_path()
if key == "T":
ctx.set_source_rgb(*TOP_COLOR)
elif key == "L":
ctx.set_source_rgb(*LEFT_COLOR)
else:
ctx.set_source_rgb(*RIGHT_COLOR)
ctx.fill_preserve()
ctx.set_line_width(LINE_WIDTH)
ctx.set_source_rgb(*EDGE_COLOR)
ctx.stroke()
surface.write_to_png("random_lozenge_tiling.png")
示例7: main
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def main():
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
fractal_tree(ctx, ITERATIONS, ROOT, TRUNK_LEN,
RATIO, THETA, ANGLE, PERTURB)
surface.write_to_png("random_fractal_tree.png")
示例8: __init__
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def __init__(self, dpi):
"""
"""
if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))
self.dpi = dpi
self.gc = GraphicsContextCairo (renderer=self)
self.text_ctx = cairo.Context (
cairo.ImageSurface (cairo.FORMAT_ARGB32,1,1))
self.mathtext_parser = MathTextParser('Cairo')
RendererBase.__init__(self)
示例9: set_ctx_from_surface
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def set_ctx_from_surface (self, surface):
self.gc.ctx = cairo.Context (surface)
示例10: __init__
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def __init__(self,
page,
offset_x=0,
offset_y=0,
rotation=0):
"""
A thin wrapper to produce vector graphics using cairo. This class provides a drawing context that we can use to
draw a figure onto a page.
:param page:
The GraphicsPage we are going to draw onto
:param offset_x:
The offset of this drawing from (0,0) on the page
:param offset_y:
The offset of this drawing from (0,0) on the page
:param rotation:
The rotation of this drawing
"""
assert isinstance(page, GraphicsPage)
self.base_line_width = line_width_base
self.base_font_size = font_size_base
self.font_size = False
self.font_bold = False
self.font_italic = False
self.line_dotted = False
self.context = cairo.Context(target=page.surface)
self.context.scale(sx=page.dots_per_metre, sy=page.dots_per_metre)
self.context.translate(tx=offset_x, ty=offset_y)
self.context.rotate(radians=rotation * unit_deg)
self.set_line_width(line_width=1)
self.set_font_style()
self.set_font_size(1)
self.context.set_fill_rule(fill_rule=cairo.FILL_RULE_EVEN_ODD)
示例11: scribe_text
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def scribe_text(text, font_style,
width, height,
x_offset, y_offset,
rotation):
fmt = cairocffi.FORMAT_A8
width = cairocffi.ImageSurface.format_stride_for_width(fmt, width)
data = array.array('b', [0] * (height * width))
surface = cairocffi.ImageSurface(fmt, width, height, data, width)
# pangocairo.pango_cairo_set_antialias(cairocffi.ANTIALIAS_SUBPIXEL)
context = cairocffi.Context(surface)
context.translate(x_offset, y_offset)
context.rotate(rotation)
layout = gobject_ref(pangocairo.pango_cairo_create_layout(context._pointer))
pango.pango_layout_set_text(layout, text.encode('utf8'), -1)
font_desc = pango.pango_font_description_from_string(font_style.encode('utf8'))
pango.pango_layout_set_font_description(layout, font_desc)
# pango.pango_layout_set_spacing(spc * 32)
pangocairo.pango_cairo_update_layout(context._pointer, layout)
pangocairo.pango_cairo_show_layout(context._pointer, layout)
# print(surface.get_width(), surface.get_height())
return np.frombuffer(data, dtype=np.uint8).reshape((height, width))
示例12: render
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def render(self, scale=109, margin=1, line_width=0.35/25.4,
bounds=None, show_bounds=True):
if cairo is None:
raise Exception('Drawing.render() requires cairo')
bounds = bounds or self.bounds
x1, y1, x2, y2 = bounds
w = x2 - x1
h = y2 - y1
margin *= scale
width = int(scale * w + margin * 2)
height = int(scale * h + margin * 2)
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
dc = cairo.Context(surface)
dc.set_line_cap(cairo.LINE_CAP_ROUND)
dc.set_line_join(cairo.LINE_JOIN_ROUND)
dc.translate(margin, margin)
dc.scale(scale, scale)
dc.translate(-x1, -y1)
dc.set_source_rgb(1, 1, 1)
dc.paint()
if show_bounds:
dc.set_source_rgb(0.5, 0.5, 0.5)
dc.set_line_width(1 / scale)
dc.rectangle(x1, y1, w, h)
dc.stroke()
dc.set_source_rgb(0, 0, 0)
dc.set_line_width(line_width)
for path in self.paths:
dc.move_to(*path[0])
for x, y in path:
dc.line_to(x, y)
dc.stroke()
return surface
示例13: generate_image_direct_decomp
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def generate_image_direct_decomp(x_norm, x_diff_encoded, scale, stroke_width):
"""Generates image array from strokes using direction feature.
Args:
x_norm: [(x', y', t), ...] Normalized points.
x_diff_encoded: [(dx, dy, dt, pendown), ...] Normalized diffs.
scale (int): scale of the image.
stroke_width (int): Brush thickness to draw.
Returns:
image (numpy.array): Image array with a shape of (scale, scale, 8).
"""
surfaces = [cairo.ImageSurface(cairo.FORMAT_A8, scale, scale)
for _ in range(8)]
curr_x = x_norm[0][0]
curr_y = x_norm[0][1]
for i, diff in enumerate(x_diff_encoded):
direction1, weight1, direction2, weight2 = get_direction(diff)
ctx = cairo.Context(surfaces[direction1])
ctx.move_to(curr_x * scale, curr_y * scale)
ctx.set_line_width(stroke_width)
ctx.set_source_rgba(1, 1, 1, weight1)
ctx.line_to((curr_x + diff[0]) * scale, (curr_y + diff[1]) * scale)
ctx.stroke()
ctx = cairo.Context(surfaces[direction2])
ctx.move_to(curr_x * scale, curr_y * scale)
ctx.set_line_width(stroke_width)
ctx.set_source_rgba(1, 1, 1, weight2)
ctx.line_to((curr_x + diff[0]) * scale, (curr_y + diff[1]) * scale)
ctx.stroke()
curr_x += diff[0]
curr_y += diff[1]
return np.array([
surface_to_array(surface) for surface in surfaces]).transpose(1, 2, 0)
示例14: generate_image_plain
# 需要導入模塊: import cairocffi [as 別名]
# 或者: from cairocffi import Context [as 別名]
def generate_image_plain(x_norm, x_diff_encoded, scale, stroke_width):
"""Generates image array from strokes without direction feature.
Args:
x_norm: [(x', y', t), ...] Normalized points.
x_diff_encoded: [(dx, dy, dt, pendown), ...] Normalized diffs.
scale (int): scale of the image.
stroke_width (int): Brush thickness to draw.
Returns:
image (numpy.array): Image array with a shape of (scale, scale, 1).
"""
surface = cairo.ImageSurface(cairo.FORMAT_A8, scale, scale)
curr_x = x_norm[0][0]
curr_y = x_norm[0][1]
for i, diff in enumerate(x_diff_encoded):
ctx = cairo.Context(surface)
ctx.move_to(curr_x * scale, curr_y * scale)
ctx.set_line_width(stroke_width)
if diff[3] == 1:
ctx.set_source_rgba(1, 1, 1, 1)
else:
ctx.set_source_rgba(1, 1, 1, 0.5)
ctx.line_to((curr_x + diff[0]) * scale, (curr_y + diff[1]) * scale)
ctx.stroke()
curr_x += diff[0]
curr_y += diff[1]
return surface_to_array(surface).reshape(scale, scale, 1)