示例#1
0
def render_record_surface():
    # image_surface = cairo.SVGSurface(None, HEIGHT, WIDTH)
    x_start, y_start, width_actual, height_actual = surface.ink_extents()
    logger.debug(
        f"x start={x_start}, y start={y_start}, width actual={width_actual}, height_actual={height_actual}"
    )
    # print(x_start, y_start, width_actual, height_actual)
    # shrink and translate to match specified width and height
    image_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    context = cairo.Context(image_surface)
    scale = min(WIDTH / width_actual, HEIGHT / height_actual)
    if _background_color is not None:
        logger.info("filling background_color")
        source = context.get_source()
        pat = cairo.SolidPattern(*htmlcolor_to_rgb(_background_color))
        context.rectangle(0, 0, WIDTH, HEIGHT)  # Rectangle(x0, y0, x1, y1)
        context.set_source(pat)
        context.fill()
        context.set_source(source)
    context.translate(WIDTH / 2, HEIGHT / 2)
    context.scale(scale, -scale)  # Normalizing the canvas
    context.translate(-x_start - width_actual / 2,
                      -y_start - height_actual / 2)
    context.set_source_surface(surface, 0, 0)
    context.paint()
    return image_surface
示例#2
0
def initialize():
    """
    Setup context with WIDTH and HEIGHT and background color
    """
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)

    pat = cairo.SolidPattern(1, 1, 1, 1)  # White

    ctx.rectangle(0, 0, WIDTH, HEIGHT)  # Rectangle(x0, y0, x1, y1)
    ctx.set_source(pat)
    ctx.fill()

    return surface, ctx
示例#3
0
def fade_pattern(x0, y0, x1, y1, r, g, b, a, fade):
    pat = cairo.LinearGradient(x0, y0, x1, y1)
    if fade == 'None':
        return cairo.SolidPattern(r, g, b, a)
    elif fade == 'in':
        pat.add_color_stop_rgba(0, r, g, b, 0)
        pat.add_color_stop_rgba(1, r, g, b, a)
    elif fade == 'out':
        pat.add_color_stop_rgba(0, r, g, b, a)
        pat.add_color_stop_rgba(1, r, g, b, 0)
    elif fade == 'both':
        pat.add_color_stop_rgba(0, r, g, b, 0)
        pat.add_color_stop_rgba(0.5, r, g, b, a)
        pat.add_color_stop_rgba(1, r, g, b, 0)
    else:
        sys.stderr.write(f"Unknown fade style {fade}!\n")
    return pat
示例#4
0
def test_rounded_rectangle_apply(cr):
    w, h = 200, 200
    s = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
    c = cairo.Context(s)

    # TODO: This DOES NOT work with the code below...
    if False:
        c.save()

        p = cairo.RadialGradient(w / 2.0, h / 2.0, 0.0, w / 2.0, h / 2.0,
                                 200.0)

        p.add_color_stop_rgba(0.0, 0.8, 0.4, 0.0, 0.0)
        p.add_color_stop_rgba(1.0, 0.8, 0.4, 0.0, 1.0)

        c.arc(w / 2.0, h / 2.0, 200.0, 0.0, 2.0 * math.pi)
        c.set_source(p)
        c.paint()
        c.restore()

    # TODO: This DOES work with the code below...
    if True:
        c.set_source_rgb(0.0, 1.0, 0.5)
        c.paint()

    # cairocks.rounded_rectangle_apply(c, 20.0, 20.0, 160.0, 160.0, 30.0)

    # TODO: The code below does the exact same thing as the call
    # to rounded_rectangle_apply.
    p2 = cairo.SolidPattern(0.0, 0.0, 0.0)

    c.save()
    c.set_operator(cairo.OPERATOR_DEST_IN)
    c.set_source(p2)

    cairocks.rounded_rectangle(c, 20.0, 20.0, 160.0, 160.0, 30.0)

    c.fill()
    c.restore()

    cr.set_source_surface(s, 400, 200)
    cr.paint()
示例#5
0
#!/usr/bin/env python

import cairocffi as cairo

WIDTH, HEIGHT = 512, 512

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas

pat = cairo.SolidPattern(1, 1, 1, 1) # White

ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
ctx.set_source (pat)
ctx.fill ()

ctx.translate (0.1, 0.1) # Changing the current transformation matrix

end = 0.8
start_step = 0
ctx.move_to(0, start_step)
ctx.line_to(end, start_step)  # Line to (x,y)

def cantor(step, start, line_len):
    step += 0.05
    if step > 0.5: return
    # Draw two lines
    # Line one
    ctx.move_to(start, step)
    ctx.line_to(start + line_len / 3, step)  # Line to (x,y)
示例#6
0
    def render(self, filename=None):
        # render the image to png. returns bytes if f is None, otherwise it calls f.write()

        if self.width == 256 and self.height == 256 and not self.g:
            # create empty tile png with minimal size, indexed color palette with only one entry
            plte = b'PLTE' + bytearray(
                tuple(int(i * 255) for i in self.background_rgb))
            return (
                b'\x89PNG\r\n\x1a\n' +
                b'\x00\x00\x00\rIHDR\x00\x00\x01\x00\x00\x00\x01\x00\x01\x03\x00\x00\x00f\xbc:%\x00\x00\x00\x03'
                + plte + zlib.crc32(plte).to_bytes(4, byteorder='big') +
                b'\x00\x00\x00\x1fIDATh\xde\xed\xc1\x01\r\x00\x00\x00\xc2\xa0\xf7Om\x0e7\xa0\x00\x00\x00\x00\x00'
                +
                b'\x00\x00\x00\xbe\r!\x00\x00\x01\x7f\x19\x9c\xa7\x00\x00\x00\x00IEND\xaeB`\x82'
            )

        if settings.SVG_RENDERER == 'rsvg':
            # create buffered surfaces
            buffered_surface = cairocffi.SVGSurface(None, self.buffered_width,
                                                    self.buffered_height)
            buffered_context = cairocffi.Context(buffered_surface)

            # draw svg with rsvg
            handle = Rsvg.Handle()
            svg = handle.new_from_data(self.get_xml(buffer=True).encode())
            svg.render_cairo(buffered_context)

            # create cropped image
            surface = buffered_surface.create_similar(cairocffi.CONTENT_COLOR,
                                                      self.width, self.height)
            context = cairocffi.Context(surface)

            # set background color
            context.set_source(cairocffi.SolidPattern(*self.background_rgb))
            context.paint()

            # paste buffered immage with offset
            context.set_source_surface(buffered_surface, -self.buffer,
                                       -self.buffer)
            context.paint()

            return surface.write_to_png()

        elif settings.SVG_RENDERER == 'rsvg-convert':
            p = subprocess.run(
                ('rsvg-convert', '-b', self.background, '--format', 'png'),
                input=self.get_xml(buffer=True).encode(),
                stdout=subprocess.PIPE,
                check=True)
            png = io.BytesIO(p.stdout)
            img = Image.open(png)
            img = img.crop((self.buffer, self.buffer, self.buffer + self.width,
                            self.buffer + self.height))

            f = io.BytesIO()
            img.save(f, 'PNG')
            f.seek(0)
            return f.read()

        elif settings.SVG_RENDERER == 'inkscape':
            p = subprocess.run(('inkscape', '-z', '-b', self.background, '-e',
                                '/dev/stderr', '/dev/stdin'),
                               input=self.get_xml().encode(),
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               check=True)
            png = p.stderr[p.stderr.index(b'\x89PNG'):]
            return png