readPicture: Import a Cairo SVG image

View source: R/import.R

readPictureR Documentation

Import a Cairo SVG image

Description

This function reads in an SVG image file and creates a "Picture" object.

Usage

readPicture(file, warn = TRUE, initDefs = TRUE)

Arguments

file

The filename of a Cairo SVG image.

warn

Logical. If TRUE, this will warn if file was not generated by Cairo graphics (see Details).

initDefs

Logical. If TRUE, the global list of definitions (for things like masks, filters, gradient fills, ...) is initialised.

Details

This function is designed to read SVG files that have been generated by the Cairo graphics system.

There are several ways to generate Cairo SVG files: The R SVG graphics device, provided by the svg function, produces Cairo SVG; the grConvert package (Linux-only) can convert PostScript, or PDF, or SVG files to Cairo SVG; and the rsvg package can convert many different graphics formats to Cairo SVG.

It is very unlikely that an SVG file that was NOT generated by Cairo will import properly. This function may read the file without error, but the render (via grid.picture) is very unlikely to faithfully reproduce the original image.

Value

An object of class "Picture".

Author(s)

Simon Potter

References

The Cairo graphics library, https://cairographics.org/

Examples

options(warn=1)

## NOT a Cairo SVG file
badfile <- system.file("SVG", "lwd.svg", package="grImport2")
## A Cairo SVG file
goodfile <- system.file("SVG", "lwd-rsvg.svg", package="grImport2")

## Warning because NOT a Cairo SVG file
## (and it will not render correctly)
img <- readPicture(badfile)

## No warning
## (and it will render correctly)
img <- readPicture(goodfile)

if (require("rsvg")) {
    ## Generate a Cairo SVG file
    goodfile <- tempfile(fileext = ".svg")
    rsvg_svg(badfile, goodfile)

    ## No warning
    ## (and it will render correctly)
    img <- readPicture(goodfile)

}

options(warn=0)

grImport2 documentation built on Oct. 27, 2023, 9:07 a.m.