How to use the cairocffi.SVGSurface 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 / cairocffi / cairocffi / test_cairo.py View on Github external
def test_document_unit():
    surface = SVGSurface(None, 1, 2)
    assert surface.get_document_unit() == SVG_UNIT_PT

    file_obj = io.BytesIO()
    surface = SVGSurface(file_obj, 1, 2)
    surface.set_document_unit(SVG_UNIT_PX)
    assert surface.get_document_unit() == SVG_UNIT_PX
    surface.finish()
    pdf_bytes = file_obj.getvalue()
    assert b'width="1px"' in pdf_bytes
    assert b'height="2px"' in pdf_bytes

    file_obj = io.BytesIO()
    surface = SVGSurface(file_obj, 1, 2)
    surface.set_document_unit(SVG_UNIT_PC)
    assert surface.get_document_unit() == SVG_UNIT_PC
    surface.finish()
github Kozea / cairocffi / cairocffi / test_cairo.py View on Github external
def test_svg_surface():
    assert set(SVGSurface.get_versions()) >= set([
        cairocffi.SVG_VERSION_1_1, cairocffi.SVG_VERSION_1_2])
    assert SVGSurface.version_to_string(cairocffi.SVG_VERSION_1_1) == 'SVG 1.1'
    with pytest.raises(TypeError):
        SVGSurface.version_to_string('SVG_VERSION_42')
    with pytest.raises(ValueError):
        SVGSurface.version_to_string(42)

    with temp_directory() as tempdir:
        filename = os.path.join(tempdir, 'foo.svg')
        filename_bytes = filename.encode(sys.getfilesystemencoding())
        file_obj = io.BytesIO()
        for target in [filename, filename_bytes, file_obj, None]:
            SVGSurface(target, 123, 432).finish()
        with open(filename, 'rb') as fd:
            assert fd.read().startswith(b'
github Kozea / cairocffi / cairocffi / test_cairo.py View on Github external
with temp_directory() as tempdir:
        filename = os.path.join(tempdir, 'foo.svg')
        filename_bytes = filename.encode(sys.getfilesystemencoding())
        file_obj = io.BytesIO()
        for target in [filename, filename_bytes, file_obj, None]:
            SVGSurface(target, 123, 432).finish()
        with open(filename, 'rb') as fd:
            assert fd.read().startswith(b'
github rdkit / rdkit / rdkit / Chem / Draw / cairoCanvas.py View on Github external
r, g, b, a = image.split()
                mrg = Image.merge("RGBA", (b, g, r, a))
                imgd = getattr(mrg, 'tobytes', mrg.tostring)("raw", "RGBA")

            a = array.array('B', imgd)
            stride = image.size[0] * 4
            surface = cairo.ImageSurface.create_for_data(a, cairo.FORMAT_ARGB32, image.size[0],
                                                         image.size[1], stride)
            ctx = cairo.Context(surface)
            size = image.size[0], image.size[1]
            self.image = image
        elif ctx is None and size is not None:
            if hasattr(cairo, "PDFSurface") and imageType == "pdf":
                surface = cairo.PDFSurface(fileName, size[0], size[1])
            elif hasattr(cairo, "SVGSurface") and imageType == "svg":
                surface = cairo.SVGSurface(fileName, size[0], size[1])
            elif hasattr(cairo, "PSSurface") and imageType == "ps":
                surface = cairo.PSSurface(fileName, size[0], size[1])
            elif imageType == "png":
                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, size[0], size[1])
            else:
                raise ValueError("Unrecognized file type. Valid choices are pdf, svg, ps, and png")
            ctx = cairo.Context(surface)
            ctx.set_source_rgb(1, 1, 1)
            ctx.paint()
        else:
            surface = ctx.get_target()
            if size is None:
                try:
                    size = surface.get_width(), surface.get_height()
                except AttributeError:
                    size = None
github neozhaoliang / pywonderland / src / uniform-tilings / tiling.py View on Github external
def render(self,
               output,
               image_width,
               image_height,
               extent=30,
               line_width=0.2,
               show_vertices_labels=False,
               face_colors=("thistle", "steelblue", "lightcoral")):
        print("=" * 40)
        print(self.get_info())

        surface = cairo.SVGSurface(output, image_width, image_height)
        ctx = cairo.Context(surface)
        ctx.scale(image_height / extent, -image_height / extent)
        ctx.translate(extent / 2, -extent / 2)
        ctx.scale(1, -1)
        ctx.set_source_rgb(0, 0, 0)
        ctx.paint()
        ctx.set_line_width(line_width)
        ctx.set_line_cap(cairo.LINE_CAP_ROUND)
        ctx.set_line_join(cairo.LINE_JOIN_ROUND)

        bar = tqdm.tqdm(desc="processing polygons", total=self.num_faces)
        for (i, j), flist in self.face_indices.items():
            color1 = Color(face_colors[self.vertex_at_mirrors(i, j)])
            color2 = dimmed(color1)
            for face in flist:
                domain1, domain2 = face.get_alternative_domains()
github LaurentCabaret / pyVhdl2Sch / decorator / pdfdrawer.py View on Github external
def __init__(self, filename, entity, options):
        self.color = (0, 0, 0)
        self.factor = 1
        self.background_color = (0, 0, 0)
        self.analyse_options(options)

        self.surface = cairo.SVGSurface(filename, 10, 10)
        self.context = cairo.Context(self.surface)

        self.factor = 1
        self.height = self.compute_height(entity)
        self.width = self.compute_width(entity)
        self.line_length = default_line_length

        self.surface = cairo.PDFSurface(
            filename, self.width + self.line_length * 2 + bbox_w_margin * 2, self.height + bbox_h_margin * 2)

        self.context = cairo.Context(self.surface)
        self.compute_wire_length(entity)

        if options.format.lower() == "svg":
            self.factor = 1
            self.surface = cairo.SVGSurface(
github buildbot / buildbot / www / badges / buildbot_badges / __init__.py View on Github external
def textwidth(self, text, config):
        """Calculates the width of the specified text.
        """
        surface = cairo.SVGSurface(None, 1280, 200)
        ctx = cairo.Context(surface)
        ctx.select_font_face(config['font_face'],
                             cairo.FONT_SLANT_NORMAL,
                             cairo.FONT_WEIGHT_BOLD)
        ctx.set_font_size(int(config['font_size']))
        return ctx.text_extents(text)[2] + 2
github cytan299 / tribahtinov / python / generate.py View on Github external
#
# Now put the entire surface onto a white background
#
cr.set_operator(cairo.OPERATOR_DEST_OVER);
cr.set_source_rgb(1,1,1);
cr.paint();

#
# Also draw out a png file
#
surface.write_to_png('tribahtinov.png')
#
# and an svg file
#
svg_surface = cairo.SVGSurface('tribahtinov.svg', PAPER_MAX_X, PAPER_MAX_Y);
cr_svg = cairo.Context(svg_surface);
cr_svg.set_source_surface(surface, 0, 0);
cr_svg.paint();
github neozhaoliang / pywonderland / src / aperiodic-tilings / ammann-beenker.py View on Github external
R = C + (B - C) * BETA
            S = A + (C - A) * ALPHA
            T = P + Q - B

            sqU = (1, (T, A, P))
            sqDL = (1, (T, R, Q))
            sqDR = (1, (R, C, S))
            lozU = (0, (A, T, R, S))
            lozD = (0, (T, P, B, Q))

            result += [lozU, lozD, sqU, sqDL, sqDR]

    return result


surface = cairo.SVGSurface("Ammann-Beenker.svg", IMAGE_SIZE[0], IMAGE_SIZE[1])
ctx = cairo.Context(surface)
ctx.translate(IMAGE_SIZE[0] / 2.0, IMAGE_SIZE[1] / 2.0)
wheel_radius = math.sqrt(IMAGE_SIZE[0] ** 2 + IMAGE_SIZE[1] ** 2) / SQRT2
ctx.scale(wheel_radius, wheel_radius)

tiles = []
for i in range(8):
    A = 0j
    B = cmath.rect(1, i * PI4)
    D = cmath.rect(1, (i + 1) * PI4)
    C = B + D
    tiles.append((0, (A, B, C, D)))

for i in range(8):
    C = cmath.rect(1, i * PI4)
    B = (1 + math.sqrt(2)) * C