Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
OUTPUT_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"test_output"
)
if __name__ == "__main__":
for test in tests.common.TESTS:
output = "Running test: %s" % test.name
if test.description:
output += " (%s)" % test.description
print(output)
surface = cairo.ImageSurface(
test.surface_format,
test.width,
test.height
)
cr = cairo.Context(surface)
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
cr.paint()
cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
try:
test(cr, test.width, test.height)
except Exception as e:
print("Error in test %s: %s" % (test.name, e))
def test_context_mask():
mask_surface = ImageSurface(cairocffi.FORMAT_ARGB32, 2, 2)
context = Context(mask_surface)
context.set_source_rgba(1, 0, .5, 1)
context.rectangle(0, 0, 1, 1)
context.fill()
context.set_source_rgba(1, .5, 1, .5)
context.rectangle(1, 1, 1, 1)
context.fill()
surface = ImageSurface(cairocffi.FORMAT_ARGB32, 4, 4)
context = Context(surface)
context.mask(SurfacePattern(mask_surface))
o = pixel(b'\x00\x00\x00\x00')
b = pixel(b'\x80\x00\x00\x00')
B = pixel(b'\xFF\x00\x00\x00')
assert surface.get_data()[:] == (
B + o + o + o +
def test_context_fill():
surface = ImageSurface(cairocffi.FORMAT_A8, 4, 4)
assert surface.get_data()[:] == b'\x00' * 16
context = Context(surface)
context.set_source_rgba(0, 0, 0, .5)
context.set_line_width(.5)
context.rectangle(1, 1, 2, 2)
assert context.fill_extents() == (1, 1, 3, 3)
assert context.stroke_extents() == (.75, .75, 3.25, 3.25)
assert context.in_fill(2, 2) is True
assert context.in_fill(.8, 2) is False
assert context.in_stroke(2, 2) is False
assert context.in_stroke(.8, 2) is True
path = list(context.copy_path())
assert path
context.fill_preserve()
assert list(context.copy_path()) == path
assert surface.get_data()[:] == (
def create_arc(w, h):
surface = cairo.ImageSurface(cairo.FORMAT_A8, w, h)
cr = cairo.Context(surface)
cr.arc(w / 2, h / 2, w / 3.0, 0.0, 2.0 * math.pi)
cr.fill()
return surface
offset = cairo_stride * y
end = offset + cairo_row_length
if big_endian: # pragma: no cover
data[offset:end:4] = alpha
data[offset + 1:end:4] = red
data[offset + 2:end:4] = green
data[offset + 3:end:4] = blue
else:
data[offset + 3:end:4] = alpha
data[offset + 2:end:4] = red
data[offset + 1:end:4] = green
data[offset:end:4] = blue
data = array('B', data)
return ImageSurface(constants.FORMAT_RGB24,
width, height, data, cairo_stride)
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in Centos 7
if multi_fonts:
fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
context.set_font_size(25)
box = context.text_extents(text)
border_w_h = (4, 4)
if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
raise IOError('Could not fit string into image. Max char count is too large for given image width.')
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
# this font list works in CentOS 7
if multi_fonts:
fonts = [
'Century Schoolbook', 'Courier', 'STIX',
'URW Chancery L', 'FreeMono']
context.select_font_face(
np.random.choice(fonts),
cairo.FONT_SLANT_NORMAL,
np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
else:
context.select_font_face('Courier',
cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_BOLD)
def paint_text(text, w, h, rotate=False, move=False, multi_fonts=False, background=False):
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
with cairo.Context(surface) as context:
context.set_source_rgb(1, 1, 1) # White
context.paint()
if multi_fonts:
# Calibri Century Comic Sans Courier New Futura Georgia
fonts = ['Century Schoolbook', 'Courier', 'Arial', 'STIX','Tahoma','Times New Roman','Trebuchet MS',
'Verdana','Wide Latin','Calibri','Century','Comic Sans','Courier','New Futura','Georgia',
'Lucida','Lucida Console','Magneto','Mistral','URW Chancery L', 'FreeMono','DejaVue Sans Mono']
font_slant = np.random.choice([cairo.FONT_SLANT_NORMAL,cairo.FONT_SLANT_ITALIC,cairo.FONT_SLANT_OBLIQUE])
font_weight = np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL, cairo.FONT_WEIGHT_NORMAL])
context.select_font_face(np.random.choice(fonts), font_slant, font_weight)
else:
context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
# context.set_font_size(25)
font_size = randint(12, 42)
context.set_font_size(font_size)
result += [(1, R, C, A), (1, Q, R, B), (0, R, Q, A)]
return result
triangles = []
for i in range(10):
B = np.exp((2*i - 1) * np.pi * 1j / 10)
C = np.exp((2*i + 1) * np.pi * 1j / 10)
if i % 2 == 0:
B, C = C, B
triangles.append((0, 0j, B, C))
for i in range(NUM_SUBDIVISIONS):
triangles = subdivide(triangles)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
ctx.translate(WIDTH / 2.0, HEIGHT / 2.0)
extent = max(WIDTH, HEIGHT) / 1.5
ctx.scale(extent, extent)
color, A, B, C = triangles[0]
ctx.set_line_width(abs(B - A) / 10.0)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
for color, A, B, C in triangles:
D = B + C - A
ctx.move_to(A.real, A.imag)
ctx.line_to(B.real, B.imag)
ctx.line_to(D.real, D.imag)
ctx.line_to(C.real, C.imag)
ctx.close_path()
def empty_surface(width, height, is_white=False):
data = None if not is_white else getbuffer(all_white(width, height).reshape(-1))
return cairocffi.ImageSurface(cairocffi.FORMAT_ARGB32,
width,
height,
data=data)