本文整理汇总了Python中cairo.Context.set_source_surface方法的典型用法代码示例。如果您正苦于以下问题:Python Context.set_source_surface方法的具体用法?Python Context.set_source_surface怎么用?Python Context.set_source_surface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo.Context
的用法示例。
在下文中一共展示了Context.set_source_surface方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spiral
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
y0 = H//2 + (random.uniform()-.1)*50
for dx,dy in spiral():
c = .25+.75*random.random()
x = int(x0+dx)
y = int(y0+dy)
checked = False
I.flush()
if not (x <= w//2 or y <= h//2 or x >= (W-w//2) or y >= (H-h//2)):
ndI = ndarray(shape=(h,w), buffer=I.get_data(), dtype=ubyte, order='C',
offset=(x-w//2) + I.get_stride() * (y-h//2),
strides=[I.get_stride(), 1])
ndL = ndarray(shape=(h,w), buffer=L.get_data(), dtype=ubyte, order='C',
strides=[L.get_stride(), 1])
if ((ndI * ndL).sum() == 0):
checked = True
new_region = RectangleInt(x-w//2, y-h//2, w, h)
if (checked or ( drawn_regions.contains_rectangle(new_region) == REGION_OVERLAP_OUT )):
ctxI.set_source_surface(L, 0, 0)
pattern = ctxI.get_source()
scalematrix = Matrix()
scalematrix.scale(1.0,1.0)
scalematrix.translate(w//2 - x, h//2 - y)
pattern.set_matrix(scalematrix)
ctxI.set_source_rgba(c,c,c,c)
ctxI.mask(pattern)
drawn_regions.union(new_region)
break
I.flush()
I.write_to_png("wordle-cairo.png")
Image.open("wordle-cairo.png").show()
示例2: make_label
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
def make_label(text, filename, size=12, angle=0):
'''
Parameters:
-----------
text : string
Text to be displayed
filename : string
Path to a font
size : int
Font size in 1/64th points
angle : float
Text angle in degrees
'''
face = Face(filename)
face.set_char_size( size*64 )
# FT_Angle is a 16.16 fixed-point value expressed in degrees.
angle = FT_Angle(angle * 65536)
matrix = FT_Matrix( FT_Cos( angle ),
- FT_Sin( angle ),
FT_Sin( angle ) ,
FT_Cos( angle ) )
flags = FT_LOAD_RENDER
pen = FT_Vector(0,0)
FT_Set_Transform( face._FT_Face, byref(matrix), byref(pen) )
previous = 0
xmin, xmax = 0, 0
ymin, ymax = 0, 0
for c in text:
face.load_char(c, flags)
kerning = face.get_kerning(previous, c)
previous = c
bitmap = face.glyph.bitmap
pitch = face.glyph.bitmap.pitch
width = face.glyph.bitmap.width
rows = face.glyph.bitmap.rows
top = face.glyph.bitmap_top
left = face.glyph.bitmap_left
pen.x += kerning.x
x0 = (pen.x >> 6) + left
x1 = x0 + width
y0 = (pen.y >> 6) - (rows - top)
y1 = y0 + rows
xmin, xmax = min(xmin, x0), max(xmax, x1)
ymin, ymax = min(ymin, y0), max(ymax, y1)
pen.x += face.glyph.advance.x
pen.y += face.glyph.advance.y
L = ImageSurface(FORMAT_A8, xmax-xmin, ymax-ymin)
previous = 0
pen.x, pen.y = 0, 0
ctx = Context(L)
for c in text:
face.load_char(c, flags)
kerning = face.get_kerning(previous, c)
previous = c
bitmap = face.glyph.bitmap
pitch = face.glyph.bitmap.pitch
width = face.glyph.bitmap.width
rows = face.glyph.bitmap.rows
top = face.glyph.bitmap_top
left = face.glyph.bitmap_left
pen.x += kerning.x
x = (pen.x >> 6) - xmin + left
y = - (pen.y >> 6) + ymax - top
if (width > 0):
glyph_surface = make_image_surface(face.glyph.bitmap)
ctx.set_source_surface(glyph_surface, x, y)
ctx.paint()
pen.x += face.glyph.advance.x
pen.y += face.glyph.advance.y
L.flush()
return L
示例3: ndarray
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
offset=2,
strides=[pitch, 3])
try:
ndI = ndarray(shape=(rows,width), buffer=I.get_data(),
dtype=uint32, order='C',
strides=[I.get_stride(), 4])
except NotImplementedError:
raise SystemExit("For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)")
# 255 * 2**24 = opaque
ndI[:,:] = 255 * 2**24 + ndR[:,:] * 2**16 + ndG[:,:] * 2**8 + ndB[:,:]
I.mark_dirty()
surface = ImageSurface(FORMAT_ARGB32, 800, 600)
ctx = Context(surface)
ctx.set_source_surface(I, 0, 0)
pattern = ctx.get_source()
SurfacePattern.set_filter(pattern, FILTER_BEST)
scale = 480.0 / rows
scalematrix = Matrix()
scalematrix.scale(1.0/scale,1.0/scale)
scalematrix.translate(-(400.0 - width *scale /2.0 )+200, -60)
pattern.set_matrix(scalematrix)
ctx.paint()
# we need this later for shifting the taller LCD_V glyph up.
rows_old = rows
# LCD_V
face.load_char('S', FT_LOAD_RENDER |
FT_LOAD_TARGET_LCD_V )
示例4: RuntimeError
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
raise RuntimeError('pitch != width * 4 for color bitmap: Please report this.')
bitmap = np.array(bitmap.buffer, dtype=np.uint8).reshape((bitmap.rows,bitmap.width,4))
I = ImageSurface(FORMAT_ARGB32, width, rows)
try:
ndI = np.ndarray(shape=(rows,width), buffer=I.get_data(),
dtype=np.uint32, order='C',
strides=[I.get_stride(), 4])
except NotImplementedError:
raise SystemExit("For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)")
# Although both are 32-bit, cairo is host-order while
# freetype is small endian.
ndI[:,:] = bitmap[:,:,3] * 2**24 + bitmap[:,:,2] * 2**16 + bitmap[:,:,1] * 2**8 + bitmap[:,:,0]
I.mark_dirty()
surface = ImageSurface(FORMAT_ARGB32, 2*width, rows)
ctx = Context(surface)
ctx.set_source_surface(I, 0, 0)
ctx.paint()
ctx.set_source_surface(I, width/2, 0)
ctx.paint()
ctx.set_source_surface(I, width , 0)
ctx.paint()
surface.write_to_png("emoji-color-cairo.png")
Image.open("emoji-color-cairo.png").show()
示例5: make_image_surface
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
F = make_image_surface(bitmap)
# Draw
surface = ImageSurface(FORMAT_ARGB32, 1200, 500)
ctx = Context(surface)
# fill background as gray
ctx.rectangle(0,0,1200,500)
ctx.set_source_rgb (0.5 , 0.5, 0.5)
ctx.fill()
# use the stroked font's size as scale, as it is likely slightly larger
scale = 400.0 / rowsZ
# draw bitmap first
ctx.set_source_surface(F, 0, 0)
patternF = ctx.get_source()
SurfacePattern.set_filter(patternF, FILTER_BEST)
scalematrix = Matrix()
scalematrix.scale(1.0/scale,1.0/scale)
scalematrix.translate(-(600.0 - widthF *scale /2.0 ), -50)
patternF.set_matrix(scalematrix)
ctx.set_source_rgb (1 , 1, 0)
ctx.mask(patternF)
ctx.fill()
scalematrix.translate(+400,0)
patternF.set_matrix(scalematrix)
ctx.mask(patternF)
ctx.fill()
示例6: max
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
baseline = max(baseline, max(0,-(slot.bitmap_top-bitmap.rows)))
kerning = face.get_kerning(previous, c)
width += (slot.advance.x >> 6) + (kerning.x >> 6)
previous = c
Z = ImageSurface(FORMAT_A8, width, height)
ctx = Context(Z)
# Second pass for actual rendering
x, y = 0, 0
previous = 0
for c in text:
face.load_char(c)
bitmap = slot.bitmap
top = slot.bitmap_top
left = slot.bitmap_left
w,h = bitmap.width, bitmap.rows
y = height-baseline-top
kerning = face.get_kerning(previous, c)
x += (kerning.x >> 6)
# cairo does not like zero-width bitmap from the space character!
if (bitmap.width > 0):
glyph_surface = make_image_surface(face.glyph.bitmap)
ctx.set_source_surface(glyph_surface, x, y)
ctx.paint()
x += (slot.advance.x >> 6)
previous = c
Z.flush()
Z.write_to_png("hello-world-cairo.png")
Image.open("hello-world-cairo.png").show()
示例7: generateOverlay
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
def generateOverlay(text,
font,
showFlumotion,
showCC,
showXiph,
width, height):
"""Generate an transparent image with text + logotypes rendered on top
of it suitable for mixing into a video stream
@param text: text to put in the top left corner
@type text: str
@param font: font description used to render the text
@type: str
@param showFlumotion: if we should show the flumotion logo
@type showFlumotion: bool
@param showCC: if we should show the Creative Common logo
@type showCC: bool
@param showXiph: if we should show the xiph logo
@type showXiph: bool
@param width: width of the image to generate
@type width: int
@param height: height of the image to generate
@type height: int
@returns: raw image and if images or if text overflowed
@rtype: 3 sized tuple of string and 2 booleans
"""
from cairo import ImageSurface
from cairo import Context
image = ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = Context(image)
subImages = []
if showXiph:
subImages.append(os.path.join(configure.imagedir, '36x36', 'xiph.png'))
if showCC:
subImages.append(os.path.join(configure.imagedir, '36x36', 'cc.png'))
if showFlumotion:
subImages.append(os.path.join(configure.imagedir, '36x36',
'fluendo.png'))
imagesOverflowed = False
offsetX = BORDER
for subPath in subImages:
sub = ImageSurface.create_from_png(subPath)
subX = sub.get_width()
subY = sub.get_height()
offsetY = height - subY - BORDER
context.set_source_surface(sub, offsetX, offsetY)
context.paint()
if (offsetX + subX) > width:
imagesOverflowed = True
offsetX += subX + BORDER
textOverflowed = False
if text:
pcContext = pangocairo.CairoContext(context)
pangoLayout = pcContext.create_layout()
if font is not None:
font = pango.FontDescription(font)
if not font.get_family() or \
not font.get_family().lower() in [family.get_name().lower()
for family in pangoLayout.get_context().list_families()]:
font.set_family(FONT)
if font.get_size() == 0:
font.set_size(FONT_SIZE)
else:
font = pango.FontDescription('%s %s' % (FONT, FONT_PROPS))
pangoLayout.set_font_description(font)
context.move_to(TEXT_XOFFSET+2, TEXT_YOFFSET+2)
pangoLayout.set_markup('<span foreground="black" >%s</span>' % text)
pcContext.show_layout(pangoLayout)
context.move_to(TEXT_XOFFSET, TEXT_YOFFSET)
pangoLayout.set_markup('<span foreground="white" >%s</span>' % text)
pcContext.show_layout(pangoLayout)
textWidth, textHeight = pangoLayout.get_pixel_size()
if textWidth > width:
textOverflowed = True
if cairo.version < '1.2.6':
buf = image.get_data_as_rgba()
else:
buf = image.get_data()
return buf, imagesOverflowed, textOverflowed
示例8: make_image_surface
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
FT_LOAD_TARGET_MONO )
bitmap = face.glyph.bitmap
width = face.glyph.bitmap.width
rows = face.glyph.bitmap.rows
pitch = face.glyph.bitmap.pitch
glyph_surface = make_image_surface(face.glyph.bitmap)
surface = ImageSurface(FORMAT_ARGB32, 800, 600)
ctx = Context(surface)
ctx.rectangle(0,0,800,600)
ctx.set_line_width(0)
ctx.set_source_rgb (0.5 , 0.5, 0.5)
ctx.fill()
#
scale = 480.0 / rows
ctx.set_source_surface(glyph_surface, 0, 0)
pattern = ctx.get_source()
SurfacePattern.set_filter(pattern, FILTER_BEST)
scalematrix = Matrix()
scalematrix.scale(1.0/scale,1.0/scale)
scalematrix.translate(-(400.0 - width *scale /2.0 ), -60)
pattern.set_matrix(scalematrix)
ctx.set_source_rgb (0 , 0, 1)
ctx.mask(pattern)
ctx.fill()
surface.flush()
surface.write_to_png("glyph-mono+alpha-cairo.png")
Image.open("glyph-mono+alpha-cairo.png").show()
示例9: do_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_surface [as 别名]
def do_draw(self, context: cairo.Context) -> bool:
if not self.adjustment or self.adjustment.get_upper() <= 0:
return False
height = self.get_allocated_height()
width = self.get_allocated_width()
if width <= 0 or height <= 0:
return False
base_bg, base_outline, handle_overdraw, handle_outline = (
self.get_map_base_colors())
x0 = self.overdraw_padding + 0.5
x1 = width - 2 * x0
height_scale = height * self.get_height_scale()
if self._cached_map is None:
surface = cairo.Surface.create_similar(
context.get_target(), cairo.CONTENT_COLOR_ALPHA, width, height)
cache_ctx = cairo.Context(surface)
cache_ctx.set_line_width(1)
cache_ctx.rectangle(x0, -0.5, x1, height_scale + 0.5)
cache_ctx.set_source_rgba(*base_bg)
cache_ctx.fill()
# We get drawing coordinates by tag to minimise our source
# colour setting, and make this loop slightly cleaner.
tagged_diffs = self.chunk_coords_by_tag()
for tag, diffs in tagged_diffs.items():
cache_ctx.set_source_rgba(*self.fill_colors[tag])
for y0, y1 in diffs:
y0 = round(y0 * height_scale) + 0.5
y1 = round(y1 * height_scale) - 0.5
cache_ctx.rectangle(x0, y0, x1, y1 - y0)
cache_ctx.fill_preserve()
cache_ctx.set_source_rgba(*self.line_colors[tag])
cache_ctx.stroke()
cache_ctx.rectangle(x0, -0.5, x1, height_scale + 0.5)
cache_ctx.set_source_rgba(*base_outline)
cache_ctx.stroke()
self._cached_map = surface
context.set_source_surface(self._cached_map, 0, 0)
context.paint()
# Draw our scroll position indicator
context.set_line_width(1)
context.set_source_rgba(*handle_overdraw)
adj_y = self.adjustment.get_value() / self.adjustment.get_upper()
adj_h = self.adjustment.get_page_size() / self.adjustment.get_upper()
context.rectangle(
x0 - self.overdraw_padding, round(height_scale * adj_y) + 0.5,
x1 + 2 * self.overdraw_padding, round(height_scale * adj_h) - 1,
)
context.fill_preserve()
context.set_source_rgba(*handle_outline)
context.stroke()
return True