How to use the cairocffi.SurfacePattern function in cairocffi

To help you get started, we’ve selected a few cairocffi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Kozea / CairoSVG / cairosvg / test_api.py View on Github external
tree = parser.Tree(bytestring=SVG_SAMPLE)
    file_like = io.BytesIO()
    png_surface = surface.PNGSurface(tree, file_like, 96)
    png_surface.finish()
    assert file_like.getvalue() == expected_content

    png_result = cairo.ImageSurface.create_from_png(
        io.BytesIO(expected_content))
    expected_width = png_result.get_width()
    expected_height = png_result.get_height()

    # Abstract surface
    png_surface = surface.PNGSurface(tree, None, 96)
    assert png_surface.width == expected_width
    assert png_surface.height == expected_height
    assert cairo.SurfacePattern(png_surface.cairo)
github qtile / qtile / test / widgets / test_volume.py View on Github external
names = (
        'audio-volume-high.svg',
        'audio-volume-low.svg',
        'audio-volume-medium.svg',
        'audio-volume-muted.svg',
    )
    for name in names:
        target = tmpdir.join(name)
        svg_img_as_pypath.copy(target)

    vol = Volume(theme_path=str(tmpdir))
    vol.bar = fake_bar
    vol.setup_images()
    assert len(vol.surfaces) == len(names)
    for name, surfpat in vol.surfaces.items():
        assert isinstance(surfpat, cairocffi.SurfacePattern)
github qtile / qtile / test / test_images.py View on Github external
def test_pattern_resize(self, path_n_bytes_image_pngs):
        path, bytes_image = path_n_bytes_image_pngs
        img = images.Img.from_path(path)
        assert isinstance(img.pattern, cairocffi.SurfacePattern)
        t_matrix = img.pattern.get_matrix().as_tuple()
        assert_approx_equal(t_matrix, (1.0, 0.0, 0.0, 1.0))
        img.width = 2.0 * img.default_size.width
        t_matrix = img.pattern.get_matrix().as_tuple()
        assert_approx_equal(t_matrix, (0.5, 0.0, 0.0, 1.0))
        img.height = 3.0 * img.default_size.height
        t_matrix = img.pattern.get_matrix().as_tuple()
        assert_approx_equal(t_matrix, (0.5, 0.0, 0.0, 1.0/3.0))
github qtile / qtile / libqtile / widget / currentlayout.py View on Github external
logger.exception(
                    'Failed to load icon from file "{}", '
                    'error was: {}'.format(icon_file_path, e.message)
                )
                return

            input_width = img.get_width()
            input_height = img.get_height()

            sp = input_height / (self.bar.height - 1)

            width = input_width / sp
            if width > self.length:
                self.length = int(width) + self.actual_padding * 2

            imgpat = cairocffi.SurfacePattern(img)

            scaler = cairocffi.Matrix()

            scaler.scale(sp, sp)
            scaler.scale(self.scale, self.scale)
            factor = (1 - 1 / self.scale) / 2
            scaler.translate(-width * factor, -width * factor)
            scaler.translate(self.actual_padding * -1, 0)
            imgpat.set_matrix(scaler)

            imgpat.set_filter(cairocffi.FILTER_BEST)
            self.surfaces[layout_name] = imgpat

        self.icons_loaded = True
github qtile / qtile / libqtile / widget / launchbar.py View on Github external
self.surfaces[img_name] = textbox
                continue
            else:
                try:
                    img = cairocffi.ImageSurface.create_from_png(iconfile)
                except cairocffi.Error:
                    logger.exception('Error loading icon for application "%s" (%s)', img_name, iconfile)
                    return

            input_width = img.get_width()
            input_height = img.get_height()

            sp = input_height / (self.bar.height - 4)
            width = int(input_width / sp)

            imgpat = cairocffi.SurfacePattern(img)
            scaler = cairocffi.Matrix()
            scaler.scale(sp, sp)
            scaler.translate(self.padding * -1, -2)
            imgpat.set_matrix(scaler)

            imgpat.set_filter(cairocffi.FILTER_BEST)
            self.surfaces[img_name] = imgpat
            self.icons_widths[img_name] = width
github Kozea / WeasyPrint / weasyprint / draw.py View on Github external
else:
        assert repeat_y == 'space'
        n_repeats = floor(positioning_height / image_height)
        if n_repeats >= 2:
            repeat_height = (
                positioning_height - image_height) / (n_repeats - 1)
            position_y = 0
        else:
            repeat_height = image_height

    sub_surface = cairo.PDFSurface(None, repeat_width, repeat_height)
    sub_context = cairo.Context(sub_surface)
    sub_context.rectangle(0, 0, image_width, image_height)
    sub_context.clip()
    layer.image.draw(sub_context, image_width, image_height, image_rendering)
    pattern = cairo.SurfacePattern(sub_surface)

    if repeat_x == repeat_y == 'no-repeat':
        pattern.set_extend(cairo.EXTEND_NONE)
    else:
        pattern.set_extend(cairo.EXTEND_REPEAT)

    with stacked(context):
        if not layer.unbounded:
            context.rectangle(painting_x, painting_y,
                              painting_width, painting_height)
            context.clip()
        # else: unrestricted, whole page box

        context.translate(positioning_x + position_x,
                          positioning_y + position_y)
        context.set_source(pattern)
github Zulko / gizeh / gizeh / gizeh.py View on Github external
def make_cairo_pattern(self):
        pat = cairo.SurfacePattern(self._cairo_surface)
        pat.set_filter({"best": cairo.FILTER_BEST,
                        "nearest": cairo.FILTER_NEAREST,
                        "gaussian": cairo.FILTER_GAUSSIAN,
                        "good": cairo.FILTER_GOOD,
                        "bilinear": cairo.FILTER_BILINEAR,
                        "fast": cairo.FILTER_FAST}[self.filter])

        pat.set_extend({"none": cairo.EXTEND_NONE,
                        "repeat": cairo.EXTEND_REPEAT,
                        "reflect": cairo.EXTEND_REFLECT,
                        "pad": cairo.EXTEND_PAD}[self.extend])

        pat.set_matrix(self._cairo_matrix())

        return pat