How to use the cairosvg.surface function in CairoSVG

To help you get started, we’ve selected a few CairoSVG 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 / WeasyPrint / weasyprint / images.py View on Github external
def get_intrinsic_size(self, _image_resolution, font_size):
        # Vector images may be affected by the font size.
        fake_surface = FakeSurface()
        fake_surface.font_size = font_size
        # Percentages don't provide an intrinsic size, we transform percentages
        # into 0 using a (0, 0) context size:
        # http://www.w3.org/TR/SVG/coords.html#IntrinsicSizing
        self._width = cairosvg.surface.size(
            fake_surface, self._tree.get('width'))
        self._height = cairosvg.surface.size(
            fake_surface, self._tree.get('height'))
        _, _, viewbox = cairosvg.surface.node_format(fake_surface, self._tree)
        self._intrinsic_width = self._width or None
        self._intrinsic_height = self._height or None
        self.intrinsic_ratio = None
        if viewbox:
            if self._width and self._height:
                self.intrinsic_ratio = self._width / self._height
            else:
                if viewbox[2] and viewbox[3]:
                    self.intrinsic_ratio = viewbox[2] / viewbox[3]
                    if self._width:
                        self._intrinsic_height = (
                            self._width / self.intrinsic_ratio)
                    elif self._height:
                        self._intrinsic_width = (
github Kozea / CairoSVG / cairosvg / surface_type.py View on Github external
def _create_surface(self, tree):
        width, height, viewbox = surface.node_format(tree)

        # The image size has integer width and height
        self._width, self._height = int(width), int(height)
        self.cairo = cairo.ImageSurface(
            cairo.FORMAT_ARGB32, self._width, self._height)

        # The context size has floating width and height
        self.context = cairo.Context(self.cairo)
        self._set_context_size(width, height, viewbox)
        self.context.move_to(0, 0)
github Kozea / CairoSVG / cairosvg / __init__.py View on Github external
else:
        # Frozen with something else (py2exe, etc.)
        # See https://github.com/Kozea/WeasyPrint/pull/269
        ROOT = Path(os.path.dirname(sys.executable))
else:
    ROOT = Path(os.path.dirname(__file__))

VERSION = __version__ = (ROOT / 'VERSION').read_text().strip()


# VERSION is used in the "url" module imported by "surface"
from . import surface  # noqa isort:skip


SURFACES = {
    'PDF': surface.PDFSurface,
    'PNG': surface.PNGSurface,
    'PS': surface.PSSurface,
    'SVG': surface.SVGSurface,
}


def svg2svg(bytestring=None, *, file_obj=None, url=None, dpi=96,
            parent_width=None, parent_height=None, scale=1, unsafe=False,
            background_color=None, negate_colors=False, invert_images=False,
            write_to=None, output_width=None, output_height=None):
    return surface.SVGSurface.convert(
        bytestring=bytestring, file_obj=file_obj, url=url, dpi=dpi,
        parent_width=parent_width, parent_height=parent_height, scale=scale,
        background_color=background_color,
        negate_colors=negate_colors, invert_images=invert_images,
        unsafe=unsafe, write_to=write_to, output_width=output_width,
github Kozea / WeasyPrint / weasyprint / images.py View on Github external
from xml.etree import ElementTree

import cairocffi
import cairosvg.parser
import cairosvg.surface

from .layout.percentages import percentage
from .logger import LOGGER
from .urls import URLFetchingError, fetch

try:
    from cairocffi import pixbuf
except OSError:
    pixbuf = None

assert cairosvg.surface.cairo is cairocffi, (
    'CairoSVG is using pycairo instead of cairocffi. '
    'Make sure it is not imported before WeasyPrint.')


# Map values of the image-rendering property to cairo FILTER values:
# Values are normalized to lower case.
IMAGE_RENDERING_TO_FILTER = {
    'auto': cairocffi.FILTER_BILINEAR,
    'crisp-edges': cairocffi.FILTER_BEST,
    'pixelated': cairocffi.FILTER_NEAREST,
}


class ImageLoadingError(ValueError):
    """An error occured when loading an image.